rebase -i -p: add a helper to add mappings for rewritten commits
[git/dscho.git] / git-rebase--interactive.sh
blob37d87f4e9b48317ebda079afd3a783deb136e1b4
1 #!/bin/sh
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
10 # The original idea comes from Eric W. Biederman, in
11 # http://article.gmane.org/gmane.comp.version-control.git/22407
13 OPTIONS_KEEPDASHDASH=
14 OPTIONS_SPEC="\
15 git-rebase [-i] [options] [--] <upstream> [<branch>]
16 git-rebase [-i] (--continue | --abort | --skip)
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
34 . git-sh-setup
35 require_work_tree
37 DOTEST="$GIT_DIR/rebase-merge"
39 # The file containing rebase commands, comments, and empty lines.
40 # This file is created by "git rebase -i" then edited by the user. As
41 # the lines are processed, they are removed from the front of this
42 # file and written to the tail of $DONE.
43 TODO="$DOTEST"/git-rebase-todo
45 # The rebase command lines that have already been processed. A line
46 # is moved here when it is first handled, before any associated user
47 # actions.
48 DONE="$DOTEST"/done
50 # The commit message that is planned to be used for any changes that
51 # need to be committed following a user interaction.
52 MSG="$DOTEST"/message
54 # The file into which is accumulated the suggested commit message for
55 # squash/fixup commands. When the first of a series of squash/fixups
56 # is seen, the file is created and the commit message from the
57 # previous commit and from the first squash/fixup commit are written
58 # to it. The commit message for each subsequent squash/fixup commit
59 # is appended to the file as it is processed.
61 # The first line of the file is of the form
62 # # This is a combination of $COUNT commits.
63 # where $COUNT is the number of commits whose messages have been
64 # written to the file so far (including the initial "pick" commit).
65 # Each time that a commit message is processed, this line is read and
66 # updated. It is deleted just before the combined commit is made.
67 SQUASH_MSG="$DOTEST"/message-squash
69 # If the current series of squash/fixups has not yet included a squash
70 # command, then this file exists and holds the commit message of the
71 # original "pick" commit. (If the series ends without a "squash"
72 # command, then this can be used as the commit message of the combined
73 # commit without opening the editor.)
74 FIXUP_MSG="$DOTEST"/message-fixup
76 # $REWRITTEN is the name of a directory containing files for each
77 # commit that is reachable by at least one merge base of $HEAD and
78 # $UPSTREAM. They are not necessarily rewritten, but their children
79 # might be. This ensures that commits on merged, but otherwise
80 # unrelated side branches are left alone. (Think "X" in the man page's
81 # example.)
82 REWRITTEN="$DOTEST"/rewritten
84 DROPPED="$DOTEST"/dropped
86 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
87 # GIT_AUTHOR_DATE that will be used for the commit that is currently
88 # being rebased.
89 AUTHOR_SCRIPT="$DOTEST"/author-script
91 # When an "edit" rebase command is being processed, the SHA1 of the
92 # commit to be edited is recorded in this file. When "git rebase
93 # --continue" is executed, if there are any staged changes then they
94 # will be amended to the HEAD commit, but only provided the HEAD
95 # commit is still the commit to be edited. When any other rebase
96 # command is processed, this file is deleted.
97 AMEND="$DOTEST"/amend
99 PRESERVE_MERGES=
100 STRATEGY=
101 ONTO=
102 VERBOSE=
103 OK_TO_SKIP_PRE_REBASE=
104 REBASE_ROOT=
105 AUTOSQUASH=
107 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
108 mark the corrected paths with 'git add <paths>', and
109 run 'git rebase --continue'"
110 export GIT_CHERRY_PICK_HELP
112 warn () {
113 echo "$*" >&2
116 output () {
117 case "$VERBOSE" in
119 output=$("$@" 2>&1 )
120 status=$?
121 test $status != 0 && printf "%s\n" "$output"
122 return $status
125 "$@"
127 esac
130 # Output the commit message for the specified commit.
131 commit_message () {
132 git cat-file commit "$1" | sed "1,/^$/d"
135 run_pre_rebase_hook () {
136 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
137 test -x "$GIT_DIR/hooks/pre-rebase"
138 then
139 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
140 echo >&2 "The pre-rebase hook refused to rebase."
141 exit 1
146 require_clean_work_tree () {
147 # test if working tree is dirty
148 git rev-parse --verify HEAD > /dev/null &&
149 git update-index --ignore-submodules --refresh &&
150 git diff-files --quiet --ignore-submodules &&
151 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
152 die "Working tree is dirty"
155 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
157 comment_for_reflog () {
158 case "$ORIG_REFLOG_ACTION" in
159 ''|rebase*)
160 GIT_REFLOG_ACTION="rebase -i ($1)"
161 export GIT_REFLOG_ACTION
163 esac
166 peek_next_command () {
167 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
170 # expects the original commit name(s) in "$REWRITTEN"/original
171 # records the current HEAD as the rewritten commit
172 add_rewritten () {
173 test ! -d "$REWRITTEN" && return
174 rewritten=$(git rev-parse --verify HEAD) &&
175 for original in $(cat "$REWRITTEN"/original)
177 original=$(git rev-parse --verify "$original") &&
178 echo $rewritten > "$REWRITTEN"/$original || break
179 done &&
180 case "$(peek_next_command)" in
181 squash|s) ;; # do nothing
182 *) rm "$REWRITTEN"/original;;
183 esac ||
184 die "Could not store information about rewritten commit"
187 # if the given commit name ends in an apostrophe, returns the rewritten commit
188 parse_commit () {
189 case "$1" in
190 *\')
191 rewritten=$(git rev-parse --verify ${1%\'}) || die
192 while test -f "$REWRITTEN"/$rewritten -o -f "$DROPPED"/$rewritten
194 test -f "$REWRITTEN"/$rewritten &&
195 new_rewritten=$(cat "$REWRITTEN"/$rewritten) ||
196 new_rewritten=$(cat "$DROPPED"/$rewritten) || break
197 test $rewritten = $new_rewritten && break
198 rewritten=$new_rewritten
199 done
200 echo $rewritten
203 git rev-parse --verify $1 || die
205 esac
208 last_count=
209 mark_action_done () {
210 sed -e 1q < "$TODO" >> "$DONE"
211 sed -e 1d < "$TODO" >> "$TODO".new
212 mv -f "$TODO".new "$TODO"
213 count=$(sane_grep -c '^[^#]' < "$DONE")
214 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
215 if test "$last_count" != "$count"
216 then
217 last_count=$count
218 printf "Rebasing (%d/%d)\r" $count $total
219 test -z "$VERBOSE" || echo
223 make_patch () {
224 sha1_and_parents="$(git rev-list --parents -1 "$1")"
225 case "$sha1_and_parents" in
226 ?*' '?*' '?*)
227 git diff --cc $sha1_and_parents
229 ?*' '?*)
230 git diff-tree -p "$1^!"
233 echo "Root commit"
235 esac > "$DOTEST"/patch
236 test -f "$MSG" ||
237 commit_message "$1" > "$MSG"
238 test -f "$AUTHOR_SCRIPT" ||
239 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
242 die_with_patch () {
243 make_patch "$1"
244 git rerere
245 die "$2"
248 die_abort () {
249 rm -rf "$DOTEST"
250 die "$1"
253 has_action () {
254 sane_grep '^[^#]' "$1" >/dev/null
257 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
258 # GIT_AUTHOR_DATE exported from the current environment.
259 do_with_author () {
261 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
262 "$@"
266 pick_one () {
267 no_ff=
268 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
269 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
270 test -d "$REWRITTEN" &&
271 pick_one_preserving_merges "$@" && return
272 if test -n "$REBASE_ROOT"
273 then
274 output git cherry-pick "$@"
275 return
277 parent_sha1=$(git rev-parse --verify $sha1^) ||
278 die "Could not get the parent of $sha1"
279 current_sha1=$(git rev-parse --verify HEAD)
280 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
281 then
282 output git reset --hard $sha1
283 output warn Fast-forward to $(git rev-parse --short $sha1)
284 else
285 output git cherry-pick "$@"
289 pick_one_preserving_merges () {
290 fast_forward=t
291 case "$1" in
293 fast_forward=f
294 sha1=$2
297 sha1=$1
299 esac
300 sha1=$(git rev-parse $sha1)
302 if test -f "$DOTEST"/current-commit
303 then
304 if test "$fast_forward" = t
305 then
306 cat "$DOTEST"/current-commit | while read current_commit
308 git rev-parse HEAD > "$REWRITTEN"/$current_commit
309 done
310 rm "$DOTEST"/current-commit ||
311 die "Cannot write current commit's replacement sha1"
315 echo $sha1 >> "$DOTEST"/current-commit
317 # rewrite parents; if none were rewritten, we can fast-forward.
318 new_parents=
319 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
320 if test "$pend" = " "
321 then
322 pend=" root"
324 while [ "$pend" != "" ]
326 p=$(expr "$pend" : ' \([^ ]*\)')
327 pend="${pend# $p}"
329 if test -f "$REWRITTEN"/$p
330 then
331 new_p=$(cat "$REWRITTEN"/$p)
333 # If the todo reordered commits, and our parent is marked for
334 # rewriting, but hasn't been gotten to yet, assume the user meant to
335 # drop it on top of the current HEAD
336 if test -z "$new_p"
337 then
338 new_p=$(git rev-parse HEAD)
341 test $p != $new_p && fast_forward=f
342 case "$new_parents" in
343 *$new_p*)
344 ;; # do nothing; that parent is already there
346 new_parents="$new_parents $new_p"
348 esac
349 else
350 if test -f "$DROPPED"/$p
351 then
352 fast_forward=f
353 replacement="$(cat "$DROPPED"/$p)"
354 test -z "$replacement" && replacement=root
355 pend=" $replacement$pend"
356 else
357 new_parents="$new_parents $p"
360 done
361 case $fast_forward in
363 output warn "Fast-forward to $sha1"
364 output git reset --hard $sha1 ||
365 die "Cannot fast-forward to $sha1"
368 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
370 if [ "$1" != "-n" ]
371 then
372 # detach HEAD to current parent
373 output git checkout $first_parent 2> /dev/null ||
374 die "Cannot move HEAD to $first_parent"
377 case "$new_parents" in
378 ' '*' '*)
379 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
381 # redo merge
382 author_script=$(get_author_ident_from_commit $sha1)
383 eval "$author_script"
384 msg="$(commit_message $sha1)"
385 # No point in merging the first parent, that's HEAD
386 new_parents=${new_parents# $first_parent}
387 if ! do_with_author output \
388 git merge $STRATEGY -m "$msg" $new_parents
389 then
390 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
391 die_with_patch $sha1 "Error redoing merge $sha1"
395 output git cherry-pick "$@" ||
396 die_with_patch $sha1 "Could not pick $sha1"
398 esac
400 esac
403 nth_string () {
404 case "$1" in
405 *1[0-9]|*[04-9]) echo "$1"th;;
406 *1) echo "$1"st;;
407 *2) echo "$1"nd;;
408 *3) echo "$1"rd;;
409 esac
412 update_squash_messages () {
413 if test -f "$SQUASH_MSG"; then
414 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
415 COUNT=$(($(sed -n \
416 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
417 -e "q" < "$SQUASH_MSG".bak)+1))
419 echo "# This is a combination of $COUNT commits."
420 sed -e 1d -e '2,/^./{
421 /^$/d
422 }' <"$SQUASH_MSG".bak
423 } >"$SQUASH_MSG"
424 else
425 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
426 COUNT=2
428 echo "# This is a combination of 2 commits."
429 echo "# The first commit's message is:"
430 echo
431 cat "$FIXUP_MSG"
432 } >"$SQUASH_MSG"
434 case $1 in
435 squash)
436 rm -f "$FIXUP_MSG"
437 echo
438 echo "# This is the $(nth_string $COUNT) commit message:"
439 echo
440 commit_message $2
442 fixup)
443 echo
444 echo "# The $(nth_string $COUNT) commit message will be skipped:"
445 echo
446 commit_message $2 | sed -e 's/^/# /'
448 esac >>"$SQUASH_MSG"
451 # A squash/fixup has failed. Prepare the long version of the squash
452 # commit message, then die_with_patch. This code path requires the
453 # user to edit the combined commit message for all commits that have
454 # been squashed/fixedup so far. So also erase the old squash
455 # messages, effectively causing the combined commit to be used as the
456 # new basis for any further squash/fixups. Args: sha1 rest
457 die_failed_squash() {
458 mv "$SQUASH_MSG" "$MSG" || exit
459 rm -f "$FIXUP_MSG"
460 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
461 warn
462 warn "Could not apply $1... $2"
463 die_with_patch $1 ""
466 do_next () {
467 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
468 read command sha1 rest < "$TODO"
469 case "$command" in
470 '#'*|''|noop)
471 mark_action_done
473 pick|p)
474 comment_for_reflog pick
476 mark_action_done
477 pick_one $sha1 ||
478 die_with_patch $sha1 "Could not apply $sha1... $rest"
480 reword|r)
481 comment_for_reflog reword
483 mark_action_done
484 pick_one $sha1 ||
485 die_with_patch $sha1 "Could not apply $sha1... $rest"
486 git commit --amend
488 edit|e)
489 comment_for_reflog edit
491 mark_action_done
492 pick_one $sha1 ||
493 die_with_patch $sha1 "Could not apply $sha1... $rest"
494 make_patch $sha1
495 git rev-parse --verify HEAD > "$AMEND"
496 warn "Stopped at $sha1... $rest"
497 warn "You can amend the commit now, with"
498 warn
499 warn " git commit --amend"
500 warn
501 warn "Once you are satisfied with your changes, run"
502 warn
503 warn " git rebase --continue"
504 warn
505 exit 0
507 squash|s|fixup|f)
508 case "$command" in
509 squash|s)
510 squash_style=squash
512 fixup|f)
513 squash_style=fixup
515 esac
516 comment_for_reflog $squash_style
518 test -f "$DONE" && has_action "$DONE" ||
519 die "Cannot '$squash_style' without a previous commit"
521 mark_action_done
522 update_squash_messages $squash_style $sha1
523 author_script=$(get_author_ident_from_commit HEAD)
524 echo "$author_script" > "$AUTHOR_SCRIPT"
525 eval "$author_script"
526 output git reset --soft HEAD^
527 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
528 case "$(peek_next_command)" in
529 squash|s|fixup|f)
530 # This is an intermediate commit; its message will only be
531 # used in case of trouble. So use the long version:
532 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
533 die_failed_squash $sha1 "$rest"
536 # This is the final command of this squash/fixup group
537 if test -f "$FIXUP_MSG"
538 then
539 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
540 die_failed_squash $sha1 "$rest"
541 else
542 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
543 rm -f "$GIT_DIR"/MERGE_MSG
544 do_with_author git commit --no-verify -e ||
545 die_failed_squash $sha1 "$rest"
547 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
549 esac
551 goto|g)
552 comment_for_reflog goto
553 mark_action_done
554 output git reset --hard $(parse_commit $sha1) ||
555 die "Could not reset to $sha1"
558 warn "Unknown command: $command $sha1 $rest"
559 if git rev-parse --verify -q "$sha1" >/dev/null
560 then
561 die_with_patch $sha1 "Please fix this in the file $TODO."
562 else
563 die "Please fix this in the file $TODO."
566 esac
567 test -s "$TODO" && return
569 comment_for_reflog finish &&
570 HEADNAME=$(cat "$DOTEST"/head-name) &&
571 OLDHEAD=$(cat "$DOTEST"/head) &&
572 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
573 NEWHEAD=$(git rev-parse HEAD) &&
574 case $HEADNAME in
575 refs/*)
576 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
577 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
578 git symbolic-ref HEAD $HEADNAME
580 esac && {
581 test ! -f "$DOTEST"/verbose ||
582 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
583 } &&
584 rm -rf "$DOTEST" &&
585 git gc --auto &&
586 warn "Successfully rebased and updated $HEADNAME."
588 exit
591 do_rest () {
592 while :
594 do_next
595 done
598 # skip picking commits whose parents are unchanged
599 skip_unnecessary_picks () {
600 fd=3
601 while read command sha1 rest
603 # fd=3 means we skip the command
604 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
605 3,pick,"$ONTO"*|3,p,"$ONTO"*)
606 # pick a commit whose parent is current $ONTO -> skip
607 ONTO=$sha1
609 3,#*|3,,*)
610 # copy comments
613 fd=1
615 esac
616 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
617 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
618 mv -f "$TODO".new "$TODO" ||
619 die "Could not skip unnecessary pick commands"
622 # check if no other options are set
623 is_standalone () {
624 test $# -eq 2 -a "$2" = '--' &&
625 test -z "$ONTO" &&
626 test -z "$PRESERVE_MERGES" &&
627 test -z "$STRATEGY" &&
628 test -z "$VERBOSE"
631 get_saved_options () {
632 test -d "$REWRITTEN" && PRESERVE_MERGES=t
633 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
634 test -f "$DOTEST"/verbose && VERBOSE=t
635 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
638 # Rearrange the todo list that has both "pick sha1 msg" and
639 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
640 # comes immediately after the former, and change "pick" to
641 # "fixup"/"squash".
642 rearrange_squash () {
643 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
644 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
645 "$1" >"$1.sq"
646 test -s "$1.sq" || return
648 used=
649 while read pick sha1 message
651 case " $used" in
652 *" $sha1 "*) continue ;;
653 esac
654 echo "$pick $sha1 $message"
655 while read squash action msg
657 case "$message" in
658 "$msg"*)
659 echo "$action $squash $action! $msg"
660 used="$used$squash "
662 esac
663 done <"$1.sq"
664 done >"$1.rearranged" <"$1"
665 cat "$1.rearranged" >"$1"
666 rm -f "$1.sq" "$1.rearranged"
669 LF='
671 parse_onto () {
672 case "$1" in
673 *...*)
674 if left=${1%...*} right=${1#*...} &&
675 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
676 then
677 case "$onto" in
678 ?*"$LF"?* | '')
679 exit 1 ;;
680 esac
681 echo "$onto"
682 exit 0
684 esac
685 git rev-parse --verify "$1^0"
688 while test $# != 0
690 case "$1" in
691 --no-verify)
692 OK_TO_SKIP_PRE_REBASE=yes
694 --verify)
696 --continue)
697 is_standalone "$@" || usage
698 get_saved_options
699 comment_for_reflog continue
701 test -d "$DOTEST" || die "No interactive rebase running"
703 # Sanity check
704 git rev-parse --verify HEAD >/dev/null ||
705 die "Cannot read HEAD"
706 git update-index --ignore-submodules --refresh &&
707 git diff-files --quiet --ignore-submodules ||
708 die "Working tree is dirty"
710 # do we have anything to commit?
711 if git diff-index --cached --quiet --ignore-submodules HEAD --
712 then
713 : Nothing to commit -- skip this
714 else
715 . "$AUTHOR_SCRIPT" ||
716 die "Cannot find the author identity"
717 amend=
718 if test -f "$AMEND"
719 then
720 amend=$(git rev-parse --verify HEAD)
721 test "$amend" = $(cat "$AMEND") ||
722 die "\
723 You have uncommitted changes in your working tree. Please, commit them
724 first and then run 'git rebase --continue' again."
725 git reset --soft HEAD^ ||
726 die "Cannot rewind the HEAD"
728 do_with_author git commit --no-verify -F "$MSG" -e || {
729 test -n "$amend" && git reset --soft $amend
730 die "Could not commit staged changes."
734 require_clean_work_tree
735 do_rest
737 --abort)
738 is_standalone "$@" || usage
739 get_saved_options
740 comment_for_reflog abort
742 git rerere clear
743 test -d "$DOTEST" || die "No interactive rebase running"
745 HEADNAME=$(cat "$DOTEST"/head-name)
746 HEAD=$(cat "$DOTEST"/head)
747 case $HEADNAME in
748 refs/*)
749 git symbolic-ref HEAD $HEADNAME
751 esac &&
752 output git reset --hard $HEAD &&
753 rm -rf "$DOTEST"
754 exit
756 --skip)
757 is_standalone "$@" || usage
758 get_saved_options
759 comment_for_reflog skip
761 git rerere clear
762 test -d "$DOTEST" || die "No interactive rebase running"
764 output git reset --hard && do_rest
767 case "$#,$1" in
768 *,*=*)
769 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
770 1,*)
771 usage ;;
773 STRATEGY="-s $2"
774 shift ;;
775 esac
778 # we use merge anyway
781 VERBOSE=t
784 PRESERVE_MERGES=t
787 # yeah, we know
789 --root)
790 REBASE_ROOT=t
792 --autosquash)
793 AUTOSQUASH=t
795 --onto)
796 shift
797 ONTO=$(parse_onto "$1") ||
798 die "Does not point to a valid commit: $1"
801 shift
802 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
803 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
804 test -d "$DOTEST" &&
805 die "Interactive rebase already started"
807 git var GIT_COMMITTER_IDENT >/dev/null ||
808 die "You need to set your committer info first"
810 if test -z "$REBASE_ROOT"
811 then
812 UPSTREAM_ARG="$1"
813 UPSTREAM=$(git rev-parse --verify "$1") ||
814 die "Invalid base"
815 test -z "$ONTO" && ONTO=$UPSTREAM
816 shift
817 else
818 UPSTREAM=
819 UPSTREAM_ARG=--root
820 test -z "$ONTO" &&
821 die "You must specify --onto when using --root"
822 UPSTREAM=$ONTO
824 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
826 comment_for_reflog start
828 require_clean_work_tree
830 if test ! -z "$1"
831 then
832 output git show-ref --verify --quiet "refs/heads/$1" ||
833 die "Invalid branchname: $1"
834 output git checkout "$1" ||
835 die "Could not checkout $1"
838 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
839 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
841 : > "$DOTEST"/interactive || die "Could not mark as interactive"
842 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
843 echo "detached HEAD" > "$DOTEST"/head-name
845 echo $HEAD > "$DOTEST"/head
846 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
847 echo $ONTO > "$DOTEST"/onto
848 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
849 test t = "$VERBOSE" && : > "$DOTEST"/verbose
850 if test t = "$PRESERVE_MERGES"
851 then
852 if test -z "$REBASE_ROOT"
853 then
854 mkdir "$REWRITTEN" &&
855 for c in $(git merge-base --all $HEAD $UPSTREAM)
857 echo $ONTO > "$REWRITTEN"/$c ||
858 die "Could not init rewritten commits"
859 done
860 else
861 mkdir "$REWRITTEN" &&
862 echo $ONTO > "$REWRITTEN"/root ||
863 die "Could not init rewritten commits"
865 # No cherry-pick because our first pass is to determine
866 # parents to rewrite and skipping dropped commits would
867 # prematurely end our probe
868 MERGES_OPTION=
869 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
870 else
871 MERGES_OPTION="--no-merges --cherry-pick"
874 SHORTHEAD=$(git rev-parse --short $HEAD)
875 SHORTONTO=$(git rev-parse --short $ONTO)
876 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
877 REVISIONS=$UPSTREAM...$HEAD
878 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
879 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
880 --abbrev=7 --reverse --left-right --topo-order \
881 $REVISIONS | \
882 sed -n "s/^>//p" | while read shortsha1 rest
884 if test t != "$PRESERVE_MERGES"
885 then
886 echo "pick $shortsha1 $rest" >> "$TODO"
887 else
888 sha1=$(git rev-parse $shortsha1)
889 if test -z "$REBASE_ROOT"
890 then
891 preserve=t
892 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
894 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
895 then
896 preserve=f
898 done
899 else
900 preserve=f
902 if test f = "$preserve"
903 then
904 touch "$REWRITTEN"/$sha1
905 echo "pick $shortsha1 $rest" >> "$TODO"
908 done
910 # Watch for commits that been dropped by --cherry-pick
911 if test t = "$PRESERVE_MERGES"
912 then
913 mkdir "$DROPPED"
914 # Save all non-cherry-picked changes
915 git rev-list $REVISIONS --left-right --cherry-pick | \
916 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
917 # Now all commits and note which ones are missing in
918 # not-cherry-picks and hence being dropped
919 git rev-list $REVISIONS |
920 while read rev
922 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
923 then
924 # Use -f2 because if rev-list is telling us this commit is
925 # not worthwhile, we don't want to track its multiple heads,
926 # just the history of its first-parent for others that will
927 # be rebasing on top of it
928 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
929 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
930 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
931 rm "$REWRITTEN"/$rev
933 done
936 test -s "$TODO" || echo noop >> "$TODO"
937 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
938 cat >> "$TODO" << EOF
940 # Rebase $SHORTREVISIONS onto $SHORTONTO
942 # Commands:
943 # p, pick = use commit
944 # r, reword = use commit, but edit the commit message
945 # e, edit = use commit, but stop for amending
946 # s, squash = use commit, but meld into previous commit
947 # f, fixup = like "squash", but discard this commit's log message
948 # g, goto = reset the current state to the given commit
950 # If you remove a line here THAT COMMIT WILL BE LOST.
951 # However, if you remove everything, the rebase will be aborted.
955 has_action "$TODO" ||
956 die_abort "Nothing to do"
958 cp "$TODO" "$TODO".backup
959 git_editor "$TODO" ||
960 die_abort "Could not execute editor"
962 has_action "$TODO" ||
963 die_abort "Nothing to do"
965 test -d "$REWRITTEN" || skip_unnecessary_picks
967 git update-ref ORIG_HEAD $HEAD
968 output git checkout $ONTO && do_rest
970 esac
971 shift
972 done