blob: ded459563816ebe36f4e467f596f0c84fa30ff57 [file] [log] [blame]
Jonathan Nieder11d62142013-11-25 13:03:52 -08001# This shell script fragment is sourced by git-rebase to implement
2# its interactive mode. "git rebase --interactive" makes it easy
3# to fix up commits in the middle of a series and rearrange commits.
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01004#
5# Copyright (c) 2006 Johannes E. Schindelin
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01006#
7# The original idea comes from Eric W. Biederman, in
8# http://article.gmane.org/gmane.comp.version-control.git/22407
Martin von Zweigbergk572a7c52012-06-26 07:51:54 -07009#
Michael Haggerty80883bb2010-01-14 06:54:45 +010010# The file containing rebase commands, comments, and empty lines.
11# This file is created by "git rebase -i" then edited by the user. As
12# the lines are processed, they are removed from the front of this
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050013# file and written to the tail of $done.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050014todo="$state_dir"/git-rebase-todo
Michael Haggerty80883bb2010-01-14 06:54:45 +010015
16# The rebase command lines that have already been processed. A line
17# is moved here when it is first handled, before any associated user
18# actions.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050019done="$state_dir"/done
Michael Haggerty80883bb2010-01-14 06:54:45 +010020
21# The commit message that is planned to be used for any changes that
22# need to be committed following a user interaction.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050023msg="$state_dir"/message
Michael Haggerty80883bb2010-01-14 06:54:45 +010024
25# The file into which is accumulated the suggested commit message for
26# squash/fixup commands. When the first of a series of squash/fixups
27# is seen, the file is created and the commit message from the
28# previous commit and from the first squash/fixup commit are written
29# to it. The commit message for each subsequent squash/fixup commit
30# is appended to the file as it is processed.
31#
32# The first line of the file is of the form
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050033# # This is a combination of $count commits.
34# where $count is the number of commits whose messages have been
Michael Haggerty80883bb2010-01-14 06:54:45 +010035# written to the file so far (including the initial "pick" commit).
36# Each time that a commit message is processed, this line is read and
37# updated. It is deleted just before the combined commit is made.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050038squash_msg="$state_dir"/message-squash
Michael Haggerty80883bb2010-01-14 06:54:45 +010039
Michael Haggertya25eb132010-01-14 06:54:55 +010040# If the current series of squash/fixups has not yet included a squash
41# command, then this file exists and holds the commit message of the
42# original "pick" commit. (If the series ends without a "squash"
43# command, then this can be used as the commit message of the combined
44# commit without opening the editor.)
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050045fixup_msg="$state_dir"/message-fixup
Michael Haggertya25eb132010-01-14 06:54:55 +010046
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050047# $rewritten is the name of a directory containing files for each
48# commit that is reachable by at least one merge base of $head and
49# $upstream. They are not necessarily rewritten, but their children
Michael Haggerty80883bb2010-01-14 06:54:45 +010050# might be. This ensures that commits on merged, but otherwise
51# unrelated side branches are left alone. (Think "X" in the man page's
52# example.)
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050053rewritten="$state_dir"/rewritten
Michael Haggerty80883bb2010-01-14 06:54:45 +010054
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050055dropped="$state_dir"/dropped
Michael Haggerty0aac0de2010-01-14 06:54:46 +010056
Zoltan Klingerb71dc3e2013-04-25 19:28:54 +100057end="$state_dir"/end
58msgnum="$state_dir"/msgnum
59
Michael Haggerty0aac0de2010-01-14 06:54:46 +010060# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
61# GIT_AUTHOR_DATE that will be used for the commit that is currently
62# being rebased.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050063author_script="$state_dir"/author-script
Michael Haggerty0aac0de2010-01-14 06:54:46 +010064
Michael Haggertya4049ae2010-01-14 06:54:47 +010065# When an "edit" rebase command is being processed, the SHA1 of the
66# commit to be edited is recorded in this file. When "git rebase
67# --continue" is executed, if there are any staged changes then they
68# will be amended to the HEAD commit, but only provided the HEAD
69# commit is still the commit to be edited. When any other rebase
70# command is processed, this file is deleted.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050071amend="$state_dir"/amend
Michael Haggertya4049ae2010-01-14 06:54:47 +010072
Thomas Rastb079fee2010-03-12 18:04:30 +010073# For the post-rewrite hook, we make a list of rewritten commits and
74# their new sha1s. The rewritten-pending list keeps the sha1s of
75# commits that have been processed, but not committed yet,
76# e.g. because they are waiting for a 'squash' command.
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -050077rewritten_list="$state_dir"/rewritten-list
78rewritten_pending="$state_dir"/rewritten-pending
Thomas Rastb079fee2010-03-12 18:04:30 +010079
Junio C Hamano39743cf2015-10-28 15:54:48 +010080# Work around Git for Windows' Bash whose "read" does not strip CRLF
81# and leaves CR at the end instead.
82cr=$(printf "\015")
83
Fabian Ruch040fd392015-12-11 21:30:36 +010084strategy_args=${strategy:+--strategy=$strategy}
Jeff King8e98b352016-05-10 17:07:10 -040085test -n "$strategy_opts" &&
Fabian Ruch040fd392015-12-11 21:30:36 +010086eval '
87 for strategy_opt in '"$strategy_opts"'
88 do
89 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
90 done
91'
Arnaud Fontainedb2b3b82013-07-02 17:05:48 +090092
Martin von Zweigbergk89c7ae92011-02-06 13:43:49 -050093GIT_CHERRY_PICK_HELP="$resolvemsg"
Wincent Colaiuta804c7172007-11-28 00:06:36 -080094export GIT_CHERRY_PICK_HELP
95
John Keeping180bad32013-02-11 23:08:04 +000096comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
97: ${comment_char:=#}
98
Johannes Schindelin1b1dce42007-06-25 01:11:14 +010099warn () {
Brandon Casey938791c2010-07-22 14:15:11 -0500100 printf '%s\n' "$*" >&2
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100101}
102
Michael Haggertyee0a4af2010-01-14 06:54:51 +0100103# Output the commit message for the specified commit.
104commit_message () {
105 git cat-file commit "$1" | sed "1,/^$/d"
106}
107
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500108orig_reflog_action="$GIT_REFLOG_ACTION"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100109
110comment_for_reflog () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500111 case "$orig_reflog_action" in
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100112 ''|rebase*)
113 GIT_REFLOG_ACTION="rebase -i ($1)"
114 export GIT_REFLOG_ACTION
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100115 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100116 esac
117}
118
Junio C Hamano4e673872008-01-14 20:01:21 -0800119last_count=
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100120mark_action_done () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500121 sed -e 1q < "$todo" >> "$done"
122 sed -e 1d < "$todo" >> "$todo".new
123 mv -f "$todo".new "$todo"
John Keeping180bad32013-02-11 23:08:04 +0000124 new_count=$(git stripspace --strip-comments <"$done" | wc -l)
Zoltan Klingerb71dc3e2013-04-25 19:28:54 +1000125 echo $new_count >"$msgnum"
John Keeping180bad32013-02-11 23:08:04 +0000126 total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
Zoltan Klingerb71dc3e2013-04-25 19:28:54 +1000127 echo $total >"$end"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500128 if test "$last_count" != "$new_count"
Junio C Hamano4e673872008-01-14 20:01:21 -0800129 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500130 last_count=$new_count
Vasco Almeida9588c522016-06-17 20:21:03 +0000131 eval_gettext "Rebasing (\$new_count/\$total)"; printf "\r"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500132 test -z "$verbose" || echo
Junio C Hamano4e673872008-01-14 20:01:21 -0800133 fi
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100134}
135
Phil Hord8cbc57c2015-04-28 18:55:20 -0400136# Put the last action marked done at the beginning of the todo list
137# again. If there has not been an action marked done yet, leave the list of
138# items on the todo list unchanged.
139reschedule_last_action () {
140 tail -n 1 "$done" | cat - "$todo" >"$todo".new
141 sed -e \$d <"$done" >"$done".new
142 mv -f "$todo".new "$todo"
143 mv -f "$done".new "$done"
144}
145
Andrew Wongfcc5ef12012-09-17 21:28:08 -0400146append_todo_help () {
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000147 gettext "
John Keeping180bad32013-02-11 23:08:04 +0000148Commands:
149 p, pick = use commit
150 r, reword = use commit, but edit the commit message
151 e, edit = use commit, but stop for amending
152 s, squash = use commit, but meld into previous commit
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000153 f, fixup = like \"squash\", but discard this commit's log message
John Keeping180bad32013-02-11 23:08:04 +0000154 x, exec = run command (the rest of the line) using shell
Galan Rémic9266d52015-06-29 22:20:30 +0200155 d, drop = remove commit
John Keeping180bad32013-02-11 23:08:04 +0000156
157These lines can be re-ordered; they are executed from top to bottom.
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000158" | git stripspace --comment-lines >>"$todo"
John Keeping180bad32013-02-11 23:08:04 +0000159
Galan Rémi37079952015-06-29 22:20:31 +0200160 if test $(get_missing_commit_check_level) = error
161 then
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000162 gettext "
Galan Rémi37079952015-06-29 22:20:31 +0200163Do not remove any line. Use 'drop' explicitly to remove a commit.
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000164" | git stripspace --comment-lines >>"$todo"
Galan Rémi37079952015-06-29 22:20:31 +0200165 else
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000166 gettext "
John Keeping180bad32013-02-11 23:08:04 +0000167If you remove a line here THAT COMMIT WILL BE LOST.
Vasco Almeidab8fc9e42016-06-17 20:21:04 +0000168" | git stripspace --comment-lines >>"$todo"
Galan Rémi37079952015-06-29 22:20:31 +0200169 fi
Andrew Wongfcc5ef12012-09-17 21:28:08 -0400170}
171
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100172make_patch () {
Johannes Schindelin4fb1a192008-12-22 22:16:51 +0100173 sha1_and_parents="$(git rev-list --parents -1 "$1")"
174 case "$sha1_and_parents" in
175 ?*' '?*' '?*)
176 git diff --cc $sha1_and_parents
177 ;;
178 ?*' '?*)
179 git diff-tree -p "$1^!"
180 ;;
181 *)
182 echo "Root commit"
183 ;;
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500184 esac > "$state_dir"/patch
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500185 test -f "$msg" ||
186 commit_message "$1" > "$msg"
187 test -f "$author_script" ||
188 get_author_ident_from_commit "$1" > "$author_script"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100189}
190
191die_with_patch () {
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500192 echo "$1" > "$state_dir"/stopped-sha
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100193 make_patch "$1"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100194 die "$2"
195}
196
Andrew Wong0becb3e2011-11-30 10:52:51 -0500197exit_with_patch () {
198 echo "$1" > "$state_dir"/stopped-sha
199 make_patch $1
200 git rev-parse --verify HEAD > "$amend"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000201 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
Vasco Almeida9588c522016-06-17 20:21:03 +0000202 warn "$(eval_gettext "\
203You can amend the commit now, with
204
205 git commit --amend \$gpg_sign_opt_quoted
206
207Once you are satisfied with your changes, run
208
209 git rebase --continue")"
Andrew Wong0becb3e2011-11-30 10:52:51 -0500210 warn
211 exit $2
212}
213
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100214die_abort () {
Patrick Steinhardt33ba9c62016-06-29 08:21:27 +0200215 apply_autostash
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500216 rm -rf "$state_dir"
Johannes Schindelinc54b7812007-06-25 18:56:55 +0100217 die "$1"
218}
219
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100220has_action () {
John Keeping180bad32013-02-11 23:08:04 +0000221 test -n "$(git stripspace --strip-comments <"$1")"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100222}
223
Neil Horman90e18182012-04-20 10:36:17 -0400224is_empty_commit() {
Vasco Almeida9588c522016-06-17 20:21:03 +0000225 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null) || {
226 sha1=$1
227 die "$(eval_gettext "\$sha1: not a commit that can be picked")"
228 }
229 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null) ||
230 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
Neil Horman90e18182012-04-20 10:36:17 -0400231 test "$tree" = "$ptree"
232}
233
Phil Hord98697782013-01-12 15:46:01 -0500234is_merge_commit()
235{
236 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
237}
238
Michael Haggerty7756ecf2010-01-14 06:54:53 +0100239# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
240# GIT_AUTHOR_DATE exported from the current environment.
241do_with_author () {
Junio C Hamano76c9c0d2010-01-22 13:09:24 -0800242 (
243 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
244 "$@"
245 )
Michael Haggerty7756ecf2010-01-14 06:54:53 +0100246}
247
Peter Oberndorfer821881d2011-10-17 22:26:23 +0200248git_sequence_editor () {
249 if test -z "$GIT_SEQUENCE_EDITOR"
250 then
251 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
252 if [ -z "$GIT_SEQUENCE_EDITOR" ]
253 then
254 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
255 fi
256 fi
257
258 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
259}
260
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100261pick_one () {
Christian Couder8e75abf2010-03-06 21:34:46 +0100262 ff=--ff
Neil Horman90e18182012-04-20 10:36:17 -0400263
Christian Couder8e75abf2010-03-06 21:34:46 +0100264 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500265 case "$force_rebase" in '') ;; ?*) ff= ;; esac
Vasco Almeida9588c522016-06-17 20:21:03 +0000266 output git rev-parse --verify $sha1 || die "$(eval_gettext "Invalid commit name: \$sha1")"
Neil Horman90e18182012-04-20 10:36:17 -0400267
268 if is_empty_commit "$sha1"
269 then
270 empty_args="--allow-empty"
271 fi
272
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500273 test -d "$rewritten" &&
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100274 pick_one_preserving_merges "$@" && return
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000275 output eval git cherry-pick \
276 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
277 "$strategy_args" $empty_args $ff "$@"
Phil Hord8cbc57c2015-04-28 18:55:20 -0400278
279 # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule
280 # previous task so this commit is not lost.
281 ret=$?
282 case "$ret" in [01]) ;; *) reschedule_last_action ;; esac
283 return $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100284}
285
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100286pick_one_preserving_merges () {
Thomas Rast71d94512008-08-13 23:41:23 +0200287 fast_forward=t
288 case "$1" in
289 -n)
290 fast_forward=f
291 sha1=$2
292 ;;
293 *)
294 sha1=$1
295 ;;
296 esac
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100297 sha1=$(git rev-parse $sha1)
298
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500299 if test -f "$state_dir"/current-commit
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100300 then
Junio C Hamano4c1360f2008-10-22 11:59:30 -0700301 if test "$fast_forward" = t
Stephen Habermanbb645072008-10-15 02:44:36 -0500302 then
Junio C Hamano41f556b2010-07-05 22:16:15 -0700303 while read current_commit
Stephen Habermanbb645072008-10-15 02:44:36 -0500304 do
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500305 git rev-parse HEAD > "$rewritten"/$current_commit
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500306 done <"$state_dir"/current-commit
307 rm "$state_dir"/current-commit ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000308 die "$(gettext "Cannot write current commit's replacement sha1")"
Stephen Habermanbb645072008-10-15 02:44:36 -0500309 fi
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100310 fi
311
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500312 echo $sha1 >> "$state_dir"/current-commit
Thomas Rasta96dc012008-08-13 23:41:24 +0200313
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100314 # rewrite parents; if none were rewritten, we can fast-forward.
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100315 new_parents=
Thomas Rastd911d142009-01-02 23:28:28 +0100316 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
317 if test "$pend" = " "
318 then
319 pend=" root"
320 fi
Stephen Habermanfaae8532008-10-05 23:26:52 -0500321 while [ "$pend" != "" ]
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100322 do
Stephen Habermanfaae8532008-10-05 23:26:52 -0500323 p=$(expr "$pend" : ' \([^ ]*\)')
324 pend="${pend# $p}"
325
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500326 if test -f "$rewritten"/$p
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100327 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500328 new_p=$(cat "$rewritten"/$p)
Stephen Haberman80fe82e2008-10-15 02:44:40 -0500329
330 # If the todo reordered commits, and our parent is marked for
331 # rewriting, but hasn't been gotten to yet, assume the user meant to
332 # drop it on top of the current HEAD
333 if test -z "$new_p"
334 then
335 new_p=$(git rev-parse HEAD)
336 fi
337
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100338 test $p != $new_p && fast_forward=f
339 case "$new_parents" in
340 *$new_p*)
341 ;; # do nothing; that parent is already there
342 *)
343 new_parents="$new_parents $new_p"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100344 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100345 esac
Stephan Beyer1c5fa0a2008-07-16 03:51:49 +0200346 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500347 if test -f "$dropped"/$p
Stephen Habermanfaae8532008-10-05 23:26:52 -0500348 then
349 fast_forward=f
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500350 replacement="$(cat "$dropped"/$p)"
Thomas Rastd911d142009-01-02 23:28:28 +0100351 test -z "$replacement" && replacement=root
352 pend=" $replacement$pend"
Stephen Habermanfaae8532008-10-05 23:26:52 -0500353 else
354 new_parents="$new_parents $p"
355 fi
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100356 fi
357 done
358 case $fast_forward in
359 t)
Vasco Almeida9588c522016-06-17 20:21:03 +0000360 output warn "$(eval_gettext "Fast-forward to \$sha1")"
Thomas Rast71d94512008-08-13 23:41:23 +0200361 output git reset --hard $sha1 ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000362 die "$(eval_gettext "Cannot fast-forward to \$sha1")"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100363 ;;
364 f)
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100365 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
Stephen Habermana4f25e32008-10-15 02:44:37 -0500366
367 if [ "$1" != "-n" ]
368 then
369 # detach HEAD to current parent
370 output git checkout $first_parent 2> /dev/null ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000371 die "$(eval_gettext "Cannot move HEAD to \$first_parent")"
Stephen Habermana4f25e32008-10-15 02:44:37 -0500372 fi
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100373
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100374 case "$new_parents" in
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100375 ' '*' '*)
Vasco Almeida9588c522016-06-17 20:21:03 +0000376 test "a$1" = a-n && die "$(eval_gettext "Refusing to squash a merge: \$sha1")"
Stephen Habermana4f25e32008-10-15 02:44:37 -0500377
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100378 # redo merge
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500379 author_script_content=$(get_author_ident_from_commit $sha1)
380 eval "$author_script_content"
381 msg_content="$(commit_message $sha1)"
Björn Steinbrinkf91333d2007-10-31 03:20:32 +0100382 # No point in merging the first parent, that's HEAD
383 new_parents=${new_parents# $first_parent}
Ralf Thielowa9f739c2013-08-21 20:48:57 +0200384 merge_args="--no-log --no-ff"
Arnaud Fontainedb2b3b82013-07-02 17:05:48 +0900385 if ! do_with_author output eval \
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000386 'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
387 $merge_args $strategy_args -m "$msg_content" $new_parents'
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100388 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500389 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
Vasco Almeida9588c522016-06-17 20:21:03 +0000390 die_with_patch $sha1 "$(eval_gettext "Error redoing merge \$sha1")"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100391 fi
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500392 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100393 ;;
394 *)
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000395 output eval git cherry-pick \
396 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
397 "$strategy_args" "$@" ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000398 die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100399 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100400 esac
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100401 ;;
Johannes Schindelinf09c9b82007-06-25 18:59:43 +0100402 esac
403}
404
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000405this_nth_commit_message () {
406 n=$1
407 case "$n" in
408 1) gettext "This is the 1st commit message:";;
409 2) gettext "This is the 2nd commit message:";;
410 3) gettext "This is the 3rd commit message:";;
411 4) gettext "This is the 4th commit message:";;
412 5) gettext "This is the 5th commit message:";;
413 6) gettext "This is the 6th commit message:";;
414 7) gettext "This is the 7th commit message:";;
415 8) gettext "This is the 8th commit message:";;
416 9) gettext "This is the 9th commit message:";;
417 10) gettext "This is the 10th commit message:";;
418 # TRANSLATORS: if the language you are translating into
419 # doesn't allow you to compose a sentence in this fashion,
420 # consider translating as if this and the following few strings
421 # were "This is the commit message ${n}:"
422 *1[0-9]|*[04-9]) eval_gettext "This is the \${n}th commit message:";;
423 *1) eval_gettext "This is the \${n}st commit message:";;
424 *2) eval_gettext "This is the \${n}nd commit message:";;
425 *3) eval_gettext "This is the \${n}rd commit message:";;
426 *) eval_gettext "This is the commit message \${n}:";;
427 esac
428}
429skip_nth_commit_message () {
430 n=$1
431 case "$n" in
432 1) gettext "The 1st commit message will be skipped:";;
433 2) gettext "The 2nd commit message will be skipped:";;
434 3) gettext "The 3rd commit message will be skipped:";;
435 4) gettext "The 4th commit message will be skipped:";;
436 5) gettext "The 5th commit message will be skipped:";;
437 6) gettext "The 6th commit message will be skipped:";;
438 7) gettext "The 7th commit message will be skipped:";;
439 8) gettext "The 8th commit message will be skipped:";;
440 9) gettext "The 9th commit message will be skipped:";;
441 10) gettext "The 10th commit message will be skipped:";;
442 # TRANSLATORS: if the language you are translating into
443 # doesn't allow you to compose a sentence in this fashion,
444 # consider translating as if this and the following few strings
445 # were "The commit message ${n} will be skipped:"
446 *1[0-9]|*[04-9]) eval_gettext "The \${n}th commit message will be skipped:";;
447 *1) eval_gettext "The \${n}st commit message will be skipped:";;
448 *2) eval_gettext "The \${n}nd commit message will be skipped:";;
449 *3) eval_gettext "The \${n}rd commit message will be skipped:";;
450 *) eval_gettext "The commit message \${n} will be skipped:";;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100451 esac
452}
453
Michael Haggertya25eb132010-01-14 06:54:55 +0100454update_squash_messages () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500455 if test -f "$squash_msg"; then
456 mv "$squash_msg" "$squash_msg".bak || exit
457 count=$(($(sed -n \
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000458 -e "1s/^$comment_char.*\([0-9][0-9]*\).*/\1/p" \
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500459 -e "q" < "$squash_msg".bak)+1))
Michael Haggertybde1a682010-01-14 06:54:54 +0100460 {
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000461 printf '%s\n' "$comment_char $(eval_ngettext \
462 "This is a combination of \$count commit." \
463 "This is a combination of \$count commits." \
464 $count)"
Michael Haggertybde1a682010-01-14 06:54:54 +0100465 sed -e 1d -e '2,/^./{
466 /^$/d
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500467 }' <"$squash_msg".bak
468 } >"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100469 else
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000470 commit_message HEAD > "$fixup_msg" || die "$(gettext "Cannot write \$fixup_msg")"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500471 count=2
Michael Haggertybde1a682010-01-14 06:54:54 +0100472 {
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000473 printf '%s\n' "$comment_char $(gettext "This is a combination of 2 commits.")"
474 printf '%s\n' "$comment_char $(gettext "This is the 1st commit message:")"
Michael Haggertybde1a682010-01-14 06:54:54 +0100475 echo
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500476 cat "$fixup_msg"
477 } >"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100478 fi
Michael Haggerty0205e722009-12-07 10:20:59 +0100479 case $1 in
480 squash)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500481 rm -f "$fixup_msg"
Michael Haggerty0205e722009-12-07 10:20:59 +0100482 echo
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000483 printf '%s\n' "$comment_char $(this_nth_commit_message $count)"
Michael Haggerty0205e722009-12-07 10:20:59 +0100484 echo
Michael Haggertyee0a4af2010-01-14 06:54:51 +0100485 commit_message $2
Michael Haggerty0205e722009-12-07 10:20:59 +0100486 ;;
487 fixup)
488 echo
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000489 printf '%s\n' "$comment_char $(skip_nth_commit_message $count)"
Michael Haggerty0205e722009-12-07 10:20:59 +0100490 echo
John Keeping180bad32013-02-11 23:08:04 +0000491 # Change the space after the comment character to TAB:
492 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
Michael Haggerty0205e722009-12-07 10:20:59 +0100493 ;;
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500494 esac >>"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100495}
496
497peek_next_command () {
John Keeping180bad32013-02-11 23:08:04 +0000498 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100499}
500
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100501# A squash/fixup has failed. Prepare the long version of the squash
502# commit message, then die_with_patch. This code path requires the
503# user to edit the combined commit message for all commits that have
504# been squashed/fixedup so far. So also erase the old squash
505# messages, effectively causing the combined commit to be used as the
506# new basis for any further squash/fixups. Args: sha1 rest
507die_failed_squash() {
Vasco Almeida9588c522016-06-17 20:21:03 +0000508 sha1=$1
509 rest=$2
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500510 mv "$squash_msg" "$msg" || exit
511 rm -f "$fixup_msg"
512 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100513 warn
Vasco Almeida9588c522016-06-17 20:21:03 +0000514 warn "$(eval_gettext "Could not apply \$sha1... \$rest")"
515 die_with_patch $sha1 ""
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100516}
517
Thomas Rastb079fee2010-03-12 18:04:30 +0100518flush_rewritten_pending() {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500519 test -s "$rewritten_pending" || return
Thomas Rastb079fee2010-03-12 18:04:30 +0100520 newsha1="$(git rev-parse HEAD^0)"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500521 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
522 rm -f "$rewritten_pending"
Thomas Rastb079fee2010-03-12 18:04:30 +0100523}
524
525record_in_rewritten() {
526 oldsha1="$(git rev-parse $1)"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500527 echo "$oldsha1" >> "$rewritten_pending"
Thomas Rastb079fee2010-03-12 18:04:30 +0100528
529 case "$(peek_next_command)" in
Junio C Hamano41f556b2010-07-05 22:16:15 -0700530 squash|s|fixup|f)
Thomas Rastb079fee2010-03-12 18:04:30 +0100531 ;;
Junio C Hamano41f556b2010-07-05 22:16:15 -0700532 *)
Thomas Rastb079fee2010-03-12 18:04:30 +0100533 flush_rewritten_pending
534 ;;
535 esac
536}
537
Chris Webbdf5df202012-06-26 22:55:23 +0100538do_pick () {
Vasco Almeida9588c522016-06-17 20:21:03 +0000539 sha1=$1
540 rest=$2
Chris Webbdf5df202012-06-26 22:55:23 +0100541 if test "$(git rev-parse HEAD)" = "$squash_onto"
542 then
543 # Set the correct commit message and author info on the
544 # sentinel root before cherry-picking the original changes
545 # without committing (-n). Finally, update the sentinel again
546 # to include these changes. If the cherry-pick results in a
547 # conflict, this means our behaviour is similar to a standard
548 # failed cherry-pick during rebase, with a dirty index to
549 # resolve before manually running git commit --amend then git
550 # rebase --continue.
551 git commit --allow-empty --allow-empty-message --amend \
Vasco Almeida9588c522016-06-17 20:21:03 +0000552 --no-post-rewrite -n -q -C $sha1 &&
553 pick_one -n $sha1 &&
Chris Webbdf5df202012-06-26 22:55:23 +0100554 git commit --allow-empty --allow-empty-message \
Vasco Almeida9588c522016-06-17 20:21:03 +0000555 --amend --no-post-rewrite -n -q -C $sha1 \
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000556 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000557 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
Chris Webbdf5df202012-06-26 22:55:23 +0100558 else
Vasco Almeida9588c522016-06-17 20:21:03 +0000559 pick_one $sha1 ||
560 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
Chris Webbdf5df202012-06-26 22:55:23 +0100561 fi
562}
563
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100564do_next () {
Matthieu Moyb12d3e92015-05-22 13:15:49 +0000565 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500566 read -r command sha1 rest < "$todo"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100567 case "$command" in
Galan Rémic9266d52015-06-29 22:20:30 +0200568 "$comment_char"*|''|noop|drop|d)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100569 mark_action_done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100570 ;;
Junio C Hamano39743cf2015-10-28 15:54:48 +0100571 "$cr")
572 # Work around CR left by "read" (e.g. with Git for Windows' Bash).
573 mark_action_done
574 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100575 pick|p)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100576 comment_for_reflog pick
577
578 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100579 do_pick $sha1 "$rest"
Thomas Rastb079fee2010-03-12 18:04:30 +0100580 record_in_rewritten $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100581 ;;
Björn Gustavsson6741aa62009-10-07 08:13:23 +0200582 reword|r)
583 comment_for_reflog reword
584
585 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100586 do_pick $sha1 "$rest"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000587 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
Vasco Almeida9588c522016-06-17 20:21:03 +0000588 warn "$(eval_gettext "\
589Could not amend commit after successfully picking \$sha1... \$rest
590This is most likely due to an empty commit message, or the pre-commit hook
591failed. If the pre-commit hook failed, you may need to resolve the issue before
592you are able to reword the commit.")"
Andrew Wong0becb3e2011-11-30 10:52:51 -0500593 exit_with_patch $sha1 1
594 }
Thomas Rastb079fee2010-03-12 18:04:30 +0100595 record_in_rewritten $sha1
Björn Gustavsson6741aa62009-10-07 08:13:23 +0200596 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100597 edit|e)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100598 comment_for_reflog edit
599
600 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100601 do_pick $sha1 "$rest"
Ralf Thielow14c793e2016-03-28 18:00:00 +0200602 sha1_abbrev=$(git rev-parse --short $sha1)
Vasco Almeida9588c522016-06-17 20:21:03 +0000603 warn "$(eval_gettext "Stopped at \$sha1_abbrev... \$rest")"
Andrew Wong0becb3e2011-11-30 10:52:51 -0500604 exit_with_patch $sha1 0
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100605 ;;
Michael Haggerty0205e722009-12-07 10:20:59 +0100606 squash|s|fixup|f)
607 case "$command" in
608 squash|s)
609 squash_style=squash
610 ;;
611 fixup|f)
612 squash_style=fixup
613 ;;
614 esac
615 comment_for_reflog $squash_style
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100616
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500617 test -f "$done" && has_action "$done" ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000618 die "$(eval_gettext "Cannot '\$squash_style' without a previous commit")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100619
620 mark_action_done
Michael Haggertya25eb132010-01-14 06:54:55 +0100621 update_squash_messages $squash_style $sha1
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500622 author_script_content=$(get_author_ident_from_commit HEAD)
623 echo "$author_script_content" > "$author_script"
624 eval "$author_script_content"
Chris Webb2147f842012-07-24 13:17:03 +0100625 if ! pick_one -n $sha1
626 then
627 git rev-parse --verify HEAD >"$amend"
628 die_failed_squash $sha1 "$rest"
629 fi
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100630 case "$(peek_next_command)" in
Michael Haggerty0205e722009-12-07 10:20:59 +0100631 squash|s|fixup|f)
Michael Haggertya25eb132010-01-14 06:54:55 +0100632 # This is an intermediate commit; its message will only be
633 # used in case of trouble. So use the long version:
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000634 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
635 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100636 die_failed_squash $sha1 "$rest"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100637 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100638 *)
Michael Haggertya25eb132010-01-14 06:54:55 +0100639 # This is the final command of this squash/fixup group
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500640 if test -f "$fixup_msg"
Michael Haggertya25eb132010-01-14 06:54:55 +0100641 then
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000642 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
643 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100644 die_failed_squash $sha1 "$rest"
645 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500646 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100647 rm -f "$GIT_DIR"/MERGE_MSG
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000648 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
649 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100650 die_failed_squash $sha1 "$rest"
Michael Haggertya25eb132010-01-14 06:54:55 +0100651 fi
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500652 rm -f "$squash_msg" "$fixup_msg"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100653 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100654 esac
Thomas Rastb079fee2010-03-12 18:04:30 +0100655 record_in_rewritten $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100656 ;;
Matthieu Moycd035b12010-08-10 17:17:51 +0200657 x|"exec")
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500658 read -r command rest < "$todo"
Matthieu Moycd035b12010-08-10 17:17:51 +0200659 mark_action_done
Vasco Almeida9588c522016-06-17 20:21:03 +0000660 eval_gettextln "Executing: \$rest"
Fredrik Medleyfe9394a2015-11-13 07:03:19 +0100661 "${SHELL:-@SHELL_PATH@}" -c "$rest" # Actual execution
Matthieu Moycd035b12010-08-10 17:17:51 +0200662 status=$?
Junio C Hamano16865192011-08-24 11:54:50 -0700663 # Run in subshell because require_clean_work_tree can die.
664 dirty=f
665 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
Matthieu Moycd035b12010-08-10 17:17:51 +0200666 if test "$status" -ne 0
667 then
Vasco Almeida9588c522016-06-17 20:21:03 +0000668 warn "$(eval_gettext "Execution failed: \$rest")"
Junio C Hamano16865192011-08-24 11:54:50 -0700669 test "$dirty" = f ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000670 warn "$(gettext "and made changes to the index and/or the working tree")"
Junio C Hamano16865192011-08-24 11:54:50 -0700671
Vasco Almeida9588c522016-06-17 20:21:03 +0000672 warn "$(gettext "\
673You can fix the problem, and then run
674
675 git rebase --continue")"
Matthieu Moycd035b12010-08-10 17:17:51 +0200676 warn
Johannes Sixtecfe1ea2012-09-18 13:15:26 +0200677 if test $status -eq 127 # command not found
678 then
679 status=1
680 fi
Matthieu Moycd035b12010-08-10 17:17:51 +0200681 exit "$status"
Junio C Hamano16865192011-08-24 11:54:50 -0700682 elif test "$dirty" = t
Matthieu Moycd035b12010-08-10 17:17:51 +0200683 then
Vasco Almeida9588c522016-06-17 20:21:03 +0000684 # TRANSLATORS: after these lines is a command to be issued by the user
685 warn "$(eval_gettext "\
686Execution succeeded: \$rest
687but left changes to the index and/or the working tree
688Commit or stash your changes, and then run
689
690 git rebase --continue")"
Matthieu Moycd035b12010-08-10 17:17:51 +0200691 warn
692 exit 1
693 fi
694 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100695 *)
Vasco Almeida9588c522016-06-17 20:21:03 +0000696 warn "$(eval_gettext "Unknown command: \$command \$sha1 \$rest")"
697 fixtodo="$(gettext "Please fix this using 'git rebase --edit-todo'.")"
Jan Krügerf1be3162009-10-27 15:58:14 +0100698 if git rev-parse --verify -q "$sha1" >/dev/null
699 then
Johannes Sixt9c8e1012012-09-19 08:43:54 +0200700 die_with_patch $sha1 "$fixtodo"
Jan Krügerf1be3162009-10-27 15:58:14 +0100701 else
Johannes Sixt9c8e1012012-09-19 08:43:54 +0200702 die "$fixtodo"
Jan Krügerf1be3162009-10-27 15:58:14 +0100703 fi
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100704 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100705 esac
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500706 test -s "$todo" && return
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100707
Johannes Schindelin68a163c2007-06-25 18:58:28 +0100708 comment_for_reflog finish &&
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500709 newhead=$(git rev-parse HEAD) &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500710 case $head_name in
Johannes Schindelin73697a02007-09-25 16:43:15 +0100711 refs/*)
Michael J Gruber1af221e2012-08-10 08:51:19 +0200712 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500713 git update-ref -m "$message" $head_name $newhead $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400714 git symbolic-ref \
715 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
716 HEAD $head_name
Johannes Schindelin73697a02007-09-25 16:43:15 +0100717 ;;
718 esac && {
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500719 test ! -f "$state_dir"/verbose ||
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500720 git diff-tree --stat $orig_head..HEAD
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100721 } &&
Thomas Rasteb2151b2010-03-12 18:04:33 +0100722 {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500723 test -s "$rewritten_list" &&
724 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
Thomas Rasteb2151b2010-03-12 18:04:33 +0100725 true # we don't care if this copying failed
726 } &&
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700727 hook="$(git rev-parse --git-path hooks/post-rewrite)"
728 if test -x "$hook" && test -s "$rewritten_list"; then
729 "$hook" rebase < "$rewritten_list"
Thomas Rastb079fee2010-03-12 18:04:30 +0100730 true # we don't care if this hook failed
731 fi &&
Vasco Almeida9588c522016-06-17 20:21:03 +0000732 warn "$(eval_gettext "Successfully rebased and updated \$head_name.")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100733
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530734 return 1 # not failure; just to break the do_rest loop
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100735}
736
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530737# can only return 0, when the infinite loop breaks
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100738do_rest () {
739 while :
740 do
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530741 do_next || break
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100742 done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100743}
744
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100745# skip picking commits whose parents are unchanged
746skip_unnecessary_picks () {
747 fd=3
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500748 while read -r command rest
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100749 do
750 # fd=3 means we skip the command
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500751 case "$fd,$command" in
752 3,pick|3,p)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500753 # pick a commit whose parent is current $onto -> skip
Chris Johnsen14d87292010-09-17 16:42:51 -0500754 sha1=${rest%% *}
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500755 case "$(git rev-parse --verify --quiet "$sha1"^)" in
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500756 "$onto"*)
757 onto=$sha1
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500758 ;;
759 *)
760 fd=1
761 ;;
762 esac
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100763 ;;
Eric Sunshine7bca7af2013-08-16 17:44:07 -0400764 3,"$comment_char"*|3,)
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100765 # copy comments
766 ;;
767 *)
768 fd=1
769 ;;
770 esac
Brandon Caseyd1c3b102010-08-13 15:47:35 -0500771 printf '%s\n' "$command${rest:+ }$rest" >&$fd
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500772 done <"$todo" >"$todo.new" 3>>"$done" &&
773 mv -f "$todo".new "$todo" &&
Thomas Rastb079fee2010-03-12 18:04:30 +0100774 case "$(peek_next_command)" in
775 squash|s|fixup|f)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500776 record_in_rewritten "$onto"
Thomas Rastb079fee2010-03-12 18:04:30 +0100777 ;;
778 esac ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000779 die "$(gettext "Could not skip unnecessary pick commands")"
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100780}
781
Junio C Hamano75c69762013-08-23 20:10:42 -0400782transform_todo_ids () {
783 while read -r command rest
784 do
785 case "$command" in
786 "$comment_char"* | exec)
787 # Be careful for oddball commands like 'exec'
788 # that do not have a SHA-1 at the beginning of $rest.
789 ;;
790 *)
Matthieu Moy31bff642015-10-01 10:18:41 +0200791 sha1=$(git rev-parse --verify --quiet "$@" ${rest%%[ ]*}) &&
792 rest="$sha1 ${rest#*[ ]}"
Junio C Hamano75c69762013-08-23 20:10:42 -0400793 ;;
794 esac
795 printf '%s\n' "$command${rest:+ }$rest"
796 done <"$todo" >"$todo.new" &&
797 mv -f "$todo.new" "$todo"
798}
799
800expand_todo_ids() {
801 transform_todo_ids
802}
803
804collapse_todo_ids() {
Kirill A. Shutemov56895032013-09-28 18:53:05 +0300805 transform_todo_ids --short
Junio C Hamano75c69762013-08-23 20:10:42 -0400806}
807
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900808# Rearrange the todo list that has both "pick sha1 msg" and
809# "pick sha1 fixup!/squash! msg" appears in it so that the latter
810# comes immediately after the former, and change "pick" to
811# "fixup"/"squash".
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400812#
813# Note that if the config has specified a custom instruction format
814# each log message will be re-retrieved in order to normalize the
815# autosquash arrangement
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900816rearrange_squash () {
Kevin Ballard68d5d032010-11-04 15:36:32 -0700817 # extract fixup!/squash! lines and resolve any referenced sha1's
818 while read -r pick sha1 message
819 do
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400820 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
Kevin Ballard68d5d032010-11-04 15:36:32 -0700821 case "$message" in
822 "squash! "*|"fixup! "*)
823 action="${message%%!*}"
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700824 rest=$message
825 prefix=
826 # skip all squash! or fixup! (but save for later)
827 while :
828 do
829 case "$rest" in
830 "squash! "*|"fixup! "*)
831 prefix="$prefix${rest%%!*},"
832 rest="${rest#*! }"
833 ;;
834 *)
835 break
836 ;;
837 esac
838 done
Uwe Storbeck47be0662014-03-15 00:56:43 +0100839 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
Kevin Ballard68d5d032010-11-04 15:36:32 -0700840 # if it's a single word, try to resolve to a full sha1 and
841 # emit a second copy. This allows us to match on both message
842 # and on sha1 prefix
843 if test "${rest#* }" = "$rest"; then
844 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
845 if test -n "$fullsha"; then
846 # prefix the action to uniquely identify this line as
847 # intended for full sha1 match
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700848 echo "$sha1 +$action $prefix $fullsha"
Kevin Ballard68d5d032010-11-04 15:36:32 -0700849 fi
850 fi
851 esac
852 done >"$1.sq" <"$1"
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900853 test -s "$1.sq" || return
854
855 used=
Junio C Hamano57f2b6b2010-07-05 23:08:36 -0700856 while read -r pick sha1 message
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900857 do
858 case " $used" in
859 *" $sha1 "*) continue ;;
860 esac
Brandon Casey938791c2010-07-22 14:15:11 -0500861 printf '%s\n' "$pick $sha1 $message"
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400862 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
Kevin Ballardd3d7a422010-11-04 15:36:31 -0700863 used="$used$sha1 "
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700864 while read -r squash action msg_prefix msg_content
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900865 do
Kevin Ballardd3d7a422010-11-04 15:36:31 -0700866 case " $used" in
867 *" $squash "*) continue ;;
868 esac
Kevin Ballard68d5d032010-11-04 15:36:32 -0700869 emit=0
870 case "$action" in
871 +*)
872 action="${action#+}"
873 # full sha1 prefix test
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500874 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
Kevin Ballard68d5d032010-11-04 15:36:32 -0700875 *)
876 # message prefix test
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500877 case "$message" in "$msg_content"*) emit=1;; esac ;;
Kevin Ballard68d5d032010-11-04 15:36:32 -0700878 esac
879 if test $emit = 1; then
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400880 if test -n "${format}"
881 then
882 msg_content=$(git log -n 1 --format="${format}" ${squash})
883 else
884 msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
885 fi
886 printf '%s\n' "$action $squash $msg_content"
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900887 used="$used$squash "
Kevin Ballard68d5d032010-11-04 15:36:32 -0700888 fi
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900889 done <"$1.sq"
890 done >"$1.rearranged" <"$1"
891 cat "$1.rearranged" >"$1"
892 rm -f "$1.sq" "$1.rearranged"
893}
894
Lucien Kongc2145382012-06-12 10:05:12 +0200895# Add commands after a pick or after a squash/fixup serie
896# in the todo list.
897add_exec_commands () {
898 {
899 first=t
900 while read -r insn rest
901 do
902 case $insn in
903 pick)
904 test -n "$first" ||
905 printf "%s" "$cmd"
906 ;;
907 esac
908 printf "%s %s\n" "$insn" "$rest"
909 first=
910 done
911 printf "%s" "$cmd"
912 } <"$1" >"$1.new" &&
913 mv "$1.new" "$1"
914}
915
Galan Rémi804098b2015-06-29 22:20:32 +0200916# Check if the SHA-1 passed as an argument is a
917# correct one, if not then print $2 in "$todo".badsha
918# $1: the SHA-1 to test
Matthieu Moy1db168e2015-10-01 10:18:42 +0200919# $2: the line number of the input
920# $3: the input filename
Galan Rémi804098b2015-06-29 22:20:32 +0200921check_commit_sha () {
922 badsha=0
Jeff King268ef4d2016-05-13 16:47:33 -0400923 if test -z "$1"
Galan Rémi804098b2015-06-29 22:20:32 +0200924 then
925 badsha=1
926 else
927 sha1_verif="$(git rev-parse --verify --quiet $1^{commit})"
Jeff King268ef4d2016-05-13 16:47:33 -0400928 if test -z "$sha1_verif"
Galan Rémi804098b2015-06-29 22:20:32 +0200929 then
930 badsha=1
931 fi
932 fi
933
934 if test $badsha -ne 0
935 then
Matthieu Moy1db168e2015-10-01 10:18:42 +0200936 line="$(sed -n -e "${2}p" "$3")"
Vasco Almeida9588c522016-06-17 20:21:03 +0000937 warn "$(eval_gettext "\
938Warning: the SHA-1 is missing or isn't a commit in the following line:
939 - \$line")"
Galan Rémi804098b2015-06-29 22:20:32 +0200940 warn
941 fi
942
943 return $badsha
944}
945
946# prints the bad commits and bad commands
947# from the todolist in stdin
948check_bad_cmd_and_sha () {
949 retval=0
Matthieu Moy1db168e2015-10-01 10:18:42 +0200950 lineno=0
951 while read -r command rest
952 do
953 lineno=$(( $lineno + 1 ))
954 case $command in
955 "$comment_char"*|''|noop|x|exec)
956 # Doesn't expect a SHA-1
957 ;;
Junio C Hamano39743cf2015-10-28 15:54:48 +0100958 "$cr")
959 # Work around CR left by "read" (e.g. with Git for
960 # Windows' Bash).
961 ;;
Matthieu Moy1db168e2015-10-01 10:18:42 +0200962 pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
963 if ! check_commit_sha "${rest%%[ ]*}" "$lineno" "$1"
964 then
Galan Rémi804098b2015-06-29 22:20:32 +0200965 retval=1
Matthieu Moy1db168e2015-10-01 10:18:42 +0200966 fi
967 ;;
968 *)
969 line="$(sed -n -e "${lineno}p" "$1")"
Vasco Almeida9588c522016-06-17 20:21:03 +0000970 warn "$(eval_gettext "\
971Warning: the command isn't recognized in the following line:
972 - \$line")"
Matthieu Moy1db168e2015-10-01 10:18:42 +0200973 warn
974 retval=1
975 ;;
976 esac
977 done <"$1"
978 return $retval
Galan Rémi804098b2015-06-29 22:20:32 +0200979}
980
Galan Rémi37079952015-06-29 22:20:31 +0200981# Print the list of the SHA-1 of the commits
982# from stdin to stdout
983todo_list_to_sha_list () {
984 git stripspace --strip-comments |
985 while read -r command sha1 rest
986 do
987 case $command in
988 "$comment_char"*|''|noop|x|"exec")
989 ;;
990 *)
991 long_sha=$(git rev-list --no-walk "$sha1" 2>/dev/null)
992 printf "%s\n" "$long_sha"
993 ;;
994 esac
995 done
996}
997
998# Use warn for each line in stdin
999warn_lines () {
1000 while read -r line
1001 do
1002 warn " - $line"
1003 done
1004}
1005
1006# Switch to the branch in $into and notify it in the reflog
1007checkout_onto () {
1008 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
Vasco Almeida9588c522016-06-17 20:21:03 +00001009 output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
Galan Rémi37079952015-06-29 22:20:31 +02001010 git update-ref ORIG_HEAD $orig_head
1011}
1012
1013get_missing_commit_check_level () {
1014 check_level=$(git config --get rebase.missingCommitsCheck)
1015 check_level=${check_level:-ignore}
1016 # Don't be case sensitive
1017 printf '%s' "$check_level" | tr 'A-Z' 'a-z'
1018}
1019
1020# Check if the user dropped some commits by mistake
1021# Behaviour determined by rebase.missingCommitsCheck.
Galan Rémi804098b2015-06-29 22:20:32 +02001022# Check if there is an unrecognized command or a
1023# bad SHA-1 in a command.
Galan Rémi37079952015-06-29 22:20:31 +02001024check_todo_list () {
1025 raise_error=f
1026
1027 check_level=$(get_missing_commit_check_level)
1028
1029 case "$check_level" in
1030 warn|error)
1031 # Get the SHA-1 of the commits
1032 todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
1033 todo_list_to_sha_list <"$todo" >"$todo".newsha1
1034
1035 # Sort the SHA-1 and compare them
1036 sort -u "$todo".oldsha1 >"$todo".oldsha1+
1037 mv "$todo".oldsha1+ "$todo".oldsha1
1038 sort -u "$todo".newsha1 >"$todo".newsha1+
1039 mv "$todo".newsha1+ "$todo".newsha1
1040 comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
1041
1042 # Warn about missing commits
1043 if test -s "$todo".miss
1044 then
1045 test "$check_level" = error && raise_error=t
1046
Vasco Almeida9588c522016-06-17 20:21:03 +00001047 warn "$(gettext "\
1048Warning: some commits may have been dropped accidentally.
1049Dropped commits (newer to older):")"
Galan Rémi37079952015-06-29 22:20:31 +02001050
1051 # Make the list user-friendly and display
1052 opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
1053 git rev-list $opt <"$todo".miss | warn_lines
1054
Vasco Almeida9588c522016-06-17 20:21:03 +00001055 warn "$(gettext "\
1056To avoid this message, use \"drop\" to explicitly remove a commit.
1057
1058Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1059The possible behaviours are: ignore, warn, error.")"
Galan Rémi37079952015-06-29 22:20:31 +02001060 warn
1061 fi
1062 ;;
1063 ignore)
1064 ;;
1065 *)
Vasco Almeida9588c522016-06-17 20:21:03 +00001066 warn "$(eval_gettext "Unrecognized setting \$check_level for option rebase.missingCommitsCheck. Ignoring.")"
Galan Rémi37079952015-06-29 22:20:31 +02001067 ;;
1068 esac
1069
Matthieu Moy1db168e2015-10-01 10:18:42 +02001070 if ! check_bad_cmd_and_sha "$todo"
Galan Rémi804098b2015-06-29 22:20:32 +02001071 then
1072 raise_error=t
1073 fi
1074
Galan Rémi37079952015-06-29 22:20:31 +02001075 if test $raise_error = t
1076 then
1077 # Checkout before the first commit of the
1078 # rebase: this way git rebase --continue
1079 # will work correctly as it expects HEAD to be
1080 # placed before the commit of the next action
1081 checkout_onto
1082
Vasco Almeida9588c522016-06-17 20:21:03 +00001083 warn "$(gettext "You can fix this with 'git rebase --edit-todo'.")"
1084 die "$(gettext "Or you can abort the rebase with 'git rebase --abort'.")"
Galan Rémi37079952015-06-29 22:20:31 +02001085 fi
1086}
1087
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001088# The whole contents of this file is run by dot-sourcing it from
1089# inside a shell function. It used to be that "return"s we see
1090# below were not inside any function, and expected to return
1091# to the function that dot-sourced us.
1092#
Ed Maste9b35cad2016-06-17 11:33:29 -04001093# However, older (9.x) versions of FreeBSD /bin/sh misbehave on such a
1094# construct and continue to run the statements that follow such a "return".
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001095# As a work-around, we introduce an extra layer of a function
1096# here, and immediately call it after defining it.
1097git_rebase__interactive () {
1098
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001099case "$action" in
1100continue)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001101 # do we have anything to commit?
John Keepinga6754cd2012-04-07 11:20:53 +01001102 if git diff-index --cached --quiet HEAD --
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001103 then
Johannes Schindelin0e0aff42015-06-18 18:38:53 +02001104 # Nothing to commit -- skip this commit
1105
1106 test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
1107 rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001108 die "$(gettext "Could not remove CHERRY_PICK_HEAD")"
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001109 else
Matthieu Moyffaaed82011-08-24 16:01:48 +02001110 if ! test -f "$author_script"
1111 then
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001112 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
Vasco Almeida9588c522016-06-17 20:21:03 +00001113 die "$(eval_gettext "\
1114You have staged changes in your working tree.
1115If these changes are meant to be
Matthieu Moyffaaed82011-08-24 16:01:48 +02001116squashed into the previous commit, run:
1117
Vasco Almeida9588c522016-06-17 20:21:03 +00001118 git commit --amend \$gpg_sign_opt_quoted
Matthieu Moyffaaed82011-08-24 16:01:48 +02001119
1120If they are meant to go into a new commit, run:
1121
Vasco Almeida9588c522016-06-17 20:21:03 +00001122 git commit \$gpg_sign_opt_quoted
Matthieu Moyffaaed82011-08-24 16:01:48 +02001123
1124In both case, once you're done, continue with:
1125
1126 git rebase --continue
Vasco Almeida9588c522016-06-17 20:21:03 +00001127")"
Matthieu Moyffaaed82011-08-24 16:01:48 +02001128 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001129 . "$author_script" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001130 die "$(gettext "Error trying to find the author identity to amend commit")"
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001131 if test -f "$amend"
Shawn O. Pearce03270622007-12-20 02:12:12 -05001132 then
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001133 current_head=$(git rev-parse --verify HEAD)
1134 test "$current_head" = $(cat "$amend") ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001135 die "$(gettext "\
1136You have uncommitted changes in your working tree. Please commit them
1137first and then run 'git rebase --continue' again.")"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001138 do_with_author git commit --amend --no-verify -F "$msg" -e \
1139 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001140 die "$(gettext "Could not commit staged changes.")"
Chris Webb2147f842012-07-24 13:17:03 +01001141 else
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001142 do_with_author git commit --no-verify -F "$msg" -e \
1143 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001144 die "$(gettext "Could not commit staged changes.")"
Shawn O. Pearce03270622007-12-20 02:12:12 -05001145 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001146 fi
Johannes Schindelin18640d92007-07-08 03:01:29 +01001147
Matthieu Moyb12d3e92015-05-22 13:15:49 +00001148 if test -r "$state_dir"/stopped-sha
1149 then
1150 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
1151 fi
Thomas Rast0acb62f2010-03-28 21:36:00 +02001152
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001153 require_clean_work_tree "rebase"
1154 do_rest
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +05301155 return 0
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001156 ;;
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001157skip)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001158 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001159
Martin von Zweigbergk2959c282011-02-06 13:43:52 -05001160 do_rest
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +05301161 return 0
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001162 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001163edit-todo)
John Keeping180bad32013-02-11 23:08:04 +00001164 git stripspace --strip-comments <"$todo" >"$todo".new
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001165 mv -f "$todo".new "$todo"
Junio C Hamano75c69762013-08-23 20:10:42 -04001166 collapse_todo_ids
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001167 append_todo_help
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001168 gettext "
John Keeping180bad32013-02-11 23:08:04 +00001169You are editing the todo file of an ongoing interactive rebase.
1170To continue rebase after editing, run:
1171 git rebase --continue
1172
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001173" | git stripspace --comment-lines >>"$todo"
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001174
1175 git_sequence_editor "$todo" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001176 die "$(gettext "Could not execute editor")"
Junio C Hamano75c69762013-08-23 20:10:42 -04001177 expand_todo_ids
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001178
1179 exit
1180 ;;
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001181esac
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001182
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001183git var GIT_COMMITTER_IDENT >/dev/null ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001184 die "$(gettext "You need to set your committer info first")"
Thomas Rastd8fab022009-01-02 23:28:26 +01001185
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001186comment_for_reflog start
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001187
Martin von Zweigbergk71786f52011-02-06 13:43:42 -05001188if test ! -z "$switch_to"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001189then
Ramkumar Ramachandra26cd1602013-06-16 14:15:13 +05301190 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
Martin von Zweigbergk71786f52011-02-06 13:43:42 -05001191 output git checkout "$switch_to" -- ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001192 die "$(eval_gettext "Could not checkout \$switch_to")"
Ramkumar Ramachandra26cd1602013-06-16 14:15:13 +05301193
1194 comment_for_reflog start
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001195fi
Matt Kraai51668102007-09-25 18:30:13 -07001196
Vasco Almeida9588c522016-06-17 20:21:03 +00001197orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
1198mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001199
Vasco Almeida9588c522016-06-17 20:21:03 +00001200: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
Martin von Zweigbergk84df4562011-02-06 13:43:53 -05001201write_basic_state
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001202if test t = "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001203then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001204 if test -z "$rebase_root"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001205 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001206 mkdir "$rewritten" &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001207 for c in $(git merge-base --all $orig_head $upstream)
Stephen Habermanacc85592008-10-15 02:44:38 -05001208 do
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001209 echo $onto > "$rewritten"/$c ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001210 die "$(gettext "Could not init rewritten commits")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001211 done
1212 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001213 mkdir "$rewritten" &&
1214 echo $onto > "$rewritten"/root ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001215 die "$(gettext "Could not init rewritten commits")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001216 fi
1217 # No cherry-pick because our first pass is to determine
1218 # parents to rewrite and skipping dropped commits would
1219 # prematurely end our probe
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001220 merges_option=
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001221else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001222 merges_option="--no-merges --cherry-pick"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001223fi
1224
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001225shorthead=$(git rev-parse --short $orig_head)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001226shortonto=$(git rev-parse --short $onto)
1227if test -z "$rebase_root"
1228 # this is now equivalent to ! -z "$upstream"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001229then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001230 shortupstream=$(git rev-parse --short $upstream)
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001231 revisions=$upstream...$orig_head
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001232 shortrevisions=$shortupstream..$shorthead
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001233else
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001234 revisions=$onto...$orig_head
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001235 shortrevisions=$shorthead
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001236fi
Michael Rappazzo16cf51c2015-06-13 12:26:58 -04001237format=$(git config --get rebase.instructionFormat)
1238# the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
1239git rev-list $merges_option --format="%m%H ${format:-%s}" \
1240 --reverse --left-right --topo-order \
John Keeping1e0dacd2014-07-16 20:23:49 +01001241 $revisions ${restrict_revision+^$restrict_revision} | \
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001242 sed -n "s/^>//p" |
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001243while read -r sha1 rest
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001244do
Neil Horman90e18182012-04-20 10:36:17 -04001245
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001246 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
Neil Horman90e18182012-04-20 10:36:17 -04001247 then
John Keeping180bad32013-02-11 23:08:04 +00001248 comment_out="$comment_char "
Neil Horman90e18182012-04-20 10:36:17 -04001249 else
1250 comment_out=
1251 fi
1252
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001253 if test t != "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001254 then
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001255 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001256 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001257 if test -z "$rebase_root"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001258 then
1259 preserve=t
1260 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1261 do
Andrew Wong12bf8282011-06-18 18:12:01 -04001262 if test -f "$rewritten"/$p
Thomas Rastd911d142009-01-02 23:28:28 +01001263 then
Thomas Rastd911d142009-01-02 23:28:28 +01001264 preserve=f
1265 fi
Stephen Habermanacc85592008-10-15 02:44:38 -05001266 done
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001267 else
1268 preserve=f
Stephen Habermanacc85592008-10-15 02:44:38 -05001269 fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001270 if test f = "$preserve"
1271 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001272 touch "$rewritten"/$sha1
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001273 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001274 fi
1275 fi
1276done
Thomas Rastd911d142009-01-02 23:28:28 +01001277
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001278# Watch for commits that been dropped by --cherry-pick
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001279if test t = "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001280then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001281 mkdir "$dropped"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001282 # Save all non-cherry-picked changes
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001283 git rev-list $revisions --left-right --cherry-pick | \
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001284 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001285 # Now all commits and note which ones are missing in
1286 # not-cherry-picks and hence being dropped
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001287 git rev-list $revisions |
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001288 while read rev
1289 do
Junio C Hamano214123c2016-03-08 15:51:36 -08001290 if test -f "$rewritten"/$rev &&
1291 ! sane_grep "$rev" "$state_dir"/not-cherry-picks >/dev/null
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001292 then
1293 # Use -f2 because if rev-list is telling us this commit is
1294 # not worthwhile, we don't want to track its multiple heads,
1295 # just the history of its first-parent for others that will
1296 # be rebasing on top of it
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001297 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001298 sha1=$(git rev-list -1 $rev)
1299 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001300 rm "$rewritten"/$rev
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001301 fi
1302 done
1303fi
1304
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001305test -s "$todo" || echo noop >> "$todo"
1306test -n "$autosquash" && rearrange_squash "$todo"
Lucien Kongc2145382012-06-12 10:05:12 +02001307test -n "$cmd" && add_exec_commands "$todo"
1308
Onno Kortmann97f05f42014-12-10 19:16:44 +01001309todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
Eric Sunshine28c8cfc2015-03-04 02:53:04 -05001310todocount=${todocount##* }
Onno Kortmann97f05f42014-12-10 19:16:44 +01001311
John Keeping180bad32013-02-11 23:08:04 +00001312cat >>"$todo" <<EOF
Johannes Schindelin6047a232007-11-22 12:30:10 +00001313
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001314$comment_char $(eval_ngettext \
1315 "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
1316 "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
1317 "$todocount")
Andrew Wongfcc5ef12012-09-17 21:28:08 -04001318EOF
1319append_todo_help
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001320gettext "
John Keeping180bad32013-02-11 23:08:04 +00001321However, if you remove everything, the rebase will be aborted.
1322
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001323" | git stripspace --comment-lines >>"$todo"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001324
Neil Horman90e18182012-04-20 10:36:17 -04001325if test -z "$keep_empty"
1326then
Vasco Almeida9588c522016-06-17 20:21:03 +00001327 printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
Neil Horman90e18182012-04-20 10:36:17 -04001328fi
1329
1330
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001331has_action "$todo" ||
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -04001332 return 2
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001333
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001334cp "$todo" "$todo".backup
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001335collapse_todo_ids
Peter Oberndorfer821881d2011-10-17 22:26:23 +02001336git_sequence_editor "$todo" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001337 die_abort "$(gettext "Could not execute editor")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001338
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001339has_action "$todo" ||
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -04001340 return 2
Johannes Schindelinc54b7812007-06-25 18:56:55 +01001341
Galan Rémi37079952015-06-29 22:20:31 +02001342check_todo_list
1343
Junio C Hamano75c69762013-08-23 20:10:42 -04001344expand_todo_ids
1345
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001346test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
Johannes Schindelin0e757e32009-03-03 10:55:31 +01001347
Galan Rémi37079952015-06-29 22:20:31 +02001348checkout_onto
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001349do_rest
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001350
1351}
1352# ... and then we call the whole thing.
1353git_rebase__interactive