What's cooking (2024/04 #09)
[alt-git.git] / RP
blobd837c1d57b6df924fa708a410326c03095bb791a
1 #!/bin/sh
2 # Replace patch series
4 usage="$0 branch [base-branch] <patchfile"
6 if test -d .dotest
7 then
8 echo >&2 "still in the middle of rebase/am"
9 exit 1
12 case "$#,$1" in
13 1,--continue)
16 rm -f .rp-state
17 target_branch="$1"
18 case $# in
20 base_branch=master
23 base_branch=$(git rev-parse --verify "$2") || exit
26 echo >&2 "$usage"
27 exit 1
29 esac
31 # find branch point
32 branch_point=$(git merge-base "$base_branch" "$target_branch") || {
33 echo >&2 "failed to compute the branch point"
34 exit 1
37 # safety -- never rewind/replace what's merged to next
38 in_branch=$(git rev-list $branch_point..$target_branch) &&
39 not_in_next=$(git rev-list $branch_point..$target_branch ^next) &&
40 test "z$in_branch" = "z$not_in_next" || {
41 echo >&2 "should not be rewinding part of $target_branch that is already in next"
42 exit 1
45 # detach the HEAD
46 git checkout "$branch_point" || {
47 echo >&2 "detaching the head at $branch_point"
48 exit 1
52 echo "target_branch=$target_branch"
53 echo "branch_point=$branch_point"
54 } >.rp-state
55 # apply patches
56 git am -3 -s -u || {
57 echo >&2 "finish the am and say $0 --continue"
58 exit 1
61 esac
63 branch_point=$(sed -ne 's/^branch_point=//p' .rp-state) &&
64 target_branch=$(sed -ne 's/^target_branch=//p' .rp-state) || {
65 echo >&2 "no replace-patch session"
66 exit 1
68 rm -f .rp-state
70 git branch -f "$target_branch" HEAD
71 git checkout "$target_branch"
72 git -p show-branch "$target_branch@{1}" "$target_branch"
73 git diff --stat -p "$target_branch@{1}" "$target_branch"