aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-04-13 12:57:27 +0200
committerMáximo Cuadros <mcuadros@gmail.com>2017-04-13 12:57:27 +0200
commit601c85a8834cd78e317f1ba435475fa18162053a (patch)
tree344415af6e47e19284a29914ed097da24ced940f /plumbing/format
parent932ced9f55f556de02610425cfa161c35c6a758b (diff)
downloadgo-git-601c85a8834cd78e317f1ba435475fa18162053a.tar.gz
format: index, Index.Entry method
Diffstat (limited to 'plumbing/format')
-rw-r--r--plumbing/format/index/index.go22
-rw-r--r--plumbing/format/index/index_test.go22
2 files changed, 39 insertions, 5 deletions
diff --git a/plumbing/format/index/index.go b/plumbing/format/index/index.go
index 61e7d66..402a48e 100644
--- a/plumbing/format/index/index.go
+++ b/plumbing/format/index/index.go
@@ -1,20 +1,21 @@
package index
import (
+ "bytes"
"errors"
"fmt"
"time"
- "bytes"
-
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/filemode"
)
var (
- // ErrUnsupportedVersion is returned by Decode when the idxindex file
- // version is not supported.
- ErrUnsupportedVersion = errors.New("Unsuported version")
+ // ErrUnsupportedVersion is returned by Decode when the index file version
+ // is not supported.
+ ErrUnsupportedVersion = errors.New("unsupported version")
+ // ErrEntryNotFound is returned by Index.Entry, if an entry is not found.
+ ErrEntryNotFound = errors.New("entry not found")
indexSignature = []byte{'D', 'I', 'R', 'C'}
treeExtSignature = []byte{'T', 'R', 'E', 'E'}
@@ -50,6 +51,17 @@ type Index struct {
ResolveUndo *ResolveUndo
}
+// Entry returns the entry that match the given path, if any.
+func (i *Index) Entry(path string) (Entry, error) {
+ for _, e := range i.Entries {
+ if e.Name == path {
+ return e, nil
+ }
+ }
+
+ return Entry{}, ErrEntryNotFound
+}
+
// String is equivalent to `git ls-files --stage --debug`
func (i *Index) String() string {
buf := bytes.NewBuffer(nil)
diff --git a/plumbing/format/index/index_test.go b/plumbing/format/index/index_test.go
new file mode 100644
index 0000000..8c915d8
--- /dev/null
+++ b/plumbing/format/index/index_test.go
@@ -0,0 +1,22 @@
+package index
+
+import (
+ . "gopkg.in/check.v1"
+)
+
+func (s *IndexSuite) TestIndexEntry(c *C) {
+ idx := &Index{
+ Entries: []Entry{
+ {Name: "foo", Size: 42},
+ {Name: "bar", Size: 82},
+ },
+ }
+
+ e, err := idx.Entry("foo")
+ c.Assert(err, IsNil)
+ c.Assert(e.Name, Equals, "foo")
+
+ e, err = idx.Entry("missing")
+ c.Assert(err, Equals, ErrEntryNotFound)
+ c.Assert(e.Name, Equals, "")
+}