diff --git a/prank.py b/prank.py index 0a6cea5..99dfce9 100755 --- a/prank.py +++ b/prank.py @@ -13,6 +13,7 @@ WARNING: This rewrites history. Force-push required afterwards. import subprocess import argparse +import shlex import sys @@ -26,6 +27,17 @@ def run(cmd, capture=True, check=True): def get_all_commits(): output = run("git log --format='%H' --all") return output.splitlines() if output else [] + + +def get_original_refs(): + output = run("git for-each-ref --format='%(refname)' refs/original", check=False) + return output.splitlines() if output else [] + + +def shell_quote(value): + return shlex.quote(value) + + def main(): parser = argparse.ArgumentParser( description="Rewrite history so all commits use the provided author." @@ -37,6 +49,11 @@ def main(): action="store_true", help="Show what would change without modifying anything", ) + parser.add_argument( + "--keep-original-refs", + action="store_true", + help="Keep git filter-branch backup refs instead of deleting them after rewrite", + ) args = parser.parse_args() # Make sure we're in a git repo @@ -69,8 +86,10 @@ def main(): # Build a filter-branch env filter script env_filter = f""" -export GIT_AUTHOR_NAME='{args.name}' -export GIT_AUTHOR_EMAIL='{args.email}' +export GIT_AUTHOR_NAME={shell_quote(args.name)} +export GIT_AUTHOR_EMAIL={shell_quote(args.email)} +export GIT_COMMITTER_NAME={shell_quote(args.name)} +export GIT_COMMITTER_EMAIL={shell_quote(args.email)} """ print("\nRewriting history with git filter-branch...") @@ -92,6 +111,22 @@ export GIT_AUTHOR_EMAIL='{args.email}' print(f"Error: {e}", file=sys.stderr) sys.exit(1) + if args.keep_original_refs: + print("\nKept git filter-branch backup refs under refs/original.") + else: + original_refs = get_original_refs() + if original_refs: + print("\nRemoving git filter-branch backup refs...") + for ref in original_refs: + subprocess.run(["git", "update-ref", "-d", ref], check=True) + + print("Expiring reflogs and pruning unreachable objects...") + subprocess.run( + ["git", "reflog", "expire", "--expire=now", "--all"], + check=True, + ) + subprocess.run(["git", "gc", "--prune=now"], check=True) + print("\n✅ Done! History rewritten.") print("\nTo publish the changes, force-push all branches:") print(" git push --force --all")