Skip to content

Commit 265943f

Browse files
committed
Guard git ref resolution against an empty revision
sha.startswith(rev) matched the first reference unconditionally when rev was an empty string, returning a bogus branch/tag tuple for any caller that passed in an uninitialised SHA. Return early when rev is empty. https://claude.ai/code/session_01KKvrvnVvsBChohuxbRRmzA
1 parent 5f01197 commit 265943f

3 files changed

Lines changed: 12 additions & 0 deletions

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Release 0.14.1 (unreleased)
44
* Fix ``dfetch import`` mangling the namespace of a generic VCS URL whose path contains ``.git`` other than as a suffix
55
* Fix ``Version`` comparison raising ``AttributeError`` when compared against a non-``Version`` object
66
* Fix ``dfetch add`` matching a remote whose base URL is only a string prefix (not a path prefix) of the project URL
7+
* Fix git ref resolution spuriously matching the first reference for an empty revision
78

89
Release 0.14.0 (released 2026-06-14)
910
===========================

dfetch/vcs/git.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ def _find_branch_tip_or_tag_from_sha(
277277
) -> tuple[str, str]:
278278
"""Check all branch tips and tags and see if the sha is one of them."""
279279
branch, tag = "", ""
280+
if not rev:
281+
return (branch, tag)
280282
for reference, sha in info.items():
281283
if sha.startswith(rev): # Also allow for shorter SHA's
282284
if reference.startswith("refs/tags/"):

tests/test_git_vcs.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,15 @@ def test_ls_remote():
318318
assert info == expected
319319

320320

321+
def test_find_branch_tip_or_tag_from_sha_empty_rev_matches_nothing():
322+
"""An empty revision must not spuriously match the first reference."""
323+
info = {
324+
"refs/heads/master": "33d11e10699bae03ba2a58a280e92494f4fa0d82",
325+
"refs/tags/v1.0": "0e3b216c7ab365b67765e94aeb45085c4db029e0",
326+
}
327+
assert GitRemote._find_branch_tip_or_tag_from_sha(info, "") == ("", "")
328+
329+
321330
@pytest.mark.parametrize(
322331
"name, env_ssh, git_config_ssh, expected",
323332
[

0 commit comments

Comments
 (0)