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