From 9401849c6675eae1e81fa8233e43ec031da512a4 Mon Sep 17 00:00:00 2001 From: mtkennerly Date: Fri, 3 May 2024 22:26:16 -0400 Subject: [PATCH] #85: Fix distance for Git when there are tags and none match --- CHANGELOG.md | 5 +++++ dunamai/__init__.py | 10 ++++------ tests/integration/test_dunamai.py | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f19851..7e1c366 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## Unreleased + +* Fixed: Distance was calculated inconsistently for Git + when there were some tags and none matched the version pattern. + ## v1.21.0 (2024-04-29) * Generally, when Dunamai can detect the VCS in use, but there's no version set yet, diff --git a/dunamai/__init__.py b/dunamai/__init__.py index 1fbb6ef..f24af34 100644 --- a/dunamai/__init__.py +++ b/dunamai/__init__.py @@ -1243,13 +1243,11 @@ def from_git( ) if matched_pattern is None: - distance = 0 - - code, msg = _run_cmd("git rev-list --max-parents=0 HEAD", path) - if msg: - initial_commit = msg.splitlines()[0].strip() - code, msg = _run_cmd("git rev-list --count {}..HEAD".format(initial_commit), path) + try: + code, msg = _run_cmd("git rev-list --count HEAD", path) distance = int(msg) + except Exception: + distance = 0 return cls._fallback( strict, diff --git a/tests/integration/test_dunamai.py b/tests/integration/test_dunamai.py index 45fcbfb..30b6be3 100644 --- a/tests/integration/test_dunamai.py +++ b/tests/integration/test_dunamai.py @@ -135,7 +135,7 @@ def test__version__from_git__with_annotated_tags(tmp_path) -> None: # Additional one-off check not in other VCS integration tests: # when the only tag in the repository does not match the pattern. run("git tag other -m Annotated") - assert from_vcs() == Version("0.0.0", dirty=False, branch=b) + assert from_vcs() == Version("0.0.0", distance=1, dirty=False, branch=b) with pytest.raises(ValueError): from_vcs(strict=True)