rebase -i -p: add a 'bookmark' command
[git/dscho.git] / git-rebase--interactive.sh
blob32d5734756a191422184f592a2017ef7fa4cfdb2
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 # A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
85 # GIT_AUTHOR_DATE that will be used for the commit that is currently
86 # being rebased.
87 AUTHOR_SCRIPT="$DOTEST"/author-script
89 # When an "edit" rebase command is being processed, the SHA1 of the
90 # commit to be edited is recorded in this file. When "git rebase
91 # --continue" is executed, if there are any staged changes then they
92 # will be amended to the HEAD commit, but only provided the HEAD
93 # commit is still the commit to be edited. When any other rebase
94 # command is processed, this file is deleted.
95 AMEND="$DOTEST"/amend
97 PRESERVE_MERGES=
98 STRATEGY=
99 ONTO=
100 VERBOSE=
101 OK_TO_SKIP_PRE_REBASE=
102 REBASE_ROOT=
103 AUTOSQUASH=
105 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
106 mark the corrected paths with 'git add <paths>', and
107 run 'git rebase --continue'"
108 export GIT_CHERRY_PICK_HELP
110 warn () {
111 echo "$*" >&2
114 output () {
115 case "$VERBOSE" in
117 output=$("$@" 2>&1 )
118 status=$?
119 test $status != 0 && printf "%s\n" "$output"
120 return $status
123 "$@"
125 esac
128 # Output the commit message for the specified commit.
129 commit_message () {
130 git cat-file commit "$1" | sed "1,/^$/d"
133 run_pre_rebase_hook () {
134 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
135 test -x "$GIT_DIR/hooks/pre-rebase"
136 then
137 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
138 echo >&2 "The pre-rebase hook refused to rebase."
139 exit 1
144 require_clean_work_tree () {
145 # test if working tree is dirty
146 git rev-parse --verify HEAD > /dev/null &&
147 git update-index --ignore-submodules --refresh &&
148 git diff-files --quiet --ignore-submodules &&
149 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
150 die "Working tree is dirty"
153 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
155 comment_for_reflog () {
156 case "$ORIG_REFLOG_ACTION" in
157 ''|rebase*)
158 GIT_REFLOG_ACTION="rebase -i ($1)"
159 export GIT_REFLOG_ACTION
161 esac
164 peek_next_command () {
165 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
168 # expects the original commit name(s) in "$REWRITTEN"/original
169 # records the current HEAD as the rewritten commit
170 add_rewritten () {
171 test ! -d "$REWRITTEN" && return
172 rewritten=$(git rev-parse --verify HEAD) &&
173 for original in $(cat "$REWRITTEN"/original)
175 original=$(git-rev-parse "$original") &&
176 echo $rewritten > "$REWRITTEN"/$original || break
177 done &&
178 case "$(peek_next_command)" in
179 squash|s) ;; # do nothing
180 *) rm "$REWRITTEN"/original;;
181 esac ||
182 die "Could not store information about rewritten commit"
185 # if the given commit name ends in an apostrophe, returns the rewritten commit
186 parse_commit () {
187 if test -f "$REWRITTEN/${1%\'}"
188 then
189 rewritten="$(cat "$REWRITTEN/${1%\'}")"
190 else
191 rewritten=$(git rev-parse --verify "${1%\'}") ||
192 die "Cannot turn $1 into a commit name"
194 while test -f "$REWRITTEN"/$rewritten
196 new_rewritten=$(cat "$REWRITTEN"/$rewritten) ||
197 die "Could not read rewritten commit for $1"
198 test $rewritten = $new_rewritten && break
199 rewritten=$new_rewritten
200 done
201 echo $rewritten
204 last_count=
205 mark_action_done () {
206 sed -e 1q < "$TODO" >> "$DONE"
207 sed -e 1d < "$TODO" >> "$TODO".new
208 mv -f "$TODO".new "$TODO"
209 count=$(sane_grep -c '^[^#]' < "$DONE")
210 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
211 if test "$last_count" != "$count"
212 then
213 last_count=$count
214 printf "Rebasing (%d/%d)\r" $count $total
215 test -z "$VERBOSE" || echo
219 make_patch () {
220 sha1_and_parents="$(git rev-list --parents -1 "$1")"
221 case "$sha1_and_parents" in
222 ?*' '?*' '?*)
223 git diff --cc $sha1_and_parents
225 ?*' '?*)
226 git diff-tree -p "$1^!"
229 echo "Root commit"
231 esac > "$DOTEST"/patch
232 test -f "$MSG" ||
233 commit_message "$1" > "$MSG"
234 test -f "$AUTHOR_SCRIPT" ||
235 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
238 die_with_patch () {
239 test -d "$REWRITTEN" && git rev-parse "$1" >> "$REWRITTEN"/original
240 make_patch "$1"
241 git rerere
242 die "$2"
245 die_abort () {
246 rm -rf "$DOTEST"
247 die "$1"
250 has_action () {
251 sane_grep '^[^#]' "$1" >/dev/null
254 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
255 # GIT_AUTHOR_DATE exported from the current environment.
256 do_with_author () {
258 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
259 "$@"
263 pick_one () {
264 no_ff=
265 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
266 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
267 test -d "$REWRITTEN" && git rev-parse $sha1 >> "$REWRITTEN"/original
268 if test -n "$REBASE_ROOT"
269 then
270 output git cherry-pick "$@"
271 return
273 parent_sha1=$(git rev-parse --verify $sha1^) ||
274 die "Could not get the parent of $sha1"
275 current_sha1=$(git rev-parse --verify HEAD)
276 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
277 then
278 output git reset --hard $sha1
279 output warn Fast-forward to $(git rev-parse --short $sha1)
280 else
281 output git cherry-pick "$@"
285 merge_one () {
286 cmd="merge $*"
287 test "$1" = parents && shift
288 parents=
289 merge_message="Merge"
290 while test $# -gt 0 -a "original" != "$1" -a "as" != "$1"
292 parents="$parents $(parse_commit $1)" ||
293 die "Could not interpret '$1' as a commit"
294 merge_message="$merge_message $1"
295 shift
296 done
298 if test -z "$2"
299 then
300 sha1=
301 else
302 sha1=$(git rev-parse "$2")
305 if test ! -z "$3"
306 then
307 shift; shift;
308 merge_message="$*"
311 # the command was "merge parents ...", so "parents" was recorded
312 # TODO: detect non-fast-forwards properly
313 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
314 git merge $parents &&
315 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
316 then
317 git commit --amend -m "$merge_message" &&
318 if test !-z "$sha1"
319 then
320 echo "$sha1" > "$REWRITTEN"/original &&
321 add_rewritten
323 fi ||
324 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
327 nth_string () {
328 case "$1" in
329 *1[0-9]|*[04-9]) echo "$1"th;;
330 *1) echo "$1"st;;
331 *2) echo "$1"nd;;
332 *3) echo "$1"rd;;
333 esac
336 update_squash_messages () {
337 if test -f "$SQUASH_MSG"; then
338 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
339 COUNT=$(($(sed -n \
340 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
341 -e "q" < "$SQUASH_MSG".bak)+1))
343 echo "# This is a combination of $COUNT commits."
344 sed -e 1d -e '2,/^./{
345 /^$/d
346 }' <"$SQUASH_MSG".bak
347 } >"$SQUASH_MSG"
348 else
349 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
350 COUNT=2
352 echo "# This is a combination of 2 commits."
353 echo "# The first commit's message is:"
354 echo
355 cat "$FIXUP_MSG"
356 } >"$SQUASH_MSG"
358 case $1 in
359 squash)
360 rm -f "$FIXUP_MSG"
361 echo
362 echo "# This is the $(nth_string $COUNT) commit message:"
363 echo
364 commit_message $2
366 fixup)
367 echo
368 echo "# The $(nth_string $COUNT) commit message will be skipped:"
369 echo
370 commit_message $2 | sed -e 's/^/# /'
372 esac >>"$SQUASH_MSG"
375 # A squash/fixup has failed. Prepare the long version of the squash
376 # commit message, then die_with_patch. This code path requires the
377 # user to edit the combined commit message for all commits that have
378 # been squashed/fixedup so far. So also erase the old squash
379 # messages, effectively causing the combined commit to be used as the
380 # new basis for any further squash/fixups. Args: sha1 rest
381 die_failed_squash() {
382 mv "$SQUASH_MSG" "$MSG" || exit
383 rm -f "$FIXUP_MSG"
384 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
385 warn
386 warn "Could not apply $1... $2"
387 die_with_patch $1 ""
390 do_next () {
391 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
392 read command sha1 rest < "$TODO"
393 case "$command" in
394 '#'*|''|noop)
395 mark_action_done
397 pick|p)
398 comment_for_reflog pick
400 mark_action_done
401 pick_one $sha1 && add_rewritten ||
402 die_with_patch $sha1 "Could not apply $sha1... $rest"
404 reword|r)
405 comment_for_reflog reword
407 mark_action_done
408 pick_one $sha1 ||
409 die_with_patch $sha1 "Could not apply $sha1... $rest"
410 git commit --amend
412 edit|e)
413 comment_for_reflog edit
415 mark_action_done
416 pick_one $sha1 ||
417 die_with_patch $sha1 "Could not apply $sha1... $rest"
418 make_patch $sha1
419 git rev-parse --verify HEAD > "$AMEND"
420 warn "Stopped at $sha1... $rest"
421 warn "You can amend the commit now, with"
422 warn
423 warn " git commit --amend"
424 warn
425 warn "Once you are satisfied with your changes, run"
426 warn
427 warn " git rebase --continue"
428 warn
429 exit 0
431 squash|s|fixup|f)
432 case "$command" in
433 squash|s)
434 squash_style=squash
436 fixup|f)
437 squash_style=fixup
439 esac
440 comment_for_reflog $squash_style
442 test -f "$DONE" && has_action "$DONE" ||
443 die "Cannot '$squash_style' without a previous commit"
445 mark_action_done
446 update_squash_messages $squash_style $sha1
447 author_script=$(get_author_ident_from_commit HEAD)
448 echo "$author_script" > "$AUTHOR_SCRIPT"
449 eval "$author_script"
450 output git reset --soft HEAD^
451 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
452 case "$(peek_next_command)" in
453 squash|s|fixup|f)
454 # This is an intermediate commit; its message will only be
455 # used in case of trouble. So use the long version:
456 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
457 add_rewritten ||
458 die_failed_squash $sha1 "$rest"
461 # This is the final command of this squash/fixup group
462 if test -f "$FIXUP_MSG"
463 then
464 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
465 add_rewritten ||
466 die_failed_squash $sha1 "$rest"
467 else
468 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
469 rm -f "$GIT_DIR"/MERGE_MSG
470 do_with_author git commit --no-verify -e &&
471 add_rewritten ||
472 die_failed_squash $sha1 "$rest"
474 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
476 esac
478 bookmark|b)
479 mark_action_done
480 git rev-parse --verify HEAD > "$REWRITTEN"/"$sha1"
482 goto|g)
483 comment_for_reflog goto
484 mark_action_done
485 output git reset --hard $(parse_commit $sha1) ||
486 die "Could not reset to $sha1"
488 merge|m)
489 comment_for_reflog merge
490 mark_action_done
491 # this already dies with patch on error
492 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
493 exit
496 warn "Unknown command: $command $sha1 $rest"
497 if git rev-parse --verify -q "$sha1" >/dev/null
498 then
499 die_with_patch $sha1 "Please fix this in the file $TODO."
500 else
501 die "Please fix this in the file $TODO."
504 esac
505 test -s "$TODO" && return
507 comment_for_reflog finish &&
508 HEADNAME=$(cat "$DOTEST"/head-name) &&
509 OLDHEAD=$(cat "$DOTEST"/head) &&
510 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
511 NEWHEAD=$(git rev-parse HEAD) &&
512 case $HEADNAME in
513 refs/*)
514 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
515 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
516 git symbolic-ref HEAD $HEADNAME
518 esac && {
519 test ! -f "$DOTEST"/verbose ||
520 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
521 } &&
522 rm -rf "$DOTEST" &&
523 git gc --auto &&
524 warn "Successfully rebased and updated $HEADNAME."
526 exit
529 do_rest () {
530 while :
532 do_next
533 done
536 # skip picking commits whose parents are unchanged
537 skip_unnecessary_picks () {
538 fd=3
539 while read command sha1 rest
541 # fd=3 means we skip the command
542 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
543 3,pick,"$ONTO"*|3,p,"$ONTO"*)
544 # pick a commit whose parent is current $ONTO -> skip
545 ONTO=$sha1
547 3,#*|3,,*)
548 # copy comments
551 fd=1
553 esac
554 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
555 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
556 mv -f "$TODO".new "$TODO" ||
557 die "Could not skip unnecessary pick commands"
560 # check if no other options are set
561 is_standalone () {
562 test $# -eq 2 -a "$2" = '--' &&
563 test -z "$ONTO" &&
564 test -z "$PRESERVE_MERGES" &&
565 test -z "$STRATEGY" &&
566 test -z "$VERBOSE"
569 get_saved_options () {
570 test -d "$REWRITTEN" && PRESERVE_MERGES=t
571 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
572 test -f "$DOTEST"/verbose && VERBOSE=t
573 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
576 # Rearrange the todo list that has both "pick sha1 msg" and
577 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
578 # comes immediately after the former, and change "pick" to
579 # "fixup"/"squash".
580 rearrange_squash () {
581 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
582 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
583 "$1" >"$1.sq"
584 test -s "$1.sq" || return
586 used=
587 while read pick sha1 message
589 case " $used" in
590 *" $sha1 "*) continue ;;
591 esac
592 echo "$pick $sha1 $message"
593 while read squash action msg
595 case "$message" in
596 "$msg"*)
597 echo "$action $squash $action! $msg"
598 used="$used$squash "
600 esac
601 done <"$1.sq"
602 done >"$1.rearranged" <"$1"
603 cat "$1.rearranged" >"$1"
604 rm -f "$1.sq" "$1.rearranged"
607 LF='
609 parse_onto () {
610 case "$1" in
611 *...*)
612 if left=${1%...*} right=${1#*...} &&
613 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
614 then
615 case "$onto" in
616 ?*"$LF"?* | '')
617 exit 1 ;;
618 esac
619 echo "$onto"
620 exit 0
622 esac
623 git rev-parse --verify "$1^0"
626 prepare_preserve_merges () {
627 # $REWRITTEN/ contains a mapping for each rewritten commit:
628 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
629 mkdir "$REWRITTEN" || die "Could not create directory $REWRITTEN"
630 if test -z "$REBASE_ROOT"
631 then
632 for c in $(git merge-base --all $HEAD $UPSTREAM)
634 echo $ONTO > "$REWRITTEN"/$c ||
635 die "Could not init rewritten commits"
636 done
639 # show merges
640 MERGES_OPTION=--parents
643 handle_dropped_commits () {
644 # Watch for commits that have been dropped by --cherry-pick
645 # The idea is that all commits that are already in upstream
646 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
647 # as if they were rewritten.
649 # Get all patch ids
650 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
651 # So take only the first patch-id for each commit id (uniq -f1).
652 git log -m -p $UPSTREAM..$HEAD | git patch-id |
653 uniq -s 41 > "$REWRITTEN"/ours
654 git log -m -p $HEAD..$UPSTREAM | git patch-id |
655 uniq -s 41 > "$REWRITTEN"/upstream
657 # Now get the correspondences
658 cat "$REWRITTEN"/ours | while read patch_id commit
660 # Is the same patch id in the upstream?
661 grep "^$patch_id " < "$REWRITTEN"/upstream > /dev/null ||
662 continue
664 # Record the parent as "rewritten" commit. As we will resolve
665 # rewritten commits recursively, this will work even if the
666 # parent was rewritten, too.
668 # If there is no parent, then we have a root commit that
669 # was cherry-picked into upstream; let's use $ONTO as
670 # fake parent of that root commit.
671 upstream=$(git rev-parse --verify "$commit^" 2> /dev/null)
672 test ! -z "$upstream" || upstream=$ONTO
673 echo "$upstream" > "$REWRITTEN"/$commit
674 done
677 get_oneline () {
678 git show -s --pretty="format:%h$2 %s" $1
681 generate_script_help () {
682 cat >> "$TODO" << EOF
684 # Rebase $SHORTREVISIONS onto $SHORTONTO
686 # Commands:
687 # p, pick = use commit
688 # r, reword = use commit, but edit the commit message
689 # e, edit = use commit, but stop for amending
690 # s, squash = use commit, but meld into previous commit
691 # f, fixup = like "squash", but discard this commit's log message
692 # g, goto = reset the current state to the given commit
693 # m, merge parents <parents> original <original merge commit>
694 # = redo the given merge commit
695 # b, bookmark = when reaching this command, name the current revision
697 # If you remove a line here THAT COMMIT WILL BE LOST.
698 # However, if you remove everything, the rebase will be aborted.
704 generate_script () {
705 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
706 $REVISIONS | grep '^>')" && {
707 echo noop
708 generate_script_help
709 return
712 current=$SHORTUPSTREAM
713 test -z "$REBASE_ROOT" || current=
714 git rev-list $MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
715 --reverse --left-right --topo-order $REVISIONS |
716 sed -n "s/^>//p" | while read shortsha1 firstparent rest
718 count=$(($count+1))
720 # generate "goto" statements
721 test -z "$PRESERVE_MERGES" || {
722 case "$firstparent" in
723 $current*)
724 # already there
726 $SHORTUPSTREAM*|'')
727 echo "goto $SHORTONTO ($(git show -s --pretty=format:%s $SHORTONTO)))"
730 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
732 esac
733 current=$shortsha1
736 test -z "$rest" && {
737 echo "pick $(get_oneline $shortsha1)"
738 continue
741 # handle merges
742 # TODO: test octopus merge
743 parents=$(echo "$rest" | sed "s/ \|$/'&/g")
744 echo "merge parents $parents original $(get_oneline $shortsha1)"
745 for parent in $rest
747 echo "# parent $(get_oneline $parent \')"
748 done
749 done
751 generate_script_help
754 while test $# != 0
756 case "$1" in
757 --no-verify)
758 OK_TO_SKIP_PRE_REBASE=yes
760 --verify)
762 --continue)
763 is_standalone "$@" || usage
764 get_saved_options
765 comment_for_reflog continue
767 test -d "$DOTEST" || die "No interactive rebase running"
769 # Sanity check
770 git rev-parse --verify HEAD >/dev/null ||
771 die "Cannot read HEAD"
772 git update-index --ignore-submodules --refresh &&
773 git diff-files --quiet --ignore-submodules ||
774 die "Working tree is dirty"
776 # do we have anything to commit?
777 if git diff-index --cached --quiet --ignore-submodules HEAD --
778 then
779 : Nothing to commit
780 test ! -f "$REWRITTEN"/original || add_rewritten
781 else
782 . "$AUTHOR_SCRIPT" ||
783 die "Cannot find the author identity"
784 amend=
785 if test -f "$AMEND"
786 then
787 amend=$(git rev-parse --verify HEAD)
788 test "$amend" = $(cat "$AMEND") ||
789 die "\
790 You have uncommitted changes in your working tree. Please, commit them
791 first and then run 'git rebase --continue' again."
792 git reset --soft HEAD^ ||
793 die "Cannot rewind the HEAD"
795 do_with_author git commit --no-verify -F "$MSG" -e &&
796 add_rewritten || {
797 test -n "$amend" && git reset --soft $amend
798 die "Could not commit staged changes."
802 require_clean_work_tree
803 do_rest
805 --abort)
806 is_standalone "$@" || usage
807 get_saved_options
808 comment_for_reflog abort
810 git rerere clear
811 test -d "$DOTEST" || die "No interactive rebase running"
813 HEADNAME=$(cat "$DOTEST"/head-name)
814 HEAD=$(cat "$DOTEST"/head)
815 case $HEADNAME in
816 refs/*)
817 git symbolic-ref HEAD $HEADNAME
819 esac &&
820 output git reset --hard $HEAD &&
821 rm -rf "$DOTEST"
822 exit
824 --skip)
825 is_standalone "$@" || usage
826 get_saved_options
827 comment_for_reflog skip
829 git rerere clear
830 test -d "$DOTEST" || die "No interactive rebase running"
832 test -d "$REWRITTEN" && {
833 # skip last to-be-rewritten commit
834 original_count=$(wc -l < "$REWRITTEN"/original)
835 test $original_count -gt 0 &&
836 head -n $(($original_count-1)) < "$REWRITTEN"/original \
837 > "$REWRITTEN"/original.new &&
838 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
840 output git reset --hard && do_rest
843 case "$#,$1" in
844 *,*=*)
845 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
846 1,*)
847 usage ;;
849 STRATEGY="-s $2"
850 shift ;;
851 esac
854 # we use merge anyway
857 VERBOSE=t
860 PRESERVE_MERGES=t
863 # yeah, we know
865 --root)
866 REBASE_ROOT=t
868 --autosquash)
869 AUTOSQUASH=t
871 --onto)
872 shift
873 ONTO=$(parse_onto "$1") ||
874 die "Does not point to a valid commit: $1"
877 shift
878 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
879 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
880 test -d "$DOTEST" &&
881 die "Interactive rebase already started"
883 git var GIT_COMMITTER_IDENT >/dev/null ||
884 die "You need to set your committer info first"
886 if test -z "$REBASE_ROOT"
887 then
888 UPSTREAM_ARG="$1"
889 UPSTREAM=$(git rev-parse --verify "$1") ||
890 die "Invalid base"
891 test -z "$ONTO" && ONTO=$UPSTREAM
892 shift
893 else
894 UPSTREAM=
895 UPSTREAM_ARG=--root
896 test -z "$ONTO" &&
897 die "You must specify --onto when using --root"
898 UPSTREAM=$ONTO
900 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
902 comment_for_reflog start
904 require_clean_work_tree
906 if test ! -z "$1"
907 then
908 output git show-ref --verify --quiet "refs/heads/$1" ||
909 die "Invalid branchname: $1"
910 output git checkout "$1" ||
911 die "Could not checkout $1"
914 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
915 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
917 : > "$DOTEST"/interactive || die "Could not mark as interactive"
918 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
919 echo "detached HEAD" > "$DOTEST"/head-name
921 echo $HEAD > "$DOTEST"/head
922 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
923 echo $ONTO > "$DOTEST"/onto
924 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
925 test t = "$VERBOSE" && : > "$DOTEST"/verbose
927 SHORTHEAD=$(git rev-parse --short $HEAD)
928 SHORTONTO=$(git rev-parse --short $ONTO)
929 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
930 REVISIONS=$UPSTREAM...$HEAD
931 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
933 MERGES_OPTION=--no-merges
934 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
936 generate_script > "$TODO"
937 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
940 test t = "$PRESERVE_MERGES" && handle_dropped_commits
942 has_action "$TODO" ||
943 die_abort "Nothing to do"
945 cp "$TODO" "$TODO".backup
946 git_editor "$TODO" ||
947 die_abort "Could not execute editor"
949 has_action "$TODO" ||
950 die_abort "Nothing to do"
952 # once the user uses -p features, implicitely turn -p on
953 ! grep "^\(goto\|merge\) " < "$TODO" > /dev/null ||
954 mkdir -p "$REWRITTEN" ||
955 die "Could not create directory $REWRITTEN/"
957 test -d "$REWRITTEN" || skip_unnecessary_picks
959 git update-ref ORIG_HEAD $HEAD
960 output git checkout $ONTO && do_rest
962 esac
963 shift
964 done