Skip to content

Commit db97cf3

Browse files
committed
Don't run on deleted files. Resolves #374
1 parent 6654fee commit db97cf3

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

pre_commit/git.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def get_conflicted_files():
6969

7070
@memoize_by_cwd
7171
def get_staged_files():
72-
return cmd_output('git', 'diff', '--staged', '--name-only')[1].splitlines()
72+
return cmd_output(
73+
'git', 'diff', '--staged', '--name-only',
74+
# Everything except for D
75+
'--diff-filter=ACMRTUXB'
76+
)[1].splitlines()
7377

7478

7579
@memoize_by_cwd

tests/git_test.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ def test_get_root_not_git_dir(tempdir_factory):
3333
git.get_root()
3434

3535

36+
def test_get_staged_files_deleted(tempdir_factory):
37+
path = git_dir(tempdir_factory)
38+
with cwd(path):
39+
open('test', 'a').close()
40+
cmd_output('git', 'add', 'test')
41+
cmd_output('git', 'commit', '-m', 'foo', '--allow-empty')
42+
cmd_output('git', 'rm', '--cached', 'test')
43+
assert git.get_staged_files() == []
44+
45+
3646
def test_is_not_in_merge_conflict(tempdir_factory):
3747
path = git_dir(tempdir_factory)
3848
with cwd(path):

0 commit comments

Comments
 (0)