diff options
| author | Shulhan <ms@kilabit.info> | 2026-01-11 18:43:56 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-01-11 21:29:17 +0700 |
| commit | cdfebe3f55dc53872079a96a9a5dd6970bc2980e (patch) | |
| tree | 271827433390beb3172f5058505abc21be281df7 /lib | |
| parent | 34693c1b7ab126e1fbda3810eb46b735c350505c (diff) | |
| download | pakakeh.go-cdfebe3f55dc53872079a96a9a5dd6970bc2980e.tar.xz | |
lib/git: fix ignore pattern with single wildcard '*'
Single wildcard should ignore everything inside it.
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/git/git.go | 8 | ||||
| -rw-r--r-- | lib/git/git_test.go | 31 | ||||
| -rw-r--r-- | lib/git/ignore_pattern.go | 3 | ||||
| -rw-r--r-- | lib/git/ignore_pattern_test.go | 12 | ||||
| -rw-r--r-- | lib/git/testdata/IsIgnored/a/b/.gitignore | 2 | ||||
| -rw-r--r-- | lib/git/testdata/IsIgnored/a/b/c/.gitignore | 1 |
6 files changed, 45 insertions, 12 deletions
diff --git a/lib/git/git.go b/lib/git/git.go index 1fc5fde3..43a27bf5 100644 --- a/lib/git/git.go +++ b/lib/git/git.go @@ -244,15 +244,15 @@ func GetTag(repoDir, revision string) (tag string, err error) { // ".gitignore" file inside the path directory and its parent, until the root // of Git repository. func (git *Git) IsIgnored(path string) (b bool) { - path = strings.TrimSpace(path) + path = strings.TrimSpace(path) // a/b/b1 if path == `` { return true } // Traverse each directory from bottom to the top of git directory to // load ".gitignore" file and match it with path. - var absPath = filepath.Join(git.absDir, path) - var dirGitignore = filepath.Dir(absPath) - var name = strings.TrimPrefix(absPath, dirGitignore) + var absPath = filepath.Join(git.absDir, path) // $git/a/b/b1 + var dirGitignore = filepath.Dir(absPath) // $git/a/b/ + var name = strings.TrimPrefix(absPath, dirGitignore) // b1 name = strings.TrimLeft(name, `/`) for strings.HasPrefix(dirGitignore, git.absDir) { var ign *Gitignore diff --git a/lib/git/git_test.go b/lib/git/git_test.go index 65fe572d..2ea5aba4 100644 --- a/lib/git/git_test.go +++ b/lib/git/git_test.go @@ -163,6 +163,37 @@ func TestGit_Equal(t *testing.T) { } } +func TestGit_IsIgnored(t *testing.T) { + agit, err := New(`testdata/IsIgnored/`) + if err != nil { + t.Fatal(err) + } + + listCase := []struct { + path string + exp bool + }{{ + path: `a/b/c/c1`, + exp: true, + }, { + path: `a/b/c`, + exp: true, + }, { + path: `a/b/b1`, + exp: true, + }, { + path: `a/b/.gitignore`, + }, { + path: `a/b`, + }, { + path: `a`, + }} + for _, tc := range listCase { + got := agit.IsIgnored(tc.path) + test.Assert(t, tc.path, tc.exp, got) + } +} + func TestGetRemoteURL(t *testing.T) { cases := []struct { desc string diff --git a/lib/git/ignore_pattern.go b/lib/git/ignore_pattern.go index db4e2256..0fe161c9 100644 --- a/lib/git/ignore_pattern.go +++ b/lib/git/ignore_pattern.go @@ -50,8 +50,7 @@ func parsePattern(line []byte) (ign ignorePattern) { line = line[1:] } if len(line) == 0 || len(line) == 1 && line[0] == '*' { - // Ignore consecutive '*' pattern, since its mean match - // anything. + ign.pattern, _ = regexp.Compile(`^/?.*$`) return ign } diff --git a/lib/git/ignore_pattern_test.go b/lib/git/ignore_pattern_test.go index 544cb0d8..ad1aeea7 100644 --- a/lib/git/ignore_pattern_test.go +++ b/lib/git/ignore_pattern_test.go @@ -44,33 +44,33 @@ func TestParsePattern(t *testing.T) { }, { pattern: `*`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), }, }, { pattern: `*/`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), isDir: true, }, }, { pattern: `**`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), }, }, { pattern: `***`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), }, }, { pattern: `**/**`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), }, }, { pattern: `**/**/`, exp: ignorePattern{ - pattern: nil, + pattern: regexp.MustCompile(`^/?.*$`), isDir: true, }, }, { diff --git a/lib/git/testdata/IsIgnored/a/b/.gitignore b/lib/git/testdata/IsIgnored/a/b/.gitignore new file mode 100644 index 00000000..120f485d --- /dev/null +++ b/lib/git/testdata/IsIgnored/a/b/.gitignore @@ -0,0 +1,2 @@ +* +!/.gitignore diff --git a/lib/git/testdata/IsIgnored/a/b/c/.gitignore b/lib/git/testdata/IsIgnored/a/b/c/.gitignore new file mode 100644 index 00000000..ab1129da --- /dev/null +++ b/lib/git/testdata/IsIgnored/a/b/c/.gitignore @@ -0,0 +1 @@ +!/.gitignore |
