1010VERSION_PATTERN = re .compile (r"__version__\s*=\s*['\"]([^'\"]+)['\"]" )
1111PYPROJECT_PATTERN = re .compile (r'^version\s*=\s*".*"$' , re .MULTILINE )
1212
13- def get_git_tag ():
14- try :
15- tag = subprocess .check_output ([
16- "git" , "describe" , "--tags" , "--exact-match"
17- ], stderr = subprocess .DEVNULL , text = True ).strip ()
18- return tag .lstrip ("v" )
19- except subprocess .CalledProcessError :
20- return None
13+ def read_version_from_init (path : pathlib .Path ) -> str :
14+ content = path .read_text ()
15+ match = VERSION_PATTERN .search (content )
16+ if not match :
17+ print (f"❌ Could not find __version__ in { path } " )
18+ sys .exit (1 )
19+ return match .group (1 )
2120
22- def get_latest_tag () :
21+ def read_version_from_git ( path : str ) -> str :
2322 try :
24- tag = subprocess .check_output ([
25- "git" , "describe" , "--tags" , "--abbrev=0"
26- ], text = True ).strip ()
27- return tag .lstrip ("v" )
23+ output = subprocess .check_output (["git" , "show" , f"HEAD:{ path } " ], text = True )
24+ match = VERSION_PATTERN .search (output )
25+ if not match :
26+ return None
27+ return match .group (1 )
2828 except subprocess .CalledProcessError :
29- return "0.0.0"
29+ return None
3030
31- def get_commit_count_since (tag ):
32- try :
33- output = subprocess .check_output ([
34- "git" , "rev-list" , f"v{ tag } ..HEAD" , "--count"
35- ], text = True ).strip ()
36- return int (output )
37- except subprocess .CalledProcessError :
38- return 0
31+ def bump_dev_version (version : str ) -> str :
32+ if ".dev" in version :
33+ base , dev = version .split (".dev" )
34+ return f"{ base } .dev{ int (dev )+ 1 } "
35+ else :
36+ return f"{ version } .dev1"
3937
4038def inject_version (version : str ):
41- print (f"\U0001f501 Injecting version: { version } " )
39+ print (f"🔁 Updating version to : { version } " )
4240
4341 # Update __init__.py
4442 init_content = INIT_FILE .read_text ()
@@ -54,20 +52,19 @@ def inject_version(version: str):
5452 PYPROJECT_FILE .write_text (new_pyproject )
5553
5654def main ():
57- dev_mode = "--dev" in sys .argv
55+ current_version = read_version_from_init (INIT_FILE )
56+ previous_version = read_version_from_git ("socketsecurity/__init__.py" )
5857
59- if dev_mode :
60- base = get_latest_tag ()
61- commits = get_commit_count_since (base )
62- version = f"{ base } .dev{ commits } "
63- else :
64- version = get_git_tag ()
65- if not version :
66- print ("\u274c Error: No exact tag found for release." )
67- sys .exit (1 )
58+ print (f"Current: { current_version } , Previous: { previous_version } " )
6859
69- inject_version (version )
70- print (f"\u2705 Injected { 'dev' if dev_mode else 'release' } version: { version } " )
60+ if current_version == previous_version :
61+ new_version = bump_dev_version (current_version )
62+ inject_version (new_version )
63+ print ("⚠️ Version was unchanged — auto-bumped. Please git add + commit again." )
64+ sys .exit (1 )
65+ else :
66+ print ("✅ Version already bumped — proceeding." )
67+ sys .exit (0 )
7168
7269if __name__ == "__main__" :
73- main ()
70+ main ()
0 commit comments