3 # Rebase the thicket of branches -- including their merge structure -- on top
4 # of the specified upstream branch (defaults to 'junio/next'), optionally
5 # retaining "fast-forwardability" by fake-merging (using the "ours" strategy)
6 # the previous state on top of the current upstream state ("merging rebase").
10 # start the rebased branch by a fake merge of the previous state
12 # The idea is to generate our very own rebase script, then call rebase -i with
13 # our fake editor to put the rebase script into place and then let the user edit
16 # To make things prettier, we rewrite the rebase script after letting the user
17 # edit it, to replace "new" rebase commands with calls to a temporary alias ".r"
18 # that is added to help with starting the merging rebase, merging things, and
19 # cleaning up afterwards.
26 git_dir
="$(git rev-parse --git-dir)" ||
27 die
"Not in a Git directory"
31 Usage: $0 [options] <upstream>
34 -m|--merging[=<msg>] allow fast-forwarding the current to the rebased branch
35 --onto=<commit> rebase onto the given commit
36 --recreate=<merge> recreate the branch merged in the specified commit
41 # Extra commands for use in the rebase script
42 extra_commands
="edit bud finish mark rewind merge start_merging_rebase cleanup"
46 GIT_SEQUENCE_EDITOR
="$GIT_EDITOR" &&
47 export GIT_EDITOR GIT_SEQUENCE_EDITOR
&&
51 sed -e '/^noop/d' < "$1" >> "$git_dir"/SHEARS-SCRIPT
&&
52 mv "$git_dir"/SHEARS-SCRIPT
"$1"
54 mv "$1" "$git_dir"/SHEARS-SCRIPT
&&
55 exprs
="$(for command in $extra_commands
57 printf " -e 's/^$command\$/exec git .r &/'"
58 printf " -e 's/^$command /exec git .r &/'"
60 eval sed $exprs < "$git_dir"/SHEARS-SCRIPT
> "$1"
63 exec "$GIT_EDITOR" "$@"
68 git update-ref
-m "Marking '$1' as rewritten" refs
/rewritten
/"$1" HEAD
72 git
reset --hard refs
/rewritten
/"$1"
76 shorthead
="$(git rev-parse --short --verify HEAD)" &&
77 git for-each-ref refs
/rewritten
/ |
79 die
"Refusing to leave unmarked revision $shorthead behind"
80 git
reset --hard refs
/rewritten
/onto
89 # parse command-line arguments
91 while test $# -gt 0 && test "a$1" != a-C
93 parents
="$parents $1" &&
101 # determine whether the merge needs to be redone
102 p
="$(git rev-parse HEAD)$parents" &&
103 o
="$(git rev-list -1 --parents $orig |
105 while p
=${p# }; o
=${o# }; test -n "$p$o"
107 p1
=${p%% *}; o1
=${o%% *};
108 test $o1 = "$(git rev-parse "$p1")" ||
break
109 p
=${p#$p1}; o
=${o#$o1}
111 # either redo merge or fast-forward
114 git
reset --hard $orig
117 msg
="$(git cat-file commit $orig |
122 for parent
in $parents
127 p
="$p $(git rev-parse --verify refs/rewritten/$parent \
131 msg
="Merge $msg into HEAD"
133 git merge
-n --no-ff -m "$msg" $p
136 start_merging_rebase
() {
137 git merge
-s ours
-m "$(cat "$git_dir"/SHEARS-MERGING-MESSAGE)" "$1"
141 rm -f "$git_dir"/SHEARS-SCRIPT
&&
144 git update-ref
-d refs
/rewritten
/$rewritten
146 for rewritten
in $
(git for-each-ref refs
/rewritten
/ |
147 sed 's/^[^ ]* commit.refs\/rewritten\///')
149 test onto
= "$rewritten" ||
151 git update-ref
-d refs
/rewritten
/$rewritten
153 git config
--unset alias..r
170 base_message
="${1#--merging=}"
181 recreate
="$recreate $1"
184 recreate
="$recreate ${1#--recreate=}"
193 echo "Unknown option: $1" >&2
203 case " $extra_commands " in
214 sed 's/[][\\\/*?]/\\&/g'
217 merge2branch_name
() {
218 git show
-s --format=%s
"$1" |
219 sed -n -e "s/^Merge [^']*'\([^']*\).*/\1/p" \
220 -e "s/^Merge pull request #[0-9]* from //p" |
226 name
="$(echo "$commit_name_map" |
234 case " $needslabel " in
238 needslabel
="$needslabel $n"
245 origtodo
="$(git rev-list --no-merges --cherry-pick --pretty=oneline \
246 --abbrev-commit --abbrev=7 --reverse --left-right --topo-order \
248 sed -n "s
/^
>/pick
/p
")"
249 shorthead
=$
(git rev-parse
--short $head)
250 shortonto
=$
(git rev-parse
--short $onto)
252 # --topo-order has the bad habit of breaking first-parent chains over
253 # merges, so we generate the topoligical order ourselves here
255 list
="$(git log --format='%h %p' --topo-order --reverse \
259 if test -n "$merging"
261 from
=$
(git rev-parse
--short "$upstream") &&
262 to
=$
(git rev-parse
--short "$onto") &&
263 cat > "$git_dir"/SHEARS-MERGING-MESSAGE
<< EOF &&
264 Start the merging-rebase to $onto
266 This commit starts the rebase of $from to $to
269 todo
="start_merging_rebase \"$shorthead\""
271 todo
="$(printf '%s\n%s\n' "$todo" \
274 toberebased
=" $(echo "$list" | cut -f 1 -d ' ' | tr '\n' ' ')"
278 # each tip is an end point of a commit->first parent chain
279 branch_tips
="$(echo "$list" |
284 ensure_labeled
$branch_tips
286 branch_tips
="$(printf '%s\n%s' "$branch_tips" "$shorthead")"
288 # set up the map tip -> branch name
289 for tip
in $branch_tips
291 merged_by
="$(echo "$list" |
292 sed -n "s
/^\
([^
]*\
) [^
]* $tip$
/\
1/p
" |
294 if test -n "$merged_by"
296 branch_name
="$(merge2branch_name "$merged_by")"
297 test -z "$branch_name" ||
298 commit_name_map
="$(printf '%s\n%s' \
299 "$tip $branch_name" "$commit_name_map")"
302 branch_name_dupes
="$(echo "$commit_name_map" |
306 if test -n "$branch_name_dupes"
308 exprs
="$(echo "$branch_name_dupes" |
309 while read branch_name
312 "$
(string2regex
"$branch_name")"
314 commit_name_map
="$(echo "$commit_name_map" |
315 eval grep -v $exprs)"
318 for tip
in $branch_tips
320 # if this is not a commit to be rebased, skip
321 case "$toberebased" in *" $tip "*) ;; *) continue;; esac
323 # if it is handled already, skip
324 case "$handled " in *" $tip "*) continue;; esac
326 # start sub-todo for this tip
331 # if already handled, this is our branch point
334 ensure_labeled
$commit
335 subtodo
="$(printf '\nrewind %s # %s\n%s' \
336 "$
(name_commit
$commit)" \
337 "$
(git show
-s --format=%s
$commit)" \
343 line
="$(echo "$list" | grep "^
$commit ")"
344 # if there is no line, branch from the 'onto' commit
347 subtodo
="$(printf '\nbud\n%s' \
355 parents2
="`for parent in ${parents#* }
357 case "$toberebased" in
359 printf refs/rewritten/
362 echo "$
(name_commit
$parent) "
364 subtodo
="$(printf '%s # %s\n%s' \
365 "merge
$parents2-C $commit" \
366 "$
(git show
-s --format=%s
$commit)" \
371 line
="$(echo "$origtodo" |
372 grep "^pick
$commit")"
375 line
="# skip $commit"
377 subtodo
="$(printf '%s\n%s' "$line" "$subtodo")"
380 handled
="$handled $commit"
381 commit
=${parents%% *}
384 branch_name
="$(name_commit "$tip")"
385 test -n "$branch_name" &&
386 test "$branch_name" = "$tip" ||
387 subtodo
="$(echo "$subtodo" |
389 # Branch: $branch_name")"
391 todo
="$(printf '%s\n\n%s' "$todo" "$subtodo")"
394 for commit
in $needslabel
396 linenumber
="$(echo "$todo" |
397 grep -n -e "^\
(pick\|
# skip\) $commit" \
398 -e "^merge [0-9a-f/ ]* -C $commit")"
399 linenumber=${linenumber%%:*}
400 test -n "$linenumber" ||
401 die "Internal error
: could not
find $commit in $todo"
402 todo="$
(echo "$todo" |
403 sed "${linenumber}a\\
404 mark $(name_commit $commit)\\
411 fixup="$
(echo "$todo" |
412 sed "$lastline,\$d" |
413 grep -n -e '^pick [^ ]* \(fixup\|squash\)!' |
415 test -n "$fixup" || break
417 linenumber=${fixup%%:*}
418 oneline="${fixup#* }"
419 shortsha1="${oneline%% *}"
420 oneline="${oneline#* }"
421 command=${oneline%%!*}
422 oneline="${oneline#*! }"
423 oneline_regex="^pick
[^
]* $
(string2regex
"$oneline")\$
"
424 targetline="$
(echo "$todo" |
425 sed "$linenumber,\$d" |
426 grep -n "$oneline_regex" |
428 targetline=${targetline%%:*}
429 if test -n "$targetline"
431 todo="$
(echo "$todo" |
432 sed -e "${linenumber}d" \
434 $command $shortsha1 $oneline")"
435 lastline=$(($linenumber+1))
437 echo "UNHANDLED
: $oneline" >&2
438 lastline=$(($linenumber))
442 while test -n "$recreate"
444 recreate="${recreate# }"
445 merge="${recreate%% *}"
446 recreate="${recreate#$merge}"
448 mark="$
(git rev-parse
--short --verify "$merge^2")" ||
449 die "Could not
find merge commit
: $merge^
2"
451 branch_name="$
(merge2branch_name
"$merge")"
452 partfile="$git_dir/SHEARS-PART
"
453 printf '%s' "$
(test -z "$branch_name" ||
454 echo "# Branch to recreate: $branch_name")" \
456 for sha1 in $(git rev-list --reverse $merge^..$merge^2)
458 msg="$
(git show
-s --format=%s
$sha1)"
459 msg_regex="^pick
[^
]* $
(string2regex
"$msg")\$
"
460 linenumber="$
(echo "$todo" |
461 grep -n "$msg_regex" |
463 test -n "$linenumber" ||
464 die "Not a commit to rebase
: $msg"
465 test 1 = $(echo "$linenumber" | wc -l) ||
466 die "More than one match
for: $msg"
468 sed -n "${linenumber}p
" >> "$partfile"
469 todo="$
(echo "$todo" |
470 sed "${linenumber}d")"
473 linenumber="$
(echo "$todo" |
478 printf 'mark %s\n\nbud\n' \
479 "$
(name_commit
$mark)" >> "$partfile"
480 todo="$
(echo "$todo" |
481 sed -e "${linenumber}r$partfile" \
483 merge refs/rewritten/$mark -C $(name_commit $merge)")"
486 needslabel="$
(for commit
in $needslabel
488 printf ' %s' $
(name_commit
$commit)
490 todo="$
(printf '%s\n\n%s' "$todo" "cleanup $needslabel")"
494 this="$
(cd "$(dirname "$0")" && pwd)/$
(basename "$0")"
496 existing=$(git for-each-ref --format='%(refname)' refs/rewritten/)
497 test -z "$existing" ||
502 git update-ref -d $ref
505 die "$
(printf '%s %s:\n%s\n' \
506 'There are still rewritten revisions' \
507 '(use --force to delete)' \
511 alias="$
(git config
--get alias..r
)"
513 test "a
$alias" = "a
!sh
\"$this\"" ||
515 die "There is already an
'.r' alias!"
517 git config alias..r "!sh
\"$this\"" &&
518 generate_script > "$git_dir"/SHEARS-SCRIPT &&
519 GIT_EDITOR="$
(cd "$git_dir" && pwd)/SHEARS-EDITOR
" &&
520 cat > "$GIT_EDITOR" << EOF &&
523 exec "$this" edit "$
(git var GIT_EDITOR
)" "\$@
"
525 chmod +x "$GIT_EDITOR" &&
526 GIT_EDITOR="\"$GIT_EDITOR\"" &&
527 GIT_SEQUENCE_EDITOR="$GIT_EDITOR" &&
528 export GIT_EDITOR GIT_SEQUENCE_EDITOR
531 test ! -d "$git_dir"/rebase-merge &&
532 test ! -d "$git_dir"/rebase-apply ||
533 die "Rebase already
in progress
"
538 head="$
(git rev-parse HEAD
)" &&
540 onto=${onto:-$upstream}||
541 die "Could not determine rebase parameters
"
543 git update-index -q --ignore-submodules --refresh &&
544 git diff-files --quiet --ignore-submodules &&
545 git diff-index --cached --quiet --ignore-submodules HEAD -- ||
546 die 'There are uncommitted changes!'
551 git rebase -i --onto "$onto" HEAD