3 # Copyright (c) 2006 Johannes E. Schindelin
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 m,merge always used (no-op)
24 i,interactive always used (no-op)
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
29 no-verify override pre-rebase hook from stopping the operation
30 root rebase all reachable commmits up to the root(s)
31 autosquash move commits that begin with squash!/fixup! under -i
37 DOTEST
="$GIT_DIR/rebase-merge"
39 # The file containing rebase commands, comments, and empty lines.
40 # This file is created by "git rebase -i" then edited by the user. As
41 # the lines are processed, they are removed from the front of this
42 # file and written to the tail of $DONE.
43 TODO
="$DOTEST"/git-rebase-todo
45 # The rebase command lines that have already been processed. A line
46 # is moved here when it is first handled, before any associated user
50 # The commit message that is planned to be used for any changes that
51 # need to be committed following a user interaction.
54 # The file into which is accumulated the suggested commit message for
55 # squash/fixup commands. When the first of a series of squash/fixups
56 # is seen, the file is created and the commit message from the
57 # previous commit and from the first squash/fixup commit are written
58 # to it. The commit message for each subsequent squash/fixup commit
59 # is appended to the file as it is processed.
61 # The first line of the file is of the form
62 # # This is a combination of $COUNT commits.
63 # where $COUNT is the number of commits whose messages have been
64 # written to the file so far (including the initial "pick" commit).
65 # Each time that a commit message is processed, this line is read and
66 # updated. It is deleted just before the combined commit is made.
67 SQUASH_MSG
="$DOTEST"/message-squash
69 # If the current series of squash/fixups has not yet included a squash
70 # command, then this file exists and holds the commit message of the
71 # original "pick" commit. (If the series ends without a "squash"
72 # command, then this can be used as the commit message of the combined
73 # commit without opening the editor.)
74 FIXUP_MSG
="$DOTEST"/message-fixup
76 # $REWRITTEN is the name of a directory containing files for each
77 # commit that is reachable by at least one merge base of $HEAD and
78 # $UPSTREAM. They are not necessarily rewritten, but their children
79 # might be. This ensures that commits on merged, but otherwise
80 # unrelated side branches are left alone. (Think "X" in the man page's
82 REWRITTEN
="$DOTEST"/rewritten
84 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
85 # GIT_AUTHOR_DATE that will be used for the commit that is currently
87 AUTHOR_SCRIPT
="$DOTEST"/author-script
89 # When an "edit" rebase command is being processed, the SHA1 of the
90 # commit to be edited is recorded in this file. When "git rebase
91 # --continue" is executed, if there are any staged changes then they
92 # will be amended to the HEAD commit, but only provided the HEAD
93 # commit is still the commit to be edited. When any other rebase
94 # command is processed, this file is deleted.
101 OK_TO_SKIP_PRE_REBASE
=
105 GIT_CHERRY_PICK_HELP
=" After resolving the conflicts,
106 mark the corrected paths with 'git add <paths>', and
107 run 'git rebase --continue'"
108 export GIT_CHERRY_PICK_HELP
119 test $status != 0 && printf "%s\n" "$output"
128 # Output the commit message for the specified commit.
130 git cat-file commit
"$1" |
sed "1,/^$/d"
133 run_pre_rebase_hook
() {
134 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
135 test -x "$GIT_DIR/hooks/pre-rebase"
137 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
{
138 echo >&2 "The pre-rebase hook refused to rebase."
144 ORIG_REFLOG_ACTION
="$GIT_REFLOG_ACTION"
146 comment_for_reflog
() {
147 case "$ORIG_REFLOG_ACTION" in
149 GIT_REFLOG_ACTION
="rebase -i ($1)"
150 export GIT_REFLOG_ACTION
155 peek_next_command
() {
156 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
159 # expects the original commit name(s) in "$REWRITTEN"/original
160 # records the current HEAD as the rewritten commit
162 test ! -d "$REWRITTEN" && return
163 rewritten
=$
(git rev-parse
--verify HEAD
) &&
164 for original
in $
(cat "$REWRITTEN"/original
)
166 original
=$
(git-rev-parse
"$original") &&
167 echo $rewritten > "$REWRITTEN"/$original ||
break
169 case "$(peek_next_command)" in
170 squash|s
) ;; # do nothing
171 *) rm "$REWRITTEN"/original
;;
173 die
"Could not store information about rewritten commit"
176 # if the given commit name ends in an apostrophe, returns the rewritten commit
178 if test -f "$REWRITTEN/${1%\'}"
180 rewritten
="$(cat "$REWRITTEN/${1%\'}")"
182 rewritten
=$
(git rev-parse
--verify "${1%\'}") ||
183 die
"Cannot turn $1 into a commit name"
185 while test -f "$REWRITTEN"/$rewritten
187 new_rewritten
=$
(cat "$REWRITTEN"/$rewritten) ||
188 die
"Could not read rewritten commit for $1"
189 test $rewritten = $new_rewritten && break
190 rewritten
=$new_rewritten
196 git show
-s --pretty="format:%h %s" $1
200 mark_action_done
() {
201 sed -e 1q
< "$TODO" >> "$DONE"
202 sed -e 1d
< "$TODO" >> "$TODO".new
203 mv -f "$TODO".new
"$TODO"
204 count
=$
(sane_grep
-c '^[^#]' < "$DONE")
205 total
=$
(($count+$
(sane_grep
-c '^[^#]' < "$TODO")))
206 if test "$last_count" != "$count"
209 printf "Rebasing (%d/%d)\r" $count $total
210 test -z "$VERBOSE" ||
echo
215 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
216 case "$sha1_and_parents" in
218 git
diff --cc $sha1_and_parents
221 git diff-tree
-p "$1^!"
226 esac > "$DOTEST"/patch
228 commit_message
"$1" > "$MSG"
229 test -f "$AUTHOR_SCRIPT" ||
230 get_author_ident_from_commit
"$1" > "$AUTHOR_SCRIPT"
234 test -d "$REWRITTEN" && git rev-parse
"$1" >> "$REWRITTEN"/original
246 sane_grep
'^[^#]' "$1" >/dev
/null
249 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
250 # GIT_AUTHOR_DATE exported from the current environment.
253 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
260 case "$1" in -n) sha1
=$2; no_ff
=t
;; *) sha1
=$1 ;; esac
261 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
262 test -d "$REWRITTEN" && git rev-parse
$sha1 >> "$REWRITTEN"/original
263 if test -n "$REBASE_ROOT"
265 output git cherry-pick
"$@"
268 parent_sha1
=$
(git rev-parse
--verify $sha1^
) ||
269 die
"Could not get the parent of $sha1"
270 current_sha1
=$
(git rev-parse
--verify HEAD
)
271 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
273 output git
reset --hard $sha1
274 output warn Fast-forward to $
(git rev-parse
--short $sha1)
276 output git cherry-pick
"$@"
282 test "$1" = parents
&& shift
284 merge_message
="Merge"
285 while test $# -gt 0 -a "original" != "$1" -a "as" != "$1"
287 parents
="$parents $(parse_commit $1)" ||
288 die
"Could not interpret '$1' as a commit"
289 merge_message
="$merge_message $1"
297 sha1
=$
(git rev-parse
--verify "$2") ||
298 die
"Invalid original merge commit: $2"
301 merge_message
="$(git cat-file commit $sha1 |
312 # the command was "merge parents ...", so "parents" was recorded
313 # TODO: detect non-fast-forwards properly
314 ORIGINAL_HEAD
=$
(git rev-parse HEAD
) &&
315 git merge
$parents &&
316 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
318 git commit
--amend -m "$merge_message" &&
321 echo "$sha1" > "$REWRITTEN"/original
&&
325 die_with_patch
$sha1 "Could not redo merge $sha1 with parents $parents"
328 handle_topic_branch
() {
329 test -z "$2" && die
"Empty 'topic' command"
335 git rev-parse
--verify HEAD \
336 > "$REWRITTEN"/merge-parent-of-
"$topic" &&
337 git
reset --hard $
(parse_commit \
338 $
(test at = "$1" && echo "$2" ||
echo onto
))
341 git rev-parse
--verify HEAD
> "$REWRITTEN"/"$topic" &&
342 git
reset --hard $
(parse_commit merge-parent-of-
"$topic") &&
343 merge_one parents
"$topic" original
"" "$*"
346 die
"Unknown 'topic' command: $command $topic $*"
353 *1[0-9]|
*[04-9]) echo "$1"th
;;
360 update_squash_messages
() {
361 if test -f "$SQUASH_MSG"; then
362 mv "$SQUASH_MSG" "$SQUASH_MSG".bak ||
exit
364 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
365 -e "q" < "$SQUASH_MSG".bak
)+1))
367 echo "# This is a combination of $COUNT commits."
368 sed -e 1d
-e '2,/^./{
370 }' <"$SQUASH_MSG".bak
373 commit_message HEAD
> "$FIXUP_MSG" || die
"Cannot write $FIXUP_MSG"
376 echo "# This is a combination of 2 commits."
377 echo "# The first commit's message is:"
386 echo "# This is the $(nth_string $COUNT) commit message:"
392 echo "# The $(nth_string $COUNT) commit message will be skipped:"
394 commit_message
$2 |
sed -e 's/^/# /'
399 # A squash/fixup has failed. Prepare the long version of the squash
400 # commit message, then die_with_patch. This code path requires the
401 # user to edit the combined commit message for all commits that have
402 # been squashed/fixedup so far. So also erase the old squash
403 # messages, effectively causing the combined commit to be used as the
404 # new basis for any further squash/fixups. Args: sha1 rest
405 die_failed_squash
() {
406 mv "$SQUASH_MSG" "$MSG" ||
exit
408 cp "$MSG" "$GIT_DIR"/MERGE_MSG ||
exit
410 warn
"Could not apply $1... $2"
415 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" ||
exit
416 read command sha1 rest
< "$TODO"
422 comment_for_reflog pick
425 # TODO: give better description/output in the "Nothing to be committed" and
427 pick_one
$sha1 && add_rewritten ||
428 die_with_patch
$sha1 "Could not apply $sha1... $rest"
431 comment_for_reflog reword
435 die_with_patch
$sha1 "Could not apply $sha1... $rest"
439 comment_for_reflog edit
443 die_with_patch
$sha1 "Could not apply $sha1... $rest"
445 git rev-parse
--verify HEAD
> "$AMEND"
446 warn
"Stopped at $sha1... $rest"
447 warn
"You can amend the commit now, with"
449 warn
" git commit --amend"
451 warn
"Once you are satisfied with your changes, run"
453 warn
" git rebase --continue"
466 comment_for_reflog
$squash_style
468 test -f "$DONE" && has_action
"$DONE" ||
469 die
"Cannot '$squash_style' without a previous commit"
472 update_squash_messages
$squash_style $sha1
473 author_script
=$
(get_author_ident_from_commit HEAD
)
474 echo "$author_script" > "$AUTHOR_SCRIPT"
475 eval "$author_script"
476 output git
reset --soft HEAD^
477 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
478 case "$(peek_next_command)" in
480 # This is an intermediate commit; its message will only be
481 # used in case of trouble. So use the long version:
482 do_with_author output git commit
--no-verify -F "$SQUASH_MSG" &&
484 die_failed_squash
$sha1 "$rest"
487 # This is the final command of this squash/fixup group
488 if test -f "$FIXUP_MSG"
490 do_with_author git commit
--no-verify -F "$FIXUP_MSG" &&
492 die_failed_squash
$sha1 "$rest"
494 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG ||
exit
495 rm -f "$GIT_DIR"/MERGE_MSG
496 do_with_author git commit
--no-verify -e &&
498 die_failed_squash
$sha1 "$rest"
500 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
506 git rev-parse
--verify HEAD
> "$REWRITTEN"/"$sha1"
509 comment_for_reflog goto
511 output git
reset --hard $
(parse_commit
$sha1) ||
512 die
"Could not reset to $sha1"
515 comment_for_reflog merge
517 # this already dies with patch on error
518 output merge_one
$sha1 $rest ||
# $sha1 is not the real sha1...
522 comment_for_reflog
"topic($sha1)"
524 output handle_topic_branch
"$sha1" $rest ||
528 warn
"Unknown command: $command $sha1 $rest"
529 if git rev-parse
--verify -q "$sha1" >/dev
/null
531 die_with_patch
$sha1 "Please fix this in the file $TODO."
533 die
"Please fix this in the file $TODO."
537 test -s "$TODO" && return
539 if test -d "$REWRITTEN"
541 missed_tips
=$
(git rev-list ^HEAD \
542 $
(cat "$REWRITTEN"/* |
sed "s/$/^!/") \
544 sed -n "s/^\([0-9A-Fa-f]\{40\}\)$/^\1/p"))
545 test -z "$missed_tips" ||
{
546 echo "The following branch tips are missing from HEAD:"
548 for commit
in $missed_tips
550 echo "$(get_oneline $commit)"
556 comment_for_reflog finish
&&
557 HEADNAME
=$
(cat "$DOTEST"/head-name
) &&
558 OLDHEAD
=$
(cat "$DOTEST"/head) &&
559 SHORTONTO
=$
(git rev-parse
--short $
(cat "$DOTEST"/onto
)) &&
560 NEWHEAD
=$
(git rev-parse HEAD
) &&
563 message
="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
564 git update-ref
-m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
565 git symbolic-ref HEAD
$HEADNAME
568 test ! -f "$DOTEST"/verbose ||
569 git diff-tree
--stat $
(cat "$DOTEST"/head)..HEAD
573 warn
"Successfully rebased and updated $HEADNAME."
585 # skip picking commits whose parents are unchanged
586 skip_unnecessary_picks
() {
588 while read command sha1 rest
590 # fd=3 means we skip the command
591 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
592 3,pick
,"$ONTO"*|
3,p
,"$ONTO"*)
593 # pick a commit whose parent is current $ONTO -> skip
603 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
604 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
605 mv -f "$TODO".new
"$TODO" ||
606 die
"Could not skip unnecessary pick commands"
609 # check if no other options are set
611 test $# -eq 2 -a "$2" = '--' &&
613 test -z "$PRESERVE_MERGES" &&
614 test -z "$STRATEGY" &&
618 get_saved_options
() {
619 test -d "$REWRITTEN" && PRESERVE_MERGES
=t
620 test -f "$DOTEST"/strategy
&& STRATEGY
="$(cat "$DOTEST"/strategy)"
621 test -f "$DOTEST"/verbose
&& VERBOSE
=t
622 test -f "$DOTEST"/rebase-root
&& REBASE_ROOT
=t
625 # Rearrange the todo list that has both "pick sha1 msg" and
626 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
627 # comes immediately after the former, and change "pick" to
629 rearrange_squash
() {
630 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
631 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
633 test -s "$1.sq" ||
return
636 while read pick sha1 message
639 *" $sha1 "*) continue ;;
641 echo "$pick $sha1 $message"
642 while read squash action msg
646 echo "$action $squash $action! $msg"
651 done >"$1.rearranged" <"$1"
652 cat "$1.rearranged" >"$1"
653 rm -f "$1.sq" "$1.rearranged"
661 if left
=${1%...*} right
=${1#*...} &&
662 onto
=$
(git merge-base
--all ${left:-HEAD} ${right:-HEAD})
672 git rev-parse
--verify "$1^0"
675 prepare_preserve_merges
() {
676 # $REWRITTEN/ contains a mapping for each rewritten commit:
677 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
678 mkdir
"$REWRITTEN" || die
"Could not create directory $REWRITTEN"
679 if test -z "$REBASE_ROOT"
681 for c
in $
(git merge-base
--all $HEAD $UPSTREAM)
683 echo $ONTO > "$REWRITTEN"/$c ||
684 die
"Could not init rewritten commits"
687 echo $ONTO > "$REWRITTEN"/root
689 echo $ONTO > "$REWRITTEN"/onto
690 echo $UPSTREAM > "$REWRITTEN"/upstream
693 MERGES_OPTION
=--parents
696 handle_dropped_commits
() {
697 # Watch for commits that have been dropped by --cherry-pick
698 # The idea is that all commits that are already in upstream
699 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
700 # as if they were rewritten.
703 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
704 # So take only the first patch-id for each commit id (uniq -f1).
705 git log
-m -p $UPSTREAM..
$HEAD | git patch-id |
706 uniq -s 41 > "$REWRITTEN"/ours
707 git log
-m -p $HEAD..
$UPSTREAM | git patch-id |
708 uniq -s 41 > "$REWRITTEN"/upstream
710 # Now get the correspondences
711 cat "$REWRITTEN"/ours |
while read patch_id commit
713 # Is the same patch id in the upstream?
714 grep "^$patch_id " < "$REWRITTEN"/upstream
> /dev
/null ||
717 # Record the parent as "rewritten" commit. As we will resolve
718 # rewritten commits recursively, this will work even if the
719 # parent was rewritten, too.
721 # If there is no parent, then we have a root commit that
722 # was cherry-picked into upstream; let's use $ONTO as
723 # fake parent of that root commit.
724 upstream
=$
(git rev-parse
--verify "$commit^" 2> /dev
/null
)
725 test ! -z "$upstream" || upstream
=$ONTO
726 echo "$upstream" > "$REWRITTEN"/$commit
728 rm -f "$REWRITTEN"/ours
"$REWRITTEN"/upstream
731 generate_script_help
() {
732 cat >> "$TODO" << EOF
734 # Rebase $SHORTREVISIONS onto $SHORTONTO
737 # p, pick = use commit
738 # r, reword = use commit, but edit the commit message
739 # e, edit = use commit, but stop for amending
740 # s, squash = use commit, but meld into previous commit
741 # f, fixup = like "squash", but discard this commit's log message
742 # g, goto = reset the current state to the given commit
743 # m, merge parents <parents> original <original merge commit>
744 # = redo the given merge commit
745 # b, bookmark = when reaching this command, name the current revision
747 # If you remove a line here THAT COMMIT WILL BE LOST.
748 # However, if you remove everything, the rebase will be aborted.
755 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
756 $REVISIONS | grep '^>')" && {
762 current
=$SHORTUPSTREAM
763 test -z "$REBASE_ROOT" || current
=
764 git rev-list
$MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
765 --reverse --left-right --topo-order $REVISIONS |
766 sed -n "s/^>//p" |
while read shortsha1 firstparent rest
770 # generate "goto" statements
771 test -z "$PRESERVE_MERGES" ||
{
772 case "$firstparent" in
780 # TODO: a failed merge at the end should not let the rebase succeed
781 # TODO: avoid outputting anything when current is SHORTUPSTREAM and
782 # firstparent is a merge base
783 # TODO: use get_oneline again
784 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
791 echo "pick $(get_oneline $shortsha1)"
796 # TODO: test octopus merge
797 echo "merge parents $rest original $(get_oneline $shortsha1)"
800 # TODO: add information about "dropped" commits, just because we can
801 echo "# parent $(get_oneline $parent)"
809 echo "$BOOKMARKS" |
sed -n "s/^$sha1[^ ]* //p"
813 echo $1 |
grep '^[0-9A-Fa-f]\{40\}$' > /dev
/null
&& return
814 test -f "$REWRITTEN"/$1 && return
815 echo "$BOOKMARKS" |
grep "^[^ ]* $1$" > /dev
/null
819 # make sure that the commit-to-bookmark will be rewritten
820 grep "^\(pick\|merge .* original\) $1 " < "$TODO" > /dev
/null ||
return
821 test -z "$(commit2bookmark $1)" && {
822 bookmark
="$(echo "$2" | tr -c 'A-Za-z0-9_' '-' |
823 sed -e 's/^-*//' -e 's/--*/-/g' -e 's/-*$//')"
824 bookmark_exists
$bookmark && {
826 while bookmark_exists
$bookmark-$i
830 bookmark
=$bookmark-$i
832 BOOKMARKS
="$(echo "$BOOKMARKS"; echo "$1 $bookmark")"
838 test ! -d "$REWRITTEN" && return
839 BOOKMARKS
="$(git show -s --pretty=format:'%h onto' \
840 $(git merge-base --all "$HEAD" "$UPSTREAM") |
843 # Merges make a nicer nick name, so take them first
846 while read command sha1 rest
850 # reuse merge messages for merge-parents
851 if test -z "$message"
853 message
=$
(git show
-s --pretty=format
:%s
"$sha1")
855 message
="merge-parent-of-$message"
857 test onto
= "$sha1" && continue
858 name_bookmark
"$sha1" "$message"
862 rest
=${rest#parents }
863 sha1s
="$sha1 ${rest%% original *}"
864 message
=$
(echo "${rest#* original * }" |
865 sed -e 's/^Merge \(.\)/\1/' \
866 -e "s/.*'\(.*\)'.*/\1/")
869 name_bookmark
"$sha1" "$message"
876 done > "$DOTEST"/bookmarks
878 # Make a sed script to rewrite the rebase script
879 cat "$DOTEST/bookmarks" |
880 while read sha1 bookmark
883 's/^pick %s .*/&\\nbookmark %s/' \
884 's/^merge .* original %s .*/&\\nbookmark %s/' \
885 's/^goto %s .*/goto %s/' \
886 's/^\\(merge .* \\)%s\\(.* original .*\\)$/\\1%s\\2/'
888 printf "$command\\n" $sha1 $bookmark
890 done > "$DOTEST"/bookmarks.
sed
892 # Now rewrite the rebase script to make use of the bookmarks
893 test ! -s "$DOTEST"/bookmarks.
sed ||
{
894 sed -f "$DOTEST"/bookmarks.
sed < "$TODO" > "$TODO".bookmarks
&&
895 # write short form of typical topic commands:
897 # topic begin blabla at blub
901 # bookmark merge-parent-of-blabla
906 # topic end blabla Merge 'blabla'
911 # goto merge-parent-of-blabla
912 # merge parents blabla original 0123456 Merge 'blabla'
917 topic
=${line#bookmark }
918 topic2
=${topic#merge-parent-of-}
920 # skip comments (after a merge)
921 while case "$line2" in \
#*) ;; *) break ;; esac
923 line
="$(echo "$line"; echo "$line2")"
926 case "$topic,$line2" in
927 "$topic,goto merge-parent-of-$topic")
930 "merge parents $topic"*)
931 msg
="$(echo "$line3" |
933 test "Merge '$topic'" = "$msg" &&
935 echo "topic end $topic $msg"
943 "merge-parent-of-$topic2,goto "*)
944 goto
="at ${line2#goto }"
945 test 'at onto' = "$goto" && goto
=
946 echo "topic begin $topic2 $goto"
956 done < "$TODO".bookmarks
> "$TODO"
957 } || die
'Could not rewrite rebase script using bookmarks'
958 # TODO: maybe it is possible to rewrite in one go? Think about it
959 # TODO: topic end seems not to work when merging branch1, committing to branch1
960 # and merging it again
967 OK_TO_SKIP_PRE_REBASE
=yes
972 is_standalone
"$@" || usage
974 comment_for_reflog
continue
976 test -d "$DOTEST" || die
"No interactive rebase running"
979 git rev-parse
--verify HEAD
>/dev
/null ||
980 die
"Cannot read HEAD"
981 git update-index
--ignore-submodules --refresh &&
982 git diff-files
--quiet --ignore-submodules ||
983 die
"Working tree is dirty"
985 # do we have anything to commit?
986 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
989 test ! -f "$REWRITTEN"/original || add_rewritten
991 .
"$AUTHOR_SCRIPT" ||
992 die
"Cannot find the author identity"
996 amend
=$
(git rev-parse
--verify HEAD
)
997 test "$amend" = $
(cat "$AMEND") ||
999 You have uncommitted changes in your working tree. Please, commit them
1000 first and then run 'git rebase --continue' again."
1001 git
reset --soft HEAD^ ||
1002 die
"Cannot rewind the HEAD"
1004 do_with_author git commit
--no-verify -F "$MSG" -e &&
1006 test -n "$amend" && git
reset --soft $amend
1007 die
"Could not commit staged changes."
1011 require_clean_work_tree
1015 is_standalone
"$@" || usage
1017 comment_for_reflog abort
1020 test -d "$DOTEST" || die
"No interactive rebase running"
1022 HEADNAME
=$
(cat "$DOTEST"/head-name
)
1023 HEAD
=$
(cat "$DOTEST"/head)
1026 git symbolic-ref HEAD
$HEADNAME
1029 output git
reset --hard $HEAD &&
1034 is_standalone
"$@" || usage
1036 comment_for_reflog skip
1039 test -d "$DOTEST" || die
"No interactive rebase running"
1041 test -d "$REWRITTEN" && {
1042 # skip last to-be-rewritten commit
1043 original_count
=$
(wc -l < "$REWRITTEN"/original
)
1044 test $original_count -gt 0 &&
1045 head -n $
(($original_count-1)) < "$REWRITTEN"/original \
1046 > "$REWRITTEN"/original.new
&&
1047 mv "$REWRITTEN"/original.new
"$REWRITTEN"/original
1049 output git
reset --hard && do_rest
1054 STRATEGY
="-s "$
(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1063 # we use merge anyway
1069 # TODO: add an option to rewrite refs that point to rewritten commits
1070 # TODO: add a big fat warning at the end when there are rewritten commits
1071 # that are now unreachable
1085 ONTO
=$
(parse_onto
"$1") ||
1086 die
"Does not point to a valid commit: $1"
1090 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
1091 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
1092 test -d "$DOTEST" &&
1093 die
"Interactive rebase already started"
1095 git var GIT_COMMITTER_IDENT
>/dev
/null ||
1096 die
"You need to set your committer info first"
1098 if test -z "$REBASE_ROOT"
1101 UPSTREAM
=$
(git rev-parse
--verify "$1") ||
1103 test -z "$ONTO" && ONTO
=$UPSTREAM
1109 die
"You must specify --onto when using --root"
1112 run_pre_rebase_hook
"$UPSTREAM_ARG" "$@"
1114 comment_for_reflog start
1116 require_clean_work_tree
1120 output git show-ref
--verify --quiet "refs/heads/$1" ||
1121 die
"Invalid branchname: $1"
1122 output git checkout
"$1" ||
1123 die
"Could not checkout $1"
1126 HEAD
=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
1127 mkdir
"$DOTEST" || die
"Could not create temporary $DOTEST"
1129 : > "$DOTEST"/interactive || die
"Could not mark as interactive"
1130 git symbolic-ref HEAD
> "$DOTEST"/head-name
2> /dev
/null ||
1131 echo "detached HEAD" > "$DOTEST"/head-name
1133 echo $HEAD > "$DOTEST"/head
1134 test -z "$REBASE_ROOT" ||
: >"$DOTEST"/rebase-root
1135 echo $ONTO > "$DOTEST"/onto
1136 test -z "$STRATEGY" ||
echo "$STRATEGY" > "$DOTEST"/strategy
1137 test t
= "$VERBOSE" && : > "$DOTEST"/verbose
1139 SHORTHEAD
=$
(git rev-parse
--short $HEAD)
1140 SHORTONTO
=$
(git rev-parse
--short $ONTO)
1141 SHORTUPSTREAM
=$
(git rev-parse
--short $UPSTREAM)
1142 REVISIONS
=$UPSTREAM...
$HEAD
1143 SHORTREVISIONS
=$SHORTUPSTREAM..
$SHORTHEAD
1145 MERGES_OPTION
=--no-merges
1146 test t
= "$PRESERVE_MERGES" && prepare_preserve_merges
1148 generate_script
> "$TODO"
1149 test -n "$AUTOSQUASH" && rearrange_squash
"$TODO"
1153 test t
= "$PRESERVE_MERGES" && handle_dropped_commits
1155 has_action
"$TODO" ||
1156 die_abort
"Nothing to do"
1158 cp "$TODO" "$TODO".backup
1159 git_editor
"$TODO" ||
1160 die_abort
"Could not execute editor"
1162 has_action
"$TODO" ||
1163 die_abort
"Nothing to do"
1165 # once the user uses -p features, implicitely turn -p on
1166 ! grep "^\(goto\|merge\) " < "$TODO" > /dev
/null ||
1167 mkdir
-p "$REWRITTEN" ||
1168 die
"Could not create directory $REWRITTEN/"
1170 test -d "$REWRITTEN" || skip_unnecessary_picks
1172 git update-ref ORIG_HEAD
$HEAD
1173 output git checkout
$ONTO && do_rest