Last active
April 13, 2026 21:29
-
-
Save szhu/53389d3ab6e1daab3b1a0d635c510fd7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import re | |
| import subprocess | |
| import sys | |
| def run(args, echo=True): | |
| if echo: | |
| print(f"$ {' '.join(args)}") | |
| r = subprocess.run(args, capture_output=True, text=True) | |
| if r.returncode: | |
| sys.exit(f"Error: {r.stderr}") | |
| return re.sub(r"\n$", "", r.stdout) | |
| # Get current operation ID before making changes | |
| current_op = run(["jj", "op", "log", "-n", "1", "--no-graph", "-T", "id"], echo=False) | |
| # 1. Get mutable commits in chain, excluding @ and merges | |
| output = run( | |
| [ | |
| "jj", | |
| "log", | |
| "--no-graph", | |
| "-r", | |
| "::@ & ~immutable() & ~@ & ~::merges()", | |
| "-T", | |
| "change_id ++ ' ' ++ description.first_line() ++ '\n'", | |
| ], | |
| echo=False, | |
| ) | |
| already_sorted = True | |
| wips, non_wips = [], [] | |
| for line in output.splitlines()[::-1]: | |
| change, description = line.split(" ", 1) | |
| if re.match(r"^WIP\b", description): | |
| wips.append(change) | |
| else: | |
| non_wips.append(change) | |
| if wips: | |
| already_sorted = False | |
| if already_sorted: | |
| print("All WIP commits are already at the top. Nothing to do.") | |
| sys.exit(0) | |
| # 2. Rebase WIPs to the top of Non-WIPs | |
| top = non_wips[-1] | |
| for wip in wips: | |
| run(["jj", "--quiet", "rebase", "-r", wip[:8], "--after", top[:8]]) | |
| top = wip | |
| print(f"\nTo undo these changes, run:\n$ jj op restore {current_op[:12]}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment