| 1 | #!/bin/sh |
| 2 | # Replace patch series |
| 3 | |
| 4 | usage="$0 branch [base-branch] <patchfile" |
| 5 | |
| 6 | if test -d .dotest |
| 7 | then |
| 8 | echo >&2 "still in the middle of rebase/am" |
| 9 | exit 1 |
| 10 | fi |
| 11 | |
| 12 | case "$#,$1" in |
| 13 | 1,--continue) |
| 14 | ;; |
| 15 | *) |
| 16 | rm -f .rp-state |
| 17 | target_branch="$1" |
| 18 | case $# in |
| 19 | 1) |
| 20 | base_branch=master |
| 21 | ;; |
| 22 | 2) |
| 23 | base_branch=$(git rev-parse --verify "$2") || exit |
| 24 | ;; |
| 25 | *) |
| 26 | echo >&2 "$usage" |
| 27 | exit 1 |
| 28 | ;; |
| 29 | esac |
| 30 | |
| 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 |
| 35 | } |
| 36 | |
| 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 |
| 43 | } |
| 44 | |
| 45 | # detach the HEAD |
| 46 | git checkout "$branch_point" || { |
| 47 | echo >&2 "detaching the head at $branch_point" |
| 48 | exit 1 |
| 49 | } |
| 50 | |
| 51 | { |
| 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 |
| 59 | } |
| 60 | ;; |
| 61 | esac |
| 62 | |
| 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 |
| 67 | } |
| 68 | rm -f .rp-state |
| 69 | |
| 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" |