Merge branch 'js/maint-1.6.1-filter-branch-submodule' into maint-1.6.1
[git/ydirson.git] / git-rebase--interactive.sh
blob1ceb57ae8302dc3f0673779cf60f70df5c26a64d
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 USE_OUTPUT=output
364 MSG_OPT=-F
365 EDIT_OR_FILE="$MSG"
366 cp "$MSG" "$SQUASH_MSG"
369 USE_OUTPUT=
370 MSG_OPT=
371 EDIT_OR_FILE=-e
372 rm -f "$SQUASH_MSG" || exit
373 cp "$MSG" "$GIT_DIR"/SQUASH_MSG
374 rm -f "$GIT_DIR"/MERGE_MSG || exit
376 esac
377 echo "$author_script" > "$DOTEST"/author-script
378 if test $failed = f
379 then
380 # This is like --amend, but with a different message
381 eval "$author_script"
382 GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
383 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
384 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
385 $USE_OUTPUT git commit --no-verify \
386 $MSG_OPT "$EDIT_OR_FILE" || failed=t
388 if test $failed = t
389 then
390 cp "$MSG" "$GIT_DIR"/MERGE_MSG
391 warn
392 warn "Could not apply $sha1... $rest"
393 die_with_patch $sha1 ""
397 warn "Unknown command: $command $sha1 $rest"
398 die_with_patch $sha1 "Please fix this in the file $TODO."
400 esac
401 test -s "$TODO" && return
403 comment_for_reflog finish &&
404 HEADNAME=$(cat "$DOTEST"/head-name) &&
405 OLDHEAD=$(cat "$DOTEST"/head) &&
406 SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
407 NEWHEAD=$(git rev-parse HEAD) &&
408 case $HEADNAME in
409 refs/*)
410 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
411 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
412 git symbolic-ref HEAD $HEADNAME
414 esac && {
415 test ! -f "$DOTEST"/verbose ||
416 git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
417 } &&
418 rm -rf "$DOTEST" &&
419 git gc --auto &&
420 warn "Successfully rebased and updated $HEADNAME."
422 exit
425 do_rest () {
426 while :
428 do_next
429 done
432 # check if no other options are set
433 is_standalone () {
434 test $# -eq 2 -a "$2" = '--' &&
435 test -z "$ONTO" &&
436 test -z "$PRESERVE_MERGES" &&
437 test -z "$STRATEGY" &&
438 test -z "$VERBOSE"
441 get_saved_options () {
442 test -d "$REWRITTEN" && PRESERVE_MERGES=t
443 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
444 test -f "$DOTEST"/verbose && VERBOSE=t
447 while test $# != 0
449 case "$1" in
450 --no-verify)
451 OK_TO_SKIP_PRE_REBASE=yes
453 --verify)
455 --continue)
456 is_standalone "$@" || usage
457 get_saved_options
458 comment_for_reflog continue
460 test -d "$DOTEST" || die "No interactive rebase running"
462 # Sanity check
463 git rev-parse --verify HEAD >/dev/null ||
464 die "Cannot read HEAD"
465 git update-index --ignore-submodules --refresh &&
466 git diff-files --quiet --ignore-submodules ||
467 die "Working tree is dirty"
469 # do we have anything to commit?
470 if git diff-index --cached --quiet --ignore-submodules HEAD --
471 then
472 : Nothing to commit -- skip this
473 else
474 . "$DOTEST"/author-script ||
475 die "Cannot find the author identity"
476 amend=
477 if test -f "$DOTEST"/amend
478 then
479 amend=$(git rev-parse --verify HEAD)
480 test "$amend" = $(cat "$DOTEST"/amend) ||
481 die "\
482 You have uncommitted changes in your working tree. Please, commit them
483 first and then run 'git rebase --continue' again."
484 git reset --soft HEAD^ ||
485 die "Cannot rewind the HEAD"
487 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
488 git commit --no-verify -F "$DOTEST"/message -e || {
489 test -n "$amend" && git reset --soft $amend
490 die "Could not commit staged changes."
494 require_clean_work_tree
495 do_rest
497 --abort)
498 is_standalone "$@" || usage
499 get_saved_options
500 comment_for_reflog abort
502 git rerere clear
503 test -d "$DOTEST" || die "No interactive rebase running"
505 HEADNAME=$(cat "$DOTEST"/head-name)
506 HEAD=$(cat "$DOTEST"/head)
507 case $HEADNAME in
508 refs/*)
509 git symbolic-ref HEAD $HEADNAME
511 esac &&
512 output git reset --hard $HEAD &&
513 rm -rf "$DOTEST"
514 exit
516 --skip)
517 is_standalone "$@" || usage
518 get_saved_options
519 comment_for_reflog skip
521 git rerere clear
522 test -d "$DOTEST" || die "No interactive rebase running"
524 output git reset --hard && do_rest
527 case "$#,$1" in
528 *,*=*)
529 STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
530 1,*)
531 usage ;;
533 STRATEGY="-s $2"
534 shift ;;
535 esac
538 # we use merge anyway
541 VERBOSE=t
544 PRESERVE_MERGES=t
547 # yeah, we know
549 --onto)
550 shift
551 ONTO=$(git rev-parse --verify "$1") ||
552 die "Does not point to a valid commit: $1"
555 shift
556 run_pre_rebase_hook ${1+"$@"}
557 test $# -eq 1 -o $# -eq 2 || usage
558 test -d "$DOTEST" &&
559 die "Interactive rebase already started"
561 git var GIT_COMMITTER_IDENT >/dev/null ||
562 die "You need to set your committer info first"
564 comment_for_reflog start
566 require_clean_work_tree
568 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
569 test -z "$ONTO" && ONTO=$UPSTREAM
571 if test ! -z "$2"
572 then
573 output git show-ref --verify --quiet "refs/heads/$2" ||
574 die "Invalid branchname: $2"
575 output git checkout "$2" ||
576 die "Could not checkout $2"
579 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
580 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
582 : > "$DOTEST"/interactive || die "Could not mark as interactive"
583 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
584 echo "detached HEAD" > "$DOTEST"/head-name
586 echo $HEAD > "$DOTEST"/head
587 echo $UPSTREAM > "$DOTEST"/upstream
588 echo $ONTO > "$DOTEST"/onto
589 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
590 test t = "$VERBOSE" && : > "$DOTEST"/verbose
591 if test t = "$PRESERVE_MERGES"
592 then
593 # $REWRITTEN contains files for each commit that is
594 # reachable by at least one merge base of $HEAD and
595 # $UPSTREAM. They are not necessarily rewritten, but
596 # their children might be.
597 # This ensures that commits on merged, but otherwise
598 # unrelated side branches are left alone. (Think "X"
599 # in the man page's example.)
600 mkdir "$REWRITTEN" &&
601 for c in $(git merge-base --all $HEAD $UPSTREAM)
603 echo $ONTO > "$REWRITTEN"/$c ||
604 die "Could not init rewritten commits"
605 done
606 # No cherry-pick because our first pass is to determine
607 # parents to rewrite and skipping dropped commits would
608 # prematurely end our probe
609 MERGES_OPTION=
610 first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
611 else
612 MERGES_OPTION="--no-merges --cherry-pick"
615 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
616 SHORTHEAD=$(git rev-parse --short $HEAD)
617 SHORTONTO=$(git rev-parse --short $ONTO)
618 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
619 --abbrev=7 --reverse --left-right --topo-order \
620 $UPSTREAM...$HEAD | \
621 sed -n "s/^>//p" | while read shortsha1 rest
623 if test t != "$PRESERVE_MERGES"
624 then
625 echo "pick $shortsha1 $rest" >> "$TODO"
626 else
627 sha1=$(git rev-parse $shortsha1)
628 preserve=t
629 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
631 if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
632 then
633 preserve=f
635 done
636 if test f = "$preserve"
637 then
638 touch "$REWRITTEN"/$sha1
639 echo "pick $shortsha1 $rest" >> "$TODO"
642 done
644 # Watch for commits that been dropped by --cherry-pick
645 if test t = "$PRESERVE_MERGES"
646 then
647 mkdir "$DROPPED"
648 # Save all non-cherry-picked changes
649 git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
650 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
651 # Now all commits and note which ones are missing in
652 # not-cherry-picks and hence being dropped
653 git rev-list $UPSTREAM..$HEAD |
654 while read rev
656 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
657 then
658 # Use -f2 because if rev-list is telling us this commit is
659 # not worthwhile, we don't want to track its multiple heads,
660 # just the history of its first-parent for others that will
661 # be rebasing on top of it
662 git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
663 short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
664 grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
665 rm "$REWRITTEN"/$rev
667 done
669 test -s "$TODO" || echo noop >> "$TODO"
670 cat >> "$TODO" << EOF
672 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
674 # Commands:
675 # p, pick = use commit
676 # e, edit = use commit, but stop for amending
677 # s, squash = use commit, but meld into previous commit
679 # If you remove a line here THAT COMMIT WILL BE LOST.
680 # However, if you remove everything, the rebase will be aborted.
684 has_action "$TODO" ||
685 die_abort "Nothing to do"
687 cp "$TODO" "$TODO".backup
688 git_editor "$TODO" ||
689 die "Could not execute editor"
691 has_action "$TODO" ||
692 die_abort "Nothing to do"
694 git update-ref ORIG_HEAD $HEAD
695 output git checkout $ONTO && do_rest
697 esac
698 shift
699 done