git-rebase--interactive.sh 22.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#!/bin/sh
#
# Copyright (c) 2006 Johannes E. Schindelin

# SHORT DESCRIPTION
#
# This script makes it easy to fix up commits in the middle of a series,
# and rearrange commits.
#
# The original idea comes from Eric W. Biederman, in
# http://article.gmane.org/gmane.comp.version-control.git/22407

. git-sh-setup

15 16 17
# The file containing rebase commands, comments, and empty lines.
# This file is created by "git rebase -i" then edited by the user.  As
# the lines are processed, they are removed from the front of this
18
# file and written to the tail of $done.
19
todo="$state_dir"/git-rebase-todo
20 21 22 23

# The rebase command lines that have already been processed.  A line
# is moved here when it is first handled, before any associated user
# actions.
24
done="$state_dir"/done
25 26 27

# The commit message that is planned to be used for any changes that
# need to be committed following a user interaction.
28
msg="$state_dir"/message
29 30 31 32 33 34 35 36 37

# The file into which is accumulated the suggested commit message for
# squash/fixup commands.  When the first of a series of squash/fixups
# is seen, the file is created and the commit message from the
# previous commit and from the first squash/fixup commit are written
# to it.  The commit message for each subsequent squash/fixup commit
# is appended to the file as it is processed.
#
# The first line of the file is of the form
38 39
#     # This is a combination of $count commits.
# where $count is the number of commits whose messages have been
40 41 42
# written to the file so far (including the initial "pick" commit).
# Each time that a commit message is processed, this line is read and
# updated.  It is deleted just before the combined commit is made.
43
squash_msg="$state_dir"/message-squash
44

45 46 47 48 49
# If the current series of squash/fixups has not yet included a squash
# command, then this file exists and holds the commit message of the
# original "pick" commit.  (If the series ends without a "squash"
# command, then this can be used as the commit message of the combined
# commit without opening the editor.)
50
fixup_msg="$state_dir"/message-fixup
51

52 53 54
# $rewritten is the name of a directory containing files for each
# commit that is reachable by at least one merge base of $head and
# $upstream. They are not necessarily rewritten, but their children
55 56 57
# might be.  This ensures that commits on merged, but otherwise
# unrelated side branches are left alone. (Think "X" in the man page's
# example.)
58
rewritten="$state_dir"/rewritten
59

60
dropped="$state_dir"/dropped
61 62 63 64

# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
# GIT_AUTHOR_DATE that will be used for the commit that is currently
# being rebased.
65
author_script="$state_dir"/author-script
66

67 68 69 70 71 72
# When an "edit" rebase command is being processed, the SHA1 of the
# commit to be edited is recorded in this file.  When "git rebase
# --continue" is executed, if there are any staged changes then they
# will be amended to the HEAD commit, but only provided the HEAD
# commit is still the commit to be edited.  When any other rebase
# command is processed, this file is deleted.
73
amend="$state_dir"/amend
74

75 76 77 78
# For the post-rewrite hook, we make a list of rewritten commits and
# their new sha1s.  The rewritten-pending list keeps the sha1s of
# commits that have been processed, but not committed yet,
# e.g. because they are waiting for a 'squash' command.
79 80
rewritten_list="$state_dir"/rewritten-list
rewritten_pending="$state_dir"/rewritten-pending
81

82
GIT_CHERRY_PICK_HELP="$resolvemsg"
83 84
export GIT_CHERRY_PICK_HELP

85
warn () {
86
	printf '%s\n' "$*" >&2
87 88
}

89 90 91 92 93
# Output the commit message for the specified commit.
commit_message () {
	git cat-file commit "$1" | sed "1,/^$/d"
}

94
orig_reflog_action="$GIT_REFLOG_ACTION"
95 96

comment_for_reflog () {
97
	case "$orig_reflog_action" in
98 99 100
	''|rebase*)
		GIT_REFLOG_ACTION="rebase -i ($1)"
		export GIT_REFLOG_ACTION
101
		;;
102 103 104
	esac
}

105
last_count=
106
mark_action_done () {
107 108 109 110 111 112
	sed -e 1q < "$todo" >> "$done"
	sed -e 1d < "$todo" >> "$todo".new
	mv -f "$todo".new "$todo"
	new_count=$(sane_grep -c '^[^#]' < "$done")
	total=$(($new_count+$(sane_grep -c '^[^#]' < "$todo")))
	if test "$last_count" != "$new_count"
113
	then
114 115 116
		last_count=$new_count
		printf "Rebasing (%d/%d)\r" $new_count $total
		test -z "$verbose" || echo
117
	fi
118 119 120
}

make_patch () {
121 122 123 124 125 126 127 128 129 130 131
	sha1_and_parents="$(git rev-list --parents -1 "$1")"
	case "$sha1_and_parents" in
	?*' '?*' '?*)
		git diff --cc $sha1_and_parents
		;;
	?*' '?*)
		git diff-tree -p "$1^!"
		;;
	*)
		echo "Root commit"
		;;
132
	esac > "$state_dir"/patch
133 134 135 136
	test -f "$msg" ||
		commit_message "$1" > "$msg"
	test -f "$author_script" ||
		get_author_ident_from_commit "$1" > "$author_script"
137 138 139
}

die_with_patch () {
140
	echo "$1" > "$state_dir"/stopped-sha
141
	make_patch "$1"
142
	git rerere
143 144 145
	die "$2"
}

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
exit_with_patch () {
	echo "$1" > "$state_dir"/stopped-sha
	make_patch $1
	git rev-parse --verify HEAD > "$amend"
	warn "You can amend the commit now, with"
	warn
	warn "	git commit --amend"
	warn
	warn "Once you are satisfied with your changes, run"
	warn
	warn "	git rebase --continue"
	warn
	exit $2
}

161
die_abort () {
162
	rm -rf "$state_dir"
163 164 165
	die "$1"
}

166
has_action () {
167
	sane_grep '^[^#]' "$1" >/dev/null
168 169
}

N
Neil Horman 已提交
170 171 172 173 174 175 176 177
is_empty_commit() {
	tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
		die "$1: not a commit that can be picked")
	ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
		ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
	test "$tree" = "$ptree"
}

178 179 180
# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
# GIT_AUTHOR_DATE exported from the current environment.
do_with_author () {
181 182 183 184
	(
		export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
		"$@"
	)
185 186
}

187 188 189 190 191 192 193 194 195 196 197 198 199
git_sequence_editor () {
	if test -z "$GIT_SEQUENCE_EDITOR"
	then
		GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
		if [ -z "$GIT_SEQUENCE_EDITOR" ]
		then
			GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
		fi
	fi

	eval "$GIT_SEQUENCE_EDITOR" '"$@"'
}

200
pick_one () {
201
	ff=--ff
N
Neil Horman 已提交
202

203
	case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
204
	case "$force_rebase" in '') ;; ?*) ff= ;; esac
205
	output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
N
Neil Horman 已提交
206 207 208 209 210 211

	if is_empty_commit "$sha1"
	then
		empty_args="--allow-empty"
	fi

212
	test -d "$rewritten" &&
213
		pick_one_preserving_merges "$@" && return
N
Neil Horman 已提交
214
	output git cherry-pick $empty_args $ff "$@"
215 216
}

217
pick_one_preserving_merges () {
218 219 220 221 222 223 224 225 226 227
	fast_forward=t
	case "$1" in
	-n)
		fast_forward=f
		sha1=$2
		;;
	*)
		sha1=$1
		;;
	esac
228 229
	sha1=$(git rev-parse $sha1)

230
	if test -f "$state_dir"/current-commit
231
	then
232
		if test "$fast_forward" = t
233
		then
J
Junio C Hamano 已提交
234
			while read current_commit
235
			do
236
				git rev-parse HEAD > "$rewritten"/$current_commit
237 238
			done <"$state_dir"/current-commit
			rm "$state_dir"/current-commit ||
239 240
			die "Cannot write current commit's replacement sha1"
		fi
241 242
	fi

243
	echo $sha1 >> "$state_dir"/current-commit
T
Thomas Rast 已提交
244

245 246
	# rewrite parents; if none were rewritten, we can fast-forward.
	new_parents=
247 248 249 250 251
	pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
	if test "$pend" = " "
	then
		pend=" root"
	fi
252
	while [ "$pend" != "" ]
253
	do
254 255 256
		p=$(expr "$pend" : ' \([^ ]*\)')
		pend="${pend# $p}"

257
		if test -f "$rewritten"/$p
258
		then
259
			new_p=$(cat "$rewritten"/$p)
260 261 262 263 264 265 266 267 268

			# If the todo reordered commits, and our parent is marked for
			# rewriting, but hasn't been gotten to yet, assume the user meant to
			# drop it on top of the current HEAD
			if test -z "$new_p"
			then
				new_p=$(git rev-parse HEAD)
			fi

269 270 271 272 273 274
			test $p != $new_p && fast_forward=f
			case "$new_parents" in
			*$new_p*)
				;; # do nothing; that parent is already there
			*)
				new_parents="$new_parents $new_p"
275
				;;
276
			esac
277
		else
278
			if test -f "$dropped"/$p
279 280
			then
				fast_forward=f
281
				replacement="$(cat "$dropped"/$p)"
282 283
				test -z "$replacement" && replacement=root
				pend=" $replacement$pend"
284 285 286
			else
				new_parents="$new_parents $p"
			fi
287 288 289 290
		fi
	done
	case $fast_forward in
	t)
291
		output warn "Fast-forward to $sha1"
292
		output git reset --hard $sha1 ||
293
			die "Cannot fast-forward to $sha1"
294 295
		;;
	f)
296
		first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
297 298 299 300 301 302 303

		if [ "$1" != "-n" ]
		then
			# detach HEAD to current parent
			output git checkout $first_parent 2> /dev/null ||
				die "Cannot move HEAD to $first_parent"
		fi
304 305

		case "$new_parents" in
306
		' '*' '*)
307 308
			test "a$1" = a-n && die "Refusing to squash a merge: $sha1"

309
			# redo merge
310 311 312
			author_script_content=$(get_author_ident_from_commit $sha1)
			eval "$author_script_content"
			msg_content="$(commit_message $sha1)"
313 314
			# No point in merging the first parent, that's HEAD
			new_parents=${new_parents# $first_parent}
315
			if ! do_with_author output \
316
				git merge --no-ff ${strategy:+-s $strategy} -m \
317
					"$msg_content" $new_parents
318
			then
319
				printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
320
				die_with_patch $sha1 "Error redoing merge $sha1"
321
			fi
322
			echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
323 324
			;;
		*)
325
			output git cherry-pick "$@" ||
326
				die_with_patch $sha1 "Could not pick $sha1"
327
			;;
328
		esac
329
		;;
330 331 332
	esac
}

333 334 335 336 337 338 339 340 341
nth_string () {
	case "$1" in
	*1[0-9]|*[04-9]) echo "$1"th;;
	*1) echo "$1"st;;
	*2) echo "$1"nd;;
	*3) echo "$1"rd;;
	esac
}

342
update_squash_messages () {
343 344 345
	if test -f "$squash_msg"; then
		mv "$squash_msg" "$squash_msg".bak || exit
		count=$(($(sed -n \
346
			-e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
347
			-e "q" < "$squash_msg".bak)+1))
348
		{
349
			echo "# This is a combination of $count commits."
350 351
			sed -e 1d -e '2,/^./{
				/^$/d
352 353
			}' <"$squash_msg".bak
		} >"$squash_msg"
354
	else
355 356
		commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
		count=2
357 358 359 360
		{
			echo "# This is a combination of 2 commits."
			echo "# The first commit's message is:"
			echo
361 362
			cat "$fixup_msg"
		} >"$squash_msg"
363
	fi
364 365
	case $1 in
	squash)
366
		rm -f "$fixup_msg"
367
		echo
368
		echo "# This is the $(nth_string $count) commit message:"
369
		echo
370
		commit_message $2
371 372 373
		;;
	fixup)
		echo
374
		echo "# The $(nth_string $count) commit message will be skipped:"
375
		echo
376
		commit_message $2 | sed -e 's/^/#	/'
377
		;;
378
	esac >>"$squash_msg"
379 380 381
}

peek_next_command () {
382
	sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$todo"
383 384
}

385 386 387 388 389 390 391
# A squash/fixup has failed.  Prepare the long version of the squash
# commit message, then die_with_patch.  This code path requires the
# user to edit the combined commit message for all commits that have
# been squashed/fixedup so far.  So also erase the old squash
# messages, effectively causing the combined commit to be used as the
# new basis for any further squash/fixups.  Args: sha1 rest
die_failed_squash() {
392 393 394
	mv "$squash_msg" "$msg" || exit
	rm -f "$fixup_msg"
	cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
395 396 397 398 399
	warn
	warn "Could not apply $1... $2"
	die_with_patch $1 ""
}

400
flush_rewritten_pending() {
401
	test -s "$rewritten_pending" || return
402
	newsha1="$(git rev-parse HEAD^0)"
403 404
	sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
	rm -f "$rewritten_pending"
405 406 407 408
}

record_in_rewritten() {
	oldsha1="$(git rev-parse $1)"
409
	echo "$oldsha1" >> "$rewritten_pending"
410 411

	case "$(peek_next_command)" in
J
Junio C Hamano 已提交
412
	squash|s|fixup|f)
413
		;;
J
Junio C Hamano 已提交
414
	*)
415 416 417 418 419
		flush_rewritten_pending
		;;
	esac
}

420
do_next () {
421 422
	rm -f "$msg" "$author_script" "$amend" || exit
	read -r command sha1 rest < "$todo"
423
	case "$command" in
424
	'#'*|''|noop)
425 426
		mark_action_done
		;;
427
	pick|p)
428 429 430 431 432
		comment_for_reflog pick

		mark_action_done
		pick_one $sha1 ||
			die_with_patch $sha1 "Could not apply $sha1... $rest"
433
		record_in_rewritten $sha1
434
		;;
435 436 437 438 439 440
	reword|r)
		comment_for_reflog reword

		mark_action_done
		pick_one $sha1 ||
			die_with_patch $sha1 "Could not apply $sha1... $rest"
441 442 443 444 445 446 447
		git commit --amend --no-post-rewrite || {
			warn "Could not amend commit after successfully picking $sha1... $rest"
			warn "This is most likely due to an empty commit message, or the pre-commit hook"
			warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
			warn "you are able to reword the commit."
			exit_with_patch $sha1 1
		}
448
		record_in_rewritten $sha1
449
		;;
450
	edit|e)
451 452 453 454 455
		comment_for_reflog edit

		mark_action_done
		pick_one $sha1 ||
			die_with_patch $sha1 "Could not apply $sha1... $rest"
456
		warn "Stopped at $sha1... $rest"
457
		exit_with_patch $sha1 0
458
		;;
459 460 461 462 463 464 465 466 467 468
	squash|s|fixup|f)
		case "$command" in
		squash|s)
			squash_style=squash
			;;
		fixup|f)
			squash_style=fixup
			;;
		esac
		comment_for_reflog $squash_style
469

470
		test -f "$done" && has_action "$done" ||
471
			die "Cannot '$squash_style' without a previous commit"
472 473

		mark_action_done
474
		update_squash_messages $squash_style $sha1
475 476 477
		author_script_content=$(get_author_ident_from_commit HEAD)
		echo "$author_script_content" > "$author_script"
		eval "$author_script_content"
478
		output git reset --soft HEAD^
479
		pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
480
		case "$(peek_next_command)" in
481
		squash|s|fixup|f)
482 483
			# This is an intermediate commit; its message will only be
			# used in case of trouble.  So use the long version:
484
			do_with_author output git commit --no-verify -F "$squash_msg" ||
485
				die_failed_squash $sha1 "$rest"
486
			;;
487
		*)
488
			# This is the final command of this squash/fixup group
489
			if test -f "$fixup_msg"
490
			then
491
				do_with_author git commit --no-verify -F "$fixup_msg" ||
492 493
					die_failed_squash $sha1 "$rest"
			else
494
				cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
495 496 497
				rm -f "$GIT_DIR"/MERGE_MSG
				do_with_author git commit --no-verify -e ||
					die_failed_squash $sha1 "$rest"
498
			fi
499
			rm -f "$squash_msg" "$fixup_msg"
500
			;;
501
		esac
502
		record_in_rewritten $sha1
503
		;;
504
	x|"exec")
505
		read -r command rest < "$todo"
506 507 508 509
		mark_action_done
		printf 'Executing: %s\n' "$rest"
		# "exec" command doesn't take a sha1 in the todo-list.
		# => can't just use $sha1 here.
510
		git rev-parse --verify HEAD > "$state_dir"/stopped-sha
511 512
		${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
		status=$?
513 514 515
		# Run in subshell because require_clean_work_tree can die.
		dirty=f
		(require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
516 517 518
		if test "$status" -ne 0
		then
			warn "Execution failed: $rest"
519 520 521
			test "$dirty" = f ||
			warn "and made changes to the index and/or the working tree"

522 523 524 525 526
			warn "You can fix the problem, and then run"
			warn
			warn "	git rebase --continue"
			warn
			exit "$status"
527
		elif test "$dirty" = t
528
		then
529 530
			warn "Execution succeeded: $rest"
			warn "but left changes to the index and/or the working tree"
531 532 533 534 535 536 537
			warn "Commit or stash your changes, and then run"
			warn
			warn "	git rebase --continue"
			warn
			exit 1
		fi
		;;
538 539
	*)
		warn "Unknown command: $command $sha1 $rest"
540 541
		if git rev-parse --verify -q "$sha1" >/dev/null
		then
542
			die_with_patch $sha1 "Please fix this in the file $todo."
543
		else
544
			die "Please fix this in the file $todo."
545
		fi
546
		;;
547
	esac
548
	test -s "$todo" && return
549

550
	comment_for_reflog finish &&
551
	newhead=$(git rev-parse HEAD) &&
552
	case $head_name in
553
	refs/*)
554
		message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
555
		git update-ref -m "$message" $head_name $newhead $orig_head &&
556 557 558
		git symbolic-ref \
		  -m "$GIT_REFLOG_ACTION: returning to $head_name" \
		  HEAD $head_name
559 560
		;;
	esac && {
561
		test ! -f "$state_dir"/verbose ||
562
			git diff-tree --stat $orig_head..HEAD
563
	} &&
564
	{
565 566
		test -s "$rewritten_list" &&
		git notes copy --for-rewrite=rebase < "$rewritten_list" ||
567 568
		true # we don't care if this copying failed
	} &&
569
	if test -x "$GIT_DIR"/hooks/post-rewrite &&
570 571
		test -s "$rewritten_list"; then
		"$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
572 573
		true # we don't care if this hook failed
	fi &&
574
	rm -rf "$state_dir" &&
575
	git gc --auto &&
576
	warn "Successfully rebased and updated $head_name."
577 578 579 580 581 582 583 584 585 586 587

	exit
}

do_rest () {
	while :
	do
		do_next
	done
}

588 589 590
# skip picking commits whose parents are unchanged
skip_unnecessary_picks () {
	fd=3
591
	while read -r command rest
592 593
	do
		# fd=3 means we skip the command
594 595
		case "$fd,$command" in
		3,pick|3,p)
596
			# pick a commit whose parent is current $onto -> skip
597
			sha1=${rest%% *}
598
			case "$(git rev-parse --verify --quiet "$sha1"^)" in
599 600
			"$onto"*)
				onto=$sha1
601 602 603 604 605
				;;
			*)
				fd=1
				;;
			esac
606
			;;
607
		3,#*|3,)
608 609 610 611 612 613
			# copy comments
			;;
		*)
			fd=1
			;;
		esac
614
		printf '%s\n' "$command${rest:+ }$rest" >&$fd
615 616
	done <"$todo" >"$todo.new" 3>>"$done" &&
	mv -f "$todo".new "$todo" &&
617 618
	case "$(peek_next_command)" in
	squash|s|fixup|f)
619
		record_in_rewritten "$onto"
620 621
		;;
	esac ||
622 623 624
	die "Could not skip unnecessary pick commands"
}

625 626 627 628 629
# Rearrange the todo list that has both "pick sha1 msg" and
# "pick sha1 fixup!/squash! msg" appears in it so that the latter
# comes immediately after the former, and change "pick" to
# "fixup"/"squash".
rearrange_squash () {
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	# extract fixup!/squash! lines and resolve any referenced sha1's
	while read -r pick sha1 message
	do
		case "$message" in
		"squash! "*|"fixup! "*)
			action="${message%%!*}"
			rest="${message#*! }"
			echo "$sha1 $action $rest"
			# if it's a single word, try to resolve to a full sha1 and
			# emit a second copy. This allows us to match on both message
			# and on sha1 prefix
			if test "${rest#* }" = "$rest"; then
				fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
				if test -n "$fullsha"; then
					# prefix the action to uniquely identify this line as
					# intended for full sha1 match
					echo "$sha1 +$action $fullsha"
				fi
			fi
		esac
	done >"$1.sq" <"$1"
651 652 653
	test -s "$1.sq" || return

	used=
654
	while read -r pick sha1 message
655 656 657 658
	do
		case " $used" in
		*" $sha1 "*) continue ;;
		esac
659
		printf '%s\n' "$pick $sha1 $message"
660
		used="$used$sha1 "
661
		while read -r squash action msg_content
662
		do
663 664 665
			case " $used" in
			*" $squash "*) continue ;;
			esac
666 667 668 669 670
			emit=0
			case "$action" in
			+*)
				action="${action#+}"
				# full sha1 prefix test
671
				case "$msg_content" in "$sha1"*) emit=1;; esac ;;
672 673
			*)
				# message prefix test
674
				case "$message" in "$msg_content"*) emit=1;; esac ;;
675 676
			esac
			if test $emit = 1; then
677
				printf '%s\n' "$action $squash $action! $msg_content"
678
				used="$used$squash "
679
			fi
680 681 682 683 684 685
		done <"$1.sq"
	done >"$1.rearranged" <"$1"
	cat "$1.rearranged" >"$1"
	rm -f "$1.sq" "$1.rearranged"
}

686 687 688
case "$action" in
continue)
	# do we have anything to commit?
689
	if git diff-index --cached --quiet HEAD --
690 691 692
	then
		: Nothing to commit -- skip this
	else
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
		if ! test -f "$author_script"
		then
			die "You have staged changes in your working tree. If these changes are meant to be
squashed into the previous commit, run:

  git commit --amend

If they are meant to go into a new commit, run:

  git commit

In both case, once you're done, continue with:

  git rebase --continue
"
		fi
709
		. "$author_script" ||
710
			die "Error trying to find the author identity to amend commit"
711 712
		current_head=
		if test -f "$amend"
713
		then
714 715 716
			current_head=$(git rev-parse --verify HEAD)
			test "$current_head" = $(cat "$amend") ||
			die "\
717 718
You have uncommitted changes in your working tree. Please, commit them
first and then run 'git rebase --continue' again."
719 720
			git reset --soft HEAD^ ||
			die "Cannot rewind the HEAD"
721
		fi
722 723 724 725 726
		do_with_author git commit --no-verify -F "$msg" -e || {
			test -n "$current_head" && git reset --soft $current_head
			die "Could not commit staged changes."
		}
	fi
727

728
	record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
729

730 731 732 733 734 735
	require_clean_work_tree "rebase"
	do_rest
	;;
skip)
	git rerere clear

736
	do_rest
737 738
	;;
esac
739

740 741
git var GIT_COMMITTER_IDENT >/dev/null ||
	die "You need to set your committer info first"
742

743 744
comment_for_reflog start

745
if test ! -z "$switch_to"
746
then
747 748
	output git checkout "$switch_to" -- ||
		die "Could not checkout $switch_to"
749 750
fi

751 752
orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
mkdir "$state_dir" || die "Could not create temporary $state_dir"
753

754
: > "$state_dir"/interactive || die "Could not mark as interactive"
755
write_basic_state
756
if test t = "$preserve_merges"
757
then
758
	if test -z "$rebase_root"
759
	then
760
		mkdir "$rewritten" &&
761
		for c in $(git merge-base --all $orig_head $upstream)
762
		do
763
			echo $onto > "$rewritten"/$c ||
764
				die "Could not init rewritten commits"
765
		done
766
	else
767 768
		mkdir "$rewritten" &&
		echo $onto > "$rewritten"/root ||
769 770 771 772 773
			die "Could not init rewritten commits"
	fi
	# No cherry-pick because our first pass is to determine
	# parents to rewrite and skipping dropped commits would
	# prematurely end our probe
774
	merges_option=
775
else
776
	merges_option="--no-merges --cherry-pick"
777 778
fi

779
shorthead=$(git rev-parse --short $orig_head)
780 781 782
shortonto=$(git rev-parse --short $onto)
if test -z "$rebase_root"
	# this is now equivalent to ! -z "$upstream"
783
then
784
	shortupstream=$(git rev-parse --short $upstream)
785
	revisions=$upstream...$orig_head
786
	shortrevisions=$shortupstream..$shorthead
787
else
788
	revisions=$onto...$orig_head
789
	shortrevisions=$shorthead
790
fi
791
git rev-list $merges_option --pretty=oneline --abbrev-commit \
792
	--abbrev=7 --reverse --left-right --topo-order \
793
	$revisions | \
794 795 796
	sed -n "s/^>//p" |
while read -r shortsha1 rest
do
N
Neil Horman 已提交
797 798 799 800 801 802 803 804

	if test -z "$keep_empty" && is_empty_commit $shortsha1
	then
		comment_out="# "
	else
		comment_out=
	fi

805
	if test t != "$preserve_merges"
806
	then
N
Neil Horman 已提交
807
		printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
808 809
	else
		sha1=$(git rev-parse $shortsha1)
810
		if test -z "$rebase_root"
811
		then
812 813
			preserve=t
			for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
814
			do
815
				if test -f "$rewritten"/$p
816
				then
817
					preserve=f
818 819
				fi
			done
820 821 822 823 824
		else
			preserve=f
		fi
		if test f = "$preserve"
		then
825
			touch "$rewritten"/$sha1
N
Neil Horman 已提交
826
			printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
827
		fi
828 829
	fi
done
830

831
# Watch for commits that been dropped by --cherry-pick
832
if test t = "$preserve_merges"
833
then
834
	mkdir "$dropped"
835
	# Save all non-cherry-picked changes
836
	git rev-list $revisions --left-right --cherry-pick | \
837
		sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
838 839
	# Now all commits and note which ones are missing in
	# not-cherry-picks and hence being dropped
840
	git rev-list $revisions |
841 842
	while read rev
	do
843
		if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
844 845 846 847 848
		then
			# Use -f2 because if rev-list is telling us this commit is
			# not worthwhile, we don't want to track its multiple heads,
			# just the history of its first-parent for others that will
			# be rebasing on top of it
849
			git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
850
			short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
851 852
			sane_grep -v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
			rm "$rewritten"/$rev
853 854 855 856
		fi
	done
fi

857 858 859
test -s "$todo" || echo noop >> "$todo"
test -n "$autosquash" && rearrange_squash "$todo"
cat >> "$todo" << EOF
860

861
# Rebase $shortrevisions onto $shortonto
862 863
#
# Commands:
864
#  p, pick = use commit
865
#  r, reword = use commit, but edit the commit message
866 867
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
868
#  f, fixup = like "squash", but discard this commit's log message
869
#  x, exec = run command (the rest of the line) using shell
870
#
871 872
# These lines can be re-ordered; they are executed from top to bottom.
#
873
# If you remove a line here THAT COMMIT WILL BE LOST.
874
# However, if you remove everything, the rebase will be aborted.
875
#
876 877
EOF

N
Neil Horman 已提交
878 879 880 881 882 883
if test -z "$keep_empty"
then
	echo "# Note that empty commits are commented out" >>"$todo"
fi


884
has_action "$todo" ||
885
	die_abort "Nothing to do"
886

887
cp "$todo" "$todo".backup
888
git_sequence_editor "$todo" ||
889
	die_abort "Could not execute editor"
890

891
has_action "$todo" ||
892
	die_abort "Nothing to do"
893

894
test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
895

896
output git checkout $onto || die_abort "could not detach HEAD"
897
git update-ref ORIG_HEAD $orig_head
898
do_rest