From 875ac19922932b12f6357d64a460c06d031000e7 Mon Sep 17 00:00:00 2001 From: Jonas Knobloch Date: Thu, 23 Apr 2026 18:14:50 +0200 Subject: Add shelf module --- shelf/ignore.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 shelf/ignore.go (limited to 'shelf/ignore.go') 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) +} -- cgit v1.3.1