Skip to content

Instantly share code, notes, and snippets.

@mfisher87
Last active May 6, 2024 18:11
Show Gist options
  • Save mfisher87/094ddc4619ba0f4c428ea66236212ce6 to your computer and use it in GitHub Desktop.
Save mfisher87/094ddc4619ba0f4c428ea66236212ce6 to your computer and use it in GitHub Desktop.
A `post-rewrite` Git hook for moving tags from the old to new commit
#!/usr/bin/env python
"""Move tags from old commit to new commit on every re-written commit.
WARNING: This is generally not a good idea.
Why would you want to do this? I hope you have a _very_ good reason! For example,
applying metadata changes to every release on a pre-commit mirror.
"""
import subprocess
import sys
def git(*args: list[str]) -> str:
"""Run a git command with `args`."""
args = ['git', *args]
subp = subprocess.run(args, check=True, capture_output=True)
return subp.stdout.decode('utf-8').strip()
def retag(old_commit: str, new_commit: str) -> None:
"""For each old/new commit pair, move tags from old commit to new."""
tags = git('tag', '--points-at', old_commit).split('\n')
if tags == ['']:
tags = []
for tag in tags:
git('tag', '--force', tag, new_commit)
def main(lines) -> None:
rewrites: list[tuple[str, str]] = []
for line in lines:
old, new = line.strip().split(' ')
rewrites.append((old, new))
for old, new in rewrites:
retag(old, new)
if __name__ == "__main__":
main(sys.stdin)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment