Skip to content

plumbing: format/ignore, Ensure inclusion patterns match full paths - #2195

Open
Abiggj wants to merge 1 commit into
go-git:mainfrom
Abiggj:bug-fix/issue-2112
Open

Abiggj wants to merge 1 commit into
go-git:mainfrom
Abiggj:bug-fix/issue-2112

Conversation

@Abiggj

@Abiggj Abiggj commented Jun 18, 2026

Copy link
Copy Markdown

This PR fixes issue #2112 where .gitignore inclusion patterns (e.g., !dir/) incorrectly matched files inside those directories when a parent directory match was found.

Changes

  • Modified simpleNameMatch: Updated logic to require that inclusion patterns match the final component of the path. Parent directory matches are now ignored for inclusions, preventing them from overriding wildcard ignores for nested files.
  • Modified globMatch: Enforced full path consumption (or trailing ** matches) for inclusion patterns to be considered a match.
  • Updated TestSimpleMatch_inclusion: Adjusted existing tests to reflect standard Git behavior, where an inclusion pattern for a directory does not automatically include its contents.

These changes ensure that configurations like:
*
!my-dir/
correctly ignore files inside my-dir/, matching the behavior of standard Git.

@pjbgf pjbgf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abiggj thanks for looking into this. Please re-phrase the commit so that it matches our contributing guide: plumbing: format/ignore, Ensure inclusion patterns match full paths.

Add the tests below into plumbing/format/gitignore/conformance_test.go so that we avoid future regression:

func (s *ConformanceSuite) TestIgnoreNegation_ancestorMatch() {
	tests := []struct {
		patterns []string
		path     string
		isDir    bool
		ignored  bool
		desc     string
	}{
		{
			[]string{"*.log", "!important"},
			"dir/important/app.log", false, true,
			"!important matches ancestor dir, must not re-include app.log",
		},
		{
			[]string{"*.log", "!important"},
			"important/app.log", false, true,
			"!important is ancestor-only; *.log still ignores the file",
		},
		{
			[]string{"*.log", "!important"},
			"important", true, false,
			"!important re-includes the dir itself (basename match)",
		},
		{
			[]string{"*", "!value/vul?ano"},
			"value/vulkano/tail", false, true,
			"!value/vul?ano matches an ancestor only; * keeps the file ignored",
		},
	}
	for _, tt := range tests {
		m := s.createMatcher(tt.patterns)
		s.assertIgnore(m, tt.patterns, tt.path, tt.isDir, tt.ignored, tt.desc)
	}
}

@pjbgf pjbgf changed the title Updates: ensure inclusion patterns match full paths plumbing: format/ignore, Ensure inclusion patterns match full paths Jun 19, 2026
@AriehSchneier

Copy link
Copy Markdown
Contributor

Add the tests below into plumbing/format/gitignore/conformance_test.go so that we avoid future regression:
...

I would add a further 2 more:

{
    []string{"*", "!dir/"},
    "dir/file.txt", false, true,
    "!dir/ re-includes only the dir itself; * still ignores files inside",
},
{
    []string{"*", "!dir/"},
    "dir", true, false,
    "!dir/ re-includes the directory itself",
},

@Abiggj
Abiggj force-pushed the bug-fix/issue-2112 branch from 9f074ba to 1fe5d15 Compare June 22, 2026 05:37
@Abiggj

Abiggj commented Jun 22, 2026

Copy link
Copy Markdown
Author

Updated the commit message to follow the contributing guide and added the requested regression tests (including the extra !dir/ cases). All tests are passing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants