Skip to content

Commit bef4a01

Browse files
committed
Don't pass deleted files to pre-commit scripts.
1 parent 6649e18 commit bef4a01

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

pre_commit/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import functools
22
import os
3+
import os.path
34
import pkg_resources
45
import re
56
from plumbum import local
@@ -57,14 +58,13 @@ def get_files_matching(all_file_list_strategy):
5758
@memoize_by_cwd
5859
def wrapper(expr):
5960
regex = re.compile(expr)
60-
return set(
61+
return set(filter(os.path.exists, (
6162
filename
6263
for filename in all_file_list_strategy()
6364
if regex.search(filename)
64-
)
65+
)))
6566
return wrapper
6667

6768

68-
6969
get_staged_files_matching = get_files_matching(get_staged_files)
7070
get_all_files_matching = get_files_matching(get_all_files)

tests/git_test.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ def test_remove_pre_commit(empty_git_dir):
4242
def get_files_matching_func():
4343
def get_filenames():
4444
return (
45-
'foo.py',
46-
'bar/baz.py',
47-
'tests/baz_test.py',
45+
'pre_commit/run.py',
46+
'pre_commit/git.py',
47+
'im_a_file_that_doesnt_exist.py',
4848
'manifest.yaml',
4949
)
5050

@@ -54,22 +54,25 @@ def get_filenames():
5454
def test_get_files_matching_base(get_files_matching_func):
5555
ret = get_files_matching_func('')
5656
assert ret == set([
57-
'foo.py',
58-
'bar/baz.py',
59-
'tests/baz_test.py',
57+
'pre_commit/run.py',
58+
'pre_commit/git.py',
6059
'manifest.yaml',
6160
])
6261

6362

6463
def test_get_files_matching_total_match(get_files_matching_func):
6564
ret = get_files_matching_func('^.*\.py$')
6665
assert ret == set([
67-
'foo.py',
68-
'bar/baz.py',
69-
'tests/baz_test.py',
66+
'pre_commit/run.py',
67+
'pre_commit/git.py',
7068
])
7169

7270

7371
def test_does_search_instead_of_match(get_files_matching_func):
7472
ret = get_files_matching_func('\.yaml$')
7573
assert ret == set(['manifest.yaml'])
74+
75+
76+
def test_does_not_include_deleted_fileS(get_files_matching_func):
77+
ret = get_files_matching_func('exist.py')
78+
assert ret == set()

0 commit comments

Comments
 (0)