git-rebase.sh 14.9 KB
Newer Older
1 2 3 4 5
#!/bin/sh
#
# Copyright (c) 2005 Junio C Hamano.
#

6
USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] (<upstream>|--root) [<branch>] [--quiet | -q]'
7 8 9 10 11
LONG_USAGE='git-rebase replaces <branch> with a new branch of the
same name.  When the --onto option is provided the new branch starts
out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
It then attempts to create a new commit for each commit from the original
<branch> that does not exist in the <upstream> branch.
12

13 14
It is possible that a merge failure will prevent this process from being
completely automatic.  You will have to resolve any such merge failure
15 16
and run git rebase --continue.  Another option is to bypass the commit
that caused the merge failure with git rebase --skip.  To restore the
17 18
original <branch> and remove the .git/rebase-apply working files, use the
command git rebase --abort instead.
19

20
Note that if <branch> is not specified on the command line, the
21
currently checked out branch is used.
22

23
Example:       git-rebase master~1 topic
24

25 26 27
        A---B---C topic                   A'\''--B'\''--C'\'' topic
       /                   -->           /
  D---E---F---G master          D---E---F---G master
28
'
29 30

SUBDIRECTORY_OK=Yes
31
OPTIONS_SPEC=
32
. git-sh-setup
33
set_reflog_action rebase
34
require_work_tree
35
cd_to_toplevel
36

37 38
LF='
'
N
Nanako Shiraishi 已提交
39
OK_TO_SKIP_PRE_REBASE=
40 41 42 43 44
RESOLVEMSG="
When you have resolved this problem run \"git rebase --continue\".
If you would prefer to skip this patch, instead run \"git rebase --skip\".
To restore the original branch and stop rebasing run \"git rebase --abort\".
"
45
unset newbase
J
Junio C Hamano 已提交
46
strategy=recursive
47
strategy_opts=
48
do_merge=
49
dotest="$GIT_DIR"/rebase-merge
50
prec=4
51
verbose=
52 53
diffstat=
test "$(git config --bool rebase.stat)" = true && diffstat=t
M
Michael S. Tsirkin 已提交
54
git_am_opt=
55
rebase_root=
56
force_rebase=
57
allow_rerere_autoupdate=
58 59 60 61 62

continue_merge () {
	test -n "$prev_head" || die "prev_head must be defined"
	test -d "$dotest" || die "$dotest directory does not exist"

63
	unmerged=$(git ls-files -u)
64 65 66
	if test -n "$unmerged"
	then
		echo "You still have unmerged paths in your index"
67
		echo "did you forget to use git add?"
68
		die "$RESOLVEMSG"
69 70
	fi

71
	cmt=`cat "$dotest/current"`
72
	if ! git diff-index --quiet --ignore-submodules HEAD --
73
	then
74
		if ! git commit --no-verify -C "$cmt"
75 76 77 78 79
		then
			echo "Commit failed, please do not call \"git commit\""
			echo "directly, but instead do one of the following: "
			die "$RESOLVEMSG"
		fi
S
Stephen Boyd 已提交
80 81 82 83
		if test -z "$GIT_QUIET"
		then
			printf "Committed: %0${prec}d " $msgnum
		fi
T
Thomas Rast 已提交
84
		echo "$cmt $(git rev-parse HEAD^0)" >> "$dotest/rewritten"
85
	else
S
Stephen Boyd 已提交
86 87 88 89 90
		if test -z "$GIT_QUIET"
		then
			printf "Already applied: %0${prec}d " $msgnum
		fi
	fi
91
	test -z "$GIT_QUIET" &&
92
	GIT_PAGER='' git log --format=%s -1 "$cmt"
93

94
	prev_head=`git rev-parse HEAD^0`
95 96 97 98 99
	# save the resulting commit so we can read-tree on it later
	echo "$prev_head" > "$dotest/prev_head"

	# onto the next patch:
	msgnum=$(($msgnum + 1))
100
	echo "$msgnum" >"$dotest/msgnum"
101 102 103
}

call_merge () {
104
	cmt="$(cat "$dotest/cmt.$1")"
105
	echo "$cmt" > "$dotest/current"
106
	hd=$(git rev-parse --verify HEAD)
107
	cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
108 109
	msgnum=$(cat "$dotest/msgnum")
	end=$(cat "$dotest/end")
110
	eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
111
	eval GITHEAD_$hd='$(cat "$dotest/onto_name")'
112
	export GITHEAD_$cmt GITHEAD_$hd
S
Stephen Boyd 已提交
113 114
	if test -n "$GIT_QUIET"
	then
115
		GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
S
Stephen Boyd 已提交
116
	fi
117
	eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
118 119 120
	rv=$?
	case "$rv" in
	0)
121
		unset GITHEAD_$cmt GITHEAD_$hd
122
		return
123 124
		;;
	1)
125
		git rerere $allow_rerere_autoupdate
126
		die "$RESOLVEMSG"
127 128 129
		;;
	2)
		echo "Strategy: $rv $strategy failed, try another" 1>&2
130
		die "$RESOLVEMSG"
131 132 133 134 135 136 137 138
		;;
	*)
		die "Unknown exit code ($rv) from command:" \
			"git-merge-$strategy $cmt^ -- HEAD $cmt"
		;;
	esac
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
move_to_original_branch () {
	test -z "$head_name" &&
		head_name="$(cat "$dotest"/head-name)" &&
		onto="$(cat "$dotest"/onto)" &&
		orig_head="$(cat "$dotest"/orig-head)"
	case "$head_name" in
	refs/*)
		message="rebase finished: $head_name onto $onto"
		git update-ref -m "$message" \
			$head_name $(git rev-parse HEAD) $orig_head &&
		git symbolic-ref HEAD $head_name ||
		die "Could not move back to $head_name"
		;;
	esac
}

155
finish_rb_merge () {
156
	move_to_original_branch
157
	git notes copy --for-rewrite=rebase < "$dotest"/rewritten
T
Thomas Rast 已提交
158 159 160 161
	if test -x "$GIT_DIR"/hooks/post-rewrite &&
		test -s "$dotest"/rewritten; then
		"$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
	fi
162
	rm -r "$dotest"
S
Stephen Boyd 已提交
163
	say All done.
164 165
}

166
is_interactive () {
167 168 169 170 171 172 173 174 175 176 177
	while test $# != 0
	do
		case "$1" in
			-i|--interactive)
				interactive_rebase=explicit
				break
			;;
			-p|--preserve-merges)
				interactive_rebase=implied
			;;
		esac
178
		shift
179 180 181 182 183 184 185 186
	done

	if [ "$interactive_rebase" = implied ]; then
		GIT_EDITOR=:
		export GIT_EDITOR
	fi

	test -n "$interactive_rebase" || test -f "$dotest"/interactive
187 188
}

189
run_pre_rebase_hook () {
N
Nanako Shiraishi 已提交
190 191
	if test -z "$OK_TO_SKIP_PRE_REBASE" &&
	   test -x "$GIT_DIR/hooks/pre-rebase"
192
	then
193 194
		"$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
		die "The pre-rebase hook refused to rebase."
195 196 197
	fi
}

198 199 200
test -f "$GIT_DIR"/rebase-apply/applying &&
	die 'It looks like git-am is in progress. Cannot rebase.'

201 202
is_interactive "$@" && exec git-rebase--interactive "$@"

203
while test $# != 0
204 205
do
	case "$1" in
N
Nanako Shiraishi 已提交
206 207 208
	--no-verify)
		OK_TO_SKIP_PRE_REBASE=yes
		;;
209
	--continue)
210
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
211 212
			die "No rebase in progress?"

213
		git update-index --ignore-submodules --refresh &&
214
		git diff-files --quiet --ignore-submodules || {
215
			echo "You must edit all merge conflicts and then"
216
			echo "mark them as resolved using git add"
217
			exit 1
218
		}
219 220
		if test -d "$dotest"
		then
221 222 223 224
			prev_head=$(cat "$dotest/prev_head")
			end=$(cat "$dotest/end")
			msgnum=$(cat "$dotest/msgnum")
			onto=$(cat "$dotest/onto")
S
Stephen Boyd 已提交
225
			GIT_QUIET=$(cat "$dotest/quiet")
226 227 228 229 230 231 232 233 234
			continue_merge
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
		fi
235 236 237
		head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
		onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
		orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
S
Stephen Boyd 已提交
238
		GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
239 240
		git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
		move_to_original_branch
241 242 243
		exit
		;;
	--skip)
244
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
245 246
			die "No rebase in progress?"

247
		git reset --hard HEAD || exit $?
248 249
		if test -d "$dotest"
		then
250
			git rerere clear
251 252 253
			prev_head=$(cat "$dotest/prev_head")
			end=$(cat "$dotest/end")
			msgnum=$(cat "$dotest/msgnum")
254
			msgnum=$(($msgnum + 1))
255
			onto=$(cat "$dotest/onto")
S
Stephen Boyd 已提交
256
			GIT_QUIET=$(cat "$dotest/quiet")
257 258 259 260 261 262 263
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
264
		fi
265 266 267
		head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) &&
		onto=$(cat "$GIT_DIR"/rebase-apply/onto) &&
		orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) &&
S
Stephen Boyd 已提交
268
		GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet)
269 270
		git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
		move_to_original_branch
271 272 273
		exit
		;;
	--abort)
274
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
275 276
			die "No rebase in progress?"

277
		git rerere clear
278 279 280 281 282 283 284 285 286 287

		test -d "$dotest" || dotest="$GIT_DIR"/rebase-apply

		head_name="$(cat "$dotest"/head-name)" &&
		case "$head_name" in
		refs/*)
			git symbolic-ref HEAD $head_name ||
			die "Could not move back to $head_name"
			;;
		esac
288
		git reset --hard $(cat "$dotest/orig-head")
289
		rm -r "$dotest"
290 291
		exit
		;;
292 293 294 295 296
	--onto)
		test 2 -le "$#" || usage
		newbase="$2"
		shift
		;;
297 298 299
	-M|-m|--m|--me|--mer|--merg|--merge)
		do_merge=t
		;;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
	-X*|--strategy-option*)
		case "$#,$1" in
		1,-X|1,--strategy-option)
			usage ;;
		*,-X|*,--strategy-option)
			newopt="$2"
			shift ;;
		*,--strategy-option=*)
			newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
		*,-X*)
			newopt="$(expr " $1" : ' -X\(.*\)')" ;;
		1,*)
			usage ;;
		esac
		strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
		do_merge=t
		;;
317 318 319 320 321
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
322
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
323 324 325 326 327 328 329 330
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
331 332 333 334 335 336
	-n|--no-stat)
		diffstat=
		;;
	--stat)
		diffstat=t
		;;
337 338
	-v|--verbose)
		verbose=t
339
		diffstat=t
S
Stephen Boyd 已提交
340 341 342 343 344 345 346
		GIT_QUIET=
		;;
	-q|--quiet)
		GIT_QUIET=t
		git_am_opt="$git_am_opt -q"
		verbose=
		diffstat=
347
		;;
348 349
	--whitespace=*)
		git_am_opt="$git_am_opt $1"
350 351 352 353 354
		case "$1" in
		--whitespace=fix|--whitespace=strip)
			force_rebase=t
			;;
		esac
355
		;;
356 357 358
	--ignore-whitespace)
		git_am_opt="$git_am_opt $1"
		;;
359 360 361 362
	--committer-date-is-author-date|--ignore-date)
		git_am_opt="$git_am_opt $1"
		force_rebase=t
		;;
M
Michael S. Tsirkin 已提交
363
	-C*)
364
		git_am_opt="$git_am_opt $1"
M
Michael S. Tsirkin 已提交
365
		;;
366 367 368
	--root)
		rebase_root=t
		;;
369
	-f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
370 371
		force_rebase=t
		;;
372 373 374
	--rerere-autoupdate|--no-rerere-autoupdate)
		allow_rerere_autoupdate="$1"
		;;
375 376 377 378 379 380 381 382 383
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
384
test $# -gt 2 && usage
385

386 387 388 389 390 391 392
if test $# -eq 0 && test -z "$rebase_root"
then
	test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || usage
	test -d "$dotest" -o -f "$GIT_DIR"/rebase-apply/rebasing &&
		die 'A rebase is in progress, try --continue, --skip or --abort.'
fi

393
# Make sure we do not have $GIT_DIR/rebase-apply
394
if test -z "$do_merge"
395
then
396
	if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null
397
	then
398
		rmdir "$GIT_DIR"/rebase-apply
399 400
	else
		echo >&2 '
401 402
It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
403 404
rebase.  If that is not the case, please
	rm -fr '"$GIT_DIR"'/rebase-apply
405
and run me again.  I am stopping in case you still have something
406
valuable there.'
407 408 409 410 411
		exit 1
	fi
else
	if test -d "$dotest"
	then
412
		die "previous rebase directory $dotest still exists." \
413
			'Try git rebase (--continue | --abort | --skip)'
414
	fi
415 416
fi

417
# The tree must be really really clean.
418 419
if ! git update-index --ignore-submodules --refresh > /dev/null; then
	echo >&2 "cannot rebase: you have unstaged changes"
420
	git diff-files --name-status -r --ignore-submodules -- >&2
421
	exit 1
422
fi
423
diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)
L
Lukas Sandström 已提交
424
case "$diff" in
425 426
?*)	echo >&2 "cannot rebase: your index contains uncommitted changes"
	echo >&2 "$diff"
427 428 429
	exit 1
	;;
esac
J
Junio C Hamano 已提交
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
if test -z "$rebase_root"
then
	# The upstream head must be given.  Make sure it is valid.
	upstream_name="$1"
	shift
	upstream=`git rev-parse --verify "${upstream_name}^0"` ||
	die "invalid upstream $upstream_name"
	unset root_flag
	upstream_arg="$upstream_name"
else
	test -z "$newbase" && die "--root must be used with --onto"
	unset upstream_name
	unset upstream
	root_flag="--root"
	upstream_arg="$root_flag"
fi
L
Lukas Sandström 已提交
447

448 449
# Make sure the branch to rebase onto is valid.
onto_name=${newbase-"$upstream_name"}
450 451 452 453 454 455 456 457 458 459 460 461 462 463
case "$onto_name" in
*...*)
	if	left=${onto_name%...*} right=${onto_name#*...} &&
		onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
	then
		case "$onto" in
		?*"$LF"?*)
			die "$onto_name: there are more than one merge bases"
			;;
		'')
			die "$onto_name: there is no merge base"
			;;
		esac
	else
464
		die "$onto_name: there is no merge base"
465 466 467
	fi
	;;
*)
468
	onto=$(git rev-parse --verify "${onto_name}^0") || exit
469 470
	;;
esac
471

472
# If a hook exists, give it a chance to interrupt
473
run_pre_rebase_hook "$upstream_arg" "$@"
474

475 476 477 478 479
# If the branch to rebase is given, that is the branch we will rebase
# $branch_name -- branch being rebased, or HEAD (already detached)
# $orig_head -- commit object name of tip of the branch before rebasing
# $head_name -- refs/heads/<that-branch> or "detached HEAD"
switch_to=
480
case "$#" in
481
1)
482
	# Is it "rebase other $branchname" or "rebase other $commit"?
483 484
	branch_name="$1"
	switch_to="$1"
485

486 487
	if git show-ref --verify --quiet -- "refs/heads/$1" &&
	   branch=$(git rev-parse -q --verify "refs/heads/$1")
488
	then
489 490
		head_name="refs/heads/$1"
	elif branch=$(git rev-parse -q --verify "$1")
491 492 493 494 495
	then
		head_name="detached HEAD"
	else
		usage
	fi
496 497
	;;
*)
498
	# Do not need to switch branches, we are already on it.
499 500
	if branch_name=`git symbolic-ref -q HEAD`
	then
501
		head_name=$branch_name
502 503
		branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
	else
504
		head_name="detached HEAD"
505 506
		branch_name=HEAD ;# detached
	fi
507
	branch=$(git rev-parse --verify "${branch_name}^0") || exit
508
	;;
509
esac
510
orig_head=$branch
511

512 513
# Now we are rebasing commits $upstream..$branch (or with --root,
# everything leading up to $branch) on top of $onto
514

515 516
# Check if we are already based on $onto with linear history,
# but this should be done only when upstream and onto are the same.
517
mb=$(git merge-base "$onto" "$branch")
518 519
if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
	# linear history?
520
	! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
521
then
522 523 524
	if test -z "$force_rebase"
	then
		# Lazily switch to the target branch if needed...
525
		test -z "$switch_to" || git checkout "$switch_to" --
S
Stephen Boyd 已提交
526
		say "Current branch $branch_name is up to date."
527 528
		exit 0
	else
S
Stephen Boyd 已提交
529
		say "Current branch $branch_name is up to date, rebase forced."
530
	fi
531 532
fi

533
# Detach HEAD and reset the tree
S
Stephen Boyd 已提交
534
say "First, rewinding head to replay your work on top of it..."
535
git checkout -q "$onto^0" || die "could not detach HEAD"
536
git update-ref ORIG_HEAD $branch
L
Lukas Sandström 已提交
537

538 539 540 541 542 543 544 545 546 547
if test -n "$diffstat"
then
	if test -n "$verbose"
	then
		echo "Changes from $mb to $onto:"
	fi
	# We want color (if set), but no pager
	GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
fi

548
# If the $onto is a proper descendant of the tip of the branch, then
549
# we just fast-forwarded.
550
if test "$mb" = "$branch"
L
Lukas Sandström 已提交
551
then
S
Stephen Boyd 已提交
552
	say "Fast-forwarded $branch_name to $onto_name."
553
	move_to_original_branch
L
Lukas Sandström 已提交
554 555 556
	exit 0
fi

557 558 559 560 561 562 563
if test -n "$rebase_root"
then
	revisions="$onto..$orig_head"
else
	revisions="$upstream..$orig_head"
fi

564 565
if test -z "$do_merge"
then
566
	git format-patch -k --stdout --full-index --ignore-if-in-upstream \
567
		--src-prefix=a/ --dst-prefix=b/ \
568
		--no-renames $root_flag "$revisions" |
J
Junio C Hamano 已提交
569
	git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
570 571
	move_to_original_branch
	ret=$?
572 573 574
	test 0 != $ret -a -d "$GIT_DIR"/rebase-apply &&
		echo $head_name > "$GIT_DIR"/rebase-apply/head-name &&
		echo $onto > "$GIT_DIR"/rebase-apply/onto &&
S
Stephen Boyd 已提交
575 576
		echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head &&
		echo "$GIT_QUIET" > "$GIT_DIR"/rebase-apply/quiet
577
	exit $ret
578 579 580 581 582 583 584
fi

# start doing a rebase with git-merge
# this is rename-aware if the recursive (default) strategy is used

mkdir -p "$dotest"
echo "$onto" > "$dotest/onto"
585
echo "$onto_name" > "$dotest/onto_name"
586
prev_head=$orig_head
587
echo "$prev_head" > "$dotest/prev_head"
588 589
echo "$orig_head" > "$dotest/orig-head"
echo "$head_name" > "$dotest/head-name"
S
Stephen Boyd 已提交
590
echo "$GIT_QUIET" > "$dotest/quiet"
591 592

msgnum=0
593
for cmt in `git rev-list --reverse --no-merges "$revisions"`
594 595
do
	msgnum=$(($msgnum + 1))
596
	echo "$cmt" > "$dotest/cmt.$msgnum"
597 598
done

599 600
echo 1 >"$dotest/msgnum"
echo $msgnum >"$dotest/end"
601 602 603 604 605 606 607 608 609

end=$msgnum
msgnum=1

while test "$msgnum" -le "$end"
do
	call_merge "$msgnum"
	continue_merge
done
610

611
finish_rb_merge