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