Skip to content

Commit 3626e6f

Browse files
authored
Friendlier behaviour when __version__ and git tag mismatch (#9)
1 parent bc1d16f commit 3626e6f

File tree

1 file changed

+23
-23
lines changed

1 file changed

+23
-23
lines changed

_version.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,34 @@ def get_extended_version() -> str:
5050
# Sample first 3 parts of __version__
5151
base_release = ".".join(__version__.split(".")[:3])
5252

53-
# Check release
54-
if not release:
55-
release = base_release
56-
elif release != base_release:
57-
warning(
58-
f"{project_name} version from git ({release})"
59-
f" and __version__ ({base_release}) don't match."
60-
)
61-
62-
# Build the total version
63-
version = release
53+
# Start version string (__version__ string is leading)
54+
version = base_release
55+
tag_prefix = "#"
56+
57+
if release and release != base_release:
58+
# Can happen between bumping and tagging. And also when merging a
59+
# version bump into a working branch, because we use --first-parent.
60+
release2, _post, _labels = get_version_info_from_git(first_parent=False)
61+
if release2 != base_release:
62+
warning(
63+
f"{project_name} version from git ({release})"
64+
f" and __version__ ({base_release}) don't match."
65+
)
66+
version += "+from_tag_" + release.replace(".", "_")
67+
tag_prefix = "."
68+
69+
# Add git info
6470
if post and post != "0":
6571
version += f".post{post}"
6672
if labels:
67-
version += "+" + ".".join(labels)
73+
version += tag_prefix + ".".join(labels)
6874
elif labels and labels[-1] == "dirty":
69-
version += "+" + ".".join(labels)
75+
version += tag_prefix + ".".join(labels)
7076

7177
return version
7278

7379

74-
def get_version_info_from_git() -> str:
80+
def get_version_info_from_git(*, first_parent: bool = True) -> str:
7581
"""
7682
Get (release, post, labels) from Git.
7783
@@ -80,15 +86,9 @@ def get_version_info_from_git() -> str:
8086
git-hash and optionally a dirty flag.
8187
"""
8288
# Call out to Git
83-
command = [
84-
"git",
85-
"describe",
86-
"--long",
87-
"--always",
88-
"--tags",
89-
"--dirty",
90-
"--first-parent",
91-
]
89+
command = ["git", "describe", "--long", "--always", "--tags", "--dirty"]
90+
if first_parent:
91+
command.append("--first-parent")
9292
try:
9393
p = subprocess.run(command, check=False, cwd=repo_dir, capture_output=True)
9494
except Exception as e:

0 commit comments

Comments
 (0)