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