Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bf45e6

Browse files
Fix #20958: Support .git/info/exclude for --exclude-gitignore (#21286)
### Summary Fixes #20958. --exclude-gitignore now respects patterns in .git/info/exclude, not just .gitignore files. Previously, mypy ignored .git/info/exclude entirely, causing files matched by those patterns to still be checked. ### Changes File: mypy/modulefinder.py — find_gitignores() File: test-data/unit/cmdline.test Added handling for .git/info/exclude inside the git root detection branch. When find_gitignores identifies a directory as a git root (i.e. it contains a .git folder), it now also checks for .git/info/exclude. If the file exists, it is parsed with PathSpec.from_lines("gitignore", ...) and included in the returned list of gitignore specs, alongside any .gitignore found in the same directory. Parse errors are printed to stderr and the file is skipped, matching the existing behavior for malformed .gitignore files. --------- Co-authored-by: Cindy Wang <[email protected]>
1 parent 880e9b6 commit 3bf45e6

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

mypy/modulefinder.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,14 @@ def find_gitignores(dir: str) -> list[tuple[str, PathSpec]]:
728728
parent_dir = os.path.dirname(dir)
729729
if parent_dir == dir or os.path.exists(os.path.join(dir, ".git")):
730730
parent_gitignores = []
731+
git_info_exclude = os.path.join(dir, ".git", "info", "exclude")
732+
if os.path.isfile(git_info_exclude):
733+
with open(git_info_exclude) as f:
734+
exclude_lines = f.readlines()
735+
try:
736+
parent_gitignores = [(dir, PathSpec.from_lines("gitignore", exclude_lines))]
737+
except GitIgnorePatternError:
738+
print(f"error: could not parse {git_info_exclude}", file=sys.stderr)
731739
else:
732740
parent_gitignores = find_gitignores(parent_dir)
733741

test-data/unit/cmdline.test

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,17 @@ b
989989
[out]
990990
b/bpkg.py:1: error: "int" not callable
991991

992+
[case testCmdlineExcludeGitignoreWithGitInfoExclude]
993+
# cmd: mypy --exclude-gitignore .
994+
[file .git/info/exclude]
995+
abc
996+
[file abc/apkg.py]
997+
1()
998+
[file c/cpkg.py]
999+
1()
1000+
[out]
1001+
c/cpkg.py:1: error: "int" not callable
1002+
9921003
[case testCmdlineCfgExclude]
9931004
# cmd: mypy .
9941005
[file mypy.ini]

0 commit comments

Comments
 (0)