aboutsummaryrefslogtreecommitdiffstats
path: root/storage/filesystem/internal/dotgit
diff options
context:
space:
mode:
Diffstat (limited to 'storage/filesystem/internal/dotgit')
-rw-r--r--storage/filesystem/internal/dotgit/dotgit.go21
-rw-r--r--storage/filesystem/internal/dotgit/dotgit_test.go25
2 files changed, 46 insertions, 0 deletions
diff --git a/storage/filesystem/internal/dotgit/dotgit.go b/storage/filesystem/internal/dotgit/dotgit.go
index ba293af..6606153 100644
--- a/storage/filesystem/internal/dotgit/dotgit.go
+++ b/storage/filesystem/internal/dotgit/dotgit.go
@@ -215,6 +215,27 @@ func (d *DotGit) Refs() ([]*core.Reference, error) {
return refs, nil
}
+// Ref returns the reference for a given reference name.
+func (d *DotGit) Ref(name core.ReferenceName) (*core.Reference, error) {
+ ref, err := d.readReferenceFile(".", name.String())
+ if err == nil {
+ return ref, nil
+ }
+
+ refs, err := d.Refs()
+ if err != nil {
+ return nil, err
+ }
+
+ for _, ref := range refs {
+ if ref.Name() == name {
+ return ref, nil
+ }
+ }
+
+ return nil, core.ErrReferenceNotFound
+}
+
func (d *DotGit) addRefsFromPackedRefs(refs *[]*core.Reference) (err error) {
f, err := d.fs.Open(packedRefsPath)
if err != nil {
diff --git a/storage/filesystem/internal/dotgit/dotgit_test.go b/storage/filesystem/internal/dotgit/dotgit_test.go
index 9e46639..a4cce09 100644
--- a/storage/filesystem/internal/dotgit/dotgit_test.go
+++ b/storage/filesystem/internal/dotgit/dotgit_test.go
@@ -44,6 +44,12 @@ func (s *SuiteDotGit) TestSetRefs(c *C) {
c.Assert(err, IsNil)
+ err = dir.SetRef(core.NewReferenceFromStrings(
+ "bar",
+ "e8d3ffab552895c19b9fcf7aa264d277cde33881",
+ ))
+ c.Assert(err, IsNil)
+
refs, err := dir.Refs()
c.Assert(err, IsNil)
c.Assert(refs, HasLen, 2)
@@ -55,6 +61,25 @@ func (s *SuiteDotGit) TestSetRefs(c *C) {
ref = findReference(refs, "refs/heads/symbolic")
c.Assert(ref, NotNil)
c.Assert(ref.Target().String(), Equals, "refs/heads/foo")
+
+ ref = findReference(refs, "bar")
+ c.Assert(ref, IsNil)
+
+ ref, err = dir.Ref("refs/heads/foo")
+ c.Assert(err, IsNil)
+ c.Assert(ref, NotNil)
+ c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
+
+ ref, err = dir.Ref("refs/heads/symbolic")
+ c.Assert(err, IsNil)
+ c.Assert(ref, NotNil)
+ c.Assert(ref.Target().String(), Equals, "refs/heads/foo")
+
+ ref, err = dir.Ref("bar")
+ c.Assert(err, IsNil)
+ c.Assert(ref, NotNil)
+ c.Assert(ref.Hash().String(), Equals, "e8d3ffab552895c19b9fcf7aa264d277cde33881")
+
}
func (s *SuiteDotGit) TestRefsFromPackedRefs(c *C) {