rebase -i: Retain user-edited commit messages after squash/fixup conflicts
[git/jrn.git] / git-rebase--interactive.sh
blobb835a2975987861cdea59c271aee1b3dea63cee6
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 # A squash/fixup has failed. Prepare the long version of the squash
412 # commit message, then die_with_patch. This code path requires the
413 # user to edit the combined commit message for all commits that have
414 # been squashed/fixedup so far. So also erase the old squash
415 # messages, effectively causing the combined commit to be used as the
416 # new basis for any further squash/fixups. Args: sha1 rest
417 die_failed_squash() {
418 mv "$SQUASH_MSG" "$MSG" || exit
419 rm -f "$FIXUP_MSG"
420 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
421 warn
422 warn "Could not apply $1... $2"
423 die_with_patch $1 ""
426 do_next () {
427 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
428 read command sha1 rest < "$TODO"
429 case "$command" in
430 '#'*|''|noop)
431 mark_action_done
433 pick|p)
434 comment_for_reflog pick
436 mark_action_done
437 pick_one $sha1 ||
438 die_with_patch $sha1 "Could not apply $sha1... $rest"
440 reword|r)
441 comment_for_reflog reword
443 mark_action_done
444 pick_one $sha1 ||
445 die_with_patch $sha1 "Could not apply $sha1... $rest"
446 git commit --amend
448 edit|e)
449 comment_for_reflog edit
451 mark_action_done
452 pick_one $sha1 ||
453 die_with_patch $sha1 "Could not apply $sha1... $rest"
454 make_patch $sha1
455 git rev-parse --verify HEAD > "$AMEND"
456 warn "Stopped at $sha1... $rest"
457 warn "You can amend the commit now, with"
458 warn
459 warn " git commit --amend"
460 warn
461 warn "Once you are satisfied with your changes, run"
462 warn
463 warn " git rebase --continue"
464 warn
465 exit 0
467 squash|s|fixup|f)
468 case "$command" in
469 squash|s)
470 squash_style=squash
472 fixup|f)
473 squash_style=fixup
475 esac
476 comment_for_reflog $squash_style
478 test -f "$DONE" && has_action "$DONE" ||
479 die "Cannot '$squash_style' without a previous commit"
481 mark_action_done
482 update_squash_messages $squash_style $sha1
483 author_script=$(get_author_ident_from_commit HEAD)
484 echo "$author_script" > "$AUTHOR_SCRIPT"
485 eval "$author_script"
486 output git reset --soft HEAD^
487 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
488 case "$(peek_next_command)" in
489 squash|s|fixup|f)
490 # This is an intermediate commit; its message will only be
491 # used in case of trouble. So use the long version:
492 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
493 die_failed_squash $sha1 "$rest"
496 # This is the final command of this squash/fixup group
497 if test -f "$FIXUP_MSG"
498 then
499 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
500 die_failed_squash $sha1 "$rest"
501 else
502 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
503 rm -f "$GIT_DIR"/MERGE_MSG
504 do_with_author git commit --no-verify -e ||
505 die_failed_squash $sha1 "$rest"
507 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
509 esac
512 warn "Unknown command: $command $sha1 $rest"
513 if git rev-parse --verify -q "$sha1" >/dev/null
514 then
515 die_with_patch $sha1 "Please fix this in the file $TODO."
516 else
517 die "Please fix this in the file $TODO."
520 esac
521 test -s "$TODO" && return
523 comment_for_reflog finish &&
524 HEADNAME=$(cat "$DOTEST"/head-name) &&
525 OLDHEAD=$(cat "$DOTEST"/head) &&
526 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
527 NEWHEAD=$(git rev-parse HEAD) &&
528 case $HEADNAME in
529 refs/*)
530 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
531 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
532 git symbolic-ref HEAD $HEADNAME
534 esac && {
535 test ! -f "$DOTEST"/verbose ||
536 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
537 } &&
538 rm -rf "$DOTEST" &&
539 git gc --auto &&
540 warn "Successfully rebased and updated $HEADNAME."
542 exit
545 do_rest () {
546 while :
548 do_next
549 done
552 # skip picking commits whose parents are unchanged
553 skip_unnecessary_picks () {
554 fd=3
555 while read command sha1 rest
557 # fd=3 means we skip the command
558 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
559 3,pick,"$ONTO"*|3,p,"$ONTO"*)
560 # pick a commit whose parent is current $ONTO -> skip
561 ONTO=$sha1
563 3,#*|3,,*)
564 # copy comments
567 fd=1
569 esac
570 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
571 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
572 mv -f "$TODO".new "$TODO" ||
573 die "Could not skip unnecessary pick commands"
576 # check if no other options are set
577 is_standalone () {
578 test $# -eq 2 -a "$2" = '--' &&
579 test -z "$ONTO" &&
580 test -z "$PRESERVE_MERGES" &&
581 test -z "$STRATEGY" &&
582 test -z "$VERBOSE"
585 get_saved_options () {
586 test -d "$REWRITTEN" && PRESERVE_MERGES=t
587 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
588 test -f "$DOTEST"/verbose && VERBOSE=t
589 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
592 while test $# != 0
594 case "$1" in
595 --no-verify)
596 OK_TO_SKIP_PRE_REBASE=yes
598 --verify)
600 --continue)
601 is_standalone "$@" || usage
602 get_saved_options
603 comment_for_reflog continue
605 test -d "$DOTEST" || die "No interactive rebase running"
607 # Sanity check
608 git rev-parse --verify HEAD >/dev/null ||
609 die "Cannot read HEAD"
610 git update-index --ignore-submodules --refresh &&
611 git diff-files --quiet --ignore-submodules ||
612 die "Working tree is dirty"
614 # do we have anything to commit?
615 if git diff-index --cached --quiet --ignore-submodules HEAD --
616 then
617 : Nothing to commit -- skip this
618 else
619 . "$AUTHOR_SCRIPT" ||
620 die "Cannot find the author identity"
621 amend=
622 if test -f "$AMEND"
623 then
624 amend=$(git rev-parse --verify HEAD)
625 test "$amend" = $(cat "$AMEND") ||
626 die "\
627 You have uncommitted changes in your working tree. Please, commit them
628 first and then run 'git rebase --continue' again."
629 git reset --soft HEAD^ ||
630 die "Cannot rewind the HEAD"
632 do_with_author git commit --no-verify -F "$MSG" -e || {
633 test -n "$amend" && git reset --soft $amend
634 die "Could not commit staged changes."
638 require_clean_work_tree
639 do_rest
641 --abort)
642 is_standalone "$@" || usage
643 get_saved_options
644 comment_for_reflog abort
646 git rerere clear
647 test -d "$DOTEST" || die "No interactive rebase running"
649 HEADNAME=$(cat "$DOTEST"/head-name)
650 HEAD=$(cat "$DOTEST"/head)
651 case $HEADNAME in
652 refs/*)
653 git symbolic-ref HEAD $HEADNAME
655 esac &&
656 output git reset --hard $HEAD &&
657 rm -rf "$DOTEST"
658 exit
660 --skip)
661 is_standalone "$@" || usage
662 get_saved_options
663 comment_for_reflog skip
665 git rerere clear
666 test -d "$DOTEST" || die "No interactive rebase running"
668 output git reset --hard && do_rest
671 case "$#,$1" in
672 *,*=*)
673 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
674 1,*)
675 usage ;;
677 STRATEGY="-s $2"
678 shift ;;
679 esac
682 # we use merge anyway
685 VERBOSE=t
688 PRESERVE_MERGES=t
691 # yeah, we know
693 --root)
694 REBASE_ROOT=t
696 --onto)
697 shift
698 ONTO=$(git rev-parse --verify "$1") ||
699 die "Does not point to a valid commit: $1"
702 shift
703 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
704 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
705 test -d "$DOTEST" &&
706 die "Interactive rebase already started"
708 git var GIT_COMMITTER_IDENT >/dev/null ||
709 die "You need to set your committer info first"
711 if test -z "$REBASE_ROOT"
712 then
713 UPSTREAM_ARG="$1"
714 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
715 test -z "$ONTO" && ONTO=$UPSTREAM
716 shift
717 else
718 UPSTREAM=
719 UPSTREAM_ARG=--root
720 test -z "$ONTO" &&
721 die "You must specify --onto when using --root"
723 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
725 comment_for_reflog start
727 require_clean_work_tree
729 if test ! -z "$1"
730 then
731 output git show-ref --verify --quiet "refs/heads/$1" ||
732 die "Invalid branchname: $1"
733 output git checkout "$1" ||
734 die "Could not checkout $1"
737 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
738 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
740 : > "$DOTEST"/interactive || die "Could not mark as interactive"
741 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
742 echo "detached HEAD" > "$DOTEST"/head-name
744 echo $HEAD > "$DOTEST"/head
745 case "$REBASE_ROOT" in
747 rm -f "$DOTEST"/rebase-root ;;
749 : >"$DOTEST"/rebase-root ;;
750 esac
751 echo $ONTO > "$DOTEST"/onto
752 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
753 test t = "$VERBOSE" && : > "$DOTEST"/verbose
754 if test t = "$PRESERVE_MERGES"
755 then
756 if test -z "$REBASE_ROOT"
757 then
758 mkdir "$REWRITTEN" &&
759 for c in $(git merge-base --all $HEAD $UPSTREAM)
761 echo $ONTO > "$REWRITTEN"/$c ||
762 die "Could not init rewritten commits"
763 done
764 else
765 mkdir "$REWRITTEN" &&
766 echo $ONTO > "$REWRITTEN"/root ||
767 die "Could not init rewritten commits"
769 # No cherry-pick because our first pass is to determine
770 # parents to rewrite and skipping dropped commits would
771 # prematurely end our probe
772 MERGES_OPTION=
773 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
774 else
775 MERGES_OPTION="--no-merges --cherry-pick"
778 SHORTHEAD=$(git rev-parse --short $HEAD)
779 SHORTONTO=$(git rev-parse --short $ONTO)
780 if test -z "$REBASE_ROOT"
781 # this is now equivalent to ! -z "$UPSTREAM"
782 then
783 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
784 REVISIONS=$UPSTREAM...$HEAD
785 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
786 else
787 REVISIONS=$ONTO...$HEAD
788 SHORTREVISIONS=$SHORTHEAD
790 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
791 --abbrev=7 --reverse --left-right --topo-order \
792 $REVISIONS | \
793 sed -n "s/^>//p" | while read shortsha1 rest
795 if test t != "$PRESERVE_MERGES"
796 then
797 echo "pick $shortsha1 $rest" >> "$TODO"
798 else
799 sha1=$(git rev-parse $shortsha1)
800 if test -z "$REBASE_ROOT"
801 then
802 preserve=t
803 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
805 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
806 then
807 preserve=f
809 done
810 else
811 preserve=f
813 if test f = "$preserve"
814 then
815 touch "$REWRITTEN"/$sha1
816 echo "pick $shortsha1 $rest" >> "$TODO"
819 done
821 # Watch for commits that been dropped by --cherry-pick
822 if test t = "$PRESERVE_MERGES"
823 then
824 mkdir "$DROPPED"
825 # Save all non-cherry-picked changes
826 git rev-list $REVISIONS --left-right --cherry-pick | \
827 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
828 # Now all commits and note which ones are missing in
829 # not-cherry-picks and hence being dropped
830 git rev-list $REVISIONS |
831 while read rev
833 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
834 then
835 # Use -f2 because if rev-list is telling us this commit is
836 # not worthwhile, we don't want to track its multiple heads,
837 # just the history of its first-parent for others that will
838 # be rebasing on top of it
839 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
840 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
841 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
842 rm "$REWRITTEN"/$rev
844 done
847 test -s "$TODO" || echo noop >> "$TODO"
848 cat >> "$TODO" << EOF
850 # Rebase $SHORTREVISIONS onto $SHORTONTO
852 # Commands:
853 # p, pick = use commit
854 # r, reword = use commit, but edit the commit message
855 # e, edit = use commit, but stop for amending
856 # s, squash = use commit, but meld into previous commit
857 # f, fixup = like "squash", but discard this commit's log message
859 # If you remove a line here THAT COMMIT WILL BE LOST.
860 # However, if you remove everything, the rebase will be aborted.
864 has_action "$TODO" ||
865 die_abort "Nothing to do"
867 cp "$TODO" "$TODO".backup
868 git_editor "$TODO" ||
869 die "Could not execute editor"
871 has_action "$TODO" ||
872 die_abort "Nothing to do"
874 test -d "$REWRITTEN" || skip_unnecessary_picks
876 git update-ref ORIG_HEAD $HEAD
877 output git checkout $ONTO && do_rest
879 esac
880 shift
881 done