aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--repository.go7
-rw-r--r--repository_test.go63
2 files changed, 59 insertions, 11 deletions
diff --git a/repository.go b/repository.go
index c05afdb..1342e7d 100644
--- a/repository.go
+++ b/repository.go
@@ -218,3 +218,10 @@ func (r *Repository) Object(h core.Hash) (Object, error) {
return nil, core.ErrInvalidType
}
}
+
+// 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) {
+ return core.ZeroHash, nil
+}
diff --git a/repository_test.go b/repository_test.go
index 7d4abb5..06f8625 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -13,19 +13,26 @@ import (
. "gopkg.in/check.v1"
)
-var dirFixtures = [...]struct {
+var dirFixturesInit = [...]struct {
name string
tgz string
+ head string
}{
{
name: "binrels",
tgz: "storage/seekable/internal/gitdir/fixtures/alcortesm-binary-relations.tgz",
+ head: "c44b5176e99085c8fe36fa27b045590a7b9d34c9",
},
}
+type dirFixture struct {
+ path string
+ head core.Hash
+}
+
type SuiteRepository struct {
- repos map[string]*Repository
- dirFixturePaths map[string]string
+ repos map[string]*Repository
+ dirFixtures map[string]dirFixture
}
var _ = Suite(&SuiteRepository{})
@@ -33,22 +40,25 @@ var _ = Suite(&SuiteRepository{})
func (s *SuiteRepository) SetUpSuite(c *C) {
s.repos = unpackFixtures(c, tagFixtures, treeWalkerFixtures)
- s.dirFixturePaths = make(map[string]string, len(dirFixtures))
- for _, fix := range dirFixtures {
+ s.dirFixtures = make(map[string]dirFixture, len(dirFixturesInit))
+ for _, fix := range dirFixturesInit {
com := Commentf("fixture name = %s\n", fix.name)
path, err := tgz.Extract(fix.tgz)
c.Assert(err, IsNil, com)
- s.dirFixturePaths[fix.name] = path
+ s.dirFixtures[fix.name] = dirFixture{
+ path: path,
+ head: core.NewHash(fix.head),
+ }
}
}
func (s *SuiteRepository) TearDownSuite(c *C) {
- for name, path := range s.dirFixturePaths {
- err := os.RemoveAll(path)
+ for name, fix := range s.dirFixtures {
+ err := os.RemoveAll(fix.path)
c.Assert(err, IsNil, Commentf("cannot delete tmp dir for fixture %s: %s\n",
- name, path))
+ name, fix.path))
}
}
@@ -66,9 +76,9 @@ func (s *SuiteRepository) TestNewRepositoryWithAuth(c *C) {
}
func (s *SuiteRepository) TestNewRepositoryFromFS(c *C) {
- for name, path := range s.dirFixturePaths {
+ for name, fix := range s.dirFixtures {
fs := fs.NewOS()
- gitPath := fs.Join(path, ".git/")
+ gitPath := fs.Join(fix.path, ".git/")
com := Commentf("dir fixture %q → %q\n", name, gitPath)
repo, err := NewRepositoryFromFS(fs, gitPath)
c.Assert(err, IsNil, com)
@@ -205,3 +215,34 @@ func (s *SuiteRepository) TestCommitIterClosePanic(c *C) {
c.Assert(err, IsNil)
commits.Close()
}
+
+func (s *SuiteRepository) TestHeadFromFs(c *C) {
+ for name, fix := range s.dirFixtures {
+ fs := fs.NewOS()
+ gitPath := fs.Join(fix.path, ".git/")
+ com := Commentf("dir fixture %q → %q\n", name, gitPath)
+ repo, err := NewRepositoryFromFS(fs, gitPath)
+ c.Assert(err, IsNil, com)
+
+ head, err := repo.Head()
+ c.Assert(err, IsNil)
+
+ c.Assert(head, Equals, fix.head)
+ }
+}
+
+func (s *SuiteRepository) TestHeadFromRemote(c *C) {
+ r, err := NewRepository(RepositoryFixture, nil)
+ c.Assert(err, IsNil)
+
+ upSrv := &MockGitUploadPackService{}
+ r.Remotes["origin"].upSrv = upSrv
+ info, err := upSrv.Info()
+ c.Assert(err, IsNil)
+ expected := info.Head
+
+ obtained, err := r.Head()
+ c.Assert(err, IsNil)
+
+ c.Assert(obtained, Equals, expected)
+}