rebase -i: use new --ff cherry-pick option
[git/dscho.git] / git-rebase--interactive.sh
blob92d19f59035e0216cb88fdc50264d816653e9f06
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)
31 autosquash move commits that begin with squash!/fixup! under -i
34 . git-sh-setup
35 require_work_tree
37 DOTEST="$GIT_DIR/rebase-merge"
39 # The file containing rebase commands, comments, and empty lines.
40 # This file is created by "git rebase -i" then edited by the user. As
41 # the lines are processed, they are removed from the front of this
42 # file and written to the tail of $DONE.
43 TODO="$DOTEST"/git-rebase-todo
45 # The rebase command lines that have already been processed. A line
46 # is moved here when it is first handled, before any associated user
47 # actions.
48 DONE="$DOTEST"/done
50 # The commit message that is planned to be used for any changes that
51 # need to be committed following a user interaction.
52 MSG="$DOTEST"/message
54 # The file into which is accumulated the suggested commit message for
55 # squash/fixup commands. When the first of a series of squash/fixups
56 # is seen, the file is created and the commit message from the
57 # previous commit and from the first squash/fixup commit are written
58 # to it. The commit message for each subsequent squash/fixup commit
59 # is appended to the file as it is processed.
61 # The first line of the file is of the form
62 # # This is a combination of $COUNT commits.
63 # where $COUNT is the number of commits whose messages have been
64 # written to the file so far (including the initial "pick" commit).
65 # Each time that a commit message is processed, this line is read and
66 # updated. It is deleted just before the combined commit is made.
67 SQUASH_MSG="$DOTEST"/message-squash
69 # If the current series of squash/fixups has not yet included a squash
70 # command, then this file exists and holds the commit message of the
71 # original "pick" commit. (If the series ends without a "squash"
72 # command, then this can be used as the commit message of the combined
73 # commit without opening the editor.)
74 FIXUP_MSG="$DOTEST"/message-fixup
76 # $REWRITTEN is the name of a directory containing files for each
77 # commit that is reachable by at least one merge base of $HEAD and
78 # $UPSTREAM. They are not necessarily rewritten, but their children
79 # might be. This ensures that commits on merged, but otherwise
80 # unrelated side branches are left alone. (Think "X" in the man page's
81 # example.)
82 REWRITTEN="$DOTEST"/rewritten
84 DROPPED="$DOTEST"/dropped
86 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
87 # GIT_AUTHOR_DATE that will be used for the commit that is currently
88 # being rebased.
89 AUTHOR_SCRIPT="$DOTEST"/author-script
91 # When an "edit" rebase command is being processed, the SHA1 of the
92 # commit to be edited is recorded in this file. When "git rebase
93 # --continue" is executed, if there are any staged changes then they
94 # will be amended to the HEAD commit, but only provided the HEAD
95 # commit is still the commit to be edited. When any other rebase
96 # command is processed, this file is deleted.
97 AMEND="$DOTEST"/amend
99 PRESERVE_MERGES=
100 STRATEGY=
101 ONTO=
102 VERBOSE=
103 OK_TO_SKIP_PRE_REBASE=
104 REBASE_ROOT=
105 AUTOSQUASH=
107 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
108 mark the corrected paths with 'git add <paths>', and
109 run 'git rebase --continue'"
110 export GIT_CHERRY_PICK_HELP
112 warn () {
113 echo "$*" >&2
116 output () {
117 case "$VERBOSE" in
119 output=$("$@" 2>&1 )
120 status=$?
121 test $status != 0 && printf "%s\n" "$output"
122 return $status
125 "$@"
127 esac
130 # Output the commit message for the specified commit.
131 commit_message () {
132 git cat-file commit "$1" | sed "1,/^$/d"
135 run_pre_rebase_hook () {
136 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
137 test -x "$GIT_DIR/hooks/pre-rebase"
138 then
139 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
140 echo >&2 "The pre-rebase hook refused to rebase."
141 exit 1
146 require_clean_work_tree () {
147 # test if working tree is dirty
148 git rev-parse --verify HEAD > /dev/null &&
149 git update-index --ignore-submodules --refresh &&
150 git diff-files --quiet --ignore-submodules &&
151 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
152 die "Working tree is dirty"
155 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
157 comment_for_reflog () {
158 case "$ORIG_REFLOG_ACTION" in
159 ''|rebase*)
160 GIT_REFLOG_ACTION="rebase -i ($1)"
161 export GIT_REFLOG_ACTION
163 esac
166 last_count=
167 mark_action_done () {
168 sed -e 1q < "$TODO" >> "$DONE"
169 sed -e 1d < "$TODO" >> "$TODO".new
170 mv -f "$TODO".new "$TODO"
171 count=$(sane_grep -c '^[^#]' < "$DONE")
172 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
173 if test "$last_count" != "$count"
174 then
175 last_count=$count
176 printf "Rebasing (%d/%d)\r" $count $total
177 test -z "$VERBOSE" || echo
181 make_patch () {
182 sha1_and_parents="$(git rev-list --parents -1 "$1")"
183 case "$sha1_and_parents" in
184 ?*' '?*' '?*)
185 git diff --cc $sha1_and_parents
187 ?*' '?*)
188 git diff-tree -p "$1^!"
191 echo "Root commit"
193 esac > "$DOTEST"/patch
194 test -f "$MSG" ||
195 commit_message "$1" > "$MSG"
196 test -f "$AUTHOR_SCRIPT" ||
197 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
200 die_with_patch () {
201 make_patch "$1"
202 git rerere
203 die "$2"
206 die_abort () {
207 rm -rf "$DOTEST"
208 die "$1"
211 has_action () {
212 sane_grep '^[^#]' "$1" >/dev/null
215 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
216 # GIT_AUTHOR_DATE exported from the current environment.
217 do_with_author () {
219 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
220 "$@"
224 pick_one () {
225 ff=--ff
226 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
227 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
228 test -d "$REWRITTEN" &&
229 pick_one_preserving_merges "$@" && return
230 if test -n "$REBASE_ROOT"
231 then
232 output git cherry-pick "$@"
233 return
235 output git cherry-pick $ff "$@"
238 pick_one_preserving_merges () {
239 fast_forward=t
240 case "$1" in
242 fast_forward=f
243 sha1=$2
246 sha1=$1
248 esac
249 sha1=$(git rev-parse $sha1)
251 if test -f "$DOTEST"/current-commit
252 then
253 if test "$fast_forward" = t
254 then
255 cat "$DOTEST"/current-commit | while read current_commit
257 git rev-parse HEAD > "$REWRITTEN"/$current_commit
258 done
259 rm "$DOTEST"/current-commit ||
260 die "Cannot write current commit's replacement sha1"
264 echo $sha1 >> "$DOTEST"/current-commit
266 # rewrite parents; if none were rewritten, we can fast-forward.
267 new_parents=
268 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
269 if test "$pend" = " "
270 then
271 pend=" root"
273 while [ "$pend" != "" ]
275 p=$(expr "$pend" : ' \([^ ]*\)')
276 pend="${pend# $p}"
278 if test -f "$REWRITTEN"/$p
279 then
280 new_p=$(cat "$REWRITTEN"/$p)
282 # If the todo reordered commits, and our parent is marked for
283 # rewriting, but hasn't been gotten to yet, assume the user meant to
284 # drop it on top of the current HEAD
285 if test -z "$new_p"
286 then
287 new_p=$(git rev-parse HEAD)
290 test $p != $new_p && fast_forward=f
291 case "$new_parents" in
292 *$new_p*)
293 ;; # do nothing; that parent is already there
295 new_parents="$new_parents $new_p"
297 esac
298 else
299 if test -f "$DROPPED"/$p
300 then
301 fast_forward=f
302 replacement="$(cat "$DROPPED"/$p)"
303 test -z "$replacement" && replacement=root
304 pend=" $replacement$pend"
305 else
306 new_parents="$new_parents $p"
309 done
310 case $fast_forward in
312 output warn "Fast-forward to $sha1"
313 output git reset --hard $sha1 ||
314 die "Cannot fast-forward to $sha1"
317 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
319 if [ "$1" != "-n" ]
320 then
321 # detach HEAD to current parent
322 output git checkout $first_parent 2> /dev/null ||
323 die "Cannot move HEAD to $first_parent"
326 case "$new_parents" in
327 ' '*' '*)
328 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
330 # redo merge
331 author_script=$(get_author_ident_from_commit $sha1)
332 eval "$author_script"
333 msg="$(commit_message $sha1)"
334 # No point in merging the first parent, that's HEAD
335 new_parents=${new_parents# $first_parent}
336 if ! do_with_author output \
337 git merge $STRATEGY -m "$msg" $new_parents
338 then
339 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
340 die_with_patch $sha1 "Error redoing merge $sha1"
344 output git cherry-pick "$@" ||
345 die_with_patch $sha1 "Could not pick $sha1"
347 esac
349 esac
352 nth_string () {
353 case "$1" in
354 *1[0-9]|*[04-9]) echo "$1"th;;
355 *1) echo "$1"st;;
356 *2) echo "$1"nd;;
357 *3) echo "$1"rd;;
358 esac
361 update_squash_messages () {
362 if test -f "$SQUASH_MSG"; then
363 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
364 COUNT=$(($(sed -n \
365 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
366 -e "q" < "$SQUASH_MSG".bak)+1))
368 echo "# This is a combination of $COUNT commits."
369 sed -e 1d -e '2,/^./{
370 /^$/d
371 }' <"$SQUASH_MSG".bak
372 } >"$SQUASH_MSG"
373 else
374 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
375 COUNT=2
377 echo "# This is a combination of 2 commits."
378 echo "# The first commit's message is:"
379 echo
380 cat "$FIXUP_MSG"
381 } >"$SQUASH_MSG"
383 case $1 in
384 squash)
385 rm -f "$FIXUP_MSG"
386 echo
387 echo "# This is the $(nth_string $COUNT) commit message:"
388 echo
389 commit_message $2
391 fixup)
392 echo
393 echo "# The $(nth_string $COUNT) commit message will be skipped:"
394 echo
395 commit_message $2 | sed -e 's/^/# /'
397 esac >>"$SQUASH_MSG"
400 peek_next_command () {
401 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
404 # A squash/fixup has failed. Prepare the long version of the squash
405 # commit message, then die_with_patch. This code path requires the
406 # user to edit the combined commit message for all commits that have
407 # been squashed/fixedup so far. So also erase the old squash
408 # messages, effectively causing the combined commit to be used as the
409 # new basis for any further squash/fixups. Args: sha1 rest
410 die_failed_squash() {
411 mv "$SQUASH_MSG" "$MSG" || exit
412 rm -f "$FIXUP_MSG"
413 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
414 warn
415 warn "Could not apply $1... $2"
416 die_with_patch $1 ""
419 do_next () {
420 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
421 read command sha1 rest < "$TODO"
422 case "$command" in
423 '#'*|''|noop)
424 mark_action_done
426 pick|p)
427 comment_for_reflog pick
429 mark_action_done
430 pick_one $sha1 ||
431 die_with_patch $sha1 "Could not apply $sha1... $rest"
433 reword|r)
434 comment_for_reflog reword
436 mark_action_done
437 pick_one $sha1 ||
438 die_with_patch $sha1 "Could not apply $sha1... $rest"
439 git commit --amend
441 edit|e)
442 comment_for_reflog edit
444 mark_action_done
445 pick_one $sha1 ||
446 die_with_patch $sha1 "Could not apply $sha1... $rest"
447 make_patch $sha1
448 git rev-parse --verify HEAD > "$AMEND"
449 warn "Stopped at $sha1... $rest"
450 warn "You can amend the commit now, with"
451 warn
452 warn " git commit --amend"
453 warn
454 warn "Once you are satisfied with your changes, run"
455 warn
456 warn " git rebase --continue"
457 warn
458 exit 0
460 squash|s|fixup|f)
461 case "$command" in
462 squash|s)
463 squash_style=squash
465 fixup|f)
466 squash_style=fixup
468 esac
469 comment_for_reflog $squash_style
471 test -f "$DONE" && has_action "$DONE" ||
472 die "Cannot '$squash_style' without a previous commit"
474 mark_action_done
475 update_squash_messages $squash_style $sha1
476 author_script=$(get_author_ident_from_commit HEAD)
477 echo "$author_script" > "$AUTHOR_SCRIPT"
478 eval "$author_script"
479 output git reset --soft HEAD^
480 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
481 case "$(peek_next_command)" in
482 squash|s|fixup|f)
483 # This is an intermediate commit; its message will only be
484 # used in case of trouble. So use the long version:
485 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
486 die_failed_squash $sha1 "$rest"
489 # This is the final command of this squash/fixup group
490 if test -f "$FIXUP_MSG"
491 then
492 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
493 die_failed_squash $sha1 "$rest"
494 else
495 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
496 rm -f "$GIT_DIR"/MERGE_MSG
497 do_with_author git commit --no-verify -e ||
498 die_failed_squash $sha1 "$rest"
500 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
502 esac
505 warn "Unknown command: $command $sha1 $rest"
506 if git rev-parse --verify -q "$sha1" >/dev/null
507 then
508 die_with_patch $sha1 "Please fix this in the file $TODO."
509 else
510 die "Please fix this in the file $TODO."
513 esac
514 test -s "$TODO" && return
516 comment_for_reflog finish &&
517 HEADNAME=$(cat "$DOTEST"/head-name) &&
518 OLDHEAD=$(cat "$DOTEST"/head) &&
519 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
520 NEWHEAD=$(git rev-parse HEAD) &&
521 case $HEADNAME in
522 refs/*)
523 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
524 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
525 git symbolic-ref HEAD $HEADNAME
527 esac && {
528 test ! -f "$DOTEST"/verbose ||
529 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
530 } &&
531 rm -rf "$DOTEST" &&
532 git gc --auto &&
533 warn "Successfully rebased and updated $HEADNAME."
535 exit
538 do_rest () {
539 while :
541 do_next
542 done
545 # skip picking commits whose parents are unchanged
546 skip_unnecessary_picks () {
547 fd=3
548 while read command sha1 rest
550 # fd=3 means we skip the command
551 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
552 3,pick,"$ONTO"*|3,p,"$ONTO"*)
553 # pick a commit whose parent is current $ONTO -> skip
554 ONTO=$sha1
556 3,#*|3,,*)
557 # copy comments
560 fd=1
562 esac
563 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
564 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
565 mv -f "$TODO".new "$TODO" ||
566 die "Could not skip unnecessary pick commands"
569 # check if no other options are set
570 is_standalone () {
571 test $# -eq 2 -a "$2" = '--' &&
572 test -z "$ONTO" &&
573 test -z "$PRESERVE_MERGES" &&
574 test -z "$STRATEGY" &&
575 test -z "$VERBOSE"
578 get_saved_options () {
579 test -d "$REWRITTEN" && PRESERVE_MERGES=t
580 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
581 test -f "$DOTEST"/verbose && VERBOSE=t
582 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
585 # Rearrange the todo list that has both "pick sha1 msg" and
586 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
587 # comes immediately after the former, and change "pick" to
588 # "fixup"/"squash".
589 rearrange_squash () {
590 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
591 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
592 "$1" >"$1.sq"
593 test -s "$1.sq" || return
595 used=
596 while read pick sha1 message
598 case " $used" in
599 *" $sha1 "*) continue ;;
600 esac
601 echo "$pick $sha1 $message"
602 while read squash action msg
604 case "$message" in
605 "$msg"*)
606 echo "$action $squash $action! $msg"
607 used="$used$squash "
609 esac
610 done <"$1.sq"
611 done >"$1.rearranged" <"$1"
612 cat "$1.rearranged" >"$1"
613 rm -f "$1.sq" "$1.rearranged"
616 LF='
618 parse_onto () {
619 case "$1" in
620 *...*)
621 if left=${1%...*} right=${1#*...} &&
622 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
623 then
624 case "$onto" in
625 ?*"$LF"?* | '')
626 exit 1 ;;
627 esac
628 echo "$onto"
629 exit 0
631 esac
632 git rev-parse --verify "$1^0"
635 while test $# != 0
637 case "$1" in
638 --no-verify)
639 OK_TO_SKIP_PRE_REBASE=yes
641 --verify)
643 --continue)
644 is_standalone "$@" || usage
645 get_saved_options
646 comment_for_reflog continue
648 test -d "$DOTEST" || die "No interactive rebase running"
650 # Sanity check
651 git rev-parse --verify HEAD >/dev/null ||
652 die "Cannot read HEAD"
653 git update-index --ignore-submodules --refresh &&
654 git diff-files --quiet --ignore-submodules ||
655 die "Working tree is dirty"
657 # do we have anything to commit?
658 if git diff-index --cached --quiet --ignore-submodules HEAD --
659 then
660 : Nothing to commit -- skip this
661 else
662 . "$AUTHOR_SCRIPT" ||
663 die "Cannot find the author identity"
664 amend=
665 if test -f "$AMEND"
666 then
667 amend=$(git rev-parse --verify HEAD)
668 test "$amend" = $(cat "$AMEND") ||
669 die "\
670 You have uncommitted changes in your working tree. Please, commit them
671 first and then run 'git rebase --continue' again."
672 git reset --soft HEAD^ ||
673 die "Cannot rewind the HEAD"
675 do_with_author git commit --no-verify -F "$MSG" -e || {
676 test -n "$amend" && git reset --soft $amend
677 die "Could not commit staged changes."
681 require_clean_work_tree
682 do_rest
684 --abort)
685 is_standalone "$@" || usage
686 get_saved_options
687 comment_for_reflog abort
689 git rerere clear
690 test -d "$DOTEST" || die "No interactive rebase running"
692 HEADNAME=$(cat "$DOTEST"/head-name)
693 HEAD=$(cat "$DOTEST"/head)
694 case $HEADNAME in
695 refs/*)
696 git symbolic-ref HEAD $HEADNAME
698 esac &&
699 output git reset --hard $HEAD &&
700 rm -rf "$DOTEST"
701 exit
703 --skip)
704 is_standalone "$@" || usage
705 get_saved_options
706 comment_for_reflog skip
708 git rerere clear
709 test -d "$DOTEST" || die "No interactive rebase running"
711 output git reset --hard && do_rest
714 case "$#,$1" in
715 *,*=*)
716 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
717 1,*)
718 usage ;;
720 STRATEGY="-s $2"
721 shift ;;
722 esac
725 # we use merge anyway
728 VERBOSE=t
731 PRESERVE_MERGES=t
734 # yeah, we know
736 --root)
737 REBASE_ROOT=t
739 --autosquash)
740 AUTOSQUASH=t
742 --onto)
743 shift
744 ONTO=$(parse_onto "$1") ||
745 die "Does not point to a valid commit: $1"
748 shift
749 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
750 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
751 test -d "$DOTEST" &&
752 die "Interactive rebase already started"
754 git var GIT_COMMITTER_IDENT >/dev/null ||
755 die "You need to set your committer info first"
757 if test -z "$REBASE_ROOT"
758 then
759 UPSTREAM_ARG="$1"
760 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
761 test -z "$ONTO" && ONTO=$UPSTREAM
762 shift
763 else
764 UPSTREAM=
765 UPSTREAM_ARG=--root
766 test -z "$ONTO" &&
767 die "You must specify --onto when using --root"
769 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
771 comment_for_reflog start
773 require_clean_work_tree
775 if test ! -z "$1"
776 then
777 output git show-ref --verify --quiet "refs/heads/$1" ||
778 die "Invalid branchname: $1"
779 output git checkout "$1" ||
780 die "Could not checkout $1"
783 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
784 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
786 : > "$DOTEST"/interactive || die "Could not mark as interactive"
787 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
788 echo "detached HEAD" > "$DOTEST"/head-name
790 echo $HEAD > "$DOTEST"/head
791 case "$REBASE_ROOT" in
793 rm -f "$DOTEST"/rebase-root ;;
795 : >"$DOTEST"/rebase-root ;;
796 esac
797 echo $ONTO > "$DOTEST"/onto
798 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
799 test t = "$VERBOSE" && : > "$DOTEST"/verbose
800 if test t = "$PRESERVE_MERGES"
801 then
802 if test -z "$REBASE_ROOT"
803 then
804 mkdir "$REWRITTEN" &&
805 for c in $(git merge-base --all $HEAD $UPSTREAM)
807 echo $ONTO > "$REWRITTEN"/$c ||
808 die "Could not init rewritten commits"
809 done
810 else
811 mkdir "$REWRITTEN" &&
812 echo $ONTO > "$REWRITTEN"/root ||
813 die "Could not init rewritten commits"
815 # No cherry-pick because our first pass is to determine
816 # parents to rewrite and skipping dropped commits would
817 # prematurely end our probe
818 MERGES_OPTION=
819 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
820 else
821 MERGES_OPTION="--no-merges --cherry-pick"
824 SHORTHEAD=$(git rev-parse --short $HEAD)
825 SHORTONTO=$(git rev-parse --short $ONTO)
826 if test -z "$REBASE_ROOT"
827 # this is now equivalent to ! -z "$UPSTREAM"
828 then
829 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
830 REVISIONS=$UPSTREAM...$HEAD
831 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
832 else
833 REVISIONS=$ONTO...$HEAD
834 SHORTREVISIONS=$SHORTHEAD
836 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
837 --abbrev=7 --reverse --left-right --topo-order \
838 $REVISIONS | \
839 sed -n "s/^>//p" | while read shortsha1 rest
841 if test t != "$PRESERVE_MERGES"
842 then
843 echo "pick $shortsha1 $rest" >> "$TODO"
844 else
845 sha1=$(git rev-parse $shortsha1)
846 if test -z "$REBASE_ROOT"
847 then
848 preserve=t
849 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
851 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
852 then
853 preserve=f
855 done
856 else
857 preserve=f
859 if test f = "$preserve"
860 then
861 touch "$REWRITTEN"/$sha1
862 echo "pick $shortsha1 $rest" >> "$TODO"
865 done
867 # Watch for commits that been dropped by --cherry-pick
868 if test t = "$PRESERVE_MERGES"
869 then
870 mkdir "$DROPPED"
871 # Save all non-cherry-picked changes
872 git rev-list $REVISIONS --left-right --cherry-pick | \
873 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
874 # Now all commits and note which ones are missing in
875 # not-cherry-picks and hence being dropped
876 git rev-list $REVISIONS |
877 while read rev
879 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
880 then
881 # Use -f2 because if rev-list is telling us this commit is
882 # not worthwhile, we don't want to track its multiple heads,
883 # just the history of its first-parent for others that will
884 # be rebasing on top of it
885 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
886 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
887 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
888 rm "$REWRITTEN"/$rev
890 done
893 test -s "$TODO" || echo noop >> "$TODO"
894 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
895 cat >> "$TODO" << EOF
897 # Rebase $SHORTREVISIONS onto $SHORTONTO
899 # Commands:
900 # p, pick = use commit
901 # r, reword = use commit, but edit the commit message
902 # e, edit = use commit, but stop for amending
903 # s, squash = use commit, but meld into previous commit
904 # f, fixup = like "squash", but discard this commit's log message
906 # If you remove a line here THAT COMMIT WILL BE LOST.
907 # However, if you remove everything, the rebase will be aborted.
911 has_action "$TODO" ||
912 die_abort "Nothing to do"
914 cp "$TODO" "$TODO".backup
915 git_editor "$TODO" ||
916 die_abort "Could not execute editor"
918 has_action "$TODO" ||
919 die_abort "Nothing to do"
921 test -d "$REWRITTEN" || skip_unnecessary_picks
923 git update-ref ORIG_HEAD $HEAD
924 output git checkout $ONTO && do_rest
926 esac
927 shift
928 done