git-rebase--interactive.sh 7.17 KB
Newer Older
1 2 3
# This shell script fragment is sourced by git-rebase to implement
# its interactive mode.  "git rebase --interactive" makes it easy
# to fix up commits in the middle of a series and rearrange commits.
4 5 6 7
#
# Copyright (c) 2006 Johannes E. Schindelin
#
# The original idea comes from Eric W. Biederman, in
8
# https://public-inbox.org/git/m1odwkyuf5.fsf_-_@ebiederm.dsl.xmission.com/
9
#
10 11 12
# 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
13
# file and written to the tail of $done.
14
todo="$state_dir"/git-rebase-todo
15

16
GIT_CHERRY_PICK_HELP="$resolvemsg"
17 18
export GIT_CHERRY_PICK_HELP

19 20 21 22 23 24 25 26 27 28 29
comment_char=$(git config --get core.commentchar 2>/dev/null)
case "$comment_char" in
'' | auto)
	comment_char="#"
	;;
?)
	;;
*)
	comment_char=$(echo "$comment_char" | cut -c1)
	;;
esac
30

31
orig_reflog_action="$GIT_REFLOG_ACTION"
32 33

comment_for_reflog () {
34
	case "$orig_reflog_action" in
35 36 37
	''|rebase*)
		GIT_REFLOG_ACTION="rebase -i ($1)"
		export GIT_REFLOG_ACTION
38
		;;
39 40 41
	esac
}

42
append_todo_help () {
43
	gettext "
44
Commands:
45 46 47 48 49
p, pick <commit> = use commit
r, reword <commit> = use commit, but edit the commit message
e, edit <commit> = use commit, but stop for amending
s, squash <commit> = use commit, but meld into previous commit
f, fixup <commit> = like \"squash\", but discard this commit's log message
50
x, exec <command> = run command (the rest of the line) using shell
51
d, drop <commit> = remove commit
52 53
l, label <label> = label current HEAD with a name
t, reset <label> = reset HEAD to a label
54 55 56 57
m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
.       create a merge commit using the original merge commit's
.       message (or the oneline, if no original merge commit was
.       specified). Use -c <commit> to reword the commit message.
58 59

These lines can be re-ordered; they are executed from top to bottom.
60
" | git stripspace --comment-lines >>"$todo"
61

62 63
	if test $(get_missing_commit_check_level) = error
	then
64
		gettext "
65
Do not remove any line. Use 'drop' explicitly to remove a commit.
66
" | git stripspace --comment-lines >>"$todo"
67
	else
68
		gettext "
69
If you remove a line here THAT COMMIT WILL BE LOST.
70
" | git stripspace --comment-lines >>"$todo"
71
	fi
72 73
}

74
die_abort () {
75
	apply_autostash
76
	rm -rf "$state_dir"
77 78 79
	die "$1"
}

80
has_action () {
81
	test -n "$(git stripspace --strip-comments <"$1")"
82 83
}

84 85 86 87 88 89 90 91 92 93 94 95 96
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" '"$@"'
}

97
expand_todo_ids() {
98
	git rebase--helper --expand-ids
99 100 101
}

collapse_todo_ids() {
102
	git rebase--helper --shorten-ids
103 104
}

105 106 107
# Switch to the branch in $into and notify it in the reflog
checkout_onto () {
	GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
108
	output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
109 110 111 112 113 114 115 116 117 118
	git update-ref ORIG_HEAD $orig_head
}

get_missing_commit_check_level () {
	check_level=$(git config --get rebase.missingCommitsCheck)
	check_level=${check_level:-ignore}
	# Don't be case sensitive
	printf '%s' "$check_level" | tr 'A-Z' 'a-z'
}

119 120 121 122 123 124 125 126 127 128 129 130 131 132
# Initiate an action. If the cannot be any
# further action it  may exec a command
# or exit and not return.
#
# TODO: Consider a cleaner return model so it
# never exits and always return 0 if process
# is complete.
#
# Parameter 1 is the action to initiate.
#
# Returns 0 if the action was able to complete
# and if 1 if further processing is required.
initiate_action () {
	case "$1" in
133
	continue)
134 135
		exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
		     --continue
136 137 138
		;;
	skip)
		git rerere clear
139 140
		exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
		     --continue
141 142 143 144 145 146 147
		;;
	edit-todo)
		git stripspace --strip-comments <"$todo" >"$todo".new
		mv -f "$todo".new "$todo"
		collapse_todo_ids
		append_todo_help
		gettext "
148 149 150 151
You are editing the todo file of an ongoing interactive rebase.
To continue rebase after editing, run:
    git rebase --continue

152
" | git stripspace --comment-lines >>"$todo"
153

154 155 156
		git_sequence_editor "$todo" ||
			die "$(gettext "Could not execute editor")"
		expand_todo_ids
157

158 159 160 161 162
		exit
		;;
	show-current-patch)
		exec git show REBASE_HEAD --
		;;
163 164 165
	*)
		return 1 # continue
		;;
166
	esac
167
}
168

169
setup_reflog_action () {
170
	comment_for_reflog start
171

172
	if test ! -z "$switch_to"
173
	then
174 175 176 177 178
		GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
		output git checkout "$switch_to" -- ||
			die "$(eval_gettext "Could not checkout \$switch_to")"

		comment_for_reflog start
179
	fi
180
}
181

182
init_basic_state () {
183 184 185
	orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
	mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
	rm -f "$(git rev-parse --git-path REBASE_HEAD)"
186

187 188
	: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
	write_basic_state
189 190 191 192 193 194 195 196 197 198 199 200 201 202
}

init_revisions_and_shortrevisions () {
	shorthead=$(git rev-parse --short $orig_head)
	shortonto=$(git rev-parse --short $onto)
	if test -z "$rebase_root"
		# this is now equivalent to ! -z "$upstream"
	then
		shortupstream=$(git rev-parse --short $upstream)
		revisions=$upstream...$orig_head
		shortrevisions=$shortupstream..$shorthead
	else
		revisions=$onto...$orig_head
		shortrevisions=$shorthead
203 204
		test -z "$squash_onto" ||
		echo "$squash_onto" >"$state_dir"/squash-onto
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	fi
}

complete_action() {
	test -s "$todo" || echo noop >> "$todo"
	test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
	test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"

	todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
	todocount=${todocount##* }

cat >>"$todo" <<EOF

$comment_char $(eval_ngettext \
	"Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
	"Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
	"$todocount")
EOF
	append_todo_help
	gettext "
	However, if you remove everything, the rebase will be aborted.

	" | git stripspace --comment-lines >>"$todo"

	if test -z "$keep_empty"
	then
		printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
	fi


	has_action "$todo" ||
		return 2

	cp "$todo" "$todo".backup
	collapse_todo_ids
	git_sequence_editor "$todo" ||
		die_abort "$(gettext "Could not execute editor")"

	has_action "$todo" ||
		return 2

	git rebase--helper --check-todo-list || {
		ret=$?
		checkout_onto
		exit $ret
	}

	expand_todo_ids

254
	test -n "$force_rebase" ||
255 256 257 258
	onto="$(git rebase--helper --skip-unnecessary-picks)" ||
	die "Could not skip unnecessary pick commands"

	checkout_onto
259 260 261
	require_clean_work_tree "rebase"
	exec git rebase--helper ${force_rebase:+--no-ff} $allow_empty_message \
	     --continue
262 263 264 265 266 267 268 269 270 271 272 273 274 275
}

git_rebase__interactive () {
	initiate_action "$action"
	ret=$?
	if test $ret = 0; then
		return 0
	fi

	setup_reflog_action
	init_basic_state

	init_revisions_and_shortrevisions

276
	git rebase--helper --make-script ${keep_empty:+--keep-empty} \
277
		${rebase_merges:+--rebase-merges} \
278
		${rebase_cousins:+--rebase-cousins} \
279 280
		$revisions ${restrict_revision+^$restrict_revision} >"$todo" ||
	die "$(gettext "Could not generate todo list")"
281 282 283

	complete_action
}