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