aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlberto Cortés <alberto@sourced.tech>2016-08-01 19:31:22 +0200
committerAlberto Cortés <alberto@sourced.tech>2016-08-01 19:31:22 +0200
commitb5613047f0d1fc6f53d5a8ad1a05415ca9c6a92a (patch)
tree1ae84ac6e7ab4b4c4dbdb2160d7a084b9fbc0185
parent49873428a53364a2a49cd521867d4fda59464911 (diff)
downloadgo-git-b5613047f0d1fc6f53d5a8ad1a05415ca9c6a92a.tar.gz
add head support for remote repos and more tests
-rw-r--r--repository.go20
-rw-r--r--repository_test.go21
2 files changed, 34 insertions, 7 deletions
diff --git a/repository.go b/repository.go
index 1342e7d..0cda947 100644
--- a/repository.go
+++ b/repository.go
@@ -219,9 +219,21 @@ func (r *Repository) Object(h core.Hash) (Object, error) {
}
}
-// Head returns the hash of the HEAD of the repository. If there is no
-// HEAD, it then returns the hash of the HEAD of the default remote. If
-// there is no default remote, it returns an error.
-func (r *Repository) Head() (core.Hash, error) {
+// Head returns the hash of the HEAD of the repository or the head of a
+// remote, if one is passed.
+func (r *Repository) Head(remote string) (core.Hash, error) {
+ if remote == "" {
+ return r.localHead()
+ }
+
+ rem, ok := r.Remotes[remote]
+ if !ok {
+ return core.ZeroHash, fmt.Errorf("unable to find remote %q", remote)
+ }
+
+ return rem.Head()
+}
+
+func (r *Repository) localHead() (core.Hash, error) {
return core.ZeroHash, nil
}
diff --git a/repository_test.go b/repository_test.go
index 06f8625..da02a4d 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -224,7 +224,7 @@ func (s *SuiteRepository) TestHeadFromFs(c *C) {
repo, err := NewRepositoryFromFS(fs, gitPath)
c.Assert(err, IsNil, com)
- head, err := repo.Head()
+ head, err := repo.Head("")
c.Assert(err, IsNil)
c.Assert(head, Equals, fix.head)
@@ -236,13 +236,28 @@ func (s *SuiteRepository) TestHeadFromRemote(c *C) {
c.Assert(err, IsNil)
upSrv := &MockGitUploadPackService{}
- r.Remotes["origin"].upSrv = upSrv
+ r.Remotes[DefaultRemoteName].upSrv = upSrv
+ err = r.Remotes[DefaultRemoteName].Connect()
+ c.Assert(err, IsNil)
+
info, err := upSrv.Info()
c.Assert(err, IsNil)
expected := info.Head
- obtained, err := r.Head()
+ obtained, err := r.Head(DefaultRemoteName)
c.Assert(err, IsNil)
c.Assert(obtained, Equals, expected)
}
+
+func (s *SuiteRepository) TestHeadFromRemoteError(c *C) {
+ r, err := NewRepository(RepositoryFixture, nil)
+ c.Assert(err, IsNil)
+
+ upSrv := &MockGitUploadPackService{}
+ r.Remotes[DefaultRemoteName].upSrv = upSrv
+
+ remote := "not found"
+ _, err = r.Head(remote)
+ c.Assert(err, ErrorMatches, fmt.Sprintf("unable to find remote %q", remote))
+}