FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Accept SHA-256 all-zero OIDs in pre-push deletion detection by Eryx-Z · Pull Request #3781 · pre-commit/pre-commit · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
13 changes: 11 additions & 2 deletions pre_commit/commands/hook_impl.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@
Z40 = '0' * 40


def _is_zero_oid(oid: str) -> bool:
"""True for the all-zero OID of either supported object format.

Git SHA-1 repos use a 40-hex zero OID; SHA-256 repos use 64. Hardcoding
only the SHA-1 form breaks ``git push --delete`` on SHA-256 repos (#3664).
"""
return len(oid) in (40, 64) and set(oid) == {'0'}


def _run_legacy(
hook_type: str,
hook_dir: str | None,
Expand Down Expand Up @@ -128,9 +137,9 @@ def _pre_push_ns(
for line in stdin.decode().splitlines():
parts = line.rsplit(maxsplit=3)
local_branch, local_sha, remote_branch, remote_sha = parts
if local_sha == Z40:
if _is_zero_oid(local_sha):
continue
elif remote_sha != Z40 and _rev_exists(remote_sha):
elif not _is_zero_oid(remote_sha) and _rev_exists(remote_sha):
return _ns(
'pre-push', color,
from_ref=remote_sha, to_ref=local_sha,
Expand Down
24 changes: 24 additions & 0 deletions tests/commands/hook_impl_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,27 @@ def test_hook_impl_main_runs_hooks(cap_out, tempdir_factory, store):
Block if "DO NOT COMMIT" is found....................(no files to check)Skipped
'''
assert cap_out.get() == expected


def test_is_zero_oid():
assert hook_impl._is_zero_oid('0' * 40)
assert hook_impl._is_zero_oid('0' * 64)
assert not hook_impl._is_zero_oid('0' * 39)
assert not hook_impl._is_zero_oid('0' * 41)
assert not hook_impl._is_zero_oid('0' * 63)
assert not hook_impl._is_zero_oid('0' * 65)
assert not hook_impl._is_zero_oid('1' + '0' * 39)
assert not hook_impl._is_zero_oid('')


def test_run_ns_pre_push_deleting_branch_sha256(push_example):
"""Deletion pushes use a 64-zero OID on SHA-256 repos (#3664)."""
src, src_head, clone, _ = push_example
z64 = '0' * 64

with contextlib.chdir(clone):
args = ('origin', src)
stdin = f'(delete) {z64} refs/heads/b {src_head}'.encode()
ns = hook_impl._run_ns('pre-push', False, args, stdin)

assert ns is None
Loading

Back | FazBrowse Home | New Git URL