diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-04-17 17:36:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-17 17:36:28 +0200 |
commit | c991d2d1c2253ac246441bc92bf97804a73c0bf2 (patch) | |
tree | 9a6965fb0023262ea17d02bcedd9f48204a80750 /plumbing/format | |
parent | 057f1dd4fa44df509cb96977c0a8193407a22767 (diff) | |
parent | 4b0fc1eb6937b6e5f8569794e8b669443e2c7584 (diff) | |
download | go-git-c991d2d1c2253ac246441bc92bf97804a73c0bf2.tar.gz |
Merge pull request #344 from mcuadros/submodules-checkout
worktree: reset and checkout support for submodules
Diffstat (limited to 'plumbing/format')
-rw-r--r-- | plumbing/format/index/index.go | 22 | ||||
-rw-r--r-- | plumbing/format/index/index_test.go | 22 |
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, "") +} |