eb3cede2b0cb308c20e90037190f0c4def192a78

Author
TheEdgeOfRage <git@theedgeofrage.com>
Committer
TheEdgeOfRage <git@theedgeofrage.com>
Date

Message

Add stack rebase script

Diff

 1diff --git a/dot_local/bin/executable_gsrb b/dot_local/bin/executable_gsrb
 2new file mode 100644
 3index 0000000000000000000000000000000000000000..3c7792b63dbc63482d6a482a40d17e14dfe5f3d3
 4--- /dev/null
 5+++ b/dot_local/bin/executable_gsrb
 6@@ -0,0 +1,61 @@
 7+#!/bin/sh
 8+set -e
 9+
10+interactive=""
11+while getopts "i" opt; do
12+	case $opt in
13+		i) interactive="-i" ;;
14+		*) echo "usage: gsrb [-i]" >&2; exit 1 ;;
15+	esac
16+done
17+
18+main=main
19+git rev-parse --verify "$main" >/dev/null 2>&1 || main=master
20+
21+current=$(git branch --show-current)
22+merge_base=$(git merge-base HEAD "$main")
23+
24+# collect branches in stack order (closest to main first)
25+branches=""
26+for commit in $(git rev-list --reverse "$merge_base"..HEAD); do
27+	for b in $(git branch --points-at "$commit" --format='%(refname:short)'); do
28+		case "$b" in main|master) continue ;; esac
29+		branches="$branches $b"
30+	done
31+done
32+branches="${branches# }"
33+
34+if [ -z "$branches" ]; then
35+	echo "no branches found in stack"
36+	exit 0
37+fi
38+
39+echo "stack: $branches"
40+
41+# save original tips before any rebase mutates them
42+tmpfile=$(mktemp)
43+trap 'rm -f "$tmpfile"' EXIT
44+for b in $branches; do
45+	echo "$b $(git rev-parse "$b")" >> "$tmpfile"
46+done
47+
48+# rebase each branch onto the previous, maintaining the stack
49+prev="$main"
50+prev_old="$merge_base"
51+for b in $branches; do
52+	old=$(grep "^$b " "$tmpfile" | cut -d' ' -f2)
53+	echo "rebasing $b onto $prev"
54+	git rebase $interactive --onto "$prev" "$prev_old" "$b"
55+	gitdir=$(git rev-parse --git-dir)
56+	if [ -d "$gitdir/rebase-merge" ] || [ -d "$gitdir/rebase-apply" ]; then
57+		echo "rebase paused — complete it, then re-run gsrb" >&2
58+		exit 1
59+	fi
60+	prev_old="$old"
61+	prev="$b"
62+done
63+
64+# restore original branch
65+if [ -n "$current" ] && [ "$current" != "$b" ]; then
66+	git checkout "$current"
67+fi