From e4651e88923221f3c0667e8c53acf824058d6259 Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Fri, 20 Jun 2025 21:38:26 +0200 Subject: 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 --- internal/go-git-gitignore/matcher.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 internal/go-git-gitignore/matcher.go (limited to 'internal/go-git-gitignore/matcher.go') 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 +} -- cgit v1.2.3