1#!/bin/sh
2#
3# Copyright (c) 2006 Johannes E. Schindelin
4
5# SHORT DESCRIPTION
6#
7# This script makes it easy to fix up commits in the middle of a series,
8# and rearrange commits.
9#
10# The original idea comes from Eric W. Biederman, in
11# http://article.gmane.org/gmane.comp.version-control.git/22407
12
13OPTIONS_KEEPDASHDASH=
14OPTIONS_SPEC="\
15git-rebase [-i] [options] [--] <upstream> [<branch>]
16git-rebase [-i] (--continue | --abort | --skip)
17--
18 Available options are
19v,verbose display a diffstat of what changed upstream
20onto= rebase onto given branch instead of upstream
21p,preserve-merges try to recreate merges instead of ignoring them
22s,strategy= use the given merge strategy
23m,merge always used (no-op)
24i,interactive always used (no-op)
25 Actions:
26continue continue rebasing process
27abort abort rebasing process and restore original branch
28skip skip current patch and continue rebasing process
29no-verify override pre-rebase hook from stopping the operation
30root rebase all reachable commmits up to the root(s)
31"
32
33. git-sh-setup
34require_work_tree
35
36DOTEST="$GIT_DIR/rebase-merge"
37TODO="$DOTEST"/git-rebase-todo
38DONE="$DOTEST"/done
39MSG="$DOTEST"/message
40SQUASH_MSG="$DOTEST"/message-squash
41REWRITTEN="$DOTEST"/rewritten
42DROPPED="$DOTEST"/dropped
43PRESERVE_MERGES=
44STRATEGY=
45ONTO=
46VERBOSE=
47OK_TO_SKIP_PRE_REBASE=
48REBASE_ROOT=
49
50GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
51mark the corrected paths with 'git add <paths>', and
52run 'git rebase --continue'"
53export GIT_CHERRY_PICK_HELP
54
55warn () {
56 echo "$*" >&2
57}
58
59output () {
60 case "$VERBOSE" in
61 '')
62 output=$("$@" 2>&1 )
63 status=$?
64 test $status != 0 && printf "%s\n" "$output"
65 return $status
66 ;;
67 *)
68 "$@"
69 ;;
70 esac
71}
72
73run_pre_rebase_hook () {
74 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
75 test -x "$GIT_DIR/hooks/pre-rebase"
76 then
77 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
78 echo >&2 "The pre-rebase hook refused to rebase."
79 exit 1
80 }
81 fi
82}
83
84require_clean_work_tree () {
85 # test if working tree is dirty
86 git rev-parse --verify HEAD > /dev/null &&
87 git update-index --ignore-submodules --refresh &&
88 git diff-files --quiet --ignore-submodules &&
89 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
90 die "Working tree is dirty"
91}
92
93ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
94
95comment_for_reflog () {
96 case "$ORIG_REFLOG_ACTION" in
97 ''|rebase*)
98 GIT_REFLOG_ACTION="rebase -i ($1)"
99 export GIT_REFLOG_ACTION
100 ;;
101 esac
102}
103
104last_count=
105mark_action_done () {
106 sed -e 1q < "$TODO" >> "$DONE"
107 sed -e 1d < "$TODO" >> "$TODO".new
108 mv -f "$TODO".new "$TODO"
109 count=$(sane_grep -c '^[^#]' < "$DONE")
110 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
111 if test "$last_count" != "$count"
112 then
113 last_count=$count
114 printf "Rebasing (%d/%d)\r" $count $total
115 test -z "$VERBOSE" || echo
116 fi
117}
118
119make_patch () {
120 sha1_and_parents="$(git rev-list --parents -1 "$1")"
121 case "$sha1_and_parents" in
122 ?*' '?*' '?*)
123 git diff --cc $sha1_and_parents
124 ;;
125 ?*' '?*)
126 git diff-tree -p "$1^!"
127 ;;
128 *)
129 echo "Root commit"
130 ;;
131 esac > "$DOTEST"/patch
132 test -f "$MSG" ||
133 git cat-file commit "$1" | sed "1,/^$/d" > "$MSG"
134 test -f "$DOTEST"/author-script ||
135 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
136}
137
138die_with_patch () {
139 make_patch "$1"
140 git rerere
141 die "$2"
142}
143
144die_abort () {
145 rm -rf "$DOTEST"
146 die "$1"
147}
148
149has_action () {
150 sane_grep '^[^#]' "$1" >/dev/null
151}
152
153pick_one () {
154 no_ff=
155 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
156 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
157 test -d "$REWRITTEN" &&
158 pick_one_preserving_merges "$@" && return
159 if test -n "$REBASE_ROOT"
160 then
161 output git cherry-pick "$@"
162 return
163 fi
164 parent_sha1=$(git rev-parse --verify $sha1^) ||
165 die "Could not get the parent of $sha1"
166 current_sha1=$(git rev-parse --verify HEAD)
167 if test -z "$no_ff" -a "$current_sha1" = "$parent_sha1"
168 then
169 output git reset --hard $sha1
170 output warn Fast-forward to $(git rev-parse --short $sha1)
171 else
172 output git cherry-pick "$@"
173 fi
174}
175
176pick_one_preserving_merges () {
177 fast_forward=t
178 case "$1" in
179 -n)
180 fast_forward=f
181 sha1=$2
182 ;;
183 *)
184 sha1=$1
185 ;;
186 esac
187 sha1=$(git rev-parse $sha1)
188
189 if test -f "$DOTEST"/current-commit
190 then
191 if test "$fast_forward" = t
192 then
193 cat "$DOTEST"/current-commit | while read current_commit
194 do
195 git rev-parse HEAD > "$REWRITTEN"/$current_commit
196 done
197 rm "$DOTEST"/current-commit ||
198 die "Cannot write current commit's replacement sha1"
199 fi
200 fi
201
202 echo $sha1 >> "$DOTEST"/current-commit
203
204 # rewrite parents; if none were rewritten, we can fast-forward.
205 new_parents=
206 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
207 if test "$pend" = " "
208 then
209 pend=" root"
210 fi
211 while [ "$pend" != "" ]
212 do
213 p=$(expr "$pend" : ' \([^ ]*\)')
214 pend="${pend# $p}"
215
216 if test -f "$REWRITTEN"/$p
217 then
218 new_p=$(cat "$REWRITTEN"/$p)
219
220 # If the todo reordered commits, and our parent is marked for
221 # rewriting, but hasn't been gotten to yet, assume the user meant to
222 # drop it on top of the current HEAD
223 if test -z "$new_p"
224 then
225 new_p=$(git rev-parse HEAD)
226 fi
227
228 test $p != $new_p && fast_forward=f
229 case "$new_parents" in
230 *$new_p*)
231 ;; # do nothing; that parent is already there
232 *)
233 new_parents="$new_parents $new_p"
234 ;;
235 esac
236 else
237 if test -f "$DROPPED"/$p
238 then
239 fast_forward=f
240 replacement="$(cat "$DROPPED"/$p)"
241 test -z "$replacement" && replacement=root
242 pend=" $replacement$pend"
243 else
244 new_parents="$new_parents $p"
245 fi
246 fi
247 done
248 case $fast_forward in
249 t)
250 output warn "Fast-forward to $sha1"
251 output git reset --hard $sha1 ||
252 die "Cannot fast-forward to $sha1"
253 ;;
254 f)
255 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
256
257 if [ "$1" != "-n" ]
258 then
259 # detach HEAD to current parent
260 output git checkout $first_parent 2> /dev/null ||
261 die "Cannot move HEAD to $first_parent"
262 fi
263
264 case "$new_parents" in
265 ' '*' '*)
266 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
267
268 # redo merge
269 author_script=$(get_author_ident_from_commit $sha1)
270 eval "$author_script"
271 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
272 # No point in merging the first parent, that's HEAD
273 new_parents=${new_parents# $first_parent}
274 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
275 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
276 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
277 output git merge $STRATEGY -m "$msg" \
278 $new_parents
279 then
280 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
281 die_with_patch $sha1 "Error redoing merge $sha1"
282 fi
283 ;;
284 *)
285 output git cherry-pick "$@" ||
286 die_with_patch $sha1 "Could not pick $sha1"
287 ;;
288 esac
289 ;;
290 esac
291}
292
293nth_string () {
294 case "$1" in
295 *1[0-9]|*[04-9]) echo "$1"th;;
296 *1) echo "$1"st;;
297 *2) echo "$1"nd;;
298 *3) echo "$1"rd;;
299 esac
300}
301
302make_squash_message () {
303 if test -f "$SQUASH_MSG"; then
304 # We want to be careful about matching only the commit
305 # message comment lines generated by this function.
306 # "[snrt][tdh]" matches the nth_string endings.
307 COUNT=$(($(sed -n "s/^# Th[^0-9]*\([1-9][0-9]*\)[snrt][tdh] commit message.*:/\1/p" \
308 < "$SQUASH_MSG" | sed -ne '$p')+1))
309 echo "# This is a combination of $COUNT commits."
310 sed -e 1d -e '2,/^./{
311 /^$/d
312 }' <"$SQUASH_MSG"
313 else
314 COUNT=2
315 echo "# This is a combination of two commits."
316 echo "# The first commit's message is:"
317 echo
318 git cat-file commit HEAD | sed -e '1,/^$/d'
319 fi
320 case $1 in
321 squash)
322 echo
323 echo "# This is the $(nth_string $COUNT) commit message:"
324 echo
325 git cat-file commit $2 | sed -e '1,/^$/d'
326 ;;
327 fixup)
328 echo
329 echo "# The $(nth_string $COUNT) commit message will be skipped:"
330 echo
331 # Comment the lines of the commit message out using
332 # "# " rather than "# " to make them less likely to
333 # confuse the sed regexp above.
334 git cat-file commit $2 | sed -e '1,/^$/d' -e 's/^/# /'
335 ;;
336 esac
337}
338
339peek_next_command () {
340 sed -n -e "/^#/d" -e "/^$/d" -e "s/ .*//p" -e "q" < "$TODO"
341}
342
343do_next () {
344 rm -f "$MSG" "$DOTEST"/author-script \
345 "$DOTEST"/amend || exit
346 read command sha1 rest < "$TODO"
347 case "$command" in
348 '#'*|''|noop)
349 mark_action_done
350 ;;
351 pick|p)
352 comment_for_reflog pick
353
354 mark_action_done
355 pick_one $sha1 ||
356 die_with_patch $sha1 "Could not apply $sha1... $rest"
357 ;;
358 reword|r)
359 comment_for_reflog reword
360
361 mark_action_done
362 pick_one $sha1 ||
363 die_with_patch $sha1 "Could not apply $sha1... $rest"
364 git commit --amend
365 ;;
366 edit|e)
367 comment_for_reflog edit
368
369 mark_action_done
370 pick_one $sha1 ||
371 die_with_patch $sha1 "Could not apply $sha1... $rest"
372 make_patch $sha1
373 git rev-parse --verify HEAD > "$DOTEST"/amend
374 warn "Stopped at $sha1... $rest"
375 warn "You can amend the commit now, with"
376 warn
377 warn " git commit --amend"
378 warn
379 warn "Once you are satisfied with your changes, run"
380 warn
381 warn " git rebase --continue"
382 warn
383 exit 0
384 ;;
385 squash|s|fixup|f)
386 case "$command" in
387 squash|s)
388 squash_style=squash
389 ;;
390 fixup|f)
391 squash_style=fixup
392 ;;
393 esac
394 comment_for_reflog $squash_style
395
396 test -f "$DONE" && has_action "$DONE" ||
397 die "Cannot '$squash_style' without a previous commit"
398
399 mark_action_done
400 make_squash_message $squash_style $sha1 > "$MSG"
401 failed=f
402 author_script=$(get_author_ident_from_commit HEAD)
403 output git reset --soft HEAD^
404 pick_one -n $sha1 || failed=t
405 case "$(peek_next_command)" in
406 squash|s|fixup|f)
407 USE_OUTPUT=output
408 MSG_OPT=-F
409 EDIT_OR_FILE="$MSG"
410 cp "$MSG" "$SQUASH_MSG"
411 ;;
412 *)
413 USE_OUTPUT=
414 MSG_OPT=
415 EDIT_OR_FILE=-e
416 rm -f "$SQUASH_MSG" || exit
417 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
418 rm -f "$GIT_DIR"/MERGE_MSG || exit
419 ;;
420 esac
421 echo "$author_script" > "$DOTEST"/author-script
422 if test $failed = f
423 then
424 # This is like --amend, but with a different message
425 eval "$author_script"
426 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
427 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
428 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
429 $USE_OUTPUT git commit --no-verify \
430 $MSG_OPT "$EDIT_OR_FILE" || failed=t
431 fi
432 if test $failed = t
433 then
434 cp "$MSG" "$GIT_DIR"/MERGE_MSG
435 warn
436 warn "Could not apply $sha1... $rest"
437 die_with_patch $sha1 ""
438 fi
439 ;;
440 *)
441 warn "Unknown command: $command $sha1 $rest"
442 if git rev-parse --verify -q "$sha1" >/dev/null
443 then
444 die_with_patch $sha1 "Please fix this in the file $TODO."
445 else
446 die "Please fix this in the file $TODO."
447 fi
448 ;;
449 esac
450 test -s "$TODO" && return
451
452 comment_for_reflog finish &&
453 HEADNAME=$(cat "$DOTEST"/head-name) &&
454 OLDHEAD=$(cat "$DOTEST"/head) &&
455 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
456 NEWHEAD=$(git rev-parse HEAD) &&
457 case $HEADNAME in
458 refs/*)
459 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
460 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
461 git symbolic-ref HEAD $HEADNAME
462 ;;
463 esac && {
464 test ! -f "$DOTEST"/verbose ||
465 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
466 } &&
467 rm -rf "$DOTEST" &&
468 git gc --auto &&
469 warn "Successfully rebased and updated $HEADNAME."
470
471 exit
472}
473
474do_rest () {
475 while :
476 do
477 do_next
478 done
479}
480
481# skip picking commits whose parents are unchanged
482skip_unnecessary_picks () {
483 fd=3
484 while read command sha1 rest
485 do
486 # fd=3 means we skip the command
487 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
488 3,pick,"$ONTO"*|3,p,"$ONTO"*)
489 # pick a commit whose parent is current $ONTO -> skip
490 ONTO=$sha1
491 ;;
492 3,#*|3,,*)
493 # copy comments
494 ;;
495 *)
496 fd=1
497 ;;
498 esac
499 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
500 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
501 mv -f "$TODO".new "$TODO" ||
502 die "Could not skip unnecessary pick commands"
503}
504
505# check if no other options are set
506is_standalone () {
507 test $# -eq 2 -a "$2" = '--' &&
508 test -z "$ONTO" &&
509 test -z "$PRESERVE_MERGES" &&
510 test -z "$STRATEGY" &&
511 test -z "$VERBOSE"
512}
513
514get_saved_options () {
515 test -d "$REWRITTEN" && PRESERVE_MERGES=t
516 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
517 test -f "$DOTEST"/verbose && VERBOSE=t
518 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
519}
520
521while test $# != 0
522do
523 case "$1" in
524 --no-verify)
525 OK_TO_SKIP_PRE_REBASE=yes
526 ;;
527 --verify)
528 ;;
529 --continue)
530 is_standalone "$@" || usage
531 get_saved_options
532 comment_for_reflog continue
533
534 test -d "$DOTEST" || die "No interactive rebase running"
535
536 # Sanity check
537 git rev-parse --verify HEAD >/dev/null ||
538 die "Cannot read HEAD"
539 git update-index --ignore-submodules --refresh &&
540 git diff-files --quiet --ignore-submodules ||
541 die "Working tree is dirty"
542
543 # do we have anything to commit?
544 if git diff-index --cached --quiet --ignore-submodules HEAD --
545 then
546 : Nothing to commit -- skip this
547 else
548 . "$DOTEST"/author-script ||
549 die "Cannot find the author identity"
550 amend=
551 if test -f "$DOTEST"/amend
552 then
553 amend=$(git rev-parse --verify HEAD)
554 test "$amend" = $(cat "$DOTEST"/amend) ||
555 die "\
556You have uncommitted changes in your working tree. Please, commit them
557first and then run 'git rebase --continue' again."
558 git reset --soft HEAD^ ||
559 die "Cannot rewind the HEAD"
560 fi
561 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
562 git commit --no-verify -F "$MSG" -e || {
563 test -n "$amend" && git reset --soft $amend
564 die "Could not commit staged changes."
565 }
566 fi
567
568 require_clean_work_tree
569 do_rest
570 ;;
571 --abort)
572 is_standalone "$@" || usage
573 get_saved_options
574 comment_for_reflog abort
575
576 git rerere clear
577 test -d "$DOTEST" || die "No interactive rebase running"
578
579 HEADNAME=$(cat "$DOTEST"/head-name)
580 HEAD=$(cat "$DOTEST"/head)
581 case $HEADNAME in
582 refs/*)
583 git symbolic-ref HEAD $HEADNAME
584 ;;
585 esac &&
586 output git reset --hard $HEAD &&
587 rm -rf "$DOTEST"
588 exit
589 ;;
590 --skip)
591 is_standalone "$@" || usage
592 get_saved_options
593 comment_for_reflog skip
594
595 git rerere clear
596 test -d "$DOTEST" || die "No interactive rebase running"
597
598 output git reset --hard && do_rest
599 ;;
600 -s)
601 case "$#,$1" in
602 *,*=*)
603 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
604 1,*)
605 usage ;;
606 *)
607 STRATEGY="-s $2"
608 shift ;;
609 esac
610 ;;
611 -m)
612 # we use merge anyway
613 ;;
614 -v)
615 VERBOSE=t
616 ;;
617 -p)
618 PRESERVE_MERGES=t
619 ;;
620 -i)
621 # yeah, we know
622 ;;
623 --root)
624 REBASE_ROOT=t
625 ;;
626 --onto)
627 shift
628 ONTO=$(git rev-parse --verify "$1") ||
629 die "Does not point to a valid commit: $1"
630 ;;
631 --)
632 shift
633 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
634 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
635 test -d "$DOTEST" &&
636 die "Interactive rebase already started"
637
638 git var GIT_COMMITTER_IDENT >/dev/null ||
639 die "You need to set your committer info first"
640
641 if test -z "$REBASE_ROOT"
642 then
643 UPSTREAM_ARG="$1"
644 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
645 test -z "$ONTO" && ONTO=$UPSTREAM
646 shift
647 else
648 UPSTREAM=
649 UPSTREAM_ARG=--root
650 test -z "$ONTO" &&
651 die "You must specify --onto when using --root"
652 fi
653 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
654
655 comment_for_reflog start
656
657 require_clean_work_tree
658
659 if test ! -z "$1"
660 then
661 output git show-ref --verify --quiet "refs/heads/$1" ||
662 die "Invalid branchname: $1"
663 output git checkout "$1" ||
664 die "Could not checkout $1"
665 fi
666
667 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
668 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
669
670 : > "$DOTEST"/interactive || die "Could not mark as interactive"
671 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
672 echo "detached HEAD" > "$DOTEST"/head-name
673
674 echo $HEAD > "$DOTEST"/head
675 case "$REBASE_ROOT" in
676 '')
677 rm -f "$DOTEST"/rebase-root ;;
678 *)
679 : >"$DOTEST"/rebase-root ;;
680 esac
681 echo $ONTO > "$DOTEST"/onto
682 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
683 test t = "$VERBOSE" && : > "$DOTEST"/verbose
684 if test t = "$PRESERVE_MERGES"
685 then
686 # $REWRITTEN contains files for each commit that is
687 # reachable by at least one merge base of $HEAD and
688 # $UPSTREAM. They are not necessarily rewritten, but
689 # their children might be.
690 # This ensures that commits on merged, but otherwise
691 # unrelated side branches are left alone. (Think "X"
692 # in the man page's example.)
693 if test -z "$REBASE_ROOT"
694 then
695 mkdir "$REWRITTEN" &&
696 for c in $(git merge-base --all $HEAD $UPSTREAM)
697 do
698 echo $ONTO > "$REWRITTEN"/$c ||
699 die "Could not init rewritten commits"
700 done
701 else
702 mkdir "$REWRITTEN" &&
703 echo $ONTO > "$REWRITTEN"/root ||
704 die "Could not init rewritten commits"
705 fi
706 # No cherry-pick because our first pass is to determine
707 # parents to rewrite and skipping dropped commits would
708 # prematurely end our probe
709 MERGES_OPTION=
710 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
711 else
712 MERGES_OPTION="--no-merges --cherry-pick"
713 fi
714
715 SHORTHEAD=$(git rev-parse --short $HEAD)
716 SHORTONTO=$(git rev-parse --short $ONTO)
717 if test -z "$REBASE_ROOT"
718 # this is now equivalent to ! -z "$UPSTREAM"
719 then
720 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
721 REVISIONS=$UPSTREAM...$HEAD
722 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
723 else
724 REVISIONS=$ONTO...$HEAD
725 SHORTREVISIONS=$SHORTHEAD
726 fi
727 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
728 --abbrev=7 --reverse --left-right --topo-order \
729 $REVISIONS | \
730 sed -n "s/^>//p" | while read shortsha1 rest
731 do
732 if test t != "$PRESERVE_MERGES"
733 then
734 echo "pick $shortsha1 $rest" >> "$TODO"
735 else
736 sha1=$(git rev-parse $shortsha1)
737 if test -z "$REBASE_ROOT"
738 then
739 preserve=t
740 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
741 do
742 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
743 then
744 preserve=f
745 fi
746 done
747 else
748 preserve=f
749 fi
750 if test f = "$preserve"
751 then
752 touch "$REWRITTEN"/$sha1
753 echo "pick $shortsha1 $rest" >> "$TODO"
754 fi
755 fi
756 done
757
758 # Watch for commits that been dropped by --cherry-pick
759 if test t = "$PRESERVE_MERGES"
760 then
761 mkdir "$DROPPED"
762 # Save all non-cherry-picked changes
763 git rev-list $REVISIONS --left-right --cherry-pick | \
764 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
765 # Now all commits and note which ones are missing in
766 # not-cherry-picks and hence being dropped
767 git rev-list $REVISIONS |
768 while read rev
769 do
770 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
771 then
772 # Use -f2 because if rev-list is telling us this commit is
773 # not worthwhile, we don't want to track its multiple heads,
774 # just the history of its first-parent for others that will
775 # be rebasing on top of it
776 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
777 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
778 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
779 rm "$REWRITTEN"/$rev
780 fi
781 done
782 fi
783
784 test -s "$TODO" || echo noop >> "$TODO"
785 cat >> "$TODO" << EOF
786
787# Rebase $SHORTREVISIONS onto $SHORTONTO
788#
789# Commands:
790# p, pick = use commit
791# r, reword = use commit, but edit the commit message
792# e, edit = use commit, but stop for amending
793# s, squash = use commit, but meld into previous commit
794# f, fixup = like "squash", but discard this commit's log message
795#
796# If you remove a line here THAT COMMIT WILL BE LOST.
797# However, if you remove everything, the rebase will be aborted.
798#
799EOF
800
801 has_action "$TODO" ||
802 die_abort "Nothing to do"
803
804 cp "$TODO" "$TODO".backup
805 git_editor "$TODO" ||
806 die "Could not execute editor"
807
808 has_action "$TODO" ||
809 die_abort "Nothing to do"
810
811 test -d "$REWRITTEN" || skip_unnecessary_picks
812
813 git update-ref ORIG_HEAD $HEAD
814 output git checkout $ONTO && do_rest
815 ;;
816 esac
817 shift
818done