rebase -i: For fixup commands without squashes, do not start editor
[git/kusma.git] / git-rebase--interactive.sh
blob1569a7ad915988cfc14be71eb550debcd134f864
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
18 Available options are
19 v,verbose display a diffstat of what changed upstream
20 onto= rebase onto given branch instead of upstream
21 p,preserve-merges try to recreate merges instead of ignoring them
22 s,strategy= use the given merge strategy
23 m,merge always used (no-op)
24 i,interactive always used (no-op)
25 Actions:
26 continue continue rebasing process
27 abort abort rebasing process and restore original branch
28 skip skip current patch and continue rebasing process
29 no-verify override pre-rebase hook from stopping the operation
30 root rebase all reachable commmits up to the root(s)
33 . git-sh-setup
34 require_work_tree
36 DOTEST="$GIT_DIR/rebase-merge"
38 # The file containing rebase commands, comments, and empty lines.
39 # This file is created by "git rebase -i" then edited by the user. As
40 # the lines are processed, they are removed from the front of this
41 # file and written to the tail of $DONE.
42 TODO="$DOTEST"/git-rebase-todo
44 # The rebase command lines that have already been processed. A line
45 # is moved here when it is first handled, before any associated user
46 # actions.
47 DONE="$DOTEST"/done
49 # The commit message that is planned to be used for any changes that
50 # need to be committed following a user interaction.
51 MSG="$DOTEST"/message
53 # The file into which is accumulated the suggested commit message for
54 # squash/fixup commands. When the first of a series of squash/fixups
55 # is seen, the file is created and the commit message from the
56 # previous commit and from the first squash/fixup commit are written
57 # to it. The commit message for each subsequent squash/fixup commit
58 # is appended to the file as it is processed.
60 # The first line of the file is of the form
61 # # This is a combination of $COUNT commits.
62 # where $COUNT is the number of commits whose messages have been
63 # written to the file so far (including the initial "pick" commit).
64 # Each time that a commit message is processed, this line is read and
65 # updated. It is deleted just before the combined commit is made.
66 SQUASH_MSG="$DOTEST"/message-squash
68 # If the current series of squash/fixups has not yet included a squash
69 # command, then this file exists and holds the commit message of the
70 # original "pick" commit. (If the series ends without a "squash"
71 # command, then this can be used as the commit message of the combined
72 # commit without opening the editor.)
73 FIXUP_MSG="$DOTEST"/message-fixup
75 # $REWRITTEN is the name of a directory containing files for each
76 # commit that is reachable by at least one merge base of $HEAD and
77 # $UPSTREAM. They are not necessarily rewritten, but their children
78 # might be. This ensures that commits on merged, but otherwise
79 # unrelated side branches are left alone. (Think "X" in the man page's
80 # example.)
81 REWRITTEN="$DOTEST"/rewritten
83 DROPPED="$DOTEST"/dropped
85 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
86 # GIT_AUTHOR_DATE that will be used for the commit that is currently
87 # being rebased.
88 AUTHOR_SCRIPT="$DOTEST"/author-script
90 # When an "edit" rebase command is being processed, the SHA1 of the
91 # commit to be edited is recorded in this file. When "git rebase
92 # --continue" is executed, if there are any staged changes then they
93 # will be amended to the HEAD commit, but only provided the HEAD
94 # commit is still the commit to be edited. When any other rebase
95 # command is processed, this file is deleted.
96 AMEND="$DOTEST"/amend
98 PRESERVE_MERGES=
99 STRATEGY=
100 ONTO=
101 VERBOSE=
102 OK_TO_SKIP_PRE_REBASE=
103 REBASE_ROOT=
105 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
106 mark the corrected paths with 'git add <paths>', and
107 run 'git rebase --continue'"
108 export GIT_CHERRY_PICK_HELP
110 warn () {
111 echo "$*" >&2
114 output () {
115 case "$VERBOSE" in
117 output=$("$@" 2>&1 )
118 status=$?
119 test $status != 0 && printf "%s\n" "$output"
120 return $status
123 "$@"
125 esac
128 # Output the commit message for the specified commit.
129 commit_message () {
130 git cat-file commit "$1" | sed "1,/^$/d"
133 run_pre_rebase_hook () {
134 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
135 test -x "$GIT_DIR/hooks/pre-rebase"
136 then
137 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
138 echo >&2 "The pre-rebase hook refused to rebase."
139 exit 1
144 require_clean_work_tree () {
145 # test if working tree is dirty
146 git rev-parse --verify HEAD > /dev/null &&
147 git update-index --ignore-submodules --refresh &&
148 git diff-files --quiet --ignore-submodules &&
149 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
150 die "Working tree is dirty"
153 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
155 comment_for_reflog () {
156 case "$ORIG_REFLOG_ACTION" in
157 ''|rebase*)
158 GIT_REFLOG_ACTION="rebase -i ($1)"
159 export GIT_REFLOG_ACTION
161 esac
164 last_count=
165 mark_action_done () {
166 sed -e 1q < "$TODO" >> "$DONE"
167 sed -e 1d < "$TODO" >> "$TODO".new
168 mv -f "$TODO".new "$TODO"
169 count=$(sane_grep -c '^[^#]' < "$DONE")
170 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
171 if test "$last_count" != "$count"
172 then
173 last_count=$count
174 printf "Rebasing (%d/%d)\r" $count $total
175 test -z "$VERBOSE" || echo
179 make_patch () {
180 sha1_and_parents="$(git rev-list --parents -1 "$1")"
181 case "$sha1_and_parents" in
182 ?*' '?*' '?*)
183 git diff --cc $sha1_and_parents
185 ?*' '?*)
186 git diff-tree -p "$1^!"
189 echo "Root commit"
191 esac > "$DOTEST"/patch
192 test -f "$MSG" ||
193 commit_message "$1" > "$MSG"
194 test -f "$AUTHOR_SCRIPT" ||
195 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
198 die_with_patch () {
199 make_patch "$1"
200 git rerere
201 die "$2"
204 die_abort () {
205 rm -rf "$DOTEST"
206 die "$1"
209 has_action () {
210 sane_grep '^[^#]' "$1" >/dev/null
213 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
214 # GIT_AUTHOR_DATE exported from the current environment.
215 do_with_author () {
216 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
217 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
218 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
219 "$@"
222 pick_one () {
223 no_ff=
224 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
225 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
226 test -d "$REWRITTEN" &&
227 pick_one_preserving_merges "$@" && return
228 if test -n "$REBASE_ROOT"
229 then
230 output git cherry-pick "$@"
231 return
233 parent_sha1=$(git rev-parse --verify $sha1^) ||
234 die "Could not get the parent of $sha1"
235 current_sha1=$(git rev-parse --verify HEAD)
236 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
237 then
238 output git reset --hard $sha1
239 output warn Fast-forward to $(git rev-parse --short $sha1)
240 else
241 output git cherry-pick "$@"
245 pick_one_preserving_merges () {
246 fast_forward=t
247 case "$1" in
249 fast_forward=f
250 sha1=$2
253 sha1=$1
255 esac
256 sha1=$(git rev-parse $sha1)
258 if test -f "$DOTEST"/current-commit
259 then
260 if test "$fast_forward" = t
261 then
262 cat "$DOTEST"/current-commit | while read current_commit
264 git rev-parse HEAD > "$REWRITTEN"/$current_commit
265 done
266 rm "$DOTEST"/current-commit ||
267 die "Cannot write current commit's replacement sha1"
271 echo $sha1 >> "$DOTEST"/current-commit
273 # rewrite parents; if none were rewritten, we can fast-forward.
274 new_parents=
275 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
276 if test "$pend" = " "
277 then
278 pend=" root"
280 while [ "$pend" != "" ]
282 p=$(expr "$pend" : ' \([^ ]*\)')
283 pend="${pend# $p}"
285 if test -f "$REWRITTEN"/$p
286 then
287 new_p=$(cat "$REWRITTEN"/$p)
289 # If the todo reordered commits, and our parent is marked for
290 # rewriting, but hasn't been gotten to yet, assume the user meant to
291 # drop it on top of the current HEAD
292 if test -z "$new_p"
293 then
294 new_p=$(git rev-parse HEAD)
297 test $p != $new_p && fast_forward=f
298 case "$new_parents" in
299 *$new_p*)
300 ;; # do nothing; that parent is already there
302 new_parents="$new_parents $new_p"
304 esac
305 else
306 if test -f "$DROPPED"/$p
307 then
308 fast_forward=f
309 replacement="$(cat "$DROPPED"/$p)"
310 test -z "$replacement" && replacement=root
311 pend=" $replacement$pend"
312 else
313 new_parents="$new_parents $p"
316 done
317 case $fast_forward in
319 output warn "Fast-forward to $sha1"
320 output git reset --hard $sha1 ||
321 die "Cannot fast-forward to $sha1"
324 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
326 if [ "$1" != "-n" ]
327 then
328 # detach HEAD to current parent
329 output git checkout $first_parent 2> /dev/null ||
330 die "Cannot move HEAD to $first_parent"
333 case "$new_parents" in
334 ' '*' '*)
335 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
337 # redo merge
338 author_script=$(get_author_ident_from_commit $sha1)
339 eval "$author_script"
340 msg="$(commit_message $sha1)"
341 # No point in merging the first parent, that's HEAD
342 new_parents=${new_parents# $first_parent}
343 if ! do_with_author output \
344 git merge $STRATEGY -m "$msg" $new_parents
345 then
346 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
347 die_with_patch $sha1 "Error redoing merge $sha1"
351 output git cherry-pick "$@" ||
352 die_with_patch $sha1 "Could not pick $sha1"
354 esac
356 esac
359 nth_string () {
360 case "$1" in
361 *1[0-9]|*[04-9]) echo "$1"th;;
362 *1) echo "$1"st;;
363 *2) echo "$1"nd;;
364 *3) echo "$1"rd;;
365 esac
368 update_squash_messages () {
369 if test -f "$SQUASH_MSG"; then
370 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
371 COUNT=$(($(sed -n \
372 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
373 -e "q" < "$SQUASH_MSG".bak)+1))
375 echo "# This is a combination of $COUNT commits."
376 sed -e 1d -e '2,/^./{
377 /^$/d
378 }' <"$SQUASH_MSG".bak
379 } >$SQUASH_MSG
380 else
381 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
382 COUNT=2
384 echo "# This is a combination of 2 commits."
385 echo "# The first commit's message is:"
386 echo
387 cat "$FIXUP_MSG"
388 } >$SQUASH_MSG
390 case $1 in
391 squash)
392 rm -f "$FIXUP_MSG"
393 echo
394 echo "# This is the $(nth_string $COUNT) commit message:"
395 echo
396 commit_message $2
398 fixup)
399 echo
400 echo "# The $(nth_string $COUNT) commit message will be skipped:"
401 echo
402 commit_message $2 | sed -e 's/^/# /'
404 esac >>$SQUASH_MSG
407 peek_next_command () {
408 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
411 do_next () {
412 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
413 read command sha1 rest < "$TODO"
414 case "$command" in
415 '#'*|''|noop)
416 mark_action_done
418 pick|p)
419 comment_for_reflog pick
421 mark_action_done
422 pick_one $sha1 ||
423 die_with_patch $sha1 "Could not apply $sha1... $rest"
425 reword|r)
426 comment_for_reflog reword
428 mark_action_done
429 pick_one $sha1 ||
430 die_with_patch $sha1 "Could not apply $sha1... $rest"
431 git commit --amend
433 edit|e)
434 comment_for_reflog edit
436 mark_action_done
437 pick_one $sha1 ||
438 die_with_patch $sha1 "Could not apply $sha1... $rest"
439 make_patch $sha1
440 git rev-parse --verify HEAD > "$AMEND"
441 warn "Stopped at $sha1... $rest"
442 warn "You can amend the commit now, with"
443 warn
444 warn " git commit --amend"
445 warn
446 warn "Once you are satisfied with your changes, run"
447 warn
448 warn " git rebase --continue"
449 warn
450 exit 0
452 squash|s|fixup|f)
453 case "$command" in
454 squash|s)
455 squash_style=squash
457 fixup|f)
458 squash_style=fixup
460 esac
461 comment_for_reflog $squash_style
463 test -f "$DONE" && has_action "$DONE" ||
464 die "Cannot '$squash_style' without a previous commit"
466 mark_action_done
467 update_squash_messages $squash_style $sha1
468 failed=f
469 author_script=$(get_author_ident_from_commit HEAD)
470 echo "$author_script" > "$AUTHOR_SCRIPT"
471 eval "$author_script"
472 output git reset --soft HEAD^
473 pick_one -n $sha1 || failed=t
474 case "$(peek_next_command)" in
475 squash|s|fixup|f)
476 # This is an intermediate commit; its message will only be
477 # used in case of trouble. So use the long version:
478 if test $failed = f
479 then
480 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
481 failed=t
483 if test $failed = t
484 then
485 cp "$SQUASH_MSG" "$MSG" || exit
486 # After any kind of hiccup, prevent committing without
487 # opening the commit message editor:
488 rm -f "$FIXUP_MSG"
489 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
490 warn
491 warn "Could not apply $sha1... $rest"
492 die_with_patch $sha1 ""
496 # This is the final command of this squash/fixup group
497 if test $failed = f
498 then
499 if test -f "$FIXUP_MSG"
500 then
501 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
502 failed=t
503 else
504 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
505 rm -f "$GIT_DIR"/MERGE_MSG
506 do_with_author git commit --no-verify -e ||
507 failed=t
510 rm -f "$FIXUP_MSG"
511 if test $failed = t
512 then
513 mv "$SQUASH_MSG" "$MSG" || exit
514 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
515 warn
516 warn "Could not apply $sha1... $rest"
517 die_with_patch $sha1 ""
519 rm -f "$SQUASH_MSG"
521 esac
524 warn "Unknown command: $command $sha1 $rest"
525 if git rev-parse --verify -q "$sha1" >/dev/null
526 then
527 die_with_patch $sha1 "Please fix this in the file $TODO."
528 else
529 die "Please fix this in the file $TODO."
532 esac
533 test -s "$TODO" && return
535 comment_for_reflog finish &&
536 HEADNAME=$(cat "$DOTEST"/head-name) &&
537 OLDHEAD=$(cat "$DOTEST"/head) &&
538 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
539 NEWHEAD=$(git rev-parse HEAD) &&
540 case $HEADNAME in
541 refs/*)
542 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
543 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
544 git symbolic-ref HEAD $HEADNAME
546 esac && {
547 test ! -f "$DOTEST"/verbose ||
548 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
549 } &&
550 rm -rf "$DOTEST" &&
551 git gc --auto &&
552 warn "Successfully rebased and updated $HEADNAME."
554 exit
557 do_rest () {
558 while :
560 do_next
561 done
564 # skip picking commits whose parents are unchanged
565 skip_unnecessary_picks () {
566 fd=3
567 while read command sha1 rest
569 # fd=3 means we skip the command
570 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
571 3,pick,"$ONTO"*|3,p,"$ONTO"*)
572 # pick a commit whose parent is current $ONTO -> skip
573 ONTO=$sha1
575 3,#*|3,,*)
576 # copy comments
579 fd=1
581 esac
582 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
583 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
584 mv -f "$TODO".new "$TODO" ||
585 die "Could not skip unnecessary pick commands"
588 # check if no other options are set
589 is_standalone () {
590 test $# -eq 2 -a "$2" = '--' &&
591 test -z "$ONTO" &&
592 test -z "$PRESERVE_MERGES" &&
593 test -z "$STRATEGY" &&
594 test -z "$VERBOSE"
597 get_saved_options () {
598 test -d "$REWRITTEN" && PRESERVE_MERGES=t
599 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
600 test -f "$DOTEST"/verbose && VERBOSE=t
601 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
604 while test $# != 0
606 case "$1" in
607 --no-verify)
608 OK_TO_SKIP_PRE_REBASE=yes
610 --verify)
612 --continue)
613 is_standalone "$@" || usage
614 get_saved_options
615 comment_for_reflog continue
617 test -d "$DOTEST" || die "No interactive rebase running"
619 # Sanity check
620 git rev-parse --verify HEAD >/dev/null ||
621 die "Cannot read HEAD"
622 git update-index --ignore-submodules --refresh &&
623 git diff-files --quiet --ignore-submodules ||
624 die "Working tree is dirty"
626 # do we have anything to commit?
627 if git diff-index --cached --quiet --ignore-submodules HEAD --
628 then
629 : Nothing to commit -- skip this
630 else
631 . "$AUTHOR_SCRIPT" ||
632 die "Cannot find the author identity"
633 amend=
634 if test -f "$AMEND"
635 then
636 amend=$(git rev-parse --verify HEAD)
637 test "$amend" = $(cat "$AMEND") ||
638 die "\
639 You have uncommitted changes in your working tree. Please, commit them
640 first and then run 'git rebase --continue' again."
641 git reset --soft HEAD^ ||
642 die "Cannot rewind the HEAD"
644 do_with_author git commit --no-verify -F "$MSG" -e || {
645 test -n "$amend" && git reset --soft $amend
646 die "Could not commit staged changes."
650 require_clean_work_tree
651 do_rest
653 --abort)
654 is_standalone "$@" || usage
655 get_saved_options
656 comment_for_reflog abort
658 git rerere clear
659 test -d "$DOTEST" || die "No interactive rebase running"
661 HEADNAME=$(cat "$DOTEST"/head-name)
662 HEAD=$(cat "$DOTEST"/head)
663 case $HEADNAME in
664 refs/*)
665 git symbolic-ref HEAD $HEADNAME
667 esac &&
668 output git reset --hard $HEAD &&
669 rm -rf "$DOTEST"
670 exit
672 --skip)
673 is_standalone "$@" || usage
674 get_saved_options
675 comment_for_reflog skip
677 git rerere clear
678 test -d "$DOTEST" || die "No interactive rebase running"
680 output git reset --hard && do_rest
683 case "$#,$1" in
684 *,*=*)
685 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
686 1,*)
687 usage ;;
689 STRATEGY="-s $2"
690 shift ;;
691 esac
694 # we use merge anyway
697 VERBOSE=t
700 PRESERVE_MERGES=t
703 # yeah, we know
705 --root)
706 REBASE_ROOT=t
708 --onto)
709 shift
710 ONTO=$(git rev-parse --verify "$1") ||
711 die "Does not point to a valid commit: $1"
714 shift
715 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
716 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
717 test -d "$DOTEST" &&
718 die "Interactive rebase already started"
720 git var GIT_COMMITTER_IDENT >/dev/null ||
721 die "You need to set your committer info first"
723 if test -z "$REBASE_ROOT"
724 then
725 UPSTREAM_ARG="$1"
726 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
727 test -z "$ONTO" && ONTO=$UPSTREAM
728 shift
729 else
730 UPSTREAM=
731 UPSTREAM_ARG=--root
732 test -z "$ONTO" &&
733 die "You must specify --onto when using --root"
735 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
737 comment_for_reflog start
739 require_clean_work_tree
741 if test ! -z "$1"
742 then
743 output git show-ref --verify --quiet "refs/heads/$1" ||
744 die "Invalid branchname: $1"
745 output git checkout "$1" ||
746 die "Could not checkout $1"
749 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
750 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
752 : > "$DOTEST"/interactive || die "Could not mark as interactive"
753 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
754 echo "detached HEAD" > "$DOTEST"/head-name
756 echo $HEAD > "$DOTEST"/head
757 case "$REBASE_ROOT" in
759 rm -f "$DOTEST"/rebase-root ;;
761 : >"$DOTEST"/rebase-root ;;
762 esac
763 echo $ONTO > "$DOTEST"/onto
764 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
765 test t = "$VERBOSE" && : > "$DOTEST"/verbose
766 if test t = "$PRESERVE_MERGES"
767 then
768 if test -z "$REBASE_ROOT"
769 then
770 mkdir "$REWRITTEN" &&
771 for c in $(git merge-base --all $HEAD $UPSTREAM)
773 echo $ONTO > "$REWRITTEN"/$c ||
774 die "Could not init rewritten commits"
775 done
776 else
777 mkdir "$REWRITTEN" &&
778 echo $ONTO > "$REWRITTEN"/root ||
779 die "Could not init rewritten commits"
781 # No cherry-pick because our first pass is to determine
782 # parents to rewrite and skipping dropped commits would
783 # prematurely end our probe
784 MERGES_OPTION=
785 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
786 else
787 MERGES_OPTION="--no-merges --cherry-pick"
790 SHORTHEAD=$(git rev-parse --short $HEAD)
791 SHORTONTO=$(git rev-parse --short $ONTO)
792 if test -z "$REBASE_ROOT"
793 # this is now equivalent to ! -z "$UPSTREAM"
794 then
795 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
796 REVISIONS=$UPSTREAM...$HEAD
797 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
798 else
799 REVISIONS=$ONTO...$HEAD
800 SHORTREVISIONS=$SHORTHEAD
802 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
803 --abbrev=7 --reverse --left-right --topo-order \
804 $REVISIONS | \
805 sed -n "s/^>//p" | while read shortsha1 rest
807 if test t != "$PRESERVE_MERGES"
808 then
809 echo "pick $shortsha1 $rest" >> "$TODO"
810 else
811 sha1=$(git rev-parse $shortsha1)
812 if test -z "$REBASE_ROOT"
813 then
814 preserve=t
815 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
817 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
818 then
819 preserve=f
821 done
822 else
823 preserve=f
825 if test f = "$preserve"
826 then
827 touch "$REWRITTEN"/$sha1
828 echo "pick $shortsha1 $rest" >> "$TODO"
831 done
833 # Watch for commits that been dropped by --cherry-pick
834 if test t = "$PRESERVE_MERGES"
835 then
836 mkdir "$DROPPED"
837 # Save all non-cherry-picked changes
838 git rev-list $REVISIONS --left-right --cherry-pick | \
839 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
840 # Now all commits and note which ones are missing in
841 # not-cherry-picks and hence being dropped
842 git rev-list $REVISIONS |
843 while read rev
845 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
846 then
847 # Use -f2 because if rev-list is telling us this commit is
848 # not worthwhile, we don't want to track its multiple heads,
849 # just the history of its first-parent for others that will
850 # be rebasing on top of it
851 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
852 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
853 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
854 rm "$REWRITTEN"/$rev
856 done
859 test -s "$TODO" || echo noop >> "$TODO"
860 cat >> "$TODO" << EOF
862 # Rebase $SHORTREVISIONS onto $SHORTONTO
864 # Commands:
865 # p, pick = use commit
866 # r, reword = use commit, but edit the commit message
867 # e, edit = use commit, but stop for amending
868 # s, squash = use commit, but meld into previous commit
869 # f, fixup = like "squash", but discard this commit's log message
871 # If you remove a line here THAT COMMIT WILL BE LOST.
872 # However, if you remove everything, the rebase will be aborted.
876 has_action "$TODO" ||
877 die_abort "Nothing to do"
879 cp "$TODO" "$TODO".backup
880 git_editor "$TODO" ||
881 die "Could not execute editor"
883 has_action "$TODO" ||
884 die_abort "Nothing to do"
886 test -d "$REWRITTEN" || skip_unnecessary_picks
888 git update-ref ORIG_HEAD $HEAD
889 output git checkout $ONTO && do_rest
891 esac
892 shift
893 done