70948bcd23c37c66545e3568e50e58f0a35d9da0
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano.
   4#
   5
   6USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] (<upstream>|--root) [<branch>] [--quiet | -q]'
   7LONG_USAGE='git-rebase replaces <branch> with a new branch of the
   8same name.  When the --onto option is provided the new branch starts
   9out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
  10It then attempts to create a new commit for each commit from the original
  11<branch> that does not exist in the <upstream> branch.
  12
  13It is possible that a merge failure will prevent this process from being
  14completely automatic.  You will have to resolve any such merge failure
  15and run git rebase --continue.  Another option is to bypass the commit
  16that caused the merge failure with git rebase --skip.  To restore the
  17original <branch> and remove the .git/rebase-apply working files, use the
  18command git rebase --abort instead.
  19
  20Note that if <branch> is not specified on the command line, the
  21currently checked out branch is used.
  22
  23Example:       git-rebase master~1 topic
  24
  25        A---B---C topic                   A'\''--B'\''--C'\'' topic
  26       /                   -->           /
  27  D---E---F---G master          D---E---F---G master
  28'
  29
  30SUBDIRECTORY_OK=Yes
  31OPTIONS_SPEC=
  32. git-sh-setup
  33set_reflog_action rebase
  34require_work_tree
  35cd_to_toplevel
  36
  37LF='
  38'
  39OK_TO_SKIP_PRE_REBASE=
  40RESOLVEMSG="
  41When you have resolved this problem run \"git rebase --continue\".
  42If you would prefer to skip this patch, instead run \"git rebase --skip\".
  43To restore the original branch and stop rebasing run \"git rebase --abort\".
  44"
  45unset newbase
  46strategy=recursive
  47strategy_opts=
  48do_merge=
  49merge_dir="$GIT_DIR"/rebase-merge
  50apply_dir="$GIT_DIR"/rebase-apply
  51prec=4
  52verbose=
  53diffstat=
  54test "$(git config --bool rebase.stat)" = true && diffstat=t
  55git_am_opt=
  56rebase_root=
  57force_rebase=
  58allow_rerere_autoupdate=
  59
  60read_state () {
  61        if test -d "$merge_dir"
  62        then
  63                state_dir="$merge_dir"
  64                prev_head=$(cat "$merge_dir"/prev_head) &&
  65                onto_name=$(cat "$merge_dir"/onto_name) &&
  66                end=$(cat "$merge_dir"/end) &&
  67                msgnum=$(cat "$merge_dir"/msgnum)
  68        else
  69                state_dir="$apply_dir"
  70        fi &&
  71        head_name=$(cat "$state_dir"/head-name) &&
  72        onto=$(cat "$state_dir"/onto) &&
  73        orig_head=$(cat "$state_dir"/orig-head) &&
  74        GIT_QUIET=$(cat "$state_dir"/quiet)
  75}
  76
  77continue_merge () {
  78        test -n "$prev_head" || die "prev_head must be defined"
  79        test -d "$merge_dir" || die "$merge_dir directory does not exist"
  80
  81        unmerged=$(git ls-files -u)
  82        if test -n "$unmerged"
  83        then
  84                echo "You still have unmerged paths in your index"
  85                echo "did you forget to use git add?"
  86                die "$RESOLVEMSG"
  87        fi
  88
  89        cmt=`cat "$merge_dir/current"`
  90        if ! git diff-index --quiet --ignore-submodules HEAD --
  91        then
  92                if ! git commit --no-verify -C "$cmt"
  93                then
  94                        echo "Commit failed, please do not call \"git commit\""
  95                        echo "directly, but instead do one of the following: "
  96                        die "$RESOLVEMSG"
  97                fi
  98                if test -z "$GIT_QUIET"
  99                then
 100                        printf "Committed: %0${prec}d " $msgnum
 101                fi
 102                echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
 103        else
 104                if test -z "$GIT_QUIET"
 105                then
 106                        printf "Already applied: %0${prec}d " $msgnum
 107                fi
 108        fi
 109        test -z "$GIT_QUIET" &&
 110        GIT_PAGER='' git log --format=%s -1 "$cmt"
 111
 112        prev_head=`git rev-parse HEAD^0`
 113        # save the resulting commit so we can read-tree on it later
 114        echo "$prev_head" > "$merge_dir/prev_head"
 115
 116        # onto the next patch:
 117        msgnum=$(($msgnum + 1))
 118        echo "$msgnum" >"$merge_dir/msgnum"
 119}
 120
 121call_merge () {
 122        cmt="$(cat "$merge_dir/cmt.$1")"
 123        echo "$cmt" > "$merge_dir/current"
 124        hd=$(git rev-parse --verify HEAD)
 125        cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
 126        msgnum=$(cat "$merge_dir/msgnum")
 127        eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
 128        eval GITHEAD_$hd='$onto_name'
 129        export GITHEAD_$cmt GITHEAD_$hd
 130        if test -n "$GIT_QUIET"
 131        then
 132                GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
 133        fi
 134        eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
 135        rv=$?
 136        case "$rv" in
 137        0)
 138                unset GITHEAD_$cmt GITHEAD_$hd
 139                return
 140                ;;
 141        1)
 142                git rerere $allow_rerere_autoupdate
 143                die "$RESOLVEMSG"
 144                ;;
 145        2)
 146                echo "Strategy: $rv $strategy failed, try another" 1>&2
 147                die "$RESOLVEMSG"
 148                ;;
 149        *)
 150                die "Unknown exit code ($rv) from command:" \
 151                        "git-merge-$strategy $cmt^ -- HEAD $cmt"
 152                ;;
 153        esac
 154}
 155
 156move_to_original_branch () {
 157        case "$head_name" in
 158        refs/*)
 159                message="rebase finished: $head_name onto $onto"
 160                git update-ref -m "$message" \
 161                        $head_name $(git rev-parse HEAD) $orig_head &&
 162                git symbolic-ref HEAD $head_name ||
 163                die "Could not move back to $head_name"
 164                ;;
 165        esac
 166}
 167
 168finish_rb_merge () {
 169        move_to_original_branch
 170        git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
 171        if test -x "$GIT_DIR"/hooks/post-rewrite &&
 172                test -s "$merge_dir"/rewritten; then
 173                "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
 174        fi
 175        rm -r "$merge_dir"
 176        say All done.
 177}
 178
 179is_interactive () {
 180        while test $# != 0
 181        do
 182                case "$1" in
 183                        -i|--interactive)
 184                                interactive_rebase=explicit
 185                                break
 186                        ;;
 187                        -p|--preserve-merges)
 188                                interactive_rebase=implied
 189                        ;;
 190                esac
 191                shift
 192        done
 193
 194        if [ "$interactive_rebase" = implied ]; then
 195                GIT_EDITOR=:
 196                export GIT_EDITOR
 197        fi
 198
 199        test -n "$interactive_rebase" || test -f "$merge_dir"/interactive
 200}
 201
 202run_pre_rebase_hook () {
 203        if test -z "$OK_TO_SKIP_PRE_REBASE" &&
 204           test -x "$GIT_DIR/hooks/pre-rebase"
 205        then
 206                "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
 207                die "The pre-rebase hook refused to rebase."
 208        fi
 209}
 210
 211test -f "$apply_dir"/applying &&
 212        die 'It looks like git-am is in progress. Cannot rebase.'
 213
 214is_interactive "$@" && exec git-rebase--interactive "$@"
 215
 216while test $# != 0
 217do
 218        case "$1" in
 219        --no-verify)
 220                OK_TO_SKIP_PRE_REBASE=yes
 221                ;;
 222        --verify)
 223                OK_TO_SKIP_PRE_REBASE=
 224                ;;
 225        --continue)
 226                test -d "$merge_dir" -o -d "$apply_dir" ||
 227                        die "No rebase in progress?"
 228
 229                git update-index --ignore-submodules --refresh &&
 230                git diff-files --quiet --ignore-submodules || {
 231                        echo "You must edit all merge conflicts and then"
 232                        echo "mark them as resolved using git add"
 233                        exit 1
 234                }
 235                read_state
 236                if test -d "$merge_dir"
 237                then
 238                        continue_merge
 239                        while test "$msgnum" -le "$end"
 240                        do
 241                                call_merge "$msgnum"
 242                                continue_merge
 243                        done
 244                        finish_rb_merge
 245                        exit
 246                fi
 247                git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
 248                move_to_original_branch
 249                exit
 250                ;;
 251        --skip)
 252                test -d "$merge_dir" -o -d "$apply_dir" ||
 253                        die "No rebase in progress?"
 254
 255                git reset --hard HEAD || exit $?
 256                read_state
 257                if test -d "$merge_dir"
 258                then
 259                        git rerere clear
 260                        msgnum=$(($msgnum + 1))
 261                        while test "$msgnum" -le "$end"
 262                        do
 263                                call_merge "$msgnum"
 264                                continue_merge
 265                        done
 266                        finish_rb_merge
 267                        exit
 268                fi
 269                git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
 270                move_to_original_branch
 271                exit
 272                ;;
 273        --abort)
 274                test -d "$merge_dir" -o -d "$apply_dir" ||
 275                        die "No rebase in progress?"
 276
 277                git rerere clear
 278                read_state
 279                case "$head_name" in
 280                refs/*)
 281                        git symbolic-ref HEAD $head_name ||
 282                        die "Could not move back to $head_name"
 283                        ;;
 284                esac
 285                git reset --hard $orig_head
 286                rm -r "$state_dir"
 287                exit
 288                ;;
 289        --onto)
 290                test 2 -le "$#" || usage
 291                newbase="$2"
 292                shift
 293                ;;
 294        -M|-m|--m|--me|--mer|--merg|--merge)
 295                do_merge=t
 296                ;;
 297        -X*|--strategy-option*)
 298                case "$#,$1" in
 299                1,-X|1,--strategy-option)
 300                        usage ;;
 301                *,-X|*,--strategy-option)
 302                        newopt="$2"
 303                        shift ;;
 304                *,--strategy-option=*)
 305                        newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
 306                *,-X*)
 307                        newopt="$(expr " $1" : ' -X\(.*\)')" ;;
 308                1,*)
 309                        usage ;;
 310                esac
 311                strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
 312                do_merge=t
 313                ;;
 314        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
 315                --strateg=*|--strategy=*|\
 316        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
 317                case "$#,$1" in
 318                *,*=*)
 319                        strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
 320                1,*)
 321                        usage ;;
 322                *)
 323                        strategy="$2"
 324                        shift ;;
 325                esac
 326                do_merge=t
 327                ;;
 328        -n|--no-stat)
 329                diffstat=
 330                ;;
 331        --stat)
 332                diffstat=t
 333                ;;
 334        -v|--verbose)
 335                verbose=t
 336                diffstat=t
 337                GIT_QUIET=
 338                ;;
 339        -q|--quiet)
 340                GIT_QUIET=t
 341                git_am_opt="$git_am_opt -q"
 342                verbose=
 343                diffstat=
 344                ;;
 345        --whitespace=*)
 346                git_am_opt="$git_am_opt $1"
 347                case "$1" in
 348                --whitespace=fix|--whitespace=strip)
 349                        force_rebase=t
 350                        ;;
 351                esac
 352                ;;
 353        --ignore-whitespace)
 354                git_am_opt="$git_am_opt $1"
 355                ;;
 356        --committer-date-is-author-date|--ignore-date)
 357                git_am_opt="$git_am_opt $1"
 358                force_rebase=t
 359                ;;
 360        -C*)
 361                git_am_opt="$git_am_opt $1"
 362                ;;
 363        --root)
 364                rebase_root=t
 365                ;;
 366        -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
 367                force_rebase=t
 368                ;;
 369        --rerere-autoupdate|--no-rerere-autoupdate)
 370                allow_rerere_autoupdate="$1"
 371                ;;
 372        -*)
 373                usage
 374                ;;
 375        *)
 376                break
 377                ;;
 378        esac
 379        shift
 380done
 381test $# -gt 2 && usage
 382
 383if test $# -eq 0 && test -z "$rebase_root"
 384then
 385        test -d "$merge_dir" -o -d "$apply_dir" || usage
 386        test -d "$merge_dir" -o -f "$apply_dir"/rebasing &&
 387                die 'A rebase is in progress, try --continue, --skip or --abort.'
 388fi
 389
 390# Make sure we do not have $apply_dir or $merge_dir
 391if test -z "$do_merge"
 392then
 393        if mkdir "$apply_dir" 2>/dev/null
 394        then
 395                rmdir "$apply_dir"
 396        else
 397                echo >&2 '
 398It seems that I cannot create a rebase-apply directory, and
 399I wonder if you are in the middle of patch application or another
 400rebase.  If that is not the case, please
 401        rm -fr '"$apply_dir"'
 402and run me again.  I am stopping in case you still have something
 403valuable there.'
 404                exit 1
 405        fi
 406else
 407        if test -d "$merge_dir"
 408        then
 409                die "previous rebase directory $merge_dir still exists." \
 410                        'Try git rebase (--continue | --abort | --skip)'
 411        fi
 412fi
 413
 414require_clean_work_tree "rebase" "Please commit or stash them."
 415
 416if test -z "$rebase_root"
 417then
 418        # The upstream head must be given.  Make sure it is valid.
 419        upstream_name="$1"
 420        shift
 421        upstream=`git rev-parse --verify "${upstream_name}^0"` ||
 422        die "invalid upstream $upstream_name"
 423        unset root_flag
 424        upstream_arg="$upstream_name"
 425else
 426        test -z "$newbase" && die "--root must be used with --onto"
 427        unset upstream_name
 428        unset upstream
 429        root_flag="--root"
 430        upstream_arg="$root_flag"
 431fi
 432
 433# Make sure the branch to rebase onto is valid.
 434onto_name=${newbase-"$upstream_name"}
 435case "$onto_name" in
 436*...*)
 437        if      left=${onto_name%...*} right=${onto_name#*...} &&
 438                onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
 439        then
 440                case "$onto" in
 441                ?*"$LF"?*)
 442                        die "$onto_name: there are more than one merge bases"
 443                        ;;
 444                '')
 445                        die "$onto_name: there is no merge base"
 446                        ;;
 447                esac
 448        else
 449                die "$onto_name: there is no merge base"
 450        fi
 451        ;;
 452*)
 453        onto=$(git rev-parse --verify "${onto_name}^0") || exit
 454        ;;
 455esac
 456
 457# If a hook exists, give it a chance to interrupt
 458run_pre_rebase_hook "$upstream_arg" "$@"
 459
 460# If the branch to rebase is given, that is the branch we will rebase
 461# $branch_name -- branch being rebased, or HEAD (already detached)
 462# $orig_head -- commit object name of tip of the branch before rebasing
 463# $head_name -- refs/heads/<that-branch> or "detached HEAD"
 464switch_to=
 465case "$#" in
 4661)
 467        # Is it "rebase other $branchname" or "rebase other $commit"?
 468        branch_name="$1"
 469        switch_to="$1"
 470
 471        if git show-ref --verify --quiet -- "refs/heads/$1" &&
 472           branch=$(git rev-parse -q --verify "refs/heads/$1")
 473        then
 474                head_name="refs/heads/$1"
 475        elif branch=$(git rev-parse -q --verify "$1")
 476        then
 477                head_name="detached HEAD"
 478        else
 479                echo >&2 "fatal: no such branch: $1"
 480                usage
 481        fi
 482        ;;
 483*)
 484        # Do not need to switch branches, we are already on it.
 485        if branch_name=`git symbolic-ref -q HEAD`
 486        then
 487                head_name=$branch_name
 488                branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
 489        else
 490                head_name="detached HEAD"
 491                branch_name=HEAD ;# detached
 492        fi
 493        branch=$(git rev-parse --verify "${branch_name}^0") || exit
 494        ;;
 495esac
 496orig_head=$branch
 497
 498# Now we are rebasing commits $upstream..$branch (or with --root,
 499# everything leading up to $branch) on top of $onto
 500
 501# Check if we are already based on $onto with linear history,
 502# but this should be done only when upstream and onto are the same.
 503mb=$(git merge-base "$onto" "$branch")
 504if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
 505        # linear history?
 506        ! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
 507then
 508        if test -z "$force_rebase"
 509        then
 510                # Lazily switch to the target branch if needed...
 511                test -z "$switch_to" || git checkout "$switch_to" --
 512                say "Current branch $branch_name is up to date."
 513                exit 0
 514        else
 515                say "Current branch $branch_name is up to date, rebase forced."
 516        fi
 517fi
 518
 519# Detach HEAD and reset the tree
 520say "First, rewinding head to replay your work on top of it..."
 521git checkout -q "$onto^0" || die "could not detach HEAD"
 522git update-ref ORIG_HEAD $branch
 523
 524if test -n "$diffstat"
 525then
 526        if test -n "$verbose"
 527        then
 528                echo "Changes from $mb to $onto:"
 529        fi
 530        # We want color (if set), but no pager
 531        GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
 532fi
 533
 534# If the $onto is a proper descendant of the tip of the branch, then
 535# we just fast-forwarded.
 536if test "$mb" = "$branch"
 537then
 538        say "Fast-forwarded $branch_name to $onto_name."
 539        move_to_original_branch
 540        exit 0
 541fi
 542
 543if test -n "$rebase_root"
 544then
 545        revisions="$onto..$orig_head"
 546else
 547        revisions="$upstream..$orig_head"
 548fi
 549
 550if test -z "$do_merge"
 551then
 552        git format-patch -k --stdout --full-index --ignore-if-in-upstream \
 553                --src-prefix=a/ --dst-prefix=b/ \
 554                --no-renames $root_flag "$revisions" |
 555        git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
 556        move_to_original_branch
 557        ret=$?
 558        test 0 != $ret -a -d "$apply_dir" &&
 559                echo $head_name > "$apply_dir/head-name" &&
 560                echo $onto > "$apply_dir/onto" &&
 561                echo $orig_head > "$apply_dir/orig-head" &&
 562                echo "$GIT_QUIET" > "$apply_dir/quiet"
 563        exit $ret
 564fi
 565
 566# start doing a rebase with git-merge
 567# this is rename-aware if the recursive (default) strategy is used
 568
 569mkdir -p "$merge_dir"
 570echo "$onto_name" > "$merge_dir/onto_name"
 571prev_head=$orig_head
 572echo "$prev_head" > "$merge_dir/prev_head"
 573echo "$head_name" > "$merge_dir/head-name"
 574echo "$onto" > "$merge_dir/onto"
 575echo "$orig_head" > "$merge_dir/orig-head"
 576echo "$GIT_QUIET" > "$merge_dir/quiet"
 577
 578msgnum=0
 579for cmt in `git rev-list --reverse --no-merges "$revisions"`
 580do
 581        msgnum=$(($msgnum + 1))
 582        echo "$cmt" > "$merge_dir/cmt.$msgnum"
 583done
 584
 585echo 1 >"$merge_dir/msgnum"
 586echo $msgnum >"$merge_dir/end"
 587
 588end=$msgnum
 589msgnum=1
 590
 591while test "$msgnum" -le "$end"
 592do
 593        call_merge "$msgnum"
 594        continue_merge
 595done
 596
 597finish_rb_merge