TODOs for rebase -i -p
[git/dscho.git] / git-rebase--interactive.sh
blobc5448c7dc82b02d6ec5e8fac88b7b7b562d7debe
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 --verify "$2") ||
307 die "Invalid original merge commit: $2"
308 if test -z "$3"
309 then
310 merge_message="$(git cat-file commit $sha1 |
311 sed '1,/^$/d')"
315 if test ! -z "$3"
316 then
317 shift; shift;
318 merge_message="$*"
321 # the command was "merge parents ...", so "parents" was recorded
322 # TODO: detect non-fast-forwards properly
323 ORIGINAL_HEAD=$(git rev-parse HEAD) &&
324 git merge $parents &&
325 if test $ORIGINAL_HEAD = "$(git rev-parse HEAD^)"
326 then
327 git commit --amend -m "$merge_message" &&
328 if test !-z "$sha1"
329 then
330 echo "$sha1" > "$REWRITTEN"/original &&
331 add_rewritten
333 fi ||
334 die_with_patch $sha1 "Could not redo merge $sha1 with parents $parents"
337 handle_topic_branch () {
338 test -z "$2" && die "Empty 'topic' command"
339 command=$1
340 topic=$2
341 shift; shift
342 case "$command" in
343 begin)
344 git rev-parse --verify HEAD \
345 > "$REWRITTEN"/merge-parent-of-"$topic" &&
346 git reset --hard $(parse_commit \
347 $(test at = "$1" && echo "$2" || echo onto))
349 end)
350 git rev-parse --verify HEAD > "$REWRITTEN"/"$topic" &&
351 git reset --hard $(parse_commit merge-parent-of-"$topic") &&
352 merge_one parents "$topic" original "" "$*"
355 die "Unknown 'topic' command: $command $topic $*"
357 esac
360 nth_string () {
361 case "$1" in
362 *1[0-9]|*[04-9]) echo "$1"th;;
363 *1) echo "$1"st;;
364 *2) echo "$1"nd;;
365 *3) echo "$1"rd;;
366 esac
369 update_squash_messages () {
370 if test -f "$SQUASH_MSG"; then
371 mv "$SQUASH_MSG" "$SQUASH_MSG".bak || exit
372 COUNT=$(($(sed -n \
373 -e "1s/^# This is a combination of \(.*\) commits\./\1/p" \
374 -e "q" < "$SQUASH_MSG".bak)+1))
376 echo "# This is a combination of $COUNT commits."
377 sed -e 1d -e '2,/^./{
378 /^$/d
379 }' <"$SQUASH_MSG".bak
380 } >"$SQUASH_MSG"
381 else
382 commit_message HEAD > "$FIXUP_MSG" || die "Cannot write $FIXUP_MSG"
383 COUNT=2
385 echo "# This is a combination of 2 commits."
386 echo "# The first commit's message is:"
387 echo
388 cat "$FIXUP_MSG"
389 } >"$SQUASH_MSG"
391 case $1 in
392 squash)
393 rm -f "$FIXUP_MSG"
394 echo
395 echo "# This is the $(nth_string $COUNT) commit message:"
396 echo
397 commit_message $2
399 fixup)
400 echo
401 echo "# The $(nth_string $COUNT) commit message will be skipped:"
402 echo
403 commit_message $2 | sed -e 's/^/# /'
405 esac >>"$SQUASH_MSG"
408 # A squash/fixup has failed. Prepare the long version of the squash
409 # commit message, then die_with_patch. This code path requires the
410 # user to edit the combined commit message for all commits that have
411 # been squashed/fixedup so far. So also erase the old squash
412 # messages, effectively causing the combined commit to be used as the
413 # new basis for any further squash/fixups. Args: sha1 rest
414 die_failed_squash() {
415 mv "$SQUASH_MSG" "$MSG" || exit
416 rm -f "$FIXUP_MSG"
417 cp "$MSG" "$GIT_DIR"/MERGE_MSG || exit
418 warn
419 warn "Could not apply $1... $2"
420 die_with_patch $1 ""
423 do_next () {
424 rm -f "$MSG" "$AUTHOR_SCRIPT" "$AMEND" || exit
425 read command sha1 rest < "$TODO"
426 case "$command" in
427 '#'*|''|noop)
428 mark_action_done
430 pick|p)
431 comment_for_reflog pick
433 mark_action_done
434 # TODO: give better description/output in the "Nothing to be committed" and
435 # the conflict case
436 pick_one $sha1 && add_rewritten ||
437 die_with_patch $sha1 "Could not apply $sha1... $rest"
439 reword|r)
440 comment_for_reflog reword
442 mark_action_done
443 pick_one $sha1 ||
444 die_with_patch $sha1 "Could not apply $sha1... $rest"
445 git commit --amend
447 edit|e)
448 comment_for_reflog edit
450 mark_action_done
451 pick_one $sha1 ||
452 die_with_patch $sha1 "Could not apply $sha1... $rest"
453 make_patch $sha1
454 git rev-parse --verify HEAD > "$AMEND"
455 warn "Stopped at $sha1... $rest"
456 warn "You can amend the commit now, with"
457 warn
458 warn " git commit --amend"
459 warn
460 warn "Once you are satisfied with your changes, run"
461 warn
462 warn " git rebase --continue"
463 warn
464 exit 0
466 squash|s|fixup|f)
467 case "$command" in
468 squash|s)
469 squash_style=squash
471 fixup|f)
472 squash_style=fixup
474 esac
475 comment_for_reflog $squash_style
477 test -f "$DONE" && has_action "$DONE" ||
478 die "Cannot '$squash_style' without a previous commit"
480 mark_action_done
481 update_squash_messages $squash_style $sha1
482 author_script=$(get_author_ident_from_commit HEAD)
483 echo "$author_script" > "$AUTHOR_SCRIPT"
484 eval "$author_script"
485 output git reset --soft HEAD^
486 pick_one -n $sha1 || die_failed_squash $sha1 "$rest"
487 case "$(peek_next_command)" in
488 squash|s|fixup|f)
489 # This is an intermediate commit; its message will only be
490 # used in case of trouble. So use the long version:
491 do_with_author output git commit --no-verify -F "$SQUASH_MSG" &&
492 add_rewritten ||
493 die_failed_squash $sha1 "$rest"
496 # This is the final command of this squash/fixup group
497 if test -f "$FIXUP_MSG"
498 then
499 do_with_author git commit --no-verify -F "$FIXUP_MSG" &&
500 add_rewritten ||
501 die_failed_squash $sha1 "$rest"
502 else
503 cp "$SQUASH_MSG" "$GIT_DIR"/SQUASH_MSG || exit
504 rm -f "$GIT_DIR"/MERGE_MSG
505 do_with_author git commit --no-verify -e &&
506 add_rewritten ||
507 die_failed_squash $sha1 "$rest"
509 rm -f "$SQUASH_MSG" "$FIXUP_MSG"
511 esac
513 bookmark|b)
514 mark_action_done
515 git rev-parse --verify HEAD > "$REWRITTEN"/"$sha1"
517 goto|g)
518 comment_for_reflog goto
519 mark_action_done
520 output git reset --hard $(parse_commit $sha1) ||
521 die "Could not reset to $sha1"
523 merge|m)
524 comment_for_reflog merge
525 mark_action_done
526 # this already dies with patch on error
527 output merge_one $sha1 $rest || # $sha1 is not the real sha1...
528 exit
530 topic|t)
531 comment_for_reflog "topic($sha1)"
532 mark_action_done
533 output handle_topic_branch "$sha1" $rest ||
534 exit
537 warn "Unknown command: $command $sha1 $rest"
538 if git rev-parse --verify -q "$sha1" >/dev/null
539 then
540 die_with_patch $sha1 "Please fix this in the file $TODO."
541 else
542 die "Please fix this in the file $TODO."
545 esac
546 test -s "$TODO" && return
548 if test -d "$REWRITTEN"
549 then
550 missed_tips=$(git rev-list ^HEAD \
551 $(cat "$REWRITTEN"/* | sed "s/$/^!/") \
552 $(ls "$REWRITTEN" |
553 sed -n "s/^\([0-9A-Fa-f]\{40\}\)$/^\1/p"))
554 test -z "$missed_tips" || {
555 echo "The following branch tips are missing from HEAD:"
556 echo
557 for commit in $missed_tips
559 echo "$(get_oneline $commit)"
560 done
561 exit 1
565 comment_for_reflog finish &&
566 HEADNAME=$(cat "$DOTEST"/head-name) &&
567 OLDHEAD=$(cat "$DOTEST"/head) &&
568 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
569 NEWHEAD=$(git rev-parse HEAD) &&
570 case $HEADNAME in
571 refs/*)
572 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO" &&
573 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
574 git symbolic-ref HEAD $HEADNAME
576 esac && {
577 test ! -f "$DOTEST"/verbose ||
578 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
579 } &&
580 rm -rf "$DOTEST" &&
581 git gc --auto &&
582 warn "Successfully rebased and updated $HEADNAME."
584 exit
587 do_rest () {
588 while :
590 do_next
591 done
594 # skip picking commits whose parents are unchanged
595 skip_unnecessary_picks () {
596 fd=3
597 while read command sha1 rest
599 # fd=3 means we skip the command
600 case "$fd,$command,$(git rev-parse --verify --quiet $sha1^)" in
601 3,pick,"$ONTO"*|3,p,"$ONTO"*)
602 # pick a commit whose parent is current $ONTO -> skip
603 ONTO=$sha1
605 3,#*|3,,*)
606 # copy comments
609 fd=1
611 esac
612 echo "$command${sha1:+ }$sha1${rest:+ }$rest" >&$fd
613 done <"$TODO" >"$TODO.new" 3>>"$DONE" &&
614 mv -f "$TODO".new "$TODO" ||
615 die "Could not skip unnecessary pick commands"
618 # check if no other options are set
619 is_standalone () {
620 test $# -eq 2 -a "$2" = '--' &&
621 test -z "$ONTO" &&
622 test -z "$PRESERVE_MERGES" &&
623 test -z "$STRATEGY" &&
624 test -z "$VERBOSE"
627 get_saved_options () {
628 test -d "$REWRITTEN" && PRESERVE_MERGES=t
629 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
630 test -f "$DOTEST"/verbose && VERBOSE=t
631 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
634 # Rearrange the todo list that has both "pick sha1 msg" and
635 # "pick sha1 fixup!/squash! msg" appears in it so that the latter
636 # comes immediately after the former, and change "pick" to
637 # "fixup"/"squash".
638 rearrange_squash () {
639 sed -n -e 's/^pick \([0-9a-f]*\) \(squash\)! /\1 \2 /p' \
640 -e 's/^pick \([0-9a-f]*\) \(fixup\)! /\1 \2 /p' \
641 "$1" >"$1.sq"
642 test -s "$1.sq" || return
644 used=
645 while read pick sha1 message
647 case " $used" in
648 *" $sha1 "*) continue ;;
649 esac
650 echo "$pick $sha1 $message"
651 while read squash action msg
653 case "$message" in
654 "$msg"*)
655 echo "$action $squash $action! $msg"
656 used="$used$squash "
658 esac
659 done <"$1.sq"
660 done >"$1.rearranged" <"$1"
661 cat "$1.rearranged" >"$1"
662 rm -f "$1.sq" "$1.rearranged"
665 LF='
667 parse_onto () {
668 case "$1" in
669 *...*)
670 if left=${1%...*} right=${1#*...} &&
671 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
672 then
673 case "$onto" in
674 ?*"$LF"?* | '')
675 exit 1 ;;
676 esac
677 echo "$onto"
678 exit 0
680 esac
681 git rev-parse --verify "$1^0"
684 prepare_preserve_merges () {
685 # $REWRITTEN/ contains a mapping for each rewritten commit:
686 # $(cat "$REWRITTEN"/$ORIGINAL_SHA1) = $REWRITTEN_SHA1
687 mkdir "$REWRITTEN" || die "Could not create directory $REWRITTEN"
688 if test -z "$REBASE_ROOT"
689 then
690 for c in $(git merge-base --all $HEAD $UPSTREAM)
692 echo $ONTO > "$REWRITTEN"/$c ||
693 die "Could not init rewritten commits"
694 done
695 else
696 echo $ONTO > "$REWRITTEN"/root
698 echo $ONTO > "$REWRITTEN"/onto
699 echo $UPSTREAM > "$REWRITTEN"/upstream
701 # show merges
702 MERGES_OPTION=--parents
705 handle_dropped_commits () {
706 # Watch for commits that have been dropped by --cherry-pick
707 # The idea is that all commits that are already in upstream
708 # have a mapping $(cat "$REWRITTEN"/<my-sha1>) = <upstream-sha1>
709 # as if they were rewritten.
711 # Get all patch ids
712 # --cherry-pick only analyzes first parent, -m analyzes _all_ parents!
713 # So take only the first patch-id for each commit id (uniq -f1).
714 git log -m -p $UPSTREAM..$HEAD | git patch-id |
715 uniq -s 41 > "$REWRITTEN"/ours
716 git log -m -p $HEAD..$UPSTREAM | git patch-id |
717 uniq -s 41 > "$REWRITTEN"/upstream
719 # Now get the correspondences
720 cat "$REWRITTEN"/ours | while read patch_id commit
722 # Is the same patch id in the upstream?
723 grep "^$patch_id " < "$REWRITTEN"/upstream > /dev/null ||
724 continue
726 # Record the parent as "rewritten" commit. As we will resolve
727 # rewritten commits recursively, this will work even if the
728 # parent was rewritten, too.
730 # If there is no parent, then we have a root commit that
731 # was cherry-picked into upstream; let's use $ONTO as
732 # fake parent of that root commit.
733 upstream=$(git rev-parse --verify "$commit^" 2> /dev/null)
734 test ! -z "$upstream" || upstream=$ONTO
735 echo "$upstream" > "$REWRITTEN"/$commit
736 done
737 rm -f "$REWRITTEN"/ours "$REWRITTEN"/upstream
740 generate_script_help () {
741 cat >> "$TODO" << EOF
743 # Rebase $SHORTREVISIONS onto $SHORTONTO
745 # Commands:
746 # p, pick = use commit
747 # r, reword = use commit, but edit the commit message
748 # e, edit = use commit, but stop for amending
749 # s, squash = use commit, but meld into previous commit
750 # f, fixup = like "squash", but discard this commit's log message
751 # g, goto = reset the current state to the given commit
752 # m, merge parents <parents> original <original merge commit>
753 # = redo the given merge commit
754 # b, bookmark = when reaching this command, name the current revision
756 # If you remove a line here THAT COMMIT WILL BE LOST.
757 # However, if you remove everything, the rebase will be aborted.
763 generate_script () {
764 test -z "$(git rev-list $MERGES_OPTION --cherry-pick --left-right \
765 $REVISIONS | grep '^>')" && {
766 echo noop
767 generate_script_help
768 return
771 current=$SHORTUPSTREAM
772 test -z "$REBASE_ROOT" || current=
773 git rev-list $MERGES_OPTION --cherry-pick --pretty="format:%m%h %p" \
774 --reverse --left-right --topo-order $REVISIONS |
775 sed -n "s/^>//p" | while read shortsha1 firstparent rest
777 count=$(($count+1))
779 # generate "goto" statements
780 test -z "$PRESERVE_MERGES" || {
781 case "$firstparent" in
782 $current*)
783 # already there
785 $SHORTUPSTREAM*|'')
786 echo 'goto onto'
789 # TODO: a failed merge at the end should not let the rebase succeed
790 # TODO: avoid outputting anything when current is SHORTUPSTREAM and
791 # firstparent is a merge base
792 # TODO: use get_oneline again
793 echo "goto $firstparent ($(git show -s --pretty=format:%s $firstparent))"
795 esac
796 current=$shortsha1
799 test -z "$rest" && {
800 echo "pick $(get_oneline $shortsha1)"
801 continue
804 # handle merges
805 # TODO: test octopus merge
806 echo "merge parents $rest original $(get_oneline $shortsha1)"
807 for parent in $rest
809 # TODO: add information about "dropped" commits, just because we can
810 echo "# parent $(get_oneline $parent)"
811 done
812 done
814 generate_script_help
817 commit2bookmark () {
818 echo "$BOOKMARKS" | sed -n "s/^$sha1[^ ]* //p"
821 bookmark_exists () {
822 echo $1 | grep '^[0-9A-Fa-f]\{40\}$' > /dev/null && return
823 test -f "$REWRITTEN"/$1 && return
824 echo "$BOOKMARKS" | grep "^[^ ]* $1$" > /dev/null
827 name_bookmark () {
828 # make sure that the commit-to-bookmark will be rewritten
829 grep "^\(pick\|merge .* original\) $1 " < "$TODO" > /dev/null || return
830 test -z "$(commit2bookmark $1)" && {
831 bookmark="$(echo "$2" | tr -c 'A-Za-z0-9_' '-' |
832 sed -e 's/^-*//' -e 's/--*/-/g' -e 's/-*$//')"
833 bookmark_exists $bookmark && {
835 while bookmark_exists $bookmark-$i
837 i=$(($i+1))
838 done
839 bookmark=$bookmark-$i
841 BOOKMARKS="$(echo "$BOOKMARKS"; echo "$1 $bookmark")"
842 echo "$1 $bookmark"
846 make_bookmarks () {
847 test ! -d "$REWRITTEN" && return
848 BOOKMARKS="$(git show -s --pretty=format:'%h onto' \
849 $(git merge-base --all "$HEAD" "$UPSTREAM") |
850 grep -v '^$')"
852 # Merges make a nicer nick name, so take them first
853 message=
854 tac "$TODO" |
855 while read command sha1 rest
857 case "$command" in
858 goto)
859 # reuse merge messages for merge-parents
860 if test -z "$message"
861 then
862 message=$(git show -s --pretty=format:%s "$sha1")
863 else
864 message="merge-parent-of-$message"
866 test onto = "$sha1" && continue
867 name_bookmark "$sha1" "$message"
868 message=
870 merge)
871 rest=${rest#parents }
872 sha1s="$sha1 ${rest%% original *}"
873 message=$(echo "${rest#* original * }" |
874 sed -e 's/^Merge \(.\)/\1/' \
875 -e "s/.*'\(.*\)'.*/\1/")
876 for sha1 in $sha1s
878 name_bookmark "$sha1" "$message"
879 done
882 message=
884 esac
885 done > "$DOTEST"/bookmarks
887 # Make a sed script to rewrite the rebase script
888 cat "$DOTEST/bookmarks" |
889 while read sha1 bookmark
891 for command in \
892 's/^pick %s .*/&\\nbookmark %s/' \
893 's/^merge .* original %s .*/&\\nbookmark %s/' \
894 's/^goto %s .*/goto %s/' \
895 's/^\\(merge .* \\)%s\\(.* original .*\\)$/\\1%s\\2/'
897 printf "$command\\n" $sha1 $bookmark
898 done
899 done > "$DOTEST"/bookmarks.sed
901 # Now rewrite the rebase script to make use of the bookmarks
902 test ! -s "$DOTEST"/bookmarks.sed || {
903 sed -f "$DOTEST"/bookmarks.sed < "$TODO" > "$TODO".bookmarks &&
904 # write short form of typical topic commands:
906 # topic begin blabla at blub
908 # instead of
910 # bookmark merge-parent-of-blabla
911 # goto blub
913 # and
915 # topic end blabla Merge 'blabla'
917 # instead of
919 # bookmark blabla
920 # goto merge-parent-of-blabla
921 # merge parents blabla original 0123456 Merge 'blabla'
922 while read line
924 case "$line" in
925 'bookmark '*)
926 topic=${line#bookmark }
927 topic2=${topic#merge-parent-of-}
928 read line2
929 # skip comments (after a merge)
930 while case "$line2" in \#*) ;; *) break ;; esac
932 line="$(echo "$line"; echo "$line2")"
933 read line2
934 done
935 case "$topic,$line2" in
936 "$topic,goto merge-parent-of-$topic")
937 read line3
938 case "$line3" in
939 "merge parents $topic"*)
940 msg="$(echo "$line3" |
941 cut -f 6- -d ' ')"
942 test "Merge '$topic'" = "$msg" &&
943 msg=
944 echo "topic end $topic $msg"
947 echo "$line"
948 echo "$line2"
949 echo "$line3"
950 esac
952 "merge-parent-of-$topic2,goto "*)
953 goto="at ${line2#goto }"
954 test 'at onto' = "$goto" && goto=
955 echo "topic begin $topic2 $goto"
958 echo "$line"
959 echo "$line2"
960 esac
963 echo "$line"
964 esac
965 done < "$TODO".bookmarks > "$TODO"
966 } || die 'Could not rewrite rebase script using bookmarks'
967 # TODO: maybe it is possible to rewrite in one go? Think about it
968 # TODO: topic end seems not to work when merging branch1, committing to branch1
969 # and merging it again
972 while test $# != 0
974 case "$1" in
975 --no-verify)
976 OK_TO_SKIP_PRE_REBASE=yes
978 --verify)
980 --continue)
981 is_standalone "$@" || usage
982 get_saved_options
983 comment_for_reflog continue
985 test -d "$DOTEST" || die "No interactive rebase running"
987 # Sanity check
988 git rev-parse --verify HEAD >/dev/null ||
989 die "Cannot read HEAD"
990 git update-index --ignore-submodules --refresh &&
991 git diff-files --quiet --ignore-submodules ||
992 die "Working tree is dirty"
994 # do we have anything to commit?
995 if git diff-index --cached --quiet --ignore-submodules HEAD --
996 then
997 : Nothing to commit
998 test ! -f "$REWRITTEN"/original || add_rewritten
999 else
1000 . "$AUTHOR_SCRIPT" ||
1001 die "Cannot find the author identity"
1002 amend=
1003 if test -f "$AMEND"
1004 then
1005 amend=$(git rev-parse --verify HEAD)
1006 test "$amend" = $(cat "$AMEND") ||
1007 die "\
1008 You have uncommitted changes in your working tree. Please, commit them
1009 first and then run 'git rebase --continue' again."
1010 git reset --soft HEAD^ ||
1011 die "Cannot rewind the HEAD"
1013 do_with_author git commit --no-verify -F "$MSG" -e &&
1014 add_rewritten || {
1015 test -n "$amend" && git reset --soft $amend
1016 die "Could not commit staged changes."
1020 require_clean_work_tree
1021 do_rest
1023 --abort)
1024 is_standalone "$@" || usage
1025 get_saved_options
1026 comment_for_reflog abort
1028 git rerere clear
1029 test -d "$DOTEST" || die "No interactive rebase running"
1031 HEADNAME=$(cat "$DOTEST"/head-name)
1032 HEAD=$(cat "$DOTEST"/head)
1033 case $HEADNAME in
1034 refs/*)
1035 git symbolic-ref HEAD $HEADNAME
1037 esac &&
1038 output git reset --hard $HEAD &&
1039 rm -rf "$DOTEST"
1040 exit
1042 --skip)
1043 is_standalone "$@" || usage
1044 get_saved_options
1045 comment_for_reflog skip
1047 git rerere clear
1048 test -d "$DOTEST" || die "No interactive rebase running"
1050 test -d "$REWRITTEN" && {
1051 # skip last to-be-rewritten commit
1052 original_count=$(wc -l < "$REWRITTEN"/original)
1053 test $original_count -gt 0 &&
1054 head -n $(($original_count-1)) < "$REWRITTEN"/original \
1055 > "$REWRITTEN"/original.new &&
1056 mv "$REWRITTEN"/original.new "$REWRITTEN"/original
1058 output git reset --hard && do_rest
1061 case "$#,$1" in
1062 *,*=*)
1063 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
1064 1,*)
1065 usage ;;
1067 STRATEGY="-s $2"
1068 shift ;;
1069 esac
1072 # we use merge anyway
1075 VERBOSE=t
1078 # TODO: add an option to rewrite refs that point to rewritten commits
1079 # TODO: add a big fat warning at the end when there are rewritten commits
1080 # that are now unreachable
1081 PRESERVE_MERGES=t
1084 # yeah, we know
1086 --root)
1087 REBASE_ROOT=t
1089 --autosquash)
1090 AUTOSQUASH=t
1092 --onto)
1093 shift
1094 ONTO=$(parse_onto "$1") ||
1095 die "Does not point to a valid commit: $1"
1098 shift
1099 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
1100 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
1101 test -d "$DOTEST" &&
1102 die "Interactive rebase already started"
1104 git var GIT_COMMITTER_IDENT >/dev/null ||
1105 die "You need to set your committer info first"
1107 if test -z "$REBASE_ROOT"
1108 then
1109 UPSTREAM_ARG="$1"
1110 UPSTREAM=$(git rev-parse --verify "$1") ||
1111 die "Invalid base"
1112 test -z "$ONTO" && ONTO=$UPSTREAM
1113 shift
1114 else
1115 UPSTREAM=
1116 UPSTREAM_ARG=--root
1117 test -z "$ONTO" &&
1118 die "You must specify --onto when using --root"
1119 UPSTREAM=$ONTO
1121 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
1123 comment_for_reflog start
1125 require_clean_work_tree
1127 if test ! -z "$1"
1128 then
1129 output git show-ref --verify --quiet "refs/heads/$1" ||
1130 die "Invalid branchname: $1"
1131 output git checkout "$1" ||
1132 die "Could not checkout $1"
1135 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
1136 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
1138 : > "$DOTEST"/interactive || die "Could not mark as interactive"
1139 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
1140 echo "detached HEAD" > "$DOTEST"/head-name
1142 echo $HEAD > "$DOTEST"/head
1143 test -z "$REBASE_ROOT" || : >"$DOTEST"/rebase-root
1144 echo $ONTO > "$DOTEST"/onto
1145 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
1146 test t = "$VERBOSE" && : > "$DOTEST"/verbose
1148 SHORTHEAD=$(git rev-parse --short $HEAD)
1149 SHORTONTO=$(git rev-parse --short $ONTO)
1150 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
1151 REVISIONS=$UPSTREAM...$HEAD
1152 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
1154 MERGES_OPTION=--no-merges
1155 test t = "$PRESERVE_MERGES" && prepare_preserve_merges
1157 generate_script > "$TODO"
1158 test -n "$AUTOSQUASH" && rearrange_squash "$TODO"
1160 make_bookmarks
1162 test t = "$PRESERVE_MERGES" && handle_dropped_commits
1164 has_action "$TODO" ||
1165 die_abort "Nothing to do"
1167 cp "$TODO" "$TODO".backup
1168 git_editor "$TODO" ||
1169 die_abort "Could not execute editor"
1171 has_action "$TODO" ||
1172 die_abort "Nothing to do"
1174 # once the user uses -p features, implicitely turn -p on
1175 ! grep "^\(goto\|merge\) " < "$TODO" > /dev/null ||
1176 mkdir -p "$REWRITTEN" ||
1177 die "Could not create directory $REWRITTEN/"
1179 test -d "$REWRITTEN" || skip_unnecessary_picks
1181 git update-ref ORIG_HEAD $HEAD
1182 output git checkout $ONTO && do_rest
1184 esac
1185 shift
1186 done