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