Skip to content

Commit 4c2cf6e

Browse files
committed
Fix parsing .git_archival.txt for archives made from tagged commits.
Given .git_archival.txt content like this: node: 3634907645428b9542cfb4fd1ceef08f14526cb8 node-date: 2022-10-13T20:38:09-06:00 describe-name: v0.0.5 ref-names: HEAD -> main, tag: v0.0.5 we would previously fail with an exception like this: $ python3 -m setuptools_scm Traceback (most recent call last): [..snip..] File ".../setuptools_scm/git.py", line 312, in parse_archival return archival_to_version(data, config=config) File ".../setuptools_scm/git.py", line 286, in archival_to_version tag, number, node, _ = _git_parse_describe(archival_describe) File ".../setuptools_scm/git.py", line 245, in _git_parse_describe tag, number, node = describe_output.rsplit("-", 2) ValueError: not enough values to unpack (expected 3, got 1) The problem is that the describe-name field is "v0.0.5" instead of looking like "v0.0.5-0-g3634907". When we invoke git-describe ourselves, we specify the --long option, but the format-strings implemented by git-archive do not currently give us any way to force long-output in a .git_archival.txt file. This change detects this condition and returns None for the missing fields.
1 parent 9a79676 commit 4c2cf6e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/setuptools_scm/git.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,18 +232,27 @@ def _git_parse_inner(
232232
)
233233

234234

235-
def _git_parse_describe(describe_output: str) -> tuple[str, int, str, bool]:
235+
def _git_parse_describe(describe_output: str) -> tuple[str, int | None, str | None, bool]:
236236
# 'describe_output' looks e.g. like 'v1.5.0-0-g4060507' or
237237
# 'v1.15.1rc1-37-g9bd1298-dirty'.
238+
# It may also just be a bare tag name if this is a tagged commit and we are
239+
# parsing a .git_archival.txt file.
238240

239241
if describe_output.endswith("-dirty"):
240242
dirty = True
241243
describe_output = describe_output[:-6]
242244
else:
243245
dirty = False
244246

245-
tag, number, node = describe_output.rsplit("-", 2)
246-
return tag, int(number), node, dirty
247+
split = describe_output.rsplit("-", 2)
248+
if len(split) < 3: # probably a tagged commit
249+
tag = describe_output
250+
number = None
251+
node = None
252+
else:
253+
tag, number_, node = split
254+
number = int(number_)
255+
return tag, number, node, dirty
247256

248257

249258
def search_parent(dirname: _t.PathT) -> GitWorkdir | None:

testing/test_git.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ def test_git_getdate_signed_commit(signed_commit_wd: WorkDir) -> None:
523523
("0.0", {"node": "0" * 20}),
524524
("1.2.2", {"describe-name": "release-1.2.2-0-g00000"}),
525525
("1.2.2.dev0", {"ref-names": "tag: release-1.2.2.dev"}),
526+
("1.2.2", {"describe-name": "v1.2.2"}),
526527
],
527528
)
528529
@pytest.mark.filterwarnings("ignore:git archive did not support describe output")

0 commit comments

Comments
 (0)