Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog/next_release/168.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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.<sha>`` instead of ``2024.1.15``.
18 changes: 7 additions & 11 deletions setuptools_git_versioning/archival.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<tag>.+)-(?P<ccount>[0-9]+)-g(?P<sha>[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
Expand Down Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions tests/test_integration/test_archival.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)."""
Expand Down Expand Up @@ -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(
Expand Down
Loading