diff options
Diffstat (limited to 'shelf/ignore.go')
| -rw-r--r-- | shelf/ignore.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/shelf/ignore.go b/shelf/ignore.go new file mode 100644 index 0000000..be061f3 --- /dev/null +++ b/shelf/ignore.go @@ -0,0 +1,48 @@ +package shelf + +import ( + "bufio" + "bytes" + "path/filepath" + "strings" + + "github.com/go-git/go-git/v5/plumbing/format/gitignore" +) + +type Ignore struct { + matcher gitignore.Matcher +} + +func newIgnore(data []byte) *Ignore { + var patterns []gitignore.Pattern + + scanner := bufio.NewScanner(bytes.NewReader(data)) + + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + + if line == "" || strings.HasPrefix(line, "#") { + continue + } + + patterns = append(patterns, gitignore.ParsePattern(line, nil)) + } + + return &Ignore{ + matcher: gitignore.NewMatcher(patterns), + } +} + +func (i *Ignore) Match(path string, isDir bool) bool { + parts := strings.Split(filepath.ToSlash(path), "/") + + var segments []string + + for _, p := range parts { + if p != "" { + segments = append(segments, p) + } + } + + return i.matcher.Match(segments, isDir) +} |
