git-rebase--interactive.shon commit Merge branch 'ls/maint-mailinfo-patch-label' (abce32b)
   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/.dotest-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                fi
 178        done
 179        case $fast_forward in
 180        t)
 181                output warn "Fast forward to $sha1"
 182                test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
 183                ;;
 184        f)
 185                test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 186
 187                first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 188                # detach HEAD to current parent
 189                output git checkout $first_parent 2> /dev/null ||
 190                        die "Cannot move HEAD to $first_parent"
 191
 192                echo $sha1 > "$DOTEST"/current-commit
 193                case "$new_parents" in
 194                ' '*' '*)
 195                        # redo merge
 196                        author_script=$(get_author_ident_from_commit $sha1)
 197                        eval "$author_script"
 198                        msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
 199                        # No point in merging the first parent, that's HEAD
 200                        new_parents=${new_parents# $first_parent}
 201                        if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 202                                GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 203                                GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 204                                output git merge $STRATEGY -m "$msg" \
 205                                        $new_parents
 206                        then
 207                                git rerere
 208                                printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
 209                                die Error redoing merge $sha1
 210                        fi
 211                        ;;
 212                *)
 213                        output git cherry-pick "$@" ||
 214                                die_with_patch $sha1 "Could not pick $sha1"
 215                        ;;
 216                esac
 217                ;;
 218        esac
 219}
 220
 221nth_string () {
 222        case "$1" in
 223        *1[0-9]|*[04-9]) echo "$1"th;;
 224        *1) echo "$1"st;;
 225        *2) echo "$1"nd;;
 226        *3) echo "$1"rd;;
 227        esac
 228}
 229
 230make_squash_message () {
 231        if test -f "$SQUASH_MSG"; then
 232                COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
 233                        < "$SQUASH_MSG" | sed -ne '$p')+1))
 234                echo "# This is a combination of $COUNT commits."
 235                sed -e 1d -e '2,/^./{
 236                        /^$/d
 237                }' <"$SQUASH_MSG"
 238        else
 239                COUNT=2
 240                echo "# This is a combination of two commits."
 241                echo "# The first commit's message is:"
 242                echo
 243                git cat-file commit HEAD | sed -e '1,/^$/d'
 244        fi
 245        echo
 246        echo "# This is the $(nth_string $COUNT) commit message:"
 247        echo
 248        git cat-file commit $1 | sed -e '1,/^$/d'
 249}
 250
 251peek_next_command () {
 252        sed -n "1s/ .*$//p" < "$TODO"
 253}
 254
 255do_next () {
 256        rm -f "$DOTEST"/message "$DOTEST"/author-script \
 257                "$DOTEST"/amend || exit
 258        read command sha1 rest < "$TODO"
 259        case "$command" in
 260        '#'*|'')
 261                mark_action_done
 262                ;;
 263        pick|p)
 264                comment_for_reflog pick
 265
 266                mark_action_done
 267                pick_one $sha1 ||
 268                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 269                ;;
 270        edit|e)
 271                comment_for_reflog edit
 272
 273                mark_action_done
 274                pick_one $sha1 ||
 275                        die_with_patch $sha1 "Could not apply $sha1... $rest"
 276                make_patch $sha1
 277                : > "$DOTEST"/amend
 278                warn
 279                warn "You can amend the commit now, with"
 280                warn
 281                warn "  git commit --amend"
 282                warn
 283                warn "Once you are satisfied with your changes, run"
 284                warn
 285                warn "  git rebase --continue"
 286                warn
 287                exit 0
 288                ;;
 289        squash|s)
 290                comment_for_reflog squash
 291
 292                has_action "$DONE" ||
 293                        die "Cannot 'squash' without a previous commit"
 294
 295                mark_action_done
 296                make_squash_message $sha1 > "$MSG"
 297                case "$(peek_next_command)" in
 298                squash|s)
 299                        EDIT_COMMIT=
 300                        USE_OUTPUT=output
 301                        cp "$MSG" "$SQUASH_MSG"
 302                        ;;
 303                *)
 304                        EDIT_COMMIT=-e
 305                        USE_OUTPUT=
 306                        rm -f "$SQUASH_MSG" || exit
 307                        ;;
 308                esac
 309
 310                failed=f
 311                author_script=$(get_author_ident_from_commit HEAD)
 312                output git reset --soft HEAD^
 313                pick_one -n $sha1 || failed=t
 314                echo "$author_script" > "$DOTEST"/author-script
 315                if test $failed = f
 316                then
 317                        # This is like --amend, but with a different message
 318                        eval "$author_script"
 319                        GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 320                        GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
 321                        GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
 322                        $USE_OUTPUT git commit --no-verify -F "$MSG" $EDIT_COMMIT || failed=t
 323                fi
 324                if test $failed = t
 325                then
 326                        cp "$MSG" "$GIT_DIR"/MERGE_MSG
 327                        warn
 328                        warn "Could not apply $sha1... $rest"
 329                        die_with_patch $sha1 ""
 330                fi
 331                ;;
 332        *)
 333                warn "Unknown command: $command $sha1 $rest"
 334                die_with_patch $sha1 "Please fix this in the file $TODO."
 335                ;;
 336        esac
 337        test -s "$TODO" && return
 338
 339        comment_for_reflog finish &&
 340        HEADNAME=$(cat "$DOTEST"/head-name) &&
 341        OLDHEAD=$(cat "$DOTEST"/head) &&
 342        SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
 343        if test -d "$REWRITTEN"
 344        then
 345                test -f "$DOTEST"/current-commit &&
 346                        current_commit=$(cat "$DOTEST"/current-commit) &&
 347                        git rev-parse HEAD > "$REWRITTEN"/$current_commit
 348                if test -f "$REWRITTEN"/$OLDHEAD
 349                then
 350                        NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
 351                else
 352                        NEWHEAD=$OLDHEAD
 353                fi
 354        else
 355                NEWHEAD=$(git rev-parse HEAD)
 356        fi &&
 357        case $HEADNAME in
 358        refs/*)
 359                message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
 360                git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
 361                git symbolic-ref HEAD $HEADNAME
 362                ;;
 363        esac && {
 364                test ! -f "$DOTEST"/verbose ||
 365                        git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
 366        } &&
 367        rm -rf "$DOTEST" &&
 368        git gc --auto &&
 369        warn "Successfully rebased and updated $HEADNAME."
 370
 371        exit
 372}
 373
 374do_rest () {
 375        while :
 376        do
 377                do_next
 378        done
 379}
 380
 381# check if no other options are set
 382is_standalone () {
 383        test $# -eq 2 -a "$2" = '--' &&
 384        test -z "$ONTO" &&
 385        test -z "$PRESERVE_MERGES" &&
 386        test -z "$STRATEGY" &&
 387        test -z "$VERBOSE"
 388}
 389
 390get_saved_options () {
 391        test -d "$REWRITTEN" && PRESERVE_MERGES=t
 392        test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
 393        test -f "$DOTEST"/verbose && VERBOSE=t
 394}
 395
 396while test $# != 0
 397do
 398        case "$1" in
 399        --continue)
 400                is_standalone "$@" || usage
 401                get_saved_options
 402                comment_for_reflog continue
 403
 404                test -d "$DOTEST" || die "No interactive rebase running"
 405
 406                # Sanity check
 407                git rev-parse --verify HEAD >/dev/null ||
 408                        die "Cannot read HEAD"
 409                git update-index --ignore-submodules --refresh &&
 410                        git diff-files --quiet --ignore-submodules ||
 411                        die "Working tree is dirty"
 412
 413                # do we have anything to commit?
 414                if git diff-index --cached --quiet --ignore-submodules HEAD --
 415                then
 416                        : Nothing to commit -- skip this
 417                else
 418                        . "$DOTEST"/author-script ||
 419                                die "Cannot find the author identity"
 420                        if test -f "$DOTEST"/amend
 421                        then
 422                                git reset --soft HEAD^ ||
 423                                die "Cannot rewind the HEAD"
 424                        fi
 425                        export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
 426                        git commit --no-verify -F "$DOTEST"/message -e ||
 427                        die "Could not commit staged changes."
 428                fi
 429
 430                require_clean_work_tree
 431                do_rest
 432                ;;
 433        --abort)
 434                is_standalone "$@" || usage
 435                get_saved_options
 436                comment_for_reflog abort
 437
 438                git rerere clear
 439                test -d "$DOTEST" || die "No interactive rebase running"
 440
 441                HEADNAME=$(cat "$DOTEST"/head-name)
 442                HEAD=$(cat "$DOTEST"/head)
 443                case $HEADNAME in
 444                refs/*)
 445                        git symbolic-ref HEAD $HEADNAME
 446                        ;;
 447                esac &&
 448                output git reset --hard $HEAD &&
 449                rm -rf "$DOTEST"
 450                exit
 451                ;;
 452        --skip)
 453                is_standalone "$@" || usage
 454                get_saved_options
 455                comment_for_reflog skip
 456
 457                git rerere clear
 458                test -d "$DOTEST" || die "No interactive rebase running"
 459
 460                output git reset --hard && do_rest
 461                ;;
 462        -s)
 463                case "$#,$1" in
 464                *,*=*)
 465                        STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
 466                1,*)
 467                        usage ;;
 468                *)
 469                        STRATEGY="-s $2"
 470                        shift ;;
 471                esac
 472                ;;
 473        -m)
 474                # we use merge anyway
 475                ;;
 476        -v)
 477                VERBOSE=t
 478                ;;
 479        -p)
 480                PRESERVE_MERGES=t
 481                ;;
 482        -i)
 483                # yeah, we know
 484                ;;
 485        --onto)
 486                shift
 487                ONTO=$(git rev-parse --verify "$1") ||
 488                        die "Does not point to a valid commit: $1"
 489                ;;
 490        --)
 491                shift
 492                test $# -eq 1 -o $# -eq 2 || usage
 493                test -d "$DOTEST" &&
 494                        die "Interactive rebase already started"
 495
 496                git var GIT_COMMITTER_IDENT >/dev/null ||
 497                        die "You need to set your committer info first"
 498
 499                comment_for_reflog start
 500
 501                require_clean_work_tree
 502
 503                UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
 504                test -z "$ONTO" && ONTO=$UPSTREAM
 505
 506                if test ! -z "$2"
 507                then
 508                        output git show-ref --verify --quiet "refs/heads/$2" ||
 509                                die "Invalid branchname: $2"
 510                        output git checkout "$2" ||
 511                                die "Could not checkout $2"
 512                fi
 513
 514                HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
 515                mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
 516
 517                : > "$DOTEST"/interactive || die "Could not mark as interactive"
 518                git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
 519                        echo "detached HEAD" > "$DOTEST"/head-name
 520
 521                echo $HEAD > "$DOTEST"/head
 522                echo $UPSTREAM > "$DOTEST"/upstream
 523                echo $ONTO > "$DOTEST"/onto
 524                test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
 525                test t = "$VERBOSE" && : > "$DOTEST"/verbose
 526                if test t = "$PRESERVE_MERGES"
 527                then
 528                        # $REWRITTEN contains files for each commit that is
 529                        # reachable by at least one merge base of $HEAD and
 530                        # $UPSTREAM. They are not necessarily rewritten, but
 531                        # their children might be.
 532                        # This ensures that commits on merged, but otherwise
 533                        # unrelated side branches are left alone. (Think "X"
 534                        # in the man page's example.)
 535                        mkdir "$REWRITTEN" &&
 536                        for c in $(git merge-base --all $HEAD $UPSTREAM)
 537                        do
 538                                echo $ONTO > "$REWRITTEN"/$c ||
 539                                        die "Could not init rewritten commits"
 540                        done
 541                        MERGES_OPTION=
 542                else
 543                        MERGES_OPTION=--no-merges
 544                fi
 545
 546                SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
 547                SHORTHEAD=$(git rev-parse --short $HEAD)
 548                SHORTONTO=$(git rev-parse --short $ONTO)
 549                git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
 550                        --abbrev=7 --reverse --left-right --cherry-pick \
 551                        $UPSTREAM...$HEAD | \
 552                        sed -n "s/^>/pick /p" > "$TODO"
 553                cat >> "$TODO" << EOF
 554
 555# Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
 556#
 557# Commands:
 558#  p, pick = use commit
 559#  e, edit = use commit, but stop for amending
 560#  s, squash = use commit, but meld into previous commit
 561#
 562# If you remove a line here THAT COMMIT WILL BE LOST.
 563# However, if you remove everything, the rebase will be aborted.
 564#
 565EOF
 566
 567                has_action "$TODO" ||
 568                        die_abort "Nothing to do"
 569
 570                cp "$TODO" "$TODO".backup
 571                git_editor "$TODO" ||
 572                        die "Could not execute editor"
 573
 574                has_action "$TODO" ||
 575                        die_abort "Nothing to do"
 576
 577                git update-ref ORIG_HEAD $HEAD
 578                output git checkout $ONTO && do_rest
 579                ;;
 580        esac
 581        shift
 582done