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 51verbose= 52diffstat= 53test"$(git config --bool rebase.stat)"= true && diffstat=t 54git_am_opt= 55rebase_root= 56force_rebase= 57allow_rerere_autoupdate= 58# Non-empty if a rebase was in progress when 'git rebase' was invoked 59in_progress= 60# One of {am, merge, interactive} 61type= 62# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge} 63state_dir= 64# One of {'', continue, skip, abort}, as parsed from command line 65action= 66preserve_merges= 67autosquash= 68test"$(git config --bool rebase.autosquash)"="true"&& autosquash=t 69 70read_basic_state () { 71 head_name=$(cat "$state_dir"/head-name)&& 72 onto=$(cat "$state_dir"/onto)&& 73 orig_head=$(cat "$state_dir"/orig-head)&& 74 GIT_QUIET=$(cat "$state_dir"/quiet) 75} 76 77move_to_original_branch () { 78case"$head_name"in 79 refs/*) 80 message="rebase finished:$head_nameonto$onto" 81 git update-ref -m"$message" \ 82$head_name $(git rev-parse HEAD) $orig_head&& 83 git symbolic-ref HEAD $head_name|| 84 die "Could not move back to$head_name" 85;; 86esac 87} 88 89run_specific_rebase () { 90if["$interactive_rebase"= implied ];then 91 GIT_EDITOR=: 92export GIT_EDITOR 93fi 94test"$type"!= am && . git-rebase--$type 95} 96 97run_pre_rebase_hook () { 98iftest -z"$ok_to_skip_pre_rebase"&& 99test -x"$GIT_DIR/hooks/pre-rebase" 100then 101"$GIT_DIR/hooks/pre-rebase"${1+"$@"}|| 102 die "The pre-rebase hook refused to rebase." 103fi 104} 105 106test -f"$apply_dir"/applying && 107 die 'It looks like git-am is in progress. Cannot rebase.' 108 109iftest -d"$apply_dir" 110then 111type=am 112 state_dir="$apply_dir" 113eliftest -d"$merge_dir" 114then 115iftest -f"$merge_dir"/interactive 116then 117type=interactive 118 interactive_rebase=explicit 119else 120type=merge 121fi 122 state_dir="$merge_dir" 123fi 124test -n"$type"&& in_progress=t 125 126total_argc=$# 127whiletest$#!=0 128do 129case"$1"in 130--no-verify) 131 ok_to_skip_pre_rebase=yes 132;; 133--verify) 134 ok_to_skip_pre_rebase= 135;; 136--continue|--skip|--abort) 137test$total_argc-eq1|| usage 138 action=${1##--} 139;; 140--onto) 141test2-le"$#"|| usage 142 onto="$2" 143shift 144;; 145-i|--interactive) 146 interactive_rebase=explicit 147;; 148-p|--preserve-merges) 149 preserve_merges=t 150test -z"$interactive_rebase"&& interactive_rebase=implied 151;; 152--autosquash) 153 autosquash=t 154;; 155--no-autosquash) 156 autosquash= 157;; 158-M|-m|--m|--me|--mer|--merg|--merge) 159 do_merge=t 160;; 161-X*|--strategy-option*) 162case"$#,$1"in 1631,-X|1,--strategy-option) 164 usage ;; 165*,-X|*,--strategy-option) 166 newopt="$2" 167shift;; 168*,--strategy-option=*) 169 newopt="$(expr " $1" : ' --strategy-option=\(.*\)')";; 170*,-X*) 171 newopt="$(expr " $1" : ' -X\(.*\)')";; 1721,*) 173 usage ;; 174esac 175 strategy_opts="$strategy_opts$(git rev-parse --sq-quote "--$newopt")" 176 do_merge=t 177test -z"$strategy"&& strategy=recursive 178;; 179-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ 180--strateg=*|--strategy=*|\ 181-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) 182case"$#,$1"in 183*,*=*) 184 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'`;; 1851,*) 186 usage ;; 187*) 188 strategy="$2" 189shift;; 190esac 191 do_merge=t 192;; 193-n|--no-stat) 194 diffstat= 195;; 196--stat) 197 diffstat=t 198;; 199-v|--verbose) 200 verbose=t 201 diffstat=t 202 GIT_QUIET= 203;; 204-q|--quiet) 205 GIT_QUIET=t 206 git_am_opt="$git_am_opt-q" 207 verbose= 208 diffstat= 209;; 210--whitespace=*) 211 git_am_opt="$git_am_opt$1" 212case"$1"in 213--whitespace=fix|--whitespace=strip) 214 force_rebase=t 215;; 216esac 217;; 218--ignore-whitespace) 219 git_am_opt="$git_am_opt$1" 220;; 221--committer-date-is-author-date|--ignore-date) 222 git_am_opt="$git_am_opt$1" 223 force_rebase=t 224;; 225-C*) 226 git_am_opt="$git_am_opt$1" 227;; 228--root) 229 rebase_root=t 230;; 231-f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff) 232 force_rebase=t 233;; 234--rerere-autoupdate|--no-rerere-autoupdate) 235 allow_rerere_autoupdate="$1" 236;; 237-*) 238 usage 239;; 240*) 241break 242;; 243esac 244shift 245done 246test$#-gt2&& usage 247 248iftest -n"$action" 249then 250test -z"$in_progress"&& die "No rebase in progress?" 251test"$type"= interactive && run_specific_rebase 252fi 253 254case"$action"in 255continue) 256 git update-index --ignore-submodules --refresh&& 257 git diff-files --quiet --ignore-submodules|| { 258echo"You must edit all merge conflicts and then" 259echo"mark them as resolved using git add" 260exit1 261} 262 read_basic_state 263 run_specific_rebase 264 git am --resolved --3way --resolvemsg="$resolvemsg"&& 265 move_to_original_branch 266exit 267;; 268skip) 269 git reset--hard HEAD ||exit $? 270 read_basic_state 271 run_specific_rebase 272 git am -3 --skip --resolvemsg="$resolvemsg"&& 273 move_to_original_branch 274exit 275;; 276abort) 277 git rerere clear 278 read_basic_state 279case"$head_name"in 280 refs/*) 281 git symbolic-ref HEAD $head_name|| 282 die "Could not move back to$head_name" 283;; 284esac 285 git reset--hard$orig_head 286rm-r"$state_dir" 287exit 288;; 289esac 290 291# Make sure no rebase is in progress 292iftest -n"$in_progress" 293then 294 die ' 295It seems that there is already a '"${state_dir##*/}"' directory, and 296I wonder if you are in the middle of another rebase. If that is the 297case, please try 298 git rebase (--continue | --abort | --skip) 299If that is not the case, please 300 rm -fr '"$state_dir"' 301and run me again. I am stopping in case you still have something 302valuable there.' 303fi 304 305test$#-eq0&&test -z"$rebase_root"&& usage 306 307iftest -n"$interactive_rebase" 308then 309type=interactive 310 state_dir="$merge_dir" 311eliftest -n"$do_merge" 312then 313type=merge 314 state_dir="$merge_dir" 315else 316type=am 317 state_dir="$apply_dir" 318fi 319 320iftest -z"$rebase_root" 321then 322# The upstream head must be given. Make sure it is valid. 323 upstream_name="$1" 324shift 325 upstream=`git rev-parse --verify "${upstream_name}^0"`|| 326 die "invalid upstream$upstream_name" 327unset root_flag 328 upstream_arg="$upstream_name" 329else 330test -z"$onto"&& die "You must specify --onto when using --root" 331unset upstream_name 332unset upstream 333 root_flag="--root" 334 upstream_arg="$root_flag" 335fi 336 337# Make sure the branch to rebase onto is valid. 338onto_name=${onto-"$upstream_name"} 339case"$onto_name"in 340*...*) 341if left=${onto_name%...*} right=${onto_name#*...}&& 342 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD}) 343then 344case"$onto"in 345 ?*"$LF"?*) 346 die "$onto_name: there are more than one merge bases" 347;; 348'') 349 die "$onto_name: there is no merge base" 350;; 351esac 352else 353 die "$onto_name: there is no merge base" 354fi 355;; 356*) 357 onto=$(git rev-parse --verify "${onto_name}^0")|| 358 die "Does not point to a valid commit:$1" 359;; 360esac 361 362# If the branch to rebase is given, that is the branch we will rebase 363# $branch_name -- branch being rebased, or HEAD (already detached) 364# $orig_head -- commit object name of tip of the branch before rebasing 365# $head_name -- refs/heads/<that-branch> or "detached HEAD" 366switch_to= 367case"$#"in 3681) 369# Is it "rebase other $branchname" or "rebase other $commit"? 370 branch_name="$1" 371 switch_to="$1" 372 373if git show-ref --verify --quiet --"refs/heads/$1"&& 374 orig_head=$(git rev-parse -q --verify "refs/heads/$1") 375then 376 head_name="refs/heads/$1" 377elif orig_head=$(git rev-parse -q --verify "$1") 378then 379 head_name="detached HEAD" 380else 381echo>&2"fatal: no such branch:$1" 382 usage 383fi 384;; 385*) 386# Do not need to switch branches, we are already on it. 387if branch_name=`git symbolic-ref -q HEAD` 388then 389 head_name=$branch_name 390 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'` 391else 392 head_name="detached HEAD" 393 branch_name=HEAD ;# detached 394fi 395 orig_head=$(git rev-parse --verify "${branch_name}^0")||exit 396;; 397esac 398 399require_clean_work_tree "rebase""Please commit or stash them." 400 401# Now we are rebasing commits $upstream..$orig_head (or with --root, 402# everything leading up to $orig_head) on top of $onto 403 404# Check if we are already based on $onto with linear history, 405# but this should be done only when upstream and onto are the same 406# and if this is not an interactive rebase. 407mb=$(git merge-base "$onto" "$orig_head") 408iftest"$type"!= interactive &&test"$upstream"="$onto"&& 409test"$mb"="$onto"&& 410# linear history? 411! (git rev-list --parents"$onto".."$orig_head"| sane_grep " .* ") > /dev/null 412then 413iftest -z"$force_rebase" 414then 415# Lazily switch to the target branch if needed... 416test -z"$switch_to"|| git checkout "$switch_to"-- 417 say "Current branch$branch_nameis up to date." 418exit0 419else 420 say "Current branch$branch_nameis up to date, rebase forced." 421fi 422fi 423 424# If a hook exists, give it a chance to interrupt 425run_pre_rebase_hook "$upstream_arg""$@" 426 427iftest -n"$diffstat" 428then 429iftest -n"$verbose" 430then 431echo"Changes from$mbto$onto:" 432fi 433# We want color (if set), but no pager 434 GIT_PAGER='' git diff--stat --summary"$mb""$onto" 435fi 436 437test"$type"= interactive && run_specific_rebase 438 439# Detach HEAD and reset the tree 440say "First, rewinding head to replay your work on top of it..." 441git checkout -q"$onto^0"|| die "could not detach HEAD" 442git update-ref ORIG_HEAD $orig_head 443 444# If the $onto is a proper descendant of the tip of the branch, then 445# we just fast-forwarded. 446iftest"$mb"="$orig_head" 447then 448 say "Fast-forwarded$branch_nameto$onto_name." 449 move_to_original_branch 450exit0 451fi 452 453iftest -n"$rebase_root" 454then 455 revisions="$onto..$orig_head" 456else 457 revisions="$upstream..$orig_head" 458fi 459 460iftest -z"$do_merge" 461then 462 git format-patch -k --stdout --full-index --ignore-if-in-upstream \ 463--src-prefix=a/--dst-prefix=b/ \ 464--no-renames$root_flag"$revisions"| 465 git am $git_am_opt--rebasing --resolvemsg="$resolvemsg"&& 466 move_to_original_branch 467 ret=$? 468test0!=$ret-a -d"$apply_dir"&& 469echo$head_name>"$apply_dir/head-name"&& 470echo$onto>"$apply_dir/onto"&& 471echo$orig_head>"$apply_dir/orig-head"&& 472echo"$GIT_QUIET">"$apply_dir/quiet" 473exit$ret 474fi 475 476# start doing a rebase with git-merge 477# this is rename-aware if the recursive (default) strategy is used 478 479run_specific_rebase