Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit 74b8b53

Browse files
committed
remote.Head and repository.PullDefault
1 parent a84f6e9 commit 74b8b53

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

remote.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ func (r *Remote) DefaultBranch() string {
8181
return r.upInfo.Capabilities.SymbolicReference("HEAD")
8282
}
8383

84+
// Head returns the Hash of the HEAD
85+
func (r *Remote) Head() (core.Hash, error) {
86+
return r.Ref(r.DefaultBranch())
87+
}
88+
8489
// Fetch returns a reader using the request
8590
func (r *Remote) Fetch(req *common.GitUploadPackRequest) (io.ReadCloser, error) {
8691
return r.upSrv.Fetch(req)

remote_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package git
22

33
import (
44
"gopkg.in/src-d/go-git.v3/clients/http"
5+
"gopkg.in/src-d/go-git.v3/core"
56
"gopkg.in/src-d/go-git.v3/formats/packfile"
67
"gopkg.in/src-d/go-git.v3/storage/memory"
78

@@ -62,3 +63,17 @@ func (s *SuiteRemote) TestFetchDefaultBranch(c *C) {
6263
c.Assert(err, IsNil)
6364
c.Assert(storage.Objects, HasLen, 28)
6465
}
66+
67+
func (s *SuiteRemote) TestHead(c *C) {
68+
r, err := NewRemote(RepositoryFixture)
69+
r.upSrv = &MockGitUploadPackService{}
70+
71+
c.Assert(err, IsNil)
72+
73+
err = r.Connect()
74+
c.Assert(err, IsNil)
75+
76+
hash, err := r.Head()
77+
c.Assert(err, IsNil)
78+
c.Assert(hash, Equals, core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5"))
79+
}

repository.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ func (r *Repository) Pull(remoteName, branch string) (err error) {
9191
return nil
9292
}
9393

94+
// PullDefault like Pull but retrieve the default branch from the default remote
95+
func (r *Repository) PullDefault() (err error) {
96+
remote, ok := r.Remotes[DefaultRemoteName]
97+
if !ok {
98+
return fmt.Errorf("unable to find default remote %q", DefaultRemoteName)
99+
}
100+
101+
return r.Pull(DefaultRemoteName, remote.DefaultBranch())
102+
}
103+
94104
// Commit return the commit with the given hash
95105
func (r *Repository) Commit(h core.Hash) (*Commit, error) {
96106
obj, err := r.Storage.Get(h)

repository_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ func (s *SuiteRepository) TestPull(c *C) {
4646
c.Assert(err, Not(IsNil), Commentf("pull leaks an open fd from the fetch"))
4747
}
4848

49+
func (s *SuiteRepository) TestPullDefault(c *C) {
50+
r, err := NewRepository(RepositoryFixture, nil)
51+
r.Remotes[DefaultRemoteName].Connect()
52+
r.Remotes[DefaultRemoteName].upSrv = &MockGitUploadPackService{}
53+
54+
c.Assert(err, IsNil)
55+
c.Assert(r.PullDefault(), IsNil)
56+
57+
mock, ok := (r.Remotes[DefaultRemoteName].upSrv).(*MockGitUploadPackService)
58+
c.Assert(ok, Equals, true)
59+
err = mock.RC.Close()
60+
c.Assert(err, Not(IsNil), Commentf("pull leaks an open fd from the fetch"))
61+
}
62+
4963
func (s *SuiteRepository) TestCommit(c *C) {
5064
r, err := NewRepository(RepositoryFixture, nil)
5165
r.Remotes["origin"].upSrv = &MockGitUploadPackService{}

0 commit comments

Comments
 (0)