Skip to content

Commit 6a9cda5

Browse files
committed
Add an implicit ".git/" line which git does internally.
This is useful if you want to list only the files that git would track. It clearly doesn't track its own files.
1 parent b8d1872 commit 6a9cda5

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

gitignore_parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ def handle_negation(file_path, rules: Reversible["IgnoreRule"]):
1515
def parse_gitignore(full_path, base_dir=None):
1616
if base_dir is None:
1717
base_dir = dirname(full_path)
18-
rules = []
18+
rules = [
19+
rule_from_pattern(".git/", base_path=_normalize_path(base_dir),
20+
source=(full_path, -1)),
21+
]
1922
with open(full_path) as ignore_file:
2023
counter = 0
2124
for line in ignore_file:

tests.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ def test_simple(self):
1919
self.assertTrue(matches('/home/michael/dir/main.pyc'))
2020
self.assertTrue(matches('/home/michael/__pycache__'))
2121

22+
def test_ignores_git_directory(self):
23+
matches = _parse_gitignore_string('*.py', fake_base_dir='/home/michael')
24+
self.assertFalse(matches('/home/michael/.gitignore'))
25+
self.assertTrue(matches('/home/michael/.git'))
26+
self.assertTrue(matches('/home/michael/.git/config'))
27+
self.assertTrue(matches('/home/michael/.git/logs/refs/remotes/origin/HEAD'))
28+
2229
def test_incomplete_filename(self):
2330
matches = _parse_gitignore_string('o.py', fake_base_dir='/home/michael')
2431
self.assertTrue(matches('/home/michael/o.py'))

0 commit comments

Comments
 (0)