Merge branch 'maint-1.6.0' into maint
[git/mingw.git] / git-rebase--interactive.sh
blob8ed2244819d0950ddf1ffaa151b4d46bdc8b6db4
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
32 . git-sh-setup
33 require_work_tree
35 DOTEST="$GIT_DIR/rebase-merge"
36 TODO="$DOTEST"/git-rebase-todo
37 DONE="$DOTEST"/done
38 MSG="$DOTEST"/message
39 SQUASH_MSG="$DOTEST"/message-squash
40 REWRITTEN="$DOTEST"/rewritten
41 DROPPED="$DOTEST"/dropped
42 PRESERVE_MERGES=
43 STRATEGY=
44 ONTO=
45 VERBOSE=
46 OK_TO_SKIP_PRE_REBASE=
48 GIT_CHERRY_PICK_HELP=" After resolving the conflicts,
49 mark the corrected paths with 'git add <paths>', and
50 run 'git rebase --continue'"
51 export GIT_CHERRY_PICK_HELP
53 warn () {
54 echo "$*" >&2
57 output () {
58 case "$VERBOSE" in
59 '')
60 output=$("$@" 2>&1 )
61 status=$?
62 test $status != 0 && printf "%s\n" "$output"
63 return $status
66 "$@"
68 esac
71 run_pre_rebase_hook () {
72 if test -z "$OK_TO_SKIP_PRE_REBASE" &&
73 test -x "$GIT_DIR/hooks/pre-rebase"
74 then
75 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
76 echo >&2 "The pre-rebase hook refused to rebase."
77 exit 1
82 require_clean_work_tree () {
83 # test if working tree is dirty
84 git rev-parse --verify HEAD > /dev/null &&
85 git update-index --ignore-submodules --refresh &&
86 git diff-files --quiet --ignore-submodules &&
87 git diff-index --cached --quiet HEAD --ignore-submodules -- ||
88 die "Working tree is dirty"
91 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
93 comment_for_reflog () {
94 case "$ORIG_REFLOG_ACTION" in
95 ''|rebase*)
96 GIT_REFLOG_ACTION="rebase -i ($1)"
97 export GIT_REFLOG_ACTION
99 esac
102 last_count=
103 mark_action_done () {
104 sed -e 1q < "$TODO" >> "$DONE"
105 sed -e 1d < "$TODO" >> "$TODO".new
106 mv -f "$TODO".new "$TODO"
107 count=$(grep -c '^[^#]' < "$DONE")
108 total=$(($count+$(grep -c '^[^#]' < "$TODO")))
109 if test "$last_count" != "$count"
110 then
111 last_count=$count
112 printf "Rebasing (%d/%d)\r" $count $total
113 test -z "$VERBOSE" || echo
117 make_patch () {
118 sha1_and_parents="$(git rev-list --parents -1 "$1")"
119 case "$sha1_and_parents" in
120 ?*' '?*' '?*)
121 git diff --cc $sha1_and_parents
123 ?*' '?*)
124 git diff-tree -p "$1^!"
127 echo "Root commit"
129 esac > "$DOTEST"/patch
130 test -f "$DOTEST"/message ||
131 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
132 test -f "$DOTEST"/author-script ||
133 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
136 die_with_patch () {
137 make_patch "$1"
138 git rerere
139 die "$2"
142 die_abort () {
143 rm -rf "$DOTEST"
144 die "$1"
147 has_action () {
148 grep '^[^#]' "$1" >/dev/null
151 pick_one () {
152 no_ff=
153 case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
154 output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
155 test -d "$REWRITTEN" &&
156 pick_one_preserving_merges "$@" && return
157 parent_sha1=$(git rev-parse --verify $sha1^) ||
158 die "Could not get the parent of $sha1"
159 current_sha1=$(git rev-parse --verify HEAD)
160 if test "$no_ff$current_sha1" = "$parent_sha1"; then
161 output git reset --hard $sha1
162 test "a$1" = a-n && output git reset --soft $current_sha1
163 sha1=$(git rev-parse --short $sha1)
164 output warn Fast forward to $sha1
165 else
166 output git cherry-pick "$@"
170 pick_one_preserving_merges () {
171 fast_forward=t
172 case "$1" in
174 fast_forward=f
175 sha1=$2
178 sha1=$1
180 esac
181 sha1=$(git rev-parse $sha1)
183 if test -f "$DOTEST"/current-commit
184 then
185 if test "$fast_forward" = t
186 then
187 cat "$DOTEST"/current-commit | while read current_commit
189 git rev-parse HEAD > "$REWRITTEN"/$current_commit
190 done
191 rm "$DOTEST"/current-commit ||
192 die "Cannot write current commit's replacement sha1"
196 echo $sha1 >> "$DOTEST"/current-commit
198 # rewrite parents; if none were rewritten, we can fast-forward.
199 new_parents=
200 pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
201 while [ "$pend" != "" ]
203 p=$(expr "$pend" : ' \([^ ]*\)')
204 pend="${pend# $p}"
206 if test -f "$REWRITTEN"/$p
207 then
208 new_p=$(cat "$REWRITTEN"/$p)
210 # If the todo reordered commits, and our parent is marked for
211 # rewriting, but hasn't been gotten to yet, assume the user meant to
212 # drop it on top of the current HEAD
213 if test -z "$new_p"
214 then
215 new_p=$(git rev-parse HEAD)
218 test $p != $new_p && fast_forward=f
219 case "$new_parents" in
220 *$new_p*)
221 ;; # do nothing; that parent is already there
223 new_parents="$new_parents $new_p"
225 esac
226 else
227 if test -f "$DROPPED"/$p
228 then
229 fast_forward=f
230 pend=" $(cat "$DROPPED"/$p)$pend"
231 else
232 new_parents="$new_parents $p"
235 done
236 case $fast_forward in
238 output warn "Fast forward to $sha1"
239 output git reset --hard $sha1 ||
240 die "Cannot fast forward to $sha1"
243 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
245 if [ "$1" != "-n" ]
246 then
247 # detach HEAD to current parent
248 output git checkout $first_parent 2> /dev/null ||
249 die "Cannot move HEAD to $first_parent"
252 case "$new_parents" in
253 ' '*' '*)
254 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
256 # redo merge
257 author_script=$(get_author_ident_from_commit $sha1)
258 eval "$author_script"
259 msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
260 # No point in merging the first parent, that's HEAD
261 new_parents=${new_parents# $first_parent}
262 if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
263 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
264 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
265 output git merge $STRATEGY -m "$msg" \
266 $new_parents
267 then
268 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
269 die_with_patch $sha1 "Error redoing merge $sha1"
273 output git cherry-pick "$@" ||
274 die_with_patch $sha1 "Could not pick $sha1"
276 esac
278 esac
281 nth_string () {
282 case "$1" in
283 *1[0-9]|*[04-9]) echo "$1"th;;
284 *1) echo "$1"st;;
285 *2) echo "$1"nd;;
286 *3) echo "$1"rd;;
287 esac
290 make_squash_message () {
291 if test -f "$SQUASH_MSG"; then
292 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
293 < "$SQUASH_MSG" | sed -ne '$p')+1))
294 echo "# This is a combination of $COUNT commits."
295 sed -e 1d -e '2,/^./{
296 /^$/d
297 }' <"$SQUASH_MSG"
298 else
299 COUNT=2
300 echo "# This is a combination of two commits."
301 echo "# The first commit's message is:"
302 echo
303 git cat-file commit HEAD | sed -e '1,/^$/d'
305 echo
306 echo "# This is the $(nth_string $COUNT) commit message:"
307 echo
308 git cat-file commit $1 | sed -e '1,/^$/d'
311 peek_next_command () {
312 sed -n "1s/ .*$//p" < "$TODO"
315 do_next () {
316 rm -f "$DOTEST"/message "$DOTEST"/author-script \
317 "$DOTEST"/amend || exit
318 read command sha1 rest < "$TODO"
319 case "$command" in
320 '#'*|''|noop)
321 mark_action_done
323 pick|p)
324 comment_for_reflog pick
326 mark_action_done
327 pick_one $sha1 ||
328 die_with_patch $sha1 "Could not apply $sha1... $rest"
330 edit|e)
331 comment_for_reflog edit
333 mark_action_done
334 pick_one $sha1 ||
335 die_with_patch $sha1 "Could not apply $sha1... $rest"
336 make_patch $sha1
337 git rev-parse --verify HEAD > "$DOTEST"/amend
338 warn "Stopped at $sha1... $rest"
339 warn "You can amend the commit now, with"
340 warn
341 warn " git commit --amend"
342 warn
343 warn "Once you are satisfied with your changes, run"
344 warn
345 warn " git rebase --continue"
346 warn
347 exit 0
349 squash|s)
350 comment_for_reflog squash
352 test -f "$DONE" && has_action "$DONE" ||
353 die "Cannot 'squash' without a previous commit"
355 mark_action_done
356 make_squash_message $sha1 > "$MSG"
357 failed=f
358 author_script=$(get_author_ident_from_commit HEAD)
359 output git reset --soft HEAD^
360 pick_one -n $sha1 || failed=t
361 case "$(peek_next_command)" in
362 squash|s)
363 EDIT_COMMIT=
364 USE_OUTPUT=output
365 MSG_OPT=-F
366 MSG_FILE="$MSG"
367 cp "$MSG" "$SQUASH_MSG"
370 EDIT_COMMIT=-e
371 USE_OUTPUT=
372 MSG_OPT=
373 MSG_FILE=
374 rm -f "$SQUASH_MSG" || exit
375 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
376 rm -f "$GIT_DIR"/MERGE_MSG || exit
378 esac
379 echo "$author_script" > "$DOTEST"/author-script
380 if test $failed = f
381 then
382 # This is like --amend, but with a different message
383 eval "$author_script"
384 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
385 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
386 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
387 $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
389 if test $failed = t
390 then
391 cp "$MSG" "$GIT_DIR"/MERGE_MSG
392 warn
393 warn "Could not apply $sha1... $rest"
394 die_with_patch $sha1 ""
398 warn "Unknown command: $command $sha1 $rest"
399 die_with_patch $sha1 "Please fix this in the file $TODO."
401 esac
402 test -s "$TODO" && return
404 comment_for_reflog finish &&
405 HEADNAME=$(cat "$DOTEST"/head-name) &&
406 OLDHEAD=$(cat "$DOTEST"/head) &&
407 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
408 NEWHEAD=$(git rev-parse HEAD) &&
409 case $HEADNAME in
410 refs/*)
411 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
412 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
413 git symbolic-ref HEAD $HEADNAME
415 esac && {
416 test ! -f "$DOTEST"/verbose ||
417 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
418 } &&
419 rm -rf "$DOTEST" &&
420 git gc --auto &&
421 warn "Successfully rebased and updated $HEADNAME."
423 exit
426 do_rest () {
427 while :
429 do_next
430 done
433 # check if no other options are set
434 is_standalone () {
435 test $# -eq 2 -a "$2" = '--' &&
436 test -z "$ONTO" &&
437 test -z "$PRESERVE_MERGES" &&
438 test -z "$STRATEGY" &&
439 test -z "$VERBOSE"
442 get_saved_options () {
443 test -d "$REWRITTEN" && PRESERVE_MERGES=t
444 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
445 test -f "$DOTEST"/verbose && VERBOSE=t
448 while test $# != 0
450 case "$1" in
451 --no-verify)
452 OK_TO_SKIP_PRE_REBASE=yes
454 --verify)
456 --continue)
457 is_standalone "$@" || usage
458 get_saved_options
459 comment_for_reflog continue
461 test -d "$DOTEST" || die "No interactive rebase running"
463 # Sanity check
464 git rev-parse --verify HEAD >/dev/null ||
465 die "Cannot read HEAD"
466 git update-index --ignore-submodules --refresh &&
467 git diff-files --quiet --ignore-submodules ||
468 die "Working tree is dirty"
470 # do we have anything to commit?
471 if git diff-index --cached --quiet --ignore-submodules HEAD --
472 then
473 : Nothing to commit -- skip this
474 else
475 . "$DOTEST"/author-script ||
476 die "Cannot find the author identity"
477 amend=
478 if test -f "$DOTEST"/amend
479 then
480 amend=$(git rev-parse --verify HEAD)
481 test "$amend" = $(cat "$DOTEST"/amend) ||
482 die "\
483 You have uncommitted changes in your working tree. Please, commit them
484 first and then run 'git rebase --continue' again."
485 git reset --soft HEAD^ ||
486 die "Cannot rewind the HEAD"
488 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
489 git commit --no-verify -F "$DOTEST"/message -e || {
490 test -n "$amend" && git reset --soft $amend
491 die "Could not commit staged changes."
495 require_clean_work_tree
496 do_rest
498 --abort)
499 is_standalone "$@" || usage
500 get_saved_options
501 comment_for_reflog abort
503 git rerere clear
504 test -d "$DOTEST" || die "No interactive rebase running"
506 HEADNAME=$(cat "$DOTEST"/head-name)
507 HEAD=$(cat "$DOTEST"/head)
508 case $HEADNAME in
509 refs/*)
510 git symbolic-ref HEAD $HEADNAME
512 esac &&
513 output git reset --hard $HEAD &&
514 rm -rf "$DOTEST"
515 exit
517 --skip)
518 is_standalone "$@" || usage
519 get_saved_options
520 comment_for_reflog skip
522 git rerere clear
523 test -d "$DOTEST" || die "No interactive rebase running"
525 output git reset --hard && do_rest
528 case "$#,$1" in
529 *,*=*)
530 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
531 1,*)
532 usage ;;
534 STRATEGY="-s $2"
535 shift ;;
536 esac
539 # we use merge anyway
542 VERBOSE=t
545 PRESERVE_MERGES=t
548 # yeah, we know
550 --onto)
551 shift
552 ONTO=$(git rev-parse --verify "$1") ||
553 die "Does not point to a valid commit: $1"
556 shift
557 run_pre_rebase_hook ${1+"$@"}
558 test $# -eq 1 -o $# -eq 2 || usage
559 test -d "$DOTEST" &&
560 die "Interactive rebase already started"
562 git var GIT_COMMITTER_IDENT >/dev/null ||
563 die "You need to set your committer info first"
565 comment_for_reflog start
567 require_clean_work_tree
569 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
570 test -z "$ONTO" && ONTO=$UPSTREAM
572 if test ! -z "$2"
573 then
574 output git show-ref --verify --quiet "refs/heads/$2" ||
575 die "Invalid branchname: $2"
576 output git checkout "$2" ||
577 die "Could not checkout $2"
580 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
581 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
583 : > "$DOTEST"/interactive || die "Could not mark as interactive"
584 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
585 echo "detached HEAD" > "$DOTEST"/head-name
587 echo $HEAD > "$DOTEST"/head
588 echo $UPSTREAM > "$DOTEST"/upstream
589 echo $ONTO > "$DOTEST"/onto
590 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
591 test t = "$VERBOSE" && : > "$DOTEST"/verbose
592 if test t = "$PRESERVE_MERGES"
593 then
594 # $REWRITTEN contains files for each commit that is
595 # reachable by at least one merge base of $HEAD and
596 # $UPSTREAM. They are not necessarily rewritten, but
597 # their children might be.
598 # This ensures that commits on merged, but otherwise
599 # unrelated side branches are left alone. (Think "X"
600 # in the man page's example.)
601 mkdir "$REWRITTEN" &&
602 for c in $(git merge-base --all $HEAD $UPSTREAM)
604 echo $ONTO > "$REWRITTEN"/$c ||
605 die "Could not init rewritten commits"
606 done
607 # No cherry-pick because our first pass is to determine
608 # parents to rewrite and skipping dropped commits would
609 # prematurely end our probe
610 MERGES_OPTION=
611 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
612 else
613 MERGES_OPTION="--no-merges --cherry-pick"
616 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
617 SHORTHEAD=$(git rev-parse --short $HEAD)
618 SHORTONTO=$(git rev-parse --short $ONTO)
619 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
620 --abbrev=7 --reverse --left-right --topo-order \
621 $UPSTREAM...$HEAD | \
622 sed -n "s/^>//p" | while read shortsha1 rest
624 if test t != "$PRESERVE_MERGES"
625 then
626 echo "pick $shortsha1 $rest" >> "$TODO"
627 else
628 sha1=$(git rev-parse $shortsha1)
629 preserve=t
630 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
632 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
633 then
634 preserve=f
636 done
637 if test f = "$preserve"
638 then
639 touch "$REWRITTEN"/$sha1
640 echo "pick $shortsha1 $rest" >> "$TODO"
643 done
645 # Watch for commits that been dropped by --cherry-pick
646 if test t = "$PRESERVE_MERGES"
647 then
648 mkdir "$DROPPED"
649 # Save all non-cherry-picked changes
650 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
651 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
652 # Now all commits and note which ones are missing in
653 # not-cherry-picks and hence being dropped
654 git rev-list $UPSTREAM..$HEAD |
655 while read rev
657 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
658 then
659 # Use -f2 because if rev-list is telling us this commit is
660 # not worthwhile, we don't want to track its multiple heads,
661 # just the history of its first-parent for others that will
662 # be rebasing on top of it
663 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
664 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
665 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
666 rm "$REWRITTEN"/$rev
668 done
670 test -s "$TODO" || echo noop >> "$TODO"
671 cat >> "$TODO" << EOF
673 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
675 # Commands:
676 # p, pick = use commit
677 # e, edit = use commit, but stop for amending
678 # s, squash = use commit, but meld into previous commit
680 # If you remove a line here THAT COMMIT WILL BE LOST.
681 # However, if you remove everything, the rebase will be aborted.
685 has_action "$TODO" ||
686 die_abort "Nothing to do"
688 cp "$TODO" "$TODO".backup
689 git_editor "$TODO" ||
690 die "Could not execute editor"
692 has_action "$TODO" ||
693 die_abort "Nothing to do"
695 git update-ref ORIG_HEAD $HEAD
696 output git checkout $ONTO && do_rest
698 esac
699 shift
700 done