rebase -i -p: use the 'topic' command in the generated rebase script
[git/dscho.git] / git-rebase--interactive.sh
bloba064925c8b6e0adb2f740be8505b6b21aca82a12
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 handle_topic_branch () {
332 test -z "$2" && die "Empty 'topic' command"
333 command=$1
334 topic=$2
335 shift; shift
336 case "$command" in
337 begin)
338 git rev-parse --verify HEAD \
339 > "$REWRITTEN"/merge-parent-of-"$topic" &&
340 git reset --hard $(parse_commit \
341 $(test at = "$1" && echo "$2" || echo onto))
343 end)
344 git rev-parse --verify HEAD > "$REWRITTEN"/"$topic" &&
345 git reset --hard $(parse_commit merge-parent-of-"$topic") &&
346 merge_one parents "$topic" original "" "$*"
349 die "Unknown 'topic' command: $command $topic $*"
351 esac
354 nth_string () {
355 case "$1" in
356 *1[0-9]|*[04-9]) echo "$1"th;;
357 *1) echo "$1"st;;
358 *2) echo "$1"nd;;
359 *3) echo "$1"rd;;
360 esac
363 update_squash_messages () {
364 if test -f "$SQUASH_MSG"; then
365 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
366 COUNT=$(($(sed -n \
367 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
368 -e "q" < "$SQUASH_MSG".bak)+1))
370 echo "# This is a combination of $COUNT commits."
371 sed -e 1d -e '2,/^./{
372 /^$/d
373 }' <"$SQUASH_MSG".bak
374 } >"$SQUASH_MSG"
375 else
376 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
377 COUNT=2
379 echo "# This is a combination of 2 commits."
380 echo "# The first commit's message is:"
381 echo
382 cat "$FIXUP_MSG"
383 } >"$SQUASH_MSG"
385 case $1 in
386 squash)
387 rm -f "$FIXUP_MSG"
388 echo
389 echo "# This is the $(nth_string $COUNT) commit message:"
390 echo
391 commit_message $2
393 fixup)
394 echo
395 echo "# The $(nth_string $COUNT) commit message will be skipped:"
396 echo
397 commit_message $2 | sed -e 's/^/# /'
399 esac >>"$SQUASH_MSG"
402 # A squash/fixup has failed. Prepare the long version of the squash
403 # commit message, then die_with_patch. This code path requires the
404 # user to edit the combined commit message for all commits that have
405 # been squashed/fixedup so far. So also erase the old squash
406 # messages, effectively causing the combined commit to be used as the
407 # new basis for any further squash/fixups. Args: sha1 rest
408 die_failed_squash() {
409 mv "$SQUASH_MSG" "$MSG" || exit
410 rm -f "$FIXUP_MSG"
411 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
412 warn
413 warn "Could not apply $1... $2"
414 die_with_patch $1 ""
417 do_next () {
418 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
419 read command sha1 rest < "$TODO"
420 case "$command" in
421 '#'*|''|noop)
422 mark_action_done
424 pick|p)
425 comment_for_reflog pick
427 mark_action_done
428 pick_one $sha1 && add_rewritten ||
429 die_with_patch $sha1 "Could not apply $sha1... $rest"
431 reword|r)
432 comment_for_reflog reword
434 mark_action_done
435 pick_one $sha1 ||
436 die_with_patch $sha1 "Could not apply $sha1... $rest"
437 git commit --amend
439 edit|e)
440 comment_for_reflog edit
442 mark_action_done
443 pick_one $sha1 ||
444 die_with_patch $sha1 "Could not apply $sha1... $rest"
445 make_patch $sha1
446 git rev-parse --verify HEAD > "$AMEND"
447 warn "Stopped at $sha1... $rest"
448 warn "You can amend the commit now, with"
449 warn
450 warn " git commit --amend"
451 warn
452 warn "Once you are satisfied with your changes, run"
453 warn
454 warn " git rebase --continue"
455 warn
456 exit 0
458 squash|s|fixup|f)
459 case "$command" in
460 squash|s)
461 squash_style=squash
463 fixup|f)
464 squash_style=fixup
466 esac
467 comment_for_reflog $squash_style
469 test -f "$DONE" && has_action "$DONE" ||
470 die "Cannot '$squash_style' without a previous commit"
472 mark_action_done
473 update_squash_messages $squash_style $sha1
474 author_script=$(get_author_ident_from_commit HEAD)
475 echo "$author_script" > "$AUTHOR_SCRIPT"
476 eval "$author_script"
477 output git reset --soft HEAD^
478 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
479 case "$(peek_next_command)" in
480 squash|s|fixup|f)
481 # This is an intermediate commit; its message will only be
482 # used in case of trouble. So use the long version:
483 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
484 add_rewritten ||
485 die_failed_squash $sha1 "$rest"
488 # This is the final command of this squash/fixup group
489 if test -f "$FIXUP_MSG"
490 then
491 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
492 add_rewritten ||
493 die_failed_squash $sha1 "$rest"
494 else
495 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
496 rm -f "$GIT_DIR"/MERGE_MSG
497 do_with_author git commit --no-verify -e &&
498 add_rewritten ||
499 die_failed_squash $sha1 "$rest"
501 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
503 esac
505 bookmark|b)
506 mark_action_done
507 git rev-parse --verify HEAD > "$REWRITTEN"/"$sha1"
509 goto|g)
510 comment_for_reflog goto
511 mark_action_done
512 output git reset --hard $(parse_commit $sha1) ||
513 die "Could not reset to $sha1"
515 merge|m)
516 comment_for_reflog merge
517 mark_action_done
518 # this already dies with patch on error
519 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
520 exit
522 topic|t)
523 comment_for_reflog "topic($sha1)"
524 mark_action_done
525 output handle_topic_branch "$sha1" $rest ||
526 exit
529 warn "Unknown command: $command $sha1 $rest"
530 if git rev-parse --verify -q "$sha1" >/dev/null
531 then
532 die_with_patch $sha1 "Please fix this in the file $TODO."
533 else
534 die "Please fix this in the file $TODO."
537 esac
538 test -s "$TODO" && return
540 if test -d "$REWRITTEN"
541 then
542 missed_tips=$(git rev-list ^HEAD \
543 $(cat "$REWRITTEN"/* | sed "s/$/^!/") \
544 $(ls "$REWRITTEN" |
545 sed -n "s/^\([0-9A-Fa-f]\{40\}\)$/^\1/p"))
546 test -z "$missed_tips" || {
547 echo "The following branch tips are missing from HEAD:"
548 echo
549 for commit in $missed_tips
551 echo "$(get_oneline $commit)"
552 done
553 exit 1
557 comment_for_reflog finish &&
558 HEADNAME=$(cat "$DOTEST"/head-name) &&
559 OLDHEAD=$(cat "$DOTEST"/head) &&
560 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
561 NEWHEAD=$(git rev-parse HEAD) &&
562 case $HEADNAME in
563 refs/*)
564 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
565 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
566 git symbolic-ref HEAD $HEADNAME
568 esac && {
569 test ! -f "$DOTEST"/verbose ||
570 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
571 } &&
572 rm -rf "$DOTEST" &&
573 git gc --auto &&
574 warn "Successfully rebased and updated $HEADNAME."
576 exit
579 do_rest () {
580 while :
582 do_next
583 done
586 # skip picking commits whose parents are unchanged
587 skip_unnecessary_picks () {
588 fd=3
589 while read command sha1 rest
591 # fd=3 means we skip the command
592 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
593 3,pick,"$ONTO"*|3,p,"$ONTO"*)
594 # pick a commit whose parent is current $ONTO -> skip
595 ONTO=$sha1
597 3,#*|3,,*)
598 # copy comments
601 fd=1
603 esac
604 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
605 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
606 mv -f "$TODO".new "$TODO" ||
607 die "Could not skip unnecessary pick commands"
610 # check if no other options are set
611 is_standalone () {
612 test $# -eq 2 -a "$2" = '--' &&
613 test -z "$ONTO" &&
614 test -z "$PRESERVE_MERGES" &&
615 test -z "$STRATEGY" &&
616 test -z "$VERBOSE"
619 get_saved_options () {
620 test -d "$REWRITTEN" && PRESERVE_MERGES=t
621 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
622 test -f "$DOTEST"/verbose && VERBOSE=t
623 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
626 # Rearrange the todo list that has both "pick sha1 msg" and
627 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
628 # comes immediately after the former, and change "pick" to
629 # "fixup"/"squash".
630 rearrange_squash () {
631 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
632 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
633 "$1" >"$1.sq"
634 test -s "$1.sq" || return
636 used=
637 while read pick sha1 message
639 case " $used" in
640 *" $sha1 "*) continue ;;
641 esac
642 echo "$pick $sha1 $message"
643 while read squash action msg
645 case "$message" in
646 "$msg"*)
647 echo "$action $squash $action! $msg"
648 used="$used$squash "
650 esac
651 done <"$1.sq"
652 done >"$1.rearranged" <"$1"
653 cat "$1.rearranged" >"$1"
654 rm -f "$1.sq" "$1.rearranged"
657 LF='
659 parse_onto () {
660 case "$1" in
661 *...*)
662 if left=${1%...*} right=${1#*...} &&
663 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
664 then
665 case "$onto" in
666 ?*"$LF"?* | '')
667 exit 1 ;;
668 esac
669 echo "$onto"
670 exit 0
672 esac
673 git rev-parse --verify "$1^0"
676 prepare_preserve_merges () {
677 # $REWRITTEN/ contains a mapping for each rewritten commit:
678 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
679 mkdir "$REWRITTEN" || die "Could not create directory $REWRITTEN"
680 if test -z "$REBASE_ROOT"
681 then
682 for c in $(git merge-base --all $HEAD $UPSTREAM)
684 echo $ONTO > "$REWRITTEN"/$c ||
685 die "Could not init rewritten commits"
686 done
687 else
688 echo $ONTO > "$REWRITTEN"/root
690 echo $ONTO > "$REWRITTEN"/onto
691 echo $UPSTREAM > "$REWRITTEN"/upstream
693 # show merges
694 MERGES_OPTION=--parents
697 handle_dropped_commits () {
698 # Watch for commits that have been dropped by --cherry-pick
699 # The idea is that all commits that are already in upstream
700 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
701 # as if they were rewritten.
703 # Get all patch ids
704 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
705 # So take only the first patch-id for each commit id (uniq -f1).
706 git log -m -p $UPSTREAM..$HEAD | git patch-id |
707 uniq -s 41 > "$REWRITTEN"/ours
708 git log -m -p $HEAD..$UPSTREAM | git patch-id |
709 uniq -s 41 > "$REWRITTEN"/upstream
711 # Now get the correspondences
712 cat "$REWRITTEN"/ours | while read patch_id commit
714 # Is the same patch id in the upstream?
715 grep "^$patch_id " < "$REWRITTEN"/upstream > /dev/null ||
716 continue
718 # Record the parent as "rewritten" commit. As we will resolve
719 # rewritten commits recursively, this will work even if the
720 # parent was rewritten, too.
722 # If there is no parent, then we have a root commit that
723 # was cherry-picked into upstream; let's use $ONTO as
724 # fake parent of that root commit.
725 upstream=$(git rev-parse --verify "$commit^" 2> /dev/null)
726 test ! -z "$upstream" || upstream=$ONTO
727 echo "$upstream" > "$REWRITTEN"/$commit
728 done
729 rm -f "$REWRITTEN"/ours "$REWRITTEN"/upstream
732 generate_script_help () {
733 cat >> "$TODO" << EOF
735 # Rebase $SHORTREVISIONS onto $SHORTONTO
737 # Commands:
738 # p, pick = use commit
739 # r, reword = use commit, but edit the commit message
740 # e, edit = use commit, but stop for amending
741 # s, squash = use commit, but meld into previous commit
742 # f, fixup = like "squash", but discard this commit's log message
743 # g, goto = reset the current state to the given commit
744 # m, merge parents <parents> original <original merge commit>
745 # = redo the given merge commit
746 # b, bookmark = when reaching this command, name the current revision
748 # If you remove a line here THAT COMMIT WILL BE LOST.
749 # However, if you remove everything, the rebase will be aborted.
755 generate_script () {
756 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
757 $REVISIONS | grep '^>')" && {
758 echo noop
759 generate_script_help
760 return
763 current=$SHORTUPSTREAM
764 test -z "$REBASE_ROOT" || current=
765 git rev-list $MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
766 --reverse --left-right --topo-order $REVISIONS |
767 sed -n "s/^>//p" | while read shortsha1 firstparent rest
769 count=$(($count+1))
771 # generate "goto" statements
772 test -z "$PRESERVE_MERGES" || {
773 case "$firstparent" in
774 $current*)
775 # already there
777 $SHORTUPSTREAM*|'')
778 echo 'goto onto'
781 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
783 esac
784 current=$shortsha1
787 test -z "$rest" && {
788 echo "pick $(get_oneline $shortsha1)"
789 continue
792 # handle merges
793 # TODO: test octopus merge
794 echo "merge parents $rest original $(get_oneline $shortsha1)"
795 for parent in $rest
797 echo "# parent $(get_oneline $parent)"
798 done
799 done
801 generate_script_help
804 commit2bookmark () {
805 echo "$BOOKMARKS" | sed -n "s/^$sha1[^ ]* //p"
808 bookmark_exists () {
809 echo $1 | grep '^[0-9A-Fa-f]\{40\}$' > /dev/null && return
810 test -f "$REWRITTEN"/$1 && return
811 echo "$BOOKMARKS" | grep "^[^ ]* $1$" > /dev/null
814 name_bookmark () {
815 # make sure that the commit-to-bookmark will be rewritten
816 grep "^\(pick\|merge .* original\) $1 " < "$TODO" > /dev/null || return
817 test -z "$(commit2bookmark $1)" && {
818 bookmark="$(echo "$2" | tr -c 'A-Za-z0-9_' '-' |
819 sed -e 's/^-*//' -e 's/--*/-/g' -e 's/-*$//')"
820 bookmark_exists $bookmark && {
822 while bookmark_exists $bookmark-$i
824 i=$(($i+1))
825 done
826 bookmark=$bookmark-$i
828 BOOKMARKS="$(echo "$BOOKMARKS"; echo "$1 $bookmark")"
829 echo "$1 $bookmark"
833 make_bookmarks () {
834 test ! -d "$REWRITTEN" && return
835 BOOKMARKS="$(git show -s --pretty=format:'%h onto' \
836 $(git merge-base --all "$HEAD" "$UPSTREAM") |
837 grep -v '^$')"
839 # Merges make a nicer nick name, so take them first
840 message=
841 tac "$TODO" |
842 while read command sha1 rest
844 case "$command" in
845 goto)
846 # reuse merge messages for merge-parents
847 if test -z "$message"
848 then
849 message=$(git show -s --pretty=format:%s "$sha1")
850 else
851 message="merge-parent-of-$message"
853 test onto = "$sha1" && continue
854 name_bookmark "$sha1" "$message"
855 message=
857 merge)
858 rest=${rest#parents }
859 sha1s="$sha1 ${rest%% original *}"
860 message=$(echo "${rest#* original * }" |
861 sed -e 's/^Merge \(.\)/\1/' \
862 -e "s/.*'\(.*\)'.*/\1/")
863 for sha1 in $sha1s
865 name_bookmark "$sha1" "$message"
866 done
869 message=
871 esac
872 done > "$DOTEST"/bookmarks
874 # Make a sed script to rewrite the rebase script
875 cat "$DOTEST/bookmarks" |
876 while read sha1 bookmark
878 for command in \
879 's/^pick %s .*/&\\nbookmark %s/' \
880 's/^merge .* original %s .*/&\\nbookmark %s/' \
881 's/^goto %s .*/goto %s/' \
882 's/^\\(merge .* \\)%s\\(.* original .*\\)$/\\1%s\\2/'
884 printf "$command\\n" $sha1 $bookmark
885 done
886 done > "$DOTEST"/bookmarks.sed
888 # Now rewrite the rebase script to make use of the bookmarks
889 test ! -s "$DOTEST"/bookmarks.sed || {
890 sed -f "$DOTEST"/bookmarks.sed < "$TODO" > "$TODO".bookmarks &&
891 # write short form of typical topic commands:
893 # topic begin blabla at blub
895 # instead of
897 # bookmark merge-parent-of-blabla
898 # goto blub
900 # and
902 # topic end blabla Merge 'blabla'
904 # instead of
906 # bookmark blabla
907 # goto merge-parent-of-blabla
908 # merge parents blabla original 0123456 Merge 'blabla'
909 while read line
911 case "$line" in
912 'bookmark '*)
913 topic=${line#bookmark }
914 topic2=${topic#merge-parent-of-}
915 read line2
916 # skip comments (after a merge)
917 while case "$line2" in \#*) ;; *) break ;; esac
919 line="$(echo "$line"; echo "$line2")"
920 read line2
921 done
922 case "$topic,$line2" in
923 "$topic,goto merge-parent-of-$topic")
924 read line3
925 case "$line3" in
926 "merge parents $topic"*)
927 msg="$(echo "$line3" |
928 cut -f 6- -d ' ')"
929 test "Merge '$topic'" = "$msg" &&
930 msg=
931 echo "topic end $topic $msg"
934 echo "$line"
935 echo "$line2"
936 echo "$line3"
937 esac
939 "merge-parent-of-$topic2,goto "*)
940 goto="at ${line2#goto }"
941 test 'at onto' = "$goto" && goto=
942 echo "topic begin $topic2 $goto"
945 echo "$line"
946 echo "$line2"
947 esac
950 echo "$line"
951 esac
952 done < "$TODO".bookmarks > "$TODO"
953 } || die 'Could not rewrite rebase script using bookmarks'
956 while test $# != 0
958 case "$1" in
959 --no-verify)
960 OK_TO_SKIP_PRE_REBASE=yes
962 --verify)
964 --continue)
965 is_standalone "$@" || usage
966 get_saved_options
967 comment_for_reflog continue
969 test -d "$DOTEST" || die "No interactive rebase running"
971 # Sanity check
972 git rev-parse --verify HEAD >/dev/null ||
973 die "Cannot read HEAD"
974 git update-index --ignore-submodules --refresh &&
975 git diff-files --quiet --ignore-submodules ||
976 die "Working tree is dirty"
978 # do we have anything to commit?
979 if git diff-index --cached --quiet --ignore-submodules HEAD --
980 then
981 : Nothing to commit
982 test ! -f "$REWRITTEN"/original || add_rewritten
983 else
984 . "$AUTHOR_SCRIPT" ||
985 die "Cannot find the author identity"
986 amend=
987 if test -f "$AMEND"
988 then
989 amend=$(git rev-parse --verify HEAD)
990 test "$amend" = $(cat "$AMEND") ||
991 die "\
992 You have uncommitted changes in your working tree. Please, commit them
993 first and then run 'git rebase --continue' again."
994 git reset --soft HEAD^ ||
995 die "Cannot rewind the HEAD"
997 do_with_author git commit --no-verify -F "$MSG" -e &&
998 add_rewritten || {
999 test -n "$amend" && git reset --soft $amend
1000 die "Could not commit staged changes."
1004 require_clean_work_tree
1005 do_rest
1007 --abort)
1008 is_standalone "$@" || usage
1009 get_saved_options
1010 comment_for_reflog abort
1012 git rerere clear
1013 test -d "$DOTEST" || die "No interactive rebase running"
1015 HEADNAME=$(cat "$DOTEST"/head-name)
1016 HEAD=$(cat "$DOTEST"/head)
1017 case $HEADNAME in
1018 refs/*)
1019 git symbolic-ref HEAD $HEADNAME
1021 esac &&
1022 output git reset --hard $HEAD &&
1023 rm -rf "$DOTEST"
1024 exit
1026 --skip)
1027 is_standalone "$@" || usage
1028 get_saved_options
1029 comment_for_reflog skip
1031 git rerere clear
1032 test -d "$DOTEST" || die "No interactive rebase running"
1034 test -d "$REWRITTEN" && {
1035 # skip last to-be-rewritten commit
1036 original_count=$(wc -l < "$REWRITTEN"/original)
1037 test $original_count -gt 0 &&
1038 head -n $(($original_count-1)) < "$REWRITTEN"/original \
1039 > "$REWRITTEN"/original.new &&
1040 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
1042 output git reset --hard && do_rest
1045 case "$#,$1" in
1046 *,*=*)
1047 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1048 1,*)
1049 usage ;;
1051 STRATEGY="-s $2"
1052 shift ;;
1053 esac
1056 # we use merge anyway
1059 VERBOSE=t
1062 PRESERVE_MERGES=t
1065 # yeah, we know
1067 --root)
1068 REBASE_ROOT=t
1070 --autosquash)
1071 AUTOSQUASH=t
1073 --onto)
1074 shift
1075 ONTO=$(parse_onto "$1") ||
1076 die "Does not point to a valid commit: $1"
1079 shift
1080 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
1081 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
1082 test -d "$DOTEST" &&
1083 die "Interactive rebase already started"
1085 git var GIT_COMMITTER_IDENT >/dev/null ||
1086 die "You need to set your committer info first"
1088 if test -z "$REBASE_ROOT"
1089 then
1090 UPSTREAM_ARG="$1"
1091 UPSTREAM=$(git rev-parse --verify "$1") ||
1092 die "Invalid base"
1093 test -z "$ONTO" && ONTO=$UPSTREAM
1094 shift
1095 else
1096 UPSTREAM=
1097 UPSTREAM_ARG=--root
1098 test -z "$ONTO" &&
1099 die "You must specify --onto when using --root"
1100 UPSTREAM=$ONTO
1102 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
1104 comment_for_reflog start
1106 require_clean_work_tree
1108 if test ! -z "$1"
1109 then
1110 output git show-ref --verify --quiet "refs/heads/$1" ||
1111 die "Invalid branchname: $1"
1112 output git checkout "$1" ||
1113 die "Could not checkout $1"
1116 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
1117 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
1119 : > "$DOTEST"/interactive || die "Could not mark as interactive"
1120 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1121 echo "detached HEAD" > "$DOTEST"/head-name
1123 echo $HEAD > "$DOTEST"/head
1124 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
1125 echo $ONTO > "$DOTEST"/onto
1126 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1127 test t = "$VERBOSE" && : > "$DOTEST"/verbose
1129 SHORTHEAD=$(git rev-parse --short $HEAD)
1130 SHORTONTO=$(git rev-parse --short $ONTO)
1131 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
1132 REVISIONS=$UPSTREAM...$HEAD
1133 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
1135 MERGES_OPTION=--no-merges
1136 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
1138 generate_script > "$TODO"
1139 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1141 make_bookmarks
1143 test t = "$PRESERVE_MERGES" && handle_dropped_commits
1145 has_action "$TODO" ||
1146 die_abort "Nothing to do"
1148 cp "$TODO" "$TODO".backup
1149 git_editor "$TODO" ||
1150 die_abort "Could not execute editor"
1152 has_action "$TODO" ||
1153 die_abort "Nothing to do"
1155 # once the user uses -p features, implicitely turn -p on
1156 ! grep "^\(goto\|merge\) " < "$TODO" > /dev/null ||
1157 mkdir -p "$REWRITTEN" ||
1158 die "Could not create directory $REWRITTEN/"
1160 test -d "$REWRITTEN" || skip_unnecessary_picks
1162 git update-ref ORIG_HEAD $HEAD
1163 output git checkout $ONTO && do_rest
1165 esac
1166 shift
1167 done