blob: 7e558b068c277c2f079de04eb96161c529e11337 [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"
Johannes Sixtd9497512016-07-28 19:47:23 +0200124 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"
194 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
Jean-Noel Avila8aa6dc12016-08-21 16:50:38 +0200407 eval_gettext "This is the commit message #\${n}:"
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000408}
Jean-Noel Avila8aa6dc12016-08-21 16:50:38 +0200409
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000410skip_nth_commit_message () {
411 n=$1
Jean-Noel Avila8aa6dc12016-08-21 16:50:38 +0200412 eval_gettext "The commit message #\${n} will be skipped:"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100413}
414
Michael Haggertya25eb132010-01-14 06:54:55 +0100415update_squash_messages () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500416 if test -f "$squash_msg"; then
417 mv "$squash_msg" "$squash_msg".bak || exit
418 count=$(($(sed -n \
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000419 -e "1s/^$comment_char.*\([0-9][0-9]*\).*/\1/p" \
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500420 -e "q" < "$squash_msg".bak)+1))
Michael Haggertybde1a682010-01-14 06:54:54 +0100421 {
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000422 printf '%s\n' "$comment_char $(eval_ngettext \
423 "This is a combination of \$count commit." \
424 "This is a combination of \$count commits." \
425 $count)"
Michael Haggertybde1a682010-01-14 06:54:54 +0100426 sed -e 1d -e '2,/^./{
427 /^$/d
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500428 }' <"$squash_msg".bak
429 } >"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100430 else
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000431 commit_message HEAD > "$fixup_msg" || die "$(gettext "Cannot write \$fixup_msg")"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500432 count=2
Michael Haggertybde1a682010-01-14 06:54:54 +0100433 {
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000434 printf '%s\n' "$comment_char $(gettext "This is a combination of 2 commits.")"
435 printf '%s\n' "$comment_char $(gettext "This is the 1st commit message:")"
Michael Haggertybde1a682010-01-14 06:54:54 +0100436 echo
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500437 cat "$fixup_msg"
438 } >"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100439 fi
Michael Haggerty0205e722009-12-07 10:20:59 +0100440 case $1 in
441 squash)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500442 rm -f "$fixup_msg"
Michael Haggerty0205e722009-12-07 10:20:59 +0100443 echo
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000444 printf '%s\n' "$comment_char $(this_nth_commit_message $count)"
Michael Haggerty0205e722009-12-07 10:20:59 +0100445 echo
Michael Haggertyee0a4af2010-01-14 06:54:51 +0100446 commit_message $2
Michael Haggerty0205e722009-12-07 10:20:59 +0100447 ;;
448 fixup)
449 echo
Vasco Almeidaf2d17062016-06-17 20:21:05 +0000450 printf '%s\n' "$comment_char $(skip_nth_commit_message $count)"
Michael Haggerty0205e722009-12-07 10:20:59 +0100451 echo
John Keeping180bad32013-02-11 23:08:04 +0000452 # Change the space after the comment character to TAB:
453 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
Michael Haggerty0205e722009-12-07 10:20:59 +0100454 ;;
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500455 esac >>"$squash_msg"
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100456}
457
458peek_next_command () {
John Keeping180bad32013-02-11 23:08:04 +0000459 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100460}
461
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100462# A squash/fixup has failed. Prepare the long version of the squash
463# commit message, then die_with_patch. This code path requires the
464# user to edit the combined commit message for all commits that have
465# been squashed/fixedup so far. So also erase the old squash
466# messages, effectively causing the combined commit to be used as the
467# new basis for any further squash/fixups. Args: sha1 rest
468die_failed_squash() {
Vasco Almeida9588c522016-06-17 20:21:03 +0000469 sha1=$1
470 rest=$2
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500471 mv "$squash_msg" "$msg" || exit
472 rm -f "$fixup_msg"
473 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100474 warn
Vasco Almeida9588c522016-06-17 20:21:03 +0000475 warn "$(eval_gettext "Could not apply \$sha1... \$rest")"
476 die_with_patch $sha1 ""
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100477}
478
Thomas Rastb079fee2010-03-12 18:04:30 +0100479flush_rewritten_pending() {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500480 test -s "$rewritten_pending" || return
Thomas Rastb079fee2010-03-12 18:04:30 +0100481 newsha1="$(git rev-parse HEAD^0)"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500482 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
483 rm -f "$rewritten_pending"
Thomas Rastb079fee2010-03-12 18:04:30 +0100484}
485
486record_in_rewritten() {
487 oldsha1="$(git rev-parse $1)"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500488 echo "$oldsha1" >> "$rewritten_pending"
Thomas Rastb079fee2010-03-12 18:04:30 +0100489
490 case "$(peek_next_command)" in
Junio C Hamano41f556b2010-07-05 22:16:15 -0700491 squash|s|fixup|f)
Thomas Rastb079fee2010-03-12 18:04:30 +0100492 ;;
Junio C Hamano41f556b2010-07-05 22:16:15 -0700493 *)
Thomas Rastb079fee2010-03-12 18:04:30 +0100494 flush_rewritten_pending
495 ;;
496 esac
497}
498
Chris Webbdf5df202012-06-26 22:55:23 +0100499do_pick () {
Vasco Almeida9588c522016-06-17 20:21:03 +0000500 sha1=$1
501 rest=$2
Chris Webbdf5df202012-06-26 22:55:23 +0100502 if test "$(git rev-parse HEAD)" = "$squash_onto"
503 then
504 # Set the correct commit message and author info on the
505 # sentinel root before cherry-picking the original changes
506 # without committing (-n). Finally, update the sentinel again
507 # to include these changes. If the cherry-pick results in a
508 # conflict, this means our behaviour is similar to a standard
509 # failed cherry-pick during rebase, with a dirty index to
510 # resolve before manually running git commit --amend then git
511 # rebase --continue.
512 git commit --allow-empty --allow-empty-message --amend \
Vasco Almeida9588c522016-06-17 20:21:03 +0000513 --no-post-rewrite -n -q -C $sha1 &&
514 pick_one -n $sha1 &&
Chris Webbdf5df202012-06-26 22:55:23 +0100515 git commit --allow-empty --allow-empty-message \
Vasco Almeida9588c522016-06-17 20:21:03 +0000516 --amend --no-post-rewrite -n -q -C $sha1 \
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000517 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000518 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
Chris Webbdf5df202012-06-26 22:55:23 +0100519 else
Vasco Almeida9588c522016-06-17 20:21:03 +0000520 pick_one $sha1 ||
521 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
Chris Webbdf5df202012-06-26 22:55:23 +0100522 fi
523}
524
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100525do_next () {
Matthieu Moyb12d3e92015-05-22 13:15:49 +0000526 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500527 read -r command sha1 rest < "$todo"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100528 case "$command" in
Galan Rémic9266d52015-06-29 22:20:30 +0200529 "$comment_char"*|''|noop|drop|d)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100530 mark_action_done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100531 ;;
Junio C Hamano39743cf2015-10-28 15:54:48 +0100532 "$cr")
533 # Work around CR left by "read" (e.g. with Git for Windows' Bash).
534 mark_action_done
535 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100536 pick|p)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100537 comment_for_reflog pick
538
539 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100540 do_pick $sha1 "$rest"
Thomas Rastb079fee2010-03-12 18:04:30 +0100541 record_in_rewritten $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100542 ;;
Björn Gustavsson6741aa62009-10-07 08:13:23 +0200543 reword|r)
544 comment_for_reflog reword
545
546 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100547 do_pick $sha1 "$rest"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000548 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
Vasco Almeida9588c522016-06-17 20:21:03 +0000549 warn "$(eval_gettext "\
550Could not amend commit after successfully picking \$sha1... \$rest
551This is most likely due to an empty commit message, or the pre-commit hook
552failed. If the pre-commit hook failed, you may need to resolve the issue before
553you are able to reword the commit.")"
Andrew Wong0becb3e2011-11-30 10:52:51 -0500554 exit_with_patch $sha1 1
555 }
Thomas Rastb079fee2010-03-12 18:04:30 +0100556 record_in_rewritten $sha1
Björn Gustavsson6741aa62009-10-07 08:13:23 +0200557 ;;
Johannes Schindelinf8babc42007-09-29 03:32:11 +0100558 edit|e)
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100559 comment_for_reflog edit
560
561 mark_action_done
Chris Webbdf5df202012-06-26 22:55:23 +0100562 do_pick $sha1 "$rest"
Ralf Thielow14c793e2016-03-28 18:00:00 +0200563 sha1_abbrev=$(git rev-parse --short $sha1)
Vasco Almeida9588c522016-06-17 20:21:03 +0000564 warn "$(eval_gettext "Stopped at \$sha1_abbrev... \$rest")"
Andrew Wong0becb3e2011-11-30 10:52:51 -0500565 exit_with_patch $sha1 0
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100566 ;;
Michael Haggerty0205e722009-12-07 10:20:59 +0100567 squash|s|fixup|f)
568 case "$command" in
569 squash|s)
570 squash_style=squash
571 ;;
572 fixup|f)
573 squash_style=fixup
574 ;;
575 esac
576 comment_for_reflog $squash_style
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100577
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500578 test -f "$done" && has_action "$done" ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000579 die "$(eval_gettext "Cannot '\$squash_style' without a previous commit")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100580
581 mark_action_done
Michael Haggertya25eb132010-01-14 06:54:55 +0100582 update_squash_messages $squash_style $sha1
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500583 author_script_content=$(get_author_ident_from_commit HEAD)
584 echo "$author_script_content" > "$author_script"
585 eval "$author_script_content"
Chris Webb2147f842012-07-24 13:17:03 +0100586 if ! pick_one -n $sha1
587 then
588 git rev-parse --verify HEAD >"$amend"
589 die_failed_squash $sha1 "$rest"
590 fi
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100591 case "$(peek_next_command)" in
Michael Haggerty0205e722009-12-07 10:20:59 +0100592 squash|s|fixup|f)
Michael Haggertya25eb132010-01-14 06:54:55 +0100593 # This is an intermediate commit; its message will only be
594 # used in case of trouble. So use the long version:
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000595 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
596 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100597 die_failed_squash $sha1 "$rest"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100598 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100599 *)
Michael Haggertya25eb132010-01-14 06:54:55 +0100600 # This is the final command of this squash/fixup group
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500601 if test -f "$fixup_msg"
Michael Haggertya25eb132010-01-14 06:54:55 +0100602 then
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000603 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
604 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100605 die_failed_squash $sha1 "$rest"
606 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500607 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100608 rm -f "$GIT_DIR"/MERGE_MSG
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000609 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
610 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Michael Haggerty6bdcd0d2010-01-14 06:54:57 +0100611 die_failed_squash $sha1 "$rest"
Michael Haggertya25eb132010-01-14 06:54:55 +0100612 fi
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500613 rm -f "$squash_msg" "$fixup_msg"
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100614 ;;
Johannes Schindelin6368f3f2007-07-21 18:09:41 +0100615 esac
Thomas Rastb079fee2010-03-12 18:04:30 +0100616 record_in_rewritten $sha1
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100617 ;;
Matthieu Moycd035b12010-08-10 17:17:51 +0200618 x|"exec")
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500619 read -r command rest < "$todo"
Matthieu Moycd035b12010-08-10 17:17:51 +0200620 mark_action_done
Vasco Almeida9588c522016-06-17 20:21:03 +0000621 eval_gettextln "Executing: \$rest"
Fredrik Medleyfe9394a2015-11-13 07:03:19 +0100622 "${SHELL:-@SHELL_PATH@}" -c "$rest" # Actual execution
Matthieu Moycd035b12010-08-10 17:17:51 +0200623 status=$?
Junio C Hamano16865192011-08-24 11:54:50 -0700624 # Run in subshell because require_clean_work_tree can die.
625 dirty=f
626 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
Matthieu Moycd035b12010-08-10 17:17:51 +0200627 if test "$status" -ne 0
628 then
Vasco Almeida9588c522016-06-17 20:21:03 +0000629 warn "$(eval_gettext "Execution failed: \$rest")"
Junio C Hamano16865192011-08-24 11:54:50 -0700630 test "$dirty" = f ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000631 warn "$(gettext "and made changes to the index and/or the working tree")"
Junio C Hamano16865192011-08-24 11:54:50 -0700632
Vasco Almeida9588c522016-06-17 20:21:03 +0000633 warn "$(gettext "\
634You can fix the problem, and then run
635
636 git rebase --continue")"
Matthieu Moycd035b12010-08-10 17:17:51 +0200637 warn
Johannes Sixtecfe1ea2012-09-18 13:15:26 +0200638 if test $status -eq 127 # command not found
639 then
640 status=1
641 fi
Matthieu Moycd035b12010-08-10 17:17:51 +0200642 exit "$status"
Junio C Hamano16865192011-08-24 11:54:50 -0700643 elif test "$dirty" = t
Matthieu Moycd035b12010-08-10 17:17:51 +0200644 then
Vasco Almeida9588c522016-06-17 20:21:03 +0000645 # TRANSLATORS: after these lines is a command to be issued by the user
646 warn "$(eval_gettext "\
647Execution succeeded: \$rest
648but left changes to the index and/or the working tree
649Commit or stash your changes, and then run
650
651 git rebase --continue")"
Matthieu Moycd035b12010-08-10 17:17:51 +0200652 warn
653 exit 1
654 fi
655 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100656 *)
Vasco Almeida9588c522016-06-17 20:21:03 +0000657 warn "$(eval_gettext "Unknown command: \$command \$sha1 \$rest")"
658 fixtodo="$(gettext "Please fix this using 'git rebase --edit-todo'.")"
Jan Krügerf1be3162009-10-27 15:58:14 +0100659 if git rev-parse --verify -q "$sha1" >/dev/null
660 then
Johannes Sixt9c8e1012012-09-19 08:43:54 +0200661 die_with_patch $sha1 "$fixtodo"
Jan Krügerf1be3162009-10-27 15:58:14 +0100662 else
Johannes Sixt9c8e1012012-09-19 08:43:54 +0200663 die "$fixtodo"
Jan Krügerf1be3162009-10-27 15:58:14 +0100664 fi
Johannes Schindelin376ccb82007-09-25 16:42:51 +0100665 ;;
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100666 esac
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500667 test -s "$todo" && return
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100668
Johannes Schindelin68a163c2007-06-25 18:58:28 +0100669 comment_for_reflog finish &&
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500670 newhead=$(git rev-parse HEAD) &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500671 case $head_name in
Johannes Schindelin73697a02007-09-25 16:43:15 +0100672 refs/*)
Michael J Gruber1af221e2012-08-10 08:51:19 +0200673 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500674 git update-ref -m "$message" $head_name $newhead $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400675 git symbolic-ref \
676 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
677 HEAD $head_name
Johannes Schindelin73697a02007-09-25 16:43:15 +0100678 ;;
679 esac && {
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -0500680 test ! -f "$state_dir"/verbose ||
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500681 git diff-tree --stat $orig_head..HEAD
Johannes Schindelin3df0a852007-07-08 03:02:13 +0100682 } &&
Thomas Rasteb2151b2010-03-12 18:04:33 +0100683 {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500684 test -s "$rewritten_list" &&
685 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
Thomas Rasteb2151b2010-03-12 18:04:33 +0100686 true # we don't care if this copying failed
687 } &&
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700688 hook="$(git rev-parse --git-path hooks/post-rewrite)"
689 if test -x "$hook" && test -s "$rewritten_list"; then
690 "$hook" rebase < "$rewritten_list"
Thomas Rastb079fee2010-03-12 18:04:30 +0100691 true # we don't care if this hook failed
692 fi &&
Vasco Almeida9588c522016-06-17 20:21:03 +0000693 warn "$(eval_gettext "Successfully rebased and updated \$head_name.")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100694
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530695 return 1 # not failure; just to break the do_rest loop
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100696}
697
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530698# can only return 0, when the infinite loop breaks
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100699do_rest () {
700 while :
701 do
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +0530702 do_next || break
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100703 done
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100704}
705
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100706# skip picking commits whose parents are unchanged
707skip_unnecessary_picks () {
708 fd=3
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500709 while read -r command rest
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100710 do
711 # fd=3 means we skip the command
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500712 case "$fd,$command" in
713 3,pick|3,p)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500714 # pick a commit whose parent is current $onto -> skip
Chris Johnsen14d87292010-09-17 16:42:51 -0500715 sha1=${rest%% *}
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500716 case "$(git rev-parse --verify --quiet "$sha1"^)" in
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500717 "$onto"*)
718 onto=$sha1
Brandon Casey2d6ca6e2010-08-13 15:47:34 -0500719 ;;
720 *)
721 fd=1
722 ;;
723 esac
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100724 ;;
Eric Sunshine7bca7af2013-08-16 17:44:07 -0400725 3,"$comment_char"*|3,)
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100726 # copy comments
727 ;;
728 *)
729 fd=1
730 ;;
731 esac
Brandon Caseyd1c3b102010-08-13 15:47:35 -0500732 printf '%s\n' "$command${rest:+ }$rest" >&$fd
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500733 done <"$todo" >"$todo.new" 3>>"$done" &&
734 mv -f "$todo".new "$todo" &&
Thomas Rastb079fee2010-03-12 18:04:30 +0100735 case "$(peek_next_command)" in
736 squash|s|fixup|f)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500737 record_in_rewritten "$onto"
Thomas Rastb079fee2010-03-12 18:04:30 +0100738 ;;
739 esac ||
Vasco Almeida9588c522016-06-17 20:21:03 +0000740 die "$(gettext "Could not skip unnecessary pick commands")"
Johannes Schindelin0e757e32009-03-03 10:55:31 +0100741}
742
Junio C Hamano75c69762013-08-23 20:10:42 -0400743transform_todo_ids () {
744 while read -r command rest
745 do
746 case "$command" in
747 "$comment_char"* | exec)
748 # Be careful for oddball commands like 'exec'
749 # that do not have a SHA-1 at the beginning of $rest.
750 ;;
751 *)
Matthieu Moy31bff642015-10-01 10:18:41 +0200752 sha1=$(git rev-parse --verify --quiet "$@" ${rest%%[ ]*}) &&
753 rest="$sha1 ${rest#*[ ]}"
Junio C Hamano75c69762013-08-23 20:10:42 -0400754 ;;
755 esac
756 printf '%s\n' "$command${rest:+ }$rest"
757 done <"$todo" >"$todo.new" &&
758 mv -f "$todo.new" "$todo"
759}
760
761expand_todo_ids() {
762 transform_todo_ids
763}
764
765collapse_todo_ids() {
Kirill A. Shutemov56895032013-09-28 18:53:05 +0300766 transform_todo_ids --short
Junio C Hamano75c69762013-08-23 20:10:42 -0400767}
768
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900769# Rearrange the todo list that has both "pick sha1 msg" and
770# "pick sha1 fixup!/squash! msg" appears in it so that the latter
771# comes immediately after the former, and change "pick" to
772# "fixup"/"squash".
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400773#
774# Note that if the config has specified a custom instruction format
775# each log message will be re-retrieved in order to normalize the
776# autosquash arrangement
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900777rearrange_squash () {
Kevin Ballard68d5d032010-11-04 15:36:32 -0700778 # extract fixup!/squash! lines and resolve any referenced sha1's
779 while read -r pick sha1 message
780 do
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400781 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
Kevin Ballard68d5d032010-11-04 15:36:32 -0700782 case "$message" in
783 "squash! "*|"fixup! "*)
784 action="${message%%!*}"
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700785 rest=$message
786 prefix=
787 # skip all squash! or fixup! (but save for later)
788 while :
789 do
790 case "$rest" in
791 "squash! "*|"fixup! "*)
792 prefix="$prefix${rest%%!*},"
793 rest="${rest#*! }"
794 ;;
795 *)
796 break
797 ;;
798 esac
799 done
Uwe Storbeck47be0662014-03-15 00:56:43 +0100800 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
Kevin Ballard68d5d032010-11-04 15:36:32 -0700801 # if it's a single word, try to resolve to a full sha1 and
802 # emit a second copy. This allows us to match on both message
803 # and on sha1 prefix
804 if test "${rest#* }" = "$rest"; then
805 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
806 if test -n "$fullsha"; then
807 # prefix the action to uniquely identify this line as
808 # intended for full sha1 match
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700809 echo "$sha1 +$action $prefix $fullsha"
Kevin Ballard68d5d032010-11-04 15:36:32 -0700810 fi
811 fi
812 esac
813 done >"$1.sq" <"$1"
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900814 test -s "$1.sq" || return
815
816 used=
Junio C Hamano57f2b6b2010-07-05 23:08:36 -0700817 while read -r pick sha1 message
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900818 do
819 case " $used" in
820 *" $sha1 "*) continue ;;
821 esac
Brandon Casey938791c2010-07-22 14:15:11 -0500822 printf '%s\n' "$pick $sha1 $message"
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400823 test -z "${format}" || message=$(git log -n 1 --format="%s" ${sha1})
Kevin Ballardd3d7a422010-11-04 15:36:31 -0700824 used="$used$sha1 "
Andrew Pimlott22c5b132013-06-27 12:26:31 -0700825 while read -r squash action msg_prefix msg_content
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900826 do
Kevin Ballardd3d7a422010-11-04 15:36:31 -0700827 case " $used" in
828 *" $squash "*) continue ;;
829 esac
Kevin Ballard68d5d032010-11-04 15:36:32 -0700830 emit=0
831 case "$action" in
832 +*)
833 action="${action#+}"
834 # full sha1 prefix test
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500835 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
Kevin Ballard68d5d032010-11-04 15:36:32 -0700836 *)
837 # message prefix test
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500838 case "$message" in "$msg_content"*) emit=1;; esac ;;
Kevin Ballard68d5d032010-11-04 15:36:32 -0700839 esac
840 if test $emit = 1; then
Michael Rappazzo16cf51c2015-06-13 12:26:58 -0400841 if test -n "${format}"
842 then
843 msg_content=$(git log -n 1 --format="${format}" ${squash})
844 else
845 msg_content="$(echo "$msg_prefix" | sed "s/,/! /g")$msg_content"
846 fi
847 printf '%s\n' "$action $squash $msg_content"
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900848 used="$used$squash "
Kevin Ballard68d5d032010-11-04 15:36:32 -0700849 fi
Nanako Shiraishif59baa52009-12-08 12:13:14 +0900850 done <"$1.sq"
851 done >"$1.rearranged" <"$1"
852 cat "$1.rearranged" >"$1"
853 rm -f "$1.sq" "$1.rearranged"
854}
855
Lucien Kongc2145382012-06-12 10:05:12 +0200856# Add commands after a pick or after a squash/fixup serie
857# in the todo list.
858add_exec_commands () {
859 {
860 first=t
861 while read -r insn rest
862 do
863 case $insn in
864 pick)
865 test -n "$first" ||
866 printf "%s" "$cmd"
867 ;;
868 esac
869 printf "%s %s\n" "$insn" "$rest"
870 first=
871 done
872 printf "%s" "$cmd"
873 } <"$1" >"$1.new" &&
874 mv "$1.new" "$1"
875}
876
Galan Rémi804098b2015-06-29 22:20:32 +0200877# Check if the SHA-1 passed as an argument is a
878# correct one, if not then print $2 in "$todo".badsha
879# $1: the SHA-1 to test
Matthieu Moy1db168e2015-10-01 10:18:42 +0200880# $2: the line number of the input
881# $3: the input filename
Galan Rémi804098b2015-06-29 22:20:32 +0200882check_commit_sha () {
883 badsha=0
Jeff King268ef4d2016-05-13 16:47:33 -0400884 if test -z "$1"
Galan Rémi804098b2015-06-29 22:20:32 +0200885 then
886 badsha=1
887 else
888 sha1_verif="$(git rev-parse --verify --quiet $1^{commit})"
Jeff King268ef4d2016-05-13 16:47:33 -0400889 if test -z "$sha1_verif"
Galan Rémi804098b2015-06-29 22:20:32 +0200890 then
891 badsha=1
892 fi
893 fi
894
895 if test $badsha -ne 0
896 then
Matthieu Moy1db168e2015-10-01 10:18:42 +0200897 line="$(sed -n -e "${2}p" "$3")"
Vasco Almeida9588c522016-06-17 20:21:03 +0000898 warn "$(eval_gettext "\
899Warning: the SHA-1 is missing or isn't a commit in the following line:
900 - \$line")"
Galan Rémi804098b2015-06-29 22:20:32 +0200901 warn
902 fi
903
904 return $badsha
905}
906
907# prints the bad commits and bad commands
908# from the todolist in stdin
909check_bad_cmd_and_sha () {
910 retval=0
Matthieu Moy1db168e2015-10-01 10:18:42 +0200911 lineno=0
912 while read -r command rest
913 do
914 lineno=$(( $lineno + 1 ))
915 case $command in
916 "$comment_char"*|''|noop|x|exec)
917 # Doesn't expect a SHA-1
918 ;;
Junio C Hamano39743cf2015-10-28 15:54:48 +0100919 "$cr")
920 # Work around CR left by "read" (e.g. with Git for
921 # Windows' Bash).
922 ;;
Matthieu Moy1db168e2015-10-01 10:18:42 +0200923 pick|p|drop|d|reword|r|edit|e|squash|s|fixup|f)
924 if ! check_commit_sha "${rest%%[ ]*}" "$lineno" "$1"
925 then
Galan Rémi804098b2015-06-29 22:20:32 +0200926 retval=1
Matthieu Moy1db168e2015-10-01 10:18:42 +0200927 fi
928 ;;
929 *)
930 line="$(sed -n -e "${lineno}p" "$1")"
Vasco Almeida9588c522016-06-17 20:21:03 +0000931 warn "$(eval_gettext "\
932Warning: the command isn't recognized in the following line:
933 - \$line")"
Matthieu Moy1db168e2015-10-01 10:18:42 +0200934 warn
935 retval=1
936 ;;
937 esac
938 done <"$1"
939 return $retval
Galan Rémi804098b2015-06-29 22:20:32 +0200940}
941
Galan Rémi37079952015-06-29 22:20:31 +0200942# Print the list of the SHA-1 of the commits
943# from stdin to stdout
944todo_list_to_sha_list () {
945 git stripspace --strip-comments |
946 while read -r command sha1 rest
947 do
948 case $command in
949 "$comment_char"*|''|noop|x|"exec")
950 ;;
951 *)
952 long_sha=$(git rev-list --no-walk "$sha1" 2>/dev/null)
953 printf "%s\n" "$long_sha"
954 ;;
955 esac
956 done
957}
958
959# Use warn for each line in stdin
960warn_lines () {
961 while read -r line
962 do
963 warn " - $line"
964 done
965}
966
967# Switch to the branch in $into and notify it in the reflog
968checkout_onto () {
969 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
Vasco Almeida9588c522016-06-17 20:21:03 +0000970 output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
Galan Rémi37079952015-06-29 22:20:31 +0200971 git update-ref ORIG_HEAD $orig_head
972}
973
974get_missing_commit_check_level () {
975 check_level=$(git config --get rebase.missingCommitsCheck)
976 check_level=${check_level:-ignore}
977 # Don't be case sensitive
978 printf '%s' "$check_level" | tr 'A-Z' 'a-z'
979}
980
981# Check if the user dropped some commits by mistake
982# Behaviour determined by rebase.missingCommitsCheck.
Galan Rémi804098b2015-06-29 22:20:32 +0200983# Check if there is an unrecognized command or a
984# bad SHA-1 in a command.
Galan Rémi37079952015-06-29 22:20:31 +0200985check_todo_list () {
986 raise_error=f
987
988 check_level=$(get_missing_commit_check_level)
989
990 case "$check_level" in
991 warn|error)
992 # Get the SHA-1 of the commits
993 todo_list_to_sha_list <"$todo".backup >"$todo".oldsha1
994 todo_list_to_sha_list <"$todo" >"$todo".newsha1
995
996 # Sort the SHA-1 and compare them
997 sort -u "$todo".oldsha1 >"$todo".oldsha1+
998 mv "$todo".oldsha1+ "$todo".oldsha1
999 sort -u "$todo".newsha1 >"$todo".newsha1+
1000 mv "$todo".newsha1+ "$todo".newsha1
1001 comm -2 -3 "$todo".oldsha1 "$todo".newsha1 >"$todo".miss
1002
1003 # Warn about missing commits
1004 if test -s "$todo".miss
1005 then
1006 test "$check_level" = error && raise_error=t
1007
Vasco Almeida9588c522016-06-17 20:21:03 +00001008 warn "$(gettext "\
1009Warning: some commits may have been dropped accidentally.
1010Dropped commits (newer to older):")"
Galan Rémi37079952015-06-29 22:20:31 +02001011
1012 # Make the list user-friendly and display
1013 opt="--no-walk=sorted --format=oneline --abbrev-commit --stdin"
1014 git rev-list $opt <"$todo".miss | warn_lines
1015
Vasco Almeida9588c522016-06-17 20:21:03 +00001016 warn "$(gettext "\
1017To avoid this message, use \"drop\" to explicitly remove a commit.
1018
1019Use 'git config rebase.missingCommitsCheck' to change the level of warnings.
1020The possible behaviours are: ignore, warn, error.")"
Galan Rémi37079952015-06-29 22:20:31 +02001021 warn
1022 fi
1023 ;;
1024 ignore)
1025 ;;
1026 *)
Vasco Almeida9588c522016-06-17 20:21:03 +00001027 warn "$(eval_gettext "Unrecognized setting \$check_level for option rebase.missingCommitsCheck. Ignoring.")"
Galan Rémi37079952015-06-29 22:20:31 +02001028 ;;
1029 esac
1030
Matthieu Moy1db168e2015-10-01 10:18:42 +02001031 if ! check_bad_cmd_and_sha "$todo"
Galan Rémi804098b2015-06-29 22:20:32 +02001032 then
1033 raise_error=t
1034 fi
1035
Galan Rémi37079952015-06-29 22:20:31 +02001036 if test $raise_error = t
1037 then
1038 # Checkout before the first commit of the
1039 # rebase: this way git rebase --continue
1040 # will work correctly as it expects HEAD to be
1041 # placed before the commit of the next action
1042 checkout_onto
1043
Vasco Almeida9588c522016-06-17 20:21:03 +00001044 warn "$(gettext "You can fix this with 'git rebase --edit-todo'.")"
1045 die "$(gettext "Or you can abort the rebase with 'git rebase --abort'.")"
Galan Rémi37079952015-06-29 22:20:31 +02001046 fi
1047}
1048
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001049# The whole contents of this file is run by dot-sourcing it from
1050# inside a shell function. It used to be that "return"s we see
1051# below were not inside any function, and expected to return
1052# to the function that dot-sourced us.
1053#
Ed Maste9b35cad2016-06-17 11:33:29 -04001054# However, older (9.x) versions of FreeBSD /bin/sh misbehave on such a
1055# construct and continue to run the statements that follow such a "return".
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001056# As a work-around, we introduce an extra layer of a function
1057# here, and immediately call it after defining it.
1058git_rebase__interactive () {
1059
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001060case "$action" in
1061continue)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001062 # do we have anything to commit?
John Keepinga6754cd2012-04-07 11:20:53 +01001063 if git diff-index --cached --quiet HEAD --
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001064 then
Johannes Schindelin0e0aff42015-06-18 18:38:53 +02001065 # Nothing to commit -- skip this commit
1066
1067 test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
1068 rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001069 die "$(gettext "Could not remove CHERRY_PICK_HEAD")"
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001070 else
Matthieu Moyffaaed82011-08-24 16:01:48 +02001071 if ! test -f "$author_script"
1072 then
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001073 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
Vasco Almeida9588c522016-06-17 20:21:03 +00001074 die "$(eval_gettext "\
1075You have staged changes in your working tree.
1076If these changes are meant to be
Matthieu Moyffaaed82011-08-24 16:01:48 +02001077squashed into the previous commit, run:
1078
Vasco Almeida9588c522016-06-17 20:21:03 +00001079 git commit --amend \$gpg_sign_opt_quoted
Matthieu Moyffaaed82011-08-24 16:01:48 +02001080
1081If they are meant to go into a new commit, run:
1082
Vasco Almeida9588c522016-06-17 20:21:03 +00001083 git commit \$gpg_sign_opt_quoted
Matthieu Moyffaaed82011-08-24 16:01:48 +02001084
1085In both case, once you're done, continue with:
1086
1087 git rebase --continue
Vasco Almeida9588c522016-06-17 20:21:03 +00001088")"
Matthieu Moyffaaed82011-08-24 16:01:48 +02001089 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001090 . "$author_script" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001091 die "$(gettext "Error trying to find the author identity to amend commit")"
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001092 if test -f "$amend"
Shawn O. Pearce03270622007-12-20 02:12:12 -05001093 then
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001094 current_head=$(git rev-parse --verify HEAD)
1095 test "$current_head" = $(cat "$amend") ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001096 die "$(gettext "\
1097You have uncommitted changes in your working tree. Please commit them
1098first and then run 'git rebase --continue' again.")"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001099 do_with_author git commit --amend --no-verify -F "$msg" -e \
1100 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001101 die "$(gettext "Could not commit staged changes.")"
Chris Webb2147f842012-07-24 13:17:03 +01001102 else
Nicolas Vigier3ee5e542014-02-10 01:03:37 +00001103 do_with_author git commit --no-verify -F "$msg" -e \
1104 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001105 die "$(gettext "Could not commit staged changes.")"
Shawn O. Pearce03270622007-12-20 02:12:12 -05001106 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001107 fi
Johannes Schindelin18640d92007-07-08 03:01:29 +01001108
Matthieu Moyb12d3e92015-05-22 13:15:49 +00001109 if test -r "$state_dir"/stopped-sha
1110 then
1111 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
1112 fi
Thomas Rast0acb62f2010-03-28 21:36:00 +02001113
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001114 require_clean_work_tree "rebase"
1115 do_rest
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +05301116 return 0
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001117 ;;
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001118skip)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001119 git rerere clear
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001120
Martin von Zweigbergk2959c282011-02-06 13:43:52 -05001121 do_rest
Ramkumar Ramachandra15d4bf22013-05-12 17:26:39 +05301122 return 0
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001123 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001124edit-todo)
John Keeping180bad32013-02-11 23:08:04 +00001125 git stripspace --strip-comments <"$todo" >"$todo".new
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001126 mv -f "$todo".new "$todo"
Junio C Hamano75c69762013-08-23 20:10:42 -04001127 collapse_todo_ids
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001128 append_todo_help
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001129 gettext "
John Keeping180bad32013-02-11 23:08:04 +00001130You are editing the todo file of an ongoing interactive rebase.
1131To continue rebase after editing, run:
1132 git rebase --continue
1133
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001134" | git stripspace --comment-lines >>"$todo"
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001135
1136 git_sequence_editor "$todo" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001137 die "$(gettext "Could not execute editor")"
Junio C Hamano75c69762013-08-23 20:10:42 -04001138 expand_todo_ids
Andrew Wongeb9a7cb2012-09-17 21:28:09 -04001139
1140 exit
1141 ;;
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -05001142esac
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001143
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001144git var GIT_COMMITTER_IDENT >/dev/null ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001145 die "$(gettext "You need to set your committer info first")"
Thomas Rastd8fab022009-01-02 23:28:26 +01001146
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001147comment_for_reflog start
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001148
Martin von Zweigbergk71786f52011-02-06 13:43:42 -05001149if test ! -z "$switch_to"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001150then
Ramkumar Ramachandra26cd1602013-06-16 14:15:13 +05301151 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
Martin von Zweigbergk71786f52011-02-06 13:43:42 -05001152 output git checkout "$switch_to" -- ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001153 die "$(eval_gettext "Could not checkout \$switch_to")"
Ramkumar Ramachandra26cd1602013-06-16 14:15:13 +05301154
1155 comment_for_reflog start
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001156fi
Matt Kraai51668102007-09-25 18:30:13 -07001157
Vasco Almeida9588c522016-06-17 20:21:03 +00001158orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
1159mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001160
Vasco Almeida9588c522016-06-17 20:21:03 +00001161: > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
Martin von Zweigbergk84df4562011-02-06 13:43:53 -05001162write_basic_state
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001163if test t = "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001164then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001165 if test -z "$rebase_root"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001166 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001167 mkdir "$rewritten" &&
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001168 for c in $(git merge-base --all $orig_head $upstream)
Stephen Habermanacc85592008-10-15 02:44:38 -05001169 do
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001170 echo $onto > "$rewritten"/$c ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001171 die "$(gettext "Could not init rewritten commits")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001172 done
1173 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001174 mkdir "$rewritten" &&
1175 echo $onto > "$rewritten"/root ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001176 die "$(gettext "Could not init rewritten commits")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001177 fi
1178 # No cherry-pick because our first pass is to determine
1179 # parents to rewrite and skipping dropped commits would
1180 # prematurely end our probe
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001181 merges_option=
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001182else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001183 merges_option="--no-merges --cherry-pick"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001184fi
1185
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001186shorthead=$(git rev-parse --short $orig_head)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001187shortonto=$(git rev-parse --short $onto)
1188if test -z "$rebase_root"
1189 # this is now equivalent to ! -z "$upstream"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001190then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001191 shortupstream=$(git rev-parse --short $upstream)
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001192 revisions=$upstream...$orig_head
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001193 shortrevisions=$shortupstream..$shorthead
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001194else
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001195 revisions=$onto...$orig_head
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001196 shortrevisions=$shorthead
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001197fi
Michael Rappazzo16cf51c2015-06-13 12:26:58 -04001198format=$(git config --get rebase.instructionFormat)
1199# the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
1200git rev-list $merges_option --format="%m%H ${format:-%s}" \
1201 --reverse --left-right --topo-order \
John Keeping1e0dacd2014-07-16 20:23:49 +01001202 $revisions ${restrict_revision+^$restrict_revision} | \
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001203 sed -n "s/^>//p" |
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001204while read -r sha1 rest
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001205do
Neil Horman90e18182012-04-20 10:36:17 -04001206
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001207 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
Neil Horman90e18182012-04-20 10:36:17 -04001208 then
John Keeping180bad32013-02-11 23:08:04 +00001209 comment_out="$comment_char "
Neil Horman90e18182012-04-20 10:36:17 -04001210 else
1211 comment_out=
1212 fi
1213
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001214 if test t != "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001215 then
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001216 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001217 else
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001218 if test -z "$rebase_root"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001219 then
1220 preserve=t
1221 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1222 do
Andrew Wong12bf8282011-06-18 18:12:01 -04001223 if test -f "$rewritten"/$p
Thomas Rastd911d142009-01-02 23:28:28 +01001224 then
Thomas Rastd911d142009-01-02 23:28:28 +01001225 preserve=f
1226 fi
Stephen Habermanacc85592008-10-15 02:44:38 -05001227 done
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001228 else
1229 preserve=f
Stephen Habermanacc85592008-10-15 02:44:38 -05001230 fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001231 if test f = "$preserve"
1232 then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001233 touch "$rewritten"/$sha1
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001234 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001235 fi
1236 fi
1237done
Thomas Rastd911d142009-01-02 23:28:28 +01001238
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001239# Watch for commits that been dropped by --cherry-pick
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001240if test t = "$preserve_merges"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001241then
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001242 mkdir "$dropped"
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001243 # Save all non-cherry-picked changes
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001244 git rev-list $revisions --left-right --cherry-pick | \
Martin von Zweigbergk431b7e72011-02-06 13:43:50 -05001245 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001246 # Now all commits and note which ones are missing in
1247 # not-cherry-picks and hence being dropped
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001248 git rev-list $revisions |
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001249 while read rev
1250 do
Junio C Hamano214123c2016-03-08 15:51:36 -08001251 if test -f "$rewritten"/$rev &&
1252 ! sane_grep "$rev" "$state_dir"/not-cherry-picks >/dev/null
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001253 then
1254 # Use -f2 because if rev-list is telling us this commit is
1255 # not worthwhile, we don't want to track its multiple heads,
1256 # just the history of its first-parent for others that will
1257 # be rebasing on top of it
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001258 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001259 sha1=$(git rev-list -1 $rev)
1260 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001261 rm "$rewritten"/$rev
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001262 fi
1263 done
1264fi
1265
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001266test -s "$todo" || echo noop >> "$todo"
1267test -n "$autosquash" && rearrange_squash "$todo"
Lucien Kongc2145382012-06-12 10:05:12 +02001268test -n "$cmd" && add_exec_commands "$todo"
1269
Onno Kortmann97f05f42014-12-10 19:16:44 +01001270todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
Eric Sunshine28c8cfc2015-03-04 02:53:04 -05001271todocount=${todocount##* }
Onno Kortmann97f05f42014-12-10 19:16:44 +01001272
John Keeping180bad32013-02-11 23:08:04 +00001273cat >>"$todo" <<EOF
Johannes Schindelin6047a232007-11-22 12:30:10 +00001274
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001275$comment_char $(eval_ngettext \
1276 "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
1277 "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
1278 "$todocount")
Andrew Wongfcc5ef12012-09-17 21:28:08 -04001279EOF
1280append_todo_help
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001281gettext "
John Keeping180bad32013-02-11 23:08:04 +00001282However, if you remove everything, the rebase will be aborted.
1283
Vasco Almeidab8fc9e42016-06-17 20:21:04 +00001284" | git stripspace --comment-lines >>"$todo"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001285
Neil Horman90e18182012-04-20 10:36:17 -04001286if test -z "$keep_empty"
1287then
Vasco Almeida9588c522016-06-17 20:21:03 +00001288 printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
Neil Horman90e18182012-04-20 10:36:17 -04001289fi
1290
1291
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001292has_action "$todo" ||
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -04001293 return 2
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001294
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001295cp "$todo" "$todo".backup
Kirill A. Shutemovedb72d52015-01-22 13:50:15 +02001296collapse_todo_ids
Peter Oberndorfer821881d2011-10-17 22:26:23 +02001297git_sequence_editor "$todo" ||
Vasco Almeida9588c522016-06-17 20:21:03 +00001298 die_abort "$(gettext "Could not execute editor")"
Johannes Schindelin1b1dce42007-06-25 01:11:14 +01001299
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001300has_action "$todo" ||
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -04001301 return 2
Johannes Schindelinc54b7812007-06-25 18:56:55 +01001302
Galan Rémi37079952015-06-29 22:20:31 +02001303check_todo_list
1304
Junio C Hamano75c69762013-08-23 20:10:42 -04001305expand_todo_ids
1306
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -05001307test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
Johannes Schindelin0e757e32009-03-03 10:55:31 +01001308
Galan Rémi37079952015-06-29 22:20:31 +02001309checkout_onto
Martin von Zweigbergk34262322011-02-06 13:43:35 -05001310do_rest
Kyle J. McKay9f50d322014-04-11 01:28:17 -07001311
1312}
1313# ... and then we call the whole thing.
1314git_rebase__interactive