aboutsummaryrefslogtreecommitdiff
path: root/internal/go-git-gitignore/matcher.go
diff options
context:
space:
mode:
authorJakob Unterwurzacher2025-06-20 21:38:26 +0200
committerJakob Unterwurzacher2025-07-09 19:28:09 +0200
commite4651e88923221f3c0667e8c53acf824058d6259 (patch)
tree8fe1f839e9a360a7623d12a633582085ad7dda15 /internal/go-git-gitignore/matcher.go
parentac2493d0fb26155b17ecbe35751534e6f50dc548 (diff)
go-git-gitignore: copy matcher.go, pattern.go from go-git/plumbing/format/gitignore
Files copied from https://github.com/go-git/go-git/tree/7bc22667c9e181cc1361eccec77f1a237abee860/plumbing/format/gitignore . Importing all of go-git pulls in a metric ton of dependencies, so I have decided to only copy the two files we need. https://github.com/rfjakob/gocryptfs/issues/927
Diffstat (limited to 'internal/go-git-gitignore/matcher.go')
-rw-r--r--internal/go-git-gitignore/matcher.go30
1 files changed, 30 insertions, 0 deletions
diff --git a/internal/go-git-gitignore/matcher.go b/internal/go-git-gitignore/matcher.go
new file mode 100644
index 0000000..bd1e9e2
--- /dev/null
+++ b/internal/go-git-gitignore/matcher.go
@@ -0,0 +1,30 @@
+package gitignore
+
+// Matcher defines a global multi-pattern matcher for gitignore patterns
+type Matcher interface {
+ // Match matches patterns in the order of priorities. As soon as an inclusion or
+ // exclusion is found, not further matching is performed.
+ Match(path []string, isDir bool) bool
+}
+
+// NewMatcher constructs a new global matcher. Patterns must be given in the order of
+// increasing priority. That is most generic settings files first, then the content of
+// the repo .gitignore, then content of .gitignore down the path or the repo and then
+// the content command line arguments.
+func NewMatcher(ps []Pattern) Matcher {
+ return &matcher{ps}
+}
+
+type matcher struct {
+ patterns []Pattern
+}
+
+func (m *matcher) Match(path []string, isDir bool) bool {
+ n := len(m.patterns)
+ for i := n - 1; i >= 0; i-- {
+ if match := m.patterns[i].Match(path, isDir); match > NoMatch {
+ return match == Exclude
+ }
+ }
+ return false
+}