]> asedeno.scripts.mit.edu Git - git.git/blob - git-rebase--interactive.sh
rebase -i --autosquash: auto-squash commits
[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 "1s/ .*$//p" < "$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 while test $# != 0
556 do
557         case "$1" in
558         --no-verify)
559                 OK_TO_SKIP_PRE_REBASE=yes
560                 ;;
561         --verify)
562                 ;;
563         --continue)
564                 is_standalone "$@" || usage
565                 get_saved_options
566                 comment_for_reflog continue
567
568                 test -d "$DOTEST" || die "No interactive rebase running"
569
570                 # Sanity check
571                 git rev-parse --verify HEAD >/dev/null ||
572                         die "Cannot read HEAD"
573                 git update-index --ignore-submodules --refresh &&
574                         git diff-files --quiet --ignore-submodules ||
575                         die "Working tree is dirty"
576
577                 # do we have anything to commit?
578                 if git diff-index --cached --quiet --ignore-submodules HEAD --
579                 then
580                         : Nothing to commit -- skip this
581                 else
582                         . "$DOTEST"/author-script ||
583                                 die "Cannot find the author identity"
584                         amend=
585                         if test -f "$DOTEST"/amend
586                         then
587                                 amend=$(git rev-parse --verify HEAD)
588                                 test "$amend" = $(cat "$DOTEST"/amend) ||
589                                 die "\
590 You have uncommitted changes in your working tree. Please, commit them
591 first and then run 'git rebase --continue' again."
592                                 git reset --soft HEAD^ ||
593                                 die "Cannot rewind the HEAD"
594                         fi
595                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
596                         git commit --no-verify -F "$DOTEST"/message -e || {
597                                 test -n "$amend" && git reset --soft $amend
598                                 die "Could not commit staged changes."
599                         }
600                 fi
601
602                 require_clean_work_tree
603                 do_rest
604                 ;;
605         --abort)
606                 is_standalone "$@" || usage
607                 get_saved_options
608                 comment_for_reflog abort
609
610                 git rerere clear
611                 test -d "$DOTEST" || die "No interactive rebase running"
612
613                 HEADNAME=$(cat "$DOTEST"/head-name)
614                 HEAD=$(cat "$DOTEST"/head)
615                 case $HEADNAME in
616                 refs/*)
617                         git symbolic-ref HEAD $HEADNAME
618                         ;;
619                 esac &&
620                 output git reset --hard $HEAD &&
621                 rm -rf "$DOTEST"
622                 exit
623                 ;;
624         --skip)
625                 is_standalone "$@" || usage
626                 get_saved_options
627                 comment_for_reflog skip
628
629                 git rerere clear
630                 test -d "$DOTEST" || die "No interactive rebase running"
631
632                 output git reset --hard && do_rest
633                 ;;
634         -s)
635                 case "$#,$1" in
636                 *,*=*)
637                         STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
638                 1,*)
639                         usage ;;
640                 *)
641                         STRATEGY="-s $2"
642                         shift ;;
643                 esac
644                 ;;
645         -m)
646                 # we use merge anyway
647                 ;;
648         -v)
649                 VERBOSE=t
650                 ;;
651         -p)
652                 PRESERVE_MERGES=t
653                 ;;
654         -i)
655                 # yeah, we know
656                 ;;
657         --root)
658                 REBASE_ROOT=t
659                 ;;
660         --autosquash)
661                 AUTOSQUASH=t
662                 ;;
663         --onto)
664                 shift
665                 ONTO=$(git rev-parse --verify "$1") ||
666                         die "Does not point to a valid commit: $1"
667                 ;;
668         --)
669                 shift
670                 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
671                 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
672                 test -d "$DOTEST" &&
673                         die "Interactive rebase already started"
674
675                 git var GIT_COMMITTER_IDENT >/dev/null ||
676                         die "You need to set your committer info first"
677
678                 if test -z "$REBASE_ROOT"
679                 then
680                         UPSTREAM_ARG="$1"
681                         UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
682                         test -z "$ONTO" && ONTO=$UPSTREAM
683                         shift
684                 else
685                         UPSTREAM=
686                         UPSTREAM_ARG=--root
687                         test -z "$ONTO" &&
688                                 die "You must specify --onto when using --root"
689                 fi
690                 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
691
692                 comment_for_reflog start
693
694                 require_clean_work_tree
695
696                 if test ! -z "$1"
697                 then
698                         output git show-ref --verify --quiet "refs/heads/$1" ||
699                                 die "Invalid branchname: $1"
700                         output git checkout "$1" ||
701                                 die "Could not checkout $1"
702                 fi
703
704                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
705                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
706
707                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
708                 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
709                         echo "detached HEAD" > "$DOTEST"/head-name
710
711                 echo $HEAD > "$DOTEST"/head
712                 case "$REBASE_ROOT" in
713                 '')
714                         rm -f "$DOTEST"/rebase-root ;;
715                 *)
716                         : >"$DOTEST"/rebase-root ;;
717                 esac
718                 echo $ONTO > "$DOTEST"/onto
719                 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
720                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
721                 if test t = "$PRESERVE_MERGES"
722                 then
723                         # $REWRITTEN contains files for each commit that is
724                         # reachable by at least one merge base of $HEAD and
725                         # $UPSTREAM. They are not necessarily rewritten, but
726                         # their children might be.
727                         # This ensures that commits on merged, but otherwise
728                         # unrelated side branches are left alone. (Think "X"
729                         # in the man page's example.)
730                         if test -z "$REBASE_ROOT"
731                         then
732                                 mkdir "$REWRITTEN" &&
733                                 for c in $(git merge-base --all $HEAD $UPSTREAM)
734                                 do
735                                         echo $ONTO > "$REWRITTEN"/$c ||
736                                                 die "Could not init rewritten commits"
737                                 done
738                         else
739                                 mkdir "$REWRITTEN" &&
740                                 echo $ONTO > "$REWRITTEN"/root ||
741                                         die "Could not init rewritten commits"
742                         fi
743                         # No cherry-pick because our first pass is to determine
744                         # parents to rewrite and skipping dropped commits would
745                         # prematurely end our probe
746                         MERGES_OPTION=
747                         first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
748                 else
749                         MERGES_OPTION="--no-merges --cherry-pick"
750                 fi
751
752                 SHORTHEAD=$(git rev-parse --short $HEAD)
753                 SHORTONTO=$(git rev-parse --short $ONTO)
754                 if test -z "$REBASE_ROOT"
755                         # this is now equivalent to ! -z "$UPSTREAM"
756                 then
757                         SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
758                         REVISIONS=$UPSTREAM...$HEAD
759                         SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
760                 else
761                         REVISIONS=$ONTO...$HEAD
762                         SHORTREVISIONS=$SHORTHEAD
763                 fi
764                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
765                         --abbrev=7 --reverse --left-right --topo-order \
766                         $REVISIONS | \
767                         sed -n "s/^>//p" | while read shortsha1 rest
768                 do
769                         if test t != "$PRESERVE_MERGES"
770                         then
771                                 echo "pick $shortsha1 $rest" >> "$TODO"
772                         else
773                                 sha1=$(git rev-parse $shortsha1)
774                                 if test -z "$REBASE_ROOT"
775                                 then
776                                         preserve=t
777                                         for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
778                                         do
779                                                 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
780                                                 then
781                                                         preserve=f
782                                                 fi
783                                         done
784                                 else
785                                         preserve=f
786                                 fi
787                                 if test f = "$preserve"
788                                 then
789                                         touch "$REWRITTEN"/$sha1
790                                         echo "pick $shortsha1 $rest" >> "$TODO"
791                                 fi
792                         fi
793                 done
794
795                 # Watch for commits that been dropped by --cherry-pick
796                 if test t = "$PRESERVE_MERGES"
797                 then
798                         mkdir "$DROPPED"
799                         # Save all non-cherry-picked changes
800                         git rev-list $REVISIONS --left-right --cherry-pick | \
801                                 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
802                         # Now all commits and note which ones are missing in
803                         # not-cherry-picks and hence being dropped
804                         git rev-list $REVISIONS |
805                         while read rev
806                         do
807                                 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
808                                 then
809                                         # Use -f2 because if rev-list is telling us this commit is
810                                         # not worthwhile, we don't want to track its multiple heads,
811                                         # just the history of its first-parent for others that will
812                                         # be rebasing on top of it
813                                         git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
814                                         short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
815                                         sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
816                                         rm "$REWRITTEN"/$rev
817                                 fi
818                         done
819                 fi
820
821                 test -s "$TODO" || echo noop >> "$TODO"
822                 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
823                 cat >> "$TODO" << EOF
824
825 # Rebase $SHORTREVISIONS onto $SHORTONTO
826 #
827 # Commands:
828 #  p, pick = use commit
829 #  r, reword = use commit, but edit the commit message
830 #  e, edit = use commit, but stop for amending
831 #  s, squash = use commit, but meld into previous commit
832 #  f, fixup = like "squash", but discard this commit's log message
833 #
834 # If you remove a line here THAT COMMIT WILL BE LOST.
835 # However, if you remove everything, the rebase will be aborted.
836 #
837 EOF
838
839                 has_action "$TODO" ||
840                         die_abort "Nothing to do"
841
842                 cp "$TODO" "$TODO".backup
843                 git_editor "$TODO" ||
844                         die "Could not execute editor"
845
846                 has_action "$TODO" ||
847                         die_abort "Nothing to do"
848
849                 test -d "$REWRITTEN" || skip_unnecessary_picks
850
851                 git update-ref ORIG_HEAD $HEAD
852                 output git checkout $ONTO && do_rest
853                 ;;
854         esac
855         shift
856 done