git-rebase.shon commit rebase -i: introduce --recreate-merges=[no-]rebase-cousins (5e05825)
   1#!/bin/sh
   2#
   3# Copyright (c) 2005 Junio C Hamano.
   4#
   5
   6SUBDIRECTORY_OK=Yes
   7OPTIONS_KEEPDASHDASH=
   8OPTIONS_STUCKLONG=t
   9OPTIONS_SPEC="\
  10git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
  11git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
  12git rebase --continue | --abort | --skip | --edit-todo
  13--
  14 Available options are
  15v,verbose!         display a diffstat of what changed upstream
  16q,quiet!           be quiet. implies --no-stat
  17autostash          automatically stash/stash pop before and after
  18fork-point         use 'merge-base --fork-point' to refine upstream
  19onto=!             rebase onto given branch instead of upstream
  20recreate-merges?   try to recreate merges instead of skipping them
  21p,preserve-merges! try to recreate merges instead of ignoring them
  22s,strategy=!       use the given merge strategy
  23no-ff!             cherry-pick all commits, even if unchanged
  24m,merge!           use merging strategies to rebase
  25i,interactive!     let the user edit the list of commits to rebase
  26x,exec=!           add exec lines after each commit of the editable list
  27k,keep-empty       preserve empty commits during rebase
  28f,force-rebase!    force rebase even if branch is up to date
  29X,strategy-option=! pass the argument through to the merge strategy
  30stat!              display a diffstat of what changed upstream
  31n,no-stat!         do not show diffstat of what changed upstream
  32verify             allow pre-rebase hook to run
  33rerere-autoupdate  allow rerere to update index with resolved conflicts
  34root!              rebase all reachable commits up to the root(s)
  35autosquash         move commits that begin with squash!/fixup! under -i
  36committer-date-is-author-date! passed to 'git am'
  37ignore-date!       passed to 'git am'
  38signoff            passed to 'git am'
  39whitespace=!       passed to 'git apply'
  40ignore-whitespace! passed to 'git apply'
  41C=!                passed to 'git apply'
  42S,gpg-sign?        GPG-sign commits
  43 Actions:
  44continue!          continue
  45abort!             abort and check out the original branch
  46skip!              skip current patch and continue
  47edit-todo!         edit the todo list during an interactive rebase
  48quit!              abort but keep HEAD where it is
  49"
  50. git-sh-setup
  51set_reflog_action rebase
  52require_work_tree_exists
  53cd_to_toplevel
  54
  55LF='
  56'
  57ok_to_skip_pre_rebase=
  58resolvemsg="
  59$(gettext 'Resolve all conflicts manually, mark them as resolved with
  60"git add/rm <conflicted_files>", then run "git rebase --continue".
  61You can instead skip this commit: run "git rebase --skip".
  62To abort and get back to the state before "git rebase", run "git rebase --abort".')
  63"
  64unset onto
  65unset restrict_revision
  66cmd=
  67strategy=
  68strategy_opts=
  69do_merge=
  70merge_dir="$GIT_DIR"/rebase-merge
  71apply_dir="$GIT_DIR"/rebase-apply
  72verbose=
  73diffstat=
  74test "$(git config --bool rebase.stat)" = true && diffstat=t
  75autostash="$(git config --bool rebase.autostash || echo false)"
  76fork_point=auto
  77git_am_opt=
  78git_format_patch_opt=
  79rebase_root=
  80force_rebase=
  81allow_rerere_autoupdate=
  82# Non-empty if a rebase was in progress when 'git rebase' was invoked
  83in_progress=
  84# One of {am, merge, interactive}
  85type=
  86# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
  87state_dir=
  88# One of {'', continue, skip, abort}, as parsed from command line
  89action=
  90recreate_merges=
  91rebase_cousins=
  92preserve_merges=
  93autosquash=
  94keep_empty=
  95test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
  96case "$(git config --bool commit.gpgsign)" in
  97true)   gpg_sign_opt=-S ;;
  98*)      gpg_sign_opt= ;;
  99esac
 100
 101read_basic_state () {
 102        test -f "$state_dir/head-name" &&
 103        test -f "$state_dir/onto" &&
 104        head_name=$(cat "$state_dir"/head-name) &&
 105        onto=$(cat "$state_dir"/onto) &&
 106        # We always write to orig-head, but interactive rebase used to write to
 107        # head. Fall back to reading from head to cover for the case that the
 108        # user upgraded git with an ongoing interactive rebase.
 109        if test -f "$state_dir"/orig-head
 110        then
 111                orig_head=$(cat "$state_dir"/orig-head)
 112        else
 113                orig_head=$(cat "$state_dir"/head)
 114        fi &&
 115        GIT_QUIET=$(cat "$state_dir"/quiet) &&
 116        test -f "$state_dir"/verbose && verbose=t
 117        test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
 118        test -f "$state_dir"/strategy_opts &&
 119                strategy_opts="$(cat "$state_dir"/strategy_opts)"
 120        test -f "$state_dir"/allow_rerere_autoupdate &&
 121                allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
 122        test -f "$state_dir"/gpg_sign_opt &&
 123                gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
 124}
 125
 126write_basic_state () {
 127        echo "$head_name" > "$state_dir"/head-name &&
 128        echo "$onto" > "$state_dir"/onto &&
 129        echo "$orig_head" > "$state_dir"/orig-head &&
 130        echo "$GIT_QUIET" > "$state_dir"/quiet &&
 131        test t = "$verbose" && : > "$state_dir"/verbose
 132        test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
 133        test -n "$strategy_opts" && echo "$strategy_opts" > \
 134                "$state_dir"/strategy_opts
 135        test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
 136                "$state_dir"/allow_rerere_autoupdate
 137        test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
 138}
 139
 140output () {
 141        case "$verbose" in
 142        '')
 143                output=$("$@" 2>&1 )
 144                status=$?
 145                test $status != 0 && printf "%s\n" "$output"
 146                return $status
 147                ;;
 148        *)
 149                "$@"
 150                ;;
 151        esac
 152}
 153
 154move_to_original_branch () {
 155        case "$head_name" in
 156        refs/*)
 157                message="rebase finished: $head_name onto $onto"
 158                git update-ref -m "$message" \
 159                        $head_name $(git rev-parse HEAD) $orig_head &&
 160                git symbolic-ref \
 161                        -m "rebase finished: returning to $head_name" \
 162                        HEAD $head_name ||
 163                die "$(eval_gettext "Could not move back to \$head_name")"
 164                ;;
 165        esac
 166}
 167
 168apply_autostash () {
 169        if test -f "$state_dir/autostash"
 170        then
 171                stash_sha1=$(cat "$state_dir/autostash")
 172                if git stash apply $stash_sha1 >/dev/null 2>&1
 173                then
 174                        echo "$(gettext 'Applied autostash.')" >&2
 175                else
 176                        git stash store -m "autostash" -q $stash_sha1 ||
 177                        die "$(eval_gettext "Cannot store \$stash_sha1")"
 178                        gettext 'Applying autostash resulted in conflicts.
 179Your changes are safe in the stash.
 180You can run "git stash pop" or "git stash drop" at any time.
 181' >&2
 182                fi
 183        fi
 184}
 185
 186finish_rebase () {
 187        apply_autostash &&
 188        { git gc --auto || true; } &&
 189        rm -rf "$state_dir"
 190}
 191
 192run_specific_rebase () {
 193        if [ "$interactive_rebase" = implied ]; then
 194                GIT_EDITOR=:
 195                export GIT_EDITOR
 196                autosquash=
 197        fi
 198        . git-rebase--$type
 199        ret=$?
 200        if test $ret -eq 0
 201        then
 202                finish_rebase
 203        elif test $ret -eq 2 # special exit status for rebase -i
 204        then
 205                apply_autostash &&
 206                rm -rf "$state_dir" &&
 207                die "Nothing to do"
 208        fi
 209        exit $ret
 210}
 211
 212run_pre_rebase_hook () {
 213        if test -z "$ok_to_skip_pre_rebase" &&
 214           test -x "$(git rev-parse --git-path hooks/pre-rebase)"
 215        then
 216                "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
 217                die "$(gettext "The pre-rebase hook refused to rebase.")"
 218        fi
 219}
 220
 221test -f "$apply_dir"/applying &&
 222        die "$(gettext "It looks like 'git am' is in progress. Cannot rebase.")"
 223
 224if test -d "$apply_dir"
 225then
 226        type=am
 227        state_dir="$apply_dir"
 228elif test -d "$merge_dir"
 229then
 230        if test -f "$merge_dir"/interactive
 231        then
 232                type=interactive
 233                interactive_rebase=explicit
 234        else
 235                type=merge
 236        fi
 237        state_dir="$merge_dir"
 238fi
 239test -n "$type" && in_progress=t
 240
 241total_argc=$#
 242while test $# != 0
 243do
 244        case "$1" in
 245        --no-verify)
 246                ok_to_skip_pre_rebase=yes
 247                ;;
 248        --verify)
 249                ok_to_skip_pre_rebase=
 250                ;;
 251        --continue|--skip|--abort|--quit|--edit-todo)
 252                test $total_argc -eq 2 || usage
 253                action=${1##--}
 254                ;;
 255        --onto=*)
 256                onto="${1#--onto=}"
 257                ;;
 258        --exec=*)
 259                cmd="${cmd}exec ${1#--exec=}${LF}"
 260                test -z "$interactive_rebase" && interactive_rebase=implied
 261                ;;
 262        --interactive)
 263                interactive_rebase=explicit
 264                ;;
 265        --keep-empty)
 266                keep_empty=yes
 267                ;;
 268        --recreate-merges)
 269                recreate_merges=t
 270                test -z "$interactive_rebase" && interactive_rebase=implied
 271                ;;
 272        --recreate-merges=*)
 273                recreate_merges=t
 274                case "${1#*=}" in
 275                rebase-cousins) rebase_cousins=t;;
 276                no-rebase-cousins) rebase_cousins=;;
 277                *) die "Unknown mode: $1";;
 278                esac
 279                test -z "$interactive_rebase" && interactive_rebase=implied
 280                ;;
 281        --preserve-merges)
 282                preserve_merges=t
 283                test -z "$interactive_rebase" && interactive_rebase=implied
 284                ;;
 285        --autosquash)
 286                autosquash=t
 287                ;;
 288        --no-autosquash)
 289                autosquash=
 290                ;;
 291        --fork-point)
 292                fork_point=t
 293                ;;
 294        --no-fork-point)
 295                fork_point=
 296                ;;
 297        --merge)
 298                do_merge=t
 299                ;;
 300        --strategy-option=*)
 301                strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
 302                do_merge=t
 303                test -z "$strategy" && strategy=recursive
 304                ;;
 305        --strategy=*)
 306                strategy="${1#--strategy=}"
 307                do_merge=t
 308                ;;
 309        --no-stat)
 310                diffstat=
 311                ;;
 312        --stat)
 313                diffstat=t
 314                ;;
 315        --autostash)
 316                autostash=true
 317                ;;
 318        --no-autostash)
 319                autostash=false
 320                ;;
 321        --verbose)
 322                verbose=t
 323                diffstat=t
 324                GIT_QUIET=
 325                ;;
 326        --quiet)
 327                GIT_QUIET=t
 328                git_am_opt="$git_am_opt -q"
 329                verbose=
 330                diffstat=
 331                ;;
 332        --whitespace=*)
 333                git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
 334                case "${1#--whitespace=}" in
 335                fix|strip)
 336                        force_rebase=t
 337                        ;;
 338                esac
 339                ;;
 340        --ignore-whitespace)
 341                git_am_opt="$git_am_opt $1"
 342                ;;
 343        --committer-date-is-author-date|--ignore-date|--signoff|--no-signoff)
 344                git_am_opt="$git_am_opt $1"
 345                force_rebase=t
 346                ;;
 347        -C*)
 348                git_am_opt="$git_am_opt $1"
 349                ;;
 350        --root)
 351                rebase_root=t
 352                ;;
 353        --force-rebase|--no-ff)
 354                force_rebase=t
 355                ;;
 356        --rerere-autoupdate|--no-rerere-autoupdate)
 357                allow_rerere_autoupdate="$1"
 358                ;;
 359        --gpg-sign)
 360                gpg_sign_opt=-S
 361                ;;
 362        --gpg-sign=*)
 363                gpg_sign_opt="-S${1#--gpg-sign=}"
 364                ;;
 365        --)
 366                shift
 367                break
 368                ;;
 369        *)
 370                usage
 371                ;;
 372        esac
 373        shift
 374done
 375test $# -gt 2 && usage
 376
 377if test -n "$action"
 378then
 379        test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
 380        # Only interactive rebase uses detailed reflog messages
 381        if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
 382        then
 383                GIT_REFLOG_ACTION="rebase -i ($action)"
 384                export GIT_REFLOG_ACTION
 385        fi
 386fi
 387
 388if test "$action" = "edit-todo" && test "$type" != "interactive"
 389then
 390        die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
 391fi
 392
 393case "$action" in
 394continue)
 395        # Sanity check
 396        git rev-parse --verify HEAD >/dev/null ||
 397                die "$(gettext "Cannot read HEAD")"
 398        git update-index --ignore-submodules --refresh &&
 399        git diff-files --quiet --ignore-submodules || {
 400                echo "$(gettext "You must edit all merge conflicts and then
 401mark them as resolved using git add")"
 402                exit 1
 403        }
 404        read_basic_state
 405        run_specific_rebase
 406        ;;
 407skip)
 408        output git reset --hard HEAD || exit $?
 409        read_basic_state
 410        run_specific_rebase
 411        ;;
 412abort)
 413        git rerere clear
 414        read_basic_state
 415        case "$head_name" in
 416        refs/*)
 417                git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
 418                die "$(eval_gettext "Could not move back to \$head_name")"
 419                ;;
 420        esac
 421        output git reset --hard $orig_head
 422        finish_rebase
 423        exit
 424        ;;
 425quit)
 426        exec rm -rf "$state_dir"
 427        ;;
 428edit-todo)
 429        run_specific_rebase
 430        ;;
 431esac
 432
 433# Make sure no rebase is in progress
 434if test -n "$in_progress"
 435then
 436        state_dir_base=${state_dir##*/}
 437        cmd_live_rebase="git rebase (--continue | --abort | --skip)"
 438        cmd_clear_stale_rebase="rm -fr \"$state_dir\""
 439        die "
 440$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
 441I wonder if you are in the middle of another rebase.  If that is the
 442case, please try
 443        $cmd_live_rebase
 444If that is not the case, please
 445        $cmd_clear_stale_rebase
 446and run me again.  I am stopping in case you still have something
 447valuable there.')"
 448fi
 449
 450if test -n "$rebase_root" && test -z "$onto"
 451then
 452        test -z "$interactive_rebase" && interactive_rebase=implied
 453fi
 454
 455if test -n "$interactive_rebase"
 456then
 457        type=interactive
 458        state_dir="$merge_dir"
 459elif test -n "$do_merge"
 460then
 461        type=merge
 462        state_dir="$merge_dir"
 463else
 464        type=am
 465        state_dir="$apply_dir"
 466fi
 467
 468if test -t 2 && test -z "$GIT_QUIET"
 469then
 470        git_format_patch_opt="$git_format_patch_opt --progress"
 471fi
 472
 473if test -z "$rebase_root"
 474then
 475        case "$#" in
 476        0)
 477                if ! upstream_name=$(git rev-parse --symbolic-full-name \
 478                        --verify -q @{upstream} 2>/dev/null)
 479                then
 480                        . git-parse-remote
 481                        error_on_missing_default_upstream "rebase" "rebase" \
 482                                "against" "git rebase $(gettext '<branch>')"
 483                fi
 484
 485                test "$fork_point" = auto && fork_point=t
 486                ;;
 487        *)      upstream_name="$1"
 488                if test "$upstream_name" = "-"
 489                then
 490                        upstream_name="@{-1}"
 491                fi
 492                shift
 493                ;;
 494        esac
 495        upstream=$(peel_committish "${upstream_name}") ||
 496        die "$(eval_gettext "invalid upstream '\$upstream_name'")"
 497        upstream_arg="$upstream_name"
 498else
 499        if test -z "$onto"
 500        then
 501                empty_tree=$(git hash-object -t tree /dev/null)
 502                onto=$(git commit-tree $empty_tree </dev/null)
 503                squash_onto="$onto"
 504        fi
 505        unset upstream_name
 506        unset upstream
 507        test $# -gt 1 && usage
 508        upstream_arg=--root
 509fi
 510
 511# Make sure the branch to rebase onto is valid.
 512onto_name=${onto-"$upstream_name"}
 513case "$onto_name" in
 514*...*)
 515        if      left=${onto_name%...*} right=${onto_name#*...} &&
 516                onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
 517        then
 518                case "$onto" in
 519                ?*"$LF"?*)
 520                        die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
 521                        ;;
 522                '')
 523                        die "$(eval_gettext "\$onto_name: there is no merge base")"
 524                        ;;
 525                esac
 526        else
 527                die "$(eval_gettext "\$onto_name: there is no merge base")"
 528        fi
 529        ;;
 530*)
 531        onto=$(peel_committish "$onto_name") ||
 532        die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
 533        ;;
 534esac
 535
 536# If the branch to rebase is given, that is the branch we will rebase
 537# $branch_name -- branch/commit being rebased, or HEAD (already detached)
 538# $orig_head -- commit object name of tip of the branch before rebasing
 539# $head_name -- refs/heads/<that-branch> or "detached HEAD"
 540switch_to=
 541case "$#" in
 5421)
 543        # Is it "rebase other $branchname" or "rebase other $commit"?
 544        branch_name="$1"
 545        switch_to="$1"
 546
 547        # Is it a local branch?
 548        if git show-ref --verify --quiet -- "refs/heads/$branch_name" &&
 549           orig_head=$(git rev-parse -q --verify "refs/heads/$branch_name")
 550        then
 551                head_name="refs/heads/$branch_name"
 552        # If not is it a valid ref (branch or commit)?
 553        elif orig_head=$(git rev-parse -q --verify "$branch_name")
 554        then
 555                head_name="detached HEAD"
 556
 557        else
 558                die "$(eval_gettext "fatal: no such branch/commit '\$branch_name'")"
 559        fi
 560        ;;
 5610)
 562        # Do not need to switch branches, we are already on it.
 563        if branch_name=$(git symbolic-ref -q HEAD)
 564        then
 565                head_name=$branch_name
 566                branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
 567        else
 568                head_name="detached HEAD"
 569                branch_name=HEAD
 570        fi
 571        orig_head=$(git rev-parse --verify HEAD) || exit
 572        ;;
 573*)
 574        die "BUG: unexpected number of arguments left to parse"
 575        ;;
 576esac
 577
 578if test "$fork_point" = t
 579then
 580        new_upstream=$(git merge-base --fork-point "$upstream_name" \
 581                        "${switch_to:-HEAD}")
 582        if test -n "$new_upstream"
 583        then
 584                restrict_revision=$new_upstream
 585        fi
 586fi
 587
 588if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
 589then
 590        stash_sha1=$(git stash create "autostash") ||
 591        die "$(gettext 'Cannot autostash')"
 592
 593        mkdir -p "$state_dir" &&
 594        echo $stash_sha1 >"$state_dir/autostash" &&
 595        stash_abbrev=$(git rev-parse --short $stash_sha1) &&
 596        echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
 597        git reset --hard
 598fi
 599
 600require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
 601
 602# Now we are rebasing commits $upstream..$orig_head (or with --root,
 603# everything leading up to $orig_head) on top of $onto
 604
 605# Check if we are already based on $onto with linear history,
 606# but this should be done only when upstream and onto are the same
 607# and if this is not an interactive rebase.
 608mb=$(git merge-base "$onto" "$orig_head")
 609if test "$type" != interactive && test "$upstream" = "$onto" &&
 610        test "$mb" = "$onto" && test -z "$restrict_revision" &&
 611        # linear history?
 612        ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
 613then
 614        if test -z "$force_rebase"
 615        then
 616                # Lazily switch to the target branch if needed...
 617                test -z "$switch_to" ||
 618                GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
 619                        git checkout -q "$switch_to" --
 620                if test "$branch_name" = "HEAD" &&
 621                         ! git symbolic-ref -q HEAD
 622                then
 623                        say "$(eval_gettext "HEAD is up to date.")"
 624                else
 625                        say "$(eval_gettext "Current branch \$branch_name is up to date.")"
 626                fi
 627                finish_rebase
 628                exit 0
 629        else
 630                if test "$branch_name" = "HEAD" &&
 631                         ! git symbolic-ref -q HEAD
 632                then
 633                        say "$(eval_gettext "HEAD is up to date, rebase forced.")"
 634                else
 635                        say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
 636                fi
 637        fi
 638fi
 639
 640# If a hook exists, give it a chance to interrupt
 641run_pre_rebase_hook "$upstream_arg" "$@"
 642
 643if test -n "$diffstat"
 644then
 645        if test -n "$verbose"
 646        then
 647                echo "$(eval_gettext "Changes from \$mb to \$onto:")"
 648        fi
 649        # We want color (if set), but no pager
 650        GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
 651fi
 652
 653test "$type" = interactive && run_specific_rebase
 654
 655# Detach HEAD and reset the tree
 656say "$(gettext "First, rewinding head to replay your work on top of it...")"
 657
 658GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
 659        git checkout -q "$onto^0" || die "could not detach HEAD"
 660git update-ref ORIG_HEAD $orig_head
 661
 662# If the $onto is a proper descendant of the tip of the branch, then
 663# we just fast-forwarded.
 664if test "$mb" = "$orig_head"
 665then
 666        say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
 667        move_to_original_branch
 668        finish_rebase
 669        exit 0
 670fi
 671
 672if test -n "$rebase_root"
 673then
 674        revisions="$onto..$orig_head"
 675else
 676        revisions="${restrict_revision-$upstream}..$orig_head"
 677fi
 678
 679run_specific_rebase