diff options
-rw-r--r-- | remote.go | 5 | ||||
-rw-r--r-- | remote_test.go | 15 | ||||
-rw-r--r-- | repository.go | 10 | ||||
-rw-r--r-- | repository_test.go | 14 |
4 files changed, 44 insertions, 0 deletions
@@ -81,6 +81,11 @@ func (r *Remote) DefaultBranch() string { return r.upInfo.Capabilities.SymbolicReference("HEAD") } +// Head returns the Hash of the HEAD +func (r *Remote) Head() (core.Hash, error) { + return r.Ref(r.DefaultBranch()) +} + // Fetch returns a reader using the request func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error) { return r.upSrv.Fetch(req) diff --git a/remote_test.go b/remote_test.go index 7bba597..85b464c 100644 --- a/remote_test.go +++ b/remote_test.go @@ -2,6 +2,7 @@ package git import ( "gopkg.in/src-d/go-git.v3/clients/http" + "gopkg.in/src-d/go-git.v3/core" "gopkg.in/src-d/go-git.v3/formats/packfile" "gopkg.in/src-d/go-git.v3/storage/memory" @@ -62,3 +63,17 @@ func (s *SuiteRemote) TestFetchDefaultBranch(c *C) { c.Assert(err, IsNil) c.Assert(storage.Objects, HasLen, 28) } + +func (s *SuiteRemote) TestHead(c *C) { + r, err := NewRemote(RepositoryFixture) + r.upSrv = &MockGitUploadPackService{} + + c.Assert(err, IsNil) + + err = r.Connect() + c.Assert(err, IsNil) + + hash, err := r.Head() + c.Assert(err, IsNil) + c.Assert(hash, Equals, core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) +} diff --git a/repository.go b/repository.go index ae583fd..f4aa26d 100644 --- a/repository.go +++ b/repository.go @@ -91,6 +91,16 @@ func (r *Repository) Pull(remoteName, branch string) (err error) { return nil } +// PullDefault like Pull but retrieve the default branch from the default remote +func (r *Repository) PullDefault() (err error) { + remote, ok := r.Remotes[DefaultRemoteName] + if !ok { + return fmt.Errorf("unable to find default remote %q", DefaultRemoteName) + } + + return r.Pull(DefaultRemoteName, remote.DefaultBranch()) +} + // Commit return the commit with the given hash func (r *Repository) Commit(h core.Hash) (*Commit, error) { obj, err := r.Storage.Get(h) diff --git a/repository_test.go b/repository_test.go index 9e81334..1f57b18 100644 --- a/repository_test.go +++ b/repository_test.go @@ -46,6 +46,20 @@ func (s *SuiteRepository) TestPull(c *C) { c.Assert(err, Not(IsNil), Commentf("pull leaks an open fd from the fetch")) } +func (s *SuiteRepository) TestPullDefault(c *C) { + r, err := NewRepository(RepositoryFixture, nil) + r.Remotes[DefaultRemoteName].Connect() + r.Remotes[DefaultRemoteName].upSrv = &MockGitUploadPackService{} + + c.Assert(err, IsNil) + c.Assert(r.PullDefault(), IsNil) + + mock, ok := (r.Remotes[DefaultRemoteName].upSrv).(*MockGitUploadPackService) + c.Assert(ok, Equals, true) + err = mock.RC.Close() + c.Assert(err, Not(IsNil), Commentf("pull leaks an open fd from the fetch")) +} + func (s *SuiteRepository) TestCommit(c *C) { r, err := NewRepository(RepositoryFixture, nil) r.Remotes["origin"].upSrv = &MockGitUploadPackService{} |