fixup.cc5711424b7ae36276a40c06ede5d95f87ca20f0
[git/dscho.git] / git-rebase--interactive.sh
blobd6d24f17f6d0b562ea2ef713a5bb307d2891f352
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 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
146 comment_for_reflog () {
147 case "$ORIG_REFLOG_ACTION" in
148 ''|rebase*)
149 GIT_REFLOG_ACTION="rebase -i ($1)"
150 export GIT_REFLOG_ACTION
152 esac
155 peek_next_command () {
156 sed -n -e "/^#/d" -e '/^$/d' -e "s/ .*//p" -e "q" < "$TODO"
159 # expects the original commit name(s) in "$REWRITTEN"/original
160 # records the current HEAD as the rewritten commit
161 add_rewritten () {
162 test ! -d "$REWRITTEN" && return
163 rewritten=$(git rev-parse --verify HEAD) &&
164 for original in $(cat "$REWRITTEN"/original)
166 original=$(git-rev-parse "$original") &&
167 echo $rewritten > "$REWRITTEN"/$original || break
168 done &&
169 case "$(peek_next_command)" in
170 squash|s) ;; # do nothing
171 *) rm "$REWRITTEN"/original;;
172 esac ||
173 die "Could not store information about rewritten commit"
176 # if the given commit name ends in an apostrophe, returns the rewritten commit
177 parse_commit () {
178 if test -f "$REWRITTEN/${1%\'}"
179 then
180 rewritten="$(cat "$REWRITTEN/${1%\'}")"
181 else
182 rewritten=$(git rev-parse --verify "${1%\'}") ||
183 die "Cannot turn $1 into a commit name"
185 while test -f "$REWRITTEN"/$rewritten
187 new_rewritten=$(cat "$REWRITTEN"/$rewritten) ||
188 die "Could not read rewritten commit for $1"
189 test $rewritten = $new_rewritten && break
190 rewritten=$new_rewritten
191 done
192 echo $rewritten
195 get_oneline () {
196 git show -s --pretty="format:%h %s" $1
199 last_count=
200 mark_action_done () {
201 sed -e 1q < "$TODO" >> "$DONE"
202 sed -e 1d < "$TODO" >> "$TODO".new
203 mv -f "$TODO".new "$TODO"
204 count=$(sane_grep -c '^[^#]' < "$DONE")
205 total=$(($count+$(sane_grep -c '^[^#]' < "$TODO")))
206 if test "$last_count" != "$count"
207 then
208 last_count=$count
209 printf "Rebasing (%d/%d)\r" $count $total
210 test -z "$VERBOSE" || echo
214 make_patch () {
215 sha1_and_parents="$(git rev-list --parents -1 "$1")"
216 case "$sha1_and_parents" in
217 ?*' '?*' '?*)
218 git diff --cc $sha1_and_parents
220 ?*' '?*)
221 git diff-tree -p "$1^!"
224 echo "Root commit"
226 esac > "$DOTEST"/patch
227 test -f "$MSG" ||
228 commit_message "$1" > "$MSG"
229 test -f "$AUTHOR_SCRIPT" ||
230 get_author_ident_from_commit "$1" > "$AUTHOR_SCRIPT"
233 die_with_patch () {
234 test -d "$REWRITTEN" && git rev-parse "$1" >> "$REWRITTEN"/original
235 make_patch "$1"
236 git rerere
237 die "$2"
240 die_abort () {
241 rm -rf "$DOTEST"
242 die "$1"
245 has_action () {
246 sane_grep '^[^#]' "$1" >/dev/null
249 # Run command with GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
250 # GIT_AUTHOR_DATE exported from the current environment.
251 do_with_author () {
253 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
254 "$@"
258 pick_one () {
259 no_ff=
260 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
261 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
262 test -d "$REWRITTEN" && git rev-parse $sha1 >> "$REWRITTEN"/original
263 if test -n "$REBASE_ROOT"
264 then
265 output git cherry-pick "$@"
266 return
268 parent_sha1=$(git rev-parse --verify $sha1^) ||
269 die "Could not get the parent of $sha1"
270 current_sha1=$(git rev-parse --verify HEAD)
271 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
272 then
273 output git reset --hard $sha1
274 output warn Fast-forward to $(git rev-parse --short $sha1)
275 else
276 output git cherry-pick "$@"
280 merge_one () {
281 cmd="merge $*"
282 test "$1" = parents && shift
283 parents=
284 merge_message="Merge"
285 while test $# -gt 0 -a "original" != "$1" -a "as" != "$1"
287 parents="$parents $(parse_commit $1)" ||
288 die "Could not interpret '$1' as a commit"
289 merge_message="$merge_message $1"
290 shift
291 done
293 if test -z "$2"
294 then
295 sha1=
296 else
297 sha1=$(git rev-parse --verify "$2") ||
298 die "Invalid original merge commit: $2"
299 if test -z "$3"
300 then
301 merge_message="$(git cat-file commit $sha1 |
302 sed '1,/^$/d')"
306 if test ! -z "$3"
307 then
308 shift; shift;
309 merge_message="$*"
312 # the command was "merge parents ...", so "parents" was recorded
313 # TODO: detect non-fast-forwards properly
314 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
315 git merge $parents &&
316 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
317 then
318 git commit --amend -m "$merge_message" &&
319 if test !-z "$sha1"
320 then
321 echo "$sha1" > "$REWRITTEN"/original &&
322 add_rewritten
324 fi ||
325 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
328 handle_topic_branch () {
329 test -z "$2" && die "Empty 'topic' command"
330 command=$1
331 topic=$2
332 shift; shift
333 case "$command" in
334 begin)
335 git rev-parse --verify HEAD \
336 > "$REWRITTEN"/merge-parent-of-"$topic" &&
337 git reset --hard $(parse_commit \
338 $(test at = "$1" && echo "$2" || echo onto))
340 end)
341 git rev-parse --verify HEAD > "$REWRITTEN"/"$topic" &&
342 git reset --hard $(parse_commit merge-parent-of-"$topic") &&
343 merge_one parents "$topic" original "" "$*"
346 die "Unknown 'topic' command: $command $topic $*"
348 esac
351 nth_string () {
352 case "$1" in
353 *1[0-9]|*[04-9]) echo "$1"th;;
354 *1) echo "$1"st;;
355 *2) echo "$1"nd;;
356 *3) echo "$1"rd;;
357 esac
360 update_squash_messages () {
361 if test -f "$SQUASH_MSG"; then
362 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
363 COUNT=$(($(sed -n \
364 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
365 -e "q" < "$SQUASH_MSG".bak)+1))
367 echo "# This is a combination of $COUNT commits."
368 sed -e 1d -e '2,/^./{
369 /^$/d
370 }' <"$SQUASH_MSG".bak
371 } >"$SQUASH_MSG"
372 else
373 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
374 COUNT=2
376 echo "# This is a combination of 2 commits."
377 echo "# The first commit's message is:"
378 echo
379 cat "$FIXUP_MSG"
380 } >"$SQUASH_MSG"
382 case $1 in
383 squash)
384 rm -f "$FIXUP_MSG"
385 echo
386 echo "# This is the $(nth_string $COUNT) commit message:"
387 echo
388 commit_message $2
390 fixup)
391 echo
392 echo "# The $(nth_string $COUNT) commit message will be skipped:"
393 echo
394 commit_message $2 | sed -e 's/^/# /'
396 esac >>"$SQUASH_MSG"
399 # A squash/fixup has failed. Prepare the long version of the squash
400 # commit message, then die_with_patch. This code path requires the
401 # user to edit the combined commit message for all commits that have
402 # been squashed/fixedup so far. So also erase the old squash
403 # messages, effectively causing the combined commit to be used as the
404 # new basis for any further squash/fixups. Args: sha1 rest
405 die_failed_squash() {
406 mv "$SQUASH_MSG" "$MSG" || exit
407 rm -f "$FIXUP_MSG"
408 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
409 warn
410 warn "Could not apply $1... $2"
411 die_with_patch $1 ""
414 do_next () {
415 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
416 read command sha1 rest < "$TODO"
417 case "$command" in
418 '#'*|''|noop)
419 mark_action_done
421 pick|p)
422 comment_for_reflog pick
424 mark_action_done
425 # TODO: give better description/output in the "Nothing to be committed" and
426 # the conflict case
427 pick_one $sha1 && add_rewritten ||
428 die_with_patch $sha1 "Could not apply $sha1... $rest"
430 reword|r)
431 comment_for_reflog reword
433 mark_action_done
434 pick_one $sha1 ||
435 die_with_patch $sha1 "Could not apply $sha1... $rest"
436 git commit --amend
438 edit|e)
439 comment_for_reflog edit
441 mark_action_done
442 pick_one $sha1 ||
443 die_with_patch $sha1 "Could not apply $sha1... $rest"
444 make_patch $sha1
445 git rev-parse --verify HEAD > "$AMEND"
446 warn "Stopped at $sha1... $rest"
447 warn "You can amend the commit now, with"
448 warn
449 warn " git commit --amend"
450 warn
451 warn "Once you are satisfied with your changes, run"
452 warn
453 warn " git rebase --continue"
454 warn
455 exit 0
457 squash|s|fixup|f)
458 case "$command" in
459 squash|s)
460 squash_style=squash
462 fixup|f)
463 squash_style=fixup
465 esac
466 comment_for_reflog $squash_style
468 test -f "$DONE" && has_action "$DONE" ||
469 die "Cannot '$squash_style' without a previous commit"
471 mark_action_done
472 update_squash_messages $squash_style $sha1
473 author_script=$(get_author_ident_from_commit HEAD)
474 echo "$author_script" > "$AUTHOR_SCRIPT"
475 eval "$author_script"
476 output git reset --soft HEAD^
477 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
478 case "$(peek_next_command)" in
479 squash|s|fixup|f)
480 # This is an intermediate commit; its message will only be
481 # used in case of trouble. So use the long version:
482 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
483 add_rewritten ||
484 die_failed_squash $sha1 "$rest"
487 # This is the final command of this squash/fixup group
488 if test -f "$FIXUP_MSG"
489 then
490 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
491 add_rewritten ||
492 die_failed_squash $sha1 "$rest"
493 else
494 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
495 rm -f "$GIT_DIR"/MERGE_MSG
496 do_with_author git commit --no-verify -e &&
497 add_rewritten ||
498 die_failed_squash $sha1 "$rest"
500 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
502 esac
504 bookmark|b)
505 mark_action_done
506 git rev-parse --verify HEAD > "$REWRITTEN"/"$sha1"
508 goto|g)
509 comment_for_reflog goto
510 mark_action_done
511 output git reset --hard $(parse_commit $sha1) ||
512 die "Could not reset to $sha1"
514 merge|m)
515 comment_for_reflog merge
516 mark_action_done
517 # this already dies with patch on error
518 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
519 exit
521 topic|t)
522 comment_for_reflog "topic($sha1)"
523 mark_action_done
524 output handle_topic_branch "$sha1" $rest ||
525 exit
528 warn "Unknown command: $command $sha1 $rest"
529 if git rev-parse --verify -q "$sha1" >/dev/null
530 then
531 die_with_patch $sha1 "Please fix this in the file $TODO."
532 else
533 die "Please fix this in the file $TODO."
536 esac
537 test -s "$TODO" && return
539 if test -d "$REWRITTEN"
540 then
541 missed_tips=$(git rev-list ^HEAD \
542 $(cat "$REWRITTEN"/* | sed "s/$/^!/") \
543 $(ls "$REWRITTEN" |
544 sed -n "s/^\([0-9A-Fa-f]\{40\}\)$/^\1/p"))
545 test -z "$missed_tips" || {
546 echo "The following branch tips are missing from HEAD:"
547 echo
548 for commit in $missed_tips
550 echo "$(get_oneline $commit)"
551 done
552 exit 1
556 comment_for_reflog finish &&
557 HEADNAME=$(cat "$DOTEST"/head-name) &&
558 OLDHEAD=$(cat "$DOTEST"/head) &&
559 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
560 NEWHEAD=$(git rev-parse HEAD) &&
561 case $HEADNAME in
562 refs/*)
563 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
564 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
565 git symbolic-ref HEAD $HEADNAME
567 esac && {
568 test ! -f "$DOTEST"/verbose ||
569 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
570 } &&
571 rm -rf "$DOTEST" &&
572 git gc --auto &&
573 warn "Successfully rebased and updated $HEADNAME."
575 exit
578 do_rest () {
579 while :
581 do_next
582 done
585 # skip picking commits whose parents are unchanged
586 skip_unnecessary_picks () {
587 fd=3
588 while read command sha1 rest
590 # fd=3 means we skip the command
591 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
592 3,pick,"$ONTO"*|3,p,"$ONTO"*)
593 # pick a commit whose parent is current $ONTO -> skip
594 ONTO=$sha1
596 3,#*|3,,*)
597 # copy comments
600 fd=1
602 esac
603 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
604 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
605 mv -f "$TODO".new "$TODO" ||
606 die "Could not skip unnecessary pick commands"
609 # check if no other options are set
610 is_standalone () {
611 test $# -eq 2 -a "$2" = '--' &&
612 test -z "$ONTO" &&
613 test -z "$PRESERVE_MERGES" &&
614 test -z "$STRATEGY" &&
615 test -z "$VERBOSE"
618 get_saved_options () {
619 test -d "$REWRITTEN" && PRESERVE_MERGES=t
620 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
621 test -f "$DOTEST"/verbose && VERBOSE=t
622 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
625 # Rearrange the todo list that has both "pick sha1 msg" and
626 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
627 # comes immediately after the former, and change "pick" to
628 # "fixup"/"squash".
629 rearrange_squash () {
630 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
631 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
632 "$1" >"$1.sq"
633 test -s "$1.sq" || return
635 used=
636 while read pick sha1 message
638 case " $used" in
639 *" $sha1 "*) continue ;;
640 esac
641 echo "$pick $sha1 $message"
642 while read squash action msg
644 case "$message" in
645 "$msg"*)
646 echo "$action $squash $action! $msg"
647 used="$used$squash "
649 esac
650 done <"$1.sq"
651 done >"$1.rearranged" <"$1"
652 cat "$1.rearranged" >"$1"
653 rm -f "$1.sq" "$1.rearranged"
656 LF='
658 parse_onto () {
659 case "$1" in
660 *...*)
661 if left=${1%...*} right=${1#*...} &&
662 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
663 then
664 case "$onto" in
665 ?*"$LF"?* | '')
666 exit 1 ;;
667 esac
668 echo "$onto"
669 exit 0
671 esac
672 git rev-parse --verify "$1^0"
675 prepare_preserve_merges () {
676 # $REWRITTEN/ contains a mapping for each rewritten commit:
677 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
678 mkdir "$REWRITTEN" || die "Could not create directory $REWRITTEN"
679 if test -z "$REBASE_ROOT"
680 then
681 for c in $(git merge-base --all $HEAD $UPSTREAM)
683 echo $ONTO > "$REWRITTEN"/$c ||
684 die "Could not init rewritten commits"
685 done
686 else
687 echo $ONTO > "$REWRITTEN"/root
689 echo $ONTO > "$REWRITTEN"/onto
690 echo $UPSTREAM > "$REWRITTEN"/upstream
692 # show merges
693 MERGES_OPTION=--parents
696 handle_dropped_commits () {
697 # Watch for commits that have been dropped by --cherry-pick
698 # The idea is that all commits that are already in upstream
699 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
700 # as if they were rewritten.
702 # Get all patch ids
703 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
704 # So take only the first patch-id for each commit id (uniq -f1).
705 git log -m -p $UPSTREAM..$HEAD | git patch-id |
706 uniq -s 41 > "$REWRITTEN"/ours
707 git log -m -p $HEAD..$UPSTREAM | git patch-id |
708 uniq -s 41 > "$REWRITTEN"/upstream
710 # Now get the correspondences
711 cat "$REWRITTEN"/ours | while read patch_id commit
713 # Is the same patch id in the upstream?
714 grep "^$patch_id " < "$REWRITTEN"/upstream > /dev/null ||
715 continue
717 # Record the parent as "rewritten" commit. As we will resolve
718 # rewritten commits recursively, this will work even if the
719 # parent was rewritten, too.
721 # If there is no parent, then we have a root commit that
722 # was cherry-picked into upstream; let's use $ONTO as
723 # fake parent of that root commit.
724 upstream=$(git rev-parse --verify "$commit^" 2> /dev/null)
725 test ! -z "$upstream" || upstream=$ONTO
726 echo "$upstream" > "$REWRITTEN"/$commit
727 done
728 rm -f "$REWRITTEN"/ours "$REWRITTEN"/upstream
731 generate_script_help () {
732 cat >> "$TODO" << EOF
734 # Rebase $SHORTREVISIONS onto $SHORTONTO
736 # Commands:
737 # p, pick = use commit
738 # r, reword = use commit, but edit the commit message
739 # e, edit = use commit, but stop for amending
740 # s, squash = use commit, but meld into previous commit
741 # f, fixup = like "squash", but discard this commit's log message
742 # g, goto = reset the current state to the given commit
743 # m, merge parents <parents> original <original merge commit>
744 # = redo the given merge commit
745 # b, bookmark = when reaching this command, name the current revision
747 # If you remove a line here THAT COMMIT WILL BE LOST.
748 # However, if you remove everything, the rebase will be aborted.
754 generate_script () {
755 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
756 $REVISIONS | grep '^>')" && {
757 echo noop
758 generate_script_help
759 return
762 current=$SHORTUPSTREAM
763 test -z "$REBASE_ROOT" || current=
764 git rev-list $MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
765 --reverse --left-right --topo-order $REVISIONS |
766 sed -n "s/^>//p" | while read shortsha1 firstparent rest
768 count=$(($count+1))
770 # generate "goto" statements
771 test -z "$PRESERVE_MERGES" || {
772 case "$firstparent" in
773 $current*)
774 # already there
776 $SHORTUPSTREAM*|'')
777 echo 'goto onto'
780 # TODO: a failed merge at the end should not let the rebase succeed
781 # TODO: avoid outputting anything when current is SHORTUPSTREAM and
782 # firstparent is a merge base
783 # TODO: use get_oneline again
784 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
786 esac
787 current=$shortsha1
790 test -z "$rest" && {
791 echo "pick $(get_oneline $shortsha1)"
792 continue
795 # handle merges
796 # TODO: test octopus merge
797 echo "merge parents $rest original $(get_oneline $shortsha1)"
798 for parent in $rest
800 # TODO: add information about "dropped" commits, just because we can
801 echo "# parent $(get_oneline $parent)"
802 done
803 done
805 generate_script_help
808 commit2bookmark () {
809 echo "$BOOKMARKS" | sed -n "s/^$sha1[^ ]* //p"
812 bookmark_exists () {
813 echo $1 | grep '^[0-9A-Fa-f]\{40\}$' > /dev/null && return
814 test -f "$REWRITTEN"/$1 && return
815 echo "$BOOKMARKS" | grep "^[^ ]* $1$" > /dev/null
818 name_bookmark () {
819 # make sure that the commit-to-bookmark will be rewritten
820 grep "^\(pick\|merge .* original\) $1 " < "$TODO" > /dev/null || return
821 test -z "$(commit2bookmark $1)" && {
822 bookmark="$(echo "$2" | tr -c 'A-Za-z0-9_' '-' |
823 sed -e 's/^-*//' -e 's/--*/-/g' -e 's/-*$//')"
824 bookmark_exists $bookmark && {
826 while bookmark_exists $bookmark-$i
828 i=$(($i+1))
829 done
830 bookmark=$bookmark-$i
832 BOOKMARKS="$(echo "$BOOKMARKS"; echo "$1 $bookmark")"
833 echo "$1 $bookmark"
837 make_bookmarks () {
838 test ! -d "$REWRITTEN" && return
839 BOOKMARKS="$(git show -s --pretty=format:'%h onto' \
840 $(git merge-base --all "$HEAD" "$UPSTREAM") |
841 grep -v '^$')"
843 # Merges make a nicer nick name, so take them first
844 message=
845 tac "$TODO" |
846 while read command sha1 rest
848 case "$command" in
849 goto)
850 # reuse merge messages for merge-parents
851 if test -z "$message"
852 then
853 message=$(git show -s --pretty=format:%s "$sha1")
854 else
855 message="merge-parent-of-$message"
857 test onto = "$sha1" && continue
858 name_bookmark "$sha1" "$message"
859 message=
861 merge)
862 rest=${rest#parents }
863 sha1s="$sha1 ${rest%% original *}"
864 message=$(echo "${rest#* original * }" |
865 sed -e 's/^Merge \(.\)/\1/' \
866 -e "s/.*'\(.*\)'.*/\1/")
867 for sha1 in $sha1s
869 name_bookmark "$sha1" "$message"
870 done
873 message=
875 esac
876 done > "$DOTEST"/bookmarks
878 # Make a sed script to rewrite the rebase script
879 cat "$DOTEST/bookmarks" |
880 while read sha1 bookmark
882 for command in \
883 's/^pick %s .*/&\\nbookmark %s/' \
884 's/^merge .* original %s .*/&\\nbookmark %s/' \
885 's/^goto %s .*/goto %s/' \
886 's/^\\(merge .* \\)%s\\(.* original .*\\)$/\\1%s\\2/'
888 printf "$command\\n" $sha1 $bookmark
889 done
890 done > "$DOTEST"/bookmarks.sed
892 # Now rewrite the rebase script to make use of the bookmarks
893 test ! -s "$DOTEST"/bookmarks.sed || {
894 sed -f "$DOTEST"/bookmarks.sed < "$TODO" > "$TODO".bookmarks &&
895 # write short form of typical topic commands:
897 # topic begin blabla at blub
899 # instead of
901 # bookmark merge-parent-of-blabla
902 # goto blub
904 # and
906 # topic end blabla Merge 'blabla'
908 # instead of
910 # bookmark blabla
911 # goto merge-parent-of-blabla
912 # merge parents blabla original 0123456 Merge 'blabla'
913 while read line
915 case "$line" in
916 'bookmark '*)
917 topic=${line#bookmark }
918 topic2=${topic#merge-parent-of-}
919 read line2
920 # skip comments (after a merge)
921 while case "$line2" in \#*) ;; *) break ;; esac
923 line="$(echo "$line"; echo "$line2")"
924 read line2
925 done
926 case "$topic,$line2" in
927 "$topic,goto merge-parent-of-$topic")
928 read line3
929 case "$line3" in
930 "merge parents $topic"*)
931 msg="$(echo "$line3" |
932 cut -f 6- -d ' ')"
933 test "Merge '$topic'" = "$msg" &&
934 msg=
935 echo "topic end $topic $msg"
938 echo "$line"
939 echo "$line2"
940 echo "$line3"
941 esac
943 "merge-parent-of-$topic2,goto "*)
944 goto="at ${line2#goto }"
945 test 'at onto' = "$goto" && goto=
946 echo "topic begin $topic2 $goto"
949 echo "$line"
950 echo "$line2"
951 esac
954 echo "$line"
955 esac
956 done < "$TODO".bookmarks > "$TODO"
957 } || die 'Could not rewrite rebase script using bookmarks'
958 # TODO: maybe it is possible to rewrite in one go? Think about it
959 # TODO: topic end seems not to work when merging branch1, committing to branch1
960 # and merging it again
963 while test $# != 0
965 case "$1" in
966 --no-verify)
967 OK_TO_SKIP_PRE_REBASE=yes
969 --verify)
971 --continue)
972 is_standalone "$@" || usage
973 get_saved_options
974 comment_for_reflog continue
976 test -d "$DOTEST" || die "No interactive rebase running"
978 # Sanity check
979 git rev-parse --verify HEAD >/dev/null ||
980 die "Cannot read HEAD"
981 git update-index --ignore-submodules --refresh &&
982 git diff-files --quiet --ignore-submodules ||
983 die "Working tree is dirty"
985 # do we have anything to commit?
986 if git diff-index --cached --quiet --ignore-submodules HEAD --
987 then
988 : Nothing to commit
989 test ! -f "$REWRITTEN"/original || add_rewritten
990 else
991 . "$AUTHOR_SCRIPT" ||
992 die "Cannot find the author identity"
993 amend=
994 if test -f "$AMEND"
995 then
996 amend=$(git rev-parse --verify HEAD)
997 test "$amend" = $(cat "$AMEND") ||
998 die "\
999 You have uncommitted changes in your working tree. Please, commit them
1000 first and then run 'git rebase --continue' again."
1001 git reset --soft HEAD^ ||
1002 die "Cannot rewind the HEAD"
1004 do_with_author git commit --no-verify -F "$MSG" -e &&
1005 add_rewritten || {
1006 test -n "$amend" && git reset --soft $amend
1007 die "Could not commit staged changes."
1011 require_clean_work_tree
1012 do_rest
1014 --abort)
1015 is_standalone "$@" || usage
1016 get_saved_options
1017 comment_for_reflog abort
1019 git rerere clear
1020 test -d "$DOTEST" || die "No interactive rebase running"
1022 HEADNAME=$(cat "$DOTEST"/head-name)
1023 HEAD=$(cat "$DOTEST"/head)
1024 case $HEADNAME in
1025 refs/*)
1026 git symbolic-ref HEAD $HEADNAME
1028 esac &&
1029 output git reset --hard $HEAD &&
1030 rm -rf "$DOTEST"
1031 exit
1033 --skip)
1034 is_standalone "$@" || usage
1035 get_saved_options
1036 comment_for_reflog skip
1038 git rerere clear
1039 test -d "$DOTEST" || die "No interactive rebase running"
1041 test -d "$REWRITTEN" && {
1042 # skip last to-be-rewritten commit
1043 original_count=$(wc -l < "$REWRITTEN"/original)
1044 test $original_count -gt 0 &&
1045 head -n $(($original_count-1)) < "$REWRITTEN"/original \
1046 > "$REWRITTEN"/original.new &&
1047 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
1049 output git reset --hard && do_rest
1052 case "$#,$1" in
1053 *,*=*)
1054 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1055 1,*)
1056 usage ;;
1058 STRATEGY="-s $2"
1059 shift ;;
1060 esac
1063 # we use merge anyway
1066 VERBOSE=t
1069 # TODO: add an option to rewrite refs that point to rewritten commits
1070 # TODO: add a big fat warning at the end when there are rewritten commits
1071 # that are now unreachable
1072 PRESERVE_MERGES=t
1075 # yeah, we know
1077 --root)
1078 REBASE_ROOT=t
1080 --autosquash)
1081 AUTOSQUASH=t
1083 --onto)
1084 shift
1085 ONTO=$(parse_onto "$1") ||
1086 die "Does not point to a valid commit: $1"
1089 shift
1090 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
1091 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
1092 test -d "$DOTEST" &&
1093 die "Interactive rebase already started"
1095 git var GIT_COMMITTER_IDENT >/dev/null ||
1096 die "You need to set your committer info first"
1098 if test -z "$REBASE_ROOT"
1099 then
1100 UPSTREAM_ARG="$1"
1101 UPSTREAM=$(git rev-parse --verify "$1") ||
1102 die "Invalid base"
1103 test -z "$ONTO" && ONTO=$UPSTREAM
1104 shift
1105 else
1106 UPSTREAM=
1107 UPSTREAM_ARG=--root
1108 test -z "$ONTO" &&
1109 die "You must specify --onto when using --root"
1110 UPSTREAM=$ONTO
1112 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
1114 comment_for_reflog start
1116 require_clean_work_tree
1118 if test ! -z "$1"
1119 then
1120 output git show-ref --verify --quiet "refs/heads/$1" ||
1121 die "Invalid branchname: $1"
1122 output git checkout "$1" ||
1123 die "Could not checkout $1"
1126 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
1127 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
1129 : > "$DOTEST"/interactive || die "Could not mark as interactive"
1130 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1131 echo "detached HEAD" > "$DOTEST"/head-name
1133 echo $HEAD > "$DOTEST"/head
1134 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
1135 echo $ONTO > "$DOTEST"/onto
1136 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1137 test t = "$VERBOSE" && : > "$DOTEST"/verbose
1139 SHORTHEAD=$(git rev-parse --short $HEAD)
1140 SHORTONTO=$(git rev-parse --short $ONTO)
1141 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
1142 REVISIONS=$UPSTREAM...$HEAD
1143 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
1145 MERGES_OPTION=--no-merges
1146 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
1148 generate_script > "$TODO"
1149 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1151 make_bookmarks
1153 test t = "$PRESERVE_MERGES" && handle_dropped_commits
1155 has_action "$TODO" ||
1156 die_abort "Nothing to do"
1158 cp "$TODO" "$TODO".backup
1159 git_editor "$TODO" ||
1160 die_abort "Could not execute editor"
1162 has_action "$TODO" ||
1163 die_abort "Nothing to do"
1165 # once the user uses -p features, implicitely turn -p on
1166 ! grep "^\(goto\|merge\) " < "$TODO" > /dev/null ||
1167 mkdir -p "$REWRITTEN" ||
1168 die "Could not create directory $REWRITTEN/"
1170 test -d "$REWRITTEN" || skip_unnecessary_picks
1172 git update-ref ORIG_HEAD $HEAD
1173 output git checkout $ONTO && do_rest
1175 esac
1176 shift
1177 done