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 dotest
="$GIT_DIR/rebase-merge"
17 # The file containing rebase commands, comments, and empty lines.
18 # This file is created by "git rebase -i" then edited by the user. As
19 # the lines are processed, they are removed from the front of this
20 # file and written to the tail of $done.
21 todo
="$dotest"/git-rebase-todo
23 # The rebase command lines that have already been processed. A line
24 # is moved here when it is first handled, before any associated user
28 # The commit message that is planned to be used for any changes that
29 # need to be committed following a user interaction.
32 # The file into which is accumulated the suggested commit message for
33 # squash/fixup commands. When the first of a series of squash/fixups
34 # is seen, the file is created and the commit message from the
35 # previous commit and from the first squash/fixup commit are written
36 # to it. The commit message for each subsequent squash/fixup commit
37 # is appended to the file as it is processed.
39 # The first line of the file is of the form
40 # # This is a combination of $count commits.
41 # where $count is the number of commits whose messages have been
42 # written to the file so far (including the initial "pick" commit).
43 # Each time that a commit message is processed, this line is read and
44 # updated. It is deleted just before the combined commit is made.
45 squash_msg
="$dotest"/message-squash
47 # If the current series of squash/fixups has not yet included a squash
48 # command, then this file exists and holds the commit message of the
49 # original "pick" commit. (If the series ends without a "squash"
50 # command, then this can be used as the commit message of the combined
51 # commit without opening the editor.)
52 fixup_msg
="$dotest"/message-fixup
54 # $rewritten is the name of a directory containing files for each
55 # commit that is reachable by at least one merge base of $head and
56 # $upstream. They are not necessarily rewritten, but their children
57 # might be. This ensures that commits on merged, but otherwise
58 # unrelated side branches are left alone. (Think "X" in the man page's
60 rewritten
="$dotest"/rewritten
62 dropped
="$dotest"/dropped
64 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
65 # GIT_AUTHOR_DATE that will be used for the commit that is currently
67 author_script
="$dotest"/author-script
69 # When an "edit" rebase command is being processed, the SHA1 of the
70 # commit to be edited is recorded in this file. When "git rebase
71 # --continue" is executed, if there are any staged changes then they
72 # will be amended to the HEAD commit, but only provided the HEAD
73 # commit is still the commit to be edited. When any other rebase
74 # command is processed, this file is deleted.
77 # For the post-rewrite hook, we make a list of rewritten commits and
78 # their new sha1s. The rewritten-pending list keeps the sha1s of
79 # commits that have been processed, but not committed yet,
80 # e.g. because they are waiting for a 'squash' command.
81 rewritten_list
="$dotest"/rewritten-list
82 rewritten_pending
="$dotest"/rewritten-pending
84 GIT_CHERRY_PICK_HELP
="$resolvemsg"
85 export GIT_CHERRY_PICK_HELP
88 printf '%s\n' "$*" >&2
96 test $status != 0 && printf "%s\n" "$output"
105 # Output the commit message for the specified commit.
107 git cat-file commit
"$1" |
sed "1,/^$/d"
110 orig_reflog_action
="$GIT_REFLOG_ACTION"
112 comment_for_reflog
() {
113 case "$orig_reflog_action" in
115 GIT_REFLOG_ACTION
="rebase -i ($1)"
116 export GIT_REFLOG_ACTION
122 mark_action_done
() {
123 sed -e 1q
< "$todo" >> "$done"
124 sed -e 1d
< "$todo" >> "$todo".new
125 mv -f "$todo".new
"$todo"
126 new_count
=$
(sane_grep
-c '^[^#]' < "$done")
127 total
=$
(($new_count+$
(sane_grep
-c '^[^#]' < "$todo")))
128 if test "$last_count" != "$new_count"
130 last_count
=$new_count
131 printf "Rebasing (%d/%d)\r" $new_count $total
132 test -z "$verbose" ||
echo
137 sha1_and_parents
="$(git rev-list --parents -1 "$1")"
138 case "$sha1_and_parents" in
140 git
diff --cc $sha1_and_parents
143 git diff-tree
-p "$1^!"
148 esac > "$dotest"/patch
150 commit_message
"$1" > "$msg"
151 test -f "$author_script" ||
152 get_author_ident_from_commit
"$1" > "$author_script"
156 echo "$1" > "$dotest"/stopped-sha
168 sane_grep
'^[^#]' "$1" >/dev
/null
171 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
172 # GIT_AUTHOR_DATE exported from the current environment.
175 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
182 case "$1" in -n) sha1
=$2; ff
= ;; *) sha1
=$1 ;; esac
183 case "$force_rebase" in '') ;; ?
*) ff
= ;; esac
184 output git rev-parse
--verify $sha1 || die
"Invalid commit name: $sha1"
185 test -d "$rewritten" &&
186 pick_one_preserving_merges
"$@" && return
187 if test -n "$rebase_root"
189 output git cherry-pick
"$@"
192 output git cherry-pick
$ff "$@"
195 pick_one_preserving_merges
() {
206 sha1
=$
(git rev-parse
$sha1)
208 if test -f "$dotest"/current-commit
210 if test "$fast_forward" = t
212 while read current_commit
214 git rev-parse HEAD
> "$rewritten"/$current_commit
215 done <"$dotest"/current-commit
216 rm "$dotest"/current-commit ||
217 die
"Cannot write current commit's replacement sha1"
221 echo $sha1 >> "$dotest"/current-commit
223 # rewrite parents; if none were rewritten, we can fast-forward.
225 pend
=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
226 if test "$pend" = " "
230 while [ "$pend" != "" ]
232 p
=$
(expr "$pend" : ' \([^ ]*\)')
235 if test -f "$rewritten"/$p
237 new_p
=$
(cat "$rewritten"/$p)
239 # If the todo reordered commits, and our parent is marked for
240 # rewriting, but hasn't been gotten to yet, assume the user meant to
241 # drop it on top of the current HEAD
244 new_p
=$
(git rev-parse HEAD
)
247 test $p != $new_p && fast_forward
=f
248 case "$new_parents" in
250 ;; # do nothing; that parent is already there
252 new_parents
="$new_parents $new_p"
256 if test -f "$dropped"/$p
259 replacement
="$(cat "$dropped"/$p)"
260 test -z "$replacement" && replacement
=root
261 pend
=" $replacement$pend"
263 new_parents
="$new_parents $p"
267 case $fast_forward in
269 output warn
"Fast-forward to $sha1"
270 output git
reset --hard $sha1 ||
271 die
"Cannot fast-forward to $sha1"
274 first_parent
=$
(expr "$new_parents" : ' \([^ ]*\)')
278 # detach HEAD to current parent
279 output git checkout
$first_parent 2> /dev
/null ||
280 die
"Cannot move HEAD to $first_parent"
283 case "$new_parents" in
285 test "a$1" = a-n
&& die
"Refusing to squash a merge: $sha1"
288 author_script_content
=$
(get_author_ident_from_commit
$sha1)
289 eval "$author_script_content"
290 msg_content
="$(commit_message $sha1)"
291 # No point in merging the first parent, that's HEAD
292 new_parents
=${new_parents# $first_parent}
293 if ! do_with_author output \
294 git merge
${strategy:+-s $strategy} -m \
295 "$msg_content" $new_parents
297 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
298 die_with_patch
$sha1 "Error redoing merge $sha1"
300 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
303 output git cherry-pick
"$@" ||
304 die_with_patch
$sha1 "Could not pick $sha1"
313 *1[0-9]|
*[04-9]) echo "$1"th
;;
320 update_squash_messages
() {
321 if test -f "$squash_msg"; then
322 mv "$squash_msg" "$squash_msg".bak ||
exit
324 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
325 -e "q" < "$squash_msg".bak
)+1))
327 echo "# This is a combination of $count commits."
328 sed -e 1d
-e '2,/^./{
330 }' <"$squash_msg".bak
333 commit_message HEAD
> "$fixup_msg" || die
"Cannot write $fixup_msg"
336 echo "# This is a combination of 2 commits."
337 echo "# The first commit's message is:"
346 echo "# This is the $(nth_string $count) commit message:"
352 echo "# The $(nth_string $count) commit message will be skipped:"
354 commit_message
$2 |
sed -e 's/^/# /'
359 peek_next_command
() {
360 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$todo"
363 # A squash/fixup has failed. Prepare the long version of the squash
364 # commit message, then die_with_patch. This code path requires the
365 # user to edit the combined commit message for all commits that have
366 # been squashed/fixedup so far. So also erase the old squash
367 # messages, effectively causing the combined commit to be used as the
368 # new basis for any further squash/fixups. Args: sha1 rest
369 die_failed_squash
() {
370 mv "$squash_msg" "$msg" ||
exit
372 cp "$msg" "$GIT_DIR"/MERGE_MSG ||
exit
374 warn
"Could not apply $1... $2"
378 flush_rewritten_pending
() {
379 test -s "$rewritten_pending" ||
return
380 newsha1
="$(git rev-parse HEAD^0)"
381 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
382 rm -f "$rewritten_pending"
385 record_in_rewritten
() {
386 oldsha1
="$(git rev-parse $1)"
387 echo "$oldsha1" >> "$rewritten_pending"
389 case "$(peek_next_command)" in
393 flush_rewritten_pending
399 rm -f "$msg" "$author_script" "$amend" ||
exit
400 read -r command sha1 rest
< "$todo"
406 comment_for_reflog pick
410 die_with_patch
$sha1 "Could not apply $sha1... $rest"
411 record_in_rewritten
$sha1
414 comment_for_reflog reword
418 die_with_patch
$sha1 "Could not apply $sha1... $rest"
419 git commit
--amend --no-post-rewrite
420 record_in_rewritten
$sha1
423 comment_for_reflog edit
427 die_with_patch
$sha1 "Could not apply $sha1... $rest"
428 echo "$sha1" > "$dotest"/stopped-sha
430 git rev-parse
--verify HEAD
> "$amend"
431 warn
"Stopped at $sha1... $rest"
432 warn
"You can amend the commit now, with"
434 warn
" git commit --amend"
436 warn
"Once you are satisfied with your changes, run"
438 warn
" git rebase --continue"
451 comment_for_reflog
$squash_style
453 test -f "$done" && has_action
"$done" ||
454 die
"Cannot '$squash_style' without a previous commit"
457 update_squash_messages
$squash_style $sha1
458 author_script_content
=$
(get_author_ident_from_commit HEAD
)
459 echo "$author_script_content" > "$author_script"
460 eval "$author_script_content"
461 output git
reset --soft HEAD^
462 pick_one
-n $sha1 || die_failed_squash
$sha1 "$rest"
463 case "$(peek_next_command)" in
465 # This is an intermediate commit; its message will only be
466 # used in case of trouble. So use the long version:
467 do_with_author output git commit
--no-verify -F "$squash_msg" ||
468 die_failed_squash
$sha1 "$rest"
471 # This is the final command of this squash/fixup group
472 if test -f "$fixup_msg"
474 do_with_author git commit
--no-verify -F "$fixup_msg" ||
475 die_failed_squash
$sha1 "$rest"
477 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG ||
exit
478 rm -f "$GIT_DIR"/MERGE_MSG
479 do_with_author git commit
--no-verify -e ||
480 die_failed_squash
$sha1 "$rest"
482 rm -f "$squash_msg" "$fixup_msg"
485 record_in_rewritten
$sha1
488 read -r command rest
< "$todo"
490 printf 'Executing: %s\n' "$rest"
491 # "exec" command doesn't take a sha1 in the todo-list.
492 # => can't just use $sha1 here.
493 git rev-parse
--verify HEAD
> "$dotest"/stopped-sha
494 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
496 if test "$status" -ne 0
498 warn
"Execution failed: $rest"
499 warn
"You can fix the problem, and then run"
501 warn
" git rebase --continue"
505 # Run in subshell because require_clean_work_tree can die.
506 if ! (require_clean_work_tree
"rebase")
508 warn
"Commit or stash your changes, and then run"
510 warn
" git rebase --continue"
516 warn
"Unknown command: $command $sha1 $rest"
517 if git rev-parse
--verify -q "$sha1" >/dev
/null
519 die_with_patch
$sha1 "Please fix this in the file $todo."
521 die
"Please fix this in the file $todo."
525 test -s "$todo" && return
527 comment_for_reflog finish
&&
528 headname
=$
(cat "$dotest"/head-name
) &&
529 oldhead
=$
(cat "$dotest"/head) &&
530 shortonto
=$
(git rev-parse
--short $
(cat "$dotest"/onto
)) &&
531 newhead
=$
(git rev-parse HEAD
) &&
534 message
="$GIT_REFLOG_ACTION: $headname onto $shortonto" &&
535 git update-ref
-m "$message" $headname $newhead $oldhead &&
536 git symbolic-ref HEAD
$headname
539 test ! -f "$dotest"/verbose ||
540 git diff-tree
--stat $
(cat "$dotest"/head)..HEAD
543 test -s "$rewritten_list" &&
544 git notes copy
--for-rewrite=rebase
< "$rewritten_list" ||
545 true
# we don't care if this copying failed
547 if test -x "$GIT_DIR"/hooks
/post-rewrite
&&
548 test -s "$rewritten_list"; then
549 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$rewritten_list"
550 true
# we don't care if this hook failed
554 warn
"Successfully rebased and updated $headname."
566 # skip picking commits whose parents are unchanged
567 skip_unnecessary_picks
() {
569 while read -r command rest
571 # fd=3 means we skip the command
572 case "$fd,$command" in
574 # pick a commit whose parent is current $onto -> skip
576 case "$(git rev-parse --verify --quiet "$sha1"^)" in
592 printf '%s\n' "$command${rest:+ }$rest" >&$fd
593 done <"$todo" >"$todo.new" 3>>"$done" &&
594 mv -f "$todo".new
"$todo" &&
595 case "$(peek_next_command)" in
597 record_in_rewritten
"$onto"
600 die
"Could not skip unnecessary pick commands"
603 get_saved_options
() {
604 test -d "$rewritten" && preserve_merges
=t
605 test -f "$dotest"/strategy
&& strategy
="$(cat "$dotest"/strategy)"
606 test -f "$dotest"/verbose
&& verbose
=t
607 test -f "$dotest"/rebase-root
&& rebase_root
=t
610 # Rearrange the todo list that has both "pick sha1 msg" and
611 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
612 # comes immediately after the former, and change "pick" to
614 rearrange_squash
() {
615 # extract fixup!/squash! lines and resolve any referenced sha1's
616 while read -r pick sha1 message
619 "squash! "*|
"fixup! "*)
620 action
="${message%%!*}"
621 rest
="${message#*! }"
622 echo "$sha1 $action $rest"
623 # if it's a single word, try to resolve to a full sha1 and
624 # emit a second copy. This allows us to match on both message
626 if test "${rest#* }" = "$rest"; then
627 fullsha
="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
628 if test -n "$fullsha"; then
629 # prefix the action to uniquely identify this line as
630 # intended for full sha1 match
631 echo "$sha1 +$action $fullsha"
636 test -s "$1.sq" ||
return
639 while read -r pick sha1 message
642 *" $sha1 "*) continue ;;
644 printf '%s\n' "$pick $sha1 $message"
646 while read -r squash action msg_content
649 *" $squash "*) continue ;;
655 # full sha1 prefix test
656 case "$msg_content" in "$sha1"*) emit
=1;; esac ;;
658 # message prefix test
659 case "$message" in "$msg_content"*) emit
=1;; esac ;;
661 if test $emit = 1; then
662 printf '%s\n' "$action $squash $action! $msg_content"
666 done >"$1.rearranged" <"$1"
667 cat "$1.rearranged" >"$1"
668 rm -f "$1.sq" "$1.rearranged"
674 comment_for_reflog
continue
677 git rev-parse
--verify HEAD
>/dev
/null ||
678 die
"Cannot read HEAD"
679 git update-index
--ignore-submodules --refresh &&
680 git diff-files
--quiet --ignore-submodules ||
681 die
"Working tree is dirty"
683 # do we have anything to commit?
684 if git diff-index
--cached --quiet --ignore-submodules HEAD
--
686 : Nothing to commit
-- skip this
688 .
"$author_script" ||
689 die
"Cannot find the author identity"
693 current_head
=$
(git rev-parse
--verify HEAD
)
694 test "$current_head" = $
(cat "$amend") ||
696 You have uncommitted changes in your working tree. Please, commit them
697 first and then run 'git rebase --continue' again."
698 git
reset --soft HEAD^ ||
699 die
"Cannot rewind the HEAD"
701 do_with_author git commit
--no-verify -F "$msg" -e ||
{
702 test -n "$current_head" && git
reset --soft $current_head
703 die
"Could not commit staged changes."
707 record_in_rewritten
"$(cat "$dotest"/stopped-sha)"
709 require_clean_work_tree
"rebase"
714 comment_for_reflog abort
718 headname
=$
(cat "$dotest"/head-name
)
719 head=$
(cat "$dotest"/head)
722 git symbolic-ref HEAD
$headname
725 output git
reset --hard $head &&
731 comment_for_reflog skip
735 output git
reset --hard && do_rest
739 git var GIT_COMMITTER_IDENT
>/dev
/null ||
740 die
"You need to set your committer info first"
742 comment_for_reflog start
744 if test ! -z "$switch_to"
746 output git checkout
"$switch_to" -- ||
747 die
"Could not checkout $switch_to"
750 head=$
(git rev-parse
--verify HEAD
) || die
"No HEAD?"
751 mkdir
"$dotest" || die
"Could not create temporary $dotest"
753 : > "$dotest"/interactive || die
"Could not mark as interactive"
754 echo "$head_name" > "$dotest"/head-name
756 echo $head > "$dotest"/head
757 case "$rebase_root" in
759 rm -f "$dotest"/rebase-root
;;
761 : >"$dotest"/rebase-root
;;
763 echo $onto > "$dotest"/onto
764 test -z "$strategy" ||
echo "$strategy" > "$dotest"/strategy
765 test t
= "$verbose" && : > "$dotest"/verbose
766 if test t
= "$preserve_merges"
768 if test -z "$rebase_root"
770 mkdir
"$rewritten" &&
771 for c
in $
(git merge-base
--all $head $upstream)
773 echo $onto > "$rewritten"/$c ||
774 die
"Could not init rewritten commits"
777 mkdir
"$rewritten" &&
778 echo $onto > "$rewritten"/root ||
779 die
"Could not init rewritten commits"
781 # No cherry-pick because our first pass is to determine
782 # parents to rewrite and skipping dropped commits would
783 # prematurely end our probe
785 first_after_upstream
="$(git rev-list --reverse --first-parent $upstream..$head | head -n 1)"
787 merges_option
="--no-merges --cherry-pick"
790 shorthead
=$
(git rev-parse
--short $head)
791 shortonto
=$
(git rev-parse
--short $onto)
792 if test -z "$rebase_root"
793 # this is now equivalent to ! -z "$upstream"
795 shortupstream
=$
(git rev-parse
--short $upstream)
796 revisions
=$upstream...
$head
797 shortrevisions
=$shortupstream..
$shorthead
799 revisions
=$onto...
$head
800 shortrevisions
=$shorthead
802 git rev-list
$merges_option --pretty=oneline
--abbrev-commit \
803 --abbrev=7 --reverse --left-right --topo-order \
806 while read -r shortsha1 rest
808 if test t
!= "$preserve_merges"
810 printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
812 sha1
=$
(git rev-parse
$shortsha1)
813 if test -z "$rebase_root"
816 for p
in $
(git rev-list
--parents -1 $sha1 | cut
-d' ' -s -f2-)
818 if test -f "$rewritten"/$p -a \
( $p != $onto -o $sha1 = $first_after_upstream \
)
826 if test f
= "$preserve"
828 touch "$rewritten"/$sha1
829 printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
834 # Watch for commits that been dropped by --cherry-pick
835 if test t
= "$preserve_merges"
838 # Save all non-cherry-picked changes
839 git rev-list
$revisions --left-right --cherry-pick | \
840 sed -n "s/^>//p" > "$dotest"/not-cherry-picks
841 # Now all commits and note which ones are missing in
842 # not-cherry-picks and hence being dropped
843 git rev-list
$revisions |
846 if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$dotest"/not-cherry-picks)" = ""
848 # Use -f2 because if rev-list is telling us this commit is
849 # not worthwhile, we don't want to track its multiple heads,
850 # just the history of its first-parent for others that will
851 # be rebasing on top of it
852 git rev-list
--parents -1 $rev | cut
-d' ' -s -f2 > "$dropped"/$rev
853 short
=$
(git rev-list
-1 --abbrev-commit --abbrev=7 $rev)
854 sane_grep
-v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
860 test -s "$todo" ||
echo noop
>> "$todo"
861 test -n "$autosquash" && rearrange_squash
"$todo"
862 cat >> "$todo" << EOF
864 # Rebase $shortrevisions onto $shortonto
867 # p, pick = use commit
868 # r, reword = use commit, but edit the commit message
869 # e, edit = use commit, but stop for amending
870 # s, squash = use commit, but meld into previous commit
871 # f, fixup = like "squash", but discard this commit's log message
872 # x, exec = run command (the rest of the line) using shell
874 # If you remove a line here THAT COMMIT WILL BE LOST.
875 # However, if you remove everything, the rebase will be aborted.
879 has_action
"$todo" ||
880 die_abort
"Nothing to do"
882 cp "$todo" "$todo".backup
883 git_editor
"$todo" ||
884 die_abort
"Could not execute editor"
886 has_action
"$todo" ||
887 die_abort
"Nothing to do"
889 test -d "$rewritten" ||
test -n "$force_rebase" || skip_unnecessary_picks
891 output git checkout
$onto || die_abort
"could not detach HEAD"
892 git update-ref ORIG_HEAD
$head