Repository: move to use an actual constructor

This should further reduce the changes of the creation of the object
going badly.
This commit is contained in:
Carlos Martín Nieto 2015-06-30 19:03:52 +02:00
parent 0ce52d9aeb
commit 66d266f971
1 changed files with 18 additions and 21 deletions

View File

@ -29,81 +29,78 @@ type Repository struct {
Notes NoteCollection Notes NoteCollection
} }
func initRepositoryObject(repo *Repository) { func newRepositoryFromC(ptr *C.git_repository) *Repository {
repo := &Repository{ptr: ptr}
repo.Remotes.repo = repo repo.Remotes.repo = repo
repo.Submodules.repo = repo repo.Submodules.repo = repo
repo.References.repo = repo repo.References.repo = repo
repo.Notes.repo = repo repo.Notes.repo = repo
runtime.SetFinalizer(repo, (*Repository).Free) runtime.SetFinalizer(repo, (*Repository).Free)
return repo
} }
func OpenRepository(path string) (*Repository, error) { func OpenRepository(path string) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path) cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() 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 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
initRepositoryObject(repo) return newRepositoryFromC(ptr), nil
return repo, nil
} }
func OpenRepositoryExtended(path string) (*Repository, error) { func OpenRepositoryExtended(path string) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path) cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() 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 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
initRepositoryObject(repo) return newRepositoryFromC(ptr), nil
return repo, nil
} }
func InitRepository(path string, isbare bool) (*Repository, error) { func InitRepository(path string, isbare bool) (*Repository, error) {
repo := new(Repository)
cpath := C.CString(path) cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cpath))
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() 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 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
initRepositoryObject(repo) return newRepositoryFromC(ptr), nil
return repo, nil
} }
func NewRepositoryWrapOdb(odb *Odb) (repo *Repository, err error) { func NewRepositoryWrapOdb(odb *Odb) (repo *Repository, err error) {
repo = new(Repository)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() 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 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
initRepositoryObject(repo) return newRepositoryFromC(ptr), nil
return repo, nil
} }
func (v *Repository) SetRefdb(refdb *Refdb) { func (v *Repository) SetRefdb(refdb *Refdb) {