Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
perf: use git cat-file -e instead of git rev-list in _rev_exists
Replace git rev-list --quiet with git cat-file -e for checking if a
revision exists. This changes the operation from O(commits) to O(1).

Benchmarks from a large monorepo:
- git rev-list --quiet <sha>: 5150ms
- git cat-file -e <sha>: 4ms

Both commands have the same semantics: return success if the revision
exists in the local repo, failure otherwise.
  • Loading branch information
claude committed Dec 24, 2025
commit 66d1c0707e4139e548ffe052f719486adb02c305
2 changes: 1 addition & 1 deletion pre_commit/commands/hook_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/commands/hook_impl_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
(
Expand Down