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