Skip to content

Commit

Permalink
#85: Fix distance for Git when there are tags and none match
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed May 4, 2024
1 parent e6d28dd commit 9401849
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
10 changes: 4 additions & 6 deletions dunamai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_dunamai.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 9401849

Please sign in to comment.