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