Skip to content

Commit 0b0750c

Browse files
fix #804: consider fallback version befor defaulting to 0.0 in git
1 parent e53d16d commit 0b0750c

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/setuptools_scm/git.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ def _git_parse_inner(
364364

365365
if version is None:
366366
# If 'git git_describe_command' failed, try to get the information otherwise.
367-
tag = config.version_cls("0.0")
367+
tag = config.version_cls(config.fallback_version or "0.0")
368368
node = wd.node()
369369
if node is None:
370370
distance = 0

testing/test_git.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,3 +823,31 @@ def test_git_describe_command_init_conflict() -> None:
823823
git=GitConfiguration(describe_command="new command")
824824
),
825825
)
826+
827+
828+
def test_git_no_commits_uses_fallback_version(wd: WorkDir) -> None:
829+
"""Test that when git describe fails (no commits), fallback_version is used instead of 0.0."""
830+
# Reinitialize as empty repo to remove any existing commits
831+
wd("rm -rf .git")
832+
wd("git init")
833+
wd("git config user.email [email protected]")
834+
wd('git config user.name "a test"')
835+
836+
# Test with fallback_version set - should use the fallback instead of "0.0"
837+
config = Configuration(fallback_version="1.2.3")
838+
version = git.parse(str(wd.cwd), config)
839+
840+
# Should get a version starting with the fallback version
841+
assert version is not None
842+
assert str(version.tag) == "1.2.3"
843+
assert version.distance == 0
844+
assert version.dirty is True # No commits means dirty
845+
846+
# Test without fallback_version - should default to "0.0"
847+
config_no_fallback = Configuration()
848+
version_no_fallback = git.parse(str(wd.cwd), config_no_fallback)
849+
850+
assert version_no_fallback is not None
851+
assert str(version_no_fallback.tag) == "0.0"
852+
assert version_no_fallback.distance == 0
853+
assert version_no_fallback.dirty is True

0 commit comments

Comments
 (0)