rebase -i -p: refactor the preparation for -p into its own function
[git/dscho.git] / git-rebase--interactive.sh
blobc2ba635cb073a8f417c3d4a0ce2afd94c5aa352d
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 test -d "$REWRITTEN" && git rev-parse "$1" >> "$REWRITTEN"/original
244 make_patch "$1"
245 git rerere
246 die "$2"
249 die_abort () {
250 rm -rf "$DOTEST"
251 die "$1"
254 has_action () {
255 sane_grep '^[^#]' "$1" >/dev/null
258 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
259 # GIT_AUTHOR_DATE exported from the current environment.
260 do_with_author () {
262 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
263 "$@"
267 pick_one () {
268 no_ff=
269 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
270 test -d "$REWRITTEN" && echo "$sha1" >> "$REWRITTEN"/original
271 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
272 test -d "$REWRITTEN" &&
273 pick_one_preserving_merges "$@" && return
274 if test -n "$REBASE_ROOT"
275 then
276 output git cherry-pick "$@"
277 return
279 parent_sha1=$(git rev-parse --verify $sha1^) ||
280 die "Could not get the parent of $sha1"
281 current_sha1=$(git rev-parse --verify HEAD)
282 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
283 then
284 output git reset --hard $sha1
285 output warn Fast-forward to $(git rev-parse --short $sha1)
286 else
287 output git cherry-pick "$@"
291 merge_one () {
292 cmd="merge $*"
293 test "$1" = parents && shift
294 parents=
295 while test "original" != "$1"
297 parents="$parents $(parse_commit $1)"
298 shift
299 done
301 test "original" != "$1" &&
302 die "Could not determine original merge commit from $cmd"
304 sha1=$2; shift; shift
306 # the command was "merge parents ...", so "parents" was recorded
307 # TODO: detect non-fast-forwards properly
308 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
309 git merge $parents &&
310 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
311 then
312 git commit --amend -m "$*" &&
313 echo "$sha1" > "$REWRITTEN"/original &&
314 add_rewritten
315 fi ||
316 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
319 pick_one_preserving_merges () {
320 fast_forward=t
321 case "$1" in
323 fast_forward=f
324 sha1=$2
327 sha1=$1
329 esac
330 sha1=$(git rev-parse $sha1)
331 echo $sha1 > "$REWRITTEN"/original
333 if test -f "$DOTEST"/current-commit
334 then
335 if test "$fast_forward" = t
336 then
337 cat "$DOTEST"/current-commit | while read current_commit
339 git rev-parse HEAD > "$REWRITTEN"/$current_commit
340 done
341 rm "$DOTEST"/current-commit ||
342 die "Cannot write current commit's replacement sha1"
346 echo $sha1 >> "$DOTEST"/current-commit
348 # rewrite parents; if none were rewritten, we can fast-forward.
349 new_parents=
350 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
351 if test "$pend" = " "
352 then
353 pend=" root"
355 while [ "$pend" != "" ]
357 p=$(expr "$pend" : ' \([^ ]*\)')
358 pend="${pend# $p}"
360 if test -f "$REWRITTEN"/$p
361 then
362 new_p=$(cat "$REWRITTEN"/$p)
364 # If the todo reordered commits, and our parent is marked for
365 # rewriting, but hasn't been gotten to yet, assume the user meant to
366 # drop it on top of the current HEAD
367 if test -z "$new_p"
368 then
369 new_p=$(git rev-parse HEAD)
372 test $p != $new_p && fast_forward=f
373 case "$new_parents" in
374 *$new_p*)
375 ;; # do nothing; that parent is already there
377 new_parents="$new_parents $new_p"
379 esac
380 else
381 if test -f "$DROPPED"/$p
382 then
383 fast_forward=f
384 replacement="$(cat "$DROPPED"/$p)"
385 test -z "$replacement" && replacement=root
386 pend=" $replacement$pend"
387 else
388 new_parents="$new_parents $p"
391 done
392 case $fast_forward in
394 output warn "Fast-forward to $sha1"
395 output git reset --hard $sha1 ||
396 die "Cannot fast-forward to $sha1"
399 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
401 if [ "$1" != "-n" ]
402 then
403 # detach HEAD to current parent
404 output git checkout $first_parent 2> /dev/null ||
405 die "Cannot move HEAD to $first_parent"
408 case "$new_parents" in
409 ' '*' '*)
410 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
412 # redo merge
413 author_script=$(get_author_ident_from_commit $sha1)
414 eval "$author_script"
415 msg="$(commit_message $sha1)"
416 # No point in merging the first parent, that's HEAD
417 new_parents=${new_parents# $first_parent}
418 if ! do_with_author output \
419 git merge $STRATEGY -m "$msg" $new_parents
420 then
421 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
422 die_with_patch $sha1 "Error redoing merge $sha1"
426 output git cherry-pick "$@" ||
427 die_with_patch $sha1 "Could not pick $sha1"
429 esac
431 esac
434 nth_string () {
435 case "$1" in
436 *1[0-9]|*[04-9]) echo "$1"th;;
437 *1) echo "$1"st;;
438 *2) echo "$1"nd;;
439 *3) echo "$1"rd;;
440 esac
443 update_squash_messages () {
444 if test -f "$SQUASH_MSG"; then
445 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
446 COUNT=$(($(sed -n \
447 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
448 -e "q" < "$SQUASH_MSG".bak)+1))
450 echo "# This is a combination of $COUNT commits."
451 sed -e 1d -e '2,/^./{
452 /^$/d
453 }' <"$SQUASH_MSG".bak
454 } >"$SQUASH_MSG"
455 else
456 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
457 COUNT=2
459 echo "# This is a combination of 2 commits."
460 echo "# The first commit's message is:"
461 echo
462 cat "$FIXUP_MSG"
463 } >"$SQUASH_MSG"
465 case $1 in
466 squash)
467 rm -f "$FIXUP_MSG"
468 echo
469 echo "# This is the $(nth_string $COUNT) commit message:"
470 echo
471 commit_message $2
473 fixup)
474 echo
475 echo "# The $(nth_string $COUNT) commit message will be skipped:"
476 echo
477 commit_message $2 | sed -e 's/^/# /'
479 esac >>"$SQUASH_MSG"
482 # A squash/fixup has failed. Prepare the long version of the squash
483 # commit message, then die_with_patch. This code path requires the
484 # user to edit the combined commit message for all commits that have
485 # been squashed/fixedup so far. So also erase the old squash
486 # messages, effectively causing the combined commit to be used as the
487 # new basis for any further squash/fixups. Args: sha1 rest
488 die_failed_squash() {
489 mv "$SQUASH_MSG" "$MSG" || exit
490 rm -f "$FIXUP_MSG"
491 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
492 warn
493 warn "Could not apply $1... $2"
494 die_with_patch $1 ""
497 do_next () {
498 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
499 read command sha1 rest < "$TODO"
500 case "$command" in
501 '#'*|''|noop)
502 mark_action_done
504 pick|p)
505 comment_for_reflog pick
507 mark_action_done
508 pick_one $sha1 && add_rewritten ||
509 die_with_patch $sha1 "Could not apply $sha1... $rest"
511 reword|r)
512 comment_for_reflog reword
514 mark_action_done
515 pick_one $sha1 ||
516 die_with_patch $sha1 "Could not apply $sha1... $rest"
517 git commit --amend
519 edit|e)
520 comment_for_reflog edit
522 mark_action_done
523 pick_one $sha1 ||
524 die_with_patch $sha1 "Could not apply $sha1... $rest"
525 make_patch $sha1
526 git rev-parse --verify HEAD > "$AMEND"
527 warn "Stopped at $sha1... $rest"
528 warn "You can amend the commit now, with"
529 warn
530 warn " git commit --amend"
531 warn
532 warn "Once you are satisfied with your changes, run"
533 warn
534 warn " git rebase --continue"
535 warn
536 exit 0
538 squash|s|fixup|f)
539 case "$command" in
540 squash|s)
541 squash_style=squash
543 fixup|f)
544 squash_style=fixup
546 esac
547 comment_for_reflog $squash_style
549 test -f "$DONE" && has_action "$DONE" ||
550 die "Cannot '$squash_style' without a previous commit"
552 mark_action_done
553 update_squash_messages $squash_style $sha1
554 author_script=$(get_author_ident_from_commit HEAD)
555 echo "$author_script" > "$AUTHOR_SCRIPT"
556 eval "$author_script"
557 output git reset --soft HEAD^
558 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
559 case "$(peek_next_command)" in
560 squash|s|fixup|f)
561 # This is an intermediate commit; its message will only be
562 # used in case of trouble. So use the long version:
563 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
564 add_rewritten ||
565 die_failed_squash $sha1 "$rest"
568 # This is the final command of this squash/fixup group
569 if test -f "$FIXUP_MSG"
570 then
571 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
572 add_rewritten ||
573 die_failed_squash $sha1 "$rest"
574 else
575 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
576 rm -f "$GIT_DIR"/MERGE_MSG
577 do_with_author git commit --no-verify -e &&
578 add_rewritten ||
579 die_failed_squash $sha1 "$rest"
581 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
583 esac
585 goto|g)
586 comment_for_reflog goto
587 mark_action_done
588 output git reset --hard $(parse_commit $sha1) ||
589 die "Could not reset to $sha1"
591 merge|m)
592 comment_for_reflog merge
593 mark_action_done
594 # this already dies with patch on error
595 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
596 exit
599 warn "Unknown command: $command $sha1 $rest"
600 if git rev-parse --verify -q "$sha1" >/dev/null
601 then
602 die_with_patch $sha1 "Please fix this in the file $TODO."
603 else
604 die "Please fix this in the file $TODO."
607 esac
608 test -s "$TODO" && return
610 comment_for_reflog finish &&
611 HEADNAME=$(cat "$DOTEST"/head-name) &&
612 OLDHEAD=$(cat "$DOTEST"/head) &&
613 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
614 NEWHEAD=$(git rev-parse HEAD) &&
615 case $HEADNAME in
616 refs/*)
617 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
618 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
619 git symbolic-ref HEAD $HEADNAME
621 esac && {
622 test ! -f "$DOTEST"/verbose ||
623 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
624 } &&
625 rm -rf "$DOTEST" &&
626 git gc --auto &&
627 warn "Successfully rebased and updated $HEADNAME."
629 exit
632 do_rest () {
633 while :
635 do_next
636 done
639 # skip picking commits whose parents are unchanged
640 skip_unnecessary_picks () {
641 fd=3
642 while read command sha1 rest
644 # fd=3 means we skip the command
645 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
646 3,pick,"$ONTO"*|3,p,"$ONTO"*)
647 # pick a commit whose parent is current $ONTO -> skip
648 ONTO=$sha1
650 3,#*|3,,*)
651 # copy comments
654 fd=1
656 esac
657 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
658 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
659 mv -f "$TODO".new "$TODO" ||
660 die "Could not skip unnecessary pick commands"
663 # check if no other options are set
664 is_standalone () {
665 test $# -eq 2 -a "$2" = '--' &&
666 test -z "$ONTO" &&
667 test -z "$PRESERVE_MERGES" &&
668 test -z "$STRATEGY" &&
669 test -z "$VERBOSE"
672 get_saved_options () {
673 test -d "$REWRITTEN" && PRESERVE_MERGES=t
674 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
675 test -f "$DOTEST"/verbose && VERBOSE=t
676 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
679 # Rearrange the todo list that has both "pick sha1 msg" and
680 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
681 # comes immediately after the former, and change "pick" to
682 # "fixup"/"squash".
683 rearrange_squash () {
684 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
685 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
686 "$1" >"$1.sq"
687 test -s "$1.sq" || return
689 used=
690 while read pick sha1 message
692 case " $used" in
693 *" $sha1 "*) continue ;;
694 esac
695 echo "$pick $sha1 $message"
696 while read squash action msg
698 case "$message" in
699 "$msg"*)
700 echo "$action $squash $action! $msg"
701 used="$used$squash "
703 esac
704 done <"$1.sq"
705 done >"$1.rearranged" <"$1"
706 cat "$1.rearranged" >"$1"
707 rm -f "$1.sq" "$1.rearranged"
710 LF='
712 parse_onto () {
713 case "$1" in
714 *...*)
715 if left=${1%...*} right=${1#*...} &&
716 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
717 then
718 case "$onto" in
719 ?*"$LF"?* | '')
720 exit 1 ;;
721 esac
722 echo "$onto"
723 exit 0
725 esac
726 git rev-parse --verify "$1^0"
729 prepare_preserve_merges () {
730 # $REWRITTEN contains files for each commit that is
731 # reachable by at least one merge base of $HEAD and
732 # $UPSTREAM. They are not necessarily rewritten, but
733 # their children might be.
734 # This ensures that commits on merged, but otherwise
735 # unrelated side branches are left alone. (Think "X"
736 # in the man page's example.)
737 if test -z "$REBASE_ROOT"
738 then
739 mkdir "$REWRITTEN" &&
740 for c in $(git merge-base --all $HEAD $UPSTREAM)
742 echo $ONTO > "$REWRITTEN"/$c ||
743 die "Could not init rewritten commits"
744 done
745 else
746 mkdir "$REWRITTEN" &&
747 echo $ONTO > "$REWRITTEN"/root ||
748 die "Could not init rewritten commits"
750 # No cherry-pick because our first pass is to determine
751 # parents to rewrite and skipping dropped commits would
752 # prematurely end our probe
753 MERGES_OPTION=
754 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
757 handle_dropped_commits () {
758 # Watch for commits that been dropped by --cherry-pick
759 mkdir "$DROPPED"
760 # Save all non-cherry-picked changes
761 git rev-list $REVISIONS --left-right --cherry-pick | \
762 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
763 # Now all commits and note which ones are missing in
764 # not-cherry-picks and hence being dropped
765 git rev-list $REVISIONS |
766 while read rev
768 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
769 then
770 # Use -f2 because if rev-list is telling us this commit is
771 # not worthwhile, we don't want to track its multiple heads,
772 # just the history of its first-parent for others that will
773 # be rebasing on top of it
774 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
775 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
776 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
777 rm "$REWRITTEN"/$rev
779 done
782 generate_script_help () {
783 cat >> "$TODO" << EOF
785 # Rebase $SHORTREVISIONS onto $SHORTONTO
787 # Commands:
788 # p, pick = use commit
789 # r, reword = use commit, but edit the commit message
790 # e, edit = use commit, but stop for amending
791 # s, squash = use commit, but meld into previous commit
792 # f, fixup = like "squash", but discard this commit's log message
793 # g, goto = reset the current state to the given commit
794 # m, merge parents <parents> original <original merge commit>
795 # = redo the given merge commit
797 # If you remove a line here THAT COMMIT WILL BE LOST.
798 # However, if you remove everything, the rebase will be aborted.
804 generate_script () {
805 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
806 $REVISIONS | grep '^>')" && {
807 echo noop
808 generate_script_help
809 return
812 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
813 --abbrev=7 --reverse --left-right --topo-order \
814 $REVISIONS | \
815 sed -n "s/^>//p" | while read shortsha1 rest
817 if test t != "$PRESERVE_MERGES"
818 then
819 echo "pick $shortsha1 $rest"
820 else
821 sha1=$(git rev-parse $shortsha1)
822 if test -z "$REBASE_ROOT"
823 then
824 preserve=t
825 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
827 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
828 then
829 preserve=f
831 done
832 else
833 preserve=f
835 if test f = "$preserve"
836 then
837 touch "$REWRITTEN"/$sha1
838 echo "pick $shortsha1 $rest"
841 done
843 generate_script_help
846 while test $# != 0
848 case "$1" in
849 --no-verify)
850 OK_TO_SKIP_PRE_REBASE=yes
852 --verify)
854 --continue)
855 is_standalone "$@" || usage
856 get_saved_options
857 comment_for_reflog continue
859 test -d "$DOTEST" || die "No interactive rebase running"
861 # Sanity check
862 git rev-parse --verify HEAD >/dev/null ||
863 die "Cannot read HEAD"
864 git update-index --ignore-submodules --refresh &&
865 git diff-files --quiet --ignore-submodules ||
866 die "Working tree is dirty"
868 # do we have anything to commit?
869 if git diff-index --cached --quiet --ignore-submodules HEAD --
870 then
871 : Nothing to commit
872 test ! -f "$REWRITTEN"/original || add_rewritten
873 else
874 . "$AUTHOR_SCRIPT" ||
875 die "Cannot find the author identity"
876 amend=
877 if test -f "$AMEND"
878 then
879 amend=$(git rev-parse --verify HEAD)
880 test "$amend" = $(cat "$AMEND") ||
881 die "\
882 You have uncommitted changes in your working tree. Please, commit them
883 first and then run 'git rebase --continue' again."
884 git reset --soft HEAD^ ||
885 die "Cannot rewind the HEAD"
887 do_with_author git commit --no-verify -F "$MSG" -e &&
888 add_rewritten || {
889 test -n "$amend" && git reset --soft $amend
890 die "Could not commit staged changes."
894 require_clean_work_tree
895 do_rest
897 --abort)
898 is_standalone "$@" || usage
899 get_saved_options
900 comment_for_reflog abort
902 git rerere clear
903 test -d "$DOTEST" || die "No interactive rebase running"
905 HEADNAME=$(cat "$DOTEST"/head-name)
906 HEAD=$(cat "$DOTEST"/head)
907 case $HEADNAME in
908 refs/*)
909 git symbolic-ref HEAD $HEADNAME
911 esac &&
912 output git reset --hard $HEAD &&
913 rm -rf "$DOTEST"
914 exit
916 --skip)
917 is_standalone "$@" || usage
918 get_saved_options
919 comment_for_reflog skip
921 git rerere clear
922 test -d "$DOTEST" || die "No interactive rebase running"
924 test -d "$REWRITTEN" && {
925 # skip last to-be-rewritten commit
926 original_count=$(wc -l < "$REWRITTEN"/original)
927 test $original_count -gt 0 &&
928 head -n $(($original_count-1)) < "$REWRITTEN"/original \
929 > "$REWRITTEN"/original.new &&
930 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
932 output git reset --hard && do_rest
935 case "$#,$1" in
936 *,*=*)
937 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
938 1,*)
939 usage ;;
941 STRATEGY="-s $2"
942 shift ;;
943 esac
946 # we use merge anyway
949 VERBOSE=t
952 PRESERVE_MERGES=t
955 # yeah, we know
957 --root)
958 REBASE_ROOT=t
960 --autosquash)
961 AUTOSQUASH=t
963 --onto)
964 shift
965 ONTO=$(parse_onto "$1") ||
966 die "Does not point to a valid commit: $1"
969 shift
970 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
971 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
972 test -d "$DOTEST" &&
973 die "Interactive rebase already started"
975 git var GIT_COMMITTER_IDENT >/dev/null ||
976 die "You need to set your committer info first"
978 if test -z "$REBASE_ROOT"
979 then
980 UPSTREAM_ARG="$1"
981 UPSTREAM=$(git rev-parse --verify "$1") ||
982 die "Invalid base"
983 test -z "$ONTO" && ONTO=$UPSTREAM
984 shift
985 else
986 UPSTREAM=
987 UPSTREAM_ARG=--root
988 test -z "$ONTO" &&
989 die "You must specify --onto when using --root"
990 UPSTREAM=$ONTO
992 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
994 comment_for_reflog start
996 require_clean_work_tree
998 if test ! -z "$1"
999 then
1000 output git show-ref --verify --quiet "refs/heads/$1" ||
1001 die "Invalid branchname: $1"
1002 output git checkout "$1" ||
1003 die "Could not checkout $1"
1006 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
1007 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
1009 : > "$DOTEST"/interactive || die "Could not mark as interactive"
1010 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1011 echo "detached HEAD" > "$DOTEST"/head-name
1013 echo $HEAD > "$DOTEST"/head
1014 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
1015 echo $ONTO > "$DOTEST"/onto
1016 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1017 test t = "$VERBOSE" && : > "$DOTEST"/verbose
1019 SHORTHEAD=$(git rev-parse --short $HEAD)
1020 SHORTONTO=$(git rev-parse --short $ONTO)
1021 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
1022 REVISIONS=$UPSTREAM...$HEAD
1023 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
1025 MERGES_OPTION="--no-merges --cherry-pick"
1026 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
1028 generate_script > "$TODO"
1029 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1032 test t = "$PRESERVE_MERGES" && handle_dropped_commits
1034 has_action "$TODO" ||
1035 die_abort "Nothing to do"
1037 cp "$TODO" "$TODO".backup
1038 git_editor "$TODO" ||
1039 die_abort "Could not execute editor"
1041 has_action "$TODO" ||
1042 die_abort "Nothing to do"
1044 test -d "$REWRITTEN" || skip_unnecessary_picks
1046 git update-ref ORIG_HEAD $HEAD
1047 output git checkout $ONTO && do_rest
1049 esac
1050 shift
1051 done