From 66d266f97185020fe80f4b573411c39fc354fc91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn=20Nieto?= Date: Tue, 30 Jun 2015 19:03:52 +0200 Subject: [PATCH] Repository: move to use an actual constructor This should further reduce the changes of the creation of the object going badly. --- repository.go | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/repository.go b/repository.go index 5a2b815..2e2e897 100644 --- a/repository.go +++ b/repository.go @@ -29,81 +29,78 @@ type Repository struct { Notes NoteCollection } -func initRepositoryObject(repo *Repository) { +func newRepositoryFromC(ptr *C.git_repository) *Repository { + repo := &Repository{ptr: ptr} + repo.Remotes.repo = repo repo.Submodules.repo = repo repo.References.repo = repo repo.Notes.repo = repo + runtime.SetFinalizer(repo, (*Repository).Free) + + return repo } func OpenRepository(path string) (*Repository, error) { - repo := new(Repository) - cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_repository_open(&repo.ptr, cpath) + var ptr *C.git_repository + ret := C.git_repository_open(&ptr, cpath) if ret < 0 { return nil, MakeGitError(ret) } - initRepositoryObject(repo) - return repo, nil + return newRepositoryFromC(ptr), nil } func OpenRepositoryExtended(path string) (*Repository, error) { - repo := new(Repository) - cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_repository_open_ext(&repo.ptr, cpath, 0, nil) + var ptr *C.git_repository + ret := C.git_repository_open_ext(&ptr, cpath, 0, nil) if ret < 0 { return nil, MakeGitError(ret) } - initRepositoryObject(repo) - return repo, nil + return newRepositoryFromC(ptr), nil } func InitRepository(path string, isbare bool) (*Repository, error) { - repo := new(Repository) - cpath := C.CString(path) defer C.free(unsafe.Pointer(cpath)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_repository_init(&repo.ptr, cpath, ucbool(isbare)) + var ptr *C.git_repository + ret := C.git_repository_init(&ptr, cpath, ucbool(isbare)) if ret < 0 { return nil, MakeGitError(ret) } - initRepositoryObject(repo) - return repo, nil + return newRepositoryFromC(ptr), nil } func NewRepositoryWrapOdb(odb *Odb) (repo *Repository, err error) { - repo = new(Repository) - runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_repository_wrap_odb(&repo.ptr, odb.ptr) + var ptr *C.git_repository + ret := C.git_repository_wrap_odb(&ptr, odb.ptr) if ret < 0 { return nil, MakeGitError(ret) } - initRepositoryObject(repo) - return repo, nil + return newRepositoryFromC(ptr), nil } func (v *Repository) SetRefdb(refdb *Refdb) {