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