Skip to content

Commit 44ee099

Browse files
committed
#99: Gate empty initial commit behavior to Git 2.7+
1 parent cb49693 commit 44ee099

File tree

3 files changed

+28
-24
lines changed

3 files changed

+28
-24
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
## Unreleased
22

3-
* Changed: Previously, if the initial commit were both tagged and empty,
3+
* Changed: Previously, if Git's initial commit were both tagged and empty,
44
Dunamai would raise an exception due to Git not reporting that tag in
55
`git log --simplify-by-decoration`.
6-
Now, if a tag is missing from that list, Dunamai treats it as the oldest tag,
7-
since, in practice, this only seems to affect the initial commit.
6+
Now, if a tag is missing from that list, Dunamai treats it as the oldest tag.
7+
This new behavior is only enabled for Git 2.7+.
88

99
## v1.23.2 (2025-05-06)
1010

dunamai/__init__.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,19 +458,21 @@ def best_date(self) -> Optional[dt.datetime]:
458458
else:
459459
return self.creatordate
460460

461-
@property
462-
def commit_offset(self) -> int:
461+
def commit_offset(self, git_version: List[int]) -> int:
463462
try:
464463
return self.tag_topo_lookup[self.fullref]
465464
except KeyError:
466-
# This mainly happens when the initial commit is both empty and tagged.
467-
# People can't really fix this in an old/existing repository,
468-
# so we just sort the tag to the oldest position.
469-
return sys.maxsize
465+
if git_version < [2, 7]:
466+
raise RuntimeError(
467+
"Unable to determine commit offset for ref {}. Is the initial commit both tagged and empty? Data: {}".format(
468+
self.fullref, self.tag_topo_lookup
469+
)
470+
)
471+
else:
472+
return sys.maxsize
470473

471-
@property
472-
def sort_key(self) -> Tuple[int, Optional[dt.datetime]]:
473-
return (-self.commit_offset, self.best_date())
474+
def sort_key(self, git_version: List[int]) -> Tuple[int, Optional[dt.datetime]]:
475+
return (-self.commit_offset(git_version), self.best_date())
474476

475477
@property
476478
def ref(self) -> str:
@@ -1243,7 +1245,7 @@ def from_git(
12431245
continue
12441246
detailed_tags.append(_GitRefInfo(*parts).with_tag_topo_lookup(tag_topo_lookup))
12451247

1246-
tags = [t.ref for t in sorted(detailed_tags, key=lambda x: x.sort_key, reverse=True)]
1248+
tags = [t.ref for t in sorted(detailed_tags, key=lambda x: x.sort_key(git_version), reverse=True)]
12471249
matched_pattern = _match_version_pattern(pattern, tags, latest_tag, strict, pattern_prefix)
12481250

12491251
if matched_pattern is None:

tests/integration/test_dunamai.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -534,22 +534,24 @@ def test__version__from_git__initial_commit_empty_and_tagged(tmp_path) -> None:
534534
run("git init")
535535
run("git commit --no-gpg-sign --allow-empty -m Initial")
536536
run("git tag v0.1.0 -m Release")
537-
assert run("dunamai from git") == "0.1.0"
538537

539-
run("git commit --no-gpg-sign --allow-empty -m Second")
540-
assert run("dunamai from git").startswith("0.1.0.post1.dev0+")
538+
if is_git_legacy():
539+
with pytest.raises(RuntimeError):
540+
Version.from_git()
541+
else:
542+
assert run("dunamai from git") == "0.1.0"
541543

542-
run("git tag v0.2.0 -m Release")
543-
if _get_git_version() < [2, 7]:
544+
run("git commit --no-gpg-sign --allow-empty -m Second")
544545
assert run("dunamai from git").startswith("0.1.0.post1.dev0+")
545-
else:
546+
547+
run("git tag v0.2.0 -m Release")
546548
assert run("dunamai from git") == "0.2.0"
547549

548-
(vcs / "foo.txt").write_text("hi")
549-
run("git add .")
550-
run("git commit --no-gpg-sign -m Third")
551-
run("git tag v0.3.0 -m Release")
552-
assert run("dunamai from git") == "0.3.0"
550+
(vcs / "foo.txt").write_text("hi")
551+
run("git add .")
552+
run("git commit --no-gpg-sign -m Third")
553+
run("git tag v0.3.0 -m Release")
554+
assert run("dunamai from git") == "0.3.0"
553555

554556

555557
@pytest.mark.skipif(shutil.which("git") is None, reason="Requires Git")

0 commit comments

Comments
 (0)