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