From da741a249e5fd78ec4ae6ea491b57a615edfe1c2 Mon Sep 17 00:00:00 2001 From: "SU, Yanyu" <238259854+TohruSu@users.noreply.github.com> Date: Wed, 11 Nov 2026 12:09:47 +0900 Subject: [PATCH] Fix .git_archival.txt parsing for tags like 2024-01-15 --- docs/changelog/next_release/168.bugfix.rst | 1 + setuptools_git_versioning/archival.py | 18 ++++----- tests/test_integration/test_archival.py | 46 ++++++++++++++++++++++ 3 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 docs/changelog/next_release/168.bugfix.rst diff --git a/docs/changelog/next_release/168.bugfix.rst b/docs/changelog/next_release/168.bugfix.rst new file mode 100644 index 0000000..7a18b37 --- /dev/null +++ b/docs/changelog/next_release/168.bugfix.rst @@ -0,0 +1 @@ +Fixed version detection from ``.git_archival.txt`` when the archived commit is on a tag like ``2024-01-15``. The tag was parsed as ``2024`` plus one commit, giving ``2024.post1+git.`` instead of ``2024.1.15``. diff --git a/setuptools_git_versioning/archival.py b/setuptools_git_versioning/archival.py index 98f7fca..40241e6 100644 --- a/setuptools_git_versioning/archival.py +++ b/setuptools_git_versioning/archival.py @@ -25,8 +25,8 @@ ARCHIVAL_FILENAME = ".git_archival.txt" DESCRIBE_UNSUPPORTED = "%(describe" FORMAT_UNSUBSTITUTED = "$Format" -DESCRIBE_PARTS = 3 # tag-N-gSHA +DESCRIBE_RE = re.compile(r"^(?P.+)-(?P[0-9]+)-g(?P[0-9a-f]+)$") # tag-N-gSHA REF_TAG_RE = re.compile(r"(?<=\btag: )([^,]+)\b") REF_HEAD_RE = re.compile(r"HEAD\s*->\s*([^,]+)") FULL_SHA_RE = re.compile(r"^([0-9a-f]{40}|[0-9a-f]{64})$") # SHA-1 or SHA-256 @@ -74,18 +74,14 @@ def _parse_describe(describe: str) -> tuple[str, int, str | None, bool]: dirty = True describe = describe[: -len("-dirty")] - parts = describe.rsplit("-", 2) - if len(parts) < DESCRIBE_PARTS: + # On a tagged commit, `git describe` prints the bare tag. Only split off + # the suffix when it really is `-N-gSHA`, so that tags like `2024-01-15` + # are not mistaken for a tag followed by a commit count and a SHA. + match = DESCRIBE_RE.match(describe) + if not match: return describe, 0, None, dirty - tag, ccount_str, gnode = parts - try: - ccount = int(ccount_str) - except ValueError: - return describe, 0, None, dirty - - short_sha = gnode[1:] if gnode.startswith("g") else gnode - return tag, ccount, short_sha, dirty + return match.group("tag"), int(match.group("ccount")), match.group("sha"), dirty def _branch_from_ref_names(ref_names: str) -> str | None: diff --git a/tests/test_integration/test_archival.py b/tests/test_integration/test_archival.py index d97bf49..f36c3bb 100644 --- a/tests/test_integration/test_archival.py +++ b/tests/test_integration/test_archival.py @@ -221,6 +221,34 @@ def test_archival_to_version_data_describe_with_non_numeric_middle_part() -> Non assert result.ccount == 0 +@pytest.mark.parametrize("tag", ["2024-01-15", "v2024-01-15", "1.2.3-4-gamma"]) +def test_archival_to_version_data_bare_tag_resembling_describe_suffix(tag: str) -> None: + """For a commit exactly on a tag, describe-name is the bare tag. A tag + whose last two hyphen-separated parts merely resemble the `-N-gSHA` + suffix (e.g., a date) must not be split into tag, ccount and SHA. + """ + data = { + "node": "4060507deadbeef0123456789abcdef012345678", + "describe-name": tag, + } + result = get_data_from_archival_file(data) + assert result is not None + assert result.tag == tag + assert result.ccount == 0 + assert result.sha == "4060507d" + + +def test_archival_to_version_data_hyphenated_tag_post_tag() -> None: + data = { + "node": "4060507deadbeef0123456789abcdef012345678", + "describe-name": "2024-01-15-3-g4060507", + } + result = get_data_from_archival_file(data) + assert result is not None + assert result.tag == "2024-01-15" + assert result.ccount == 3 + + def test_archival_to_version_data_short_sha_fallback_when_node_missing() -> None: """When `node` is absent/invalid, full_sha should fall back to the short SHA from describe-name (best-effort).""" @@ -425,6 +453,24 @@ def test_archival_end_to_end_bare_tag(repo: Path, tmp_path_factory: pytest.TempP assert get_version(extracted) == "1.2.3" +@pytest.mark.important +def test_archival_end_to_end_bare_hyphenated_tag( + repo: Path, + tmp_path_factory: pytest.TempPathFactory, + create_config, +) -> None: + create_config(repo, {"template": "{tag}"}) + _add_archival_template(repo) + create_tag(repo, "2024-01-15") + assert get_version(repo) == "2024.1.15" + + extracted = tmp_path_factory.mktemp("extracted") + _git_archive_extract(repo, extracted) + shutil.copy(repo / ".coveragerc", extracted / ".coveragerc") + + assert get_version(extracted) == "2024.1.15" + + @pytest.mark.important def test_archival_end_to_end_tag_formatter(repo: Path, tmp_path_factory: pytest.TempPathFactory, create_config) -> None: create_file(