git-rebase--interactive.shon commit Merge git://repo.or.cz/git-gui (731ab1f)
   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
  29"
  30
  31. git-sh-setup
  32require_work_tree
  33
  34DOTEST="$GIT_DIR/rebase-merge"
  35TODO="$DOTEST"/git-rebase-todo
  36DONE="$DOTEST"/done
  37MSG="$DOTEST"/message
  38SQUASH_MSG="$DOTEST"/message-squash
  39REWRITTEN="$DOTEST"/rewritten
  40PRESERVE_MERGES=
  41STRATEGY=
  42ONTO=
  43VERBOSE=
  44
  45GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
  46mark the corrected paths with 'git add <paths>', and
  47run 'git rebase --continue'"
  48export GIT_CHERRY_PICK_HELP
  49
  50warn () {
  51        echo "$*" >&2
  52}
  53
  54output () {
  55        case "$VERBOSE" in
  56        '')
  57                output=$("$@" 2>&1 )
  58                status=$?
  59                test $status != 0 && printf "%s\n" "$output"
  60                return $status
  61                ;;
  62        *)
  63                "$@"
  64                ;;
  65        esac
  66}
  67
  68require_clean_work_tree () {
  69        # test if working tree is dirty
  70        git rev-parse --verify HEAD > /dev/null &&
  71        git update-index --ignore-submodules --refresh &&
  72        git diff-files --quiet --ignore-submodules &&
  73        git diff-index --cached --quiet HEAD --ignore-submodules -- ||
  74        die "Working tree is dirty"
  75}
  76
  77ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
  78
  79comment_for_reflog () {
  80        case "$ORIG_REFLOG_ACTION" in
  81        ''|rebase*)
  82                GIT_REFLOG_ACTION="rebase -i ($1)"
  83                export GIT_REFLOG_ACTION
  84                ;;
  85        esac
  86}
  87
  88last_count=
  89mark_action_done () {
  90        sed -e 1q < "$TODO" >> "$DONE"
  91        sed -e 1d < "$TODO" >> "$TODO".new
  92        mv -f "$TODO".new "$TODO"
  93        count=$(grep -c '^[^#]' < "$DONE")
  94        total=$(($count+$(grep -c '^[^#]' < "$TODO")))
  95        if test "$last_count" != "$count"
  96        then
  97                last_count=$count
  98                printf "Rebasing (%d/%d)\r" $count $total
  99                test -z "$VERBOSE" || echo
 100        fi
 101}
 102
 103make_patch () {
 104        parent_sha1=$(git rev-parse --verify "$1"^) ||
 105                die "Cannot get patch for $1^"
 106        git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
 107        test -f "$DOTEST"/message ||
 108                git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
 109        test -f "$DOTEST"/author-script ||
 110                get_author_ident_from_commit "$1" > "$DOTEST"/author-script
 111}
 112
 113die_with_patch () {
 114        make_patch "$1"
 115        git rerere
 116        die "$2"
 117}
 118
 119die_abort () {
 120        rm -rf "$DOTEST"
 121        die "$1"
 122}
 123
 124has_action () {
 125        grep '^[^#]' "$1" >/dev/null
 126}
 127
 128pick_one () {
 129        no_ff=
 130        case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
 131        output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
 132        test -d "$REWRITTEN" &&
 133                pick_one_preserving_merges "$@" && return
 134        parent_sha1=$(git rev-parse --verify $sha1^) ||
 135                die "Could not get the parent of $sha1"
 136        current_sha1=$(git rev-parse --verify HEAD)
 137        if test "$no_ff$current_sha1" = "$parent_sha1"; then
 138                output git reset --hard $sha1
 139                test "a$1" = a-n && output git reset --soft $current_sha1
 140                sha1=$(git rev-parse --short $sha1)
 141                output warn Fast forward to $sha1
 142        else
 143                output git cherry-pick "$@"
 144        fi
 145}
 146
 147pick_one_preserving_merges () {
 148        case "$1" in -n) sha1=$2 ;; *) sha1=$1 ;; esac
 149        sha1=$(git rev-parse $sha1)
 150
 151        if test -f "$DOTEST"/current-commit
 152        then
 153                current_commit=$(cat "$DOTEST"/current-commit) &&
 154                git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
 155                rm "$DOTEST"/current-commit ||
 156                die "Cannot write current commit's replacement sha1"
 157        fi
 158
 159        # rewrite parents; if none were rewritten, we can fast-forward.
 160        fast_forward=t
 161        preserve=t
 162        new_parents=
 163        for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
 164        do
 165                if test -f "$REWRITTEN"/$p
 166                then
 167                        preserve=f
 168                        new_p=$(cat "$REWRITTEN"/$p)
 169                        test $p != $new_p && fast_forward=f
 170                        case "$new_parents" in
 171                        *$new_p*)
 172                                ;; # do nothing; that parent is already there
 173                        *)
 174                                new_parents="$new_parents $new_p"
 175                                ;;
 176                        esac
 177                else
 178                        new_parents="$new_parents $p"
 179                fi
 180        done
 181        case $fast_forward in
 182        t)
 183                output warn "Fast forward to $sha1"
 184                test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
 185                ;;
 186        f)
 187                test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 188
 189                first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 190                # detach HEAD to current parent
 191                output git checkout $first_parent 2> /dev/null ||
 192                        die "Cannot move HEAD to $first_parent"
 193
 194                echo $sha1 > "$DOTEST"/current-commit
 195                case "$new_parents" in
 196                ' '*' '*)
 197                        # redo merge
 198                        author_script=$(get_author_ident_from_commit $sha1)
 199                        eval "$author_script"
 200                        msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
 201                        # No point in merging the first parent, that's HEAD
 202                        new_parents=${new_parents# $first_parent}
 203                        if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 204                                GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 205                                GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 206                                output git merge $STRATEGY -m "$msg" \
 207                                        $new_parents
 208                        then
 209                                git rerere
 210                                printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
 211                                die Error redoing merge $sha1
 212                        fi
 213                        ;;
 214                *)
 215                        output git cherry-pick "$@" ||
 216                                die_with_patch $sha1 "Could not pick $sha1"
 217                        ;;
 218                esac
 219                ;;
 220        esac
 221}
 222
 223nth_string () {
 224        case "$1" in
 225        *1[0-9]|*[04-9]) echo "$1"th;;
 226        *1) echo "$1"st;;
 227        *2) echo "$1"nd;;
 228        *3) echo "$1"rd;;
 229        esac
 230}
 231
 232make_squash_message () {
 233        if test -f "$SQUASH_MSG"; then
 234                COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
 235                        < "$SQUASH_MSG" | sed -ne '$p')+1))
 236                echo "# This is a combination of $COUNT commits."
 237                sed -e 1d -e '2,/^./{
 238                        /^$/d
 239                }' <"$SQUASH_MSG"
 240        else
 241                COUNT=2
 242                echo "# This is a combination of two commits."
 243                echo "# The first commit's message is:"
 244                echo
 245                git cat-file commit HEAD | sed -e '1,/^$/d'
 246        fi
 247        echo
 248        echo "# This is the $(nth_string $COUNT) commit message:"
 249        echo
 250        git cat-file commit $1 | sed -e '1,/^$/d'
 251}
 252
 253peek_next_command () {
 254        sed -n "1s/ .*$//p" < "$TODO"
 255}
 256
 257do_next () {
 258        rm -f "$DOTEST"/message "$DOTEST"/author-script \
 259                "$DOTEST"/amend || exit
 260        read command sha1 rest < "$TODO"
 261        case "$command" in
 262        '#'*|'')
 263                mark_action_done
 264                ;;
 265        pick|p)
 266                comment_for_reflog pick
 267
 268                mark_action_done
 269                pick_one $sha1 ||
 270                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 271                ;;
 272        edit|e)
 273                comment_for_reflog edit
 274
 275                mark_action_done
 276                pick_one $sha1 ||
 277                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 278                make_patch $sha1
 279                : > "$DOTEST"/amend
 280                warn "Stopped at $sha1... $rest"
 281                warn "You can amend the commit now, with"
 282                warn
 283                warn "  git commit --amend"
 284                warn
 285                warn "Once you are satisfied with your changes, run"
 286                warn
 287                warn "  git rebase --continue"
 288                warn
 289                exit 0
 290                ;;
 291        squash|s)
 292                comment_for_reflog squash
 293
 294                has_action "$DONE" ||
 295                        die "Cannot 'squash' without a previous commit"
 296
 297                mark_action_done
 298                make_squash_message $sha1 > "$MSG"
 299                case "$(peek_next_command)" in
 300                squash|s)
 301                        EDIT_COMMIT=
 302                        USE_OUTPUT=output
 303                        cp "$MSG" "$SQUASH_MSG"
 304                        ;;
 305                *)
 306                        EDIT_COMMIT=-e
 307                        USE_OUTPUT=
 308                        rm -f "$SQUASH_MSG" || exit
 309                        ;;
 310                esac
 311
 312                failed=f
 313                author_script=$(get_author_ident_from_commit HEAD)
 314                output git reset --soft HEAD^
 315                pick_one -n $sha1 || failed=t
 316                echo "$author_script" > "$DOTEST"/author-script
 317                if test $failed = f
 318                then
 319                        # This is like --amend, but with a different message
 320                        eval "$author_script"
 321                        GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 322                        GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 323                        GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 324                        $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
 325                fi
 326                if test $failed = t
 327                then
 328                        cp "$MSG" "$GIT_DIR"/MERGE_MSG
 329                        warn
 330                        warn "Could not apply $sha1... $rest"
 331                        die_with_patch $sha1 ""
 332                fi
 333                ;;
 334        *)
 335                warn "Unknown command: $command $sha1 $rest"
 336                die_with_patch $sha1 "Please fix this in the file $TODO."
 337                ;;
 338        esac
 339        test -s "$TODO" && return
 340
 341        comment_for_reflog finish &&
 342        HEADNAME=$(cat "$DOTEST"/head-name) &&
 343        OLDHEAD=$(cat "$DOTEST"/head) &&
 344        SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
 345        if test -d "$REWRITTEN"
 346        then
 347                test -f "$DOTEST"/current-commit &&
 348                        current_commit=$(cat "$DOTEST"/current-commit) &&
 349                        git rev-parse HEAD > "$REWRITTEN"/$current_commit
 350                if test -f "$REWRITTEN"/$OLDHEAD
 351                then
 352                        NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
 353                else
 354                        NEWHEAD=$OLDHEAD
 355                fi
 356        else
 357                NEWHEAD=$(git rev-parse HEAD)
 358        fi &&
 359        case $HEADNAME in
 360        refs/*)
 361                message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
 362                git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
 363                git symbolic-ref HEAD $HEADNAME
 364                ;;
 365        esac && {
 366                test ! -f "$DOTEST"/verbose ||
 367                        git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
 368        } &&
 369        rm -rf "$DOTEST" &&
 370        git gc --auto &&
 371        warn "Successfully rebased and updated $HEADNAME."
 372
 373        exit
 374}
 375
 376do_rest () {
 377        while :
 378        do
 379                do_next
 380        done
 381}
 382
 383# check if no other options are set
 384is_standalone () {
 385        test $# -eq 2 -a "$2" = '--' &&
 386        test -z "$ONTO" &&
 387        test -z "$PRESERVE_MERGES" &&
 388        test -z "$STRATEGY" &&
 389        test -z "$VERBOSE"
 390}
 391
 392get_saved_options () {
 393        test -d "$REWRITTEN" && PRESERVE_MERGES=t
 394        test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
 395        test -f "$DOTEST"/verbose && VERBOSE=t
 396}
 397
 398while test $# != 0
 399do
 400        case "$1" in
 401        --continue)
 402                is_standalone "$@" || usage
 403                get_saved_options
 404                comment_for_reflog continue
 405
 406                test -d "$DOTEST" || die "No interactive rebase running"
 407
 408                # Sanity check
 409                git rev-parse --verify HEAD >/dev/null ||
 410                        die "Cannot read HEAD"
 411                git update-index --ignore-submodules --refresh &&
 412                        git diff-files --quiet --ignore-submodules ||
 413                        die "Working tree is dirty"
 414
 415                # do we have anything to commit?
 416                if git diff-index --cached --quiet --ignore-submodules HEAD --
 417                then
 418                        : Nothing to commit -- skip this
 419                else
 420                        . "$DOTEST"/author-script ||
 421                                die "Cannot find the author identity"
 422                        if test -f "$DOTEST"/amend
 423                        then
 424                                git reset --soft HEAD^ ||
 425                                die "Cannot rewind the HEAD"
 426                        fi
 427                        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
 428                        git commit --no-verify -F "$DOTEST"/message -e ||
 429                        die "Could not commit staged changes."
 430                fi
 431
 432                require_clean_work_tree
 433                do_rest
 434                ;;
 435        --abort)
 436                is_standalone "$@" || usage
 437                get_saved_options
 438                comment_for_reflog abort
 439
 440                git rerere clear
 441                test -d "$DOTEST" || die "No interactive rebase running"
 442
 443                HEADNAME=$(cat "$DOTEST"/head-name)
 444                HEAD=$(cat "$DOTEST"/head)
 445                case $HEADNAME in
 446                refs/*)
 447                        git symbolic-ref HEAD $HEADNAME
 448                        ;;
 449                esac &&
 450                output git reset --hard $HEAD &&
 451                rm -rf "$DOTEST"
 452                exit
 453                ;;
 454        --skip)
 455                is_standalone "$@" || usage
 456                get_saved_options
 457                comment_for_reflog skip
 458
 459                git rerere clear
 460                test -d "$DOTEST" || die "No interactive rebase running"
 461
 462                output git reset --hard && do_rest
 463                ;;
 464        -s)
 465                case "$#,$1" in
 466                *,*=*)
 467                        STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
 468                1,*)
 469                        usage ;;
 470                *)
 471                        STRATEGY="-s $2"
 472                        shift ;;
 473                esac
 474                ;;
 475        -m)
 476                # we use merge anyway
 477                ;;
 478        -v)
 479                VERBOSE=t
 480                ;;
 481        -p)
 482                PRESERVE_MERGES=t
 483                ;;
 484        -i)
 485                # yeah, we know
 486                ;;
 487        --onto)
 488                shift
 489                ONTO=$(git rev-parse --verify "$1") ||
 490                        die "Does not point to a valid commit: $1"
 491                ;;
 492        --)
 493                shift
 494                test $# -eq 1 -o $# -eq 2 || usage
 495                test -d "$DOTEST" &&
 496                        die "Interactive rebase already started"
 497
 498                git var GIT_COMMITTER_IDENT >/dev/null ||
 499                        die "You need to set your committer info first"
 500
 501                comment_for_reflog start
 502
 503                require_clean_work_tree
 504
 505                UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
 506                test -z "$ONTO" && ONTO=$UPSTREAM
 507
 508                if test ! -z "$2"
 509                then
 510                        output git show-ref --verify --quiet "refs/heads/$2" ||
 511                                die "Invalid branchname: $2"
 512                        output git checkout "$2" ||
 513                                die "Could not checkout $2"
 514                fi
 515
 516                HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
 517                mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
 518
 519                : > "$DOTEST"/interactive || die "Could not mark as interactive"
 520                git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
 521                        echo "detached HEAD" > "$DOTEST"/head-name
 522
 523                echo $HEAD > "$DOTEST"/head
 524                echo $UPSTREAM > "$DOTEST"/upstream
 525                echo $ONTO > "$DOTEST"/onto
 526                test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
 527                test t = "$VERBOSE" && : > "$DOTEST"/verbose
 528                if test t = "$PRESERVE_MERGES"
 529                then
 530                        # $REWRITTEN contains files for each commit that is
 531                        # reachable by at least one merge base of $HEAD and
 532                        # $UPSTREAM. They are not necessarily rewritten, but
 533                        # their children might be.
 534                        # This ensures that commits on merged, but otherwise
 535                        # unrelated side branches are left alone. (Think "X"
 536                        # in the man page's example.)
 537                        mkdir "$REWRITTEN" &&
 538                        for c in $(git merge-base --all $HEAD $UPSTREAM)
 539                        do
 540                                echo $ONTO > "$REWRITTEN"/$c ||
 541                                        die "Could not init rewritten commits"
 542                        done
 543                        MERGES_OPTION=
 544                else
 545                        MERGES_OPTION=--no-merges
 546                fi
 547
 548                SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
 549                SHORTHEAD=$(git rev-parse --short $HEAD)
 550                SHORTONTO=$(git rev-parse --short $ONTO)
 551                git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
 552                        --abbrev=7 --reverse --left-right --cherry-pick \
 553                        $UPSTREAM...$HEAD | \
 554                        sed -n "s/^>/pick /p" > "$TODO"
 555                cat >> "$TODO" << EOF
 556
 557# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
 558#
 559# Commands:
 560#  p, pick = use commit
 561#  e, edit = use commit, but stop for amending
 562#  s, squash = use commit, but meld into previous commit
 563#
 564# If you remove a line here THAT COMMIT WILL BE LOST.
 565# However, if you remove everything, the rebase will be aborted.
 566#
 567EOF
 568
 569                has_action "$TODO" ||
 570                        die_abort "Nothing to do"
 571
 572                cp "$TODO" "$TODO".backup
 573                git_editor "$TODO" ||
 574                        die "Could not execute editor"
 575
 576                has_action "$TODO" ||
 577                        die_abort "Nothing to do"
 578
 579                git update-ref ORIG_HEAD $HEAD
 580                output git checkout $ONTO && do_rest
 581                ;;
 582        esac
 583        shift
 584done