]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase--interactive.sh
Merge git://repo.or.cz/git-gui
[thirdparty/git.git] / git-rebase--interactive.sh
CommitLineData
1b1dce4b
JS
1#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4
5# SHORT DESCRIPTION
6#
7# This script makes it easy to fix up commits in the middle of a series,
8# and rearrange commits.
9#
10# The original idea comes from Eric W. Biederman, in
11# http://article.gmane.org/gmane.comp.version-control.git/22407
572a7c52 12#
80883bb3
MH
13# The file containing rebase commands, comments, and empty lines.
14# This file is created by "git rebase -i" then edited by the user. As
15# the lines are processed, they are removed from the front of this
6bb4e485 16# file and written to the tail of $done.
431b7e78 17todo="$state_dir"/git-rebase-todo
80883bb3
MH
18
19# The rebase command lines that have already been processed. A line
20# is moved here when it is first handled, before any associated user
21# actions.
431b7e78 22done="$state_dir"/done
80883bb3
MH
23
24# The commit message that is planned to be used for any changes that
25# need to be committed following a user interaction.
431b7e78 26msg="$state_dir"/message
80883bb3
MH
27
28# The file into which is accumulated the suggested commit message for
29# squash/fixup commands. When the first of a series of squash/fixups
30# is seen, the file is created and the commit message from the
31# previous commit and from the first squash/fixup commit are written
32# to it. The commit message for each subsequent squash/fixup commit
33# is appended to the file as it is processed.
34#
35# The first line of the file is of the form
6bb4e485
MZ
36# # This is a combination of $count commits.
37# where $count is the number of commits whose messages have been
80883bb3
MH
38# written to the file so far (including the initial "pick" commit).
39# Each time that a commit message is processed, this line is read and
40# updated. It is deleted just before the combined commit is made.
431b7e78 41squash_msg="$state_dir"/message-squash
80883bb3 42
a25eb139
MH
43# If the current series of squash/fixups has not yet included a squash
44# command, then this file exists and holds the commit message of the
45# original "pick" commit. (If the series ends without a "squash"
46# command, then this can be used as the commit message of the combined
47# commit without opening the editor.)
431b7e78 48fixup_msg="$state_dir"/message-fixup
a25eb139 49
6bb4e485
MZ
50# $rewritten is the name of a directory containing files for each
51# commit that is reachable by at least one merge base of $head and
52# $upstream. They are not necessarily rewritten, but their children
80883bb3
MH
53# might be. This ensures that commits on merged, but otherwise
54# unrelated side branches are left alone. (Think "X" in the man page's
55# example.)
431b7e78 56rewritten="$state_dir"/rewritten
80883bb3 57
431b7e78 58dropped="$state_dir"/dropped
0aac0de4 59
b71dc3e1
ZK
60end="$state_dir"/end
61msgnum="$state_dir"/msgnum
62
0aac0de4
MH
63# A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
64# GIT_AUTHOR_DATE that will be used for the commit that is currently
65# being rebased.
431b7e78 66author_script="$state_dir"/author-script
0aac0de4 67
a4049ae7
MH
68# When an "edit" rebase command is being processed, the SHA1 of the
69# commit to be edited is recorded in this file. When "git rebase
70# --continue" is executed, if there are any staged changes then they
71# will be amended to the HEAD commit, but only provided the HEAD
72# commit is still the commit to be edited. When any other rebase
73# command is processed, this file is deleted.
431b7e78 74amend="$state_dir"/amend
a4049ae7 75
b079feed
TR
76# For the post-rewrite hook, we make a list of rewritten commits and
77# their new sha1s. The rewritten-pending list keeps the sha1s of
78# commits that have been processed, but not committed yet,
79# e.g. because they are waiting for a 'squash' command.
431b7e78
MZ
80rewritten_list="$state_dir"/rewritten-list
81rewritten_pending="$state_dir"/rewritten-pending
6bb4e485 82
db2b3b82
AF
83strategy_args=
84if test -n "$do_merge"
85then
86 strategy_args=${strategy:+--strategy=$strategy}
87 eval '
88 for strategy_opt in '"$strategy_opts"'
89 do
90 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
91 done
92 '
93fi
94
89c7ae9c 95GIT_CHERRY_PICK_HELP="$resolvemsg"
804c7174
WC
96export GIT_CHERRY_PICK_HELP
97
180bad3d
JK
98comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
99: ${comment_char:=#}
100
1b1dce4b 101warn () {
938791cd 102 printf '%s\n' "$*" >&2
1b1dce4b
JS
103}
104
ee0a4afb
MH
105# Output the commit message for the specified commit.
106commit_message () {
107 git cat-file commit "$1" | sed "1,/^$/d"
108}
109
6bb4e485 110orig_reflog_action="$GIT_REFLOG_ACTION"
1b1dce4b
JS
111
112comment_for_reflog () {
6bb4e485 113 case "$orig_reflog_action" in
1b1dce4b
JS
114 ''|rebase*)
115 GIT_REFLOG_ACTION="rebase -i ($1)"
116 export GIT_REFLOG_ACTION
376ccb8c 117 ;;
1b1dce4b
JS
118 esac
119}
120
4e673877 121last_count=
1b1dce4b 122mark_action_done () {
6bb4e485
MZ
123 sed -e 1q < "$todo" >> "$done"
124 sed -e 1d < "$todo" >> "$todo".new
125 mv -f "$todo".new "$todo"
180bad3d 126 new_count=$(git stripspace --strip-comments <"$done" | wc -l)
b71dc3e1 127 echo $new_count >"$msgnum"
180bad3d 128 total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
b71dc3e1 129 echo $total >"$end"
6bb4e485 130 if test "$last_count" != "$new_count"
4e673877 131 then
6bb4e485
MZ
132 last_count=$new_count
133 printf "Rebasing (%d/%d)\r" $new_count $total
134 test -z "$verbose" || echo
4e673877 135 fi
1b1dce4b
JS
136}
137
fcc5ef1c 138append_todo_help () {
180bad3d
JK
139 git stripspace --comment-lines >>"$todo" <<\EOF
140
141Commands:
142 p, pick = use commit
143 r, reword = use commit, but edit the commit message
144 e, edit = use commit, but stop for amending
145 s, squash = use commit, but meld into previous commit
146 f, fixup = like "squash", but discard this commit's log message
147 x, exec = run command (the rest of the line) using shell
148
149These lines can be re-ordered; they are executed from top to bottom.
150
151If you remove a line here THAT COMMIT WILL BE LOST.
fcc5ef1c
AW
152EOF
153}
154
1b1dce4b 155make_patch () {
4fb1a19d
JS
156 sha1_and_parents="$(git rev-list --parents -1 "$1")"
157 case "$sha1_and_parents" in
158 ?*' '?*' '?*)
159 git diff --cc $sha1_and_parents
160 ;;
161 ?*' '?*)
162 git diff-tree -p "$1^!"
163 ;;
164 *)
165 echo "Root commit"
166 ;;
431b7e78 167 esac > "$state_dir"/patch
6bb4e485
MZ
168 test -f "$msg" ||
169 commit_message "$1" > "$msg"
170 test -f "$author_script" ||
171 get_author_ident_from_commit "$1" > "$author_script"
1b1dce4b
JS
172}
173
174die_with_patch () {
431b7e78 175 echo "$1" > "$state_dir"/stopped-sha
1b1dce4b 176 make_patch "$1"
ecfe72ff 177 git rerere
1b1dce4b
JS
178 die "$2"
179}
180
0becb3e4
AW
181exit_with_patch () {
182 echo "$1" > "$state_dir"/stopped-sha
183 make_patch $1
184 git rev-parse --verify HEAD > "$amend"
185 warn "You can amend the commit now, with"
186 warn
187 warn " git commit --amend"
188 warn
189 warn "Once you are satisfied with your changes, run"
190 warn
191 warn " git rebase --continue"
192 warn
193 exit $2
194}
195
c54b7817 196die_abort () {
431b7e78 197 rm -rf "$state_dir"
c54b7817
JS
198 die "$1"
199}
200
376ccb8c 201has_action () {
180bad3d 202 test -n "$(git stripspace --strip-comments <"$1")"
376ccb8c
JS
203}
204
90e1818f
NH
205is_empty_commit() {
206 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
207 die "$1: not a commit that can be picked")
208 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
209 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
210 test "$tree" = "$ptree"
211}
212
98697784
PH
213is_merge_commit()
214{
215 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
216}
217
7756ecff
MH
218# Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
219# GIT_AUTHOR_DATE exported from the current environment.
220do_with_author () {
76c9c0db
JH
221 (
222 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
223 "$@"
224 )
7756ecff
MH
225}
226
821881d8
PO
227git_sequence_editor () {
228 if test -z "$GIT_SEQUENCE_EDITOR"
229 then
230 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
231 if [ -z "$GIT_SEQUENCE_EDITOR" ]
232 then
233 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
234 fi
235 fi
236
237 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
238}
239
1b1dce4b 240pick_one () {
8e75abfd 241 ff=--ff
90e1818f 242
8e75abfd 243 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
6bb4e485 244 case "$force_rebase" in '') ;; ?*) ff= ;; esac
dfa49f33 245 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
90e1818f
NH
246
247 if is_empty_commit "$sha1"
248 then
249 empty_args="--allow-empty"
250 fi
251
6bb4e485 252 test -d "$rewritten" &&
f09c9b8c 253 pick_one_preserving_merges "$@" && return
db2b3b82 254 output eval git cherry-pick "$strategy_args" $empty_args $ff "$@"
1b1dce4b
JS
255}
256
f09c9b8c 257pick_one_preserving_merges () {
71d9451e
TR
258 fast_forward=t
259 case "$1" in
260 -n)
261 fast_forward=f
262 sha1=$2
263 ;;
264 *)
265 sha1=$1
266 ;;
267 esac
f09c9b8c
JS
268 sha1=$(git rev-parse $sha1)
269
431b7e78 270 if test -f "$state_dir"/current-commit
f09c9b8c 271 then
4c1360f4 272 if test "$fast_forward" = t
bb645071 273 then
41f556b9 274 while read current_commit
bb645071 275 do
6bb4e485 276 git rev-parse HEAD > "$rewritten"/$current_commit
431b7e78
MZ
277 done <"$state_dir"/current-commit
278 rm "$state_dir"/current-commit ||
bb645071
SH
279 die "Cannot write current commit's replacement sha1"
280 fi
f09c9b8c
JS
281 fi
282
431b7e78 283 echo $sha1 >> "$state_dir"/current-commit
a96dc01e 284
f09c9b8c 285 # rewrite parents; if none were rewritten, we can fast-forward.
f09c9b8c 286 new_parents=
d911d146
TR
287 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
288 if test "$pend" = " "
289 then
290 pend=" root"
291 fi
faae853c 292 while [ "$pend" != "" ]
f09c9b8c 293 do
faae853c
SH
294 p=$(expr "$pend" : ' \([^ ]*\)')
295 pend="${pend# $p}"
296
6bb4e485 297 if test -f "$rewritten"/$p
f09c9b8c 298 then
6bb4e485 299 new_p=$(cat "$rewritten"/$p)
80fe82e4
SH
300
301 # If the todo reordered commits, and our parent is marked for
302 # rewriting, but hasn't been gotten to yet, assume the user meant to
303 # drop it on top of the current HEAD
304 if test -z "$new_p"
305 then
306 new_p=$(git rev-parse HEAD)
307 fi
308
f09c9b8c
JS
309 test $p != $new_p && fast_forward=f
310 case "$new_parents" in
311 *$new_p*)
312 ;; # do nothing; that parent is already there
313 *)
314 new_parents="$new_parents $new_p"
376ccb8c 315 ;;
f09c9b8c 316 esac
1c5fa0a1 317 else
6bb4e485 318 if test -f "$dropped"/$p
faae853c
SH
319 then
320 fast_forward=f
6bb4e485 321 replacement="$(cat "$dropped"/$p)"
d911d146
TR
322 test -z "$replacement" && replacement=root
323 pend=" $replacement$pend"
faae853c
SH
324 else
325 new_parents="$new_parents $p"
326 fi
f09c9b8c
JS
327 fi
328 done
329 case $fast_forward in
330 t)
a75d7b54 331 output warn "Fast-forward to $sha1"
71d9451e 332 output git reset --hard $sha1 ||
a75d7b54 333 die "Cannot fast-forward to $sha1"
f09c9b8c
JS
334 ;;
335 f)
376ccb8c 336 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
a4f25e36
SH
337
338 if [ "$1" != "-n" ]
339 then
340 # detach HEAD to current parent
341 output git checkout $first_parent 2> /dev/null ||
342 die "Cannot move HEAD to $first_parent"
343 fi
f09c9b8c 344
f09c9b8c 345 case "$new_parents" in
376ccb8c 346 ' '*' '*)
a4f25e36
SH
347 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
348
f09c9b8c 349 # redo merge
6bb4e485
MZ
350 author_script_content=$(get_author_ident_from_commit $sha1)
351 eval "$author_script_content"
352 msg_content="$(commit_message $sha1)"
f91333d6
BS
353 # No point in merging the first parent, that's HEAD
354 new_parents=${new_parents# $first_parent}
a9f739c1 355 merge_args="--no-log --no-ff"
db2b3b82 356 if ! do_with_author output eval \
a9f739c1 357 'git merge $merge_args $strategy_args -m "$msg_content" $new_parents'
f09c9b8c 358 then
6bb4e485 359 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
f5b49ea6 360 die_with_patch $sha1 "Error redoing merge $sha1"
f09c9b8c 361 fi
6bb4e485 362 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
f09c9b8c
JS
363 ;;
364 *)
db2b3b82 365 output eval git cherry-pick "$strategy_args" "$@" ||
f09c9b8c 366 die_with_patch $sha1 "Could not pick $sha1"
376ccb8c 367 ;;
f09c9b8c 368 esac
376ccb8c 369 ;;
f09c9b8c
JS
370 esac
371}
372
6368f3f8
JS
373nth_string () {
374 case "$1" in
375 *1[0-9]|*[04-9]) echo "$1"th;;
376 *1) echo "$1"st;;
377 *2) echo "$1"nd;;
378 *3) echo "$1"rd;;
379 esac
380}
381
a25eb139 382update_squash_messages () {
6bb4e485
MZ
383 if test -f "$squash_msg"; then
384 mv "$squash_msg" "$squash_msg".bak || exit
385 count=$(($(sed -n \
180bad3d 386 -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
6bb4e485 387 -e "q" < "$squash_msg".bak)+1))
bde1a686 388 {
180bad3d 389 printf '%s\n' "$comment_char This is a combination of $count commits."
bde1a686
MH
390 sed -e 1d -e '2,/^./{
391 /^$/d
6bb4e485
MZ
392 }' <"$squash_msg".bak
393 } >"$squash_msg"
6368f3f8 394 else
6bb4e485
MZ
395 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
396 count=2
bde1a686 397 {
180bad3d
JK
398 printf '%s\n' "$comment_char This is a combination of 2 commits."
399 printf '%s\n' "$comment_char The first commit's message is:"
bde1a686 400 echo
6bb4e485
MZ
401 cat "$fixup_msg"
402 } >"$squash_msg"
6368f3f8 403 fi
0205e72f
MH
404 case $1 in
405 squash)
6bb4e485 406 rm -f "$fixup_msg"
0205e72f 407 echo
180bad3d 408 printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
0205e72f 409 echo
ee0a4afb 410 commit_message $2
0205e72f
MH
411 ;;
412 fixup)
413 echo
180bad3d 414 printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
0205e72f 415 echo
180bad3d
JK
416 # Change the space after the comment character to TAB:
417 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
0205e72f 418 ;;
6bb4e485 419 esac >>"$squash_msg"
6368f3f8
JS
420}
421
422peek_next_command () {
180bad3d 423 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
6368f3f8
JS
424}
425
6bdcd0d2
MH
426# A squash/fixup has failed. Prepare the long version of the squash
427# commit message, then die_with_patch. This code path requires the
428# user to edit the combined commit message for all commits that have
429# been squashed/fixedup so far. So also erase the old squash
430# messages, effectively causing the combined commit to be used as the
431# new basis for any further squash/fixups. Args: sha1 rest
432die_failed_squash() {
6bb4e485
MZ
433 mv "$squash_msg" "$msg" || exit
434 rm -f "$fixup_msg"
435 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
6bdcd0d2
MH
436 warn
437 warn "Could not apply $1... $2"
438 die_with_patch $1 ""
439}
440
b079feed 441flush_rewritten_pending() {
6bb4e485 442 test -s "$rewritten_pending" || return
b079feed 443 newsha1="$(git rev-parse HEAD^0)"
6bb4e485
MZ
444 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
445 rm -f "$rewritten_pending"
b079feed
TR
446}
447
448record_in_rewritten() {
449 oldsha1="$(git rev-parse $1)"
6bb4e485 450 echo "$oldsha1" >> "$rewritten_pending"
b079feed
TR
451
452 case "$(peek_next_command)" in
41f556b9 453 squash|s|fixup|f)
b079feed 454 ;;
41f556b9 455 *)
b079feed
TR
456 flush_rewritten_pending
457 ;;
458 esac
459}
460
df5df20c
CW
461do_pick () {
462 if test "$(git rev-parse HEAD)" = "$squash_onto"
463 then
464 # Set the correct commit message and author info on the
465 # sentinel root before cherry-picking the original changes
466 # without committing (-n). Finally, update the sentinel again
467 # to include these changes. If the cherry-pick results in a
468 # conflict, this means our behaviour is similar to a standard
469 # failed cherry-pick during rebase, with a dirty index to
470 # resolve before manually running git commit --amend then git
471 # rebase --continue.
472 git commit --allow-empty --allow-empty-message --amend \
473 --no-post-rewrite -n -q -C $1 &&
474 pick_one -n $1 &&
475 git commit --allow-empty --allow-empty-message \
476 --amend --no-post-rewrite -n -q -C $1 ||
477 die_with_patch $1 "Could not apply $1... $2"
478 else
479 pick_one $1 ||
480 die_with_patch $1 "Could not apply $1... $2"
481 fi
482}
483
1b1dce4b 484do_next () {
6bb4e485
MZ
485 rm -f "$msg" "$author_script" "$amend" || exit
486 read -r command sha1 rest < "$todo"
1b1dce4b 487 case "$command" in
180bad3d 488 "$comment_char"*|''|noop)
1b1dce4b 489 mark_action_done
1b1dce4b 490 ;;
f8babc4d 491 pick|p)
1b1dce4b
JS
492 comment_for_reflog pick
493
494 mark_action_done
df5df20c 495 do_pick $sha1 "$rest"
b079feed 496 record_in_rewritten $sha1
1b1dce4b 497 ;;
6741aa6c
BG
498 reword|r)
499 comment_for_reflog reword
500
501 mark_action_done
df5df20c 502 do_pick $sha1 "$rest"
0becb3e4
AW
503 git commit --amend --no-post-rewrite || {
504 warn "Could not amend commit after successfully picking $sha1... $rest"
505 warn "This is most likely due to an empty commit message, or the pre-commit hook"
506 warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
507 warn "you are able to reword the commit."
508 exit_with_patch $sha1 1
509 }
b079feed 510 record_in_rewritten $sha1
6741aa6c 511 ;;
f8babc4d 512 edit|e)
1b1dce4b
JS
513 comment_for_reflog edit
514
515 mark_action_done
df5df20c 516 do_pick $sha1 "$rest"
a8ccc204 517 warn "Stopped at $sha1... $rest"
0becb3e4 518 exit_with_patch $sha1 0
1b1dce4b 519 ;;
0205e72f
MH
520 squash|s|fixup|f)
521 case "$command" in
522 squash|s)
523 squash_style=squash
524 ;;
525 fixup|f)
526 squash_style=fixup
527 ;;
528 esac
529 comment_for_reflog $squash_style
1b1dce4b 530
6bb4e485 531 test -f "$done" && has_action "$done" ||
0205e72f 532 die "Cannot '$squash_style' without a previous commit"
1b1dce4b
JS
533
534 mark_action_done
a25eb139 535 update_squash_messages $squash_style $sha1
6bb4e485
MZ
536 author_script_content=$(get_author_ident_from_commit HEAD)
537 echo "$author_script_content" > "$author_script"
538 eval "$author_script_content"
2147f844
CW
539 if ! pick_one -n $sha1
540 then
541 git rev-parse --verify HEAD >"$amend"
542 die_failed_squash $sha1 "$rest"
543 fi
6368f3f8 544 case "$(peek_next_command)" in
0205e72f 545 squash|s|fixup|f)
a25eb139
MH
546 # This is an intermediate commit; its message will only be
547 # used in case of trouble. So use the long version:
2147f844 548 do_with_author output git commit --amend --no-verify -F "$squash_msg" ||
6bdcd0d2 549 die_failed_squash $sha1 "$rest"
376ccb8c 550 ;;
6368f3f8 551 *)
a25eb139 552 # This is the final command of this squash/fixup group
6bb4e485 553 if test -f "$fixup_msg"
a25eb139 554 then
2147f844 555 do_with_author git commit --amend --no-verify -F "$fixup_msg" ||
6bdcd0d2
MH
556 die_failed_squash $sha1 "$rest"
557 else
6bb4e485 558 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
6bdcd0d2 559 rm -f "$GIT_DIR"/MERGE_MSG
2147f844 560 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e ||
6bdcd0d2 561 die_failed_squash $sha1 "$rest"
a25eb139 562 fi
6bb4e485 563 rm -f "$squash_msg" "$fixup_msg"
376ccb8c 564 ;;
6368f3f8 565 esac
b079feed 566 record_in_rewritten $sha1
1b1dce4b 567 ;;
cd035b1c 568 x|"exec")
6bb4e485 569 read -r command rest < "$todo"
cd035b1c
MM
570 mark_action_done
571 printf 'Executing: %s\n' "$rest"
572 # "exec" command doesn't take a sha1 in the todo-list.
573 # => can't just use $sha1 here.
431b7e78 574 git rev-parse --verify HEAD > "$state_dir"/stopped-sha
cd035b1c
MM
575 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
576 status=$?
1686519a
JH
577 # Run in subshell because require_clean_work_tree can die.
578 dirty=f
579 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
cd035b1c
MM
580 if test "$status" -ne 0
581 then
582 warn "Execution failed: $rest"
1686519a
JH
583 test "$dirty" = f ||
584 warn "and made changes to the index and/or the working tree"
585
cd035b1c
MM
586 warn "You can fix the problem, and then run"
587 warn
588 warn " git rebase --continue"
589 warn
ecfe1ea9
JS
590 if test $status -eq 127 # command not found
591 then
592 status=1
593 fi
cd035b1c 594 exit "$status"
1686519a 595 elif test "$dirty" = t
cd035b1c 596 then
1686519a
JH
597 warn "Execution succeeded: $rest"
598 warn "but left changes to the index and/or the working tree"
cd035b1c
MM
599 warn "Commit or stash your changes, and then run"
600 warn
601 warn " git rebase --continue"
602 warn
603 exit 1
604 fi
605 ;;
1b1dce4b
JS
606 *)
607 warn "Unknown command: $command $sha1 $rest"
9c8e1011 608 fixtodo="Please fix this using 'git rebase --edit-todo'."
f1be316a
JK
609 if git rev-parse --verify -q "$sha1" >/dev/null
610 then
9c8e1011 611 die_with_patch $sha1 "$fixtodo"
f1be316a 612 else
9c8e1011 613 die "$fixtodo"
f1be316a 614 fi
376ccb8c 615 ;;
1b1dce4b 616 esac
6bb4e485 617 test -s "$todo" && return
1b1dce4b 618
68a163c9 619 comment_for_reflog finish &&
6bb4e485 620 newhead=$(git rev-parse HEAD) &&
431b7e78 621 case $head_name in
73697a0b 622 refs/*)
1af221ef 623 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
431b7e78 624 git update-ref -m "$message" $head_name $newhead $orig_head &&
53f2ffa8
JK
625 git symbolic-ref \
626 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
627 HEAD $head_name
73697a0b
JS
628 ;;
629 esac && {
431b7e78 630 test ! -f "$state_dir"/verbose ||
2959c283 631 git diff-tree --stat $orig_head..HEAD
3df0a859 632 } &&
eb2151bb 633 {
6bb4e485
MZ
634 test -s "$rewritten_list" &&
635 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
eb2151bb
TR
636 true # we don't care if this copying failed
637 } &&
b079feed 638 if test -x "$GIT_DIR"/hooks/post-rewrite &&
6bb4e485
MZ
639 test -s "$rewritten_list"; then
640 "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
b079feed
TR
641 true # we don't care if this hook failed
642 fi &&
431b7e78 643 warn "Successfully rebased and updated $head_name."
1b1dce4b 644
15d4bf2e 645 return 1 # not failure; just to break the do_rest loop
1b1dce4b
JS
646}
647
15d4bf2e 648# can only return 0, when the infinite loop breaks
1b1dce4b
JS
649do_rest () {
650 while :
651 do
15d4bf2e 652 do_next || break
1b1dce4b 653 done
1b1dce4b
JS
654}
655
0e757e30
JS
656# skip picking commits whose parents are unchanged
657skip_unnecessary_picks () {
658 fd=3
2d6ca6ef 659 while read -r command rest
0e757e30
JS
660 do
661 # fd=3 means we skip the command
2d6ca6ef
BC
662 case "$fd,$command" in
663 3,pick|3,p)
6bb4e485 664 # pick a commit whose parent is current $onto -> skip
14d87298 665 sha1=${rest%% *}
2d6ca6ef 666 case "$(git rev-parse --verify --quiet "$sha1"^)" in
6bb4e485
MZ
667 "$onto"*)
668 onto=$sha1
2d6ca6ef
BC
669 ;;
670 *)
671 fd=1
672 ;;
673 esac
0e757e30 674 ;;
7bca7afe 675 3,"$comment_char"*|3,)
0e757e30
JS
676 # copy comments
677 ;;
678 *)
679 fd=1
680 ;;
681 esac
d1c3b10f 682 printf '%s\n' "$command${rest:+ }$rest" >&$fd
6bb4e485
MZ
683 done <"$todo" >"$todo.new" 3>>"$done" &&
684 mv -f "$todo".new "$todo" &&
b079feed
TR
685 case "$(peek_next_command)" in
686 squash|s|fixup|f)
6bb4e485 687 record_in_rewritten "$onto"
b079feed
TR
688 ;;
689 esac ||
0e757e30
JS
690 die "Could not skip unnecessary pick commands"
691}
692
75c69766
JH
693transform_todo_ids () {
694 while read -r command rest
695 do
696 case "$command" in
697 "$comment_char"* | exec)
698 # Be careful for oddball commands like 'exec'
699 # that do not have a SHA-1 at the beginning of $rest.
700 ;;
701 *)
702 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
703 rest="$sha1 ${rest#* }"
704 ;;
705 esac
706 printf '%s\n' "$command${rest:+ }$rest"
707 done <"$todo" >"$todo.new" &&
708 mv -f "$todo.new" "$todo"
709}
710
711expand_todo_ids() {
712 transform_todo_ids
713}
714
715collapse_todo_ids() {
56895038 716 transform_todo_ids --short
75c69766
JH
717}
718
f59baa50
NS
719# Rearrange the todo list that has both "pick sha1 msg" and
720# "pick sha1 fixup!/squash! msg" appears in it so that the latter
721# comes immediately after the former, and change "pick" to
722# "fixup"/"squash".
723rearrange_squash () {
68d5d03b
KB
724 # extract fixup!/squash! lines and resolve any referenced sha1's
725 while read -r pick sha1 message
726 do
727 case "$message" in
728 "squash! "*|"fixup! "*)
729 action="${message%%!*}"
22c5b136
AP
730 rest=$message
731 prefix=
732 # skip all squash! or fixup! (but save for later)
733 while :
734 do
735 case "$rest" in
736 "squash! "*|"fixup! "*)
737 prefix="$prefix${rest%%!*},"
738 rest="${rest#*! }"
739 ;;
740 *)
741 break
742 ;;
743 esac
744 done
745 echo "$sha1 $action $prefix $rest"
68d5d03b
KB
746 # if it's a single word, try to resolve to a full sha1 and
747 # emit a second copy. This allows us to match on both message
748 # and on sha1 prefix
749 if test "${rest#* }" = "$rest"; then
750 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
751 if test -n "$fullsha"; then
752 # prefix the action to uniquely identify this line as
753 # intended for full sha1 match
22c5b136 754 echo "$sha1 +$action $prefix $fullsha"
68d5d03b
KB
755 fi
756 fi
757 esac
758 done >"$1.sq" <"$1"
f59baa50
NS
759 test -s "$1.sq" || return
760
761 used=
57f2b6b2 762 while read -r pick sha1 message
f59baa50
NS
763 do
764 case " $used" in
765 *" $sha1 "*) continue ;;
766 esac
938791cd 767 printf '%s\n' "$pick $sha1 $message"
d3d7a421 768 used="$used$sha1 "
22c5b136 769 while read -r squash action msg_prefix msg_content
f59baa50 770 do
d3d7a421
KB
771 case " $used" in
772 *" $squash "*) continue ;;
773 esac
68d5d03b
KB
774 emit=0
775 case "$action" in
776 +*)
777 action="${action#+}"
778 # full sha1 prefix test
6bb4e485 779 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
68d5d03b
KB
780 *)
781 # message prefix test
6bb4e485 782 case "$message" in "$msg_content"*) emit=1;; esac ;;
68d5d03b
KB
783 esac
784 if test $emit = 1; then
22c5b136
AP
785 real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g")
786 printf '%s\n' "$action $squash ${real_prefix}$msg_content"
f59baa50 787 used="$used$squash "
68d5d03b 788 fi
f59baa50
NS
789 done <"$1.sq"
790 done >"$1.rearranged" <"$1"
791 cat "$1.rearranged" >"$1"
792 rm -f "$1.sq" "$1.rearranged"
793}
794
c2145384
LK
795# Add commands after a pick or after a squash/fixup serie
796# in the todo list.
797add_exec_commands () {
798 {
799 first=t
800 while read -r insn rest
801 do
802 case $insn in
803 pick)
804 test -n "$first" ||
805 printf "%s" "$cmd"
806 ;;
807 esac
808 printf "%s %s\n" "$insn" "$rest"
809 first=
810 done
811 printf "%s" "$cmd"
812 } <"$1" >"$1.new" &&
813 mv "$1.new" "$1"
814}
815
cf432ca0
MZ
816case "$action" in
817continue)
cf432ca0 818 # do we have anything to commit?
a6754cda 819 if git diff-index --cached --quiet HEAD --
cf432ca0
MZ
820 then
821 : Nothing to commit -- skip this
822 else
ffaaed88
MM
823 if ! test -f "$author_script"
824 then
825 die "You have staged changes in your working tree. If these changes are meant to be
826squashed into the previous commit, run:
827
828 git commit --amend
829
830If they are meant to go into a new commit, run:
831
832 git commit
833
834In both case, once you're done, continue with:
835
836 git rebase --continue
837"
838 fi
cf432ca0 839 . "$author_script" ||
ffaaed88 840 die "Error trying to find the author identity to amend commit"
cf432ca0 841 if test -f "$amend"
03270628 842 then
cf432ca0
MZ
843 current_head=$(git rev-parse --verify HEAD)
844 test "$current_head" = $(cat "$amend") ||
845 die "\
c14c3c82
DP
846You have uncommitted changes in your working tree. Please, commit them
847first and then run 'git rebase --continue' again."
2147f844
CW
848 do_with_author git commit --amend --no-verify -F "$msg" -e ||
849 die "Could not commit staged changes."
850 else
851 do_with_author git commit --no-verify -F "$msg" -e ||
852 die "Could not commit staged changes."
03270628 853 fi
cf432ca0 854 fi
18640d99 855
431b7e78 856 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
0acb62f2 857
cf432ca0
MZ
858 require_clean_work_tree "rebase"
859 do_rest
15d4bf2e 860 return 0
cf432ca0 861 ;;
cf432ca0 862skip)
cf432ca0 863 git rerere clear
cf432ca0 864
2959c283 865 do_rest
15d4bf2e 866 return 0
cf432ca0 867 ;;
eb9a7cb4 868edit-todo)
180bad3d 869 git stripspace --strip-comments <"$todo" >"$todo".new
eb9a7cb4 870 mv -f "$todo".new "$todo"
75c69766 871 collapse_todo_ids
eb9a7cb4 872 append_todo_help
180bad3d
JK
873 git stripspace --comment-lines >>"$todo" <<\EOF
874
875You are editing the todo file of an ongoing interactive rebase.
876To continue rebase after editing, run:
877 git rebase --continue
878
eb9a7cb4
AW
879EOF
880
881 git_sequence_editor "$todo" ||
882 die "Could not execute editor"
75c69766 883 expand_todo_ids
eb9a7cb4
AW
884
885 exit
886 ;;
cf432ca0 887esac
1b1dce4b 888
34262322
MZ
889git var GIT_COMMITTER_IDENT >/dev/null ||
890 die "You need to set your committer info first"
1b1dce4b 891
34262322
MZ
892comment_for_reflog start
893
71786f54 894if test ! -z "$switch_to"
34262322 895then
26cd160c 896 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
71786f54 897 output git checkout "$switch_to" -- ||
26cd160c
RR
898 die "Could not checkout $switch_to"
899
900 comment_for_reflog start
34262322
MZ
901fi
902
431b7e78 903orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
1224f3d0 904mkdir -p "$state_dir" || die "Could not create temporary $state_dir"
34262322 905
431b7e78 906: > "$state_dir"/interactive || die "Could not mark as interactive"
84df4560 907write_basic_state
6bb4e485 908if test t = "$preserve_merges"
34262322 909then
6bb4e485 910 if test -z "$rebase_root"
34262322 911 then
6bb4e485 912 mkdir "$rewritten" &&
431b7e78 913 for c in $(git merge-base --all $orig_head $upstream)
acc8559a 914 do
6bb4e485 915 echo $onto > "$rewritten"/$c ||
34262322 916 die "Could not init rewritten commits"
acc8559a 917 done
34262322 918 else
6bb4e485
MZ
919 mkdir "$rewritten" &&
920 echo $onto > "$rewritten"/root ||
34262322
MZ
921 die "Could not init rewritten commits"
922 fi
923 # No cherry-pick because our first pass is to determine
924 # parents to rewrite and skipping dropped commits would
925 # prematurely end our probe
6bb4e485 926 merges_option=
34262322 927else
6bb4e485 928 merges_option="--no-merges --cherry-pick"
34262322
MZ
929fi
930
431b7e78 931shorthead=$(git rev-parse --short $orig_head)
6bb4e485
MZ
932shortonto=$(git rev-parse --short $onto)
933if test -z "$rebase_root"
934 # this is now equivalent to ! -z "$upstream"
34262322 935then
6bb4e485 936 shortupstream=$(git rev-parse --short $upstream)
431b7e78 937 revisions=$upstream...$orig_head
6bb4e485 938 shortrevisions=$shortupstream..$shorthead
34262322 939else
431b7e78 940 revisions=$onto...$orig_head
6bb4e485 941 shortrevisions=$shorthead
34262322 942fi
6bb4e485 943git rev-list $merges_option --pretty=oneline --abbrev-commit \
34262322 944 --abbrev=7 --reverse --left-right --topo-order \
6bb4e485 945 $revisions | \
34262322
MZ
946 sed -n "s/^>//p" |
947while read -r shortsha1 rest
948do
90e1818f 949
98697784 950 if test -z "$keep_empty" && is_empty_commit $shortsha1 && ! is_merge_commit $shortsha1
90e1818f 951 then
180bad3d 952 comment_out="$comment_char "
90e1818f
NH
953 else
954 comment_out=
955 fi
956
6bb4e485 957 if test t != "$preserve_merges"
34262322 958 then
90e1818f 959 printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
34262322
MZ
960 else
961 sha1=$(git rev-parse $shortsha1)
6bb4e485 962 if test -z "$rebase_root"
acc8559a 963 then
34262322
MZ
964 preserve=t
965 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
acc8559a 966 do
12bf8283 967 if test -f "$rewritten"/$p
acc8559a 968 then
34262322 969 preserve=f
acc8559a
SH
970 fi
971 done
34262322
MZ
972 else
973 preserve=f
974 fi
975 if test f = "$preserve"
976 then
6bb4e485 977 touch "$rewritten"/$sha1
90e1818f 978 printf '%s\n' "${comment_out}pick $shortsha1 $rest" >>"$todo"
acc8559a 979 fi
34262322
MZ
980 fi
981done
d911d146 982
34262322 983# Watch for commits that been dropped by --cherry-pick
6bb4e485 984if test t = "$preserve_merges"
34262322 985then
6bb4e485 986 mkdir "$dropped"
34262322 987 # Save all non-cherry-picked changes
6bb4e485 988 git rev-list $revisions --left-right --cherry-pick | \
431b7e78 989 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
34262322
MZ
990 # Now all commits and note which ones are missing in
991 # not-cherry-picks and hence being dropped
6bb4e485 992 git rev-list $revisions |
34262322
MZ
993 while read rev
994 do
431b7e78 995 if test -f "$rewritten"/$rev -a "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
34262322
MZ
996 then
997 # Use -f2 because if rev-list is telling us this commit is
998 # not worthwhile, we don't want to track its multiple heads,
999 # just the history of its first-parent for others that will
1000 # be rebasing on top of it
6bb4e485 1001 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
34262322 1002 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
6bb4e485
MZ
1003 sane_grep -v "^[a-z][a-z]* $short" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1004 rm "$rewritten"/$rev
34262322
MZ
1005 fi
1006 done
1007fi
1008
6bb4e485
MZ
1009test -s "$todo" || echo noop >> "$todo"
1010test -n "$autosquash" && rearrange_squash "$todo"
c2145384
LK
1011test -n "$cmd" && add_exec_commands "$todo"
1012
180bad3d 1013cat >>"$todo" <<EOF
6047a234 1014
180bad3d 1015$comment_char Rebase $shortrevisions onto $shortonto
fcc5ef1c
AW
1016EOF
1017append_todo_help
180bad3d
JK
1018git stripspace --comment-lines >>"$todo" <<\EOF
1019
1020However, if you remove everything, the rebase will be aborted.
1021
1b1dce4b 1022EOF
1b1dce4b 1023
90e1818f
NH
1024if test -z "$keep_empty"
1025then
180bad3d 1026 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
90e1818f
NH
1027fi
1028
1029
6bb4e485 1030has_action "$todo" ||
34262322 1031 die_abort "Nothing to do"
1b1dce4b 1032
6bb4e485 1033cp "$todo" "$todo".backup
821881d8 1034git_sequence_editor "$todo" ||
34262322 1035 die_abort "Could not execute editor"
1b1dce4b 1036
6bb4e485 1037has_action "$todo" ||
34262322 1038 die_abort "Nothing to do"
c54b7817 1039
75c69766
JH
1040expand_todo_ids
1041
6bb4e485 1042test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
0e757e30 1043
26cd160c 1044GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
6bb4e485 1045output git checkout $onto || die_abort "could not detach HEAD"
431b7e78 1046git update-ref ORIG_HEAD $orig_head
34262322 1047do_rest