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