rebase -i -p: Add the "merge" command
[git/dscho.git] / git-rebase--interactive.sh
blobc3ea09c653c310b8a00d3dfb9d4f81cccce589e4
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 merge_one () {
290 cmd="merge $*"
291 test "$1" = parents && shift
292 parents=
293 while test "original" != "$1"
295 parents="$parents $(parse_commit $1)"
296 shift
297 done
299 test "original" != "$1" &&
300 die "Could not determine original merge commit from $cmd"
302 sha1=$2; shift; shift
304 # the command was "merge parents ...", so "parents" was recorded
305 # TODO: detect non-fast-forwards properly
306 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
307 git merge $parents &&
308 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
309 then
310 git commit --amend -m "$*" &&
311 echo "$sha1" > "$REWRITTEN"/original &&
312 add_rewritten
313 fi ||
314 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
317 pick_one_preserving_merges () {
318 fast_forward=t
319 case "$1" in
321 fast_forward=f
322 sha1=$2
325 sha1=$1
327 esac
328 sha1=$(git rev-parse $sha1)
330 if test -f "$DOTEST"/current-commit
331 then
332 if test "$fast_forward" = t
333 then
334 cat "$DOTEST"/current-commit | while read current_commit
336 git rev-parse HEAD > "$REWRITTEN"/$current_commit
337 done
338 rm "$DOTEST"/current-commit ||
339 die "Cannot write current commit's replacement sha1"
343 echo $sha1 >> "$DOTEST"/current-commit
345 # rewrite parents; if none were rewritten, we can fast-forward.
346 new_parents=
347 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
348 if test "$pend" = " "
349 then
350 pend=" root"
352 while [ "$pend" != "" ]
354 p=$(expr "$pend" : ' \([^ ]*\)')
355 pend="${pend# $p}"
357 if test -f "$REWRITTEN"/$p
358 then
359 new_p=$(cat "$REWRITTEN"/$p)
361 # If the todo reordered commits, and our parent is marked for
362 # rewriting, but hasn't been gotten to yet, assume the user meant to
363 # drop it on top of the current HEAD
364 if test -z "$new_p"
365 then
366 new_p=$(git rev-parse HEAD)
369 test $p != $new_p && fast_forward=f
370 case "$new_parents" in
371 *$new_p*)
372 ;; # do nothing; that parent is already there
374 new_parents="$new_parents $new_p"
376 esac
377 else
378 if test -f "$DROPPED"/$p
379 then
380 fast_forward=f
381 replacement="$(cat "$DROPPED"/$p)"
382 test -z "$replacement" && replacement=root
383 pend=" $replacement$pend"
384 else
385 new_parents="$new_parents $p"
388 done
389 case $fast_forward in
391 output warn "Fast-forward to $sha1"
392 output git reset --hard $sha1 ||
393 die "Cannot fast-forward to $sha1"
396 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
398 if [ "$1" != "-n" ]
399 then
400 # detach HEAD to current parent
401 output git checkout $first_parent 2> /dev/null ||
402 die "Cannot move HEAD to $first_parent"
405 case "$new_parents" in
406 ' '*' '*)
407 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
409 # redo merge
410 author_script=$(get_author_ident_from_commit $sha1)
411 eval "$author_script"
412 msg="$(commit_message $sha1)"
413 # No point in merging the first parent, that's HEAD
414 new_parents=${new_parents# $first_parent}
415 if ! do_with_author output \
416 git merge $STRATEGY -m "$msg" $new_parents
417 then
418 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
419 die_with_patch $sha1 "Error redoing merge $sha1"
423 output git cherry-pick "$@" ||
424 die_with_patch $sha1 "Could not pick $sha1"
426 esac
428 esac
431 nth_string () {
432 case "$1" in
433 *1[0-9]|*[04-9]) echo "$1"th;;
434 *1) echo "$1"st;;
435 *2) echo "$1"nd;;
436 *3) echo "$1"rd;;
437 esac
440 update_squash_messages () {
441 if test -f "$SQUASH_MSG"; then
442 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
443 COUNT=$(($(sed -n \
444 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
445 -e "q" < "$SQUASH_MSG".bak)+1))
447 echo "# This is a combination of $COUNT commits."
448 sed -e 1d -e '2,/^./{
449 /^$/d
450 }' <"$SQUASH_MSG".bak
451 } >"$SQUASH_MSG"
452 else
453 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
454 COUNT=2
456 echo "# This is a combination of 2 commits."
457 echo "# The first commit's message is:"
458 echo
459 cat "$FIXUP_MSG"
460 } >"$SQUASH_MSG"
462 case $1 in
463 squash)
464 rm -f "$FIXUP_MSG"
465 echo
466 echo "# This is the $(nth_string $COUNT) commit message:"
467 echo
468 commit_message $2
470 fixup)
471 echo
472 echo "# The $(nth_string $COUNT) commit message will be skipped:"
473 echo
474 commit_message $2 | sed -e 's/^/# /'
476 esac >>"$SQUASH_MSG"
479 # A squash/fixup has failed. Prepare the long version of the squash
480 # commit message, then die_with_patch. This code path requires the
481 # user to edit the combined commit message for all commits that have
482 # been squashed/fixedup so far. So also erase the old squash
483 # messages, effectively causing the combined commit to be used as the
484 # new basis for any further squash/fixups. Args: sha1 rest
485 die_failed_squash() {
486 mv "$SQUASH_MSG" "$MSG" || exit
487 rm -f "$FIXUP_MSG"
488 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
489 warn
490 warn "Could not apply $1... $2"
491 die_with_patch $1 ""
494 do_next () {
495 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
496 read command sha1 rest < "$TODO"
497 case "$command" in
498 '#'*|''|noop)
499 mark_action_done
501 pick|p)
502 comment_for_reflog pick
504 mark_action_done
505 pick_one $sha1 ||
506 die_with_patch $sha1 "Could not apply $sha1... $rest"
508 reword|r)
509 comment_for_reflog reword
511 mark_action_done
512 pick_one $sha1 ||
513 die_with_patch $sha1 "Could not apply $sha1... $rest"
514 git commit --amend
516 edit|e)
517 comment_for_reflog edit
519 mark_action_done
520 pick_one $sha1 ||
521 die_with_patch $sha1 "Could not apply $sha1... $rest"
522 make_patch $sha1
523 git rev-parse --verify HEAD > "$AMEND"
524 warn "Stopped at $sha1... $rest"
525 warn "You can amend the commit now, with"
526 warn
527 warn " git commit --amend"
528 warn
529 warn "Once you are satisfied with your changes, run"
530 warn
531 warn " git rebase --continue"
532 warn
533 exit 0
535 squash|s|fixup|f)
536 case "$command" in
537 squash|s)
538 squash_style=squash
540 fixup|f)
541 squash_style=fixup
543 esac
544 comment_for_reflog $squash_style
546 test -f "$DONE" && has_action "$DONE" ||
547 die "Cannot '$squash_style' without a previous commit"
549 mark_action_done
550 update_squash_messages $squash_style $sha1
551 author_script=$(get_author_ident_from_commit HEAD)
552 echo "$author_script" > "$AUTHOR_SCRIPT"
553 eval "$author_script"
554 output git reset --soft HEAD^
555 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
556 case "$(peek_next_command)" in
557 squash|s|fixup|f)
558 # This is an intermediate commit; its message will only be
559 # used in case of trouble. So use the long version:
560 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
561 die_failed_squash $sha1 "$rest"
564 # This is the final command of this squash/fixup group
565 if test -f "$FIXUP_MSG"
566 then
567 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
568 die_failed_squash $sha1 "$rest"
569 else
570 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
571 rm -f "$GIT_DIR"/MERGE_MSG
572 do_with_author git commit --no-verify -e ||
573 die_failed_squash $sha1 "$rest"
575 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
577 esac
579 goto|g)
580 comment_for_reflog goto
581 mark_action_done
582 output git reset --hard $(parse_commit $sha1) ||
583 die "Could not reset to $sha1"
585 merge|m)
586 comment_for_reflog merge
587 mark_action_done
588 # this already dies with patch on error
589 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
590 exit
593 warn "Unknown command: $command $sha1 $rest"
594 if git rev-parse --verify -q "$sha1" >/dev/null
595 then
596 die_with_patch $sha1 "Please fix this in the file $TODO."
597 else
598 die "Please fix this in the file $TODO."
601 esac
602 test -s "$TODO" && return
604 comment_for_reflog finish &&
605 HEADNAME=$(cat "$DOTEST"/head-name) &&
606 OLDHEAD=$(cat "$DOTEST"/head) &&
607 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
608 NEWHEAD=$(git rev-parse HEAD) &&
609 case $HEADNAME in
610 refs/*)
611 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
612 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
613 git symbolic-ref HEAD $HEADNAME
615 esac && {
616 test ! -f "$DOTEST"/verbose ||
617 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
618 } &&
619 rm -rf "$DOTEST" &&
620 git gc --auto &&
621 warn "Successfully rebased and updated $HEADNAME."
623 exit
626 do_rest () {
627 while :
629 do_next
630 done
633 # skip picking commits whose parents are unchanged
634 skip_unnecessary_picks () {
635 fd=3
636 while read command sha1 rest
638 # fd=3 means we skip the command
639 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
640 3,pick,"$ONTO"*|3,p,"$ONTO"*)
641 # pick a commit whose parent is current $ONTO -> skip
642 ONTO=$sha1
644 3,#*|3,,*)
645 # copy comments
648 fd=1
650 esac
651 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
652 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
653 mv -f "$TODO".new "$TODO" ||
654 die "Could not skip unnecessary pick commands"
657 # check if no other options are set
658 is_standalone () {
659 test $# -eq 2 -a "$2" = '--' &&
660 test -z "$ONTO" &&
661 test -z "$PRESERVE_MERGES" &&
662 test -z "$STRATEGY" &&
663 test -z "$VERBOSE"
666 get_saved_options () {
667 test -d "$REWRITTEN" && PRESERVE_MERGES=t
668 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
669 test -f "$DOTEST"/verbose && VERBOSE=t
670 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
673 # Rearrange the todo list that has both "pick sha1 msg" and
674 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
675 # comes immediately after the former, and change "pick" to
676 # "fixup"/"squash".
677 rearrange_squash () {
678 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
679 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
680 "$1" >"$1.sq"
681 test -s "$1.sq" || return
683 used=
684 while read pick sha1 message
686 case " $used" in
687 *" $sha1 "*) continue ;;
688 esac
689 echo "$pick $sha1 $message"
690 while read squash action msg
692 case "$message" in
693 "$msg"*)
694 echo "$action $squash $action! $msg"
695 used="$used$squash "
697 esac
698 done <"$1.sq"
699 done >"$1.rearranged" <"$1"
700 cat "$1.rearranged" >"$1"
701 rm -f "$1.sq" "$1.rearranged"
704 LF='
706 parse_onto () {
707 case "$1" in
708 *...*)
709 if left=${1%...*} right=${1#*...} &&
710 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
711 then
712 case "$onto" in
713 ?*"$LF"?* | '')
714 exit 1 ;;
715 esac
716 echo "$onto"
717 exit 0
719 esac
720 git rev-parse --verify "$1^0"
723 while test $# != 0
725 case "$1" in
726 --no-verify)
727 OK_TO_SKIP_PRE_REBASE=yes
729 --verify)
731 --continue)
732 is_standalone "$@" || usage
733 get_saved_options
734 comment_for_reflog continue
736 test -d "$DOTEST" || die "No interactive rebase running"
738 # Sanity check
739 git rev-parse --verify HEAD >/dev/null ||
740 die "Cannot read HEAD"
741 git update-index --ignore-submodules --refresh &&
742 git diff-files --quiet --ignore-submodules ||
743 die "Working tree is dirty"
745 # do we have anything to commit?
746 if git diff-index --cached --quiet --ignore-submodules HEAD --
747 then
748 : Nothing to commit -- skip this
749 else
750 . "$AUTHOR_SCRIPT" ||
751 die "Cannot find the author identity"
752 amend=
753 if test -f "$AMEND"
754 then
755 amend=$(git rev-parse --verify HEAD)
756 test "$amend" = $(cat "$AMEND") ||
757 die "\
758 You have uncommitted changes in your working tree. Please, commit them
759 first and then run 'git rebase --continue' again."
760 git reset --soft HEAD^ ||
761 die "Cannot rewind the HEAD"
763 do_with_author git commit --no-verify -F "$MSG" -e || {
764 test -n "$amend" && git reset --soft $amend
765 die "Could not commit staged changes."
769 require_clean_work_tree
770 do_rest
772 --abort)
773 is_standalone "$@" || usage
774 get_saved_options
775 comment_for_reflog abort
777 git rerere clear
778 test -d "$DOTEST" || die "No interactive rebase running"
780 HEADNAME=$(cat "$DOTEST"/head-name)
781 HEAD=$(cat "$DOTEST"/head)
782 case $HEADNAME in
783 refs/*)
784 git symbolic-ref HEAD $HEADNAME
786 esac &&
787 output git reset --hard $HEAD &&
788 rm -rf "$DOTEST"
789 exit
791 --skip)
792 is_standalone "$@" || usage
793 get_saved_options
794 comment_for_reflog skip
796 git rerere clear
797 test -d "$DOTEST" || die "No interactive rebase running"
799 output git reset --hard && do_rest
802 case "$#,$1" in
803 *,*=*)
804 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
805 1,*)
806 usage ;;
808 STRATEGY="-s $2"
809 shift ;;
810 esac
813 # we use merge anyway
816 VERBOSE=t
819 PRESERVE_MERGES=t
822 # yeah, we know
824 --root)
825 REBASE_ROOT=t
827 --autosquash)
828 AUTOSQUASH=t
830 --onto)
831 shift
832 ONTO=$(parse_onto "$1") ||
833 die "Does not point to a valid commit: $1"
836 shift
837 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
838 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
839 test -d "$DOTEST" &&
840 die "Interactive rebase already started"
842 git var GIT_COMMITTER_IDENT >/dev/null ||
843 die "You need to set your committer info first"
845 if test -z "$REBASE_ROOT"
846 then
847 UPSTREAM_ARG="$1"
848 UPSTREAM=$(git rev-parse --verify "$1") ||
849 die "Invalid base"
850 test -z "$ONTO" && ONTO=$UPSTREAM
851 shift
852 else
853 UPSTREAM=
854 UPSTREAM_ARG=--root
855 test -z "$ONTO" &&
856 die "You must specify --onto when using --root"
857 UPSTREAM=$ONTO
859 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
861 comment_for_reflog start
863 require_clean_work_tree
865 if test ! -z "$1"
866 then
867 output git show-ref --verify --quiet "refs/heads/$1" ||
868 die "Invalid branchname: $1"
869 output git checkout "$1" ||
870 die "Could not checkout $1"
873 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
874 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
876 : > "$DOTEST"/interactive || die "Could not mark as interactive"
877 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
878 echo "detached HEAD" > "$DOTEST"/head-name
880 echo $HEAD > "$DOTEST"/head
881 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
882 echo $ONTO > "$DOTEST"/onto
883 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
884 test t = "$VERBOSE" && : > "$DOTEST"/verbose
885 if test t = "$PRESERVE_MERGES"
886 then
887 if test -z "$REBASE_ROOT"
888 then
889 mkdir "$REWRITTEN" &&
890 for c in $(git merge-base --all $HEAD $UPSTREAM)
892 echo $ONTO > "$REWRITTEN"/$c ||
893 die "Could not init rewritten commits"
894 done
895 else
896 mkdir "$REWRITTEN" &&
897 echo $ONTO > "$REWRITTEN"/root ||
898 die "Could not init rewritten commits"
900 # No cherry-pick because our first pass is to determine
901 # parents to rewrite and skipping dropped commits would
902 # prematurely end our probe
903 MERGES_OPTION=
904 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
905 else
906 MERGES_OPTION="--no-merges --cherry-pick"
909 SHORTHEAD=$(git rev-parse --short $HEAD)
910 SHORTONTO=$(git rev-parse --short $ONTO)
911 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
912 REVISIONS=$UPSTREAM...$HEAD
913 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
914 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
915 --abbrev=7 --reverse --left-right --topo-order \
916 $REVISIONS | \
917 sed -n "s/^>//p" | while read shortsha1 rest
919 if test t != "$PRESERVE_MERGES"
920 then
921 echo "pick $shortsha1 $rest" >> "$TODO"
922 else
923 sha1=$(git rev-parse $shortsha1)
924 if test -z "$REBASE_ROOT"
925 then
926 preserve=t
927 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
929 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
930 then
931 preserve=f
933 done
934 else
935 preserve=f
937 if test f = "$preserve"
938 then
939 touch "$REWRITTEN"/$sha1
940 echo "pick $shortsha1 $rest" >> "$TODO"
943 done
945 # Watch for commits that been dropped by --cherry-pick
946 if test t = "$PRESERVE_MERGES"
947 then
948 mkdir "$DROPPED"
949 # Save all non-cherry-picked changes
950 git rev-list $REVISIONS --left-right --cherry-pick | \
951 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
952 # Now all commits and note which ones are missing in
953 # not-cherry-picks and hence being dropped
954 git rev-list $REVISIONS |
955 while read rev
957 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
958 then
959 # Use -f2 because if rev-list is telling us this commit is
960 # not worthwhile, we don't want to track its multiple heads,
961 # just the history of its first-parent for others that will
962 # be rebasing on top of it
963 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
964 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
965 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
966 rm "$REWRITTEN"/$rev
968 done
971 test -s "$TODO" || echo noop >> "$TODO"
972 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
973 cat >> "$TODO" << EOF
975 # Rebase $SHORTREVISIONS onto $SHORTONTO
977 # Commands:
978 # p, pick = use commit
979 # r, reword = use commit, but edit the commit message
980 # e, edit = use commit, but stop for amending
981 # s, squash = use commit, but meld into previous commit
982 # f, fixup = like "squash", but discard this commit's log message
983 # g, goto = reset the current state to the given commit
984 # m, merge parents <parents> original <original merge commit>
985 # = redo the given merge commit
987 # If you remove a line here THAT COMMIT WILL BE LOST.
988 # However, if you remove everything, the rebase will be aborted.
992 has_action "$TODO" ||
993 die_abort "Nothing to do"
995 cp "$TODO" "$TODO".backup
996 git_editor "$TODO" ||
997 die_abort "Could not execute editor"
999 has_action "$TODO" ||
1000 die_abort "Nothing to do"
1002 test -d "$REWRITTEN" || skip_unnecessary_picks
1004 git update-ref ORIG_HEAD $HEAD
1005 output git checkout $ONTO && do_rest
1007 esac
1008 shift
1009 done