Merge branch 'mc/commit-doc-grammofix' into maint
[git.git] / git-rebase--interactive.sh
blob00080572529ae4c7e0c86c0aa2ba7cb83146dd2a
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.
5 # Copyright (c) 2006 Johannes E. Schindelin
7 # The original idea comes from Eric W. Biederman, in
8 # http://article.gmane.org/gmane.comp.version-control.git/22407
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
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
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
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.
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
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
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
55 dropped="$state_dir"/dropped
57 end="$state_dir"/end
58 msgnum="$state_dir"/msgnum
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
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
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
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"'
87 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
88 done
92 GIT_CHERRY_PICK_HELP="$resolvemsg"
93 export GIT_CHERRY_PICK_HELP
95 comment_char=$(git config --get core.commentchar 2>/dev/null | cut -c1)
96 : ${comment_char:=#}
98 warn () {
99 printf '%s\n' "$*" >&2
102 # Output the commit message for the specified commit.
103 commit_message () {
104 git cat-file commit "$1" | sed "1,/^$/d"
107 orig_reflog_action="$GIT_REFLOG_ACTION"
109 comment_for_reflog () {
110 case "$orig_reflog_action" in
111 ''|rebase*)
112 GIT_REFLOG_ACTION="rebase -i ($1)"
113 export GIT_REFLOG_ACTION
115 esac
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
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"
145 append_todo_help () {
146 git stripspace --comment-lines >>"$todo" <<\EOF
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
156 These lines can be re-ordered; they are executed from top to bottom.
158 If you remove a line here THAT COMMIT WILL BE LOST.
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
168 ?*' '?*)
169 git diff-tree -p "$1^!"
172 echo "Root commit"
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"
181 die_with_patch () {
182 echo "$1" > "$state_dir"/stopped-sha
183 make_patch "$1"
184 git rerere
185 die "$2"
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
204 die_abort () {
205 rm -rf "$state_dir"
206 die "$1"
209 has_action () {
210 test -n "$(git stripspace --strip-comments <"$1")"
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"
221 is_merge_commit()
223 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
226 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
227 # GIT_AUTHOR_DATE exported from the current environment.
228 do_with_author () {
230 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
231 "$@"
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 $?
245 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
248 pick_one () {
249 ff=--ff
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"
255 if is_empty_commit "$sha1"
256 then
257 empty_args="--allow-empty"
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 "$@"
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
273 pick_one_preserving_merges () {
274 fast_forward=t
275 case "$1" in
277 fast_forward=f
278 sha1=$2
281 sha1=$1
283 esac
284 sha1=$(git rev-parse $sha1)
286 if test -f "$state_dir"/current-commit
287 then
288 if test "$fast_forward" = t
289 then
290 while read current_commit
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"
299 echo $sha1 >> "$state_dir"/current-commit
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"
308 while [ "$pend" != "" ]
310 p=$(expr "$pend" : ' \([^ ]*\)')
311 pend="${pend# $p}"
313 if test -f "$rewritten"/$p
314 then
315 new_p=$(cat "$rewritten"/$p)
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)
325 test $p != $new_p && fast_forward=f
326 case "$new_parents" in
327 *$new_p*)
328 ;; # do nothing; that parent is already there
330 new_parents="$new_parents $new_p"
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"
344 done
345 case $fast_forward in
347 output warn "Fast-forward to $sha1"
348 output git reset --hard $sha1 ||
349 die "Cannot fast-forward to $sha1"
352 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
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"
361 case "$new_parents" in
362 ' '*' '*)
363 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
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"
379 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
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"
387 esac
389 esac
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
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))
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
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"
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
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/ / /'
438 esac >>"$squash_msg"
441 peek_next_command () {
442 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
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 ""
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"
467 record_in_rewritten() {
468 oldsha1="$(git rev-parse $1)"
469 echo "$oldsha1" >> "$rewritten_pending"
471 case "$(peek_next_command)" in
472 squash|s|fixup|f)
475 flush_rewritten_pending
477 esac
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"
504 do_next () {
505 rm -f "$msg" "$author_script" "$amend" || exit
506 read -r command sha1 rest < "$todo"
507 case "$command" in
508 "$comment_char"*|''|noop)
509 mark_action_done
511 pick|p)
512 comment_for_reflog pick
514 mark_action_done
515 do_pick $sha1 "$rest"
516 record_in_rewritten $sha1
518 reword|r)
519 comment_for_reflog reword
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
530 record_in_rewritten $sha1
532 edit|e)
533 comment_for_reflog edit
535 mark_action_done
536 do_pick $sha1 "$rest"
537 warn "Stopped at $sha1... $rest"
538 exit_with_patch $sha1 0
540 squash|s|fixup|f)
541 case "$command" in
542 squash|s)
543 squash_style=squash
545 fixup|f)
546 squash_style=fixup
548 esac
549 comment_for_reflog $squash_style
551 test -f "$done" && has_action "$done" ||
552 die "Cannot '$squash_style' without a previous commit"
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"
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"
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"
586 rm -f "$squash_msg" "$fixup_msg"
588 esac
589 record_in_rewritten $sha1
591 x|"exec")
592 read -r command rest < "$todo"
593 mark_action_done
594 printf 'Executing: %s\n' "$rest"
595 # "exec" command doesn't take a sha1 in the todo-list.
596 # => can't just use $sha1 here.
597 git rev-parse --verify HEAD > "$state_dir"/stopped-sha
598 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
599 status=$?
600 # Run in subshell because require_clean_work_tree can die.
601 dirty=f
602 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
603 if test "$status" -ne 0
604 then
605 warn "Execution failed: $rest"
606 test "$dirty" = f ||
607 warn "and made changes to the index and/or the working tree"
609 warn "You can fix the problem, and then run"
610 warn
611 warn " git rebase --continue"
612 warn
613 if test $status -eq 127 # command not found
614 then
615 status=1
617 exit "$status"
618 elif test "$dirty" = t
619 then
620 warn "Execution succeeded: $rest"
621 warn "but left changes to the index and/or the working tree"
622 warn "Commit or stash your changes, and then run"
623 warn
624 warn " git rebase --continue"
625 warn
626 exit 1
630 warn "Unknown command: $command $sha1 $rest"
631 fixtodo="Please fix this using 'git rebase --edit-todo'."
632 if git rev-parse --verify -q "$sha1" >/dev/null
633 then
634 die_with_patch $sha1 "$fixtodo"
635 else
636 die "$fixtodo"
639 esac
640 test -s "$todo" && return
642 comment_for_reflog finish &&
643 newhead=$(git rev-parse HEAD) &&
644 case $head_name in
645 refs/*)
646 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
647 git update-ref -m "$message" $head_name $newhead $orig_head &&
648 git symbolic-ref \
649 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
650 HEAD $head_name
652 esac && {
653 test ! -f "$state_dir"/verbose ||
654 git diff-tree --stat $orig_head..HEAD
655 } &&
657 test -s "$rewritten_list" &&
658 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
659 true # we don't care if this copying failed
660 } &&
661 if test -x "$GIT_DIR"/hooks/post-rewrite &&
662 test -s "$rewritten_list"; then
663 "$GIT_DIR"/hooks/post-rewrite rebase < "$rewritten_list"
664 true # we don't care if this hook failed
665 fi &&
666 warn "Successfully rebased and updated $head_name."
668 return 1 # not failure; just to break the do_rest loop
671 # can only return 0, when the infinite loop breaks
672 do_rest () {
673 while :
675 do_next || break
676 done
679 # skip picking commits whose parents are unchanged
680 skip_unnecessary_picks () {
681 fd=3
682 while read -r command rest
684 # fd=3 means we skip the command
685 case "$fd,$command" in
686 3,pick|3,p)
687 # pick a commit whose parent is current $onto -> skip
688 sha1=${rest%% *}
689 case "$(git rev-parse --verify --quiet "$sha1"^)" in
690 "$onto"*)
691 onto=$sha1
694 fd=1
696 esac
698 3,"$comment_char"*|3,)
699 # copy comments
702 fd=1
704 esac
705 printf '%s\n' "$command${rest:+ }$rest" >&$fd
706 done <"$todo" >"$todo.new" 3>>"$done" &&
707 mv -f "$todo".new "$todo" &&
708 case "$(peek_next_command)" in
709 squash|s|fixup|f)
710 record_in_rewritten "$onto"
712 esac ||
713 die "Could not skip unnecessary pick commands"
716 transform_todo_ids () {
717 while read -r command rest
719 case "$command" in
720 "$comment_char"* | exec)
721 # Be careful for oddball commands like 'exec'
722 # that do not have a SHA-1 at the beginning of $rest.
725 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
726 rest="$sha1 ${rest#* }"
728 esac
729 printf '%s\n' "$command${rest:+ }$rest"
730 done <"$todo" >"$todo.new" &&
731 mv -f "$todo.new" "$todo"
734 expand_todo_ids() {
735 transform_todo_ids
738 collapse_todo_ids() {
739 transform_todo_ids --short
742 # Rearrange the todo list that has both "pick sha1 msg" and
743 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
744 # comes immediately after the former, and change "pick" to
745 # "fixup"/"squash".
746 rearrange_squash () {
747 # extract fixup!/squash! lines and resolve any referenced sha1's
748 while read -r pick sha1 message
750 case "$message" in
751 "squash! "*|"fixup! "*)
752 action="${message%%!*}"
753 rest=$message
754 prefix=
755 # skip all squash! or fixup! (but save for later)
756 while :
758 case "$rest" in
759 "squash! "*|"fixup! "*)
760 prefix="$prefix${rest%%!*},"
761 rest="${rest#*! }"
764 break
766 esac
767 done
768 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
769 # if it's a single word, try to resolve to a full sha1 and
770 # emit a second copy. This allows us to match on both message
771 # and on sha1 prefix
772 if test "${rest#* }" = "$rest"; then
773 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
774 if test -n "$fullsha"; then
775 # prefix the action to uniquely identify this line as
776 # intended for full sha1 match
777 echo "$sha1 +$action $prefix $fullsha"
780 esac
781 done >"$1.sq" <"$1"
782 test -s "$1.sq" || return
784 used=
785 while read -r pick sha1 message
787 case " $used" in
788 *" $sha1 "*) continue ;;
789 esac
790 printf '%s\n' "$pick $sha1 $message"
791 used="$used$sha1 "
792 while read -r squash action msg_prefix msg_content
794 case " $used" in
795 *" $squash "*) continue ;;
796 esac
797 emit=0
798 case "$action" in
800 action="${action#+}"
801 # full sha1 prefix test
802 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
804 # message prefix test
805 case "$message" in "$msg_content"*) emit=1;; esac ;;
806 esac
807 if test $emit = 1; then
808 real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g")
809 printf '%s\n' "$action $squash ${real_prefix}$msg_content"
810 used="$used$squash "
812 done <"$1.sq"
813 done >"$1.rearranged" <"$1"
814 cat "$1.rearranged" >"$1"
815 rm -f "$1.sq" "$1.rearranged"
818 # Add commands after a pick or after a squash/fixup serie
819 # in the todo list.
820 add_exec_commands () {
822 first=t
823 while read -r insn rest
825 case $insn in
826 pick)
827 test -n "$first" ||
828 printf "%s" "$cmd"
830 esac
831 printf "%s %s\n" "$insn" "$rest"
832 first=
833 done
834 printf "%s" "$cmd"
835 } <"$1" >"$1.new" &&
836 mv "$1.new" "$1"
839 # The whole contents of this file is run by dot-sourcing it from
840 # inside a shell function. It used to be that "return"s we see
841 # below were not inside any function, and expected to return
842 # to the function that dot-sourced us.
844 # However, FreeBSD /bin/sh misbehaves on such a construct and
845 # continues to run the statements that follow such a "return".
846 # As a work-around, we introduce an extra layer of a function
847 # here, and immediately call it after defining it.
848 git_rebase__interactive () {
850 case "$action" in
851 continue)
852 # do we have anything to commit?
853 if git diff-index --cached --quiet HEAD --
854 then
855 : Nothing to commit -- skip this
856 else
857 if ! test -f "$author_script"
858 then
859 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
860 die "You have staged changes in your working tree. If these changes are meant to be
861 squashed into the previous commit, run:
863 git commit --amend $gpg_sign_opt_quoted
865 If they are meant to go into a new commit, run:
867 git commit $gpg_sign_opt_quoted
869 In both case, once you're done, continue with:
871 git rebase --continue
874 . "$author_script" ||
875 die "Error trying to find the author identity to amend commit"
876 if test -f "$amend"
877 then
878 current_head=$(git rev-parse --verify HEAD)
879 test "$current_head" = $(cat "$amend") ||
880 die "\
881 You have uncommitted changes in your working tree. Please, commit them
882 first and then run 'git rebase --continue' again."
883 do_with_author git commit --amend --no-verify -F "$msg" -e \
884 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
885 die "Could not commit staged changes."
886 else
887 do_with_author git commit --no-verify -F "$msg" -e \
888 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
889 die "Could not commit staged changes."
893 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
895 require_clean_work_tree "rebase"
896 do_rest
897 return 0
899 skip)
900 git rerere clear
902 do_rest
903 return 0
905 edit-todo)
906 git stripspace --strip-comments <"$todo" >"$todo".new
907 mv -f "$todo".new "$todo"
908 collapse_todo_ids
909 append_todo_help
910 git stripspace --comment-lines >>"$todo" <<\EOF
912 You are editing the todo file of an ongoing interactive rebase.
913 To continue rebase after editing, run:
914 git rebase --continue
918 git_sequence_editor "$todo" ||
919 die "Could not execute editor"
920 expand_todo_ids
922 exit
924 esac
926 git var GIT_COMMITTER_IDENT >/dev/null ||
927 die "You need to set your committer info first"
929 comment_for_reflog start
931 if test ! -z "$switch_to"
932 then
933 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
934 output git checkout "$switch_to" -- ||
935 die "Could not checkout $switch_to"
937 comment_for_reflog start
940 orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
941 mkdir -p "$state_dir" || die "Could not create temporary $state_dir"
943 : > "$state_dir"/interactive || die "Could not mark as interactive"
944 write_basic_state
945 if test t = "$preserve_merges"
946 then
947 if test -z "$rebase_root"
948 then
949 mkdir "$rewritten" &&
950 for c in $(git merge-base --all $orig_head $upstream)
952 echo $onto > "$rewritten"/$c ||
953 die "Could not init rewritten commits"
954 done
955 else
956 mkdir "$rewritten" &&
957 echo $onto > "$rewritten"/root ||
958 die "Could not init rewritten commits"
960 # No cherry-pick because our first pass is to determine
961 # parents to rewrite and skipping dropped commits would
962 # prematurely end our probe
963 merges_option=
964 else
965 merges_option="--no-merges --cherry-pick"
968 shorthead=$(git rev-parse --short $orig_head)
969 shortonto=$(git rev-parse --short $onto)
970 if test -z "$rebase_root"
971 # this is now equivalent to ! -z "$upstream"
972 then
973 shortupstream=$(git rev-parse --short $upstream)
974 revisions=$upstream...$orig_head
975 shortrevisions=$shortupstream..$shorthead
976 else
977 revisions=$onto...$orig_head
978 shortrevisions=$shorthead
980 git rev-list $merges_option --pretty=oneline --reverse --left-right --topo-order \
981 $revisions ${restrict_revision+^$restrict_revision} | \
982 sed -n "s/^>//p" |
983 while read -r sha1 rest
986 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
987 then
988 comment_out="$comment_char "
989 else
990 comment_out=
993 if test t != "$preserve_merges"
994 then
995 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
996 else
997 if test -z "$rebase_root"
998 then
999 preserve=t
1000 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1002 if test -f "$rewritten"/$p
1003 then
1004 preserve=f
1006 done
1007 else
1008 preserve=f
1010 if test f = "$preserve"
1011 then
1012 touch "$rewritten"/$sha1
1013 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1016 done
1018 # Watch for commits that been dropped by --cherry-pick
1019 if test t = "$preserve_merges"
1020 then
1021 mkdir "$dropped"
1022 # Save all non-cherry-picked changes
1023 git rev-list $revisions --left-right --cherry-pick | \
1024 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
1025 # Now all commits and note which ones are missing in
1026 # not-cherry-picks and hence being dropped
1027 git rev-list $revisions |
1028 while read rev
1030 if test -f "$rewritten"/$rev && test "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
1031 then
1032 # Use -f2 because if rev-list is telling us this commit is
1033 # not worthwhile, we don't want to track its multiple heads,
1034 # just the history of its first-parent for others that will
1035 # be rebasing on top of it
1036 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1037 sha1=$(git rev-list -1 $rev)
1038 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1039 rm "$rewritten"/$rev
1041 done
1044 test -s "$todo" || echo noop >> "$todo"
1045 test -n "$autosquash" && rearrange_squash "$todo"
1046 test -n "$cmd" && add_exec_commands "$todo"
1048 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
1049 todocount=${todocount##* }
1051 cat >>"$todo" <<EOF
1053 $comment_char Rebase $shortrevisions onto $shortonto ($todocount command(s))
1055 append_todo_help
1056 git stripspace --comment-lines >>"$todo" <<\EOF
1058 However, if you remove everything, the rebase will be aborted.
1062 if test -z "$keep_empty"
1063 then
1064 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
1068 has_action "$todo" ||
1069 return 2
1071 cp "$todo" "$todo".backup
1072 collapse_todo_ids
1073 git_sequence_editor "$todo" ||
1074 die_abort "Could not execute editor"
1076 has_action "$todo" ||
1077 return 2
1079 expand_todo_ids
1081 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1083 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
1084 output git checkout $onto || die_abort "could not detach HEAD"
1085 git update-ref ORIG_HEAD $orig_head
1086 do_rest
1089 # ... and then we call the whole thing.
1090 git_rebase__interactive