git-rebase--interactive.shon commit tests: move test for rebase messages from t3400 to t3406 (c9581cc)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Johannes E. Schindelin
   4
   5# SHORT DESCRIPTION
   6#
   7# This script makes it easy to fix up commits in the middle of a series,
   8# and rearrange commits.
   9#
  10# The original idea comes from Eric W. Biederman, in
  11# http://article.gmane.org/gmane.comp.version-control.git/22407
  12#
  13# The file containing rebase commands, comments, and empty lines.
  14# This file is created by "git rebase -i" then edited by the user.  As
  15# the lines are processed, they are removed from the front of this
  16# file and written to the tail of $done.
  17todo="$state_dir"/git-rebase-todo
  18
  19# The rebase command lines that have already been processed.  A line
  20# is moved here when it is first handled, before any associated user
  21# actions.
  22done="$state_dir"/done
  23
  24# The commit message that is planned to be used for any changes that
  25# need to be committed following a user interaction.
  26msg="$state_dir"/message
  27
  28# The file into which is accumulated the suggested commit message for
  29# squash/fixup commands.  When the first of a series of squash/fixups
  30# is seen, the file is created and the commit message from the
  31# previous commit and from the first squash/fixup commit are written
  32# to it.  The commit message for each subsequent squash/fixup commit
  33# is appended to the file as it is processed.
  34#
  35# The first line of the file is of the form
  36#     # This is a combination of $count commits.
  37# where $count is the number of commits whose messages have been
  38# written to the file so far (including the initial "pick" commit).
  39# Each time that a commit message is processed, this line is read and
  40# updated.  It is deleted just before the combined commit is made.
  41squash_msg="$state_dir"/message-squash
  42
  43# If the current series of squash/fixups has not yet included a squash
  44# command, then this file exists and holds the commit message of the
  45# original "pick" commit.  (If the series ends without a "squash"
  46# command, then this can be used as the commit message of the combined
  47# commit without opening the editor.)
  48fixup_msg="$state_dir"/message-fixup
  49
  50# $rewritten is the name of a directory containing files for each
  51# commit that is reachable by at least one merge base of $head and
  52# $upstream. They are not necessarily rewritten, but their children
  53# might be.  This ensures that commits on merged, but otherwise
  54# unrelated side branches are left alone. (Think "X" in the man page's
  55# example.)
  56rewritten="$state_dir"/rewritten
  57
  58dropped="$state_dir"/dropped
  59
  60end="$state_dir"/end
  61msgnum="$state_dir"/msgnum
  62
  63# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
  64# GIT_AUTHOR_DATE that will be used for the commit that is currently
  65# being rebased.
  66author_script="$state_dir"/author-script
  67
  68# When an "edit" rebase command is being processed, the SHA1 of the
  69# commit to be edited is recorded in this file.  When "git rebase
  70# --continue" is executed, if there are any staged changes then they
  71# will be amended to the HEAD commit, but only provided the HEAD
  72# commit is still the commit to be edited.  When any other rebase
  73# command is processed, this file is deleted.
  74amend="$state_dir"/amend
  75
  76# For the post-rewrite hook, we make a list of rewritten commits and
  77# their new sha1s.  The rewritten-pending list keeps the sha1s of
  78# commits that have been processed, but not committed yet,
  79# e.g. because they are waiting for a 'squash' command.
  80rewritten_list="$state_dir"/rewritten-list
  81rewritten_pending="$state_dir"/rewritten-pending
  82
  83GIT_CHERRY_PICK_HELP="$resolvemsg"
  84export GIT_CHERRY_PICK_HELP
  85
  86comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
  87: ${comment_char:=#}
  88
  89warn () {
  90        printf '%s\n' "$*" >&2
  91}
  92
  93# Output the commit message for the specified commit.
  94commit_message () {
  95        git cat-file commit "$1" | sed "1,/^$/d"
  96}
  97
  98orig_reflog_action="$GIT_REFLOG_ACTION"
  99
 100comment_for_reflog () {
 101        case "$orig_reflog_action" in
 102        ''|rebase*)
 103                GIT_REFLOG_ACTION="rebase -i ($1)"
 104                export GIT_REFLOG_ACTION
 105                ;;
 106        esac
 107}
 108
 109last_count=
 110mark_action_done () {
 111        sed -e 1q < "$todo" >> "$done"
 112        sed -e 1d < "$todo" >> "$todo".new
 113        mv -f "$todo".new "$todo"
 114        new_count=$(git stripspace --strip-comments <"$done" | wc -l)
 115        echo $new_count >"$msgnum"
 116        total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
 117        echo $total >"$end"
 118        if test "$last_count" != "$new_count"
 119        then
 120                last_count=$new_count
 121                printf "Rebasing (%d/%d)\r" $new_count $total
 122                test -z "$verbose" || echo
 123        fi
 124}
 125
 126append_todo_help () {
 127        git stripspace --comment-lines >>"$todo" <<\EOF
 128
 129Commands:
 130 p, pick = use commit
 131 r, reword = use commit, but edit the commit message
 132 e, edit = use commit, but stop for amending
 133 s, squash = use commit, but meld into previous commit
 134 f, fixup = like "squash", but discard this commit's log message
 135 x, exec = run command (the rest of the line) using shell
 136
 137These lines can be re-ordered; they are executed from top to bottom.
 138
 139If you remove a line here THAT COMMIT WILL BE LOST.
 140EOF
 141}
 142
 143make_patch () {
 144        sha1_and_parents="$(git rev-list --parents -1 "$1")"
 145        case "$sha1_and_parents" in
 146        ?*' '?*' '?*)
 147                git diff --cc $sha1_and_parents
 148                ;;
 149        ?*' '?*)
 150                git diff-tree -p "$1^!"
 151                ;;
 152        *)
 153                echo "Root commit"
 154                ;;
 155        esac > "$state_dir"/patch
 156        test -f "$msg" ||
 157                commit_message "$1" > "$msg"
 158        test -f "$author_script" ||
 159                get_author_ident_from_commit "$1" > "$author_script"
 160}
 161
 162die_with_patch () {
 163        echo "$1" > "$state_dir"/stopped-sha
 164        make_patch "$1"
 165        git rerere
 166        die "$2"
 167}
 168
 169exit_with_patch () {
 170        echo "$1" > "$state_dir"/stopped-sha
 171        make_patch $1
 172        git rev-parse --verify HEAD > "$amend"
 173        warn "You can amend the commit now, with"
 174        warn
 175        warn "  git commit --amend"
 176        warn
 177        warn "Once you are satisfied with your changes, run"
 178        warn
 179        warn "  git rebase --continue"
 180        warn
 181        exit $2
 182}
 183
 184die_abort () {
 185        rm -rf "$state_dir"
 186        die "$1"
 187}
 188
 189has_action () {
 190        test -n "$(git stripspace --strip-comments <"$1")"
 191}
 192
 193is_empty_commit() {
 194        tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
 195                die "$1: not a commit that can be picked")
 196        ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
 197                ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
 198        test "$tree" = "$ptree"
 199}
 200
 201is_merge_commit()
 202{
 203        git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
 204}
 205
 206# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
 207# GIT_AUTHOR_DATE exported from the current environment.
 208do_with_author () {
 209        (
 210                export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 211                "$@"
 212        )
 213}
 214
 215git_sequence_editor () {
 216        if test -z "$GIT_SEQUENCE_EDITOR"
 217        then
 218                GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
 219                if [ -z "$GIT_SEQUENCE_EDITOR" ]
 220                then
 221                        GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
 222                fi
 223        fi
 224
 225        eval "$GIT_SEQUENCE_EDITOR" '"$@"'
 226}
 227
 228pick_one () {
 229        ff=--ff
 230
 231        case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
 232        case "$force_rebase" in '') ;; ?*) ff= ;; esac
 233        output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
 234
 235        if is_empty_commit "$sha1"
 236        then
 237                empty_args="--allow-empty"
 238        fi
 239
 240        test -d "$rewritten" &&
 241                pick_one_preserving_merges "$@" && return
 242        output git cherry-pick $empty_args $ff "$@"
 243}
 244
 245pick_one_preserving_merges () {
 246        fast_forward=t
 247        case "$1" in
 248        -n)
 249                fast_forward=f
 250                sha1=$2
 251                ;;
 252        *)
 253                sha1=$1
 254                ;;
 255        esac
 256        sha1=$(git rev-parse $sha1)
 257
 258        if test -f "$state_dir"/current-commit
 259        then
 260                if test "$fast_forward" = t
 261                then
 262                        while read current_commit
 263                        do
 264                                git rev-parse HEAD > "$rewritten"/$current_commit
 265                        done <"$state_dir"/current-commit
 266                        rm "$state_dir"/current-commit ||
 267                        die "Cannot write current commit's replacement sha1"
 268                fi
 269        fi
 270
 271        echo $sha1 >> "$state_dir"/current-commit
 272
 273        # rewrite parents; if none were rewritten, we can fast-forward.
 274        new_parents=
 275        pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
 276        if test "$pend" = " "
 277        then
 278                pend=" root"
 279        fi
 280        while [ "$pend" != "" ]
 281        do
 282                p=$(expr "$pend" : ' \([^ ]*\)')
 283                pend="${pend# $p}"
 284
 285                if test -f "$rewritten"/$p
 286                then
 287                        new_p=$(cat "$rewritten"/$p)
 288
 289                        # If the todo reordered commits, and our parent is marked for
 290                        # rewriting, but hasn't been gotten to yet, assume the user meant to
 291                        # drop it on top of the current HEAD
 292                        if test -z "$new_p"
 293                        then
 294                                new_p=$(git rev-parse HEAD)
 295                        fi
 296
 297                        test $p != $new_p && fast_forward=f
 298                        case "$new_parents" in
 299                        *$new_p*)
 300                                ;; # do nothing; that parent is already there
 301                        *)
 302                                new_parents="$new_parents $new_p"
 303                                ;;
 304                        esac
 305                else
 306                        if test -f "$dropped"/$p
 307                        then
 308                                fast_forward=f
 309                                replacement="$(cat "$dropped"/$p)"
 310                                test -z "$replacement" && replacement=root
 311                                pend=" $replacement$pend"
 312                        else
 313                                new_parents="$new_parents $p"
 314                        fi
 315                fi
 316        done
 317        case $fast_forward in
 318        t)
 319                output warn "Fast-forward to $sha1"
 320                output git reset --hard $sha1 ||
 321                        die "Cannot fast-forward to $sha1"
 322                ;;
 323        f)
 324                first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 325
 326                if [ "$1" != "-n" ]
 327                then
 328                        # detach HEAD to current parent
 329                        output git checkout $first_parent 2> /dev/null ||
 330                                die "Cannot move HEAD to $first_parent"
 331                fi
 332
 333                case "$new_parents" in
 334                ' '*' '*)
 335                        test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 336
 337                        # redo merge
 338                        author_script_content=$(get_author_ident_from_commit $sha1)
 339                        eval "$author_script_content"
 340                        msg_content="$(commit_message $sha1)"
 341                        # No point in merging the first parent, that's HEAD
 342                        new_parents=${new_parents# $first_parent}
 343                        if ! do_with_author output \
 344                                git merge --no-ff ${strategy:+-s $strategy} -m \
 345                                        "$msg_content" $new_parents
 346                        then
 347                                printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
 348                                die_with_patch $sha1 "Error redoing merge $sha1"
 349                        fi
 350                        echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
 351                        ;;
 352                *)
 353                        output git cherry-pick "$@" ||
 354                                die_with_patch $sha1 "Could not pick $sha1"
 355                        ;;
 356                esac
 357                ;;
 358        esac
 359}
 360
 361nth_string () {
 362        case "$1" in
 363        *1[0-9]|*[04-9]) echo "$1"th;;
 364        *1) echo "$1"st;;
 365        *2) echo "$1"nd;;
 366        *3) echo "$1"rd;;
 367        esac
 368}
 369
 370update_squash_messages () {
 371        if test -f "$squash_msg"; then
 372                mv "$squash_msg" "$squash_msg".bak || exit
 373                count=$(($(sed -n \
 374                        -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
 375                        -e "q" < "$squash_msg".bak)+1))
 376                {
 377                        printf '%s\n' "$comment_char This is a combination of $count commits."
 378                        sed -e 1d -e '2,/^./{
 379                                /^$/d
 380                        }' <"$squash_msg".bak
 381                } >"$squash_msg"
 382        else
 383                commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
 384                count=2
 385                {
 386                        printf '%s\n' "$comment_char This is a combination of 2 commits."
 387                        printf '%s\n' "$comment_char The first commit's message is:"
 388                        echo
 389                        cat "$fixup_msg"
 390                } >"$squash_msg"
 391        fi
 392        case $1 in
 393        squash)
 394                rm -f "$fixup_msg"
 395                echo
 396                printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
 397                echo
 398                commit_message $2
 399                ;;
 400        fixup)
 401                echo
 402                printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
 403                echo
 404                # Change the space after the comment character to TAB:
 405                commit_message $2 | git stripspace --comment-lines | sed -e 's/ /       /'
 406                ;;
 407        esac >>"$squash_msg"
 408}
 409
 410peek_next_command () {
 411        git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
 412}
 413
 414# A squash/fixup has failed.  Prepare the long version of the squash
 415# commit message, then die_with_patch.  This code path requires the
 416# user to edit the combined commit message for all commits that have
 417# been squashed/fixedup so far.  So also erase the old squash
 418# messages, effectively causing the combined commit to be used as the
 419# new basis for any further squash/fixups.  Args: sha1 rest
 420die_failed_squash() {
 421        mv "$squash_msg" "$msg" || exit
 422        rm -f "$fixup_msg"
 423        cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
 424        warn
 425        warn "Could not apply $1... $2"
 426        die_with_patch $1 ""
 427}
 428
 429flush_rewritten_pending() {
 430        test -s "$rewritten_pending" || return
 431        newsha1="$(git rev-parse HEAD^0)"
 432        sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
 433        rm -f "$rewritten_pending"
 434}
 435
 436record_in_rewritten() {
 437        oldsha1="$(git rev-parse $1)"
 438        echo "$oldsha1" >> "$rewritten_pending"
 439
 440        case "$(peek_next_command)" in
 441        squash|s|fixup|f)
 442                ;;
 443        *)
 444                flush_rewritten_pending
 445                ;;
 446        esac
 447}
 448
 449do_pick () {
 450        if test "$(git rev-parse HEAD)" = "$squash_onto"
 451        then
 452                # Set the correct commit message and author info on the
 453                # sentinel root before cherry-picking the original changes
 454                # without committing (-n).  Finally, update the sentinel again
 455                # to include these changes.  If the cherry-pick results in a
 456                # conflict, this means our behaviour is similar to a standard
 457                # failed cherry-pick during rebase, with a dirty index to
 458                # resolve before manually running git commit --amend then git
 459                # rebase --continue.
 460                git commit --allow-empty --allow-empty-message --amend \
 461                           --no-post-rewrite -n -q -C $1 &&
 462                        pick_one -n $1 &&
 463                        git commit --allow-empty --allow-empty-message \
 464                                   --amend --no-post-rewrite -n -q -C $1 ||
 465                        die_with_patch $1 "Could not apply $1... $2"
 466        else
 467                pick_one $1 ||
 468                        die_with_patch $1 "Could not apply $1... $2"
 469        fi
 470}
 471
 472do_next () {
 473        rm -f "$msg" "$author_script" "$amend" || exit
 474        read -r command sha1 rest < "$todo"
 475        case "$command" in
 476        "$comment_char"*|''|noop)
 477                mark_action_done
 478                ;;
 479        pick|p)
 480                comment_for_reflog pick
 481
 482                mark_action_done
 483                do_pick $sha1 "$rest"
 484                record_in_rewritten $sha1
 485                ;;
 486        reword|r)
 487                comment_for_reflog reword
 488
 489                mark_action_done
 490                do_pick $sha1 "$rest"
 491                git commit --amend --no-post-rewrite || {
 492                        warn "Could not amend commit after successfully picking $sha1... $rest"
 493                        warn "This is most likely due to an empty commit message, or the pre-commit hook"
 494                        warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
 495                        warn "you are able to reword the commit."
 496                        exit_with_patch $sha1 1
 497                }
 498                record_in_rewritten $sha1
 499                ;;
 500        edit|e)
 501                comment_for_reflog edit
 502
 503                mark_action_done
 504                do_pick $sha1 "$rest"
 505                warn "Stopped at $sha1... $rest"
 506                exit_with_patch $sha1 0
 507                ;;
 508        squash|s|fixup|f)
 509                case "$command" in
 510                squash|s)
 511                        squash_style=squash
 512                        ;;
 513                fixup|f)
 514                        squash_style=fixup
 515                        ;;
 516                esac
 517                comment_for_reflog $squash_style
 518
 519                test -f "$done" && has_action "$done" ||
 520                        die "Cannot '$squash_style' without a previous commit"
 521
 522                mark_action_done
 523                update_squash_messages $squash_style $sha1
 524                author_script_content=$(get_author_ident_from_commit HEAD)
 525                echo "$author_script_content" > "$author_script"
 526                eval "$author_script_content"
 527                if ! pick_one -n $sha1
 528                then
 529                        git rev-parse --verify HEAD >"$amend"
 530                        die_failed_squash $sha1 "$rest"
 531                fi
 532                case "$(peek_next_command)" in
 533                squash|s|fixup|f)
 534                        # This is an intermediate commit; its message will only be
 535                        # used in case of trouble.  So use the long version:
 536                        do_with_author output git commit --amend --no-verify -F "$squash_msg" ||
 537                                die_failed_squash $sha1 "$rest"
 538                        ;;
 539                *)
 540                        # This is the final command of this squash/fixup group
 541                        if test -f "$fixup_msg"
 542                        then
 543                                do_with_author git commit --amend --no-verify -F "$fixup_msg" ||
 544                                        die_failed_squash $sha1 "$rest"
 545                        else
 546                                cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
 547                                rm -f "$GIT_DIR"/MERGE_MSG
 548                                do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e ||
 549                                        die_failed_squash $sha1 "$rest"
 550                        fi
 551                        rm -f "$squash_msg" "$fixup_msg"
 552                        ;;
 553                esac
 554                record_in_rewritten $sha1
 555                ;;
 556        x|"exec")
 557                read -r command rest < "$todo"
 558                mark_action_done
 559                printf 'Executing: %s\n' "$rest"
 560                # "exec" command doesn't take a sha1 in the todo-list.
 561                # => can't just use $sha1 here.
 562                git rev-parse --verify HEAD > "$state_dir"/stopped-sha
 563                ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
 564                status=$?
 565                # Run in subshell because require_clean_work_tree can die.
 566                dirty=f
 567                (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
 568                if test "$status" -ne 0
 569                then
 570                        warn "Execution failed: $rest"
 571                        test "$dirty" = f ||
 572                        warn "and made changes to the index and/or the working tree"
 573
 574                        warn "You can fix the problem, and then run"
 575                        warn
 576                        warn "  git rebase --continue"
 577                        warn
 578                        if test $status -eq 127         # command not found
 579                        then
 580                                status=1
 581                        fi
 582                        exit "$status"
 583                elif test "$dirty" = t
 584                then
 585                        warn "Execution succeeded: $rest"
 586                        warn "but left changes to the index and/or the working tree"
 587                        warn "Commit or stash your changes, and then run"
 588                        warn
 589                        warn "  git rebase --continue"
 590                        warn
 591                        exit 1
 592                fi
 593                ;;
 594        *)
 595                warn "Unknown command: $command $sha1 $rest"
 596                fixtodo="Please fix this using 'git rebase --edit-todo'."
 597                if git rev-parse --verify -q "$sha1" >/dev/null
 598                then
 599                        die_with_patch $sha1 "$fixtodo"
 600                else
 601                        die "$fixtodo"
 602                fi
 603                ;;
 604        esac
 605        test -s "$todo" && return
 606
 607        comment_for_reflog finish &&
 608        newhead=$(git rev-parse HEAD) &&
 609        case $head_name in
 610        refs/*)
 611                message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
 612                git update-ref -m "$message" $head_name $newhead $orig_head &&
 613                git symbolic-ref \
 614                  -m "$GIT_REFLOG_ACTION: returning to $head_name" \
 615                  HEAD $head_name
 616                ;;
 617        esac && {
 618                test ! -f "$state_dir"/verbose ||
 619                        git diff-tree --stat $orig_head..HEAD
 620        } &&
 621        {
 622                test -s "$rewritten_list" &&
 623                git notes copy --for-rewrite=rebase < "$rewritten_list" ||
 624                true # we don't care if this copying failed
 625        } &&
 626        if test -x "$GIT_DIR"/hooks/post-rewrite &&
 627                test -s "$rewritten_list"; then
 628                "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
 629                true # we don't care if this hook failed
 630        fi &&
 631        rm -rf "$state_dir" &&
 632        git gc --auto &&
 633        warn "Successfully rebased and updated $head_name."
 634
 635        exit
 636}
 637
 638do_rest () {
 639        while :
 640        do
 641                do_next
 642        done
 643}
 644
 645# skip picking commits whose parents are unchanged
 646skip_unnecessary_picks () {
 647        fd=3
 648        while read -r command rest
 649        do
 650                # fd=3 means we skip the command
 651                case "$fd,$command" in
 652                3,pick|3,p)
 653                        # pick a commit whose parent is current $onto -> skip
 654                        sha1=${rest%% *}
 655                        case "$(git rev-parse --verify --quiet "$sha1"^)" in
 656                        "$onto"*)
 657                                onto=$sha1
 658                                ;;
 659                        *)
 660                                fd=1
 661                                ;;
 662                        esac
 663                        ;;
 664                3,#*|3,)
 665                        # copy comments
 666                        ;;
 667                *)
 668                        fd=1
 669                        ;;
 670                esac
 671                printf '%s\n' "$command${rest:+ }$rest" >&$fd
 672        done <"$todo" >"$todo.new" 3>>"$done" &&
 673        mv -f "$todo".new "$todo" &&
 674        case "$(peek_next_command)" in
 675        squash|s|fixup|f)
 676                record_in_rewritten "$onto"
 677                ;;
 678        esac ||
 679        die "Could not skip unnecessary pick commands"
 680}
 681
 682# Rearrange the todo list that has both "pick sha1 msg" and
 683# "pick sha1 fixup!/squash! msg" appears in it so that the latter
 684# comes immediately after the former, and change "pick" to
 685# "fixup"/"squash".
 686rearrange_squash () {
 687        # extract fixup!/squash! lines and resolve any referenced sha1's
 688        while read -r pick sha1 message
 689        do
 690                case "$message" in
 691                "squash! "*|"fixup! "*)
 692                        action="${message%%!*}"
 693                        rest="${message#*! }"
 694                        echo "$sha1 $action $rest"
 695                        # if it's a single word, try to resolve to a full sha1 and
 696                        # emit a second copy. This allows us to match on both message
 697                        # and on sha1 prefix
 698                        if test "${rest#* }" = "$rest"; then
 699                                fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
 700                                if test -n "$fullsha"; then
 701                                        # prefix the action to uniquely identify this line as
 702                                        # intended for full sha1 match
 703                                        echo "$sha1 +$action $fullsha"
 704                                fi
 705                        fi
 706                esac
 707        done >"$1.sq" <"$1"
 708        test -s "$1.sq" || return
 709
 710        used=
 711        while read -r pick sha1 message
 712        do
 713                case " $used" in
 714                *" $sha1 "*) continue ;;
 715                esac
 716                printf '%s\n' "$pick $sha1 $message"
 717                used="$used$sha1 "
 718                while read -r squash action msg_content
 719                do
 720                        case " $used" in
 721                        *" $squash "*) continue ;;
 722                        esac
 723                        emit=0
 724                        case "$action" in
 725                        +*)
 726                                action="${action#+}"
 727                                # full sha1 prefix test
 728                                case "$msg_content" in "$sha1"*) emit=1;; esac ;;
 729                        *)
 730                                # message prefix test
 731                                case "$message" in "$msg_content"*) emit=1;; esac ;;
 732                        esac
 733                        if test $emit = 1; then
 734                                printf '%s\n' "$action $squash $action! $msg_content"
 735                                used="$used$squash "
 736                        fi
 737                done <"$1.sq"
 738        done >"$1.rearranged" <"$1"
 739        cat "$1.rearranged" >"$1"
 740        rm -f "$1.sq" "$1.rearranged"
 741}
 742
 743# Add commands after a pick or after a squash/fixup serie
 744# in the todo list.
 745add_exec_commands () {
 746        {
 747                first=t
 748                while read -r insn rest
 749                do
 750                        case $insn in
 751                        pick)
 752                                test -n "$first" ||
 753                                printf "%s" "$cmd"
 754                                ;;
 755                        esac
 756                        printf "%s %s\n" "$insn" "$rest"
 757                        first=
 758                done
 759                printf "%s" "$cmd"
 760        } <"$1" >"$1.new" &&
 761        mv "$1.new" "$1"
 762}
 763
 764case "$action" in
 765continue)
 766        # do we have anything to commit?
 767        if git diff-index --cached --quiet HEAD --
 768        then
 769                : Nothing to commit -- skip this
 770        else
 771                if ! test -f "$author_script"
 772                then
 773                        die "You have staged changes in your working tree. If these changes are meant to be
 774squashed into the previous commit, run:
 775
 776  git commit --amend
 777
 778If they are meant to go into a new commit, run:
 779
 780  git commit
 781
 782In both case, once you're done, continue with:
 783
 784  git rebase --continue
 785"
 786                fi
 787                . "$author_script" ||
 788                        die "Error trying to find the author identity to amend commit"
 789                if test -f "$amend"
 790                then
 791                        current_head=$(git rev-parse --verify HEAD)
 792                        test "$current_head" = $(cat "$amend") ||
 793                        die "\
 794You have uncommitted changes in your working tree. Please, commit them
 795first and then run 'git rebase --continue' again."
 796                        do_with_author git commit --amend --no-verify -F "$msg" -e ||
 797                                die "Could not commit staged changes."
 798                else
 799                        do_with_author git commit --no-verify -F "$msg" -e ||
 800                                die "Could not commit staged changes."
 801                fi
 802        fi
 803
 804        record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
 805
 806        require_clean_work_tree "rebase"
 807        do_rest
 808        ;;
 809skip)
 810        git rerere clear
 811
 812        do_rest
 813        ;;
 814edit-todo)
 815        git stripspace --strip-comments <"$todo" >"$todo".new
 816        mv -f "$todo".new "$todo"
 817        append_todo_help
 818        git stripspace --comment-lines >>"$todo" <<\EOF
 819
 820You are editing the todo file of an ongoing interactive rebase.
 821To continue rebase after editing, run:
 822    git rebase --continue
 823
 824EOF
 825
 826        git_sequence_editor "$todo" ||
 827                die "Could not execute editor"
 828
 829        exit
 830        ;;
 831esac
 832
 833git var GIT_COMMITTER_IDENT >/dev/null ||
 834        die "You need to set your committer info first"
 835
 836comment_for_reflog start
 837
 838if test ! -z "$switch_to"
 839then
 840        output git checkout "$switch_to" -- ||
 841                die "Could not checkout $switch_to"
 842fi
 843
 844orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
 845mkdir "$state_dir" || die "Could not create temporary $state_dir"
 846
 847: > "$state_dir"/interactive || die "Could not mark as interactive"
 848write_basic_state
 849if test t = "$preserve_merges"
 850then
 851        if test -z "$rebase_root"
 852        then
 853                mkdir "$rewritten" &&
 854                for c in $(git merge-base --all $orig_head $upstream)
 855                do
 856                        echo $onto > "$rewritten"/$c ||
 857                                die "Could not init rewritten commits"
 858                done
 859        else
 860                mkdir "$rewritten" &&
 861                echo $onto > "$rewritten"/root ||
 862                        die "Could not init rewritten commits"
 863        fi
 864        # No cherry-pick because our first pass is to determine
 865        # parents to rewrite and skipping dropped commits would
 866        # prematurely end our probe
 867        merges_option=
 868else
 869        merges_option="--no-merges --cherry-pick"
 870fi
 871
 872shorthead=$(git rev-parse --short $orig_head)
 873shortonto=$(git rev-parse --short $onto)
 874if test -z "$rebase_root"
 875        # this is now equivalent to ! -z "$upstream"
 876then
 877        shortupstream=$(git rev-parse --short $upstream)
 878        revisions=$upstream...$orig_head
 879        shortrevisions=$shortupstream..$shorthead
 880else
 881        revisions=$onto...$orig_head
 882        shortrevisions=$shorthead
 883fi
 884git rev-list $merges_option --pretty=oneline --abbrev-commit \
 885        --abbrev=7 --reverse --left-right --topo-order \
 886        $revisions | \
 887        sed -n "s/^>//p" |
 888while read -r shortsha1 rest
 889do
 890
 891        if test -z "$keep_empty" && is_empty_commit $shortsha1 && ! is_merge_commit $shortsha1
 892        then
 893                comment_out="$comment_char "
 894        else
 895                comment_out=
 896        fi
 897
 898        if test t != "$preserve_merges"
 899        then
 900                printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
 901        else
 902                sha1=$(git rev-parse $shortsha1)
 903                if test -z "$rebase_root"
 904                then
 905                        preserve=t
 906                        for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
 907                        do
 908                                if test -f "$rewritten"/$p
 909                                then
 910                                        preserve=f
 911                                fi
 912                        done
 913                else
 914                        preserve=f
 915                fi
 916                if test f = "$preserve"
 917                then
 918                        touch "$rewritten"/$sha1
 919                        printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
 920                fi
 921        fi
 922done
 923
 924# Watch for commits that been dropped by --cherry-pick
 925if test t = "$preserve_merges"
 926then
 927        mkdir "$dropped"
 928        # Save all non-cherry-picked changes
 929        git rev-list $revisions --left-right --cherry-pick | \
 930                sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
 931        # Now all commits and note which ones are missing in
 932        # not-cherry-picks and hence being dropped
 933        git rev-list $revisions |
 934        while read rev
 935        do
 936                if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
 937                then
 938                        # Use -f2 because if rev-list is telling us this commit is
 939                        # not worthwhile, we don't want to track its multiple heads,
 940                        # just the history of its first-parent for others that will
 941                        # be rebasing on top of it
 942                        git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
 943                        short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
 944                        sane_grep -v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
 945                        rm "$rewritten"/$rev
 946                fi
 947        done
 948fi
 949
 950test -s "$todo" || echo noop >> "$todo"
 951test -n "$autosquash" && rearrange_squash "$todo"
 952test -n "$cmd" && add_exec_commands "$todo"
 953
 954cat >>"$todo" <<EOF
 955
 956$comment_char Rebase $shortrevisions onto $shortonto
 957EOF
 958append_todo_help
 959git stripspace --comment-lines >>"$todo" <<\EOF
 960
 961However, if you remove everything, the rebase will be aborted.
 962
 963EOF
 964
 965if test -z "$keep_empty"
 966then
 967        printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
 968fi
 969
 970
 971has_action "$todo" ||
 972        die_abort "Nothing to do"
 973
 974cp "$todo" "$todo".backup
 975git_sequence_editor "$todo" ||
 976        die_abort "Could not execute editor"
 977
 978has_action "$todo" ||
 979        die_abort "Nothing to do"
 980
 981test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
 982
 983output git checkout $onto || die_abort "could not detach HEAD"
 984git update-ref ORIG_HEAD $orig_head
 985do_rest