diff --git a/pre_commit/commands/hook_impl.py b/pre_commit/commands/hook_impl.py index de5c8f346..ddfac5194 100644 --- a/pre_commit/commands/hook_impl.py +++ b/pre_commit/commands/hook_impl.py @@ -111,7 +111,7 @@ def _ns( def _rev_exists(rev: str) -> bool: - return not subprocess.call(('git', 'rev-list', '--quiet', rev)) + return subprocess.call(('git', 'cat-file', '-e', rev)) == 0 def _pre_push_ns( diff --git a/tests/commands/hook_impl_test.py b/tests/commands/hook_impl_test.py index d757e85c0..f5a49be8c 100644 --- a/tests/commands/hook_impl_test.py +++ b/tests/commands/hook_impl_test.py @@ -91,6 +91,22 @@ def call(*_, **__): call() +def test_rev_exists_with_existing_rev(tempdir_factory): + src = git_dir(tempdir_factory) + git_commit(cwd=src) + head = git.head_rev(src) + with cwd(src): + assert hook_impl._rev_exists(head) is True + + +def test_rev_exists_with_nonexistent_rev(tempdir_factory): + src = git_dir(tempdir_factory) + git_commit(cwd=src) + with cwd(src): + fake_sha = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef' + assert hook_impl._rev_exists(fake_sha) is False + + @pytest.mark.parametrize( ('hook_type', 'args'), (