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

6
USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<branch>]'
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

N
Nanako Shiraishi 已提交
37
OK_TO_SKIP_PRE_REBASE=
38 39 40 41 42
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\".
"
43
unset newbase
J
Junio C Hamano 已提交
44
strategy=recursive
45
do_merge=
46
dotest="$GIT_DIR"/rebase-merge
47
prec=4
48
verbose=
49
diffstat=$(git config --bool rebase.stat)
M
Michael S. Tsirkin 已提交
50
git_am_opt=
51
rebase_root=
52
force_rebase=
53 54 55 56 57

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

58
	unmerged=$(git ls-files -u)
59 60 61
	if test -n "$unmerged"
	then
		echo "You still have unmerged paths in your index"
62
		echo "did you forget to use git add?"
63
		die "$RESOLVEMSG"
64 65
	fi

66
	cmt=`cat "$dotest/current"`
67
	if ! git diff-index --quiet --ignore-submodules HEAD --
68
	then
69
		if ! git commit --no-verify -C "$cmt"
70 71 72 73 74
		then
			echo "Commit failed, please do not call \"git commit\""
			echo "directly, but instead do one of the following: "
			die "$RESOLVEMSG"
		fi
75
		printf "Committed: %0${prec}d " $msgnum
76
	else
77
		printf "Already applied: %0${prec}d " $msgnum
78
	fi
79
	git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //'
80

81
	prev_head=`git rev-parse HEAD^0`
82 83 84 85 86
	# 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))
87
	echo "$msgnum" >"$dotest/msgnum"
88 89 90
}

call_merge () {
91
	cmt="$(cat "$dotest/cmt.$1")"
92
	echo "$cmt" > "$dotest/current"
93
	hd=$(git rev-parse --verify HEAD)
94
	cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
95 96
	msgnum=$(cat "$dotest/msgnum")
	end=$(cat "$dotest/end")
97
	eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
98
	eval GITHEAD_$hd='$(cat "$dotest/onto_name")'
99 100
	export GITHEAD_$cmt GITHEAD_$hd
	git-merge-$strategy "$cmt^" -- "$hd" "$cmt"
101 102 103
	rv=$?
	case "$rv" in
	0)
104
		unset GITHEAD_$cmt GITHEAD_$hd
105
		return
106 107
		;;
	1)
108
		git rerere
109
		die "$RESOLVEMSG"
110 111 112
		;;
	2)
		echo "Strategy: $rv $strategy failed, try another" 1>&2
113
		die "$RESOLVEMSG"
114 115 116 117 118 119 120 121
		;;
	*)
		die "Unknown exit code ($rv) from command:" \
			"git-merge-$strategy $cmt^ -- HEAD $cmt"
		;;
	esac
}

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
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
}

138
finish_rb_merge () {
139
	move_to_original_branch
140 141 142 143
	rm -r "$dotest"
	echo "All done."
}

144
is_interactive () {
145 146 147 148 149 150 151 152 153 154 155
	while test $# != 0
	do
		case "$1" in
			-i|--interactive)
				interactive_rebase=explicit
				break
			;;
			-p|--preserve-merges)
				interactive_rebase=implied
			;;
		esac
156
		shift
157 158 159 160 161 162 163 164
	done

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

	test -n "$interactive_rebase" || test -f "$dotest"/interactive
165 166
}

167
run_pre_rebase_hook () {
N
Nanako Shiraishi 已提交
168 169
	if test -z "$OK_TO_SKIP_PRE_REBASE" &&
	   test -x "$GIT_DIR/hooks/pre-rebase"
170 171 172 173 174 175 176 177
	then
		"$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
			echo >&2 "The pre-rebase hook refused to rebase."
			exit 1
		}
	fi
}

178 179 180
test -f "$GIT_DIR"/rebase-apply/applying &&
	die 'It looks like git-am is in progress. Cannot rebase.'

181 182
is_interactive "$@" && exec git-rebase--interactive "$@"

183 184 185 186 187 188 189 190
if test $# -eq 0
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.'
	die "No arguments given and $GIT_DIR/rebase-apply already exists."
fi

191
while test $# != 0
192 193
do
	case "$1" in
N
Nanako Shiraishi 已提交
194 195 196
	--no-verify)
		OK_TO_SKIP_PRE_REBASE=yes
		;;
197
	--continue)
198
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
199 200
			die "No rebase in progress?"

201
		git diff-files --quiet --ignore-submodules || {
202
			echo "You must edit all merge conflicts and then"
203
			echo "mark them as resolved using git add"
204
			exit 1
205
		}
206 207
		if test -d "$dotest"
		then
208 209 210 211
			prev_head=$(cat "$dotest/prev_head")
			end=$(cat "$dotest/end")
			msgnum=$(cat "$dotest/msgnum")
			onto=$(cat "$dotest/onto")
212 213 214 215 216 217 218 219 220
			continue_merge
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
		fi
221 222 223
		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) &&
224 225
		git am --resolved --3way --resolvemsg="$RESOLVEMSG" &&
		move_to_original_branch
226 227 228
		exit
		;;
	--skip)
229
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
230 231
			die "No rebase in progress?"

232
		git reset --hard HEAD || exit $?
233 234
		if test -d "$dotest"
		then
235
			git rerere clear
236 237 238
			prev_head=$(cat "$dotest/prev_head")
			end=$(cat "$dotest/end")
			msgnum=$(cat "$dotest/msgnum")
239
			msgnum=$(($msgnum + 1))
240
			onto=$(cat "$dotest/onto")
241 242 243 244 245 246 247
			while test "$msgnum" -le "$end"
			do
				call_merge "$msgnum"
				continue_merge
			done
			finish_rb_merge
			exit
248
		fi
249 250 251
		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) &&
252 253
		git am -3 --skip --resolvemsg="$RESOLVEMSG" &&
		move_to_original_branch
254 255 256
		exit
		;;
	--abort)
257
		test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply ||
258 259
			die "No rebase in progress?"

260
		git rerere clear
261 262
		if test -d "$dotest"
		then
263
			move_to_original_branch
264
		else
265
			dotest="$GIT_DIR"/rebase-apply
266
			move_to_original_branch
267
		fi
268
		git reset --hard $(cat "$dotest/orig-head")
269
		rm -r "$dotest"
270 271
		exit
		;;
272 273 274 275 276
	--onto)
		test 2 -le "$#" || usage
		newbase="$2"
		shift
		;;
277 278 279 280 281 282 283 284
	-M|-m|--m|--me|--mer|--merg|--merge)
		do_merge=t
		;;
	-s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
		--strateg=*|--strategy=*|\
	-s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
		case "$#,$1" in
		*,*=*)
D
Dennis Stosberg 已提交
285
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
286 287 288 289 290 291 292 293
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
294 295 296 297 298 299
	-n|--no-stat)
		diffstat=
		;;
	--stat)
		diffstat=t
		;;
300 301
	-v|--verbose)
		verbose=t
302
		diffstat=t
303
		;;
304 305
	--whitespace=*)
		git_am_opt="$git_am_opt $1"
306 307 308 309 310
		case "$1" in
		--whitespace=fix|--whitespace=strip)
			force_rebase=t
			;;
		esac
311
		;;
312 313 314 315
	--committer-date-is-author-date|--ignore-date)
		git_am_opt="$git_am_opt $1"
		force_rebase=t
		;;
M
Michael S. Tsirkin 已提交
316
	-C*)
317
		git_am_opt="$git_am_opt $1"
M
Michael S. Tsirkin 已提交
318
		;;
319 320 321
	--root)
		rebase_root=t
		;;
322
	-f|--f|--fo|--for|--forc|force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase)
323 324
		force_rebase=t
		;;
325 326 327 328 329 330 331 332 333
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
334
test $# -gt 2 && usage
335

336
# Make sure we do not have $GIT_DIR/rebase-apply
337
if test -z "$do_merge"
338
then
339
	if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null
340
	then
341
		rmdir "$GIT_DIR"/rebase-apply
342 343
	else
		echo >&2 '
344 345
It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
346 347
rebase.  If that is not the case, please
	rm -fr '"$GIT_DIR"'/rebase-apply
348
and run me again.  I am stopping in case you still have something
349
valuable there.'
350 351 352 353 354
		exit 1
	fi
else
	if test -d "$dotest"
	then
355
		die "previous rebase directory $dotest still exists." \
356
			'Try git rebase (--continue | --abort | --skip)'
357
	fi
358 359
fi

360
# The tree must be really really clean.
361 362 363 364
if ! git update-index --ignore-submodules --refresh; then
	echo >&2 "cannot rebase: you have unstaged changes"
	exit 1
fi
365
diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)
L
Lukas Sandström 已提交
366
case "$diff" in
367 368
?*)	echo >&2 "cannot rebase: your index contains uncommitted changes"
	echo >&2 "$diff"
369 370 371
	exit 1
	;;
esac
J
Junio C Hamano 已提交
372

373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
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 已提交
389

390 391
# Make sure the branch to rebase onto is valid.
onto_name=${newbase-"$upstream_name"}
392
onto=$(git rev-parse --verify "${onto_name}^0") || exit
393

394
# If a hook exists, give it a chance to interrupt
395
run_pre_rebase_hook "$upstream_arg" "$@"
396

397 398 399 400 401
# 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=
402
case "$#" in
403
1)
404
	# Is it "rebase other $branchname" or "rebase other $commit"?
405 406
	branch_name="$1"
	switch_to="$1"
407

408 409
	if git show-ref --verify --quiet -- "refs/heads/$1" &&
	   branch=$(git rev-parse -q --verify "refs/heads/$1")
410
	then
411 412
		head_name="refs/heads/$1"
	elif branch=$(git rev-parse -q --verify "$1")
413 414 415 416 417
	then
		head_name="detached HEAD"
	else
		usage
	fi
418 419
	;;
*)
420
	# Do not need to switch branches, we are already on it.
421 422
	if branch_name=`git symbolic-ref -q HEAD`
	then
423
		head_name=$branch_name
424 425
		branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
	else
426
		head_name="detached HEAD"
427 428
		branch_name=HEAD ;# detached
	fi
429
	branch=$(git rev-parse --verify "${branch_name}^0") || exit
430
	;;
431
esac
432
orig_head=$branch
433

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

437 438
# Check if we are already based on $onto with linear history,
# but this should be done only when upstream and onto are the same.
439
mb=$(git merge-base "$onto" "$branch")
440 441
if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
	# linear history?
J
Jeff King 已提交
442
	! (git rev-list --parents "$onto".."$branch" | grep " .* ") > /dev/null
443
then
444 445 446 447 448 449 450 451 452
	if test -z "$force_rebase"
	then
		# Lazily switch to the target branch if needed...
		test -z "$switch_to" || git checkout "$switch_to"
		echo >&2 "Current branch $branch_name is up to date."
		exit 0
	else
		echo "Current branch $branch_name is up to date, rebase forced."
	fi
453 454
fi

455
# Detach HEAD and reset the tree
456
echo "First, rewinding head to replay your work on top of it..."
457
git checkout -q "$onto^0" || die "could not detach HEAD"
458
git update-ref ORIG_HEAD $branch
L
Lukas Sandström 已提交
459

460 461 462 463 464 465 466 467 468 469
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

470
# If the $onto is a proper descendant of the tip of the branch, then
L
Lukas Sandström 已提交
471
# we just fast forwarded.
472
if test "$mb" = "$branch"
L
Lukas Sandström 已提交
473
then
474
	echo >&2 "Fast-forwarded $branch_name to $onto_name."
475
	move_to_original_branch
L
Lukas Sandström 已提交
476 477 478
	exit 0
fi

479 480 481 482 483 484 485
if test -n "$rebase_root"
then
	revisions="$onto..$orig_head"
else
	revisions="$upstream..$orig_head"
fi

486 487
if test -z "$do_merge"
then
488
	git format-patch -k --stdout --full-index --ignore-if-in-upstream \
489
		$root_flag "$revisions" |
J
Junio C Hamano 已提交
490
	git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
491 492
	move_to_original_branch
	ret=$?
493 494 495 496
	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 &&
		echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head
497
	exit $ret
498 499 500 501 502 503 504
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"
505
echo "$onto_name" > "$dotest/onto_name"
506
prev_head=$orig_head
507
echo "$prev_head" > "$dotest/prev_head"
508 509
echo "$orig_head" > "$dotest/orig-head"
echo "$head_name" > "$dotest/head-name"
510 511

msgnum=0
512
for cmt in `git rev-list --reverse --no-merges "$revisions"`
513 514
do
	msgnum=$(($msgnum + 1))
515
	echo "$cmt" > "$dotest/cmt.$msgnum"
516 517
done

518 519
echo 1 >"$dotest/msgnum"
echo $msgnum >"$dotest/end"
520 521 522 523 524 525 526 527 528

end=$msgnum
msgnum=1

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

530
finish_rb_merge