aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--remote.go5
-rw-r--r--remote_test.go15
-rw-r--r--repository.go10
-rw-r--r--repository_test.go14
4 files changed, 44 insertions, 0 deletions
diff --git a/remote.go b/remote.go
index 283b6b3..095faff 100644
--- a/remote.go
+++ b/remote.go
@@ -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{}