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