mirror of
https://github.com/pre-commit/pre-commit.git
synced 2026-02-17 08:14:42 +04:00
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.
This commit is contained in:
parent
8416413a0e
commit
66d1c0707e
2 changed files with 17 additions and 1 deletions
|
|
@ -111,7 +111,7 @@ def _ns(
|
||||||
|
|
||||||
|
|
||||||
def _rev_exists(rev: str) -> bool:
|
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(
|
def _pre_push_ns(
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,22 @@ def test_run_legacy_recursive(tmpdir):
|
||||||
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(
|
@pytest.mark.parametrize(
|
||||||
('hook_type', 'args'),
|
('hook_type', 'args'),
|
||||||
(
|
(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue