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