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
155 d, drop = remove commit
157 These lines can be re-ordered; they are executed from top to bottom.
159 If you remove a line here THAT COMMIT WILL BE LOST.
164 sha1_and_parents="$(git rev-list --parents -1 "$1")"
165 case "$sha1_and_parents" in
167 git diff --cc $sha1_and_parents
170 git diff-tree -p "$1^!"
175 esac > "$state_dir"/patch
177 commit_message "$1" > "$msg"
178 test -f "$author_script" ||
179 get_author_ident_from_commit "$1" > "$author_script"
183 echo "$1" > "$state_dir"/stopped-sha
190 echo "$1" > "$state_dir"/stopped-sha
192 git rev-parse --verify HEAD > "$amend"
193 gpg_sign_opt_quoted=${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")}
194 warn "You can amend the commit now, with"
196 warn " git commit --amend $gpg_sign_opt_quoted"
198 warn "Once you are satisfied with your changes, run"
200 warn " git rebase --continue"
211 test -n "$(git stripspace --strip-comments <"$1")"
215 tree=$(git rev-parse -q --verify "$1"^{tree} 2>/dev/null ||
216 die "$1: not a commit that can be picked")
217 ptree=$(git rev-parse -q --verify "$1"^^{tree} 2>/dev/null ||
218 ptree=4b825dc642cb6eb9a060e54bf8d69288fbee4904)
219 test "$tree" = "$ptree"
224 git rev-parse --verify --quiet "$1"^2 >/dev/null 2>&1
227 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
228 # GIT_AUTHOR_DATE exported from the current environment.
231 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
236 git_sequence_editor () {
237 if test -z "$GIT_SEQUENCE_EDITOR"
239 GIT_SEQUENCE_EDITOR="$(git config sequence.editor)"
240 if [ -z "$GIT_SEQUENCE_EDITOR" ]
242 GIT_SEQUENCE_EDITOR="$(git var GIT_EDITOR)" || return $?
246 eval "$GIT_SEQUENCE_EDITOR" '"$@"'
252 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
253 case "$force_rebase" in '') ;; ?*) ff= ;; esac
254 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
256 if is_empty_commit "$sha1"
258 empty_args="--allow-empty"
261 test -d "$rewritten" &&
262 pick_one_preserving_merges "$@" && return
263 output eval git cherry-pick \
264 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
265 "$strategy_args" $empty_args $ff "$@"
267 # If cherry-pick dies it leaves the to-be-picked commit unrecorded. Reschedule
268 # previous task so this commit is not lost.
270 case "$ret" in [01]) ;; *) reschedule_last_action ;; esac
274 pick_one_preserving_merges () {
285 sha1=$(git rev-parse $sha1)
287 if test -f "$state_dir"/current-commit
289 if test "$fast_forward" = t
291 while read current_commit
293 git rev-parse HEAD > "$rewritten"/$current_commit
294 done <"$state_dir"/current-commit
295 rm "$state_dir"/current-commit ||
296 die "Cannot write current commit's replacement sha1"
300 echo $sha1 >> "$state_dir"/current-commit
302 # rewrite parents; if none were rewritten, we can fast-forward.
304 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
305 if test "$pend" = " "
309 while [ "$pend" != "" ]
311 p=$(expr "$pend" : ' \([^ ]*\)')
314 if test -f "$rewritten"/$p
316 new_p=$(cat "$rewritten"/$p)
318 # If the todo reordered commits, and our parent is marked for
319 # rewriting, but hasn't been gotten to yet, assume the user meant to
320 # drop it on top of the current HEAD
323 new_p=$(git rev-parse HEAD)
326 test $p != $new_p && fast_forward=f
327 case "$new_parents" in
329 ;; # do nothing; that parent is already there
331 new_parents="$new_parents $new_p"
335 if test -f "$dropped"/$p
338 replacement="$(cat "$dropped"/$p)"
339 test -z "$replacement" && replacement=root
340 pend=" $replacement$pend"
342 new_parents="$new_parents $p"
346 case $fast_forward in
348 output warn "Fast-forward to $sha1"
349 output git reset --hard $sha1 ||
350 die "Cannot fast-forward to $sha1"
353 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
357 # detach HEAD to current parent
358 output git checkout $first_parent 2> /dev/null ||
359 die "Cannot move HEAD to $first_parent"
362 case "$new_parents" in
364 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
367 author_script_content=$(get_author_ident_from_commit $sha1)
368 eval "$author_script_content"
369 msg_content="$(commit_message $sha1)"
370 # No point in merging the first parent, that's HEAD
371 new_parents=${new_parents# $first_parent}
372 merge_args="--no-log --no-ff"
373 if ! do_with_author output eval \
374 'git merge ${gpg_sign_opt:+"$gpg_sign_opt"} \
375 $merge_args $strategy_args -m "$msg_content" $new_parents'
377 printf "%s\n" "$msg_content" > "$GIT_DIR"/MERGE_MSG
378 die_with_patch $sha1 "Error redoing merge $sha1"
380 echo "$sha1 $(git rev-parse HEAD^0)" >> "$rewritten_list"
383 output eval git cherry-pick \
384 ${gpg_sign_opt:+$(git rev-parse --sq-quote "$gpg_sign_opt")} \
385 "$strategy_args" "$@" ||
386 die_with_patch $sha1 "Could not pick $sha1"
395 *1[0-9]|*[04-9]) echo "$1"th;;
402 update_squash_messages () {
403 if test -f "$squash_msg"; then
404 mv "$squash_msg" "$squash_msg".bak || exit
406 -e "1s/^. This is a combination of \(.*\) commits\./\1/p" \
407 -e "q" < "$squash_msg".bak)+1))
409 printf '%s\n' "$comment_char This is a combination of $count commits."
410 sed -e 1d -e '2,/^./{
412 }' <"$squash_msg".bak
415 commit_message HEAD > "$fixup_msg" || die "Cannot write $fixup_msg"
418 printf '%s\n' "$comment_char This is a combination of 2 commits."
419 printf '%s\n' "$comment_char The first commit's message is:"
428 printf '%s\n' "$comment_char This is the $(nth_string $count) commit message:"
434 printf '%s\n' "$comment_char The $(nth_string $count) commit message will be skipped:"
436 # Change the space after the comment character to TAB:
437 commit_message $2 | git stripspace --comment-lines | sed -e 's/ / /'
442 peek_next_command () {
443 git stripspace --strip-comments <"$todo" | sed -n -e 's/ .*//p' -e q
446 # A squash/fixup has failed. Prepare the long version of the squash
447 # commit message, then die_with_patch. This code path requires the
448 # user to edit the combined commit message for all commits that have
449 # been squashed/fixedup so far. So also erase the old squash
450 # messages, effectively causing the combined commit to be used as the
451 # new basis for any further squash/fixups. Args: sha1 rest
452 die_failed_squash() {
453 mv "$squash_msg" "$msg" || exit
455 cp "$msg" "$GIT_DIR"/MERGE_MSG || exit
457 warn "Could not apply $1... $2"
461 flush_rewritten_pending() {
462 test -s "$rewritten_pending" || return
463 newsha1="$(git rev-parse HEAD^0)"
464 sed "s/$/ $newsha1/" < "$rewritten_pending" >> "$rewritten_list"
465 rm -f "$rewritten_pending"
468 record_in_rewritten() {
469 oldsha1="$(git rev-parse $1)"
470 echo "$oldsha1" >> "$rewritten_pending"
472 case "$(peek_next_command)" in
476 flush_rewritten_pending
482 if test "$(git rev-parse HEAD)" = "$squash_onto"
484 # Set the correct commit message and author info on the
485 # sentinel root before cherry-picking the original changes
486 # without committing (-n). Finally, update the sentinel again
487 # to include these changes. If the cherry-pick results in a
488 # conflict, this means our behaviour is similar to a standard
489 # failed cherry-pick during rebase, with a dirty index to
490 # resolve before manually running git commit --amend then git
492 git commit --allow-empty --allow-empty-message --amend \
493 --no-post-rewrite -n -q -C $1 &&
495 git commit --allow-empty --allow-empty-message \
496 --amend --no-post-rewrite -n -q -C $1 \
497 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
498 die_with_patch $1 "Could not apply $1... $2"
501 die_with_patch $1 "Could not apply $1... $2"
506 rm -f "$msg" "$author_script" "$amend" || exit
507 read -r command sha1 rest < "$todo"
509 "$comment_char"*|''|noop|drop|d)
513 comment_for_reflog pick
516 do_pick $sha1 "$rest"
517 record_in_rewritten $sha1
520 comment_for_reflog reword
523 do_pick $sha1 "$rest"
524 git commit --amend --no-post-rewrite ${gpg_sign_opt:+"$gpg_sign_opt"} || {
525 warn "Could not amend commit after successfully picking $sha1... $rest"
526 warn "This is most likely due to an empty commit message, or the pre-commit hook"
527 warn "failed. If the pre-commit hook failed, you may need to resolve the issue before"
528 warn "you are able to reword the commit."
529 exit_with_patch $sha1 1
531 record_in_rewritten $sha1
534 comment_for_reflog edit
537 do_pick $sha1 "$rest"
538 warn "Stopped at $sha1... $rest"
539 exit_with_patch $sha1 0
550 comment_for_reflog $squash_style
552 test -f "$done" && has_action "$done" ||
553 die "Cannot '$squash_style' without a previous commit"
556 update_squash_messages $squash_style $sha1
557 author_script_content=$(get_author_ident_from_commit HEAD)
558 echo "$author_script_content" > "$author_script"
559 eval "$author_script_content"
560 if ! pick_one -n $sha1
562 git rev-parse --verify HEAD >"$amend"
563 die_failed_squash $sha1 "$rest"
565 case "$(peek_next_command)" in
567 # This is an intermediate commit; its message will only be
568 # used in case of trouble. So use the long version:
569 do_with_author output git commit --amend --no-verify -F "$squash_msg" \
570 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
571 die_failed_squash $sha1 "$rest"
574 # This is the final command of this squash/fixup group
575 if test -f "$fixup_msg"
577 do_with_author git commit --amend --no-verify -F "$fixup_msg" \
578 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
579 die_failed_squash $sha1 "$rest"
581 cp "$squash_msg" "$GIT_DIR"/SQUASH_MSG || exit
582 rm -f "$GIT_DIR"/MERGE_MSG
583 do_with_author git commit --amend --no-verify -F "$GIT_DIR"/SQUASH_MSG -e \
584 ${gpg_sign_opt:+"$gpg_sign_opt"} ||
585 die_failed_squash $sha1 "$rest"
587 rm -f "$squash_msg" "$fixup_msg"
590 record_in_rewritten $sha1
593 read -r command rest < "$todo"
595 printf 'Executing: %s\n' "$rest"
596 # "exec" command doesn't take a sha1 in the todo-list.
597 # => can't just use $sha1 here.
598 git rev-parse --verify HEAD > "$state_dir"/stopped-sha
599 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
601 # Run in subshell because require_clean_work_tree can die.
603 (require_clean_work_tree "rebase" 2>/dev/null) || dirty=t
604 if test "$status" -ne 0
606 warn "Execution failed: $rest"
608 warn "and made changes to the index and/or the working tree"
610 warn "You can fix the problem, and then run"
612 warn " git rebase --continue"
614 if test $status -eq 127 # command not found
619 elif test "$dirty" = t
621 warn "Execution succeeded: $rest"
622 warn "but left changes to the index and/or the working tree"
623 warn "Commit or stash your changes, and then run"
625 warn " git rebase --continue"
631 warn "Unknown command: $command $sha1 $rest"
632 fixtodo="Please fix this using 'git rebase --edit-todo'."
633 if git rev-parse --verify -q "$sha1" >/dev/null
635 die_with_patch $sha1 "$fixtodo"
641 test -s "$todo" && return
643 comment_for_reflog finish &&
644 newhead=$(git rev-parse HEAD) &&
647 message="$GIT_REFLOG_ACTION: $head_name onto $onto" &&
648 git update-ref -m "$message" $head_name $newhead $orig_head &&
650 -m "$GIT_REFLOG_ACTION: returning to $head_name" \
654 test ! -f "$state_dir"/verbose ||
655 git diff-tree --stat $orig_head..HEAD
658 test -s "$rewritten_list" &&
659 git notes copy --for-rewrite=rebase < "$rewritten_list" ||
660 true # we don't care if this copying failed
662 hook="$(git rev-parse --git-path hooks/post-rewrite)"
663 if test -x "$hook" && test -s "$rewritten_list"; then
664 "$hook" rebase < "$rewritten_list"
665 true # we don't care if this hook failed
667 warn "Successfully rebased and updated $head_name."
669 return 1 # not failure; just to break the do_rest loop
672 # can only return 0, when the infinite loop breaks
680 # skip picking commits whose parents are unchanged
681 skip_unnecessary_picks () {
683 while read -r command rest
685 # fd=3 means we skip the command
686 case "$fd,$command" in
688 # pick a commit whose parent is current $onto -> skip
690 case "$(git rev-parse --verify --quiet "$sha1"^)" in
699 3,"$comment_char"*|3,)
706 printf '%s\n' "$command${rest:+ }$rest" >&$fd
707 done <"$todo" >"$todo.new" 3>>"$done" &&
708 mv -f "$todo".new "$todo" &&
709 case "$(peek_next_command)" in
711 record_in_rewritten "$onto"
714 die "Could not skip unnecessary pick commands"
717 transform_todo_ids () {
718 while read -r command rest
721 "$comment_char"* | exec)
722 # Be careful for oddball commands like 'exec'
723 # that do not have a SHA-1 at the beginning of $rest.
726 sha1=$(git rev-parse --verify --quiet "$@" ${rest%% *}) &&
727 rest="$sha1 ${rest#* }"
730 printf '%s\n' "$command${rest:+ }$rest"
731 done <"$todo" >"$todo.new" &&
732 mv -f "$todo.new" "$todo"
739 collapse_todo_ids() {
740 transform_todo_ids --short
743 # Rearrange the todo list that has both "pick sha1 msg" and
744 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
745 # comes immediately after the former, and change "pick" to
747 rearrange_squash () {
748 # extract fixup!/squash! lines and resolve any referenced sha1's
749 while read -r pick sha1 message
752 "squash! "*|"fixup! "*)
753 action="${message%%!*}"
756 # skip all squash! or fixup! (but save for later)
760 "squash! "*|"fixup! "*)
761 prefix="$prefix${rest%%!*},"
769 printf '%s %s %s %s\n' "$sha1" "$action" "$prefix" "$rest"
770 # if it's a single word, try to resolve to a full sha1 and
771 # emit a second copy. This allows us to match on both message
773 if test "${rest#* }" = "$rest"; then
774 fullsha="$(git rev-parse -q --verify "$rest" 2>/dev/null)"
775 if test -n "$fullsha"; then
776 # prefix the action to uniquely identify this line as
777 # intended for full sha1 match
778 echo "$sha1 +$action $prefix $fullsha"
783 test -s "$1.sq" || return
786 while read -r pick sha1 message
789 *" $sha1 "*) continue ;;
791 printf '%s\n' "$pick $sha1 $message"
793 while read -r squash action msg_prefix msg_content
796 *" $squash "*) continue ;;
802 # full sha1 prefix test
803 case "$msg_content" in "$sha1"*) emit=1;; esac ;;
805 # message prefix test
806 case "$message" in "$msg_content"*) emit=1;; esac ;;
808 if test $emit = 1; then
809 real_prefix=$(echo "$msg_prefix" | sed "s/,/! /g")
810 printf '%s\n' "$action $squash ${real_prefix}$msg_content"
814 done >"$1.rearranged" <"$1"
815 cat "$1.rearranged" >"$1"
816 rm -f "$1.sq" "$1.rearranged"
819 # Add commands after a pick or after a squash/fixup serie
821 add_exec_commands () {
824 while read -r insn rest
832 printf "%s %s\n" "$insn" "$rest"
840 # The whole contents of this file is run by dot-sourcing it from
841 # inside a shell function. It used to be that "return"s we see
842 # below were not inside any function, and expected to return
843 # to the function that dot-sourced us.
845 # However, FreeBSD /bin/sh misbehaves on such a construct and
846 # continues to run the statements that follow such a "return".
847 # As a work-around, we introduce an extra layer of a function
848 # here, and immediately call it after defining it.
849 git_rebase__interactive () {
853 # do we have anything to commit?
854 if git diff-index --cached --quiet HEAD --
856 : Nothing to commit -- skip this
858 if ! test -f "$author_script"
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:
864 git commit --amend $gpg_sign_opt_quoted
866 If they are meant to go into a new commit, run:
868 git commit $gpg_sign_opt_quoted
870 In both case, once you're done, continue with:
872 git rebase --continue
875 . "$author_script" ||
876 die "Error trying to find the author identity to amend commit"
879 current_head=$(git rev-parse --verify HEAD)
880 test "$current_head" = $(cat "$amend") ||
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."
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."
894 record_in_rewritten "$(cat "$state_dir"/stopped-sha)"
896 require_clean_work_tree "rebase"
907 git stripspace --strip-comments <"$todo" >"$todo".new
908 mv -f "$todo".new "$todo"
911 git stripspace --comment-lines >>"$todo" <<\EOF
913 You are editing the todo file of an ongoing interactive rebase.
914 To continue rebase after editing, run:
915 git rebase --continue
919 git_sequence_editor "$todo" ||
920 die "Could not execute editor"
927 git var GIT_COMMITTER_IDENT >/dev/null ||
928 die "You need to set your committer info first"
930 comment_for_reflog start
932 if test ! -z "$switch_to"
934 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to"
935 output git checkout "$switch_to" -- ||
936 die "Could not checkout $switch_to"
938 comment_for_reflog start
941 orig_head=$(git rev-parse --verify HEAD) || die "No HEAD?"
942 mkdir -p "$state_dir" || die "Could not create temporary $state_dir"
944 : > "$state_dir"/interactive || die "Could not mark as interactive"
946 if test t = "$preserve_merges"
948 if test -z "$rebase_root"
950 mkdir "$rewritten" &&
951 for c in $(git merge-base --all $orig_head $upstream)
953 echo $onto > "$rewritten"/$c ||
954 die "Could not init rewritten commits"
957 mkdir "$rewritten" &&
958 echo $onto > "$rewritten"/root ||
959 die "Could not init rewritten commits"
961 # No cherry-pick because our first pass is to determine
962 # parents to rewrite and skipping dropped commits would
963 # prematurely end our probe
966 merges_option="--no-merges --cherry-pick"
969 shorthead=$(git rev-parse --short $orig_head)
970 shortonto=$(git rev-parse --short $onto)
971 if test -z "$rebase_root"
972 # this is now equivalent to ! -z "$upstream"
974 shortupstream=$(git rev-parse --short $upstream)
975 revisions=$upstream...$orig_head
976 shortrevisions=$shortupstream..$shorthead
978 revisions=$onto...$orig_head
979 shortrevisions=$shorthead
981 git rev-list $merges_option --pretty=oneline --reverse --left-right --topo-order \
982 $revisions ${restrict_revision+^$restrict_revision} | \
984 while read -r sha1 rest
987 if test -z "$keep_empty" && is_empty_commit $sha1 && ! is_merge_commit $sha1
989 comment_out="$comment_char "
994 if test t != "$preserve_merges"
996 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
998 if test -z "$rebase_root"
1001 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
1003 if test -f "$rewritten"/$p
1011 if test f = "$preserve"
1013 touch "$rewritten"/$sha1
1014 printf '%s\n' "${comment_out}pick $sha1 $rest" >>"$todo"
1019 # Watch for commits that been dropped by --cherry-pick
1020 if test t = "$preserve_merges"
1023 # Save all non-cherry-picked changes
1024 git rev-list $revisions --left-right --cherry-pick | \
1025 sed -n "s/^>//p" > "$state_dir"/not-cherry-picks
1026 # Now all commits and note which ones are missing in
1027 # not-cherry-picks and hence being dropped
1028 git rev-list $revisions |
1031 if test -f "$rewritten"/$rev && test "$(sane_grep "$rev" "$state_dir"/not-cherry-picks)" = ""
1033 # Use -f2 because if rev-list is telling us this commit is
1034 # not worthwhile, we don't want to track its multiple heads,
1035 # just the history of its first-parent for others that will
1036 # be rebasing on top of it
1037 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$dropped"/$rev
1038 sha1=$(git rev-list -1 $rev)
1039 sane_grep -v "^[a-z][a-z]* $sha1" <"$todo" > "${todo}2" ; mv "${todo}2" "$todo"
1040 rm "$rewritten"/$rev
1045 test -s "$todo" || echo noop >> "$todo"
1046 test -n "$autosquash" && rearrange_squash "$todo"
1047 test -n "$cmd" && add_exec_commands "$todo"
1049 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
1050 todocount=${todocount##* }
1054 $comment_char Rebase $shortrevisions onto $shortonto ($todocount command(s))
1057 git stripspace --comment-lines >>"$todo" <<\EOF
1059 However, if you remove everything, the rebase will be aborted.
1063 if test -z "$keep_empty"
1065 printf '%s\n' "$comment_char Note that empty commits are commented out" >>"$todo"
1069 has_action "$todo" ||
1072 cp "$todo" "$todo".backup
1074 git_sequence_editor "$todo" ||
1075 die_abort "Could not execute editor"
1077 has_action "$todo" ||
1082 test -d "$rewritten" || test -n "$force_rebase" || skip_unnecessary_picks
1084 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
1085 output git checkout $onto || die_abort "could not detach HEAD"
1086 git update-ref ORIG_HEAD $orig_head
1090 # ... and then we call the whole thing.
1091 git_rebase__interactive