f4ad7c1659c4576e15886a0cb67bfcd6b6fbf977
   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 onto
  46strategy=
  47strategy_opts=
  48do_merge=
  49merge_dir="$GIT_DIR"/rebase-merge
  50apply_dir="$GIT_DIR"/rebase-apply
  51verbose=
  52diffstat=
  53test "$(git config --bool rebase.stat)" = true && diffstat=t
  54git_am_opt=
  55rebase_root=
  56force_rebase=
  57allow_rerere_autoupdate=
  58# Non-empty if a rebase was in progress when 'git rebase' was invoked
  59in_progress=
  60# One of {am, merge, interactive}
  61type=
  62# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
  63state_dir=
  64# One of {'', continue, skip, abort}, as parsed from command line
  65action=
  66preserve_merges=
  67autosquash=
  68test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
  69
  70read_basic_state () {
  71        head_name=$(cat "$state_dir"/head-name) &&
  72        onto=$(cat "$state_dir"/onto) &&
  73        # We always write to orig-head, but interactive rebase used to write to
  74        # head. Fall back to reading from head to cover for the case that the
  75        # user upgraded git with an ongoing interactive rebase.
  76        if test -f "$state_dir"/orig-head
  77        then
  78                orig_head=$(cat "$state_dir"/orig-head)
  79        else
  80                orig_head=$(cat "$state_dir"/head)
  81        fi &&
  82        GIT_QUIET=$(cat "$state_dir"/quiet) &&
  83        test -f "$state_dir"/verbose && verbose=t
  84        test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
  85        test -f "$state_dir"/strategy_opts &&
  86                strategy_opts="$(cat "$state_dir"/strategy_opts)"
  87}
  88
  89write_basic_state () {
  90        echo "$head_name" > "$state_dir"/head-name &&
  91        echo "$onto" > "$state_dir"/onto &&
  92        echo "$orig_head" > "$state_dir"/orig-head &&
  93        echo "$GIT_QUIET" > "$state_dir"/quiet &&
  94        test t = "$verbose" && : > "$state_dir"/verbose
  95        test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
  96        test -n "$strategy_opts" && echo "$strategy_opts" > \
  97                "$state_dir"/strategy_opts
  98}
  99
 100output () {
 101        case "$verbose" in
 102        '')
 103                output=$("$@" 2>&1 )
 104                status=$?
 105                test $status != 0 && printf "%s\n" "$output"
 106                return $status
 107                ;;
 108        *)
 109                "$@"
 110                ;;
 111        esac
 112}
 113
 114move_to_original_branch () {
 115        case "$head_name" in
 116        refs/*)
 117                message="rebase finished: $head_name onto $onto"
 118                git update-ref -m "$message" \
 119                        $head_name $(git rev-parse HEAD) $orig_head &&
 120                git symbolic-ref HEAD $head_name ||
 121                die "Could not move back to $head_name"
 122                ;;
 123        esac
 124}
 125
 126run_specific_rebase () {
 127        if [ "$interactive_rebase" = implied ]; then
 128                GIT_EDITOR=:
 129                export GIT_EDITOR
 130        fi
 131        . git-rebase--$type
 132}
 133
 134run_pre_rebase_hook () {
 135        if test -z "$ok_to_skip_pre_rebase" &&
 136           test -x "$GIT_DIR/hooks/pre-rebase"
 137        then
 138                "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
 139                die "The pre-rebase hook refused to rebase."
 140        fi
 141}
 142
 143test -f "$apply_dir"/applying &&
 144        die 'It looks like git-am is in progress. Cannot rebase.'
 145
 146if test -d "$apply_dir"
 147then
 148        type=am
 149        state_dir="$apply_dir"
 150elif test -d "$merge_dir"
 151then
 152        if test -f "$merge_dir"/interactive
 153        then
 154                type=interactive
 155                interactive_rebase=explicit
 156        else
 157                type=merge
 158        fi
 159        state_dir="$merge_dir"
 160fi
 161test -n "$type" && in_progress=t
 162
 163total_argc=$#
 164while test $# != 0
 165do
 166        case "$1" in
 167        --no-verify)
 168                ok_to_skip_pre_rebase=yes
 169                ;;
 170        --verify)
 171                ok_to_skip_pre_rebase=
 172                ;;
 173        --continue|--skip|--abort)
 174                test $total_argc -eq 1 || usage
 175                action=${1##--}
 176                ;;
 177        --onto)
 178                test 2 -le "$#" || usage
 179                onto="$2"
 180                shift
 181                ;;
 182        -i|--interactive)
 183                interactive_rebase=explicit
 184                ;;
 185        -p|--preserve-merges)
 186                preserve_merges=t
 187                test -z "$interactive_rebase" && interactive_rebase=implied
 188                ;;
 189        --autosquash)
 190                autosquash=t
 191                ;;
 192        --no-autosquash)
 193                autosquash=
 194                ;;
 195        -M|-m|--m|--me|--mer|--merg|--merge)
 196                do_merge=t
 197                ;;
 198        -X*|--strategy-option*)
 199                case "$#,$1" in
 200                1,-X|1,--strategy-option)
 201                        usage ;;
 202                *,-X|*,--strategy-option)
 203                        newopt="$2"
 204                        shift ;;
 205                *,--strategy-option=*)
 206                        newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
 207                *,-X*)
 208                        newopt="$(expr " $1" : ' -X\(.*\)')" ;;
 209                1,*)
 210                        usage ;;
 211                esac
 212                strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
 213                do_merge=t
 214                test -z "$strategy" && strategy=recursive
 215                ;;
 216        -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
 217                --strateg=*|--strategy=*|\
 218        -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
 219                case "$#,$1" in
 220                *,*=*)
 221                        strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
 222                1,*)
 223                        usage ;;
 224                *)
 225                        strategy="$2"
 226                        shift ;;
 227                esac
 228                do_merge=t
 229                ;;
 230        -n|--no-stat)
 231                diffstat=
 232                ;;
 233        --stat)
 234                diffstat=t
 235                ;;
 236        -v|--verbose)
 237                verbose=t
 238                diffstat=t
 239                GIT_QUIET=
 240                ;;
 241        -q|--quiet)
 242                GIT_QUIET=t
 243                git_am_opt="$git_am_opt -q"
 244                verbose=
 245                diffstat=
 246                ;;
 247        --whitespace=*)
 248                git_am_opt="$git_am_opt $1"
 249                case "$1" in
 250                --whitespace=fix|--whitespace=strip)
 251                        force_rebase=t
 252                        ;;
 253                esac
 254                ;;
 255        --ignore-whitespace)
 256                git_am_opt="$git_am_opt $1"
 257                ;;
 258        --committer-date-is-author-date|--ignore-date)
 259                git_am_opt="$git_am_opt $1"
 260                force_rebase=t
 261                ;;
 262        -C*)
 263                git_am_opt="$git_am_opt $1"
 264                ;;
 265        --root)
 266                rebase_root=t
 267                ;;
 268        -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
 269                force_rebase=t
 270                ;;
 271        --rerere-autoupdate|--no-rerere-autoupdate)
 272                allow_rerere_autoupdate="$1"
 273                ;;
 274        -*)
 275                usage
 276                ;;
 277        *)
 278                break
 279                ;;
 280        esac
 281        shift
 282done
 283test $# -gt 2 && usage
 284
 285if test -n "$action"
 286then
 287        test -z "$in_progress" && die "No rebase in progress?"
 288        # Only interactive rebase uses detailed reflog messages
 289        if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
 290        then
 291                GIT_REFLOG_ACTION="rebase -i ($action)"
 292                export GIT_REFLOG_ACTION
 293        fi
 294fi
 295
 296case "$action" in
 297continue)
 298        # Sanity check
 299        git rev-parse --verify HEAD >/dev/null ||
 300                die "Cannot read HEAD"
 301        git update-index --ignore-submodules --refresh &&
 302        git diff-files --quiet --ignore-submodules || {
 303                echo "You must edit all merge conflicts and then"
 304                echo "mark them as resolved using git add"
 305                exit 1
 306        }
 307        read_basic_state
 308        run_specific_rebase
 309        ;;
 310skip)
 311        output git reset --hard HEAD || exit $?
 312        read_basic_state
 313        run_specific_rebase
 314        ;;
 315abort)
 316        git rerere clear
 317        read_basic_state
 318        case "$head_name" in
 319        refs/*)
 320                git symbolic-ref HEAD $head_name ||
 321                die "Could not move back to $head_name"
 322                ;;
 323        esac
 324        output git reset --hard $orig_head
 325        rm -r "$state_dir"
 326        exit
 327        ;;
 328esac
 329
 330# Make sure no rebase is in progress
 331if test -n "$in_progress"
 332then
 333        die '
 334It seems that there is already a '"${state_dir##*/}"' directory, and
 335I wonder if you are in the middle of another rebase.  If that is the
 336case, please try
 337        git rebase (--continue | --abort | --skip)
 338If that is not the case, please
 339        rm -fr '"$state_dir"'
 340and run me again.  I am stopping in case you still have something
 341valuable there.'
 342fi
 343
 344test $# -eq 0 && test -z "$rebase_root" && usage
 345
 346if test -n "$interactive_rebase"
 347then
 348        type=interactive
 349        state_dir="$merge_dir"
 350elif test -n "$do_merge"
 351then
 352        type=merge
 353        state_dir="$merge_dir"
 354else
 355        type=am
 356        state_dir="$apply_dir"
 357fi
 358
 359if test -z "$rebase_root"
 360then
 361        # The upstream head must be given.  Make sure it is valid.
 362        upstream_name="$1"
 363        shift
 364        upstream=`git rev-parse --verify "${upstream_name}^0"` ||
 365        die "invalid upstream $upstream_name"
 366        upstream_arg="$upstream_name"
 367else
 368        test -z "$onto" && die "You must specify --onto when using --root"
 369        unset upstream_name
 370        unset upstream
 371        upstream_arg=--root
 372fi
 373
 374# Make sure the branch to rebase onto is valid.
 375onto_name=${onto-"$upstream_name"}
 376case "$onto_name" in
 377*...*)
 378        if      left=${onto_name%...*} right=${onto_name#*...} &&
 379                onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
 380        then
 381                case "$onto" in
 382                ?*"$LF"?*)
 383                        die "$onto_name: there are more than one merge bases"
 384                        ;;
 385                '')
 386                        die "$onto_name: there is no merge base"
 387                        ;;
 388                esac
 389        else
 390                die "$onto_name: there is no merge base"
 391        fi
 392        ;;
 393*)
 394        onto=$(git rev-parse --verify "${onto_name}^0") ||
 395        die "Does not point to a valid commit: $1"
 396        ;;
 397esac
 398
 399# If the branch to rebase is given, that is the branch we will rebase
 400# $branch_name -- branch being rebased, or HEAD (already detached)
 401# $orig_head -- commit object name of tip of the branch before rebasing
 402# $head_name -- refs/heads/<that-branch> or "detached HEAD"
 403switch_to=
 404case "$#" in
 4051)
 406        # Is it "rebase other $branchname" or "rebase other $commit"?
 407        branch_name="$1"
 408        switch_to="$1"
 409
 410        if git show-ref --verify --quiet -- "refs/heads/$1" &&
 411           orig_head=$(git rev-parse -q --verify "refs/heads/$1")
 412        then
 413                head_name="refs/heads/$1"
 414        elif orig_head=$(git rev-parse -q --verify "$1")
 415        then
 416                head_name="detached HEAD"
 417        else
 418                echo >&2 "fatal: no such branch: $1"
 419                usage
 420        fi
 421        ;;
 422*)
 423        # Do not need to switch branches, we are already on it.
 424        if branch_name=`git symbolic-ref -q HEAD`
 425        then
 426                head_name=$branch_name
 427                branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
 428        else
 429                head_name="detached HEAD"
 430                branch_name=HEAD ;# detached
 431        fi
 432        orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
 433        ;;
 434esac
 435
 436require_clean_work_tree "rebase" "Please commit or stash them."
 437
 438# Now we are rebasing commits $upstream..$orig_head (or with --root,
 439# everything leading up to $orig_head) on top of $onto
 440
 441# Check if we are already based on $onto with linear history,
 442# but this should be done only when upstream and onto are the same
 443# and if this is not an interactive rebase.
 444mb=$(git merge-base "$onto" "$orig_head")
 445if test "$type" != interactive && test "$upstream" = "$onto" &&
 446        test "$mb" = "$onto" &&
 447        # linear history?
 448        ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
 449then
 450        if test -z "$force_rebase"
 451        then
 452                # Lazily switch to the target branch if needed...
 453                test -z "$switch_to" || git checkout "$switch_to" --
 454                say "Current branch $branch_name is up to date."
 455                exit 0
 456        else
 457                say "Current branch $branch_name is up to date, rebase forced."
 458        fi
 459fi
 460
 461# If a hook exists, give it a chance to interrupt
 462run_pre_rebase_hook "$upstream_arg" "$@"
 463
 464if test -n "$diffstat"
 465then
 466        if test -n "$verbose"
 467        then
 468                echo "Changes from $mb to $onto:"
 469        fi
 470        # We want color (if set), but no pager
 471        GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
 472fi
 473
 474test "$type" = interactive && run_specific_rebase
 475
 476# Detach HEAD and reset the tree
 477say "First, rewinding head to replay your work on top of it..."
 478git checkout -q "$onto^0" || die "could not detach HEAD"
 479git update-ref ORIG_HEAD $orig_head
 480
 481# If the $onto is a proper descendant of the tip of the branch, then
 482# we just fast-forwarded.
 483if test "$mb" = "$orig_head"
 484then
 485        say "Fast-forwarded $branch_name to $onto_name."
 486        move_to_original_branch
 487        exit 0
 488fi
 489
 490if test -n "$rebase_root"
 491then
 492        revisions="$onto..$orig_head"
 493else
 494        revisions="$upstream..$orig_head"
 495fi
 496
 497run_specific_rebase