1414
1515VERSION_PATTERN = re .compile (r"^(\d+\.\d+\.\d+(?:\.\d+)?)$" )
1616DEFAULT_INITIAL_VERSION = "0.0.0.1"
17- EXPECTED_REMOTE_URL = f"git@github.com: { METTA_GITHUB_ORGANIZATION } / { METTA_GITHUB_REPO } .git"
17+
1818DISCORD_CHANNEL_WEBHOOK_URL_SECRET_NAME = "discord/channel-webhook/updates"
1919
2020
@@ -29,6 +29,31 @@ class Package(StrEnum):
2929}
3030
3131
32+ def _check_git_state (force : bool ) -> tuple [str , str ]:
33+ try :
34+ status_output = git .run_git ("status" , "--porcelain" )
35+ except subprocess .CalledProcessError as exc :
36+ error (f"Failed to read git status: { exc } " )
37+ raise typer .Exit (exc .returncode ) from exc
38+
39+ if status_output .strip () and not force :
40+ error ("Working tree is not clean. Commit, stash, or clean changes before publishing (use --force to override)." )
41+ raise typer .Exit (1 )
42+
43+ try :
44+ current_branch = git .run_git ("rev-parse" , "--abbrev-ref" , "HEAD" )
45+ current_commit = git .run_git ("rev-parse" , "HEAD" )
46+ except subprocess .CalledProcessError as exc :
47+ error (f"Failed to determine git state: { exc } " )
48+ raise typer .Exit (exc .returncode ) from exc
49+
50+ if current_branch not in {"main" } and not force :
51+ error ("Publishing is only supported from the main branch. Switch to 'main' or pass --force to override." )
52+ raise typer .Exit (1 )
53+
54+ return current_branch , current_commit
55+
56+
3257def _get_metta_remote () -> str :
3358 try :
3459 remotes_output = git .run_git ("remote" , "-v" )
@@ -75,34 +100,6 @@ def _ensure_tag_unique(package: str, version: str) -> None:
75100 raise typer .Exit (1 )
76101
77102
78- def _push_child_repo (package : str , dry_run : bool ) -> None :
79- if dry_run :
80- info (f" [dry-run] Would push { package } to child repo" )
81- return
82- info (f"Pushing { package } to child repo..." )
83- try :
84- subprocess .run (
85- [f"{ get_repo_root ()} /devops/git/push_child_repo.py" , package , "-y" ],
86- check = True ,
87- )
88- success (f"Pushed { package } to child repo." )
89- except subprocess .CalledProcessError as exc :
90- error (f"Failed to push child repo: { exc } " )
91- raise typer .Exit (exc .returncode ) from exc
92-
93-
94- def _create_and_push_tag (package : str , version : str , remote : str , dry_run : bool ) -> str :
95- tag_name = f"{ package } -v{ version } "
96- if dry_run :
97- info (f" [dry-run] Would create and push tag { tag_name } " )
98- return tag_name
99- info (f"Tagging { package } { version } ..." )
100- git .run_git ("tag" , "-a" , tag_name , "-m" , f"Release { package } { version } " )
101- git .run_git ("push" , remote , tag_name )
102- success (f"Published { tag_name } to { remote } ." )
103- return tag_name
104-
105-
106103def _get_next_version (package : str , version_override : Optional [str ], remote : str ) -> tuple [str , Optional [str ]]:
107104 prefix = f"{ package } -v"
108105
@@ -138,29 +135,16 @@ def _get_next_version(package: str, version_override: Optional[str], remote: str
138135 return target_version , latest_tag
139136
140137
141- def _check_git_state (force : bool ) -> tuple [str , str ]:
142- try :
143- status_output = git .run_git ("status" , "--porcelain" )
144- except subprocess .CalledProcessError as exc :
145- error (f"Failed to read git status: { exc } " )
146- raise typer .Exit (exc .returncode ) from exc
147-
148- if status_output .strip () and not force :
149- error ("Working tree is not clean. Commit, stash, or clean changes before publishing (use --force to override)." )
150- raise typer .Exit (1 )
151-
152- try :
153- current_branch = git .run_git ("rev-parse" , "--abbrev-ref" , "HEAD" )
154- current_commit = git .run_git ("rev-parse" , "HEAD" )
155- except subprocess .CalledProcessError as exc :
156- error (f"Failed to determine git state: { exc } " )
157- raise typer .Exit (exc .returncode ) from exc
158-
159- if current_branch not in {"main" } and not force :
160- error ("Publishing is only supported from the main branch. Switch to 'main' or pass --force to override." )
161- raise typer .Exit (1 )
162-
163- return current_branch , current_commit
138+ def _create_and_push_tag (package : str , version : str , remote : str , dry_run : bool ) -> str :
139+ tag_name = f"{ package } -v{ version } "
140+ if dry_run :
141+ info (f" [dry-run] Would create and push tag { tag_name } " )
142+ return tag_name
143+ info (f"Tagging { package } { version } ..." )
144+ git .run_git ("tag" , "-a" , tag_name , "-m" , f"Release { package } { version } " )
145+ git .run_git ("push" , remote , tag_name )
146+ success (f"Published { tag_name } to { remote } ." )
147+ return tag_name
164148
165149
166150def _post_to_discord (
@@ -209,32 +193,20 @@ def _post_to_discord(
209193 success ("Posted release announcement to Discord." )
210194
211195
212- def cmd_publish (
213- package : Annotated [Package , typer .Argument (help = "Package to publish" )],
214- version : Annotated [
215- Optional [str ],
216- typer .Option ("--version" , "-v" , help = "Explicit version to tag (digits separated by dots)" ),
217- ] = None ,
218- dry_run : Annotated [bool , typer .Option ("--dry-run" , help = "Preview actions without executing" )] = False ,
219- repo_only : Annotated [bool , typer .Option ("--repo-only" , help = "Only push to child repo" )] = False ,
220- tag_only : Annotated [bool , typer .Option ("--tag-only" , help = "Only create/push tag (skip child repo)" )] = False ,
221- force : Annotated [bool , typer .Option ("--force" , help = "Bypass branch and clean checks" )] = False ,
222- ):
223- if repo_only and tag_only :
224- error ("Cannot use --repo-only and --tag-only together" )
225- raise typer .Exit (1 )
226-
227- remote = _get_metta_remote ()
228-
229- _publish (
230- package = package ,
231- version = version ,
232- dry_run = dry_run ,
233- repo_only = repo_only ,
234- tag_only = tag_only ,
235- remote = remote ,
236- force = force ,
237- )
196+ def _push_child_repo (package : str , dry_run : bool ) -> None :
197+ if dry_run :
198+ info (f" [dry-run] Would push { package } to child repo" )
199+ return
200+ info (f"Pushing { package } to child repo..." )
201+ try :
202+ subprocess .run (
203+ [f"{ get_repo_root ()} /devops/git/push_child_repo.py" , package , "-y" ],
204+ check = True ,
205+ )
206+ success (f"Pushed { package } to child repo." )
207+ except subprocess .CalledProcessError as exc :
208+ error (f"Failed to push child repo: { exc } " )
209+ raise typer .Exit (exc .returncode ) from exc
238210
239211
240212def _publish (
@@ -308,3 +280,31 @@ def _publish(
308280
309281 if dry_run :
310282 success ("Dry run complete." )
283+
284+
285+ def cmd_publish (
286+ package : Annotated [Package , typer .Argument (help = "Package to publish" )],
287+ version : Annotated [
288+ Optional [str ],
289+ typer .Option ("--version" , "-v" , help = "Explicit version to tag (digits separated by dots)" ),
290+ ] = None ,
291+ dry_run : Annotated [bool , typer .Option ("--dry-run" , help = "Preview actions without executing" )] = False ,
292+ repo_only : Annotated [bool , typer .Option ("--repo-only" , help = "Only push to child repo" )] = False ,
293+ tag_only : Annotated [bool , typer .Option ("--tag-only" , help = "Only create/push tag (skip child repo)" )] = False ,
294+ force : Annotated [bool , typer .Option ("--force" , help = "Bypass branch and clean checks" )] = False ,
295+ ):
296+ if repo_only and tag_only :
297+ error ("Cannot use --repo-only and --tag-only together" )
298+ raise typer .Exit (1 )
299+
300+ remote = _get_metta_remote ()
301+
302+ _publish (
303+ package = package ,
304+ version = version ,
305+ dry_run = dry_run ,
306+ repo_only = repo_only ,
307+ tag_only = tag_only ,
308+ remote = remote ,
309+ force = force ,
310+ )
0 commit comments