diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-02-26 00:26:37 +0100 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2018-02-26 00:26:37 +0100 |
commit | 9fb58fc0561855882b1741dbb8b6cbaf6e889351 (patch) | |
tree | 803da8be65d8c78e458d084714efd9b969f6da47 /plumbing/format/index/index.go | |
parent | 66f08b836000cb79d2a5b76d3e15e1f81dc77e0b (diff) | |
download | go-git-9fb58fc0561855882b1741dbb8b6cbaf6e889351.tar.gz |
plumbing: format index, Index.Add and Index.Glob methods
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
Diffstat (limited to 'plumbing/format/index/index.go')
-rw-r--r-- | plumbing/format/index/index.go | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/plumbing/format/index/index.go b/plumbing/format/index/index.go index 9de4230..fc7b8cd 100644 --- a/plumbing/format/index/index.go +++ b/plumbing/format/index/index.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "path/filepath" "time" "gopkg.in/src-d/go-git.v4/plumbing" @@ -51,8 +52,20 @@ type Index struct { ResolveUndo *ResolveUndo } +// Add creates a new Entry and returns it. The caller should first check that +// another entry with the same path does not exist. +func (i *Index) Add(path string) *Entry { + e := &Entry{ + Name: filepath.ToSlash(path), + } + + i.Entries = append(i.Entries, e) + return e +} + // Entry returns the entry that match the given path, if any. func (i *Index) Entry(path string) (*Entry, error) { + path = filepath.ToSlash(path) for _, e := range i.Entries { if e.Name == path { return e, nil @@ -64,6 +77,7 @@ func (i *Index) Entry(path string) (*Entry, error) { // Remove remove the entry that match the give path and returns deleted entry. func (i *Index) Remove(path string) (*Entry, error) { + path = filepath.ToSlash(path) for index, e := range i.Entries { if e.Name == path { i.Entries = append(i.Entries[:index], i.Entries[index+1:]...) @@ -74,6 +88,24 @@ func (i *Index) Remove(path string) (*Entry, error) { return nil, ErrEntryNotFound } +// Glob returns the all entries matching pattern or nil if there is no matching +// entry. The syntax of patterns is the same as in filepath.Glob. +func (i *Index) Glob(pattern string) (matches []*Entry, err error) { + pattern = filepath.ToSlash(pattern) + for _, e := range i.Entries { + m, err := match(pattern, e.Name) + if err != nil { + return nil, err + } + + if m { + matches = append(matches, e) + } + } + + return +} + // String is equivalent to `git ls-files --stage --debug` func (i *Index) String() string { buf := bytes.NewBuffer(nil) |