git-rebase.sh 14.2 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='
'
39 40
ok_to_skip_pre_rebase=
resolvemsg="
41 42 43 44
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 onto
46
strategy=
47
strategy_opts=
48
do_merge=
49 50
merge_dir="$GIT_DIR"/rebase-merge
apply_dir="$GIT_DIR"/rebase-apply
51
prec=4
52
verbose=
53 54
diffstat=
test "$(git config --bool rebase.stat)" = true && diffstat=t
M
Michael S. Tsirkin 已提交
55
git_am_opt=
56
rebase_root=
57
force_rebase=
58
allow_rerere_autoupdate=
59 60 61 62 63 64
# Non-empty if a rebase was in progress when 'git rebase' was invoked
in_progress=
# One of {am, merge, interactive}
type=
# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
state_dir=
65 66
# One of {'', continue, skip, abort}, as parsed from command line
action=
67 68 69
preserve_merges=
autosquash=
test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
70

71
read_state () {
72
	if test "$type" = merge
73
	then
74 75 76
		onto_name=$(cat "$state_dir"/onto_name) &&
		end=$(cat "$state_dir"/end) &&
		msgnum=$(cat "$state_dir"/msgnum)
77 78 79 80 81 82 83
	fi &&
	head_name=$(cat "$state_dir"/head-name) &&
	onto=$(cat "$state_dir"/onto) &&
	orig_head=$(cat "$state_dir"/orig-head) &&
	GIT_QUIET=$(cat "$state_dir"/quiet)
}

84
continue_merge () {
85
	test -d "$merge_dir" || die "$merge_dir directory does not exist"
86

87
	unmerged=$(git ls-files -u)
88 89 90
	if test -n "$unmerged"
	then
		echo "You still have unmerged paths in your index"
91
		echo "did you forget to use git add?"
92
		die "$resolvemsg"
93 94
	fi

95
	cmt=`cat "$merge_dir/current"`
96
	if ! git diff-index --quiet --ignore-submodules HEAD --
97
	then
98
		if ! git commit --no-verify -C "$cmt"
99 100 101
		then
			echo "Commit failed, please do not call \"git commit\""
			echo "directly, but instead do one of the following: "
102
			die "$resolvemsg"
103
		fi
S
Stephen Boyd 已提交
104 105 106 107
		if test -z "$GIT_QUIET"
		then
			printf "Committed: %0${prec}d " $msgnum
		fi
108
		echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
109
	else
S
Stephen Boyd 已提交
110 111 112 113 114
		if test -z "$GIT_QUIET"
		then
			printf "Already applied: %0${prec}d " $msgnum
		fi
	fi
115
	test -z "$GIT_QUIET" &&
116
	GIT_PAGER='' git log --format=%s -1 "$cmt"
117 118 119

	# onto the next patch:
	msgnum=$(($msgnum + 1))
120
	echo "$msgnum" >"$merge_dir/msgnum"
121 122 123
}

call_merge () {
124 125
	cmt="$(cat "$merge_dir/cmt.$1")"
	echo "$cmt" > "$merge_dir/current"
126
	hd=$(git rev-parse --verify HEAD)
127
	cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
128
	msgnum=$(cat "$merge_dir/msgnum")
129
	eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
130
	eval GITHEAD_$hd='$onto_name'
131
	export GITHEAD_$cmt GITHEAD_$hd
S
Stephen Boyd 已提交
132 133
	if test -n "$GIT_QUIET"
	then
134
		GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
S
Stephen Boyd 已提交
135
	fi
136
	test -z "$strategy" && strategy=recursive
137
	eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
138 139 140
	rv=$?
	case "$rv" in
	0)
141
		unset GITHEAD_$cmt GITHEAD_$hd
142
		return
143 144
		;;
	1)
145
		git rerere $allow_rerere_autoupdate
146
		die "$resolvemsg"
147 148 149
		;;
	2)
		echo "Strategy: $rv $strategy failed, try another" 1>&2
150
		die "$resolvemsg"
151 152 153 154 155 156 157 158
		;;
	*)
		die "Unknown exit code ($rv) from command:" \
			"git-merge-$strategy $cmt^ -- HEAD $cmt"
		;;
	esac
}

159 160 161 162 163 164 165 166 167 168 169 170
move_to_original_branch () {
	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
}

171
finish_rb_merge () {
172
	move_to_original_branch
173
	git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
T
Thomas Rast 已提交
174
	if test -x "$GIT_DIR"/hooks/post-rewrite &&
175 176
		test -s "$merge_dir"/rewritten; then
		"$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
T
Thomas Rast 已提交
177
	fi
178
	rm -r "$merge_dir"
S
Stephen Boyd 已提交
179
	say All done.
180 181
}

182
run_interactive_rebase () {
183 184 185 186
	if [ "$interactive_rebase" = implied ]; then
		GIT_EDITOR=:
		export GIT_EDITOR
	fi
187
	. git-rebase--interactive "$@"
188 189
}

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

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

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
if test -d "$apply_dir"
then
	type=am
	state_dir="$apply_dir"
elif test -d "$merge_dir"
then
	if test -f "$merge_dir"/interactive
	then
		type=interactive
		interactive_rebase=explicit
	else
		type=merge
	fi
	state_dir="$merge_dir"
fi
test -n "$type" && in_progress=t

219
total_argc=$#
220
while test $# != 0
221 222
do
	case "$1" in
N
Nanako Shiraishi 已提交
223
	--no-verify)
224
		ok_to_skip_pre_rebase=yes
N
Nanako Shiraishi 已提交
225
		;;
M
Martin von Zweigbergk 已提交
226
	--verify)
227
		ok_to_skip_pre_rebase=
M
Martin von Zweigbergk 已提交
228
		;;
229
	--continue|--skip|--abort)
230
		test $total_argc -eq 1 || usage
231
		action=${1##--}
232
		;;
233 234
	--onto)
		test 2 -le "$#" || usage
235
		onto="$2"
236 237
		shift
		;;
238 239 240 241 242 243 244 245 246 247 248 249 250
	-i|--interactive)
		interactive_rebase=explicit
		;;
	-p|--preserve-merges)
		preserve_merges=t
		test -z "$interactive_rebase" && interactive_rebase=implied
		;;
	--autosquash)
		autosquash=t
		;;
	--no-autosquash)
		autosquash=
		;;
251 252 253
	-M|-m|--m|--me|--mer|--merg|--merge)
		do_merge=t
		;;
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
	-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
270
		test -z "$strategy" && strategy=recursive
271
		;;
272 273 274 275 276
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
277
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
278 279 280 281 282 283 284 285
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
286 287 288 289 290 291
	-n|--no-stat)
		diffstat=
		;;
	--stat)
		diffstat=t
		;;
292 293
	-v|--verbose)
		verbose=t
294
		diffstat=t
S
Stephen Boyd 已提交
295 296 297 298 299 300 301
		GIT_QUIET=
		;;
	-q|--quiet)
		GIT_QUIET=t
		git_am_opt="$git_am_opt -q"
		verbose=
		diffstat=
302
		;;
303 304
	--whitespace=*)
		git_am_opt="$git_am_opt $1"
305 306 307 308 309
		case "$1" in
		--whitespace=fix|--whitespace=strip)
			force_rebase=t
			;;
		esac
310
		;;
311 312 313
	--ignore-whitespace)
		git_am_opt="$git_am_opt $1"
		;;
314 315 316 317
	--committer-date-is-author-date|--ignore-date)
		git_am_opt="$git_am_opt $1"
		force_rebase=t
		;;
M
Michael S. Tsirkin 已提交
318
	-C*)
319
		git_am_opt="$git_am_opt $1"
M
Michael S. Tsirkin 已提交
320
		;;
321 322 323
	--root)
		rebase_root=t
		;;
324
	-f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
325 326
		force_rebase=t
		;;
327 328 329
	--rerere-autoupdate|--no-rerere-autoupdate)
		allow_rerere_autoupdate="$1"
		;;
330 331 332 333 334 335 336 337 338
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
339
test $# -gt 2 && usage
340

341 342 343 344 345
if test -n "$action"
then
	test -z "$in_progress" && die "No rebase in progress?"
	test "$type" = interactive && run_interactive_rebase
fi
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366

case "$action" in
continue)
	git update-index --ignore-submodules --refresh &&
	git diff-files --quiet --ignore-submodules || {
		echo "You must edit all merge conflicts and then"
		echo "mark them as resolved using git add"
		exit 1
	}
	read_state
	if test -d "$merge_dir"
	then
		continue_merge
		while test "$msgnum" -le "$end"
		do
			call_merge "$msgnum"
			continue_merge
		done
		finish_rb_merge
		exit
	fi
367
	git am --resolved --3way --resolvemsg="$resolvemsg" &&
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
	move_to_original_branch
	exit
	;;
skip)
	git reset --hard HEAD || exit $?
	read_state
	if test -d "$merge_dir"
	then
		git rerere clear
		msgnum=$(($msgnum + 1))
		while test "$msgnum" -le "$end"
		do
			call_merge "$msgnum"
			continue_merge
		done
		finish_rb_merge
		exit
	fi
386
	git am -3 --skip --resolvemsg="$resolvemsg" &&
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
	move_to_original_branch
	exit
	;;
abort)
	git rerere clear
	read_state
	case "$head_name" in
	refs/*)
		git symbolic-ref HEAD $head_name ||
		die "Could not move back to $head_name"
		;;
	esac
	git reset --hard $orig_head
	rm -r "$state_dir"
	exit
	;;
esac

405 406
# Make sure no rebase is in progress
if test -n "$in_progress"
407
then
408 409 410 411 412 413 414
	die '
It seems that there is already a '"${state_dir##*/}"' directory, and
I wonder if you are in the middle of another rebase.  If that is the
case, please try
	git rebase (--continue | --abort | --skip)
If that is not the case, please
	rm -fr '"$state_dir"'
415
and run me again.  I am stopping in case you still have something
416
valuable there.'
417 418
fi

419 420
test $# -eq 0 && test -z "$rebase_root" && usage

421 422 423 424 425 426 427 428 429 430 431 432 433
if test -n "$interactive_rebase"
then
	type=interactive
	state_dir="$merge_dir"
elif test -n "$do_merge"
then
	type=merge
	state_dir="$merge_dir"
else
	type=am
	state_dir="$apply_dir"
fi

434 435 436 437 438 439 440 441 442 443
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
444
	test -z "$onto" && die "You must specify --onto when using --root"
445 446 447 448 449
	unset upstream_name
	unset upstream
	root_flag="--root"
	upstream_arg="$root_flag"
fi
L
Lukas Sandström 已提交
450

451
# Make sure the branch to rebase onto is valid.
452
onto_name=${onto-"$upstream_name"}
453 454 455 456 457 458 459 460 461 462 463 464 465 466
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
467
		die "$onto_name: there is no merge base"
468 469 470
	fi
	;;
*)
471 472
	onto=$(git rev-parse --verify "${onto_name}^0") ||
	die "Does not point to a valid commit: $1"
473 474
	;;
esac
475

476 477 478 479 480
# 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=
481
case "$#" in
482
1)
483
	# Is it "rebase other $branchname" or "rebase other $commit"?
484 485
	branch_name="$1"
	switch_to="$1"
486

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

514 515
test "$type" = interactive && run_interactive_rebase "$@"

516 517
require_clean_work_tree "rebase" "Please commit or stash them."

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

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

539 540 541
# If a hook exists, give it a chance to interrupt
run_pre_rebase_hook "$upstream_arg" "$@"

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

547 548 549 550 551 552 553 554 555 556
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

557
# If the $onto is a proper descendant of the tip of the branch, then
558
# we just fast-forwarded.
559
if test "$mb" = "$branch"
L
Lukas Sandström 已提交
560
then
S
Stephen Boyd 已提交
561
	say "Fast-forwarded $branch_name to $onto_name."
562
	move_to_original_branch
L
Lukas Sandström 已提交
563 564 565
	exit 0
fi

566 567 568 569 570 571 572
if test -n "$rebase_root"
then
	revisions="$onto..$orig_head"
else
	revisions="$upstream..$orig_head"
fi

573 574
if test -z "$do_merge"
then
575
	git format-patch -k --stdout --full-index --ignore-if-in-upstream \
576
		--src-prefix=a/ --dst-prefix=b/ \
577
		--no-renames $root_flag "$revisions" |
578
	git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" &&
579 580
	move_to_original_branch
	ret=$?
581 582 583 584 585
	test 0 != $ret -a -d "$apply_dir" &&
		echo $head_name > "$apply_dir/head-name" &&
		echo $onto > "$apply_dir/onto" &&
		echo $orig_head > "$apply_dir/orig-head" &&
		echo "$GIT_QUIET" > "$apply_dir/quiet"
586
	exit $ret
587 588 589 590 591
fi

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

592 593 594
mkdir -p "$merge_dir"
echo "$onto_name" > "$merge_dir/onto_name"
echo "$head_name" > "$merge_dir/head-name"
595 596
echo "$onto" > "$merge_dir/onto"
echo "$orig_head" > "$merge_dir/orig-head"
597
echo "$GIT_QUIET" > "$merge_dir/quiet"
598 599

msgnum=0
600
for cmt in `git rev-list --reverse --no-merges "$revisions"`
601 602
do
	msgnum=$(($msgnum + 1))
603
	echo "$cmt" > "$merge_dir/cmt.$msgnum"
604 605
done

606 607
echo 1 >"$merge_dir/msgnum"
echo $msgnum >"$merge_dir/end"
608 609 610 611 612 613 614 615 616

end=$msgnum
msgnum=1

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

618
finish_rb_merge