git-rebase--interactive.shon commit Windows: boost startup by avoiding a static dependency on shell32.dll (928500e)
   1#!/bin/sh
   2#
   3# Copyright (c) 2006 Johannes E. Schindelin
   4
   5# SHORT DESCRIPTION
   6#
   7# This script makes it easy to fix up commits in the middle of a series,
   8# and rearrange commits.
   9#
  10# The original idea comes from Eric W. Biederman, in
  11# http://article.gmane.org/gmane.comp.version-control.git/22407
  12
  13OPTIONS_KEEPDASHDASH=
  14OPTIONS_SPEC="\
  15git-rebase [-i] [options] [--] <upstream> [<branch>]
  16git-rebase [-i] (--continue | --abort | --skip)
  17--
  18 Available options are
  19v,verbose          display a diffstat of what changed upstream
  20onto=              rebase onto given branch instead of upstream
  21p,preserve-merges  try to recreate merges instead of ignoring them
  22s,strategy=        use the given merge strategy
  23m,merge            always used (no-op)
  24i,interactive      always used (no-op)
  25 Actions:
  26continue           continue rebasing process
  27abort              abort rebasing process and restore original branch
  28skip               skip current patch and continue rebasing process
  29no-verify          override pre-rebase hook from stopping the operation
  30root               rebase all reachable commmits up to the root(s)
  31"
  32
  33. git-sh-setup
  34require_work_tree
  35
  36DOTEST="$GIT_DIR/rebase-merge"
  37TODO="$DOTEST"/git-rebase-todo
  38DONE="$DOTEST"/done
  39MSG="$DOTEST"/message
  40SQUASH_MSG="$DOTEST"/message-squash
  41REWRITTEN="$DOTEST"/rewritten
  42DROPPED="$DOTEST"/dropped
  43PRESERVE_MERGES=
  44STRATEGY=
  45ONTO=
  46VERBOSE=
  47OK_TO_SKIP_PRE_REBASE=
  48REBASE_ROOT=
  49
  50GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
  51mark the corrected paths with 'git add <paths>', and
  52run 'git rebase --continue'"
  53export GIT_CHERRY_PICK_HELP
  54
  55warn () {
  56        echo "$*" >&2
  57}
  58
  59output () {
  60        case "$VERBOSE" in
  61        '')
  62                output=$("$@" 2>&1 )
  63                status=$?
  64                test $status != 0 && printf "%s\n" "$output"
  65                return $status
  66                ;;
  67        *)
  68                "$@"
  69                ;;
  70        esac
  71}
  72
  73run_pre_rebase_hook () {
  74        if test -z "$OK_TO_SKIP_PRE_REBASE" &&
  75           test -x "$GIT_DIR/hooks/pre-rebase"
  76        then
  77                "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
  78                        echo >&2 "The pre-rebase hook refused to rebase."
  79                        exit 1
  80                }
  81        fi
  82}
  83
  84require_clean_work_tree () {
  85        # test if working tree is dirty
  86        git rev-parse --verify HEAD > /dev/null &&
  87        git update-index --ignore-submodules --refresh &&
  88        git diff-files --quiet --ignore-submodules &&
  89        git diff-index --cached --quiet HEAD --ignore-submodules -- ||
  90        die "Working tree is dirty"
  91}
  92
  93ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
  94
  95comment_for_reflog () {
  96        case "$ORIG_REFLOG_ACTION" in
  97        ''|rebase*)
  98                GIT_REFLOG_ACTION="rebase -i ($1)"
  99                export GIT_REFLOG_ACTION
 100                ;;
 101        esac
 102}
 103
 104last_count=
 105mark_action_done () {
 106        sed -e 1q < "$TODO" >> "$DONE"
 107        sed -e 1d < "$TODO" >> "$TODO".new
 108        mv -f "$TODO".new "$TODO"
 109        count=$(sane_grep -c '^[^#]' < "$DONE")
 110        total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
 111        if test "$last_count" != "$count"
 112        then
 113                last_count=$count
 114                printf "Rebasing (%d/%d)\r" $count $total
 115                test -z "$VERBOSE" || echo
 116        fi
 117}
 118
 119make_patch () {
 120        sha1_and_parents="$(git rev-list --parents -1 "$1")"
 121        case "$sha1_and_parents" in
 122        ?*' '?*' '?*)
 123                git diff --cc $sha1_and_parents
 124                ;;
 125        ?*' '?*)
 126                git diff-tree -p "$1^!"
 127                ;;
 128        *)
 129                echo "Root commit"
 130                ;;
 131        esac > "$DOTEST"/patch
 132        test -f "$DOTEST"/message ||
 133                git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
 134        test -f "$DOTEST"/author-script ||
 135                get_author_ident_from_commit "$1" > "$DOTEST"/author-script
 136}
 137
 138die_with_patch () {
 139        make_patch "$1"
 140        git rerere
 141        die "$2"
 142}
 143
 144die_abort () {
 145        rm -rf "$DOTEST"
 146        die "$1"
 147}
 148
 149has_action () {
 150        sane_grep '^[^#]' "$1" >/dev/null
 151}
 152
 153pick_one () {
 154        no_ff=
 155        case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
 156        output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
 157        test -d "$REWRITTEN" &&
 158                pick_one_preserving_merges "$@" && return
 159        if test ! -z "$REBASE_ROOT"
 160        then
 161                output git cherry-pick "$@"
 162                return
 163        fi
 164        parent_sha1=$(git rev-parse --verify $sha1^) ||
 165                die "Could not get the parent of $sha1"
 166        current_sha1=$(git rev-parse --verify HEAD)
 167        if test "$no_ff$current_sha1" = "$parent_sha1"; then
 168                output git reset --hard $sha1
 169                test "a$1" = a-n && output git reset --soft $current_sha1
 170                sha1=$(git rev-parse --short $sha1)
 171                output warn Fast-forward to $sha1
 172        else
 173                output git cherry-pick "$@"
 174        fi
 175}
 176
 177pick_one_preserving_merges () {
 178        fast_forward=t
 179        case "$1" in
 180        -n)
 181                fast_forward=f
 182                sha1=$2
 183                ;;
 184        *)
 185                sha1=$1
 186                ;;
 187        esac
 188        sha1=$(git rev-parse $sha1)
 189
 190        if test -f "$DOTEST"/current-commit
 191        then
 192                if test "$fast_forward" = t
 193                then
 194                        cat "$DOTEST"/current-commit | while read current_commit
 195                        do
 196                                git rev-parse HEAD > "$REWRITTEN"/$current_commit
 197                        done
 198                        rm "$DOTEST"/current-commit ||
 199                        die "Cannot write current commit's replacement sha1"
 200                fi
 201        fi
 202
 203        echo $sha1 >> "$DOTEST"/current-commit
 204
 205        # rewrite parents; if none were rewritten, we can fast-forward.
 206        new_parents=
 207        pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
 208        if test "$pend" = " "
 209        then
 210                pend=" root"
 211        fi
 212        while [ "$pend" != "" ]
 213        do
 214                p=$(expr "$pend" : ' \([^ ]*\)')
 215                pend="${pend# $p}"
 216
 217                if test -f "$REWRITTEN"/$p
 218                then
 219                        new_p=$(cat "$REWRITTEN"/$p)
 220
 221                        # If the todo reordered commits, and our parent is marked for
 222                        # rewriting, but hasn't been gotten to yet, assume the user meant to
 223                        # drop it on top of the current HEAD
 224                        if test -z "$new_p"
 225                        then
 226                                new_p=$(git rev-parse HEAD)
 227                        fi
 228
 229                        test $p != $new_p && fast_forward=f
 230                        case "$new_parents" in
 231                        *$new_p*)
 232                                ;; # do nothing; that parent is already there
 233                        *)
 234                                new_parents="$new_parents $new_p"
 235                                ;;
 236                        esac
 237                else
 238                        if test -f "$DROPPED"/$p
 239                        then
 240                                fast_forward=f
 241                                replacement="$(cat "$DROPPED"/$p)"
 242                                test -z "$replacement" && replacement=root
 243                                pend=" $replacement$pend"
 244                        else
 245                                new_parents="$new_parents $p"
 246                        fi
 247                fi
 248        done
 249        case $fast_forward in
 250        t)
 251                output warn "Fast-forward to $sha1"
 252                output git reset --hard $sha1 ||
 253                        die "Cannot fast-forward to $sha1"
 254                ;;
 255        f)
 256                first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 257
 258                if [ "$1" != "-n" ]
 259                then
 260                        # detach HEAD to current parent
 261                        output git checkout $first_parent 2> /dev/null ||
 262                                die "Cannot move HEAD to $first_parent"
 263                fi
 264
 265                case "$new_parents" in
 266                ' '*' '*)
 267                        test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 268
 269                        # redo merge
 270                        author_script=$(get_author_ident_from_commit $sha1)
 271                        eval "$author_script"
 272                        msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
 273                        # No point in merging the first parent, that's HEAD
 274                        new_parents=${new_parents# $first_parent}
 275                        if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 276                                GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 277                                GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 278                                output git merge $STRATEGY -m "$msg" \
 279                                        $new_parents
 280                        then
 281                                printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
 282                                die_with_patch $sha1 "Error redoing merge $sha1"
 283                        fi
 284                        ;;
 285                *)
 286                        output git cherry-pick "$@" ||
 287                                die_with_patch $sha1 "Could not pick $sha1"
 288                        ;;
 289                esac
 290                ;;
 291        esac
 292}
 293
 294nth_string () {
 295        case "$1" in
 296        *1[0-9]|*[04-9]) echo "$1"th;;
 297        *1) echo "$1"st;;
 298        *2) echo "$1"nd;;
 299        *3) echo "$1"rd;;
 300        esac
 301}
 302
 303make_squash_message () {
 304        if test -f "$SQUASH_MSG"; then
 305                COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
 306                        < "$SQUASH_MSG" | sed -ne '$p')+1))
 307                echo "# This is a combination of $COUNT commits."
 308                sed -e 1d -e '2,/^./{
 309                        /^$/d
 310                }' <"$SQUASH_MSG"
 311        else
 312                COUNT=2
 313                echo "# This is a combination of two commits."
 314                echo "# The first commit's message is:"
 315                echo
 316                git cat-file commit HEAD | sed -e '1,/^$/d'
 317        fi
 318        echo
 319        echo "# This is the $(nth_string $COUNT) commit message:"
 320        echo
 321        git cat-file commit $1 | sed -e '1,/^$/d'
 322}
 323
 324peek_next_command () {
 325        sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
 326}
 327
 328do_next () {
 329        rm -f "$DOTEST"/message "$DOTEST"/author-script \
 330                "$DOTEST"/amend || exit
 331        read command sha1 rest < "$TODO"
 332        case "$command" in
 333        '#'*|''|noop)
 334                mark_action_done
 335                ;;
 336        pick|p)
 337                comment_for_reflog pick
 338
 339                mark_action_done
 340                pick_one $sha1 ||
 341                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 342                ;;
 343        reword|r)
 344                comment_for_reflog reword
 345
 346                mark_action_done
 347                pick_one $sha1 ||
 348                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 349                git commit --amend
 350                ;;
 351        edit|e)
 352                comment_for_reflog edit
 353
 354                mark_action_done
 355                pick_one $sha1 ||
 356                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 357                make_patch $sha1
 358                git rev-parse --verify HEAD > "$DOTEST"/amend
 359                warn "Stopped at $sha1... $rest"
 360                warn "You can amend the commit now, with"
 361                warn
 362                warn "  git commit --amend"
 363                warn
 364                warn "Once you are satisfied with your changes, run"
 365                warn
 366                warn "  git rebase --continue"
 367                warn
 368                exit 0
 369                ;;
 370        squash|s)
 371                comment_for_reflog squash
 372
 373                test -f "$DONE" && has_action "$DONE" ||
 374                        die "Cannot 'squash' without a previous commit"
 375
 376                mark_action_done
 377                make_squash_message $sha1 > "$MSG"
 378                failed=f
 379                author_script=$(get_author_ident_from_commit HEAD)
 380                output git reset --soft HEAD^
 381                pick_one -n $sha1 || failed=t
 382                case "$(peek_next_command)" in
 383                squash|s)
 384                        USE_OUTPUT=output
 385                        MSG_OPT=-F
 386                        EDIT_OR_FILE="$MSG"
 387                        cp "$MSG" "$SQUASH_MSG"
 388                        ;;
 389                *)
 390                        USE_OUTPUT=
 391                        MSG_OPT=
 392                        EDIT_OR_FILE=-e
 393                        rm -f "$SQUASH_MSG" || exit
 394                        cp "$MSG" "$GIT_DIR"/SQUASH_MSG
 395                        rm -f "$GIT_DIR"/MERGE_MSG || exit
 396                        ;;
 397                esac
 398                echo "$author_script" > "$DOTEST"/author-script
 399                if test $failed = f
 400                then
 401                        # This is like --amend, but with a different message
 402                        eval "$author_script"
 403                        GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 404                        GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 405                        GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 406                        $USE_OUTPUT git commit --no-verify \
 407                                $MSG_OPT "$EDIT_OR_FILE" || failed=t
 408                fi
 409                if test $failed = t
 410                then
 411                        cp "$MSG" "$GIT_DIR"/MERGE_MSG
 412                        warn
 413                        warn "Could not apply $sha1... $rest"
 414                        die_with_patch $sha1 ""
 415                fi
 416                ;;
 417        *)
 418                warn "Unknown command: $command $sha1 $rest"
 419                if git rev-parse --verify -q "$sha1" >/dev/null
 420                then
 421                        die_with_patch $sha1 "Please fix this in the file $TODO."
 422                else
 423                        die "Please fix this in the file $TODO."
 424                fi
 425                ;;
 426        esac
 427        test -s "$TODO" && return
 428
 429        comment_for_reflog finish &&
 430        HEADNAME=$(cat "$DOTEST"/head-name) &&
 431        OLDHEAD=$(cat "$DOTEST"/head) &&
 432        SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
 433        NEWHEAD=$(git rev-parse HEAD) &&
 434        case $HEADNAME in
 435        refs/*)
 436                message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
 437                git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
 438                git symbolic-ref HEAD $HEADNAME
 439                ;;
 440        esac && {
 441                test ! -f "$DOTEST"/verbose ||
 442                        git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
 443        } &&
 444        rm -rf "$DOTEST" &&
 445        git gc --auto &&
 446        warn "Successfully rebased and updated $HEADNAME."
 447
 448        exit
 449}
 450
 451do_rest () {
 452        while :
 453        do
 454                do_next
 455        done
 456}
 457
 458# skip picking commits whose parents are unchanged
 459skip_unnecessary_picks () {
 460        fd=3
 461        while read command sha1 rest
 462        do
 463                # fd=3 means we skip the command
 464                case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
 465                3,pick,"$ONTO"*|3,p,"$ONTO"*)
 466                        # pick a commit whose parent is current $ONTO -> skip
 467                        ONTO=$sha1
 468                        ;;
 469                3,#*|3,,*)
 470                        # copy comments
 471                        ;;
 472                *)
 473                        fd=1
 474                        ;;
 475                esac
 476                echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
 477        done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
 478        mv -f "$TODO".new "$TODO" ||
 479        die "Could not skip unnecessary pick commands"
 480}
 481
 482# check if no other options are set
 483is_standalone () {
 484        test $# -eq 2 -a "$2" = '--' &&
 485        test -z "$ONTO" &&
 486        test -z "$PRESERVE_MERGES" &&
 487        test -z "$STRATEGY" &&
 488        test -z "$VERBOSE"
 489}
 490
 491get_saved_options () {
 492        test -d "$REWRITTEN" && PRESERVE_MERGES=t
 493        test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
 494        test -f "$DOTEST"/verbose && VERBOSE=t
 495        test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
 496}
 497
 498LF='
 499'
 500parse_onto () {
 501        case "$1" in
 502        *...*)
 503                if      left=${1%...*} right=${1#*...} &&
 504                        onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
 505                then
 506                        case "$onto" in
 507                        ?*"$LF"?* | '')
 508                                exit 1 ;;
 509                        esac
 510                        echo "$onto"
 511                        exit 0
 512                fi
 513        esac
 514        git rev-parse --verify "$1^0"
 515}
 516
 517while test $# != 0
 518do
 519        case "$1" in
 520        --no-verify)
 521                OK_TO_SKIP_PRE_REBASE=yes
 522                ;;
 523        --verify)
 524                ;;
 525        --continue)
 526                is_standalone "$@" || usage
 527                get_saved_options
 528                comment_for_reflog continue
 529
 530                test -d "$DOTEST" || die "No interactive rebase running"
 531
 532                # Sanity check
 533                git rev-parse --verify HEAD >/dev/null ||
 534                        die "Cannot read HEAD"
 535                git update-index --ignore-submodules --refresh &&
 536                        git diff-files --quiet --ignore-submodules ||
 537                        die "Working tree is dirty"
 538
 539                # do we have anything to commit?
 540                if git diff-index --cached --quiet --ignore-submodules HEAD --
 541                then
 542                        : Nothing to commit -- skip this
 543                else
 544                        . "$DOTEST"/author-script ||
 545                                die "Cannot find the author identity"
 546                        amend=
 547                        if test -f "$DOTEST"/amend
 548                        then
 549                                amend=$(git rev-parse --verify HEAD)
 550                                test "$amend" = $(cat "$DOTEST"/amend) ||
 551                                die "\
 552You have uncommitted changes in your working tree. Please, commit them
 553first and then run 'git rebase --continue' again."
 554                                git reset --soft HEAD^ ||
 555                                die "Cannot rewind the HEAD"
 556                        fi
 557                        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
 558                        git commit --no-verify -F "$DOTEST"/message -e || {
 559                                test -n "$amend" && git reset --soft $amend
 560                                die "Could not commit staged changes."
 561                        }
 562                fi
 563
 564                require_clean_work_tree
 565                do_rest
 566                ;;
 567        --abort)
 568                is_standalone "$@" || usage
 569                get_saved_options
 570                comment_for_reflog abort
 571
 572                git rerere clear
 573                test -d "$DOTEST" || die "No interactive rebase running"
 574
 575                HEADNAME=$(cat "$DOTEST"/head-name)
 576                HEAD=$(cat "$DOTEST"/head)
 577                case $HEADNAME in
 578                refs/*)
 579                        git symbolic-ref HEAD $HEADNAME
 580                        ;;
 581                esac &&
 582                output git reset --hard $HEAD &&
 583                rm -rf "$DOTEST"
 584                exit
 585                ;;
 586        --skip)
 587                is_standalone "$@" || usage
 588                get_saved_options
 589                comment_for_reflog skip
 590
 591                git rerere clear
 592                test -d "$DOTEST" || die "No interactive rebase running"
 593
 594                output git reset --hard && do_rest
 595                ;;
 596        -s)
 597                case "$#,$1" in
 598                *,*=*)
 599                        STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
 600                1,*)
 601                        usage ;;
 602                *)
 603                        STRATEGY="-s $2"
 604                        shift ;;
 605                esac
 606                ;;
 607        -m)
 608                # we use merge anyway
 609                ;;
 610        -v)
 611                VERBOSE=t
 612                ;;
 613        -p)
 614                PRESERVE_MERGES=t
 615                ;;
 616        -i)
 617                # yeah, we know
 618                ;;
 619        --root)
 620                REBASE_ROOT=t
 621                ;;
 622        --onto)
 623                shift
 624                ONTO=$(parse_onto "$1") ||
 625                        die "Does not point to a valid commit: $1"
 626                ;;
 627        --)
 628                shift
 629                test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
 630                test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
 631                test -d "$DOTEST" &&
 632                        die "Interactive rebase already started"
 633
 634                git var GIT_COMMITTER_IDENT >/dev/null ||
 635                        die "You need to set your committer info first"
 636
 637                if test -z "$REBASE_ROOT"
 638                then
 639                        UPSTREAM_ARG="$1"
 640                        UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
 641                        test -z "$ONTO" && ONTO=$UPSTREAM
 642                        shift
 643                else
 644                        UPSTREAM=
 645                        UPSTREAM_ARG=--root
 646                        test -z "$ONTO" &&
 647                                die "You must specify --onto when using --root"
 648                fi
 649                run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
 650
 651                comment_for_reflog start
 652
 653                require_clean_work_tree
 654
 655                if test ! -z "$1"
 656                then
 657                        output git show-ref --verify --quiet "refs/heads/$1" ||
 658                                die "Invalid branchname: $1"
 659                        output git checkout "$1" ||
 660                                die "Could not checkout $1"
 661                fi
 662
 663                HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
 664                mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
 665
 666                : > "$DOTEST"/interactive || die "Could not mark as interactive"
 667                git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
 668                        echo "detached HEAD" > "$DOTEST"/head-name
 669
 670                echo $HEAD > "$DOTEST"/head
 671                case "$REBASE_ROOT" in
 672                '')
 673                        rm -f "$DOTEST"/rebase-root ;;
 674                *)
 675                        : >"$DOTEST"/rebase-root ;;
 676                esac
 677                echo $ONTO > "$DOTEST"/onto
 678                test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
 679                test t = "$VERBOSE" && : > "$DOTEST"/verbose
 680                if test t = "$PRESERVE_MERGES"
 681                then
 682                        # $REWRITTEN contains files for each commit that is
 683                        # reachable by at least one merge base of $HEAD and
 684                        # $UPSTREAM. They are not necessarily rewritten, but
 685                        # their children might be.
 686                        # This ensures that commits on merged, but otherwise
 687                        # unrelated side branches are left alone. (Think "X"
 688                        # in the man page's example.)
 689                        if test -z "$REBASE_ROOT"
 690                        then
 691                                mkdir "$REWRITTEN" &&
 692                                for c in $(git merge-base --all $HEAD $UPSTREAM)
 693                                do
 694                                        echo $ONTO > "$REWRITTEN"/$c ||
 695                                                die "Could not init rewritten commits"
 696                                done
 697                        else
 698                                mkdir "$REWRITTEN" &&
 699                                echo $ONTO > "$REWRITTEN"/root ||
 700                                        die "Could not init rewritten commits"
 701                        fi
 702                        # No cherry-pick because our first pass is to determine
 703                        # parents to rewrite and skipping dropped commits would
 704                        # prematurely end our probe
 705                        MERGES_OPTION=
 706                        first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
 707                else
 708                        MERGES_OPTION="--no-merges --cherry-pick"
 709                fi
 710
 711                SHORTHEAD=$(git rev-parse --short $HEAD)
 712                SHORTONTO=$(git rev-parse --short $ONTO)
 713                if test -z "$REBASE_ROOT"
 714                        # this is now equivalent to ! -z "$UPSTREAM"
 715                then
 716                        SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
 717                        REVISIONS=$UPSTREAM...$HEAD
 718                        SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
 719                else
 720                        REVISIONS=$ONTO...$HEAD
 721                        SHORTREVISIONS=$SHORTHEAD
 722                fi
 723                git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
 724                        --abbrev=7 --reverse --left-right --topo-order \
 725                        $REVISIONS | \
 726                        sed -n "s/^>//p" | while read shortsha1 rest
 727                do
 728                        if test t != "$PRESERVE_MERGES"
 729                        then
 730                                echo "pick $shortsha1 $rest" >> "$TODO"
 731                        else
 732                                sha1=$(git rev-parse $shortsha1)
 733                                if test -z "$REBASE_ROOT"
 734                                then
 735                                        preserve=t
 736                                        for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
 737                                        do
 738                                                if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
 739                                                then
 740                                                        preserve=f
 741                                                fi
 742                                        done
 743                                else
 744                                        preserve=f
 745                                fi
 746                                if test f = "$preserve"
 747                                then
 748                                        touch "$REWRITTEN"/$sha1
 749                                        echo "pick $shortsha1 $rest" >> "$TODO"
 750                                fi
 751                        fi
 752                done
 753
 754                # Watch for commits that been dropped by --cherry-pick
 755                if test t = "$PRESERVE_MERGES"
 756                then
 757                        mkdir "$DROPPED"
 758                        # Save all non-cherry-picked changes
 759                        git rev-list $REVISIONS --left-right --cherry-pick | \
 760                                sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
 761                        # Now all commits and note which ones are missing in
 762                        # not-cherry-picks and hence being dropped
 763                        git rev-list $REVISIONS |
 764                        while read rev
 765                        do
 766                                if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
 767                                then
 768                                        # Use -f2 because if rev-list is telling us this commit is
 769                                        # not worthwhile, we don't want to track its multiple heads,
 770                                        # just the history of its first-parent for others that will
 771                                        # be rebasing on top of it
 772                                        git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
 773                                        short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
 774                                        sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
 775                                        rm "$REWRITTEN"/$rev
 776                                fi
 777                        done
 778                fi
 779
 780                test -s "$TODO" || echo noop >> "$TODO"
 781                cat >> "$TODO" << EOF
 782
 783# Rebase $SHORTREVISIONS onto $SHORTONTO
 784#
 785# Commands:
 786#  p, pick = use commit
 787#  r, reword = use commit, but edit the commit message
 788#  e, edit = use commit, but stop for amending
 789#  s, squash = use commit, but meld into previous commit
 790#
 791# If you remove a line here THAT COMMIT WILL BE LOST.
 792# However, if you remove everything, the rebase will be aborted.
 793#
 794EOF
 795
 796                has_action "$TODO" ||
 797                        die_abort "Nothing to do"
 798
 799                cp "$TODO" "$TODO".backup
 800                git_editor "$TODO" ||
 801                        die_abort "Could not execute editor"
 802
 803                has_action "$TODO" ||
 804                        die_abort "Nothing to do"
 805
 806                test -d "$REWRITTEN" || skip_unnecessary_picks
 807
 808                git update-ref ORIG_HEAD $HEAD
 809                output git checkout $ONTO && do_rest
 810                ;;
 811        esac
 812        shift
 813done