1+ import subprocess
2+
13import click
24
35from .admin import admin
@@ -21,12 +23,59 @@ def cli():
2123@click .command ()
2224def version () -> None :
2325 """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).
30+ try :
31+ # Check if we're in a git repository
32+ subprocess .check_output (
33+ ["git" , "rev-parse" , "--is-inside-work-tree" ], stderr = subprocess .DEVNULL
34+ )
35+
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+
51+ # Get the short commit hash
52+ commit = subprocess .check_output (
53+ ["git" , "rev-parse" , "--short=8" , "HEAD" ], stderr = subprocess .DEVNULL , text = True
54+ ).strip ()
55+
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"
67+
68+ click .echo (f"warnet version { version_str } (from git)" )
69+ return
70+ 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
2475 try :
2576 from warnet ._version import __version__
2677
2778 version = __version__
28- # If running from source/git, setuptools_scm will append git info
29- # e.g. "1.1.11.dev1+g123456[.dirty]"
3079 click .echo (f"warnet version { version } " )
3180 except ImportError :
3281 click .echo ("warnet version unknown" )
0 commit comments