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