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

6
USAGE='[--interactive | -i] [-v] [--onto <newbase>] <upstream> [<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=
M
Michael S. Tsirkin 已提交
49
git_am_opt=
50 51 52 53 54

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

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

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

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

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

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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
}

135
finish_rb_merge () {
136
	move_to_original_branch
137 138 139 140
	rm -r "$dotest"
	echo "All done."
}

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

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

	test -n "$interactive_rebase" || test -f "$dotest"/interactive
162 163
}

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

175 176 177
test -f "$GIT_DIR"/rebase-apply/applying &&
	die 'It looks like git-am is in progress. Cannot rebase.'

178 179
is_interactive "$@" && exec git-rebase--interactive "$@"

180 181 182 183 184 185 186 187
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

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

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

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

257
		git rerere clear
258 259
		if test -d "$dotest"
		then
260
			move_to_original_branch
261
		else
262
			dotest="$GIT_DIR"/rebase-apply
263
			move_to_original_branch
264
		fi
265
		git reset --hard $(cat "$dotest/orig-head")
266
		rm -r "$dotest"
267 268
		exit
		;;
269 270 271 272 273
	--onto)
		test 2 -le "$#" || usage
		newbase="$2"
		shift
		;;
274 275 276 277 278 279 280 281
	-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 已提交
282
			strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
283 284 285 286 287 288 289 290
		1,*)
			usage ;;
		*)
			strategy="$2"
			shift ;;
		esac
		do_merge=t
		;;
291 292 293
	-v|--verbose)
		verbose=t
		;;
294 295 296
	--whitespace=*)
		git_am_opt="$git_am_opt $1"
		;;
M
Michael S. Tsirkin 已提交
297
	-C*)
298
		git_am_opt="$git_am_opt $1"
M
Michael S. Tsirkin 已提交
299
		;;
300 301 302 303 304 305 306 307 308
	-*)
		usage
		;;
	*)
		break
		;;
	esac
	shift
done
309

310
# Make sure we do not have $GIT_DIR/rebase-apply
311
if test -z "$do_merge"
312
then
313
	if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null
314
	then
315
		rmdir "$GIT_DIR"/rebase-apply
316 317
	else
		echo >&2 '
318 319
It seems that I cannot create a rebase-apply directory, and
I wonder if you are in the middle of patch application or another
320 321
rebase.  If that is not the case, please
	rm -fr '"$GIT_DIR"'/rebase-apply
322
and run me again.  I am stopping in case you still have something
323
valuable there.'
324 325 326 327 328
		exit 1
	fi
else
	if test -d "$dotest"
	then
329
		die "previous rebase directory $dotest still exists." \
330
			'Try git rebase (--continue | --abort | --skip)'
331
	fi
332 333
fi

334
# The tree must be really really clean.
335 336 337 338
if ! git update-index --ignore-submodules --refresh; then
	echo >&2 "cannot rebase: you have unstaged changes"
	exit 1
fi
339
diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --)
L
Lukas Sandström 已提交
340
case "$diff" in
341 342
?*)	echo >&2 "cannot rebase: your index contains uncommitted changes"
	echo >&2 "$diff"
343 344 345
	exit 1
	;;
esac
J
Junio C Hamano 已提交
346

347 348 349
# The upstream head must be given.  Make sure it is valid.
upstream_name="$1"
upstream=`git rev-parse --verify "${upstream_name}^0"` ||
J
Jason Riedy 已提交
350
    die "invalid upstream $upstream_name"
L
Lukas Sandström 已提交
351

352 353
# Make sure the branch to rebase onto is valid.
onto_name=${newbase-"$upstream_name"}
354
onto=$(git rev-parse --verify "${onto_name}^0") || exit
355

356
# If a hook exists, give it a chance to interrupt
357
run_pre_rebase_hook ${1+"$@"}
358

359 360 361 362 363
# 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=
364
case "$#" in
365
2)
366
	# Is it "rebase other $branchname" or "rebase other $commit"?
367
	branch_name="$2"
368 369 370
	switch_to="$2"

	if git show-ref --verify --quiet -- "refs/heads/$2" &&
M
Miklos Vajna 已提交
371
	   branch=$(git rev-parse -q --verify "refs/heads/$2")
372 373
	then
		head_name="refs/heads/$2"
M
Miklos Vajna 已提交
374
	elif branch=$(git rev-parse -q --verify "$2")
375 376 377 378 379
	then
		head_name="detached HEAD"
	else
		usage
	fi
380 381
	;;
*)
382
	# Do not need to switch branches, we are already on it.
383 384
	if branch_name=`git symbolic-ref -q HEAD`
	then
385
		head_name=$branch_name
386 387
		branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
	else
388
		head_name="detached HEAD"
389 390
		branch_name=HEAD ;# detached
	fi
391
	branch=$(git rev-parse --verify "${branch_name}^0") || exit
392
	;;
393
esac
394
orig_head=$branch
395

396 397
# Now we are rebasing commits $upstream..$branch on top of $onto

398 399
# Check if we are already based on $onto with linear history,
# but this should be done only when upstream and onto are the same.
400
mb=$(git merge-base "$onto" "$branch")
401 402
if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
	# linear history?
J
Jeff King 已提交
403
	! (git rev-list --parents "$onto".."$branch" | grep " .* ") > /dev/null
404
then
405 406
	# Lazily switch to the target branch if needed...
	test -z "$switch_to" || git checkout "$switch_to"
407 408
	echo >&2 "Current branch $branch_name is up to date."
	exit 0
409 410
fi

411 412 413
if test -n "$verbose"
then
	echo "Changes from $mb to $onto:"
414
	# We want color (if set), but no pager
415
	GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
416 417
fi

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

423
# If the $onto is a proper descendant of the tip of the branch, then
L
Lukas Sandström 已提交
424
# we just fast forwarded.
425
if test "$mb" = "$branch"
L
Lukas Sandström 已提交
426
then
427
	echo >&2 "Fast-forwarded $branch_name to $onto_name."
428
	move_to_original_branch
L
Lukas Sandström 已提交
429 430 431
	exit 0
fi

432 433
if test -z "$do_merge"
then
434 435
	git format-patch -k --stdout --full-index --ignore-if-in-upstream \
		"$upstream..$orig_head" |
J
Junio C Hamano 已提交
436
	git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" &&
437 438
	move_to_original_branch
	ret=$?
439 440 441 442
	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
443
	exit $ret
444 445 446 447 448 449 450
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"
451
echo "$onto_name" > "$dotest/onto_name"
452
prev_head=$orig_head
453
echo "$prev_head" > "$dotest/prev_head"
454 455
echo "$orig_head" > "$dotest/orig-head"
echo "$head_name" > "$dotest/head-name"
456 457

msgnum=0
458
for cmt in `git rev-list --reverse --no-merges "$upstream..$orig_head"`
459 460
do
	msgnum=$(($msgnum + 1))
461
	echo "$cmt" > "$dotest/cmt.$msgnum"
462 463
done

464 465
echo 1 >"$dotest/msgnum"
echo $msgnum >"$dotest/end"
466 467 468 469 470 471 472 473 474

end=$msgnum
msgnum=1

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

476
finish_rb_merge