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