@@ -89,13 +89,7 @@ def prepare() -> None:
89
89
"""
90
90
91
91
# Make sure we're in a git repo.
92
- try :
93
- repo = git .Repo ()
94
- except git .InvalidGitRepositoryError :
95
- raise click .ClickException ("Not in Synapse repo." )
96
-
97
- if repo .is_dirty ():
98
- raise click .ClickException ("Uncommitted changes exist." )
92
+ repo = get_repo_and_check_clean_checkout ()
99
93
100
94
click .secho ("Updating git repo..." )
101
95
repo .remote ().fetch ()
@@ -171,9 +165,7 @@ def prepare() -> None:
171
165
assert not parsed_new_version .is_devrelease
172
166
assert not parsed_new_version .is_postrelease
173
167
174
- release_branch_name = (
175
- f"release-v{ parsed_new_version .major } .{ parsed_new_version .minor } "
176
- )
168
+ release_branch_name = get_release_branch_name (parsed_new_version )
177
169
release_branch = find_ref (repo , release_branch_name )
178
170
if release_branch :
179
171
if release_branch .is_remote ():
@@ -274,13 +266,7 @@ def tag(gh_token: Optional[str]) -> None:
274
266
"""Tags the release and generates a draft GitHub release"""
275
267
276
268
# Make sure we're in a git repo.
277
- try :
278
- repo = git .Repo ()
279
- except git .InvalidGitRepositoryError :
280
- raise click .ClickException ("Not in Synapse repo." )
281
-
282
- if repo .is_dirty ():
283
- raise click .ClickException ("Uncommitted changes exist." )
269
+ repo = get_repo_and_check_clean_checkout ()
284
270
285
271
click .secho ("Updating git repo..." )
286
272
repo .remote ().fetch ()
@@ -293,6 +279,15 @@ def tag(gh_token: Optional[str]) -> None:
293
279
if tag_name in repo .tags :
294
280
raise click .ClickException (f"Tag { tag_name } already exists!\n " )
295
281
282
+ # Check we're on the right release branch
283
+ release_branch = get_release_branch_name (current_version )
284
+ if repo .active_branch .name != release_branch :
285
+ click .echo (
286
+ f"Need to be on the release branch ({ release_branch } ) before tagging. "
287
+ f"Currently on ({ repo .active_branch .name } )."
288
+ )
289
+ click .get_current_context ().abort ()
290
+
296
291
# Get the appropriate changelogs and tag.
297
292
changes = get_changes_for_version (current_version )
298
293
@@ -358,21 +353,15 @@ def tag(gh_token: Optional[str]) -> None:
358
353
@cli .command ()
359
354
@click .option ("--gh-token" , envvar = ["GH_TOKEN" , "GITHUB_TOKEN" ], required = True )
360
355
def publish (gh_token : str ) -> None :
361
- """Publish release."""
356
+ """Publish release on GitHub ."""
362
357
363
358
# Make sure we're in a git repo.
364
- try :
365
- repo = git .Repo ()
366
- except git .InvalidGitRepositoryError :
367
- raise click .ClickException ("Not in Synapse repo." )
368
-
369
- if repo .is_dirty ():
370
- raise click .ClickException ("Uncommitted changes exist." )
359
+ get_repo_and_check_clean_checkout ()
371
360
372
361
current_version = get_package_version ()
373
362
tag_name = f"v{ current_version } "
374
363
375
- if not click .confirm (f"Publish { tag_name } ?" , default = True ):
364
+ if not click .confirm (f"Publish release { tag_name } on GitHub ?" , default = True ):
376
365
return
377
366
378
367
# Publish the draft release
@@ -406,6 +395,13 @@ def upload() -> None:
406
395
current_version = get_package_version ()
407
396
tag_name = f"v{ current_version } "
408
397
398
+ # Check we have the right tag checked out.
399
+ repo = get_repo_and_check_clean_checkout ()
400
+ tag = repo .tag (f"refs/tags/{ tag_name } " )
401
+ if repo .head .commit != tag .commit :
402
+ click .echo ("Tag {tag_name} (tag.commit) is not currently checked out!" )
403
+ click .get_current_context ().abort ()
404
+
409
405
pypi_asset_names = [
410
406
f"matrix_synapse-{ current_version } -py3-none-any.whl" ,
411
407
f"matrix-synapse-{ current_version } .tar.gz" ,
@@ -469,6 +465,21 @@ def get_package_version() -> version.Version:
469
465
return version .Version (version_string )
470
466
471
467
468
+ def get_release_branch_name (version_number : version .Version ) -> str :
469
+ return f"release-v{ version_number .major } .{ version_number .minor } "
470
+
471
+
472
+ def get_repo_and_check_clean_checkout () -> git .Repo :
473
+ """Get the project repo and check it's not got any uncommitted changes."""
474
+ try :
475
+ repo = git .Repo ()
476
+ except git .InvalidGitRepositoryError :
477
+ raise click .ClickException ("Not in Synapse repo." )
478
+ if repo .is_dirty ():
479
+ raise click .ClickException ("Uncommitted changes exist." )
480
+ return repo
481
+
482
+
472
483
def find_ref (repo : git .Repo , ref_name : str ) -> Optional [git .HEAD ]:
473
484
"""Find the branch/ref, looking first locally then in the remote."""
474
485
if ref_name in repo .references :
0 commit comments