diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-15 15:54:41 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-15 17:22:25 +0700 |
| commit | f8a08c8a55efbaecc8a99bf39b63d85b3d89170d (patch) | |
| tree | 4fe187befc7146751fcba617d6dd655cdc4c4268 | |
| parent | 83aa06572a833823a233ae3834282bdb0be524e5 (diff) | |
| download | pakakeh.go-f8a08c8a55efbaecc8a99bf39b63d85b3d89170d.tar.xz | |
lib/git: handle pattern "**/foo/**"
The "**/foo/**" means accept any files as long as there is foo directory
in the middle.
| -rw-r--r-- | lib/git/ignore_pattern.go | 9 | ||||
| -rw-r--r-- | lib/git/ignore_pattern_test.go | 10 |
2 files changed, 16 insertions, 3 deletions
diff --git a/lib/git/ignore_pattern.go b/lib/git/ignore_pattern.go index 37fc7035..6097d46d 100644 --- a/lib/git/ignore_pattern.go +++ b/lib/git/ignore_pattern.go @@ -42,8 +42,11 @@ func ParseIgnorePattern(line []byte) (ign IgnorePattern) { line = line[:len(line)-1] } + var sepIdx int + // The "**/foo" pattern is equal to "foo", so we can remove the "**/". for bytes.HasPrefix(line, []byte("**/")) { + sepIdx = -1 // Flag it as zero or more directory before. line = line[3:] } if len(line) == 0 { @@ -57,9 +60,9 @@ func ParseIgnorePattern(line []byte) (ign IgnorePattern) { return ign } - // Get the index of directory separator, before we replace it some - // special characters with regex. - var sepIdx = bytes.LastIndexByte(line, '/') + if sepIdx == 0 { + sepIdx = bytes.LastIndexByte(line, '/') + } var RE_EVERYTHING_INSIDE = []byte(`/(.*)`) var RE_FILE_OR_DIR = []byte(`/?$`) diff --git a/lib/git/ignore_pattern_test.go b/lib/git/ignore_pattern_test.go index c90620ba..54c4e9e9 100644 --- a/lib/git/ignore_pattern_test.go +++ b/lib/git/ignore_pattern_test.go @@ -79,6 +79,16 @@ func TestParseIgnorePattern(t *testing.T) { pattern: regexp.MustCompile(`^(.*/|/)?[^/]*foo/?$`), }, }, { + pattern: `**/foo/**`, + exp: IgnorePattern{ + pattern: regexp.MustCompile(`^(.*/|/)?foo/(.*)/?$`), + }, + }, { + pattern: `foo/**`, + exp: IgnorePattern{ + pattern: regexp.MustCompile(`^/?foo/(.*)/?$`), + }, + }, { pattern: `foo`, exp: IgnorePattern{ pattern: regexp.MustCompile(`^(.*/|/)?foo/?$`), |
