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