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