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