Skip to content

Commit d799f83

Browse files
committed
Simplify versioning - don't append dirty, try get _version file first
1 parent a55ed48 commit d799f83

File tree

2 files changed

+18
-57
lines changed

2 files changed

+18
-57
lines changed

setup.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ def version(version) -> str:
2323

2424
def local_version(version) -> str:
2525
"""
26-
Format the local version using the git hash and status of the working directory.
27-
If all succeeds, the returned string will be something like "63fe5aa1-dirty"
26+
Format the local version to include only the git commit hash.
2827
This function is called by setuptools_scm to determine the local version part.
2928
"""
30-
suffix = ""
31-
3229
try:
3330
# Get the short git hash
3431
result = subprocess.run(
@@ -37,20 +34,12 @@ def local_version(version) -> str:
3734
commit = result.stdout.strip()
3835

3936
if commit:
40-
suffix = f"{commit}"
41-
42-
# Check if working directory is dirty
43-
status_result = subprocess.run(
44-
["git", "status", "--porcelain"], capture_output=True, text=True, check=True
45-
)
46-
47-
if status_result.stdout.strip():
48-
suffix += "-dirty"
37+
return f"-{commit}"
4938
except (subprocess.SubprocessError, FileNotFoundError):
5039
# Ignore errors from git commands
5140
pass
5241

53-
return suffix
42+
return ""
5443

5544

5645
# Using setuptools_scm with custom version functions

src/warnet/main.py

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,62 +23,34 @@ def cli():
2323
@click.command()
2424
def version() -> None:
2525
"""Display the installed version of warnet"""
26-
# Try to get dynamic version from the Git repository
27-
# This allows a developer to get up-to-date version information
28-
# depending on the state of their local git repository
29-
# (e.g. when installed from source in editable mode).
26+
# First try to get the version from the installed package
27+
try:
28+
from warnet._version import __version__
29+
version_str = __version__
30+
except ImportError:
31+
version_str = "0.0.0"
32+
33+
# Try to get git commit to append
3034
try:
3135
# Check if we're in a git repository
3236
subprocess.check_output(
3337
["git", "rev-parse", "--is-inside-work-tree"], stderr=subprocess.DEVNULL
3438
)
3539

36-
# Get the base version from the latest tag
37-
try:
38-
tag = (
39-
subprocess.check_output(
40-
["git", "describe", "--tags", "--abbrev=0"],
41-
stderr=subprocess.DEVNULL,
42-
text=True,
43-
)
44-
.strip()
45-
.lstrip("v")
46-
)
47-
except subprocess.SubprocessError:
48-
# No tags found
49-
tag = "0.0.0"
50-
5140
# Get the short commit hash
5241
commit = subprocess.check_output(
5342
["git", "rev-parse", "--short=8", "HEAD"], stderr=subprocess.DEVNULL, text=True
5443
).strip()
5544

56-
# Check if there are uncommitted changes
57-
status = subprocess.check_output(
58-
["git", "status", "--porcelain"], stderr=subprocess.DEVNULL, text=True
59-
).strip()
60-
61-
# Format the version string to match setup.py
62-
version_str = tag
63-
if commit:
64-
version_str += f".{commit}"
65-
if status:
66-
version_str += "-dirty"
45+
# If we have a commit hash, append it to the version
46+
# Don't append if already has a hash
47+
if commit and "-" not in version_str:
48+
version_str = f"{version_str}-{commit}"
6749

68-
click.echo(f"warnet version {version_str} (from git)")
69-
return
50+
click.echo(f"warnet version {version_str}")
7051
except (subprocess.SubprocessError, FileNotFoundError):
71-
# Git commands failed or git not available, fall back to installed version
72-
pass
73-
74-
# Fall back to the version file generated during installation
75-
try:
76-
from warnet._version import __version__
77-
78-
version = __version__
79-
click.echo(f"warnet version {version}")
80-
except ImportError:
81-
click.echo("warnet version unknown")
52+
# Git commands failed or git not available, just use the version without the commit
53+
click.echo(f"warnet version {version_str}")
8254

8355

8456
cli.add_command(admin)

0 commit comments

Comments
 (0)