diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2017-05-21 13:01:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-05-21 13:01:30 +0200 |
commit | d98ebb5e42cd07007010f6d10db11a792c58206e (patch) | |
tree | d53451935a81f90f4da8727a6af1d61952712ae8 /plumbing/format | |
parent | 0f2abe7da065ed25085ea265db20e77609cefdbc (diff) | |
parent | 52add52ea3f92a2a092e6879b6d75fb1279430d5 (diff) | |
download | go-git-d98ebb5e42cd07007010f6d10db11a792c58206e.tar.gz |
Merge pull request #395 from mcuadros/rm-mv
worktree: Remove and Move methods
Diffstat (limited to 'plumbing/format')
-rw-r--r-- | plumbing/format/index/index.go | 12 | ||||
-rw-r--r-- | plumbing/format/index/index_test.go | 17 |
2 files changed, 29 insertions, 0 deletions
diff --git a/plumbing/format/index/index.go b/plumbing/format/index/index.go index 782e3d1..9de4230 100644 --- a/plumbing/format/index/index.go +++ b/plumbing/format/index/index.go @@ -62,6 +62,18 @@ func (i *Index) Entry(path string) (*Entry, error) { return nil, ErrEntryNotFound } +// Remove remove the entry that match the give path and returns deleted entry. +func (i *Index) Remove(path string) (*Entry, error) { + for index, e := range i.Entries { + if e.Name == path { + i.Entries = append(i.Entries[:index], i.Entries[index+1:]...) + return e, nil + } + } + + return nil, 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 index 67286b3..cad5f9c 100644 --- a/plumbing/format/index/index_test.go +++ b/plumbing/format/index/index_test.go @@ -20,3 +20,20 @@ func (s *IndexSuite) TestIndexEntry(c *C) { c.Assert(e, IsNil) c.Assert(err, Equals, ErrEntryNotFound) } + +func (s *IndexSuite) TestIndexRemove(c *C) { + idx := &Index{ + Entries: []*Entry{ + {Name: "foo", Size: 42}, + {Name: "bar", Size: 82}, + }, + } + + e, err := idx.Remove("foo") + c.Assert(err, IsNil) + c.Assert(e.Name, Equals, "foo") + + e, err = idx.Remove("foo") + c.Assert(e, IsNil) + c.Assert(err, Equals, ErrEntryNotFound) +} |