rebase: support --verify
[git/debian.git] / git-rebase--interactive.sh
blob4eabe54e36aece1d21d4e421416b48184b2f5a6b
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 no-ff cherry-pick all commits, even if unchanged
24 m,merge always used (no-op)
25 i,interactive always used (no-op)
26 Actions:
27 continue continue rebasing process
28 abort abort rebasing process and restore original branch
29 skip skip current patch and continue rebasing process
30 no-verify override pre-rebase hook from stopping the operation
31 verify allow pre-rebase hook to run
32 root rebase all reachable commmits up to the root(s)
33 autosquash move commits that begin with squash!/fixup! under -i
36 . git-sh-setup
37 require_work_tree
39 DOTEST="$GIT_DIR/rebase-merge"
41 # The file containing rebase commands, comments, and empty lines.
42 # This file is created by "git rebase -i" then edited by the user. As
43 # the lines are processed, they are removed from the front of this
44 # file and written to the tail of $DONE.
45 TODO="$DOTEST"/git-rebase-todo
47 # The rebase command lines that have already been processed. A line
48 # is moved here when it is first handled, before any associated user
49 # actions.
50 DONE="$DOTEST"/done
52 # The commit message that is planned to be used for any changes that
53 # need to be committed following a user interaction.
54 MSG="$DOTEST"/message
56 # The file into which is accumulated the suggested commit message for
57 # squash/fixup commands. When the first of a series of squash/fixups
58 # is seen, the file is created and the commit message from the
59 # previous commit and from the first squash/fixup commit are written
60 # to it. The commit message for each subsequent squash/fixup commit
61 # is appended to the file as it is processed.
63 # The first line of the file is of the form
64 # # This is a combination of $COUNT commits.
65 # where $COUNT is the number of commits whose messages have been
66 # written to the file so far (including the initial "pick" commit).
67 # Each time that a commit message is processed, this line is read and
68 # updated. It is deleted just before the combined commit is made.
69 SQUASH_MSG="$DOTEST"/message-squash
71 # If the current series of squash/fixups has not yet included a squash
72 # command, then this file exists and holds the commit message of the
73 # original "pick" commit. (If the series ends without a "squash"
74 # command, then this can be used as the commit message of the combined
75 # commit without opening the editor.)
76 FIXUP_MSG="$DOTEST"/message-fixup
78 # $REWRITTEN is the name of a directory containing files for each
79 # commit that is reachable by at least one merge base of $HEAD and
80 # $UPSTREAM. They are not necessarily rewritten, but their children
81 # might be. This ensures that commits on merged, but otherwise
82 # unrelated side branches are left alone. (Think "X" in the man page's
83 # example.)
84 REWRITTEN="$DOTEST"/rewritten
86 DROPPED="$DOTEST"/dropped
88 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
89 # GIT_AUTHOR_DATE that will be used for the commit that is currently
90 # being rebased.
91 AUTHOR_SCRIPT="$DOTEST"/author-script
93 # When an "edit" rebase command is being processed, the SHA1 of the
94 # commit to be edited is recorded in this file. When "git rebase
95 # --continue" is executed, if there are any staged changes then they
96 # will be amended to the HEAD commit, but only provided the HEAD
97 # commit is still the commit to be edited. When any other rebase
98 # command is processed, this file is deleted.
99 AMEND="$DOTEST"/amend
101 # For the post-rewrite hook, we make a list of rewritten commits and
102 # their new sha1s. The rewritten-pending list keeps the sha1s of
103 # commits that have been processed, but not committed yet,
104 # e.g. because they are waiting for a 'squash' command.
105 REWRITTEN_LIST="$DOTEST"/rewritten-list
106 REWRITTEN_PENDING="$DOTEST"/rewritten-pending
108 PRESERVE_MERGES=
109 STRATEGY=
110 ONTO=
111 VERBOSE=
112 OK_TO_SKIP_PRE_REBASE=
113 REBASE_ROOT=
114 AUTOSQUASH=
115 test "$(git config --bool rebase.autosquash)" = "true" && AUTOSQUASH=t
116 NEVER_FF=
118 GIT_CHERRY_PICK_HELP="\
119 hint: after resolving the conflicts, mark the corrected paths
120 hint: with 'git add <paths>' and run 'git rebase --continue'"
121 export GIT_CHERRY_PICK_HELP
123 warn () {
124 printf '%s\n' "$*" >&2
127 output () {
128 case "$VERBOSE" in
130 output=$("$@" 2>&1 )
131 status=$?
132 test $status != 0 && printf "%s\n" "$output"
133 return $status
136 "$@"
138 esac
141 # Output the commit message for the specified commit.
142 commit_message () {
143 git cat-file commit "$1" | sed "1,/^$/d"
146 run_pre_rebase_hook () {
147 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
148 test -x "$GIT_DIR/hooks/pre-rebase"
149 then
150 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
151 echo >&2 "The pre-rebase hook refused to rebase."
152 exit 1
157 require_clean_work_tree () {
158 # test if working tree is dirty
159 git rev-parse --verify HEAD > /dev/null &&
160 git update-index --ignore-submodules --refresh &&
161 git diff-files --quiet --ignore-submodules &&
162 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
163 die "Working tree is dirty"
166 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
168 comment_for_reflog () {
169 case "$ORIG_REFLOG_ACTION" in
170 ''|rebase*)
171 GIT_REFLOG_ACTION="rebase -i ($1)"
172 export GIT_REFLOG_ACTION
174 esac
177 last_count=
178 mark_action_done () {
179 sed -e 1q < "$TODO" >> "$DONE"
180 sed -e 1d < "$TODO" >> "$TODO".new
181 mv -f "$TODO".new "$TODO"
182 count=$(sane_grep -c '^[^#]' < "$DONE")
183 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
184 if test "$last_count" != "$count"
185 then
186 last_count=$count
187 printf "Rebasing (%d/%d)\r" $count $total
188 test -z "$VERBOSE" || echo
192 make_patch () {
193 sha1_and_parents="$(git rev-list --parents -1 "$1")"
194 case "$sha1_and_parents" in
195 ?*' '?*' '?*)
196 git diff --cc $sha1_and_parents
198 ?*' '?*)
199 git diff-tree -p "$1^!"
202 echo "Root commit"
204 esac > "$DOTEST"/patch
205 test -f "$MSG" ||
206 commit_message "$1" > "$MSG"
207 test -f "$AUTHOR_SCRIPT" ||
208 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
211 die_with_patch () {
212 echo "$1" > "$DOTEST"/stopped-sha
213 make_patch "$1"
214 git rerere
215 die "$2"
218 die_abort () {
219 rm -rf "$DOTEST"
220 die "$1"
223 has_action () {
224 sane_grep '^[^#]' "$1" >/dev/null
227 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
228 # GIT_AUTHOR_DATE exported from the current environment.
229 do_with_author () {
231 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
232 "$@"
236 pick_one () {
237 ff=--ff
238 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
239 case "$NEVER_FF" in '') ;; ?*) ff= ;; esac
240 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
241 test -d "$REWRITTEN" &&
242 pick_one_preserving_merges "$@" && return
243 if test -n "$REBASE_ROOT"
244 then
245 output git cherry-pick "$@"
246 return
248 output git cherry-pick $ff "$@"
251 pick_one_preserving_merges () {
252 fast_forward=t
253 case "$1" in
255 fast_forward=f
256 sha1=$2
259 sha1=$1
261 esac
262 sha1=$(git rev-parse $sha1)
264 if test -f "$DOTEST"/current-commit
265 then
266 if test "$fast_forward" = t
267 then
268 while read current_commit
270 git rev-parse HEAD > "$REWRITTEN"/$current_commit
271 done <"$DOTEST"/current-commit
272 rm "$DOTEST"/current-commit ||
273 die "Cannot write current commit's replacement sha1"
277 echo $sha1 >> "$DOTEST"/current-commit
279 # rewrite parents; if none were rewritten, we can fast-forward.
280 new_parents=
281 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
282 if test "$pend" = " "
283 then
284 pend=" root"
286 while [ "$pend" != "" ]
288 p=$(expr "$pend" : ' \([^ ]*\)')
289 pend="${pend# $p}"
291 if test -f "$REWRITTEN"/$p
292 then
293 new_p=$(cat "$REWRITTEN"/$p)
295 # If the todo reordered commits, and our parent is marked for
296 # rewriting, but hasn't been gotten to yet, assume the user meant to
297 # drop it on top of the current HEAD
298 if test -z "$new_p"
299 then
300 new_p=$(git rev-parse HEAD)
303 test $p != $new_p && fast_forward=f
304 case "$new_parents" in
305 *$new_p*)
306 ;; # do nothing; that parent is already there
308 new_parents="$new_parents $new_p"
310 esac
311 else
312 if test -f "$DROPPED"/$p
313 then
314 fast_forward=f
315 replacement="$(cat "$DROPPED"/$p)"
316 test -z "$replacement" && replacement=root
317 pend=" $replacement$pend"
318 else
319 new_parents="$new_parents $p"
322 done
323 case $fast_forward in
325 output warn "Fast-forward to $sha1"
326 output git reset --hard $sha1 ||
327 die "Cannot fast-forward to $sha1"
330 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
332 if [ "$1" != "-n" ]
333 then
334 # detach HEAD to current parent
335 output git checkout $first_parent 2> /dev/null ||
336 die "Cannot move HEAD to $first_parent"
339 case "$new_parents" in
340 ' '*' '*)
341 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
343 # redo merge
344 author_script=$(get_author_ident_from_commit $sha1)
345 eval "$author_script"
346 msg="$(commit_message $sha1)"
347 # No point in merging the first parent, that's HEAD
348 new_parents=${new_parents# $first_parent}
349 if ! do_with_author output \
350 git merge $STRATEGY -m "$msg" $new_parents
351 then
352 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
353 die_with_patch $sha1 "Error redoing merge $sha1"
355 echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
358 output git cherry-pick "$@" ||
359 die_with_patch $sha1 "Could not pick $sha1"
361 esac
363 esac
366 nth_string () {
367 case "$1" in
368 *1[0-9]|*[04-9]) echo "$1"th;;
369 *1) echo "$1"st;;
370 *2) echo "$1"nd;;
371 *3) echo "$1"rd;;
372 esac
375 update_squash_messages () {
376 if test -f "$SQUASH_MSG"; then
377 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
378 COUNT=$(($(sed -n \
379 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
380 -e "q" < "$SQUASH_MSG".bak)+1))
382 echo "# This is a combination of $COUNT commits."
383 sed -e 1d -e '2,/^./{
384 /^$/d
385 }' <"$SQUASH_MSG".bak
386 } >"$SQUASH_MSG"
387 else
388 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
389 COUNT=2
391 echo "# This is a combination of 2 commits."
392 echo "# The first commit's message is:"
393 echo
394 cat "$FIXUP_MSG"
395 } >"$SQUASH_MSG"
397 case $1 in
398 squash)
399 rm -f "$FIXUP_MSG"
400 echo
401 echo "# This is the $(nth_string $COUNT) commit message:"
402 echo
403 commit_message $2
405 fixup)
406 echo
407 echo "# The $(nth_string $COUNT) commit message will be skipped:"
408 echo
409 commit_message $2 | sed -e 's/^/# /'
411 esac >>"$SQUASH_MSG"
414 peek_next_command () {
415 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
418 # A squash/fixup has failed. Prepare the long version of the squash
419 # commit message, then die_with_patch. This code path requires the
420 # user to edit the combined commit message for all commits that have
421 # been squashed/fixedup so far. So also erase the old squash
422 # messages, effectively causing the combined commit to be used as the
423 # new basis for any further squash/fixups. Args: sha1 rest
424 die_failed_squash() {
425 mv "$SQUASH_MSG" "$MSG" || exit
426 rm -f "$FIXUP_MSG"
427 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
428 warn
429 warn "Could not apply $1... $2"
430 die_with_patch $1 ""
433 flush_rewritten_pending() {
434 test -s "$REWRITTEN_PENDING" || return
435 newsha1="$(git rev-parse HEAD^0)"
436 sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
437 rm -f "$REWRITTEN_PENDING"
440 record_in_rewritten() {
441 oldsha1="$(git rev-parse $1)"
442 echo "$oldsha1" >> "$REWRITTEN_PENDING"
444 case "$(peek_next_command)" in
445 squash|s|fixup|f)
448 flush_rewritten_pending
450 esac
453 do_next () {
454 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
455 read -r command sha1 rest < "$TODO"
456 case "$command" in
457 '#'*|''|noop)
458 mark_action_done
460 pick|p)
461 comment_for_reflog pick
463 mark_action_done
464 pick_one $sha1 ||
465 die_with_patch $sha1 "Could not apply $sha1... $rest"
466 record_in_rewritten $sha1
468 reword|r)
469 comment_for_reflog reword
471 mark_action_done
472 pick_one $sha1 ||
473 die_with_patch $sha1 "Could not apply $sha1... $rest"
474 git commit --amend --no-post-rewrite
475 record_in_rewritten $sha1
477 edit|e)
478 comment_for_reflog edit
480 mark_action_done
481 pick_one $sha1 ||
482 die_with_patch $sha1 "Could not apply $sha1... $rest"
483 echo "$sha1" > "$DOTEST"/stopped-sha
484 make_patch $sha1
485 git rev-parse --verify HEAD > "$AMEND"
486 warn "Stopped at $sha1... $rest"
487 warn "You can amend the commit now, with"
488 warn
489 warn " git commit --amend"
490 warn
491 warn "Once you are satisfied with your changes, run"
492 warn
493 warn " git rebase --continue"
494 warn
495 exit 0
497 squash|s|fixup|f)
498 case "$command" in
499 squash|s)
500 squash_style=squash
502 fixup|f)
503 squash_style=fixup
505 esac
506 comment_for_reflog $squash_style
508 test -f "$DONE" && has_action "$DONE" ||
509 die "Cannot '$squash_style' without a previous commit"
511 mark_action_done
512 update_squash_messages $squash_style $sha1
513 author_script=$(get_author_ident_from_commit HEAD)
514 echo "$author_script" > "$AUTHOR_SCRIPT"
515 eval "$author_script"
516 output git reset --soft HEAD^
517 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
518 case "$(peek_next_command)" in
519 squash|s|fixup|f)
520 # This is an intermediate commit; its message will only be
521 # used in case of trouble. So use the long version:
522 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
523 die_failed_squash $sha1 "$rest"
526 # This is the final command of this squash/fixup group
527 if test -f "$FIXUP_MSG"
528 then
529 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
530 die_failed_squash $sha1 "$rest"
531 else
532 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
533 rm -f "$GIT_DIR"/MERGE_MSG
534 do_with_author git commit --no-verify -e ||
535 die_failed_squash $sha1 "$rest"
537 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
539 esac
540 record_in_rewritten $sha1
542 x|"exec")
543 read -r command rest < "$TODO"
544 mark_action_done
545 printf 'Executing: %s\n' "$rest"
546 # "exec" command doesn't take a sha1 in the todo-list.
547 # => can't just use $sha1 here.
548 git rev-parse --verify HEAD > "$DOTEST"/stopped-sha
549 ${SHELL:-@SHELL_PATH@} -c "$rest" # Actual execution
550 status=$?
551 if test "$status" -ne 0
552 then
553 warn "Execution failed: $rest"
554 warn "You can fix the problem, and then run"
555 warn
556 warn " git rebase --continue"
557 warn
558 exit "$status"
560 # Run in subshell because require_clean_work_tree can die.
561 if ! (require_clean_work_tree)
562 then
563 warn "Commit or stash your changes, and then run"
564 warn
565 warn " git rebase --continue"
566 warn
567 exit 1
571 warn "Unknown command: $command $sha1 $rest"
572 if git rev-parse --verify -q "$sha1" >/dev/null
573 then
574 die_with_patch $sha1 "Please fix this in the file $TODO."
575 else
576 die "Please fix this in the file $TODO."
579 esac
580 test -s "$TODO" && return
582 comment_for_reflog finish &&
583 HEADNAME=$(cat "$DOTEST"/head-name) &&
584 OLDHEAD=$(cat "$DOTEST"/head) &&
585 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
586 NEWHEAD=$(git rev-parse HEAD) &&
587 case $HEADNAME in
588 refs/*)
589 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
590 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
591 git symbolic-ref HEAD $HEADNAME
593 esac && {
594 test ! -f "$DOTEST"/verbose ||
595 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
596 } &&
598 test -s "$REWRITTEN_LIST" &&
599 git notes copy --for-rewrite=rebase < "$REWRITTEN_LIST" ||
600 true # we don't care if this copying failed
601 } &&
602 if test -x "$GIT_DIR"/hooks/post-rewrite &&
603 test -s "$REWRITTEN_LIST"; then
604 "$GIT_DIR"/hooks/post-rewrite rebase < "$REWRITTEN_LIST"
605 true # we don't care if this hook failed
606 fi &&
607 rm -rf "$DOTEST" &&
608 git gc --auto &&
609 warn "Successfully rebased and updated $HEADNAME."
611 exit
614 do_rest () {
615 while :
617 do_next
618 done
621 # skip picking commits whose parents are unchanged
622 skip_unnecessary_picks () {
623 fd=3
624 while read -r command rest
626 # fd=3 means we skip the command
627 case "$fd,$command" in
628 3,pick|3,p)
629 # pick a commit whose parent is current $ONTO -> skip
630 sha1=${rest%% *}
631 case "$(git rev-parse --verify --quiet "$sha1"^)" in
632 "$ONTO"*)
633 ONTO=$sha1
636 fd=1
638 esac
640 3,#*|3,)
641 # copy comments
644 fd=1
646 esac
647 printf '%s\n' "$command${rest:+ }$rest" >&$fd
648 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
649 mv -f "$TODO".new "$TODO" &&
650 case "$(peek_next_command)" in
651 squash|s|fixup|f)
652 record_in_rewritten "$ONTO"
654 esac ||
655 die "Could not skip unnecessary pick commands"
658 # check if no other options are set
659 is_standalone () {
660 test $# -eq 2 -a "$2" = '--' &&
661 test -z "$ONTO" &&
662 test -z "$PRESERVE_MERGES" &&
663 test -z "$STRATEGY" &&
664 test -z "$VERBOSE"
667 get_saved_options () {
668 test -d "$REWRITTEN" && PRESERVE_MERGES=t
669 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
670 test -f "$DOTEST"/verbose && VERBOSE=t
671 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
674 # Rearrange the todo list that has both "pick sha1 msg" and
675 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
676 # comes immediately after the former, and change "pick" to
677 # "fixup"/"squash".
678 rearrange_squash () {
679 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
680 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
681 "$1" >"$1.sq"
682 test -s "$1.sq" || return
684 used=
685 while read -r pick sha1 message
687 case " $used" in
688 *" $sha1 "*) continue ;;
689 esac
690 printf '%s\n' "$pick $sha1 $message"
691 while read -r squash action msg
693 case "$message" in
694 "$msg"*)
695 printf '%s\n' "$action $squash $action! $msg"
696 used="$used$squash "
698 esac
699 done <"$1.sq"
700 done >"$1.rearranged" <"$1"
701 cat "$1.rearranged" >"$1"
702 rm -f "$1.sq" "$1.rearranged"
705 LF='
707 parse_onto () {
708 case "$1" in
709 *...*)
710 if left=${1%...*} right=${1#*...} &&
711 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
712 then
713 case "$onto" in
714 ?*"$LF"?* | '')
715 exit 1 ;;
716 esac
717 echo "$onto"
718 exit 0
720 esac
721 git rev-parse --verify "$1^0"
724 while test $# != 0
726 case "$1" in
727 --no-verify)
728 OK_TO_SKIP_PRE_REBASE=yes
730 --verify)
731 OK_TO_SKIP_PRE_REBASE=
733 --continue)
734 is_standalone "$@" || usage
735 get_saved_options
736 comment_for_reflog continue
738 test -d "$DOTEST" || die "No interactive rebase running"
740 # Sanity check
741 git rev-parse --verify HEAD >/dev/null ||
742 die "Cannot read HEAD"
743 git update-index --ignore-submodules --refresh &&
744 git diff-files --quiet --ignore-submodules ||
745 die "Working tree is dirty"
747 # do we have anything to commit?
748 if git diff-index --cached --quiet --ignore-submodules HEAD --
749 then
750 : Nothing to commit -- skip this
751 else
752 . "$AUTHOR_SCRIPT" ||
753 die "Cannot find the author identity"
754 amend=
755 if test -f "$AMEND"
756 then
757 amend=$(git rev-parse --verify HEAD)
758 test "$amend" = $(cat "$AMEND") ||
759 die "\
760 You have uncommitted changes in your working tree. Please, commit them
761 first and then run 'git rebase --continue' again."
762 git reset --soft HEAD^ ||
763 die "Cannot rewind the HEAD"
765 do_with_author git commit --no-verify -F "$MSG" -e || {
766 test -n "$amend" && git reset --soft $amend
767 die "Could not commit staged changes."
771 record_in_rewritten "$(cat "$DOTEST"/stopped-sha)"
773 require_clean_work_tree
774 do_rest
776 --abort)
777 is_standalone "$@" || usage
778 get_saved_options
779 comment_for_reflog abort
781 git rerere clear
782 test -d "$DOTEST" || die "No interactive rebase running"
784 HEADNAME=$(cat "$DOTEST"/head-name)
785 HEAD=$(cat "$DOTEST"/head)
786 case $HEADNAME in
787 refs/*)
788 git symbolic-ref HEAD $HEADNAME
790 esac &&
791 output git reset --hard $HEAD &&
792 rm -rf "$DOTEST"
793 exit
795 --skip)
796 is_standalone "$@" || usage
797 get_saved_options
798 comment_for_reflog skip
800 git rerere clear
801 test -d "$DOTEST" || die "No interactive rebase running"
803 output git reset --hard && do_rest
806 case "$#,$1" in
807 *,*=*)
808 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
809 1,*)
810 usage ;;
812 STRATEGY="-s $2"
813 shift ;;
814 esac
817 # we use merge anyway
820 VERBOSE=t
823 PRESERVE_MERGES=t
826 # yeah, we know
828 --no-ff)
829 NEVER_FF=t
831 --root)
832 REBASE_ROOT=t
834 --autosquash)
835 AUTOSQUASH=t
837 --no-autosquash)
838 AUTOSQUASH=
840 --onto)
841 shift
842 ONTO=$(parse_onto "$1") ||
843 die "Does not point to a valid commit: $1"
846 shift
847 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
848 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
849 test -d "$DOTEST" &&
850 die "Interactive rebase already started"
852 git var GIT_COMMITTER_IDENT >/dev/null ||
853 die "You need to set your committer info first"
855 if test -z "$REBASE_ROOT"
856 then
857 UPSTREAM_ARG="$1"
858 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
859 test -z "$ONTO" && ONTO=$UPSTREAM
860 shift
861 else
862 UPSTREAM=
863 UPSTREAM_ARG=--root
864 test -z "$ONTO" &&
865 die "You must specify --onto when using --root"
867 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
869 comment_for_reflog start
871 require_clean_work_tree
873 if test ! -z "$1"
874 then
875 output git checkout "$1" ||
876 die "Could not checkout $1"
879 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
880 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
882 : > "$DOTEST"/interactive || die "Could not mark as interactive"
883 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
884 echo "detached HEAD" > "$DOTEST"/head-name
886 echo $HEAD > "$DOTEST"/head
887 case "$REBASE_ROOT" in
889 rm -f "$DOTEST"/rebase-root ;;
891 : >"$DOTEST"/rebase-root ;;
892 esac
893 echo $ONTO > "$DOTEST"/onto
894 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
895 test t = "$VERBOSE" && : > "$DOTEST"/verbose
896 if test t = "$PRESERVE_MERGES"
897 then
898 if test -z "$REBASE_ROOT"
899 then
900 mkdir "$REWRITTEN" &&
901 for c in $(git merge-base --all $HEAD $UPSTREAM)
903 echo $ONTO > "$REWRITTEN"/$c ||
904 die "Could not init rewritten commits"
905 done
906 else
907 mkdir "$REWRITTEN" &&
908 echo $ONTO > "$REWRITTEN"/root ||
909 die "Could not init rewritten commits"
911 # No cherry-pick because our first pass is to determine
912 # parents to rewrite and skipping dropped commits would
913 # prematurely end our probe
914 MERGES_OPTION=
915 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
916 else
917 MERGES_OPTION="--no-merges --cherry-pick"
920 SHORTHEAD=$(git rev-parse --short $HEAD)
921 SHORTONTO=$(git rev-parse --short $ONTO)
922 if test -z "$REBASE_ROOT"
923 # this is now equivalent to ! -z "$UPSTREAM"
924 then
925 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
926 REVISIONS=$UPSTREAM...$HEAD
927 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
928 else
929 REVISIONS=$ONTO...$HEAD
930 SHORTREVISIONS=$SHORTHEAD
932 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
933 --abbrev=7 --reverse --left-right --topo-order \
934 $REVISIONS | \
935 sed -n "s/^>//p" |
936 while read -r shortsha1 rest
938 if test t != "$PRESERVE_MERGES"
939 then
940 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
941 else
942 sha1=$(git rev-parse $shortsha1)
943 if test -z "$REBASE_ROOT"
944 then
945 preserve=t
946 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
948 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
949 then
950 preserve=f
952 done
953 else
954 preserve=f
956 if test f = "$preserve"
957 then
958 touch "$REWRITTEN"/$sha1
959 printf '%s\n' "pick $shortsha1 $rest" >> "$TODO"
962 done
964 # Watch for commits that been dropped by --cherry-pick
965 if test t = "$PRESERVE_MERGES"
966 then
967 mkdir "$DROPPED"
968 # Save all non-cherry-picked changes
969 git rev-list $REVISIONS --left-right --cherry-pick | \
970 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
971 # Now all commits and note which ones are missing in
972 # not-cherry-picks and hence being dropped
973 git rev-list $REVISIONS |
974 while read rev
976 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
977 then
978 # Use -f2 because if rev-list is telling us this commit is
979 # not worthwhile, we don't want to track its multiple heads,
980 # just the history of its first-parent for others that will
981 # be rebasing on top of it
982 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
983 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
984 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
985 rm "$REWRITTEN"/$rev
987 done
990 test -s "$TODO" || echo noop >> "$TODO"
991 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
992 cat >> "$TODO" << EOF
994 # Rebase $SHORTREVISIONS onto $SHORTONTO
996 # Commands:
997 # p, pick = use commit
998 # r, reword = use commit, but edit the commit message
999 # e, edit = use commit, but stop for amending
1000 # s, squash = use commit, but meld into previous commit
1001 # f, fixup = like "squash", but discard this commit's log message
1002 # x <cmd>, exec <cmd> = Run a shell command <cmd>, and stop if it fails
1004 # If you remove a line here THAT COMMIT WILL BE LOST.
1005 # However, if you remove everything, the rebase will be aborted.
1009 has_action "$TODO" ||
1010 die_abort "Nothing to do"
1012 cp "$TODO" "$TODO".backup
1013 git_editor "$TODO" ||
1014 die_abort "Could not execute editor"
1016 has_action "$TODO" ||
1017 die_abort "Nothing to do"
1019 test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
1021 output git checkout $ONTO || die_abort "could not detach HEAD"
1022 git update-ref ORIG_HEAD $HEAD
1023 do_rest
1025 esac
1026 shift
1027 done