From 601c85a8834cd78e317f1ba435475fa18162053a Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Thu, 13 Apr 2017 12:57:27 +0200 Subject: format: index, Index.Entry method --- plumbing/format/index/index.go | 22 +++++++++++++++++----- plumbing/format/index/index_test.go | 22 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 plumbing/format/index/index_test.go (limited to 'plumbing/format/index') 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, "") +} -- cgit