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