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
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
53 rewritten="$state_dir"/rewritten
55 dropped="$state_dir"/dropped
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
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
81 if test -n "$do_merge"
83 strategy_args=${strategy:+--strategy=$strategy}
85 for strategy_opt in '"$strategy_opts"'
87 strategy_args="$strategy_args -X$(git rev-parse --sq-quote "${strategy_opt#--}")"
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)
99 printf '%s\n' "$*" >&2
102 # Output the commit message for the specified commit.
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
112 GIT_REFLOG_ACTION="rebase -i ($1)"
113 export GIT_REFLOG_ACTION
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)))
127 if test "$last_count" != "$new_count"
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
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.
163 sha1_and_parents="$(git rev-list --parents -1 "$1")"
164 case "$sha1_and_parents" in
166 git diff --cc $sha1_and_parents
169 git diff-tree -p "$1^!"
174 esac > "$state_dir"/patch
176 commit_message "$1" > "$msg"
177 test -f "$author_script" ||
178 get_author_ident_from_commit "$1" > "$author_script"
182 echo "$1" > "$state_dir"/stopped-sha
189 echo "$1" > "$state_dir"/stopped-sha
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"
195 warn " git commit --amend $gpg_sign_opt_quoted"
197 warn "Once you are satisfied with your changes, run"
199 warn " git rebase --continue"
210 test -n "$(git stripspace --strip-comments <"$1")"
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"
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.
230 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
235 git_sequence_editor () {
236 if test -z "$GIT_SEQUENCE_EDITOR"
238 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
239 if [ -z "$GIT_SEQUENCE_EDITOR" ]
241 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
245 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
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"
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.
269 case "$ret" in [01]) ;; *) reschedule_last_action ;; esac
273 pick_one_preserving_merges () {
284 sha1=$(git rev-parse $sha1)
286 if test -f "$state_dir"/current-commit
288 if test "$fast_forward" = t
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.
303 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
304 if test "$pend" = " "
308 while [ "$pend" != "" ]
310 p=$(expr "$pend" : ' \([^ ]*\)')
313 if test -f "$rewritten"/$p
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
322 new_p=$(git rev-parse HEAD)
325 test $p != $new_p && fast_forward=f
326 case "$new_parents" in
328 ;; # do nothing; that parent is already there
330 new_parents="$new_parents $new_p"
334 if test -f "$dropped"/$p
337 replacement="$(cat "$dropped"/$p)"
338 test -z "$replacement" && replacement=root
339 pend=" $replacement$pend"
341 new_parents="$new_parents $p"
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" : ' \([^ ]*\)')
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
363 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
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'
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"
394 *1[0-9]|*[04-9]) echo "$1"th;;
401 update_squash_messages () {
402 if test -f "$squash_msg"; then
403 mv "$squash_msg" "$squash_msg".bak || exit
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,/^./{
411 }' <"$squash_msg".bak
414 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
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:"
427 printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
433 printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
435 # Change the space after the comment character to TAB:
436 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
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
454 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
456 warn "Could not apply $1... $2"
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
475 flush_rewritten_pending
481 if test "$(git rev-parse HEAD)" = "$squash_onto"
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
491 git commit --allow-empty --allow-empty-message --amend \
492 --no-post-rewrite -n -q -C $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"
500 die_with_patch $1 "Could not apply $1... $2"
505 rm -f "$msg" "$author_script" "$amend" "$state_dir"/stopped-sha || exit
506 read -r command sha1 rest < "$todo"
508 "$comment_char"*|''|noop)
512 comment_for_reflog pick
515 do_pick $sha1 "$rest"
516 record_in_rewritten $sha1
519 comment_for_reflog reword
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
533 comment_for_reflog edit
536 do_pick $sha1 "$rest"
537 warn "Stopped at $sha1... $rest"
538 exit_with_patch $sha1 0
549 comment_for_reflog $squash_style
551 test -f "$done" && has_action "$done" ||
552 die "Cannot '$squash_style' without a previous commit"
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
561 git rev-parse --verify HEAD >"$amend"
562 die_failed_squash $sha1 "$rest"
564 case "$(peek_next_command)" in
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"
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"
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"
589 record_in_rewritten $sha1
592 read -r command rest < "$todo"
594 printf 'Executing: %s\n' "$rest"
595 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
597 # Run in subshell because require_clean_work_tree can die.
599 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
600 if test "$status" -ne 0
602 warn "Execution failed: $rest"
604 warn "and made changes to the index and/or the working tree"
606 warn "You can fix the problem, and then run"
608 warn " git rebase --continue"
610 if test $status -eq 127 # command not found
615 elif test "$dirty" = t
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"
621 warn " git rebase --continue"
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
631 die_with_patch $sha1 "$fixtodo"
637 test -s "$todo" && return
639 comment_for_reflog finish &&
640 newhead=$(git rev-parse HEAD) &&
643 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
644 git update-ref -m "$message" $head_name $newhead $orig_head &&
646 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
650 test ! -f "$state_dir"/verbose ||
651 git diff-tree --stat $orig_head..HEAD
654 test -s "$rewritten_list" &&
655 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
656 true # we don't care if this copying failed
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
663 warn "Successfully rebased and updated $head_name."
665 return 1 # not failure; just to break the do_rest loop
668 # can only return 0, when the infinite loop breaks
676 # skip picking commits whose parents are unchanged
677 skip_unnecessary_picks () {
679 while read -r command rest
681 # fd=3 means we skip the command
682 case "$fd,$command" in
684 # pick a commit whose parent is current $onto -> skip
686 case "$(git rev-parse --verify --quiet "$sha1"^)" in
695 3,"$comment_char"*|3,)
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
707 record_in_rewritten "$onto"
710 die "Could not skip unnecessary pick commands"
713 transform_todo_ids () {
714 while read -r command rest
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.
722 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
723 rest="$sha1 ${rest#* }"
726 printf '%s\n' "$command${rest:+ }$rest"
727 done <"$todo" >"$todo.new" &&
728 mv -f "$todo.new" "$todo"
735 collapse_todo_ids() {
736 transform_todo_ids --short
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
743 rearrange_squash () {
744 # extract fixup!/squash! lines and resolve any referenced sha1's
745 while read -r pick sha1 message
748 "squash! "*|"fixup! "*)
749 action="${message%%!*}"
752 # skip all squash! or fixup! (but save for later)
756 "squash! "*|"fixup! "*)
757 prefix="$prefix${rest%%!*},"
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
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"
779 test -s "$1.sq" || return
782 while read -r pick sha1 message
785 *" $sha1 "*) continue ;;
787 printf '%s\n' "$pick $sha1 $message"
789 while read -r squash action msg_prefix msg_content
792 *" $squash "*) continue ;;
798 # full sha1 prefix test
799 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
801 # message prefix test
802 case "$message" in "$msg_content"*) emit=1;; 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"
810 done >"$1.rearranged" <"$1"
811 cat "$1.rearranged" >"$1"
812 rm -f "$1.sq" "$1.rearranged"
815 # Add commands after a pick or after a squash/fixup serie
817 add_exec_commands () {
820 while read -r insn rest
828 printf "%s %s\n" "$insn" "$rest"
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.
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 () {
849 # do we have anything to commit?
850 if git diff-index --cached --quiet HEAD --
852 : Nothing to commit -- skip this
854 if ! test -f "$author_script"
856 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
857 die "You have staged changes in your working tree. If these changes are meant to be
858 squashed into the previous commit, run:
860 git commit --amend $gpg_sign_opt_quoted
862 If they are meant to go into a new commit, run:
864 git commit $gpg_sign_opt_quoted
866 In both case, once you're done, continue with:
868 git rebase --continue
871 . "$author_script" ||
872 die "Error trying to find the author identity to amend commit"
875 current_head=$(git rev-parse --verify HEAD)
876 test "$current_head" = $(cat "$amend") ||
878 You have uncommitted changes in your working tree. Please, commit them
879 first and then run 'git rebase --continue' again."
880 do_with_author git commit --amend --no-verify -F "$msg" -e \
881 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
882 die "Could not commit staged changes."
884 do_with_author git commit --no-verify -F "$msg" -e \
885 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
886 die "Could not commit staged changes."
890 if test -r "$state_dir"/stopped-sha
892 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
895 require_clean_work_tree "rebase"
906 git stripspace --strip-comments <"$todo" >"$todo".new
907 mv -f "$todo".new "$todo"
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"
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"
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"
945 if test t = "$preserve_merges"
947 if test -z "$rebase_root"
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"
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
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"
973 shortupstream=$(git rev-parse --short $upstream)
974 revisions=$upstream...$orig_head
975 shortrevisions=$shortupstream..$shorthead
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} | \
983 while read -r sha1 rest
986 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
988 comment_out="$comment_char "
993 if test t != "$preserve_merges"
995 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
997 if test -z "$rebase_root"
1000 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1002 if test -f "$rewritten"/$p
1010 if test f = "$preserve"
1012 touch "$rewritten"/$sha1
1013 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1018 # Watch for commits that been dropped by --cherry-pick
1019 if test t = "$preserve_merges"
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 |
1030 if test -f "$rewritten"/$rev && test "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
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
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##* }
1053 $comment_char Rebase $shortrevisions onto $shortonto ($todocount command(s))
1056 git stripspace --comment-lines >>"$todo" <<\EOF
1058 However, if you remove everything, the rebase will be aborted.
1062 if test -z "$keep_empty"
1064 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
1068 has_action "$todo" ||
1071 cp "$todo" "$todo".backup
1073 git_sequence_editor "$todo" ||
1074 die_abort "Could not execute editor"
1076 has_action "$todo" ||
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
1089 # ... and then we call the whole thing.
1090 git_rebase__interactive