add configuration variable for --autosquash option of interactive rebase
[git/jrn.git] / git-rebase--interactive.sh
blob7b35f80818018a5b53f779541c27d85afd73dd00
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 root rebase all reachable commmits up to the root(s)
32 autosquash move commits that begin with squash!/fixup! under -i
35 . git-sh-setup
36 require_work_tree
38 DOTEST="$GIT_DIR/rebase-merge"
40 # The file containing rebase commands, comments, and empty lines.
41 # This file is created by "git rebase -i" then edited by the user. As
42 # the lines are processed, they are removed from the front of this
43 # file and written to the tail of $DONE.
44 TODO="$DOTEST"/git-rebase-todo
46 # The rebase command lines that have already been processed. A line
47 # is moved here when it is first handled, before any associated user
48 # actions.
49 DONE="$DOTEST"/done
51 # The commit message that is planned to be used for any changes that
52 # need to be committed following a user interaction.
53 MSG="$DOTEST"/message
55 # The file into which is accumulated the suggested commit message for
56 # squash/fixup commands. When the first of a series of squash/fixups
57 # is seen, the file is created and the commit message from the
58 # previous commit and from the first squash/fixup commit are written
59 # to it. The commit message for each subsequent squash/fixup commit
60 # is appended to the file as it is processed.
62 # The first line of the file is of the form
63 # # This is a combination of $COUNT commits.
64 # where $COUNT is the number of commits whose messages have been
65 # written to the file so far (including the initial "pick" commit).
66 # Each time that a commit message is processed, this line is read and
67 # updated. It is deleted just before the combined commit is made.
68 SQUASH_MSG="$DOTEST"/message-squash
70 # If the current series of squash/fixups has not yet included a squash
71 # command, then this file exists and holds the commit message of the
72 # original "pick" commit. (If the series ends without a "squash"
73 # command, then this can be used as the commit message of the combined
74 # commit without opening the editor.)
75 FIXUP_MSG="$DOTEST"/message-fixup
77 # $REWRITTEN is the name of a directory containing files for each
78 # commit that is reachable by at least one merge base of $HEAD and
79 # $UPSTREAM. They are not necessarily rewritten, but their children
80 # might be. This ensures that commits on merged, but otherwise
81 # unrelated side branches are left alone. (Think "X" in the man page's
82 # example.)
83 REWRITTEN="$DOTEST"/rewritten
85 DROPPED="$DOTEST"/dropped
87 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
88 # GIT_AUTHOR_DATE that will be used for the commit that is currently
89 # being rebased.
90 AUTHOR_SCRIPT="$DOTEST"/author-script
92 # When an "edit" rebase command is being processed, the SHA1 of the
93 # commit to be edited is recorded in this file. When "git rebase
94 # --continue" is executed, if there are any staged changes then they
95 # will be amended to the HEAD commit, but only provided the HEAD
96 # commit is still the commit to be edited. When any other rebase
97 # command is processed, this file is deleted.
98 AMEND="$DOTEST"/amend
100 # For the post-rewrite hook, we make a list of rewritten commits and
101 # their new sha1s. The rewritten-pending list keeps the sha1s of
102 # commits that have been processed, but not committed yet,
103 # e.g. because they are waiting for a 'squash' command.
104 REWRITTEN_LIST="$DOTEST"/rewritten-list
105 REWRITTEN_PENDING="$DOTEST"/rewritten-pending
107 PRESERVE_MERGES=
108 STRATEGY=
109 ONTO=
110 VERBOSE=
111 OK_TO_SKIP_PRE_REBASE=
112 REBASE_ROOT=
113 AUTOSQUASH=
114 test "$(git config --bool rebase.autosquash)" = "true" && AUTOSQUASH=t
115 NEVER_FF=
117 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
118 mark the corrected paths with 'git add <paths>', and
119 run 'git rebase --continue'"
120 export GIT_CHERRY_PICK_HELP
122 warn () {
123 echo "$*" >&2
126 output () {
127 case "$VERBOSE" in
129 output=$("$@" 2>&1 )
130 status=$?
131 test $status != 0 && printf "%s\n" "$output"
132 return $status
135 "$@"
137 esac
140 # Output the commit message for the specified commit.
141 commit_message () {
142 git cat-file commit "$1" | sed "1,/^$/d"
145 run_pre_rebase_hook () {
146 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
147 test -x "$GIT_DIR/hooks/pre-rebase"
148 then
149 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
150 echo >&2 "The pre-rebase hook refused to rebase."
151 exit 1
156 require_clean_work_tree () {
157 # test if working tree is dirty
158 git rev-parse --verify HEAD > /dev/null &&
159 git update-index --ignore-submodules --refresh &&
160 git diff-files --quiet --ignore-submodules &&
161 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
162 die "Working tree is dirty"
165 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
167 comment_for_reflog () {
168 case "$ORIG_REFLOG_ACTION" in
169 ''|rebase*)
170 GIT_REFLOG_ACTION="rebase -i ($1)"
171 export GIT_REFLOG_ACTION
173 esac
176 last_count=
177 mark_action_done () {
178 sed -e 1q < "$TODO" >> "$DONE"
179 sed -e 1d < "$TODO" >> "$TODO".new
180 mv -f "$TODO".new "$TODO"
181 count=$(sane_grep -c '^[^#]' < "$DONE")
182 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
183 if test "$last_count" != "$count"
184 then
185 last_count=$count
186 printf "Rebasing (%d/%d)\r" $count $total
187 test -z "$VERBOSE" || echo
191 make_patch () {
192 sha1_and_parents="$(git rev-list --parents -1 "$1")"
193 case "$sha1_and_parents" in
194 ?*' '?*' '?*)
195 git diff --cc $sha1_and_parents
197 ?*' '?*)
198 git diff-tree -p "$1^!"
201 echo "Root commit"
203 esac > "$DOTEST"/patch
204 test -f "$MSG" ||
205 commit_message "$1" > "$MSG"
206 test -f "$AUTHOR_SCRIPT" ||
207 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
210 die_with_patch () {
211 echo "$1" > "$DOTEST"/stopped-sha
212 make_patch "$1"
213 git rerere
214 die "$2"
217 die_abort () {
218 rm -rf "$DOTEST"
219 die "$1"
222 has_action () {
223 sane_grep '^[^#]' "$1" >/dev/null
226 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
227 # GIT_AUTHOR_DATE exported from the current environment.
228 do_with_author () {
230 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
231 "$@"
235 pick_one () {
236 ff=--ff
237 case "$1" in -n) sha1=$2; ff= ;; *) sha1=$1 ;; esac
238 case "$NEVER_FF" in '') ;; ?*) ff= ;; esac
239 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
240 test -d "$REWRITTEN" &&
241 pick_one_preserving_merges "$@" && return
242 if test -n "$REBASE_ROOT"
243 then
244 output git cherry-pick "$@"
245 return
247 output git cherry-pick $ff "$@"
250 pick_one_preserving_merges () {
251 fast_forward=t
252 case "$1" in
254 fast_forward=f
255 sha1=$2
258 sha1=$1
260 esac
261 sha1=$(git rev-parse $sha1)
263 if test -f "$DOTEST"/current-commit
264 then
265 if test "$fast_forward" = t
266 then
267 cat "$DOTEST"/current-commit | while read current_commit
269 git rev-parse HEAD > "$REWRITTEN"/$current_commit
270 done
271 rm "$DOTEST"/current-commit ||
272 die "Cannot write current commit's replacement sha1"
276 echo $sha1 >> "$DOTEST"/current-commit
278 # rewrite parents; if none were rewritten, we can fast-forward.
279 new_parents=
280 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
281 if test "$pend" = " "
282 then
283 pend=" root"
285 while [ "$pend" != "" ]
287 p=$(expr "$pend" : ' \([^ ]*\)')
288 pend="${pend# $p}"
290 if test -f "$REWRITTEN"/$p
291 then
292 new_p=$(cat "$REWRITTEN"/$p)
294 # If the todo reordered commits, and our parent is marked for
295 # rewriting, but hasn't been gotten to yet, assume the user meant to
296 # drop it on top of the current HEAD
297 if test -z "$new_p"
298 then
299 new_p=$(git rev-parse HEAD)
302 test $p != $new_p && fast_forward=f
303 case "$new_parents" in
304 *$new_p*)
305 ;; # do nothing; that parent is already there
307 new_parents="$new_parents $new_p"
309 esac
310 else
311 if test -f "$DROPPED"/$p
312 then
313 fast_forward=f
314 replacement="$(cat "$DROPPED"/$p)"
315 test -z "$replacement" && replacement=root
316 pend=" $replacement$pend"
317 else
318 new_parents="$new_parents $p"
321 done
322 case $fast_forward in
324 output warn "Fast-forward to $sha1"
325 output git reset --hard $sha1 ||
326 die "Cannot fast-forward to $sha1"
329 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
331 if [ "$1" != "-n" ]
332 then
333 # detach HEAD to current parent
334 output git checkout $first_parent 2> /dev/null ||
335 die "Cannot move HEAD to $first_parent"
338 case "$new_parents" in
339 ' '*' '*)
340 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
342 # redo merge
343 author_script=$(get_author_ident_from_commit $sha1)
344 eval "$author_script"
345 msg="$(commit_message $sha1)"
346 # No point in merging the first parent, that's HEAD
347 new_parents=${new_parents# $first_parent}
348 if ! do_with_author output \
349 git merge $STRATEGY -m "$msg" $new_parents
350 then
351 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
352 die_with_patch $sha1 "Error redoing merge $sha1"
354 echo "$sha1 $(git rev-parse HEAD^0)" >> "$REWRITTEN_LIST"
357 output git cherry-pick "$@" ||
358 die_with_patch $sha1 "Could not pick $sha1"
360 esac
362 esac
365 nth_string () {
366 case "$1" in
367 *1[0-9]|*[04-9]) echo "$1"th;;
368 *1) echo "$1"st;;
369 *2) echo "$1"nd;;
370 *3) echo "$1"rd;;
371 esac
374 update_squash_messages () {
375 if test -f "$SQUASH_MSG"; then
376 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
377 COUNT=$(($(sed -n \
378 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
379 -e "q" < "$SQUASH_MSG".bak)+1))
381 echo "# This is a combination of $COUNT commits."
382 sed -e 1d -e '2,/^./{
383 /^$/d
384 }' <"$SQUASH_MSG".bak
385 } >"$SQUASH_MSG"
386 else
387 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
388 COUNT=2
390 echo "# This is a combination of 2 commits."
391 echo "# The first commit's message is:"
392 echo
393 cat "$FIXUP_MSG"
394 } >"$SQUASH_MSG"
396 case $1 in
397 squash)
398 rm -f "$FIXUP_MSG"
399 echo
400 echo "# This is the $(nth_string $COUNT) commit message:"
401 echo
402 commit_message $2
404 fixup)
405 echo
406 echo "# The $(nth_string $COUNT) commit message will be skipped:"
407 echo
408 commit_message $2 | sed -e 's/^/# /'
410 esac >>"$SQUASH_MSG"
413 peek_next_command () {
414 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
417 # A squash/fixup has failed. Prepare the long version of the squash
418 # commit message, then die_with_patch. This code path requires the
419 # user to edit the combined commit message for all commits that have
420 # been squashed/fixedup so far. So also erase the old squash
421 # messages, effectively causing the combined commit to be used as the
422 # new basis for any further squash/fixups. Args: sha1 rest
423 die_failed_squash() {
424 mv "$SQUASH_MSG" "$MSG" || exit
425 rm -f "$FIXUP_MSG"
426 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
427 warn
428 warn "Could not apply $1... $2"
429 die_with_patch $1 ""
432 flush_rewritten_pending() {
433 test -s "$REWRITTEN_PENDING" || return
434 newsha1="$(git rev-parse HEAD^0)"
435 sed "s/$/ $newsha1/" < "$REWRITTEN_PENDING" >> "$REWRITTEN_LIST"
436 rm -f "$REWRITTEN_PENDING"
439 record_in_rewritten() {
440 oldsha1="$(git rev-parse $1)"
441 echo "$oldsha1" >> "$REWRITTEN_PENDING"
443 case "$(peek_next_command)" in
444 squash|s|fixup|f)
447 flush_rewritten_pending
449 esac
452 do_next () {
453 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
454 read command sha1 rest < "$TODO"
455 case "$command" in
456 '#'*|''|noop)
457 mark_action_done
459 pick|p)
460 comment_for_reflog pick
462 mark_action_done
463 pick_one $sha1 ||
464 die_with_patch $sha1 "Could not apply $sha1... $rest"
465 record_in_rewritten $sha1
467 reword|r)
468 comment_for_reflog reword
470 mark_action_done
471 pick_one $sha1 ||
472 die_with_patch $sha1 "Could not apply $sha1... $rest"
473 git commit --amend --no-post-rewrite
474 record_in_rewritten $sha1
476 edit|e)
477 comment_for_reflog edit
479 mark_action_done
480 pick_one $sha1 ||
481 die_with_patch $sha1 "Could not apply $sha1... $rest"
482 echo "$sha1" > "$DOTEST"/stopped-sha
483 make_patch $sha1
484 git rev-parse --verify HEAD > "$AMEND"
485 warn "Stopped at $sha1... $rest"
486 warn "You can amend the commit now, with"
487 warn
488 warn " git commit --amend"
489 warn
490 warn "Once you are satisfied with your changes, run"
491 warn
492 warn " git rebase --continue"
493 warn
494 exit 0
496 squash|s|fixup|f)
497 case "$command" in
498 squash|s)
499 squash_style=squash
501 fixup|f)
502 squash_style=fixup
504 esac
505 comment_for_reflog $squash_style
507 test -f "$DONE" && has_action "$DONE" ||
508 die "Cannot '$squash_style' without a previous commit"
510 mark_action_done
511 update_squash_messages $squash_style $sha1
512 author_script=$(get_author_ident_from_commit HEAD)
513 echo "$author_script" > "$AUTHOR_SCRIPT"
514 eval "$author_script"
515 output git reset --soft HEAD^
516 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
517 case "$(peek_next_command)" in
518 squash|s|fixup|f)
519 # This is an intermediate commit; its message will only be
520 # used in case of trouble. So use the long version:
521 do_with_author output git commit --no-verify -F "$SQUASH_MSG" ||
522 die_failed_squash $sha1 "$rest"
525 # This is the final command of this squash/fixup group
526 if test -f "$FIXUP_MSG"
527 then
528 do_with_author git commit --no-verify -F "$FIXUP_MSG" ||
529 die_failed_squash $sha1 "$rest"
530 else
531 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
532 rm -f "$GIT_DIR"/MERGE_MSG
533 do_with_author git commit --no-verify -e ||
534 die_failed_squash $sha1 "$rest"
536 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
538 esac
539 record_in_rewritten $sha1
542 warn "Unknown command: $command $sha1 $rest"
543 if git rev-parse --verify -q "$sha1" >/dev/null
544 then
545 die_with_patch $sha1 "Please fix this in the file $TODO."
546 else
547 die "Please fix this in the file $TODO."
550 esac
551 test -s "$TODO" && return
553 comment_for_reflog finish &&
554 HEADNAME=$(cat "$DOTEST"/head-name) &&
555 OLDHEAD=$(cat "$DOTEST"/head) &&
556 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
557 NEWHEAD=$(git rev-parse HEAD) &&
558 case $HEADNAME in
559 refs/*)
560 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
561 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
562 git symbolic-ref HEAD $HEADNAME
564 esac && {
565 test ! -f "$DOTEST"/verbose ||
566 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
567 } &&
569 test -s "$REWRITTEN_LIST" &&
570 git notes copy --for-rewrite=rebase < "$REWRITTEN_LIST" ||
571 true # we don't care if this copying failed
572 } &&
573 if test -x "$GIT_DIR"/hooks/post-rewrite &&
574 test -s "$REWRITTEN_LIST"; then
575 "$GIT_DIR"/hooks/post-rewrite rebase < "$REWRITTEN_LIST"
576 true # we don't care if this hook failed
577 fi &&
578 rm -rf "$DOTEST" &&
579 git gc --auto &&
580 warn "Successfully rebased and updated $HEADNAME."
582 exit
585 do_rest () {
586 while :
588 do_next
589 done
592 # skip picking commits whose parents are unchanged
593 skip_unnecessary_picks () {
594 fd=3
595 while read command sha1 rest
597 # fd=3 means we skip the command
598 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
599 3,pick,"$ONTO"*|3,p,"$ONTO"*)
600 # pick a commit whose parent is current $ONTO -> skip
601 ONTO=$sha1
603 3,#*|3,,*)
604 # copy comments
607 fd=1
609 esac
610 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
611 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
612 mv -f "$TODO".new "$TODO" &&
613 case "$(peek_next_command)" in
614 squash|s|fixup|f)
615 record_in_rewritten "$ONTO"
617 esac ||
618 die "Could not skip unnecessary pick commands"
621 # check if no other options are set
622 is_standalone () {
623 test $# -eq 2 -a "$2" = '--' &&
624 test -z "$ONTO" &&
625 test -z "$PRESERVE_MERGES" &&
626 test -z "$STRATEGY" &&
627 test -z "$VERBOSE"
630 get_saved_options () {
631 test -d "$REWRITTEN" && PRESERVE_MERGES=t
632 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
633 test -f "$DOTEST"/verbose && VERBOSE=t
634 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
637 # Rearrange the todo list that has both "pick sha1 msg" and
638 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
639 # comes immediately after the former, and change "pick" to
640 # "fixup"/"squash".
641 rearrange_squash () {
642 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
643 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
644 "$1" >"$1.sq"
645 test -s "$1.sq" || return
647 used=
648 while read pick sha1 message
650 case " $used" in
651 *" $sha1 "*) continue ;;
652 esac
653 echo "$pick $sha1 $message"
654 while read squash action msg
656 case "$message" in
657 "$msg"*)
658 echo "$action $squash $action! $msg"
659 used="$used$squash "
661 esac
662 done <"$1.sq"
663 done >"$1.rearranged" <"$1"
664 cat "$1.rearranged" >"$1"
665 rm -f "$1.sq" "$1.rearranged"
668 LF='
670 parse_onto () {
671 case "$1" in
672 *...*)
673 if left=${1%...*} right=${1#*...} &&
674 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
675 then
676 case "$onto" in
677 ?*"$LF"?* | '')
678 exit 1 ;;
679 esac
680 echo "$onto"
681 exit 0
683 esac
684 git rev-parse --verify "$1^0"
687 while test $# != 0
689 case "$1" in
690 --no-verify)
691 OK_TO_SKIP_PRE_REBASE=yes
693 --verify)
695 --continue)
696 is_standalone "$@" || usage
697 get_saved_options
698 comment_for_reflog continue
700 test -d "$DOTEST" || die "No interactive rebase running"
702 # Sanity check
703 git rev-parse --verify HEAD >/dev/null ||
704 die "Cannot read HEAD"
705 git update-index --ignore-submodules --refresh &&
706 git diff-files --quiet --ignore-submodules ||
707 die "Working tree is dirty"
709 # do we have anything to commit?
710 if git diff-index --cached --quiet --ignore-submodules HEAD --
711 then
712 : Nothing to commit -- skip this
713 else
714 . "$AUTHOR_SCRIPT" ||
715 die "Cannot find the author identity"
716 amend=
717 if test -f "$AMEND"
718 then
719 amend=$(git rev-parse --verify HEAD)
720 test "$amend" = $(cat "$AMEND") ||
721 die "\
722 You have uncommitted changes in your working tree. Please, commit them
723 first and then run 'git rebase --continue' again."
724 git reset --soft HEAD^ ||
725 die "Cannot rewind the HEAD"
727 do_with_author git commit --no-verify -F "$MSG" -e || {
728 test -n "$amend" && git reset --soft $amend
729 die "Could not commit staged changes."
733 record_in_rewritten "$(cat "$DOTEST"/stopped-sha)"
735 require_clean_work_tree
736 do_rest
738 --abort)
739 is_standalone "$@" || usage
740 get_saved_options
741 comment_for_reflog abort
743 git rerere clear
744 test -d "$DOTEST" || die "No interactive rebase running"
746 HEADNAME=$(cat "$DOTEST"/head-name)
747 HEAD=$(cat "$DOTEST"/head)
748 case $HEADNAME in
749 refs/*)
750 git symbolic-ref HEAD $HEADNAME
752 esac &&
753 output git reset --hard $HEAD &&
754 rm -rf "$DOTEST"
755 exit
757 --skip)
758 is_standalone "$@" || usage
759 get_saved_options
760 comment_for_reflog skip
762 git rerere clear
763 test -d "$DOTEST" || die "No interactive rebase running"
765 output git reset --hard && do_rest
768 case "$#,$1" in
769 *,*=*)
770 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
771 1,*)
772 usage ;;
774 STRATEGY="-s $2"
775 shift ;;
776 esac
779 # we use merge anyway
782 VERBOSE=t
785 PRESERVE_MERGES=t
788 # yeah, we know
790 --no-ff)
791 NEVER_FF=t
793 --root)
794 REBASE_ROOT=t
796 --autosquash)
797 AUTOSQUASH=t
799 --no-autosquash)
800 AUTOSQUASH=
802 --onto)
803 shift
804 ONTO=$(parse_onto "$1") ||
805 die "Does not point to a valid commit: $1"
808 shift
809 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
810 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
811 test -d "$DOTEST" &&
812 die "Interactive rebase already started"
814 git var GIT_COMMITTER_IDENT >/dev/null ||
815 die "You need to set your committer info first"
817 if test -z "$REBASE_ROOT"
818 then
819 UPSTREAM_ARG="$1"
820 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
821 test -z "$ONTO" && ONTO=$UPSTREAM
822 shift
823 else
824 UPSTREAM=
825 UPSTREAM_ARG=--root
826 test -z "$ONTO" &&
827 die "You must specify --onto when using --root"
829 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
831 comment_for_reflog start
833 require_clean_work_tree
835 if test ! -z "$1"
836 then
837 output git checkout "$1" ||
838 die "Could not checkout $1"
841 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
842 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
844 : > "$DOTEST"/interactive || die "Could not mark as interactive"
845 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
846 echo "detached HEAD" > "$DOTEST"/head-name
848 echo $HEAD > "$DOTEST"/head
849 case "$REBASE_ROOT" in
851 rm -f "$DOTEST"/rebase-root ;;
853 : >"$DOTEST"/rebase-root ;;
854 esac
855 echo $ONTO > "$DOTEST"/onto
856 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
857 test t = "$VERBOSE" && : > "$DOTEST"/verbose
858 if test t = "$PRESERVE_MERGES"
859 then
860 if test -z "$REBASE_ROOT"
861 then
862 mkdir "$REWRITTEN" &&
863 for c in $(git merge-base --all $HEAD $UPSTREAM)
865 echo $ONTO > "$REWRITTEN"/$c ||
866 die "Could not init rewritten commits"
867 done
868 else
869 mkdir "$REWRITTEN" &&
870 echo $ONTO > "$REWRITTEN"/root ||
871 die "Could not init rewritten commits"
873 # No cherry-pick because our first pass is to determine
874 # parents to rewrite and skipping dropped commits would
875 # prematurely end our probe
876 MERGES_OPTION=
877 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
878 else
879 MERGES_OPTION="--no-merges --cherry-pick"
882 SHORTHEAD=$(git rev-parse --short $HEAD)
883 SHORTONTO=$(git rev-parse --short $ONTO)
884 if test -z "$REBASE_ROOT"
885 # this is now equivalent to ! -z "$UPSTREAM"
886 then
887 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
888 REVISIONS=$UPSTREAM...$HEAD
889 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
890 else
891 REVISIONS=$ONTO...$HEAD
892 SHORTREVISIONS=$SHORTHEAD
894 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
895 --abbrev=7 --reverse --left-right --topo-order \
896 $REVISIONS | \
897 sed -n "s/^>//p" | while read shortsha1 rest
899 if test t != "$PRESERVE_MERGES"
900 then
901 echo "pick $shortsha1 $rest" >> "$TODO"
902 else
903 sha1=$(git rev-parse $shortsha1)
904 if test -z "$REBASE_ROOT"
905 then
906 preserve=t
907 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
909 if test -f "$REWRITTEN"/$p -a \( $p != $ONTO -o $sha1 = $first_after_upstream \)
910 then
911 preserve=f
913 done
914 else
915 preserve=f
917 if test f = "$preserve"
918 then
919 touch "$REWRITTEN"/$sha1
920 echo "pick $shortsha1 $rest" >> "$TODO"
923 done
925 # Watch for commits that been dropped by --cherry-pick
926 if test t = "$PRESERVE_MERGES"
927 then
928 mkdir "$DROPPED"
929 # Save all non-cherry-picked changes
930 git rev-list $REVISIONS --left-right --cherry-pick | \
931 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
932 # Now all commits and note which ones are missing in
933 # not-cherry-picks and hence being dropped
934 git rev-list $REVISIONS |
935 while read rev
937 if test -f "$REWRITTEN"/$rev -a "$(sane_grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
938 then
939 # Use -f2 because if rev-list is telling us this commit is
940 # not worthwhile, we don't want to track its multiple heads,
941 # just the history of its first-parent for others that will
942 # be rebasing on top of it
943 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
944 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
945 sane_grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
946 rm "$REWRITTEN"/$rev
948 done
951 test -s "$TODO" || echo noop >> "$TODO"
952 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
953 cat >> "$TODO" << EOF
955 # Rebase $SHORTREVISIONS onto $SHORTONTO
957 # Commands:
958 # p, pick = use commit
959 # r, reword = use commit, but edit the commit message
960 # e, edit = use commit, but stop for amending
961 # s, squash = use commit, but meld into previous commit
962 # f, fixup = like "squash", but discard this commit's log message
964 # If you remove a line here THAT COMMIT WILL BE LOST.
965 # However, if you remove everything, the rebase will be aborted.
969 has_action "$TODO" ||
970 die_abort "Nothing to do"
972 cp "$TODO" "$TODO".backup
973 git_editor "$TODO" ||
974 die_abort "Could not execute editor"
976 has_action "$TODO" ||
977 die_abort "Nothing to do"
979 test -d "$REWRITTEN" || test -n "$NEVER_FF" || skip_unnecessary_picks
981 output git checkout $ONTO || die_abort "could not detach HEAD"
982 git update-ref ORIG_HEAD $HEAD
983 do_rest
985 esac
986 shift
987 done