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 # The file containing rebase commands, comments, and empty lines.
 
  16 # This file is created by "git rebase -i" then edited by the user.  As
 
  17 # the lines are processed, they are removed from the front of this
 
  18 # file and written to the tail of $done.
 
  19 todo="$state_dir"/git-rebase-todo
 
  21 # The rebase command lines that have already been processed.  A line
 
  22 # is moved here when it is first handled, before any associated user
 
  24 done="$state_dir"/done
 
  26 # The commit message that is planned to be used for any changes that
 
  27 # need to be committed following a user interaction.
 
  28 msg="$state_dir"/message
 
  30 # The file into which is accumulated the suggested commit message for
 
  31 # squash/fixup commands.  When the first of a series of squash/fixups
 
  32 # is seen, the file is created and the commit message from the
 
  33 # previous commit and from the first squash/fixup commit are written
 
  34 # to it.  The commit message for each subsequent squash/fixup commit
 
  35 # is appended to the file as it is processed.
 
  37 # The first line of the file is of the form
 
  38 #     # This is a combination of $count commits.
 
  39 # where $count is the number of commits whose messages have been
 
  40 # written to the file so far (including the initial "pick" commit).
 
  41 # Each time that a commit message is processed, this line is read and
 
  42 # updated.  It is deleted just before the combined commit is made.
 
  43 squash_msg="$state_dir"/message-squash
 
  45 # If the current series of squash/fixups has not yet included a squash
 
  46 # command, then this file exists and holds the commit message of the
 
  47 # original "pick" commit.  (If the series ends without a "squash"
 
  48 # command, then this can be used as the commit message of the combined
 
  49 # commit without opening the editor.)
 
  50 fixup_msg="$state_dir"/message-fixup
 
  52 # $rewritten is the name of a directory containing files for each
 
  53 # commit that is reachable by at least one merge base of $head and
 
  54 # $upstream. They are not necessarily rewritten, but their children
 
  55 # might be.  This ensures that commits on merged, but otherwise
 
  56 # unrelated side branches are left alone. (Think "X" in the man page's
 
  58 rewritten="$state_dir"/rewritten
 
  60 dropped="$state_dir"/dropped
 
  62 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
 
  63 # GIT_AUTHOR_DATE that will be used for the commit that is currently
 
  65 author_script="$state_dir"/author-script
 
  67 # When an "edit" rebase command is being processed, the SHA1 of the
 
  68 # commit to be edited is recorded in this file.  When "git rebase
 
  69 # --continue" is executed, if there are any staged changes then they
 
  70 # will be amended to the HEAD commit, but only provided the HEAD
 
  71 # commit is still the commit to be edited.  When any other rebase
 
  72 # command is processed, this file is deleted.
 
  73 amend="$state_dir"/amend
 
  75 # For the post-rewrite hook, we make a list of rewritten commits and
 
  76 # their new sha1s.  The rewritten-pending list keeps the sha1s of
 
  77 # commits that have been processed, but not committed yet,
 
  78 # e.g. because they are waiting for a 'squash' command.
 
  79 rewritten_list="$state_dir"/rewritten-list
 
  80 rewritten_pending="$state_dir"/rewritten-pending
 
  82 GIT_CHERRY_PICK_HELP="$resolvemsg"
 
  83 export GIT_CHERRY_PICK_HELP
 
  86         printf '%s\n' "$*" >&2
 
  89 # Output the commit message for the specified commit.
 
  91         git cat-file commit "$1" | sed "1,/^$/d"
 
  94 orig_reflog_action="$GIT_REFLOG_ACTION"
 
  96 comment_for_reflog () {
 
  97         case "$orig_reflog_action" in
 
  99                 GIT_REFLOG_ACTION="rebase -i ($1)"
 
 100                 export GIT_REFLOG_ACTION
 
 106 mark_action_done () {
 
 107         sed -e 1q < "$todo" >> "$done"
 
 108         sed -e 1d < "$todo" >> "$todo".new
 
 109         mv -f "$todo".new "$todo"
 
 110         new_count=$(sane_grep -c '^[^#]' < "$done")
 
 111         total=$(($new_count+$(sane_grep -c '^[^#]' < "$todo")))
 
 112         if test "$last_count" != "$new_count"
 
 114                 last_count=$new_count
 
 115                 printf "Rebasing (%d/%d)\r" $new_count $total
 
 116                 test -z "$verbose" || echo
 
 121         sha1_and_parents="$(git rev-list --parents -1 "$1")"
 
 122         case "$sha1_and_parents" in
 
 124                 git diff --cc $sha1_and_parents
 
 127                 git diff-tree -p "$1^!"
 
 132         esac > "$state_dir"/patch
 
 134                 commit_message "$1" > "$msg"
 
 135         test -f "$author_script" ||
 
 136                 get_author_ident_from_commit "$1" > "$author_script"
 
 140         echo "$1" > "$state_dir"/stopped-sha
 
 152         sane_grep '^[^#]' "$1" >/dev/null
 
 155 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
 
 156 # GIT_AUTHOR_DATE exported from the current environment.
 
 159                 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 
 166         case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
 
 167         case "$force_rebase" in '') ;; ?*) ff= ;; esac
 
 168         output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
 
 169         test -d "$rewritten" &&
 
 170                 pick_one_preserving_merges "$@" && return
 
 171         output git cherry-pick $ff "$@"
 
 174 pick_one_preserving_merges () {
 
 185         sha1=$(git rev-parse $sha1)
 
 187         if test -f "$state_dir"/current-commit
 
 189                 if test "$fast_forward" = t
 
 191                         while read current_commit
 
 193                                 git rev-parse HEAD > "$rewritten"/$current_commit
 
 194                         done <"$state_dir"/current-commit
 
 195                         rm "$state_dir"/current-commit ||
 
 196                         die "Cannot write current commit's replacement sha1"
 
 200         echo $sha1 >> "$state_dir"/current-commit
 
 202         # rewrite parents; if none were rewritten, we can fast-forward.
 
 204         pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
 
 205         if test "$pend" = " "
 
 209         while [ "$pend" != "" ]
 
 211                 p=$(expr "$pend" : ' \([^ ]*\)')
 
 214                 if test -f "$rewritten"/$p
 
 216                         new_p=$(cat "$rewritten"/$p)
 
 218                         # If the todo reordered commits, and our parent is marked for
 
 219                         # rewriting, but hasn't been gotten to yet, assume the user meant to
 
 220                         # drop it on top of the current HEAD
 
 223                                 new_p=$(git rev-parse HEAD)
 
 226                         test $p != $new_p && fast_forward=f
 
 227                         case "$new_parents" in
 
 229                                 ;; # do nothing; that parent is already there
 
 231                                 new_parents="$new_parents $new_p"
 
 235                         if test -f "$dropped"/$p
 
 238                                 replacement="$(cat "$dropped"/$p)"
 
 239                                 test -z "$replacement" && replacement=root
 
 240                                 pend=" $replacement$pend"
 
 242                                 new_parents="$new_parents $p"
 
 246         case $fast_forward in
 
 248                 output warn "Fast-forward to $sha1"
 
 249                 output git reset --hard $sha1 ||
 
 250                         die "Cannot fast-forward to $sha1"
 
 253                 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 
 257                         # detach HEAD to current parent
 
 258                         output git checkout $first_parent 2> /dev/null ||
 
 259                                 die "Cannot move HEAD to $first_parent"
 
 262                 case "$new_parents" in
 
 264                         test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 
 267                         author_script_content=$(get_author_ident_from_commit $sha1)
 
 268                         eval "$author_script_content"
 
 269                         msg_content="$(commit_message $sha1)"
 
 270                         # No point in merging the first parent, that's HEAD
 
 271                         new_parents=${new_parents# $first_parent}
 
 272                         if ! do_with_author output \
 
 273                                 git merge --no-ff ${strategy:+-s $strategy} -m \
 
 274                                         "$msg_content" $new_parents
 
 276                                 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
 
 277                                 die_with_patch $sha1 "Error redoing merge $sha1"
 
 279                         echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
 
 282                         output git cherry-pick "$@" ||
 
 283                                 die_with_patch $sha1 "Could not pick $sha1"
 
 292         *1[0-9]|*[04-9]) echo "$1"th;;
 
 299 update_squash_messages () {
 
 300         if test -f "$squash_msg"; then
 
 301                 mv "$squash_msg" "$squash_msg".bak || exit
 
 303                         -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
 
 304                         -e "q" < "$squash_msg".bak)+1))
 
 306                         echo "# This is a combination of $count commits."
 
 307                         sed -e 1d -e '2,/^./{
 
 309                         }' <"$squash_msg".bak
 
 312                 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
 
 315                         echo "# This is a combination of 2 commits."
 
 316                         echo "# The first commit's message is:"
 
 325                 echo "# This is the $(nth_string $count) commit message:"
 
 331                 echo "# The $(nth_string $count) commit message will be skipped:"
 
 333                 commit_message $2 | sed -e 's/^/#       /'
 
 338 peek_next_command () {
 
 339         sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$todo"
 
 342 # A squash/fixup has failed.  Prepare the long version of the squash
 
 343 # commit message, then die_with_patch.  This code path requires the
 
 344 # user to edit the combined commit message for all commits that have
 
 345 # been squashed/fixedup so far.  So also erase the old squash
 
 346 # messages, effectively causing the combined commit to be used as the
 
 347 # new basis for any further squash/fixups.  Args: sha1 rest
 
 348 die_failed_squash() {
 
 349         mv "$squash_msg" "$msg" || exit
 
 351         cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
 
 353         warn "Could not apply $1... $2"
 
 357 flush_rewritten_pending() {
 
 358         test -s "$rewritten_pending" || return
 
 359         newsha1="$(git rev-parse HEAD^0)"
 
 360         sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
 
 361         rm -f "$rewritten_pending"
 
 364 record_in_rewritten() {
 
 365         oldsha1="$(git rev-parse $1)"
 
 366         echo "$oldsha1" >> "$rewritten_pending"
 
 368         case "$(peek_next_command)" in
 
 372                 flush_rewritten_pending
 
 378         rm -f "$msg" "$author_script" "$amend" || exit
 
 379         read -r command sha1 rest < "$todo"
 
 385                 comment_for_reflog pick
 
 389                         die_with_patch $sha1 "Could not apply $sha1... $rest"
 
 390                 record_in_rewritten $sha1
 
 393                 comment_for_reflog reword
 
 397                         die_with_patch $sha1 "Could not apply $sha1... $rest"
 
 398                 git commit --amend --no-post-rewrite
 
 399                 record_in_rewritten $sha1
 
 402                 comment_for_reflog edit
 
 406                         die_with_patch $sha1 "Could not apply $sha1... $rest"
 
 407                 echo "$sha1" > "$state_dir"/stopped-sha
 
 409                 git rev-parse --verify HEAD > "$amend"
 
 410                 warn "Stopped at $sha1... $rest"
 
 411                 warn "You can amend the commit now, with"
 
 413                 warn "  git commit --amend"
 
 415                 warn "Once you are satisfied with your changes, run"
 
 417                 warn "  git rebase --continue"
 
 430                 comment_for_reflog $squash_style
 
 432                 test -f "$done" && has_action "$done" ||
 
 433                         die "Cannot '$squash_style' without a previous commit"
 
 436                 update_squash_messages $squash_style $sha1
 
 437                 author_script_content=$(get_author_ident_from_commit HEAD)
 
 438                 echo "$author_script_content" > "$author_script"
 
 439                 eval "$author_script_content"
 
 440                 output git reset --soft HEAD^
 
 441                 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
 
 442                 case "$(peek_next_command)" in
 
 444                         # This is an intermediate commit; its message will only be
 
 445                         # used in case of trouble.  So use the long version:
 
 446                         do_with_author output git commit --no-verify -F "$squash_msg" ||
 
 447                                 die_failed_squash $sha1 "$rest"
 
 450                         # This is the final command of this squash/fixup group
 
 451                         if test -f "$fixup_msg"
 
 453                                 do_with_author git commit --no-verify -F "$fixup_msg" ||
 
 454                                         die_failed_squash $sha1 "$rest"
 
 456                                 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
 
 457                                 rm -f "$GIT_DIR"/MERGE_MSG
 
 458                                 do_with_author git commit --no-verify -e ||
 
 459                                         die_failed_squash $sha1 "$rest"
 
 461                         rm -f "$squash_msg" "$fixup_msg"
 
 464                 record_in_rewritten $sha1
 
 467                 read -r command rest < "$todo"
 
 469                 printf 'Executing: %s\n' "$rest"
 
 470                 # "exec" command doesn't take a sha1 in the todo-list.
 
 471                 # => can't just use $sha1 here.
 
 472                 git rev-parse --verify HEAD > "$state_dir"/stopped-sha
 
 473                 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
 
 475                 if test "$status" -ne 0
 
 477                         warn "Execution failed: $rest"
 
 478                         warn "You can fix the problem, and then run"
 
 480                         warn "  git rebase --continue"
 
 484                 # Run in subshell because require_clean_work_tree can die.
 
 485                 if ! (require_clean_work_tree "rebase")
 
 487                         warn "Commit or stash your changes, and then run"
 
 489                         warn "  git rebase --continue"
 
 495                 warn "Unknown command: $command $sha1 $rest"
 
 496                 if git rev-parse --verify -q "$sha1" >/dev/null
 
 498                         die_with_patch $sha1 "Please fix this in the file $todo."
 
 500                         die "Please fix this in the file $todo."
 
 504         test -s "$todo" && return
 
 506         comment_for_reflog finish &&
 
 507         shortonto=$(git rev-parse --short $onto) &&
 
 508         newhead=$(git rev-parse HEAD) &&
 
 511                 message="$GIT_REFLOG_ACTION: $head_name onto $shortonto" &&
 
 512                 git update-ref -m "$message" $head_name $newhead $orig_head &&
 
 514                   -m "$GIT_REFLOG_ACTION: returning to $head_name" \
 
 518                 test ! -f "$state_dir"/verbose ||
 
 519                         git diff-tree --stat $orig_head..HEAD
 
 522                 test -s "$rewritten_list" &&
 
 523                 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
 
 524                 true # we don't care if this copying failed
 
 526         if test -x "$GIT_DIR"/hooks/post-rewrite &&
 
 527                 test -s "$rewritten_list"; then
 
 528                 "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
 
 529                 true # we don't care if this hook failed
 
 531         rm -rf "$state_dir" &&
 
 533         warn "Successfully rebased and updated $head_name."
 
 545 # skip picking commits whose parents are unchanged
 
 546 skip_unnecessary_picks () {
 
 548         while read -r command rest
 
 550                 # fd=3 means we skip the command
 
 551                 case "$fd,$command" in
 
 553                         # pick a commit whose parent is current $onto -> skip
 
 555                         case "$(git rev-parse --verify --quiet "$sha1"^)" in
 
 571                 printf '%s\n' "$command${rest:+ }$rest" >&$fd
 
 572         done <"$todo" >"$todo.new" 3>>"$done" &&
 
 573         mv -f "$todo".new "$todo" &&
 
 574         case "$(peek_next_command)" in
 
 576                 record_in_rewritten "$onto"
 
 579         die "Could not skip unnecessary pick commands"
 
 582 # Rearrange the todo list that has both "pick sha1 msg" and
 
 583 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
 
 584 # comes immediately after the former, and change "pick" to
 
 586 rearrange_squash () {
 
 587         # extract fixup!/squash! lines and resolve any referenced sha1's
 
 588         while read -r pick sha1 message
 
 591                 "squash! "*|"fixup! "*)
 
 592                         action="${message%%!*}"
 
 593                         rest="${message#*! }"
 
 594                         echo "$sha1 $action $rest"
 
 595                         # if it's a single word, try to resolve to a full sha1 and
 
 596                         # emit a second copy. This allows us to match on both message
 
 598                         if test "${rest#* }" = "$rest"; then
 
 599                                 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
 
 600                                 if test -n "$fullsha"; then
 
 601                                         # prefix the action to uniquely identify this line as
 
 602                                         # intended for full sha1 match
 
 603                                         echo "$sha1 +$action $fullsha"
 
 608         test -s "$1.sq" || return
 
 611         while read -r pick sha1 message
 
 614                 *" $sha1 "*) continue ;;
 
 616                 printf '%s\n' "$pick $sha1 $message"
 
 618                 while read -r squash action msg_content
 
 621                         *" $squash "*) continue ;;
 
 627                                 # full sha1 prefix test
 
 628                                 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
 
 630                                 # message prefix test
 
 631                                 case "$message" in "$msg_content"*) emit=1;; esac ;;
 
 633                         if test $emit = 1; then
 
 634                                 printf '%s\n' "$action $squash $action! $msg_content"
 
 638         done >"$1.rearranged" <"$1"
 
 639         cat "$1.rearranged" >"$1"
 
 640         rm -f "$1.sq" "$1.rearranged"
 
 645         # do we have anything to commit?
 
 646         if git diff-index --cached --quiet --ignore-submodules HEAD --
 
 648                 : Nothing to commit -- skip this
 
 650                 . "$author_script" ||
 
 651                         die "Cannot find the author identity"
 
 655                         current_head=$(git rev-parse --verify HEAD)
 
 656                         test "$current_head" = $(cat "$amend") ||
 
 658 You have uncommitted changes in your working tree. Please, commit them
 
 659 first and then run 'git rebase --continue' again."
 
 660                         git reset --soft HEAD^ ||
 
 661                         die "Cannot rewind the HEAD"
 
 663                 do_with_author git commit --no-verify -F "$msg" -e || {
 
 664                         test -n "$current_head" && git reset --soft $current_head
 
 665                         die "Could not commit staged changes."
 
 669         record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
 
 671         require_clean_work_tree "rebase"
 
 681 git var GIT_COMMITTER_IDENT >/dev/null ||
 
 682         die "You need to set your committer info first"
 
 684 comment_for_reflog start
 
 686 if test ! -z "$switch_to"
 
 688         output git checkout "$switch_to" -- ||
 
 689                 die "Could not checkout $switch_to"
 
 692 orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
 
 693 mkdir "$state_dir" || die "Could not create temporary $state_dir"
 
 695 : > "$state_dir"/interactive || die "Could not mark as interactive"
 
 697 if test t = "$preserve_merges"
 
 699         if test -z "$rebase_root"
 
 701                 mkdir "$rewritten" &&
 
 702                 for c in $(git merge-base --all $orig_head $upstream)
 
 704                         echo $onto > "$rewritten"/$c ||
 
 705                                 die "Could not init rewritten commits"
 
 708                 mkdir "$rewritten" &&
 
 709                 echo $onto > "$rewritten"/root ||
 
 710                         die "Could not init rewritten commits"
 
 712         # No cherry-pick because our first pass is to determine
 
 713         # parents to rewrite and skipping dropped commits would
 
 714         # prematurely end our probe
 
 717         merges_option="--no-merges --cherry-pick"
 
 720 shorthead=$(git rev-parse --short $orig_head)
 
 721 shortonto=$(git rev-parse --short $onto)
 
 722 if test -z "$rebase_root"
 
 723         # this is now equivalent to ! -z "$upstream"
 
 725         shortupstream=$(git rev-parse --short $upstream)
 
 726         revisions=$upstream...$orig_head
 
 727         shortrevisions=$shortupstream..$shorthead
 
 729         revisions=$onto...$orig_head
 
 730         shortrevisions=$shorthead
 
 732 git rev-list $merges_option --pretty=oneline --abbrev-commit \
 
 733         --abbrev=7 --reverse --left-right --topo-order \
 
 736 while read -r shortsha1 rest
 
 738         if test t != "$preserve_merges"
 
 740                 printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
 
 742                 sha1=$(git rev-parse $shortsha1)
 
 743                 if test -z "$rebase_root"
 
 746                         for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
 
 748                                 if test -f "$rewritten"/$p
 
 756                 if test f = "$preserve"
 
 758                         touch "$rewritten"/$sha1
 
 759                         printf '%s\n' "pick $shortsha1 $rest" >> "$todo"
 
 764 # Watch for commits that been dropped by --cherry-pick
 
 765 if test t = "$preserve_merges"
 
 768         # Save all non-cherry-picked changes
 
 769         git rev-list $revisions --left-right --cherry-pick | \
 
 770                 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
 
 771         # Now all commits and note which ones are missing in
 
 772         # not-cherry-picks and hence being dropped
 
 773         git rev-list $revisions |
 
 776                 if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
 
 778                         # Use -f2 because if rev-list is telling us this commit is
 
 779                         # not worthwhile, we don't want to track its multiple heads,
 
 780                         # just the history of its first-parent for others that will
 
 781                         # be rebasing on top of it
 
 782                         git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
 
 783                         short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
 
 784                         sane_grep -v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
 
 790 test -s "$todo" || echo noop >> "$todo"
 
 791 test -n "$autosquash" && rearrange_squash "$todo"
 
 792 cat >> "$todo" << EOF
 
 794 # Rebase $shortrevisions onto $shortonto
 
 797 #  p, pick = use commit
 
 798 #  r, reword = use commit, but edit the commit message
 
 799 #  e, edit = use commit, but stop for amending
 
 800 #  s, squash = use commit, but meld into previous commit
 
 801 #  f, fixup = like "squash", but discard this commit's log message
 
 802 #  x, exec = run command (the rest of the line) using shell
 
 804 # If you remove a line here THAT COMMIT WILL BE LOST.
 
 805 # However, if you remove everything, the rebase will be aborted.
 
 809 has_action "$todo" ||
 
 810         die_abort "Nothing to do"
 
 812 cp "$todo" "$todo".backup
 
 813 git_editor "$todo" ||
 
 814         die_abort "Could not execute editor"
 
 816 has_action "$todo" ||
 
 817         die_abort "Nothing to do"
 
 819 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
 
 821 output git checkout $onto || die_abort "could not detach HEAD"
 
 822 git update-ref ORIG_HEAD $orig_head