git-am: minor cleanups
[4msysgit-hv.git] / git-rebase--interactive.sh
blob1438650ae8a2ec71615821168970936b9d170f35
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)
33 . git-sh-setup
34 require_work_tree
36 DOTEST="$GIT_DIR/rebase-merge"
37 TODO="$DOTEST"/git-rebase-todo
38 DONE="$DOTEST"/done
39 MSG="$DOTEST"/message
40 SQUASH_MSG="$DOTEST"/message-squash
41 REWRITTEN="$DOTEST"/rewritten
42 DROPPED="$DOTEST"/dropped
43 PRESERVE_MERGES=
44 STRATEGY=
45 ONTO=
46 VERBOSE=
47 OK_TO_SKIP_PRE_REBASE=
48 REBASE_ROOT=
50 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
51 mark the corrected paths with 'git add <paths>', and
52 run 'git rebase --continue'"
53 export GIT_CHERRY_PICK_HELP
55 warn () {
56 echo "$*" >&2
59 output () {
60 case "$VERBOSE" in
61 '')
62 output=$("$@" 2>&1 )
63 status=$?
64 test $status != 0 && printf "%s\n" "$output"
65 return $status
68 "$@"
70 esac
73 run_pre_rebase_hook () {
74 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
75 test -x "$GIT_DIR/hooks/pre-rebase"
76 then
77 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
78 echo >&2 "The pre-rebase hook refused to rebase."
79 exit 1
84 require_clean_work_tree () {
85 # test if working tree is dirty
86 git rev-parse --verify HEAD > /dev/null &&
87 git update-index --ignore-submodules --refresh &&
88 git diff-files --quiet --ignore-submodules &&
89 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
90 die "Working tree is dirty"
93 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
95 comment_for_reflog () {
96 case "$ORIG_REFLOG_ACTION" in
97 ''|rebase*)
98 GIT_REFLOG_ACTION="rebase -i ($1)"
99 export GIT_REFLOG_ACTION
101 esac
104 last_count=
105 mark_action_done () {
106 sed -e 1q < "$TODO" >> "$DONE"
107 sed -e 1d < "$TODO" >> "$TODO".new
108 mv -f "$TODO".new "$TODO"
109 count=$(grep -c '^[^#]' < "$DONE")
110 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
111 if test "$last_count" != "$count"
112 then
113 last_count=$count
114 printf "Rebasing (%d/%d)\r" $count $total
115 test -z "$VERBOSE" || echo
119 make_patch () {
120 sha1_and_parents="$(git rev-list --parents -1 "$1")"
121 case "$sha1_and_parents" in
122 ?*' '?*' '?*)
123 git diff --cc $sha1_and_parents
125 ?*' '?*)
126 git diff-tree -p "$1^!"
129 echo "Root commit"
131 esac > "$DOTEST"/patch
132 test -f "$DOTEST"/message ||
133 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
134 test -f "$DOTEST"/author-script ||
135 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
138 die_with_patch () {
139 make_patch "$1"
140 git rerere
141 die "$2"
144 die_abort () {
145 rm -rf "$DOTEST"
146 die "$1"
149 has_action () {
150 grep '^[^#]' "$1" >/dev/null
153 pick_one () {
154 no_ff=
155 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
156 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
157 test -d "$REWRITTEN" &&
158 pick_one_preserving_merges "$@" && return
159 if test ! -z "$REBASE_ROOT"
160 then
161 output git cherry-pick "$@"
162 return
164 parent_sha1=$(git rev-parse --verify $sha1^) ||
165 die "Could not get the parent of $sha1"
166 current_sha1=$(git rev-parse --verify HEAD)
167 if test "$no_ff$current_sha1" = "$parent_sha1"; then
168 output git reset --hard $sha1
169 test "a$1" = a-n && output git reset --soft $current_sha1
170 sha1=$(git rev-parse --short $sha1)
171 output warn Fast forward to $sha1
172 else
173 output git cherry-pick "$@"
177 pick_one_preserving_merges () {
178 fast_forward=t
179 case "$1" in
181 fast_forward=f
182 sha1=$2
185 sha1=$1
187 esac
188 sha1=$(git rev-parse $sha1)
190 if test -f "$DOTEST"/current-commit
191 then
192 if test "$fast_forward" = t
193 then
194 cat "$DOTEST"/current-commit | while read current_commit
196 git rev-parse HEAD > "$REWRITTEN"/$current_commit
197 done
198 rm "$DOTEST"/current-commit ||
199 die "Cannot write current commit's replacement sha1"
203 echo $sha1 >> "$DOTEST"/current-commit
205 # rewrite parents; if none were rewritten, we can fast-forward.
206 new_parents=
207 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)"
208 if test "$pend" = " "
209 then
210 pend=" root"
212 while [ "$pend" != "" ]
214 p=$(expr "$pend" : ' \([^ ]*\)')
215 pend="${pend# $p}"
217 if test -f "$REWRITTEN"/$p
218 then
219 new_p=$(cat "$REWRITTEN"/$p)
221 # If the todo reordered commits, and our parent is marked for
222 # rewriting, but hasn't been gotten to yet, assume the user meant to
223 # drop it on top of the current HEAD
224 if test -z "$new_p"
225 then
226 new_p=$(git rev-parse HEAD)
229 test $p != $new_p && fast_forward=f
230 case "$new_parents" in
231 *$new_p*)
232 ;; # do nothing; that parent is already there
234 new_parents="$new_parents $new_p"
236 esac
237 else
238 if test -f "$DROPPED"/$p
239 then
240 fast_forward=f
241 replacement="$(cat "$DROPPED"/$p)"
242 test -z "$replacement" && replacement=root
243 pend=" $replacement$pend"
244 else
245 new_parents="$new_parents $p"
248 done
249 case $fast_forward in
251 output warn "Fast forward to $sha1"
252 output git reset --hard $sha1 ||
253 die "Cannot fast forward to $sha1"
256 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
258 if [ "$1" != "-n" ]
259 then
260 # detach HEAD to current parent
261 output git checkout $first_parent 2> /dev/null ||
262 die "Cannot move HEAD to $first_parent"
265 case "$new_parents" in
266 ' '*' '*)
267 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
269 # redo merge
270 author_script=$(get_author_ident_from_commit $sha1)
271 eval "$author_script"
272 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
273 # No point in merging the first parent, that's HEAD
274 new_parents=${new_parents# $first_parent}
275 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
276 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
277 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
278 output git merge $STRATEGY -m "$msg" \
279 $new_parents
280 then
281 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
282 die_with_patch $sha1 "Error redoing merge $sha1"
286 output git cherry-pick "$@" ||
287 die_with_patch $sha1 "Could not pick $sha1"
289 esac
291 esac
294 nth_string () {
295 case "$1" in
296 *1[0-9]|*[04-9]) echo "$1"th;;
297 *1) echo "$1"st;;
298 *2) echo "$1"nd;;
299 *3) echo "$1"rd;;
300 esac
303 make_squash_message () {
304 if test -f "$SQUASH_MSG"; then
305 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
306 < "$SQUASH_MSG" | sed -ne '$p')+1))
307 echo "# This is a combination of $COUNT commits."
308 sed -e 1d -e '2,/^./{
309 /^$/d
310 }' <"$SQUASH_MSG"
311 else
312 COUNT=2
313 echo "# This is a combination of two commits."
314 echo "# The first commit's message is:"
315 echo
316 git cat-file commit HEAD | sed -e '1,/^$/d'
318 echo
319 echo "# This is the $(nth_string $COUNT) commit message:"
320 echo
321 git cat-file commit $1 | sed -e '1,/^$/d'
324 peek_next_command () {
325 sed -n "1s/ .*$//p" < "$TODO"
328 do_next () {
329 rm -f "$DOTEST"/message "$DOTEST"/author-script \
330 "$DOTEST"/amend || exit
331 read command sha1 rest < "$TODO"
332 case "$command" in
333 '#'*|''|noop)
334 mark_action_done
336 pick|p)
337 comment_for_reflog pick
339 mark_action_done
340 pick_one $sha1 ||
341 die_with_patch $sha1 "Could not apply $sha1... $rest"
343 edit|e)
344 comment_for_reflog edit
346 mark_action_done
347 pick_one $sha1 ||
348 die_with_patch $sha1 "Could not apply $sha1... $rest"
349 make_patch $sha1
350 git rev-parse --verify HEAD > "$DOTEST"/amend
351 warn "Stopped at $sha1... $rest"
352 warn "You can amend the commit now, with"
353 warn
354 warn " git commit --amend"
355 warn
356 warn "Once you are satisfied with your changes, run"
357 warn
358 warn " git rebase --continue"
359 warn
360 exit 0
362 squash|s)
363 comment_for_reflog squash
365 test -f "$DONE" && has_action "$DONE" ||
366 die "Cannot 'squash' without a previous commit"
368 mark_action_done
369 make_squash_message $sha1 > "$MSG"
370 failed=f
371 author_script=$(get_author_ident_from_commit HEAD)
372 output git reset --soft HEAD^
373 pick_one -n $sha1 || failed=t
374 case "$(peek_next_command)" in
375 squash|s)
376 EDIT_COMMIT=
377 USE_OUTPUT=output
378 MSG_OPT=-F
379 MSG_FILE="$MSG"
380 cp "$MSG" "$SQUASH_MSG"
383 EDIT_COMMIT=-e
384 USE_OUTPUT=
385 MSG_OPT=
386 MSG_FILE=
387 rm -f "$SQUASH_MSG" || exit
388 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
389 rm -f "$GIT_DIR"/MERGE_MSG || exit
391 esac
392 echo "$author_script" > "$DOTEST"/author-script
393 if test $failed = f
394 then
395 # This is like --amend, but with a different message
396 eval "$author_script"
397 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
398 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
399 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
400 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
402 if test $failed = t
403 then
404 cp "$MSG" "$GIT_DIR"/MERGE_MSG
405 warn
406 warn "Could not apply $sha1... $rest"
407 die_with_patch $sha1 ""
411 warn "Unknown command: $command $sha1 $rest"
412 die_with_patch $sha1 "Please fix this in the file $TODO."
414 esac
415 test -s "$TODO" && return
417 comment_for_reflog finish &&
418 HEADNAME=$(cat "$DOTEST"/head-name) &&
419 OLDHEAD=$(cat "$DOTEST"/head) &&
420 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
421 NEWHEAD=$(git rev-parse HEAD) &&
422 case $HEADNAME in
423 refs/*)
424 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
425 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
426 git symbolic-ref HEAD $HEADNAME
428 esac && {
429 test ! -f "$DOTEST"/verbose ||
430 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
431 } &&
432 rm -rf "$DOTEST" &&
433 git gc --auto &&
434 warn "Successfully rebased and updated $HEADNAME."
436 exit
439 do_rest () {
440 while :
442 do_next
443 done
446 # check if no other options are set
447 is_standalone () {
448 test $# -eq 2 -a "$2" = '--' &&
449 test -z "$ONTO" &&
450 test -z "$PRESERVE_MERGES" &&
451 test -z "$STRATEGY" &&
452 test -z "$VERBOSE"
455 get_saved_options () {
456 test -d "$REWRITTEN" && PRESERVE_MERGES=t
457 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
458 test -f "$DOTEST"/verbose && VERBOSE=t
459 test -f "$DOTEST"/rebase-root && REBASE_ROOT=t
462 while test $# != 0
464 case "$1" in
465 --no-verify)
466 OK_TO_SKIP_PRE_REBASE=yes
468 --verify)
470 --continue)
471 is_standalone "$@" || usage
472 get_saved_options
473 comment_for_reflog continue
475 test -d "$DOTEST" || die "No interactive rebase running"
477 # Sanity check
478 git rev-parse --verify HEAD >/dev/null ||
479 die "Cannot read HEAD"
480 git update-index --ignore-submodules --refresh &&
481 git diff-files --quiet --ignore-submodules ||
482 die "Working tree is dirty"
484 # do we have anything to commit?
485 if git diff-index --cached --quiet --ignore-submodules HEAD --
486 then
487 : Nothing to commit -- skip this
488 else
489 . "$DOTEST"/author-script ||
490 die "Cannot find the author identity"
491 amend=
492 if test -f "$DOTEST"/amend
493 then
494 amend=$(git rev-parse --verify HEAD)
495 test "$amend" = $(cat "$DOTEST"/amend) ||
496 die "\
497 You have uncommitted changes in your working tree. Please, commit them
498 first and then run 'git rebase --continue' again."
499 git reset --soft HEAD^ ||
500 die "Cannot rewind the HEAD"
502 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
503 git commit --no-verify -F "$DOTEST"/message -e || {
504 test -n "$amend" && git reset --soft $amend
505 die "Could not commit staged changes."
509 require_clean_work_tree
510 do_rest
512 --abort)
513 is_standalone "$@" || usage
514 get_saved_options
515 comment_for_reflog abort
517 git rerere clear
518 test -d "$DOTEST" || die "No interactive rebase running"
520 HEADNAME=$(cat "$DOTEST"/head-name)
521 HEAD=$(cat "$DOTEST"/head)
522 case $HEADNAME in
523 refs/*)
524 git symbolic-ref HEAD $HEADNAME
526 esac &&
527 output git reset --hard $HEAD &&
528 rm -rf "$DOTEST"
529 exit
531 --skip)
532 is_standalone "$@" || usage
533 get_saved_options
534 comment_for_reflog skip
536 git rerere clear
537 test -d "$DOTEST" || die "No interactive rebase running"
539 output git reset --hard && do_rest
542 case "$#,$1" in
543 *,*=*)
544 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
545 1,*)
546 usage ;;
548 STRATEGY="-s $2"
549 shift ;;
550 esac
553 # we use merge anyway
556 VERBOSE=t
559 PRESERVE_MERGES=t
562 # yeah, we know
564 --root)
565 REBASE_ROOT=t
567 --onto)
568 shift
569 ONTO=$(git rev-parse --verify "$1") ||
570 die "Does not point to a valid commit: $1"
573 shift
574 test -z "$REBASE_ROOT" -a $# -ge 1 -a $# -le 2 ||
575 test ! -z "$REBASE_ROOT" -a $# -le 1 || usage
576 test -d "$DOTEST" &&
577 die "Interactive rebase already started"
579 git var GIT_COMMITTER_IDENT >/dev/null ||
580 die "You need to set your committer info first"
582 if test -z "$REBASE_ROOT"
583 then
584 UPSTREAM_ARG="$1"
585 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
586 test -z "$ONTO" && ONTO=$UPSTREAM
587 shift
588 else
589 UPSTREAM=
590 UPSTREAM_ARG=--root
591 test -z "$ONTO" &&
592 die "You must specify --onto when using --root"
594 run_pre_rebase_hook "$UPSTREAM_ARG" "$@"
596 comment_for_reflog start
598 require_clean_work_tree
600 if test ! -z "$1"
601 then
602 output git show-ref --verify --quiet "refs/heads/$1" ||
603 die "Invalid branchname: $1"
604 output git checkout "$1" ||
605 die "Could not checkout $1"
608 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
609 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
611 : > "$DOTEST"/interactive || die "Could not mark as interactive"
612 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
613 echo "detached HEAD" > "$DOTEST"/head-name
615 echo $HEAD > "$DOTEST"/head
616 case "$REBASE_ROOT" in
618 rm -f "$DOTEST"/rebase-root ;;
620 : >"$DOTEST"/rebase-root ;;
621 esac
622 echo $ONTO > "$DOTEST"/onto
623 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
624 test t = "$VERBOSE" && : > "$DOTEST"/verbose
625 if test t = "$PRESERVE_MERGES"
626 then
627 # $REWRITTEN contains files for each commit that is
628 # reachable by at least one merge base of $HEAD and
629 # $UPSTREAM. They are not necessarily rewritten, but
630 # their children might be.
631 # This ensures that commits on merged, but otherwise
632 # unrelated side branches are left alone. (Think "X"
633 # in the man page's example.)
634 if test -z "$REBASE_ROOT"
635 then
636 mkdir "$REWRITTEN" &&
637 for c in $(git merge-base --all $HEAD $UPSTREAM)
639 echo $ONTO > "$REWRITTEN"/$c ||
640 die "Could not init rewritten commits"
641 done
642 else
643 mkdir "$REWRITTEN" &&
644 echo $ONTO > "$REWRITTEN"/root ||
645 die "Could not init rewritten commits"
647 # No cherry-pick because our first pass is to determine
648 # parents to rewrite and skipping dropped commits would
649 # prematurely end our probe
650 MERGES_OPTION=
651 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
652 else
653 MERGES_OPTION="--no-merges --cherry-pick"
656 SHORTHEAD=$(git rev-parse --short $HEAD)
657 SHORTONTO=$(git rev-parse --short $ONTO)
658 if test -z "$REBASE_ROOT"
659 # this is now equivalent to ! -z "$UPSTREAM"
660 then
661 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
662 REVISIONS=$UPSTREAM...$HEAD
663 SHORTREVISIONS=$SHORTUPSTREAM..$SHORTHEAD
664 else
665 REVISIONS=$ONTO...$HEAD
666 SHORTREVISIONS=$SHORTHEAD
668 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
669 --abbrev=7 --reverse --left-right --topo-order \
670 $REVISIONS | \
671 sed -n "s/^>//p" | while read shortsha1 rest
673 if test t != "$PRESERVE_MERGES"
674 then
675 echo "pick $shortsha1 $rest" >> "$TODO"
676 else
677 sha1=$(git rev-parse $shortsha1)
678 if test -z "$REBASE_ROOT"
679 then
680 preserve=t
681 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -s -f2-)
683 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
684 then
685 preserve=f
687 done
688 else
689 preserve=f
691 if test f = "$preserve"
692 then
693 touch "$REWRITTEN"/$sha1
694 echo "pick $shortsha1 $rest" >> "$TODO"
697 done
699 # Watch for commits that been dropped by --cherry-pick
700 if test t = "$PRESERVE_MERGES"
701 then
702 mkdir "$DROPPED"
703 # Save all non-cherry-picked changes
704 git rev-list $REVISIONS --left-right --cherry-pick | \
705 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
706 # Now all commits and note which ones are missing in
707 # not-cherry-picks and hence being dropped
708 git rev-list $REVISIONS |
709 while read rev
711 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
712 then
713 # Use -f2 because if rev-list is telling us this commit is
714 # not worthwhile, we don't want to track its multiple heads,
715 # just the history of its first-parent for others that will
716 # be rebasing on top of it
717 git rev-list --parents -1 $rev | cut -d' ' -s -f2 > "$DROPPED"/$rev
718 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
719 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
720 rm "$REWRITTEN"/$rev
722 done
725 test -s "$TODO" || echo noop >> "$TODO"
726 cat >> "$TODO" << EOF
728 # Rebase $SHORTREVISIONS onto $SHORTONTO
730 # Commands:
731 # p, pick = use commit
732 # e, edit = use commit, but stop for amending
733 # s, squash = use commit, but meld into previous commit
735 # If you remove a line here THAT COMMIT WILL BE LOST.
736 # However, if you remove everything, the rebase will be aborted.
740 has_action "$TODO" ||
741 die_abort "Nothing to do"
743 cp "$TODO" "$TODO".backup
744 git_editor "$TODO" ||
745 die "Could not execute editor"
747 has_action "$TODO" ||
748 die_abort "Nothing to do"
750 git update-ref ORIG_HEAD $HEAD
751 output git checkout $ONTO && do_rest
753 esac
754 shift
755 done