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