rebase -i -p: use better bookmark names for merge parents
[git/dscho.git] / git-rebase--interactive.sh
blob21efa7f6d4ba8437328ed1d0cb3cb507e68950b9
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 get_oneline () {
205 git show -s --pretty="format:%h %s" $1
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 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
271 test -d "$REWRITTEN" && git rev-parse $sha1 >> "$REWRITTEN"/original
272 if test -n "$REBASE_ROOT"
273 then
274 output git cherry-pick "$@"
275 return
277 parent_sha1=$(git rev-parse --verify $sha1^) ||
278 die "Could not get the parent of $sha1"
279 current_sha1=$(git rev-parse --verify HEAD)
280 if test -z "$no_ff" && test "$current_sha1" = "$parent_sha1"
281 then
282 output git reset --hard $sha1
283 output warn Fast-forward to $(git rev-parse --short $sha1)
284 else
285 output git cherry-pick "$@"
289 merge_one () {
290 cmd="merge $*"
291 test "$1" = parents && shift
292 parents=
293 merge_message="Merge"
294 while test $# -gt 0 -a "original" != "$1" -a "as" != "$1"
296 parents="$parents $(parse_commit $1)" ||
297 die "Could not interpret '$1' as a commit"
298 merge_message="$merge_message $1"
299 shift
300 done
302 if test -z "$2"
303 then
304 sha1=
305 else
306 sha1=$(git rev-parse "$2")
309 if test ! -z "$3"
310 then
311 shift; shift;
312 merge_message="$*"
315 # the command was "merge parents ...", so "parents" was recorded
316 # TODO: detect non-fast-forwards properly
317 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
318 git merge $parents &&
319 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
320 then
321 git commit --amend -m "$merge_message" &&
322 if test !-z "$sha1"
323 then
324 echo "$sha1" > "$REWRITTEN"/original &&
325 add_rewritten
327 fi ||
328 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
331 nth_string () {
332 case "$1" in
333 *1[0-9]|*[04-9]) echo "$1"th;;
334 *1) echo "$1"st;;
335 *2) echo "$1"nd;;
336 *3) echo "$1"rd;;
337 esac
340 update_squash_messages () {
341 if test -f "$SQUASH_MSG"; then
342 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
343 COUNT=$(($(sed -n \
344 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
345 -e "q" < "$SQUASH_MSG".bak)+1))
347 echo "# This is a combination of $COUNT commits."
348 sed -e 1d -e '2,/^./{
349 /^$/d
350 }' <"$SQUASH_MSG".bak
351 } >"$SQUASH_MSG"
352 else
353 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
354 COUNT=2
356 echo "# This is a combination of 2 commits."
357 echo "# The first commit's message is:"
358 echo
359 cat "$FIXUP_MSG"
360 } >"$SQUASH_MSG"
362 case $1 in
363 squash)
364 rm -f "$FIXUP_MSG"
365 echo
366 echo "# This is the $(nth_string $COUNT) commit message:"
367 echo
368 commit_message $2
370 fixup)
371 echo
372 echo "# The $(nth_string $COUNT) commit message will be skipped:"
373 echo
374 commit_message $2 | sed -e 's/^/# /'
376 esac >>"$SQUASH_MSG"
379 # A squash/fixup has failed. Prepare the long version of the squash
380 # commit message, then die_with_patch. This code path requires the
381 # user to edit the combined commit message for all commits that have
382 # been squashed/fixedup so far. So also erase the old squash
383 # messages, effectively causing the combined commit to be used as the
384 # new basis for any further squash/fixups. Args: sha1 rest
385 die_failed_squash() {
386 mv "$SQUASH_MSG" "$MSG" || exit
387 rm -f "$FIXUP_MSG"
388 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
389 warn
390 warn "Could not apply $1... $2"
391 die_with_patch $1 ""
394 do_next () {
395 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
396 read command sha1 rest < "$TODO"
397 case "$command" in
398 '#'*|''|noop)
399 mark_action_done
401 pick|p)
402 comment_for_reflog pick
404 mark_action_done
405 pick_one $sha1 && add_rewritten ||
406 die_with_patch $sha1 "Could not apply $sha1... $rest"
408 reword|r)
409 comment_for_reflog reword
411 mark_action_done
412 pick_one $sha1 ||
413 die_with_patch $sha1 "Could not apply $sha1... $rest"
414 git commit --amend
416 edit|e)
417 comment_for_reflog edit
419 mark_action_done
420 pick_one $sha1 ||
421 die_with_patch $sha1 "Could not apply $sha1... $rest"
422 make_patch $sha1
423 git rev-parse --verify HEAD > "$AMEND"
424 warn "Stopped at $sha1... $rest"
425 warn "You can amend the commit now, with"
426 warn
427 warn " git commit --amend"
428 warn
429 warn "Once you are satisfied with your changes, run"
430 warn
431 warn " git rebase --continue"
432 warn
433 exit 0
435 squash|s|fixup|f)
436 case "$command" in
437 squash|s)
438 squash_style=squash
440 fixup|f)
441 squash_style=fixup
443 esac
444 comment_for_reflog $squash_style
446 test -f "$DONE" && has_action "$DONE" ||
447 die "Cannot '$squash_style' without a previous commit"
449 mark_action_done
450 update_squash_messages $squash_style $sha1
451 author_script=$(get_author_ident_from_commit HEAD)
452 echo "$author_script" > "$AUTHOR_SCRIPT"
453 eval "$author_script"
454 output git reset --soft HEAD^
455 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
456 case "$(peek_next_command)" in
457 squash|s|fixup|f)
458 # This is an intermediate commit; its message will only be
459 # used in case of trouble. So use the long version:
460 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
461 add_rewritten ||
462 die_failed_squash $sha1 "$rest"
465 # This is the final command of this squash/fixup group
466 if test -f "$FIXUP_MSG"
467 then
468 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
469 add_rewritten ||
470 die_failed_squash $sha1 "$rest"
471 else
472 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
473 rm -f "$GIT_DIR"/MERGE_MSG
474 do_with_author git commit --no-verify -e &&
475 add_rewritten ||
476 die_failed_squash $sha1 "$rest"
478 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
480 esac
482 bookmark|b)
483 mark_action_done
484 git rev-parse --verify HEAD > "$REWRITTEN"/"$sha1"
486 goto|g)
487 comment_for_reflog goto
488 mark_action_done
489 output git reset --hard $(parse_commit $sha1) ||
490 die "Could not reset to $sha1"
492 merge|m)
493 comment_for_reflog merge
494 mark_action_done
495 # this already dies with patch on error
496 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
497 exit
500 warn "Unknown command: $command $sha1 $rest"
501 if git rev-parse --verify -q "$sha1" >/dev/null
502 then
503 die_with_patch $sha1 "Please fix this in the file $TODO."
504 else
505 die "Please fix this in the file $TODO."
508 esac
509 test -s "$TODO" && return
511 if test -d "$REWRITTEN"
512 then
513 missed_tips=$(git rev-list ^HEAD \
514 $(cat "$REWRITTEN"/* | sed "s/$/^!/") \
515 $(ls "$REWRITTEN" |
516 sed -n "s/^\([0-9A-Fa-f]\{40\}\)$/^\1/p"))
517 test -z "$missed_tips" || {
518 echo "The following branch tips are missing from HEAD:"
519 echo
520 for commit in $missed_tips
522 echo "$(get_oneline $commit)"
523 done
524 exit 1
528 comment_for_reflog finish &&
529 HEADNAME=$(cat "$DOTEST"/head-name) &&
530 OLDHEAD=$(cat "$DOTEST"/head) &&
531 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
532 NEWHEAD=$(git rev-parse HEAD) &&
533 case $HEADNAME in
534 refs/*)
535 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
536 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
537 git symbolic-ref HEAD $HEADNAME
539 esac && {
540 test ! -f "$DOTEST"/verbose ||
541 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
542 } &&
543 rm -rf "$DOTEST" &&
544 git gc --auto &&
545 warn "Successfully rebased and updated $HEADNAME."
547 exit
550 do_rest () {
551 while :
553 do_next
554 done
557 # skip picking commits whose parents are unchanged
558 skip_unnecessary_picks () {
559 fd=3
560 while read command sha1 rest
562 # fd=3 means we skip the command
563 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
564 3,pick,"$ONTO"*|3,p,"$ONTO"*)
565 # pick a commit whose parent is current $ONTO -> skip
566 ONTO=$sha1
568 3,#*|3,,*)
569 # copy comments
572 fd=1
574 esac
575 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
576 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
577 mv -f "$TODO".new "$TODO" ||
578 die "Could not skip unnecessary pick commands"
581 # check if no other options are set
582 is_standalone () {
583 test $# -eq 2 -a "$2" = '--' &&
584 test -z "$ONTO" &&
585 test -z "$PRESERVE_MERGES" &&
586 test -z "$STRATEGY" &&
587 test -z "$VERBOSE"
590 get_saved_options () {
591 test -d "$REWRITTEN" && PRESERVE_MERGES=t
592 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
593 test -f "$DOTEST"/verbose && VERBOSE=t
594 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
597 # Rearrange the todo list that has both "pick sha1 msg" and
598 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
599 # comes immediately after the former, and change "pick" to
600 # "fixup"/"squash".
601 rearrange_squash () {
602 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
603 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
604 "$1" >"$1.sq"
605 test -s "$1.sq" || return
607 used=
608 while read pick sha1 message
610 case " $used" in
611 *" $sha1 "*) continue ;;
612 esac
613 echo "$pick $sha1 $message"
614 while read squash action msg
616 case "$message" in
617 "$msg"*)
618 echo "$action $squash $action! $msg"
619 used="$used$squash "
621 esac
622 done <"$1.sq"
623 done >"$1.rearranged" <"$1"
624 cat "$1.rearranged" >"$1"
625 rm -f "$1.sq" "$1.rearranged"
628 LF='
630 parse_onto () {
631 case "$1" in
632 *...*)
633 if left=${1%...*} right=${1#*...} &&
634 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
635 then
636 case "$onto" in
637 ?*"$LF"?* | '')
638 exit 1 ;;
639 esac
640 echo "$onto"
641 exit 0
643 esac
644 git rev-parse --verify "$1^0"
647 prepare_preserve_merges () {
648 # $REWRITTEN/ contains a mapping for each rewritten commit:
649 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
650 mkdir "$REWRITTEN" || die "Could not create directory $REWRITTEN"
651 if test -z "$REBASE_ROOT"
652 then
653 for c in $(git merge-base --all $HEAD $UPSTREAM)
655 echo $ONTO > "$REWRITTEN"/$c ||
656 die "Could not init rewritten commits"
657 done
658 else
659 echo $ONTO > "$REWRITTEN"/root
661 echo $ONTO > "$REWRITTEN"/onto
662 echo $UPSTREAM > "$REWRITTEN"/upstream
664 # show merges
665 MERGES_OPTION=--parents
668 handle_dropped_commits () {
669 # Watch for commits that have been dropped by --cherry-pick
670 # The idea is that all commits that are already in upstream
671 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
672 # as if they were rewritten.
674 # Get all patch ids
675 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
676 # So take only the first patch-id for each commit id (uniq -f1).
677 git log -m -p $UPSTREAM..$HEAD | git patch-id |
678 uniq -s 41 > "$REWRITTEN"/ours
679 git log -m -p $HEAD..$UPSTREAM | git patch-id |
680 uniq -s 41 > "$REWRITTEN"/upstream
682 # Now get the correspondences
683 cat "$REWRITTEN"/ours | while read patch_id commit
685 # Is the same patch id in the upstream?
686 grep "^$patch_id " < "$REWRITTEN"/upstream > /dev/null ||
687 continue
689 # Record the parent as "rewritten" commit. As we will resolve
690 # rewritten commits recursively, this will work even if the
691 # parent was rewritten, too.
693 # If there is no parent, then we have a root commit that
694 # was cherry-picked into upstream; let's use $ONTO as
695 # fake parent of that root commit.
696 upstream=$(git rev-parse --verify "$commit^" 2> /dev/null)
697 test ! -z "$upstream" || upstream=$ONTO
698 echo "$upstream" > "$REWRITTEN"/$commit
699 done
700 rm -f "$REWRITTEN"/ours "$REWRITTEN"/upstream
703 generate_script_help () {
704 cat >> "$TODO" << EOF
706 # Rebase $SHORTREVISIONS onto $SHORTONTO
708 # Commands:
709 # p, pick = use commit
710 # r, reword = use commit, but edit the commit message
711 # e, edit = use commit, but stop for amending
712 # s, squash = use commit, but meld into previous commit
713 # f, fixup = like "squash", but discard this commit's log message
714 # g, goto = reset the current state to the given commit
715 # m, merge parents <parents> original <original merge commit>
716 # = redo the given merge commit
717 # b, bookmark = when reaching this command, name the current revision
719 # If you remove a line here THAT COMMIT WILL BE LOST.
720 # However, if you remove everything, the rebase will be aborted.
726 generate_script () {
727 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
728 $REVISIONS | grep '^>')" && {
729 echo noop
730 generate_script_help
731 return
734 current=$SHORTUPSTREAM
735 test -z "$REBASE_ROOT" || current=
736 git rev-list $MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
737 --reverse --left-right --topo-order $REVISIONS |
738 sed -n "s/^>//p" | while read shortsha1 firstparent rest
740 count=$(($count+1))
742 # generate "goto" statements
743 test -z "$PRESERVE_MERGES" || {
744 case "$firstparent" in
745 $current*)
746 # already there
748 $SHORTUPSTREAM*|'')
749 echo 'goto onto'
752 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
754 esac
755 current=$shortsha1
758 test -z "$rest" && {
759 echo "pick $(get_oneline $shortsha1)"
760 continue
763 # handle merges
764 # TODO: test octopus merge
765 echo "merge parents $rest original $(get_oneline $shortsha1)"
766 for parent in $rest
768 echo "# parent $(get_oneline $parent)"
769 done
770 done
772 generate_script_help
775 commit2bookmark () {
776 echo "$BOOKMARKS" | sed -n "s/^$sha1[^ ]* //p"
779 bookmark_exists () {
780 echo $1 | grep '^[0-9A-Fa-f]\{40\}$' > /dev/null && return
781 test -f "$REWRITTEN"/$1 && return
782 echo "$BOOKMARKS" | grep "^[^ ]* $1$" > /dev/null
785 name_bookmark () {
786 # make sure that the commit-to-bookmark will be rewritten
787 grep "^\(pick\|merge .* original\) $1 " < "$TODO" > /dev/null || return
788 test -z "$(commit2bookmark $1)" && {
789 bookmark="$(echo "$2" | tr -c 'A-Za-z0-9_' '-' |
790 sed -e 's/^-*//' -e 's/--*/-/g' -e 's/-*$//')"
791 bookmark_exists $bookmark && {
793 while bookmark_exists $bookmark-$i
795 i=$(($i+1))
796 done
797 bookmark=$bookmark-$i
799 BOOKMARKS="$(echo "$BOOKMARKS"; echo "$1 $bookmark")"
800 echo "$1 $bookmark"
804 make_bookmarks () {
805 test ! -d "$REWRITTEN" && return
806 BOOKMARKS=
808 # Merges make a nicer nick name, so take them first
809 message=
810 tac "$TODO" |
811 while read command sha1 rest
813 case "$command" in
814 goto)
815 # reuse merge messages for merge-parents
816 if test -z "$message"
817 then
818 message=$(git show -s --pretty=format:%s "$sha1")
819 else
820 message="merge-parent-of-$message"
822 test onto = "$sha1" && continue
823 name_bookmark "$sha1" "$message"
824 message=
826 merge)
827 rest=${rest#parents }
828 sha1s="$sha1 ${rest%% original *}"
829 message=$(echo "${rest#* original * }" |
830 sed -e 's/^Merge \(.\)/\1/' \
831 -e "s/.*'\(.*\)'.*/\1/")
832 for sha1 in $sha1s
834 name_bookmark "$sha1" "$message"
835 done
838 message=
840 esac
841 done > "$DOTEST"/bookmarks
843 # Make a sed script to rewrite the rebase script
844 cat "$DOTEST/bookmarks" |
845 while read sha1 bookmark
847 for command in \
848 's/^pick %s .*/&\\nbookmark %s/' \
849 's/^merge .* original %s .*/&\\nbookmark %s/' \
850 's/^goto %s .*/goto %s/' \
851 's/^\\(merge .* \\)%s\\(.* original .*\\)$/\\1%s\\2/'
853 printf "$command\\n" $sha1 $bookmark
854 done
855 done > "$DOTEST"/bookmarks.sed
857 # Now rewrite the rebase script to make use of the bookmarks
858 test ! -s "$DOTEST"/bookmarks.sed || {
859 mv "$TODO" "$TODO".backup &&
860 sed -f "$DOTEST"/bookmarks.sed < "$TODO".backup > "$TODO"
861 } || die 'Could not rewrite rebase script using bookmarks'
864 while test $# != 0
866 case "$1" in
867 --no-verify)
868 OK_TO_SKIP_PRE_REBASE=yes
870 --verify)
872 --continue)
873 is_standalone "$@" || usage
874 get_saved_options
875 comment_for_reflog continue
877 test -d "$DOTEST" || die "No interactive rebase running"
879 # Sanity check
880 git rev-parse --verify HEAD >/dev/null ||
881 die "Cannot read HEAD"
882 git update-index --ignore-submodules --refresh &&
883 git diff-files --quiet --ignore-submodules ||
884 die "Working tree is dirty"
886 # do we have anything to commit?
887 if git diff-index --cached --quiet --ignore-submodules HEAD --
888 then
889 : Nothing to commit
890 test ! -f "$REWRITTEN"/original || add_rewritten
891 else
892 . "$AUTHOR_SCRIPT" ||
893 die "Cannot find the author identity"
894 amend=
895 if test -f "$AMEND"
896 then
897 amend=$(git rev-parse --verify HEAD)
898 test "$amend" = $(cat "$AMEND") ||
899 die "\
900 You have uncommitted changes in your working tree. Please, commit them
901 first and then run 'git rebase --continue' again."
902 git reset --soft HEAD^ ||
903 die "Cannot rewind the HEAD"
905 do_with_author git commit --no-verify -F "$MSG" -e &&
906 add_rewritten || {
907 test -n "$amend" && git reset --soft $amend
908 die "Could not commit staged changes."
912 require_clean_work_tree
913 do_rest
915 --abort)
916 is_standalone "$@" || usage
917 get_saved_options
918 comment_for_reflog abort
920 git rerere clear
921 test -d "$DOTEST" || die "No interactive rebase running"
923 HEADNAME=$(cat "$DOTEST"/head-name)
924 HEAD=$(cat "$DOTEST"/head)
925 case $HEADNAME in
926 refs/*)
927 git symbolic-ref HEAD $HEADNAME
929 esac &&
930 output git reset --hard $HEAD &&
931 rm -rf "$DOTEST"
932 exit
934 --skip)
935 is_standalone "$@" || usage
936 get_saved_options
937 comment_for_reflog skip
939 git rerere clear
940 test -d "$DOTEST" || die "No interactive rebase running"
942 test -d "$REWRITTEN" && {
943 # skip last to-be-rewritten commit
944 original_count=$(wc -l < "$REWRITTEN"/original)
945 test $original_count -gt 0 &&
946 head -n $(($original_count-1)) < "$REWRITTEN"/original \
947 > "$REWRITTEN"/original.new &&
948 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
950 output git reset --hard && do_rest
953 case "$#,$1" in
954 *,*=*)
955 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
956 1,*)
957 usage ;;
959 STRATEGY="-s $2"
960 shift ;;
961 esac
964 # we use merge anyway
967 VERBOSE=t
970 PRESERVE_MERGES=t
973 # yeah, we know
975 --root)
976 REBASE_ROOT=t
978 --autosquash)
979 AUTOSQUASH=t
981 --onto)
982 shift
983 ONTO=$(parse_onto "$1") ||
984 die "Does not point to a valid commit: $1"
987 shift
988 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
989 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
990 test -d "$DOTEST" &&
991 die "Interactive rebase already started"
993 git var GIT_COMMITTER_IDENT >/dev/null ||
994 die "You need to set your committer info first"
996 if test -z "$REBASE_ROOT"
997 then
998 UPSTREAM_ARG="$1"
999 UPSTREAM=$(git rev-parse --verify "$1") ||
1000 die "Invalid base"
1001 test -z "$ONTO" && ONTO=$UPSTREAM
1002 shift
1003 else
1004 UPSTREAM=
1005 UPSTREAM_ARG=--root
1006 test -z "$ONTO" &&
1007 die "You must specify --onto when using --root"
1008 UPSTREAM=$ONTO
1010 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
1012 comment_for_reflog start
1014 require_clean_work_tree
1016 if test ! -z "$1"
1017 then
1018 output git show-ref --verify --quiet "refs/heads/$1" ||
1019 die "Invalid branchname: $1"
1020 output git checkout "$1" ||
1021 die "Could not checkout $1"
1024 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
1025 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
1027 : > "$DOTEST"/interactive || die "Could not mark as interactive"
1028 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1029 echo "detached HEAD" > "$DOTEST"/head-name
1031 echo $HEAD > "$DOTEST"/head
1032 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
1033 echo $ONTO > "$DOTEST"/onto
1034 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1035 test t = "$VERBOSE" && : > "$DOTEST"/verbose
1037 SHORTHEAD=$(git rev-parse --short $HEAD)
1038 SHORTONTO=$(git rev-parse --short $ONTO)
1039 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
1040 REVISIONS=$UPSTREAM...$HEAD
1041 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
1043 MERGES_OPTION=--no-merges
1044 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
1046 generate_script > "$TODO"
1047 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1049 make_bookmarks
1051 test t = "$PRESERVE_MERGES" && handle_dropped_commits
1053 has_action "$TODO" ||
1054 die_abort "Nothing to do"
1056 cp "$TODO" "$TODO".backup
1057 git_editor "$TODO" ||
1058 die_abort "Could not execute editor"
1060 has_action "$TODO" ||
1061 die_abort "Nothing to do"
1063 # once the user uses -p features, implicitely turn -p on
1064 ! grep "^\(goto\|merge\) " < "$TODO" > /dev/null ||
1065 mkdir -p "$REWRITTEN" ||
1066 die "Could not create directory $REWRITTEN/"
1068 test -d "$REWRITTEN" || skip_unnecessary_picks
1070 git update-ref ORIG_HEAD $HEAD
1071 output git checkout $ONTO && do_rest
1073 esac
1074 shift
1075 done