From db97cf3329d39a3ff00a2ddcaf5197cb2630feb5 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Wed, 25 May 2016 08:42:02 -0700 Subject: [PATCH] Don't run on deleted files. Resolves #374 --- pre_commit/git.py | 6 +++++- tests/git_test.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pre_commit/git.py b/pre_commit/git.py index 796a0b8a..1f16b6e0 100644 --- a/pre_commit/git.py +++ b/pre_commit/git.py @@ -69,7 +69,11 @@ def get_conflicted_files(): @memoize_by_cwd def get_staged_files(): - return cmd_output('git', 'diff', '--staged', '--name-only')[1].splitlines() + return cmd_output( + 'git', 'diff', '--staged', '--name-only', + # Everything except for D + '--diff-filter=ACMRTUXB' + )[1].splitlines() @memoize_by_cwd diff --git a/tests/git_test.py b/tests/git_test.py index c4e01450..701d36b4 100644 --- a/tests/git_test.py +++ b/tests/git_test.py @@ -33,6 +33,16 @@ def test_get_root_not_git_dir(tempdir_factory): git.get_root() +def test_get_staged_files_deleted(tempdir_factory): + path = git_dir(tempdir_factory) + with cwd(path): + open('test', 'a').close() + cmd_output('git', 'add', 'test') + cmd_output('git', 'commit', '-m', 'foo', '--allow-empty') + cmd_output('git', 'rm', '--cached', 'test') + assert git.get_staged_files() == [] + + def test_is_not_in_merge_conflict(tempdir_factory): path = git_dir(tempdir_factory) with cwd(path):