e132f070606ab3586a43ad268583b728ca737b63
   1# This shell script fragment is sourced by git-rebase to implement
   2# its interactive mode.  "git rebase --interactive" makes it easy
   3# to fix up commits in the middle of a series and rearrange commits.
   4#
   5# Copyright (c) 2006 Johannes E. Schindelin
   6#
   7# The original idea comes from Eric W. Biederman, in
   8# http://article.gmane.org/gmane.comp.version-control.git/22407
   9#
  10# The file containing rebase commands, comments, and empty lines.
  11# This file is created by "git rebase -i" then edited by the user.  As
  12# the lines are processed, they are removed from the front of this
  13# file and written to the tail of $done.
  14todo="$state_dir"/git-rebase-todo
  15
  16# The rebase command lines that have already been processed.  A line
  17# is moved here when it is first handled, before any associated user
  18# actions.
  19done="$state_dir"/done
  20
  21# The commit message that is planned to be used for any changes that
  22# need to be committed following a user interaction.
  23msg="$state_dir"/message
  24
  25# The file into which is accumulated the suggested commit message for
  26# squash/fixup commands.  When the first of a series of squash/fixups
  27# is seen, the file is created and the commit message from the
  28# previous commit and from the first squash/fixup commit are written
  29# to it.  The commit message for each subsequent squash/fixup commit
  30# is appended to the file as it is processed.
  31#
  32# The first line of the file is of the form
  33#     # This is a combination of $count commits.
  34# where $count is the number of commits whose messages have been
  35# written to the file so far (including the initial "pick" commit).
  36# Each time that a commit message is processed, this line is read and
  37# updated.  It is deleted just before the combined commit is made.
  38squash_msg="$state_dir"/message-squash
  39
  40# If the current series of squash/fixups has not yet included a squash
  41# command, then this file exists and holds the commit message of the
  42# original "pick" commit.  (If the series ends without a "squash"
  43# command, then this can be used as the commit message of the combined
  44# commit without opening the editor.)
  45fixup_msg="$state_dir"/message-fixup
  46
  47# $rewritten is the name of a directory containing files for each
  48# commit that is reachable by at least one merge base of $head and
  49# $upstream. They are not necessarily rewritten, but their children
  50# might be.  This ensures that commits on merged, but otherwise
  51# unrelated side branches are left alone. (Think "X" in the man page's
  52# example.)
  53rewritten="$state_dir"/rewritten
  54
  55dropped="$state_dir"/dropped
  56
  57end="$state_dir"/end
  58msgnum="$state_dir"/msgnum
  59
  60# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
  61# GIT_AUTHOR_DATE that will be used for the commit that is currently
  62# being rebased.
  63author_script="$state_dir"/author-script
  64
  65# When an "edit" rebase command is being processed, the SHA1 of the
  66# commit to be edited is recorded in this file.  When "git rebase
  67# --continue" is executed, if there are any staged changes then they
  68# will be amended to the HEAD commit, but only provided the HEAD
  69# commit is still the commit to be edited.  When any other rebase
  70# command is processed, this file is deleted.
  71amend="$state_dir"/amend
  72
  73# For the post-rewrite hook, we make a list of rewritten commits and
  74# their new sha1s.  The rewritten-pending list keeps the sha1s of
  75# commits that have been processed, but not committed yet,
  76# e.g. because they are waiting for a 'squash' command.
  77rewritten_list="$state_dir"/rewritten-list
  78rewritten_pending="$state_dir"/rewritten-pending
  79
  80# Work around Git for Windows' Bash whose "read" does not strip CRLF
  81# and leaves CR at the end instead.
  82cr=$(printf "\015")
  83
  84strategy_args=${strategy:+--strategy=$strategy}
  85test -n "$strategy_opts" &&
  86eval '
  87        for strategy_opt in '"$strategy_opts"'
  88        do
  89                strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
  90        done
  91'
  92
  93GIT_CHERRY_PICK_HELP="$resolvemsg"
  94export GIT_CHERRY_PICK_HELP
  95
  96comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
  97: ${comment_char:=#}
  98
  99warn () {
 100        printf '%s\n' "$*" >&2
 101}
 102
 103# Output the commit message for the specified commit.
 104commit_message () {
 105        git cat-file commit "$1" | sed "1,/^$/d"
 106}
 107
 108orig_reflog_action="$GIT_REFLOG_ACTION"
 109
 110comment_for_reflog () {
 111        case "$orig_reflog_action" in
 112        ''|rebase*)
 113                GIT_REFLOG_ACTION="rebase -i ($1)"
 114                export GIT_REFLOG_ACTION
 115                ;;
 116        esac
 117}
 118
 119last_count=
 120mark_action_done () {
 121        sed -e 1q < "$todo" >> "$done"
 122        sed -e 1d < "$todo" >> "$todo".new
 123        mv -f "$todo".new "$todo"
 124        new_count=$(git stripspace --strip-comments <"$done" | wc -l)
 125        echo $new_count >"$msgnum"
 126        total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
 127        echo $total >"$end"
 128        if test "$last_count" != "$new_count"
 129        then
 130                last_count=$new_count
 131                eval_gettext "Rebasing (\$new_count/\$total)"; printf "\r"
 132                test -z "$verbose" || echo
 133        fi
 134}
 135
 136# Put the last action marked done at the beginning of the todo list
 137# again. If there has not been an action marked done yet, leave the list of
 138# items on the todo list unchanged.
 139reschedule_last_action () {
 140        tail -n 1 "$done" | cat - "$todo" >"$todo".new
 141        sed -e \$d <"$done" >"$done".new
 142        mv -f "$todo".new "$todo"
 143        mv -f "$done".new "$done"
 144}
 145
 146append_todo_help () {
 147        gettext "
 148Commands:
 149 p, pick = use commit
 150 r, reword = use commit, but edit the commit message
 151 e, edit = use commit, but stop for amending
 152 s, squash = use commit, but meld into previous commit
 153 f, fixup = like \"squash\", but discard this commit's log message
 154 x, exec = run command (the rest of the line) using shell
 155 d, drop = remove commit
 156
 157These lines can be re-ordered; they are executed from top to bottom.
 158" | git stripspace --comment-lines >>"$todo"
 159
 160        if test $(get_missing_commit_check_level) = error
 161        then
 162                gettext "
 163Do not remove any line. Use 'drop' explicitly to remove a commit.
 164" | git stripspace --comment-lines >>"$todo"
 165        else
 166                gettext "
 167If you remove a line here THAT COMMIT WILL BE LOST.
 168" | git stripspace --comment-lines >>"$todo"
 169        fi
 170}
 171
 172make_patch () {
 173        sha1_and_parents="$(git rev-list --parents -1 "$1")"
 174        case "$sha1_and_parents" in
 175        ?*' '?*' '?*)
 176                git diff --cc $sha1_and_parents
 177                ;;
 178        ?*' '?*)
 179                git diff-tree -p "$1^!"
 180                ;;
 181        *)
 182                echo "Root commit"
 183                ;;
 184        esac > "$state_dir"/patch
 185        test -f "$msg" ||
 186                commit_message "$1" > "$msg"
 187        test -f "$author_script" ||
 188                get_author_ident_from_commit "$1" > "$author_script"
 189}
 190
 191die_with_patch () {
 192        echo "$1" > "$state_dir"/stopped-sha
 193        make_patch "$1"
 194        git rerere
 195        die "$2"
 196}
 197
 198exit_with_patch () {
 199        echo "$1" > "$state_dir"/stopped-sha
 200        make_patch $1
 201        git rev-parse --verify HEAD > "$amend"
 202        gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
 203        warn "$(eval_gettext "\
 204You can amend the commit now, with
 205
 206        git commit --amend \$gpg_sign_opt_quoted
 207
 208Once you are satisfied with your changes, run
 209
 210        git rebase --continue")"
 211        warn
 212        exit $2
 213}
 214
 215die_abort () {
 216        rm -rf "$state_dir"
 217        die "$1"
 218}
 219
 220has_action () {
 221        test -n "$(git stripspace --strip-comments <"$1")"
 222}
 223
 224is_empty_commit() {
 225        tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null) || {
 226                sha1=$1
 227                die "$(eval_gettext "\$sha1: not a commit that can be picked")"
 228        }
 229        ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null) ||
 230                ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
 231        test "$tree" = "$ptree"
 232}
 233
 234is_merge_commit()
 235{
 236        git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
 237}
 238
 239# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
 240# GIT_AUTHOR_DATE exported from the current environment.
 241do_with_author () {
 242        (
 243                export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
 244                "$@"
 245        )
 246}
 247
 248git_sequence_editor () {
 249        if test -z "$GIT_SEQUENCE_EDITOR"
 250        then
 251                GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
 252                if [ -z "$GIT_SEQUENCE_EDITOR" ]
 253                then
 254                        GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
 255                fi
 256        fi
 257
 258        eval "$GIT_SEQUENCE_EDITOR" '"$@"'
 259}
 260
 261pick_one () {
 262        ff=--ff
 263
 264        case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
 265        case "$force_rebase" in '') ;; ?*) ff= ;; esac
 266        output git rev-parse --verify $sha1 || die "$(eval_gettext "Invalid commit name: \$sha1")"
 267
 268        if is_empty_commit "$sha1"
 269        then
 270                empty_args="--allow-empty"
 271        fi
 272
 273        test -d "$rewritten" &&
 274                pick_one_preserving_merges "$@" && return
 275        output eval git cherry-pick \
 276                        ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
 277                        "$strategy_args" $empty_args $ff "$@"
 278
 279        # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule
 280        # previous task so this commit is not lost.
 281        ret=$?
 282        case "$ret" in [01]) ;; *) reschedule_last_action ;; esac
 283        return $ret
 284}
 285
 286pick_one_preserving_merges () {
 287        fast_forward=t
 288        case "$1" in
 289        -n)
 290                fast_forward=f
 291                sha1=$2
 292                ;;
 293        *)
 294                sha1=$1
 295                ;;
 296        esac
 297        sha1=$(git rev-parse $sha1)
 298
 299        if test -f "$state_dir"/current-commit
 300        then
 301                if test "$fast_forward" = t
 302                then
 303                        while read current_commit
 304                        do
 305                                git rev-parse HEAD > "$rewritten"/$current_commit
 306                        done <"$state_dir"/current-commit
 307                        rm "$state_dir"/current-commit ||
 308                                die "$(gettext "Cannot write current commit's replacement sha1")"
 309                fi
 310        fi
 311
 312        echo $sha1 >> "$state_dir"/current-commit
 313
 314        # rewrite parents; if none were rewritten, we can fast-forward.
 315        new_parents=
 316        pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
 317        if test "$pend" = " "
 318        then
 319                pend=" root"
 320        fi
 321        while [ "$pend" != "" ]
 322        do
 323                p=$(expr "$pend" : ' \([^ ]*\)')
 324                pend="${pend# $p}"
 325
 326                if test -f "$rewritten"/$p
 327                then
 328                        new_p=$(cat "$rewritten"/$p)
 329
 330                        # If the todo reordered commits, and our parent is marked for
 331                        # rewriting, but hasn't been gotten to yet, assume the user meant to
 332                        # drop it on top of the current HEAD
 333                        if test -z "$new_p"
 334                        then
 335                                new_p=$(git rev-parse HEAD)
 336                        fi
 337
 338                        test $p != $new_p && fast_forward=f
 339                        case "$new_parents" in
 340                        *$new_p*)
 341                                ;; # do nothing; that parent is already there
 342                        *)
 343                                new_parents="$new_parents $new_p"
 344                                ;;
 345                        esac
 346                else
 347                        if test -f "$dropped"/$p
 348                        then
 349                                fast_forward=f
 350                                replacement="$(cat "$dropped"/$p)"
 351                                test -z "$replacement" && replacement=root
 352                                pend=" $replacement$pend"
 353                        else
 354                                new_parents="$new_parents $p"
 355                        fi
 356                fi
 357        done
 358        case $fast_forward in
 359        t)
 360                output warn "$(eval_gettext "Fast-forward to \$sha1")"
 361                output git reset --hard $sha1 ||
 362                        die "$(eval_gettext "Cannot fast-forward to \$sha1")"
 363                ;;
 364        f)
 365                first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 366
 367                if [ "$1" != "-n" ]
 368                then
 369                        # detach HEAD to current parent
 370                        output git checkout $first_parent 2> /dev/null ||
 371                                die "$(eval_gettext "Cannot move HEAD to \$first_parent")"
 372                fi
 373
 374                case "$new_parents" in
 375                ' '*' '*)
 376                        test "a$1" = a-n && die "$(eval_gettext "Refusing to squash a merge: \$sha1")"
 377
 378                        # redo merge
 379                        author_script_content=$(get_author_ident_from_commit $sha1)
 380                        eval "$author_script_content"
 381                        msg_content="$(commit_message $sha1)"
 382                        # No point in merging the first parent, that's HEAD
 383                        new_parents=${new_parents# $first_parent}
 384                        merge_args="--no-log --no-ff"
 385                        if ! do_with_author output eval \
 386                        'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
 387                                $merge_args $strategy_args -m "$msg_content" $new_parents'
 388                        then
 389                                printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
 390                                die_with_patch $sha1 "$(eval_gettext "Error redoing merge \$sha1")"
 391                        fi
 392                        echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
 393                        ;;
 394                *)
 395                        output eval git cherry-pick \
 396                                ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
 397                                "$strategy_args" "$@" ||
 398                                die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
 399                        ;;
 400                esac
 401                ;;
 402        esac
 403}
 404
 405nth_string () {
 406        case "$1" in
 407        *1[0-9]|*[04-9]) echo "$1"th;;
 408        *1) echo "$1"st;;
 409        *2) echo "$1"nd;;
 410        *3) echo "$1"rd;;
 411        esac
 412}
 413
 414update_squash_messages () {
 415        if test -f "$squash_msg"; then
 416                mv "$squash_msg" "$squash_msg".bak || exit
 417                count=$(($(sed -n \
 418                        -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
 419                        -e "q" < "$squash_msg".bak)+1))
 420                {
 421                        printf '%s\n' "$comment_char This is a combination of $count commits."
 422                        sed -e 1d -e '2,/^./{
 423                                /^$/d
 424                        }' <"$squash_msg".bak
 425                } >"$squash_msg"
 426        else
 427                commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
 428                count=2
 429                {
 430                        printf '%s\n' "$comment_char This is a combination of 2 commits."
 431                        printf '%s\n' "$comment_char The first commit's message is:"
 432                        echo
 433                        cat "$fixup_msg"
 434                } >"$squash_msg"
 435        fi
 436        case $1 in
 437        squash)
 438                rm -f "$fixup_msg"
 439                echo
 440                printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
 441                echo
 442                commit_message $2
 443                ;;
 444        fixup)
 445                echo
 446                printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
 447                echo
 448                # Change the space after the comment character to TAB:
 449                commit_message $2 | git stripspace --comment-lines | sed -e 's/ /       /'
 450                ;;
 451        esac >>"$squash_msg"
 452}
 453
 454peek_next_command () {
 455        git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
 456}
 457
 458# A squash/fixup has failed.  Prepare the long version of the squash
 459# commit message, then die_with_patch.  This code path requires the
 460# user to edit the combined commit message for all commits that have
 461# been squashed/fixedup so far.  So also erase the old squash
 462# messages, effectively causing the combined commit to be used as the
 463# new basis for any further squash/fixups.  Args: sha1 rest
 464die_failed_squash() {
 465        sha1=$1
 466        rest=$2
 467        mv "$squash_msg" "$msg" || exit
 468        rm -f "$fixup_msg"
 469        cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
 470        warn
 471        warn "$(eval_gettext "Could not apply \$sha1... \$rest")"
 472        die_with_patch $sha1 ""
 473}
 474
 475flush_rewritten_pending() {
 476        test -s "$rewritten_pending" || return
 477        newsha1="$(git rev-parse HEAD^0)"
 478        sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
 479        rm -f "$rewritten_pending"
 480}
 481
 482record_in_rewritten() {
 483        oldsha1="$(git rev-parse $1)"
 484        echo "$oldsha1" >> "$rewritten_pending"
 485
 486        case "$(peek_next_command)" in
 487        squash|s|fixup|f)
 488                ;;
 489        *)
 490                flush_rewritten_pending
 491                ;;
 492        esac
 493}
 494
 495do_pick () {
 496        sha1=$1
 497        rest=$2
 498        if test "$(git rev-parse HEAD)" = "$squash_onto"
 499        then
 500                # Set the correct commit message and author info on the
 501                # sentinel root before cherry-picking the original changes
 502                # without committing (-n).  Finally, update the sentinel again
 503                # to include these changes.  If the cherry-pick results in a
 504                # conflict, this means our behaviour is similar to a standard
 505                # failed cherry-pick during rebase, with a dirty index to
 506                # resolve before manually running git commit --amend then git
 507                # rebase --continue.
 508                git commit --allow-empty --allow-empty-message --amend \
 509                           --no-post-rewrite -n -q -C $sha1 &&
 510                        pick_one -n $sha1 &&
 511                        git commit --allow-empty --allow-empty-message \
 512                                   --amend --no-post-rewrite -n -q -C $sha1 \
 513                                   ${gpg_sign_opt:+"$gpg_sign_opt"} ||
 514                                   die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
 515        else
 516                pick_one $sha1 ||
 517                        die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
 518        fi
 519}
 520
 521do_next () {
 522        rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
 523        read -r command sha1 rest < "$todo"
 524        case "$command" in
 525        "$comment_char"*|''|noop|drop|d)
 526                mark_action_done
 527                ;;
 528        "$cr")
 529                # Work around CR left by "read" (e.g. with Git for Windows' Bash).
 530                mark_action_done
 531                ;;
 532        pick|p)
 533                comment_for_reflog pick
 534
 535                mark_action_done
 536                do_pick $sha1 "$rest"
 537                record_in_rewritten $sha1
 538                ;;
 539        reword|r)
 540                comment_for_reflog reword
 541
 542                mark_action_done
 543                do_pick $sha1 "$rest"
 544                git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
 545                        warn "$(eval_gettext "\
 546Could not amend commit after successfully picking \$sha1... \$rest
 547This is most likely due to an empty commit message, or the pre-commit hook
 548failed. If the pre-commit hook failed, you may need to resolve the issue before
 549you are able to reword the commit.")"
 550                        exit_with_patch $sha1 1
 551                }
 552                record_in_rewritten $sha1
 553                ;;
 554        edit|e)
 555                comment_for_reflog edit
 556
 557                mark_action_done
 558                do_pick $sha1 "$rest"
 559                sha1_abbrev=$(git rev-parse --short $sha1)
 560                warn "$(eval_gettext "Stopped at \$sha1_abbrev... \$rest")"
 561                exit_with_patch $sha1 0
 562                ;;
 563        squash|s|fixup|f)
 564                case "$command" in
 565                squash|s)
 566                        squash_style=squash
 567                        ;;
 568                fixup|f)
 569                        squash_style=fixup
 570                        ;;
 571                esac
 572                comment_for_reflog $squash_style
 573
 574                test -f "$done" && has_action "$done" ||
 575                        die "$(eval_gettext "Cannot '\$squash_style' without a previous commit")"
 576
 577                mark_action_done
 578                update_squash_messages $squash_style $sha1
 579                author_script_content=$(get_author_ident_from_commit HEAD)
 580                echo "$author_script_content" > "$author_script"
 581                eval "$author_script_content"
 582                if ! pick_one -n $sha1
 583                then
 584                        git rev-parse --verify HEAD >"$amend"
 585                        die_failed_squash $sha1 "$rest"
 586                fi
 587                case "$(peek_next_command)" in
 588                squash|s|fixup|f)
 589                        # This is an intermediate commit; its message will only be
 590                        # used in case of trouble.  So use the long version:
 591                        do_with_author output git commit --amend --no-verify -F "$squash_msg" \
 592                                ${gpg_sign_opt:+"$gpg_sign_opt"} ||
 593                                die_failed_squash $sha1 "$rest"
 594                        ;;
 595                *)
 596                        # This is the final command of this squash/fixup group
 597                        if test -f "$fixup_msg"
 598                        then
 599                                do_with_author git commit --amend --no-verify -F "$fixup_msg" \
 600                                        ${gpg_sign_opt:+"$gpg_sign_opt"} ||
 601                                        die_failed_squash $sha1 "$rest"
 602                        else
 603                                cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
 604                                rm -f "$GIT_DIR"/MERGE_MSG
 605                                do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
 606                                        ${gpg_sign_opt:+"$gpg_sign_opt"} ||
 607                                        die_failed_squash $sha1 "$rest"
 608                        fi
 609                        rm -f "$squash_msg" "$fixup_msg"
 610                        ;;
 611                esac
 612                record_in_rewritten $sha1
 613                ;;
 614        x|"exec")
 615                read -r command rest < "$todo"
 616                mark_action_done
 617                eval_gettextln "Executing: \$rest"
 618                "${SHELL:-@SHELL_PATH@}" -c "$rest" # Actual execution
 619                status=$?
 620                # Run in subshell because require_clean_work_tree can die.
 621                dirty=f
 622                (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
 623                if test "$status" -ne 0
 624                then
 625                        warn "$(eval_gettext "Execution failed: \$rest")"
 626                        test "$dirty" = f ||
 627                                warn "$(gettext "and made changes to the index and/or the working tree")"
 628
 629                        warn "$(gettext "\
 630You can fix the problem, and then run
 631
 632        git rebase --continue")"
 633                        warn
 634                        if test $status -eq 127         # command not found
 635                        then
 636                                status=1
 637                        fi
 638                        exit "$status"
 639                elif test "$dirty" = t
 640                then
 641                        # TRANSLATORS: after these lines is a command to be issued by the user
 642                        warn "$(eval_gettext "\
 643Execution succeeded: \$rest
 644but left changes to the index and/or the working tree
 645Commit or stash your changes, and then run
 646
 647        git rebase --continue")"
 648                        warn
 649                        exit 1
 650                fi
 651                ;;
 652        *)
 653                warn "$(eval_gettext "Unknown command: \$command \$sha1 \$rest")"
 654                fixtodo="$(gettext "Please fix this using 'git rebase --edit-todo'.")"
 655                if git rev-parse --verify -q "$sha1" >/dev/null
 656                then
 657                        die_with_patch $sha1 "$fixtodo"
 658                else
 659                        die "$fixtodo"
 660                fi
 661                ;;
 662        esac
 663        test -s "$todo" && return
 664
 665        comment_for_reflog finish &&
 666        newhead=$(git rev-parse HEAD) &&
 667        case $head_name in
 668        refs/*)
 669                message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
 670                git update-ref -m "$message" $head_name $newhead $orig_head &&
 671                git symbolic-ref \
 672                  -m "$GIT_REFLOG_ACTION: returning to $head_name" \
 673                  HEAD $head_name
 674                ;;
 675        esac && {
 676                test ! -f "$state_dir"/verbose ||
 677                        git diff-tree --stat $orig_head..HEAD
 678        } &&
 679        {
 680                test -s "$rewritten_list" &&
 681                git notes copy --for-rewrite=rebase < "$rewritten_list" ||
 682                true # we don't care if this copying failed
 683        } &&
 684        hook="$(git rev-parse --git-path hooks/post-rewrite)"
 685        if test -x "$hook" && test -s "$rewritten_list"; then
 686                "$hook" rebase < "$rewritten_list"
 687                true # we don't care if this hook failed
 688        fi &&
 689                warn "$(eval_gettext "Successfully rebased and updated \$head_name.")"
 690
 691        return 1 # not failure; just to break the do_rest loop
 692}
 693
 694# can only return 0, when the infinite loop breaks
 695do_rest () {
 696        while :
 697        do
 698                do_next || break
 699        done
 700}
 701
 702# skip picking commits whose parents are unchanged
 703skip_unnecessary_picks () {
 704        fd=3
 705        while read -r command rest
 706        do
 707                # fd=3 means we skip the command
 708                case "$fd,$command" in
 709                3,pick|3,p)
 710                        # pick a commit whose parent is current $onto -> skip
 711                        sha1=${rest%% *}
 712                        case "$(git rev-parse --verify --quiet "$sha1"^)" in
 713                        "$onto"*)
 714                                onto=$sha1
 715                                ;;
 716                        *)
 717                                fd=1
 718                                ;;
 719                        esac
 720                        ;;
 721                3,"$comment_char"*|3,)
 722                        # copy comments
 723                        ;;
 724                *)
 725                        fd=1
 726                        ;;
 727                esac
 728                printf '%s\n' "$command${rest:+ }$rest" >&$fd
 729        done <"$todo" >"$todo.new" 3>>"$done" &&
 730        mv -f "$todo".new "$todo" &&
 731        case "$(peek_next_command)" in
 732        squash|s|fixup|f)
 733                record_in_rewritten "$onto"
 734                ;;
 735        esac ||
 736                die "$(gettext "Could not skip unnecessary pick commands")"
 737}
 738
 739transform_todo_ids () {
 740        while read -r command rest
 741        do
 742                case "$command" in
 743                "$comment_char"* | exec)
 744                        # Be careful for oddball commands like 'exec'
 745                        # that do not have a SHA-1 at the beginning of $rest.
 746                        ;;
 747                *)
 748                        sha1=$(git rev-parse --verify --quiet "$@" ${rest%%[     ]*}) &&
 749                        rest="$sha1 ${rest#*[    ]}"
 750                        ;;
 751                esac
 752                printf '%s\n' "$command${rest:+ }$rest"
 753        done <"$todo" >"$todo.new" &&
 754        mv -f "$todo.new" "$todo"
 755}
 756
 757expand_todo_ids() {
 758        transform_todo_ids
 759}
 760
 761collapse_todo_ids() {
 762        transform_todo_ids --short
 763}
 764
 765# Rearrange the todo list that has both "pick sha1 msg" and
 766# "pick sha1 fixup!/squash! msg" appears in it so that the latter
 767# comes immediately after the former, and change "pick" to
 768# "fixup"/"squash".
 769#
 770# Note that if the config has specified a custom instruction format
 771# each log message will be re-retrieved in order to normalize the
 772# autosquash arrangement
 773rearrange_squash () {
 774        # extract fixup!/squash! lines and resolve any referenced sha1's
 775        while read -r pick sha1 message
 776        do
 777                test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
 778                case "$message" in
 779                "squash! "*|"fixup! "*)
 780                        action="${message%%!*}"
 781                        rest=$message
 782                        prefix=
 783                        # skip all squash! or fixup! (but save for later)
 784                        while :
 785                        do
 786                                case "$rest" in
 787                                "squash! "*|"fixup! "*)
 788                                        prefix="$prefix${rest%%!*},"
 789                                        rest="${rest#*! }"
 790                                        ;;
 791                                *)
 792                                        break
 793                                        ;;
 794                                esac
 795                        done
 796                        printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
 797                        # if it's a single word, try to resolve to a full sha1 and
 798                        # emit a second copy. This allows us to match on both message
 799                        # and on sha1 prefix
 800                        if test "${rest#* }" = "$rest"; then
 801                                fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
 802                                if test -n "$fullsha"; then
 803                                        # prefix the action to uniquely identify this line as
 804                                        # intended for full sha1 match
 805                                        echo "$sha1 +$action $prefix $fullsha"
 806                                fi
 807                        fi
 808                esac
 809        done >"$1.sq" <"$1"
 810        test -s "$1.sq" || return
 811
 812        used=
 813        while read -r pick sha1 message
 814        do
 815                case " $used" in
 816                *" $sha1 "*) continue ;;
 817                esac
 818                printf '%s\n' "$pick $sha1 $message"
 819                test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
 820                used="$used$sha1 "
 821                while read -r squash action msg_prefix msg_content
 822                do
 823                        case " $used" in
 824                        *" $squash "*) continue ;;
 825                        esac
 826                        emit=0
 827                        case "$action" in
 828                        +*)
 829                                action="${action#+}"
 830                                # full sha1 prefix test
 831                                case "$msg_content" in "$sha1"*) emit=1;; esac ;;
 832                        *)
 833                                # message prefix test
 834                                case "$message" in "$msg_content"*) emit=1;; esac ;;
 835                        esac
 836                        if test $emit = 1; then
 837                                if test -n "${format}"
 838                                then
 839                                        msg_content=$(git log -n 1 --format="${format}" ${squash})
 840                                else
 841                                        msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
 842                                fi
 843                                printf '%s\n' "$action $squash $msg_content"
 844                                used="$used$squash "
 845                        fi
 846                done <"$1.sq"
 847        done >"$1.rearranged" <"$1"
 848        cat "$1.rearranged" >"$1"
 849        rm -f "$1.sq" "$1.rearranged"
 850}
 851
 852# Add commands after a pick or after a squash/fixup serie
 853# in the todo list.
 854add_exec_commands () {
 855        {
 856                first=t
 857                while read -r insn rest
 858                do
 859                        case $insn in
 860                        pick)
 861                                test -n "$first" ||
 862                                printf "%s" "$cmd"
 863                                ;;
 864                        esac
 865                        printf "%s %s\n" "$insn" "$rest"
 866                        first=
 867                done
 868                printf "%s" "$cmd"
 869        } <"$1" >"$1.new" &&
 870        mv "$1.new" "$1"
 871}
 872
 873# Check if the SHA-1 passed as an argument is a
 874# correct one, if not then print $2 in "$todo".badsha
 875# $1: the SHA-1 to test
 876# $2: the line number of the input
 877# $3: the input filename
 878check_commit_sha () {
 879        badsha=0
 880        if test -z "$1"
 881        then
 882                badsha=1
 883        else
 884                sha1_verif="$(git rev-parse --verify --quiet $1^{commit})"
 885                if test -z "$sha1_verif"
 886                then
 887                        badsha=1
 888                fi
 889        fi
 890
 891        if test $badsha -ne 0
 892        then
 893                line="$(sed -n -e "${2}p" "$3")"
 894                warn "$(eval_gettext "\
 895Warning: the SHA-1 is missing or isn't a commit in the following line:
 896 - \$line")"
 897                warn
 898        fi
 899
 900        return $badsha
 901}
 902
 903# prints the bad commits and bad commands
 904# from the todolist in stdin
 905check_bad_cmd_and_sha () {
 906        retval=0
 907        lineno=0
 908        while read -r command rest
 909        do
 910                lineno=$(( $lineno + 1 ))
 911                case $command in
 912                "$comment_char"*|''|noop|x|exec)
 913                        # Doesn't expect a SHA-1
 914                        ;;
 915                "$cr")
 916                        # Work around CR left by "read" (e.g. with Git for
 917                        # Windows' Bash).
 918                        ;;
 919                pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
 920                        if ! check_commit_sha "${rest%%[        ]*}" "$lineno" "$1"
 921                        then
 922                                retval=1
 923                        fi
 924                        ;;
 925                *)
 926                        line="$(sed -n -e "${lineno}p" "$1")"
 927                        warn "$(eval_gettext "\
 928Warning: the command isn't recognized in the following line:
 929 - \$line")"
 930                        warn
 931                        retval=1
 932                        ;;
 933                esac
 934        done <"$1"
 935        return $retval
 936}
 937
 938# Print the list of the SHA-1 of the commits
 939# from stdin to stdout
 940todo_list_to_sha_list () {
 941        git stripspace --strip-comments |
 942        while read -r command sha1 rest
 943        do
 944                case $command in
 945                "$comment_char"*|''|noop|x|"exec")
 946                        ;;
 947                *)
 948                        long_sha=$(git rev-list --no-walk "$sha1" 2>/dev/null)
 949                        printf "%s\n" "$long_sha"
 950                        ;;
 951                esac
 952        done
 953}
 954
 955# Use warn for each line in stdin
 956warn_lines () {
 957        while read -r line
 958        do
 959                warn " - $line"
 960        done
 961}
 962
 963# Switch to the branch in $into and notify it in the reflog
 964checkout_onto () {
 965        GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
 966        output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
 967        git update-ref ORIG_HEAD $orig_head
 968}
 969
 970get_missing_commit_check_level () {
 971        check_level=$(git config --get rebase.missingCommitsCheck)
 972        check_level=${check_level:-ignore}
 973        # Don't be case sensitive
 974        printf '%s' "$check_level" | tr 'A-Z' 'a-z'
 975}
 976
 977# Check if the user dropped some commits by mistake
 978# Behaviour determined by rebase.missingCommitsCheck.
 979# Check if there is an unrecognized command or a
 980# bad SHA-1 in a command.
 981check_todo_list () {
 982        raise_error=f
 983
 984        check_level=$(get_missing_commit_check_level)
 985
 986        case "$check_level" in
 987        warn|error)
 988                # Get the SHA-1 of the commits
 989                todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
 990                todo_list_to_sha_list <"$todo" >"$todo".newsha1
 991
 992                # Sort the SHA-1 and compare them
 993                sort -u "$todo".oldsha1 >"$todo".oldsha1+
 994                mv "$todo".oldsha1+ "$todo".oldsha1
 995                sort -u "$todo".newsha1 >"$todo".newsha1+
 996                mv "$todo".newsha1+ "$todo".newsha1
 997                comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
 998
 999                # Warn about missing commits
1000                if test -s "$todo".miss
1001                then
1002                        test "$check_level" = error && raise_error=t
1003
1004                        warn "$(gettext "\
1005Warning: some commits may have been dropped accidentally.
1006Dropped commits (newer to older):")"
1007
1008                        # Make the list user-friendly and display
1009                        opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
1010                        git rev-list $opt <"$todo".miss | warn_lines
1011
1012                        warn "$(gettext "\
1013To avoid this message, use \"drop\" to explicitly remove a commit.
1014
1015Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1016The possible behaviours are: ignore, warn, error.")"
1017                        warn
1018                fi
1019                ;;
1020        ignore)
1021                ;;
1022        *)
1023                warn "$(eval_gettext "Unrecognized setting \$check_level for option rebase.missingCommitsCheck. Ignoring.")"
1024                ;;
1025        esac
1026
1027        if ! check_bad_cmd_and_sha "$todo"
1028        then
1029                raise_error=t
1030        fi
1031
1032        if test $raise_error = t
1033        then
1034                # Checkout before the first commit of the
1035                # rebase: this way git rebase --continue
1036                # will work correctly as it expects HEAD to be
1037                # placed before the commit of the next action
1038                checkout_onto
1039
1040                warn "$(gettext "You can fix this with 'git rebase --edit-todo'.")"
1041                die "$(gettext "Or you can abort the rebase with 'git rebase --abort'.")"
1042        fi
1043}
1044
1045# The whole contents of this file is run by dot-sourcing it from
1046# inside a shell function.  It used to be that "return"s we see
1047# below were not inside any function, and expected to return
1048# to the function that dot-sourced us.
1049#
1050# However, FreeBSD /bin/sh misbehaves on such a construct and
1051# continues to run the statements that follow such a "return".
1052# As a work-around, we introduce an extra layer of a function
1053# here, and immediately call it after defining it.
1054git_rebase__interactive () {
1055
1056case "$action" in
1057continue)
1058        # do we have anything to commit?
1059        if git diff-index --cached --quiet HEAD --
1060        then
1061                # Nothing to commit -- skip this commit
1062
1063                test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
1064                rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
1065                die "$(gettext "Could not remove CHERRY_PICK_HEAD")"
1066        else
1067                if ! test -f "$author_script"
1068                then
1069                        gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
1070                        die "$(eval_gettext "\
1071You have staged changes in your working tree.
1072If these changes are meant to be
1073squashed into the previous commit, run:
1074
1075  git commit --amend \$gpg_sign_opt_quoted
1076
1077If they are meant to go into a new commit, run:
1078
1079  git commit \$gpg_sign_opt_quoted
1080
1081In both case, once you're done, continue with:
1082
1083  git rebase --continue
1084")"
1085                fi
1086                . "$author_script" ||
1087                        die "$(gettext "Error trying to find the author identity to amend commit")"
1088                if test -f "$amend"
1089                then
1090                        current_head=$(git rev-parse --verify HEAD)
1091                        test "$current_head" = $(cat "$amend") ||
1092                        die "$(gettext "\
1093You have uncommitted changes in your working tree. Please commit them
1094first and then run 'git rebase --continue' again.")"
1095                        do_with_author git commit --amend --no-verify -F "$msg" -e \
1096                                ${gpg_sign_opt:+"$gpg_sign_opt"} ||
1097                                die "$(gettext "Could not commit staged changes.")"
1098                else
1099                        do_with_author git commit --no-verify -F "$msg" -e \
1100                                ${gpg_sign_opt:+"$gpg_sign_opt"} ||
1101                                die "$(gettext "Could not commit staged changes.")"
1102                fi
1103        fi
1104
1105        if test -r "$state_dir"/stopped-sha
1106        then
1107                record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
1108        fi
1109
1110        require_clean_work_tree "rebase"
1111        do_rest
1112        return 0
1113        ;;
1114skip)
1115        git rerere clear
1116
1117        do_rest
1118        return 0
1119        ;;
1120edit-todo)
1121        git stripspace --strip-comments <"$todo" >"$todo".new
1122        mv -f "$todo".new "$todo"
1123        collapse_todo_ids
1124        append_todo_help
1125        gettext "
1126You are editing the todo file of an ongoing interactive rebase.
1127To continue rebase after editing, run:
1128    git rebase --continue
1129
1130" | git stripspace --comment-lines >>"$todo"
1131
1132        git_sequence_editor "$todo" ||
1133                die "$(gettext "Could not execute editor")"
1134        expand_todo_ids
1135
1136        exit
1137        ;;
1138esac
1139
1140git var GIT_COMMITTER_IDENT >/dev/null ||
1141        die "$(gettext "You need to set your committer info first")"
1142
1143comment_for_reflog start
1144
1145if test ! -z "$switch_to"
1146then
1147        GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
1148        output git checkout "$switch_to" -- ||
1149                die "$(eval_gettext "Could not checkout \$switch_to")"
1150
1151        comment_for_reflog start
1152fi
1153
1154orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
1155mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
1156
1157: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
1158write_basic_state
1159if test t = "$preserve_merges"
1160then
1161        if test -z "$rebase_root"
1162        then
1163                mkdir "$rewritten" &&
1164                for c in $(git merge-base --all $orig_head $upstream)
1165                do
1166                        echo $onto > "$rewritten"/$c ||
1167                                die "$(gettext "Could not init rewritten commits")"
1168                done
1169        else
1170                mkdir "$rewritten" &&
1171                echo $onto > "$rewritten"/root ||
1172                        die "$(gettext "Could not init rewritten commits")"
1173        fi
1174        # No cherry-pick because our first pass is to determine
1175        # parents to rewrite and skipping dropped commits would
1176        # prematurely end our probe
1177        merges_option=
1178else
1179        merges_option="--no-merges --cherry-pick"
1180fi
1181
1182shorthead=$(git rev-parse --short $orig_head)
1183shortonto=$(git rev-parse --short $onto)
1184if test -z "$rebase_root"
1185        # this is now equivalent to ! -z "$upstream"
1186then
1187        shortupstream=$(git rev-parse --short $upstream)
1188        revisions=$upstream...$orig_head
1189        shortrevisions=$shortupstream..$shorthead
1190else
1191        revisions=$onto...$orig_head
1192        shortrevisions=$shorthead
1193fi
1194format=$(git config --get rebase.instructionFormat)
1195# the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
1196git rev-list $merges_option --format="%m%H ${format:-%s}" \
1197        --reverse --left-right --topo-order \
1198        $revisions ${restrict_revision+^$restrict_revision} | \
1199        sed -n "s/^>//p" |
1200while read -r sha1 rest
1201do
1202
1203        if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
1204        then
1205                comment_out="$comment_char "
1206        else
1207                comment_out=
1208        fi
1209
1210        if test t != "$preserve_merges"
1211        then
1212                printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1213        else
1214                if test -z "$rebase_root"
1215                then
1216                        preserve=t
1217                        for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1218                        do
1219                                if test -f "$rewritten"/$p
1220                                then
1221                                        preserve=f
1222                                fi
1223                        done
1224                else
1225                        preserve=f
1226                fi
1227                if test f = "$preserve"
1228                then
1229                        touch "$rewritten"/$sha1
1230                        printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1231                fi
1232        fi
1233done
1234
1235# Watch for commits that been dropped by --cherry-pick
1236if test t = "$preserve_merges"
1237then
1238        mkdir "$dropped"
1239        # Save all non-cherry-picked changes
1240        git rev-list $revisions --left-right --cherry-pick | \
1241                sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
1242        # Now all commits and note which ones are missing in
1243        # not-cherry-picks and hence being dropped
1244        git rev-list $revisions |
1245        while read rev
1246        do
1247                if test -f "$rewritten"/$rev &&
1248                   ! sane_grep "$rev" "$state_dir"/not-cherry-picks >/dev/null
1249                then
1250                        # Use -f2 because if rev-list is telling us this commit is
1251                        # not worthwhile, we don't want to track its multiple heads,
1252                        # just the history of its first-parent for others that will
1253                        # be rebasing on top of it
1254                        git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1255                        sha1=$(git rev-list -1 $rev)
1256                        sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1257                        rm "$rewritten"/$rev
1258                fi
1259        done
1260fi
1261
1262test -s "$todo" || echo noop >> "$todo"
1263test -n "$autosquash" && rearrange_squash "$todo"
1264test -n "$cmd" && add_exec_commands "$todo"
1265
1266todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
1267todocount=${todocount##* }
1268
1269cat >>"$todo" <<EOF
1270
1271$comment_char $(eval_ngettext \
1272        "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
1273        "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
1274        "$todocount")
1275EOF
1276append_todo_help
1277gettext "
1278However, if you remove everything, the rebase will be aborted.
1279
1280" | git stripspace --comment-lines >>"$todo"
1281
1282if test -z "$keep_empty"
1283then
1284        printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
1285fi
1286
1287
1288has_action "$todo" ||
1289        return 2
1290
1291cp "$todo" "$todo".backup
1292collapse_todo_ids
1293git_sequence_editor "$todo" ||
1294        die_abort "$(gettext "Could not execute editor")"
1295
1296has_action "$todo" ||
1297        return 2
1298
1299check_todo_list
1300
1301expand_todo_ids
1302
1303test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1304
1305checkout_onto
1306do_rest
1307
1308}
1309# ... and then we call the whole thing.
1310git_rebase__interactive