Create a RemoteCollection for managing remotes

Instead of making the 'Remote' part of the function calls, create a
collection object which serves to namespace the operations for the
remotes.
This commit is contained in:
Carlos Martín Nieto 2015-06-28 00:51:17 +02:00
parent ba0a24087a
commit 4b9cbd78fd
4 changed files with 49 additions and 36 deletions

View File

@ -11,7 +11,7 @@ func TestRemotePush(t *testing.T) {
localRepo := createTestRepo(t) localRepo := createTestRepo(t)
defer cleanupTestRepo(t, localRepo) defer cleanupTestRepo(t, localRepo)
remote, err := localRepo.CreateRemote("test_push", repo.Path()) remote, err := localRepo.Remotes.Create("test_push", repo.Path())
checkFatal(t, err) checkFatal(t, err)
seedTestRepo(t, localRepo) seedTestRepo(t, localRepo)

View File

@ -317,13 +317,17 @@ func (r *Remote) Free() {
C.git_remote_free(r.ptr) C.git_remote_free(r.ptr)
} }
func (repo *Repository) ListRemotes() ([]string, error) { type RemoteCollection struct {
repo *Repository
}
func (c *RemoteCollection) List() ([]string, error) {
var r C.git_strarray var r C.git_strarray
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ecode := C.git_remote_list(&r, repo.ptr) ecode := C.git_remote_list(&r, c.repo.ptr)
if ecode < 0 { if ecode < 0 {
return nil, MakeGitError(ecode) return nil, MakeGitError(ecode)
} }
@ -333,7 +337,7 @@ func (repo *Repository) ListRemotes() ([]string, error) {
return remotes, nil return remotes, nil
} }
func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) { func (c *RemoteCollection) Create(name string, url string) (*Remote, error) {
remote := &Remote{} remote := &Remote{}
cname := C.CString(name) cname := C.CString(name)
@ -344,7 +348,7 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_create(&remote.ptr, repo.ptr, cname, curl) ret := C.git_remote_create(&remote.ptr, c.repo.ptr, cname, curl)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
@ -352,21 +356,21 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) {
return remote, nil return remote, nil
} }
func (repo *Repository) DeleteRemote(name string) error { func (c *RemoteCollection) Delete(name string) error {
cname := C.CString(name) cname := C.CString(name)
defer C.free(unsafe.Pointer(cname)) defer C.free(unsafe.Pointer(cname))
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_delete(repo.ptr, cname) ret := C.git_remote_delete(c.repo.ptr, cname)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }
return nil return nil
} }
func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) { func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch string) (*Remote, error) {
remote := &Remote{} remote := &Remote{}
cname := C.CString(name) cname := C.CString(name)
@ -379,7 +383,7 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_create_with_fetchspec(&remote.ptr, repo.ptr, cname, curl, cfetch) ret := C.git_remote_create_with_fetchspec(&remote.ptr, c.repo.ptr, cname, curl, cfetch)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
@ -387,7 +391,7 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch
return remote, nil return remote, nil
} }
func (repo *Repository) CreateAnonymousRemote(url string) (*Remote, error) { func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) {
remote := &Remote{} remote := &Remote{}
curl := C.CString(url) curl := C.CString(url)
@ -396,7 +400,7 @@ func (repo *Repository) CreateAnonymousRemote(url string) (*Remote, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_create_anonymous(&remote.ptr, repo.ptr, curl) ret := C.git_remote_create_anonymous(&remote.ptr, c.repo.ptr, curl)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
@ -404,7 +408,7 @@ func (repo *Repository) CreateAnonymousRemote(url string) (*Remote, error) {
return remote, nil return remote, nil
} }
func (repo *Repository) LookupRemote(name string) (*Remote, error) { func (c *RemoteCollection) Lookup(name string) (*Remote, error) {
remote := &Remote{} remote := &Remote{}
cname := C.CString(name) cname := C.CString(name)
@ -413,7 +417,7 @@ func (repo *Repository) LookupRemote(name string) (*Remote, error) {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_lookup(&remote.ptr, repo.ptr, cname) ret := C.git_remote_lookup(&remote.ptr, c.repo.ptr, cname)
if ret < 0 { if ret < 0 {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
@ -433,7 +437,7 @@ func (o *Remote) PushUrl() string {
return C.GoString(C.git_remote_pushurl(o.ptr)) return C.GoString(C.git_remote_pushurl(o.ptr))
} }
func (o *Repository) RemoteSetUrl(remote, url string) error { func (c *RemoteCollection) SetUrl(remote, url string) error {
curl := C.CString(url) curl := C.CString(url)
defer C.free(unsafe.Pointer(curl)) defer C.free(unsafe.Pointer(curl))
cremote := C.CString(remote) cremote := C.CString(remote)
@ -442,14 +446,14 @@ func (o *Repository) RemoteSetUrl(remote, url string) error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_set_url(o.ptr, cremote, curl) ret := C.git_remote_set_url(c.repo.ptr, cremote, curl)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }
return nil return nil
} }
func (o *Repository) RemoteSetPushUrl(remote, url string) error { func (c *RemoteCollection) SetPushUrl(remote, url string) error {
curl := C.CString(url) curl := C.CString(url)
defer C.free(unsafe.Pointer(curl)) defer C.free(unsafe.Pointer(curl))
cremote := C.CString(remote) cremote := C.CString(remote)
@ -458,14 +462,14 @@ func (o *Repository) RemoteSetPushUrl(remote, url string) error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_set_pushurl(o.ptr, cremote, curl) ret := C.git_remote_set_pushurl(c.repo.ptr, cremote, curl)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }
return nil return nil
} }
func (o *Repository) RemoteAddFetch(remote, refspec string) error { func (c *RemoteCollection) AddFetch(remote, refspec string) error {
crefspec := C.CString(refspec) crefspec := C.CString(refspec)
defer C.free(unsafe.Pointer(crefspec)) defer C.free(unsafe.Pointer(crefspec))
cremote := C.CString(remote) cremote := C.CString(remote)
@ -474,7 +478,7 @@ func (o *Repository) RemoteAddFetch(remote, refspec string) error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_add_fetch(o.ptr, cremote, crefspec) ret := C.git_remote_add_fetch(c.repo.ptr, cremote, crefspec)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }
@ -535,7 +539,7 @@ func (o *Remote) FetchRefspecs() ([]string, error) {
return refspecs, nil return refspecs, nil
} }
func (o *Repository) RemoteAddPush(remote, refspec string) error { func (c *RemoteCollection) AddPush(remote, refspec string) error {
crefspec := C.CString(refspec) crefspec := C.CString(refspec)
defer C.free(unsafe.Pointer(crefspec)) defer C.free(unsafe.Pointer(crefspec))
cremote := C.CString(remote) cremote := C.CString(remote)
@ -544,7 +548,7 @@ func (o *Repository) RemoteAddPush(remote, refspec string) error {
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_remote_add_push(o.ptr, cremote, crefspec) ret := C.git_remote_add_push(c.repo.ptr, cremote, crefspec)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }

View File

@ -9,7 +9,7 @@ func TestListRemotes(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
_, err := repo.CreateRemote("test", "git://foo/bar") _, err := repo.Remotes.Create("test", "git://foo/bar")
checkFatal(t, err) checkFatal(t, err)
@ -17,7 +17,7 @@ func TestListRemotes(t *testing.T) {
"test", "test",
} }
actual, err := repo.ListRemotes() actual, err := repo.Remotes.List()
checkFatal(t, err) checkFatal(t, err)
compareStringList(t, expected, actual) compareStringList(t, expected, actual)
@ -36,7 +36,7 @@ func TestCertificateCheck(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err) checkFatal(t, err)
options := FetchOptions { options := FetchOptions {
@ -55,7 +55,7 @@ func TestRemoteConnect(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err) checkFatal(t, err)
err = remote.ConnectFetch(nil) err = remote.ConnectFetch(nil)
@ -66,7 +66,7 @@ func TestRemoteLs(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err) checkFatal(t, err)
err = remote.ConnectFetch(nil) err = remote.ConnectFetch(nil)
@ -84,7 +84,7 @@ func TestRemoteLsFiltering(t *testing.T) {
repo := createTestRepo(t) repo := createTestRepo(t)
defer cleanupTestRepo(t, repo) defer cleanupTestRepo(t, repo)
remote, err := repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") remote, err := repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err) checkFatal(t, err)
err = remote.ConnectFetch(nil) err = remote.ConnectFetch(nil)
@ -117,10 +117,10 @@ func TestRemotePruneRefs(t *testing.T) {
err = config.SetBool("remote.origin.prune", true) err = config.SetBool("remote.origin.prune", true)
checkFatal(t, err) checkFatal(t, err)
_, err = repo.CreateRemote("origin", "https://github.com/libgit2/TestGitRepository") _, err = repo.Remotes.Create("origin", "https://github.com/libgit2/TestGitRepository")
checkFatal(t, err) checkFatal(t, err)
remote, err := repo.LookupRemote("origin") remote, err := repo.Remotes.Lookup("origin")
checkFatal(t, err) checkFatal(t, err)
if !remote.PruneRefs() { if !remote.PruneRefs() {
@ -148,7 +148,7 @@ func TestRemotePrune(t *testing.T) {
defer config.Free() defer config.Free()
remoteUrl := fmt.Sprintf("file://%s", remoteRepo.Workdir()) remoteUrl := fmt.Sprintf("file://%s", remoteRepo.Workdir())
remote, err := repo.CreateRemote("origin", remoteUrl) remote, err := repo.Remotes.Create("origin", remoteUrl)
checkFatal(t, err) checkFatal(t, err)
err = remote.Fetch([]string{"test-prune"}, nil, "") err = remote.Fetch([]string{"test-prune"}, nil, "")
@ -163,7 +163,7 @@ func TestRemotePrune(t *testing.T) {
err = config.SetBool("remote.origin.prune", true) err = config.SetBool("remote.origin.prune", true)
checkFatal(t, err) checkFatal(t, err)
rr, err := repo.LookupRemote("origin") rr, err := repo.Remotes.Lookup("origin")
checkFatal(t, err) checkFatal(t, err)
err = rr.ConnectFetch(nil) err = rr.ConnectFetch(nil)

View File

@ -12,7 +12,16 @@ import (
// Repository // Repository
type Repository struct { type Repository struct {
ptr *C.git_repository ptr *C.git_repository
// Remotes represents the collection of remotes and can be
// used to add, remove and configure remotes for this
// repository.
Remotes RemoteCollection
}
func initRepositoryObject(repo *Repository) {
repo.Remotes.repo = repo
runtime.SetFinalizer(repo, (*Repository).Free)
} }
func OpenRepository(path string) (*Repository, error) { func OpenRepository(path string) (*Repository, error) {
@ -29,7 +38,7 @@ func OpenRepository(path string) (*Repository, error) {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
runtime.SetFinalizer(repo, (*Repository).Free) initRepositoryObject(repo)
return repo, nil return repo, nil
} }
@ -47,7 +56,7 @@ func OpenRepositoryExtended(path string) (*Repository, error) {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
runtime.SetFinalizer(repo, (*Repository).Free) initRepositoryObject(repo)
return repo, nil return repo, nil
} }
@ -65,7 +74,7 @@ func InitRepository(path string, isbare bool) (*Repository, error) {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
runtime.SetFinalizer(repo, (*Repository).Free) initRepositoryObject(repo)
return repo, nil return repo, nil
} }
@ -80,7 +89,7 @@ func NewRepositoryWrapOdb(odb *Odb) (repo *Repository, err error) {
return nil, MakeGitError(ret) return nil, MakeGitError(ret)
} }
runtime.SetFinalizer(repo, (*Repository).Free) initRepositoryObject(repo)
return repo, nil return repo, nil
} }