diff options
| author | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-04-23 18:14:50 +0200 |
|---|---|---|
| committer | Jonas Knobloch <jonas.knobloch@t-online.de> | 2026-04-23 20:21:12 +0200 |
| commit | 875ac19922932b12f6357d64a460c06d031000e7 (patch) | |
| tree | f4f268ef2b029a5e187613d6ebbec1a166615dbc /shelf/ignore.go | |
| parent | d35f5fad1d26fc09cfd98f6e7faf17d6a48e4e8f (diff) | |
Add shelf module
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) +} |
