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