From c21a41c7ccc0350841cb57733f845dd2054bd285 Mon Sep 17 00:00:00 2001 From: Gavin Mak Date: Tue, 23 Jun 2026 22:47:32 +0000 Subject: [PATCH] git_config: Support SHA-256 object IDs Update ID_RE to match both 40-char (SHA-1) and 64-char (SHA-256) IDs as part of Git 3.0 support. Bug: 483758905 Change-Id: Ie5d9a2a5df4c6e7adda7f1d89f1bfb7724846057 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/600341 Reviewed-by: Brian Gan Tested-by: Gavin Mak Reviewed-by: Mike Frysinger Commit-Queue: Gavin Mak --- git_config.py | 6 +++--- project.py | 2 +- tests/test_git_config.py | 24 ++++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/git_config.py b/git_config.py index 9b3188f1e..888ae888c 100644 --- a/git_config.py +++ b/git_config.py @@ -40,7 +40,7 @@ from repo_trace import Trace # that is saved in the config. SYNC_STATE_PREFIX = "repo.syncstate." -ID_RE = re.compile(r"^[0-9a-f]{40}$") +ID_RE = re.compile(r"^[0-9a-f]{40,64}$") REVIEW_CACHE = {} @@ -49,8 +49,8 @@ def IsChange(rev): return rev.startswith(R_CHANGES) -def IsId(rev): - return ID_RE.match(rev) +def IsId(rev: str) -> bool: + return bool(ID_RE.match(rev)) def IsTag(rev): diff --git a/project.py b/project.py index 0ad558446..0b336b272 100644 --- a/project.py +++ b/project.py @@ -2709,7 +2709,7 @@ class Project: if depth: current_branch_only = True - is_sha1 = bool(IsId(self.revisionExpr)) + is_sha1 = IsId(self.revisionExpr) if current_branch_only: if self.revisionExpr.startswith(R_TAGS): diff --git a/tests/test_git_config.py b/tests/test_git_config.py index 3bc140522..9583f3e9f 100644 --- a/tests/test_git_config.py +++ b/tests/test_git_config.py @@ -244,3 +244,27 @@ def test_remote_save_with_push_url_without_projectname( assert ( written_config.GetString("remote.origin.pushurl") == "ssh://example.com" ) + + +@pytest.mark.parametrize( + "rev, expected", + ( + ("a" * 40, True), + ("0" * 40, True), + ("f" * 40, True), + ("a" * 64, True), + ("0" * 64, True), + ("f" * 64, True), + ("a" * 39, False), + ("a" * 41, True), + ("a" * 63, True), + ("a" * 65, False), + ("g" * 40, False), + ("g" * 64, False), + ("refs/heads/master", False), + ("refs/tags/v1.0", False), + ), +) +def test_is_id(rev: str, expected: bool) -> None: + """Test IsId identifies both SHA-1 and SHA-256 hashes.""" + assert git_config.IsId(rev) == expected