1 # This shell script fragment is sourced by git-rebase to implement its
2 # preserve-merges mode.
4 # Copyright (c) 2006 Johannes E. Schindelin
6 # The file containing rebase commands, comments, and empty lines.
7 # This file is created by "git rebase -i" then edited by the user. As
8 # the lines are processed, they are removed from the front of this
9 # file and written to the tail of $done.
10 todo="$state_dir"/git-rebase-todo
12 # The rebase command lines that have already been processed. A line
13 # is moved here when it is first handled, before any associated user
15 done="$state_dir"/done
17 # The commit message that is planned to be used for any changes that
18 # need to be committed following a user interaction.
19 msg="$state_dir"/message
21 # The file into which is accumulated the suggested commit message for
22 # squash/fixup commands. When the first of a series of squash/fixups
23 # is seen, the file is created and the commit message from the
24 # previous commit and from the first squash/fixup commit are written
25 # to it. The commit message for each subsequent squash/fixup commit
26 # is appended to the file as it is processed.
28 # The first line of the file is of the form
29 # # This is a combination of $count commits.
30 # where $count is the number of commits whose messages have been
31 # written to the file so far (including the initial "pick" commit).
32 # Each time that a commit message is processed, this line is read and
33 # updated. It is deleted just before the combined commit is made.
34 squash_msg="$state_dir"/message-squash
36 # If the current series of squash/fixups has not yet included a squash
37 # command, then this file exists and holds the commit message of the
38 # original "pick" commit. (If the series ends without a "squash"
39 # command, then this can be used as the commit message of the combined
40 # commit without opening the editor.)
41 fixup_msg="$state_dir"/message-fixup
43 # $rewritten is the name of a directory containing files for each
44 # commit that is reachable by at least one merge base of $head and
45 # $upstream. They are not necessarily rewritten, but their children
46 # might be. This ensures that commits on merged, but otherwise
47 # unrelated side branches are left alone. (Think "X" in the man page's
49 rewritten="$state_dir"/rewritten
51 dropped="$state_dir"/dropped
54 msgnum="$state_dir"/msgnum
56 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
57 # GIT_AUTHOR_DATE that will be used for the commit that is currently
59 author_script="$state_dir"/author-script
61 # When an "edit" rebase command is being processed, the SHA1 of the
62 # commit to be edited is recorded in this file. When "git rebase
63 # --continue" is executed, if there are any staged changes then they
64 # will be amended to the HEAD commit, but only provided the HEAD
65 # commit is still the commit to be edited. When any other rebase
66 # command is processed, this file is deleted.
67 amend="$state_dir"/amend
69 # For the post-rewrite hook, we make a list of rewritten commits and
70 # their new sha1s. The rewritten-pending list keeps the sha1s of
71 # commits that have been processed, but not committed yet,
72 # e.g. because they are waiting for a 'squash' command.
73 rewritten_list="$state_dir"/rewritten-list
74 rewritten_pending="$state_dir"/rewritten-pending
76 # Work around Git for Windows' Bash whose "read" does not strip CRLF
77 # and leaves CR at the end instead.
80 strategy_args=${strategy:+--strategy=$strategy}
81 test -n "$strategy_opts" &&
83 for strategy_opt in '"$strategy_opts"'
85 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
89 GIT_CHERRY_PICK_HELP="$resolvemsg"
90 export GIT_CHERRY_PICK_HELP
92 comment_char=$(git config --get core.commentchar 2>/dev/null)
93 case "$comment_char" in
100 comment_char=$(echo "$comment_char" | cut -c1)
105 printf '%s\n' "$*" >&2
108 # Output the commit message for the specified commit.
110 git cat-file commit "$1" | sed "1,/^$/d"
113 orig_reflog_action="$GIT_REFLOG_ACTION"
115 comment_for_reflog () {
116 case "$orig_reflog_action" in
118 GIT_REFLOG_ACTION="rebase -i ($1)"
119 export GIT_REFLOG_ACTION
125 mark_action_done () {
126 sed -e 1q < "$todo" >> "$done"
127 sed -e 1d < "$todo" >> "$todo".new
128 mv -f "$todo".new "$todo"
129 new_count=$(( $(git stripspace --strip-comments <"$done" | wc -l) ))
130 echo $new_count >"$msgnum"
131 total=$(($new_count + $(git stripspace --strip-comments <"$todo" | wc -l)))
133 if test "$last_count" != "$new_count"
135 last_count=$new_count
136 eval_gettext "Rebasing (\$new_count/\$total)"; printf "\r"
137 test -z "$verbose" || echo
141 # Put the last action marked done at the beginning of the todo list
142 # again. If there has not been an action marked done yet, leave the list of
143 # items on the todo list unchanged.
144 reschedule_last_action () {
145 tail -n 1 "$done" | cat - "$todo" >"$todo".new
146 sed -e \$d <"$done" >"$done".new
147 mv -f "$todo".new "$todo"
148 mv -f "$done".new "$done"
151 append_todo_help () {
154 p, pick <commit> = use commit
155 r, reword <commit> = use commit, but edit the commit message
156 e, edit <commit> = use commit, but stop for amending
157 s, squash <commit> = use commit, but meld into previous commit
158 f, fixup <commit> = like \"squash\", but discard this commit's log message
159 x, exec <commit> = run command (the rest of the line) using shell
160 d, drop <commit> = remove commit
161 l, label <label> = label current HEAD with a name
162 t, reset <label> = reset HEAD to a label
163 m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
164 . create a merge commit using the original merge commit's
165 . message (or the oneline, if no original merge commit was
166 . specified). Use -c <commit> to reword the commit message.
168 These lines can be re-ordered; they are executed from top to bottom.
169 " | git stripspace --comment-lines >>"$todo"
171 if test $(get_missing_commit_check_level) = error
174 Do not remove any line. Use 'drop' explicitly to remove a commit.
175 " | git stripspace --comment-lines >>"$todo"
178 If you remove a line here THAT COMMIT WILL BE LOST.
179 " | git stripspace --comment-lines >>"$todo"
184 sha1_and_parents="$(git rev-list --parents -1 "$1")"
185 case "$sha1_and_parents" in
187 git diff --cc $sha1_and_parents
190 git diff-tree -p "$1^!"
195 esac > "$state_dir"/patch
197 commit_message "$1" > "$msg"
198 test -f "$author_script" ||
199 get_author_ident_from_commit "$1" > "$author_script"
203 echo "$1" > "$state_dir"/stopped-sha
204 git update-ref REBASE_HEAD "$1"
210 echo "$1" > "$state_dir"/stopped-sha
211 git update-ref REBASE_HEAD "$1"
213 git rev-parse --verify HEAD > "$amend"
214 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
215 warn "$(eval_gettext "\
216 You can amend the commit now, with
218 git commit --amend \$gpg_sign_opt_quoted
220 Once you are satisfied with your changes, run
222 git rebase --continue")"
234 test -n "$(git stripspace --strip-comments <"$1")"
238 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null) || {
240 die "$(eval_gettext "\$sha1: not a commit that can be picked")"
242 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null) ||
243 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
244 test "$tree" = "$ptree"
249 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
252 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
253 # GIT_AUTHOR_DATE exported from the current environment.
256 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
261 git_sequence_editor () {
262 if test -z "$GIT_SEQUENCE_EDITOR"
264 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
265 if [ -z "$GIT_SEQUENCE_EDITOR" ]
267 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
271 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
277 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
278 case "$force_rebase" in '') ;; ?*) ff= ;; esac
279 output git rev-parse --verify $sha1 || die "$(eval_gettext "Invalid commit name: \$sha1")"
281 if is_empty_commit "$sha1"
283 empty_args="--allow-empty"
286 pick_one_preserving_merges "$@"
289 pick_one_preserving_merges () {
300 sha1=$(git rev-parse $sha1)
302 if test -f "$state_dir"/current-commit && test "$fast_forward" = t
304 while read current_commit
306 git rev-parse HEAD > "$rewritten"/$current_commit
307 done <"$state_dir"/current-commit
308 rm "$state_dir"/current-commit ||
309 die "$(gettext "Cannot write current commit's replacement sha1")"
312 echo $sha1 >> "$state_dir"/current-commit
314 # rewrite parents; if none were rewritten, we can fast-forward.
316 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
317 if test "$pend" = " "
321 while [ "$pend" != "" ]
323 p=$(expr "$pend" : ' \([^ ]*\)')
326 if test -f "$rewritten"/$p
328 new_p=$(cat "$rewritten"/$p)
330 # If the todo reordered commits, and our parent is marked for
331 # rewriting, but hasn't been gotten to yet, assume the user meant to
332 # drop it on top of the current HEAD
335 new_p=$(git rev-parse HEAD)
338 test $p != $new_p && fast_forward=f
339 case "$new_parents" in
341 ;; # do nothing; that parent is already there
343 new_parents="$new_parents $new_p"
347 if test -f "$dropped"/$p
350 replacement="$(cat "$dropped"/$p)"
351 test -z "$replacement" && replacement=root
352 pend=" $replacement$pend"
354 new_parents="$new_parents $p"
358 case $fast_forward in
360 output warn "$(eval_gettext "Fast-forward to \$sha1")"
361 output git reset --hard $sha1 ||
362 die "$(eval_gettext "Cannot fast-forward to \$sha1")"
365 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
369 # detach HEAD to current parent
370 output git checkout $first_parent 2> /dev/null ||
371 die "$(eval_gettext "Cannot move HEAD to \$first_parent")"
374 case "$new_parents" in
376 test "a$1" = a-n && die "$(eval_gettext "Refusing to squash a merge: \$sha1")"
379 author_script_content=$(get_author_ident_from_commit $sha1)
380 eval "$author_script_content"
381 msg_content="$(commit_message $sha1)"
382 # No point in merging the first parent, that's HEAD
383 new_parents=${new_parents# $first_parent}
384 merge_args="--no-log --no-ff"
385 if ! do_with_author output eval \
386 git merge ${gpg_sign_opt:+$(git rev-parse \
387 --sq-quote "$gpg_sign_opt")} \
388 $allow_rerere_autoupdate "$merge_args" \
390 -m "$(git rev-parse --sq-quote "$msg_content")" \
393 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
394 die_with_patch $sha1 "$(eval_gettext "Error redoing merge \$sha1")"
396 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
399 output eval git cherry-pick $allow_rerere_autoupdate \
400 $allow_empty_message \
401 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
402 "$strategy_args" "$@" ||
403 die_with_patch $sha1 "$(eval_gettext "Could not pick \$sha1")"
410 this_nth_commit_message () {
412 eval_gettext "This is the commit message #\${n}:"
415 skip_nth_commit_message () {
417 eval_gettext "The commit message #\${n} will be skipped:"
420 update_squash_messages () {
421 if test -f "$squash_msg"; then
422 mv "$squash_msg" "$squash_msg".bak || exit
424 -e "1s/^$comment_char[^0-9]*\([0-9][0-9]*\).*/\1/p" \
425 -e "q" < "$squash_msg".bak)+1))
427 printf '%s\n' "$comment_char $(eval_ngettext \
428 "This is a combination of \$count commit." \
429 "This is a combination of \$count commits." \
431 sed -e 1d -e '2,/^./{
433 }' <"$squash_msg".bak
436 commit_message HEAD >"$fixup_msg" ||
437 die "$(eval_gettext "Cannot write \$fixup_msg")"
440 printf '%s\n' "$comment_char $(gettext "This is a combination of 2 commits.")"
441 printf '%s\n' "$comment_char $(gettext "This is the 1st commit message:")"
450 printf '%s\n' "$comment_char $(this_nth_commit_message $count)"
456 printf '%s\n' "$comment_char $(skip_nth_commit_message $count)"
458 # Change the space after the comment character to TAB:
459 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
464 peek_next_command () {
465 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
468 # A squash/fixup has failed. Prepare the long version of the squash
469 # commit message, then die_with_patch. This code path requires the
470 # user to edit the combined commit message for all commits that have
471 # been squashed/fixedup so far. So also erase the old squash
472 # messages, effectively causing the combined commit to be used as the
473 # new basis for any further squash/fixups. Args: sha1 rest
474 die_failed_squash() {
477 mv "$squash_msg" "$msg" || exit
479 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
481 warn "$(eval_gettext "Could not apply \$sha1... \$rest")"
482 die_with_patch $sha1 ""
485 flush_rewritten_pending() {
486 test -s "$rewritten_pending" || return
487 newsha1="$(git rev-parse HEAD^0)"
488 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
489 rm -f "$rewritten_pending"
492 record_in_rewritten() {
493 oldsha1="$(git rev-parse $1)"
494 echo "$oldsha1" >> "$rewritten_pending"
496 case "$(peek_next_command)" in
500 flush_rewritten_pending
508 if test "$(git rev-parse HEAD)" = "$squash_onto"
510 # Set the correct commit message and author info on the
511 # sentinel root before cherry-picking the original changes
512 # without committing (-n). Finally, update the sentinel again
513 # to include these changes. If the cherry-pick results in a
514 # conflict, this means our behaviour is similar to a standard
515 # failed cherry-pick during rebase, with a dirty index to
516 # resolve before manually running git commit --amend then git
518 git commit --allow-empty --allow-empty-message --amend \
519 --no-post-rewrite -n -q -C $sha1 $signoff &&
521 git commit --allow-empty --allow-empty-message \
522 --amend --no-post-rewrite -n -q -C $sha1 $signoff \
523 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
524 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
527 die_with_patch $sha1 "$(eval_gettext "Could not apply \$sha1... \$rest")"
532 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
533 read -r command sha1 rest < "$todo"
535 "$comment_char"*|''|noop|drop|d)
539 # Work around CR left by "read" (e.g. with Git for Windows' Bash).
543 comment_for_reflog pick
546 do_pick $sha1 "$rest"
547 record_in_rewritten $sha1
550 comment_for_reflog reword
553 do_pick $sha1 "$rest"
554 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} \
555 $allow_empty_message || {
556 warn "$(eval_gettext "\
557 Could not amend commit after successfully picking \$sha1... \$rest
558 This is most likely due to an empty commit message, or the pre-commit hook
559 failed. If the pre-commit hook failed, you may need to resolve the issue before
560 you are able to reword the commit.")"
561 exit_with_patch $sha1 1
563 record_in_rewritten $sha1
566 comment_for_reflog edit
569 do_pick $sha1 "$rest"
570 sha1_abbrev=$(git rev-parse --short $sha1)
571 warn "$(eval_gettext "Stopped at \$sha1_abbrev... \$rest")"
572 exit_with_patch $sha1 0
583 comment_for_reflog $squash_style
585 test -f "$done" && has_action "$done" ||
586 die "$(eval_gettext "Cannot '\$squash_style' without a previous commit")"
589 update_squash_messages $squash_style $sha1
590 author_script_content=$(get_author_ident_from_commit HEAD)
591 echo "$author_script_content" > "$author_script"
592 eval "$author_script_content"
593 if ! pick_one -n $sha1
595 git rev-parse --verify HEAD >"$amend"
596 die_failed_squash $sha1 "$rest"
598 case "$(peek_next_command)" in
600 # This is an intermediate commit; its message will only be
601 # used in case of trouble. So use the long version:
602 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
603 ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
604 die_failed_squash $sha1 "$rest"
607 # This is the final command of this squash/fixup group
608 if test -f "$fixup_msg"
610 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
611 ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
612 die_failed_squash $sha1 "$rest"
614 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
615 rm -f "$GIT_DIR"/MERGE_MSG
616 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
617 ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
618 die_failed_squash $sha1 "$rest"
620 rm -f "$squash_msg" "$fixup_msg"
623 record_in_rewritten $sha1
626 read -r command rest < "$todo"
628 eval_gettextln "Executing: \$rest"
629 "${SHELL:-@SHELL_PATH@}" -c "$rest" # Actual execution
631 # Run in subshell because require_clean_work_tree can die.
633 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
634 if test "$status" -ne 0
636 warn "$(eval_gettext "Execution failed: \$rest")"
638 warn "$(gettext "and made changes to the index and/or the working tree")"
641 You can fix the problem, and then run
643 git rebase --continue")"
645 if test $status -eq 127 # command not found
650 elif test "$dirty" = t
652 # TRANSLATORS: after these lines is a command to be issued by the user
653 warn "$(eval_gettext "\
654 Execution succeeded: \$rest
655 but left changes to the index and/or the working tree
656 Commit or stash your changes, and then run
658 git rebase --continue")"
664 warn "$(eval_gettext "Unknown command: \$command \$sha1 \$rest")"
665 fixtodo="$(gettext "Please fix this using 'git rebase --edit-todo'.")"
666 if git rev-parse --verify -q "$sha1" >/dev/null
668 die_with_patch $sha1 "$fixtodo"
674 test -s "$todo" && return
676 comment_for_reflog finish &&
677 newhead=$(git rev-parse HEAD) &&
680 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
681 git update-ref -m "$message" $head_name $newhead $orig_head &&
683 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
687 test ! -f "$state_dir"/verbose ||
688 git diff-tree --stat $orig_head..HEAD
691 test -s "$rewritten_list" &&
692 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
693 true # we don't care if this copying failed
695 hook="$(git rev-parse --git-path hooks/post-rewrite)"
696 if test -x "$hook" && test -s "$rewritten_list"; then
697 "$hook" rebase < "$rewritten_list"
698 true # we don't care if this hook failed
700 warn "$(eval_gettext "Successfully rebased and updated \$head_name.")"
702 return 1 # not failure; just to break the do_rest loop
705 # can only return 0, when the infinite loop breaks
714 git rebase--helper --expand-ids
717 collapse_todo_ids() {
718 git rebase--helper --shorten-ids
721 # Switch to the branch in $into and notify it in the reflog
723 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
724 output git checkout $onto || die_abort "$(gettext "could not detach HEAD")"
725 git update-ref ORIG_HEAD $orig_head
728 get_missing_commit_check_level () {
729 check_level=$(git config --get rebase.missingCommitsCheck)
730 check_level=${check_level:-ignore}
731 # Don't be case sensitive
732 printf '%s' "$check_level" | tr 'A-Z' 'a-z'
735 # Initiate an action. If the cannot be any
736 # further action it may exec a command
737 # or exit and not return.
739 # TODO: Consider a cleaner return model so it
740 # never exits and always return 0 if process
743 # Parameter 1 is the action to initiate.
745 # Returns 0 if the action was able to complete
746 # and if 1 if further processing is required.
750 # do we have anything to commit?
751 if git diff-index --cached --quiet HEAD --
753 # Nothing to commit -- skip this commit
755 test ! -f "$GIT_DIR"/CHERRY_PICK_HEAD ||
756 rm "$GIT_DIR"/CHERRY_PICK_HEAD ||
757 die "$(gettext "Could not remove CHERRY_PICK_HEAD")"
759 if ! test -f "$author_script"
761 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
762 die "$(eval_gettext "\
763 You have staged changes in your working tree.
764 If these changes are meant to be
765 squashed into the previous commit, run:
767 git commit --amend \$gpg_sign_opt_quoted
769 If they are meant to go into a new commit, run:
771 git commit \$gpg_sign_opt_quoted
773 In both cases, once you're done, continue with:
775 git rebase --continue
778 . "$author_script" ||
779 die "$(gettext "Error trying to find the author identity to amend commit")"
782 current_head=$(git rev-parse --verify HEAD)
783 test "$current_head" = $(cat "$amend") ||
785 You have uncommitted changes in your working tree. Please commit them
786 first and then run 'git rebase --continue' again.")"
787 do_with_author git commit --amend --no-verify -F "$msg" -e \
788 ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
789 die "$(gettext "Could not commit staged changes.")"
791 do_with_author git commit --no-verify -F "$msg" -e \
792 ${gpg_sign_opt:+"$gpg_sign_opt"} $allow_empty_message ||
793 die "$(gettext "Could not commit staged changes.")"
797 if test -r "$state_dir"/stopped-sha
799 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
802 require_clean_work_tree "rebase"
812 git stripspace --strip-comments <"$todo" >"$todo".new
813 mv -f "$todo".new "$todo"
817 You are editing the todo file of an ongoing interactive rebase.
818 To continue rebase after editing, run:
819 git rebase --continue
821 " | git stripspace --comment-lines >>"$todo"
823 git_sequence_editor "$todo" ||
824 die "$(gettext "Could not execute editor")"
830 exec git show REBASE_HEAD --
838 setup_reflog_action () {
839 comment_for_reflog start
841 if test ! -z "$switch_to"
843 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
844 output git checkout "$switch_to" -- ||
845 die "$(eval_gettext "Could not checkout \$switch_to")"
847 comment_for_reflog start
851 init_basic_state () {
852 orig_head=$(git rev-parse --verify HEAD) || die "$(gettext "No HEAD?")"
853 mkdir -p "$state_dir" || die "$(eval_gettext "Could not create temporary \$state_dir")"
854 rm -f "$(git rev-parse --git-path REBASE_HEAD)"
856 : > "$state_dir"/interactive || die "$(gettext "Could not mark as interactive")"
860 init_revisions_and_shortrevisions () {
861 shorthead=$(git rev-parse --short $orig_head)
862 shortonto=$(git rev-parse --short $onto)
863 if test -z "$rebase_root"
864 # this is now equivalent to ! -z "$upstream"
866 shortupstream=$(git rev-parse --short $upstream)
867 revisions=$upstream...$orig_head
868 shortrevisions=$shortupstream..$shorthead
870 revisions=$onto...$orig_head
871 shortrevisions=$shorthead
872 test -z "$squash_onto" ||
873 echo "$squash_onto" >"$state_dir"/squash-onto
878 test -s "$todo" || echo noop >> "$todo"
879 test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
880 test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
882 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
883 todocount=${todocount##* }
887 $comment_char $(eval_ngettext \
888 "Rebase \$shortrevisions onto \$shortonto (\$todocount command)" \
889 "Rebase \$shortrevisions onto \$shortonto (\$todocount commands)" \
894 However, if you remove everything, the rebase will be aborted.
896 " | git stripspace --comment-lines >>"$todo"
898 if test -z "$keep_empty"
900 printf '%s\n' "$comment_char $(gettext "Note that empty commits are commented out")" >>"$todo"
904 has_action "$todo" ||
907 cp "$todo" "$todo".backup
909 git_sequence_editor "$todo" ||
910 die_abort "$(gettext "Could not execute editor")"
912 has_action "$todo" ||
915 git rebase--helper --check-todo-list || {
926 git_rebase__preserve_merges () {
927 initiate_action "$action"
929 if test $ret = 0; then
936 if test -z "$rebase_root"
938 mkdir "$rewritten" &&
939 for c in $(git merge-base --all $orig_head $upstream)
941 echo $onto > "$rewritten"/$c ||
942 die "$(gettext "Could not init rewritten commits")"
945 mkdir "$rewritten" &&
946 echo $onto > "$rewritten"/root ||
947 die "$(gettext "Could not init rewritten commits")"
950 init_revisions_and_shortrevisions
952 format=$(git config --get rebase.instructionFormat)
953 # the 'rev-list .. | sed' requires %m to parse; the instruction requires %H to parse
954 git rev-list --format="%m%H ${format:-%s}" \
955 --reverse --left-right --topo-order \
956 $revisions ${restrict_revision+^$restrict_revision} | \
958 while read -r sha1 rest
960 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
962 comment_out="$comment_char "
967 if test -z "$rebase_root"
970 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
972 if test -f "$rewritten"/$p
980 if test f = "$preserve"
982 touch "$rewritten"/$sha1
983 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
987 # Watch for commits that been dropped by --cherry-pick
989 # Save all non-cherry-picked changes
990 git rev-list $revisions --left-right --cherry-pick | \
991 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
992 # Now all commits and note which ones are missing in
993 # not-cherry-picks and hence being dropped
994 git rev-list $revisions |
997 if test -f "$rewritten"/$rev &&
998 ! sane_grep "$rev" "$state_dir"/not-cherry-picks >/dev/null
1000 # Use -f2 because if rev-list is telling us this commit is
1001 # not worthwhile, we don't want to track its multiple heads,
1002 # just the history of its first-parent for others that will
1003 # be rebasing on top of it
1004 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1005 sha1=$(git rev-list -1 $rev)
1006 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1007 rm "$rewritten"/$rev