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