diff --git a/release.py b/release.py index 920887c..3c8659f 100644 --- a/release.py +++ b/release.py @@ -36,14 +36,39 @@ def update_changelog(version): f.write(f'\n## [{version}] - {datetime.now().strftime("%Y-%m-%d")}\n') -def update_git(new_version): +def delete_tag(tag): + # Delete the local and remote tag if it exists + print(f"Deleting existing tag {tag} locally and remotely...") + subprocess.run(["git", "tag", "-d", tag], check=True) + subprocess.run(["git", "push", "origin", f":refs/tags/{tag}"], check=True) + + +def tag_exists_on_remote(tag): + result = subprocess.run( + ["git", "ls-remote", "--tags", "origin", tag], capture_output=True, text=True + ) + return bool(result.stdout.strip()) + + +def update_git(new_version: str, delete_existing_tag: bool = False): + # Fetch tags to ensure the local repo is up to date + subprocess.run(["git", "fetch", "--tags"], check=True) + tag_name = f"v{new_version}" + + if tag_exists_on_remote(tag_name) and delete_existing_tag: + delete_tag(tag_name) + else: + print(f"Tag {tag_name} already exists on the remote. Aborting!") + sys.exit(1) + subprocess.run( ["git", "commit", "-am", f"Bump version to {new_version}"], check=True ) + subprocess.run( - ["git", "tag", f"v{new_version}", "-m", f"Release v{new_version}"], check=True + ["git", "tag", tag_name, "-m", f"Release v{new_version}"], check=True ) - subprocess.run(["git", "push", "origin", f"v{new_version}"], check=True) + subprocess.run(["git", "push", "origin", tag_name], check=True) def main():