Code

rebase -i -p: Fix --continue after a merge could not be redone
[git.git] / git-rebase--interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Johannes E. Schindelin
5 # SHORT DESCRIPTION
6 #
7 # This script makes it easy to fix up commits in the middle of a series,
8 # and rearrange commits.
9 #
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)
17 --
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 "
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
55 }
57 output () {
58         case "$VERBOSE" in
59         '')
60                 output=$("$@" 2>&1 )
61                 status=$?
62                 test $status != 0 && printf "%s\n" "$output"
63                 return $status
64                 ;;
65         *)
66                 "$@"
67                 ;;
68         esac
69 }
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
78                 }
79         fi
80 }
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"
89 }
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
98                 ;;
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
114         fi
117 make_patch () {
118         parent_sha1=$(git rev-parse --verify "$1"^) ||
119                 die "Cannot get patch for $1^"
120         git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
121         test -f "$DOTEST"/message ||
122                 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
123         test -f "$DOTEST"/author-script ||
124                 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
127 die_with_patch () {
128         make_patch "$1"
129         git rerere
130         die "$2"
133 die_abort () {
134         rm -rf "$DOTEST"
135         die "$1"
138 has_action () {
139         grep '^[^#]' "$1" >/dev/null
142 pick_one () {
143         no_ff=
144         case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
145         output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
146         test -d "$REWRITTEN" &&
147                 pick_one_preserving_merges "$@" && return
148         parent_sha1=$(git rev-parse --verify $sha1^) ||
149                 die "Could not get the parent of $sha1"
150         current_sha1=$(git rev-parse --verify HEAD)
151         if test "$no_ff$current_sha1" = "$parent_sha1"; then
152                 output git reset --hard $sha1
153                 test "a$1" = a-n && output git reset --soft $current_sha1
154                 sha1=$(git rev-parse --short $sha1)
155                 output warn Fast forward to $sha1
156         else
157                 output git cherry-pick "$@"
158         fi
161 pick_one_preserving_merges () {
162         fast_forward=t
163         case "$1" in
164         -n)
165                 fast_forward=f
166                 sha1=$2
167                 ;;
168         *)
169                 sha1=$1
170                 ;;
171         esac
172         sha1=$(git rev-parse $sha1)
174         if test -f "$DOTEST"/current-commit
175         then
176                 if test "$fast_forward" = t
177                 then
178                         cat "$DOTEST"/current-commit | while read current_commit
179                         do
180                                 git rev-parse HEAD > "$REWRITTEN"/$current_commit
181                         done
182                         rm "$DOTEST"/current-commit ||
183                         die "Cannot write current commit's replacement sha1"
184                 fi
185         fi
187         echo $sha1 >> "$DOTEST"/current-commit
189         # rewrite parents; if none were rewritten, we can fast-forward.
190         new_parents=
191         pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
192         while [ "$pend" != "" ]
193         do
194                 p=$(expr "$pend" : ' \([^ ]*\)')
195                 pend="${pend# $p}"
197                 if test -f "$REWRITTEN"/$p
198                 then
199                         new_p=$(cat "$REWRITTEN"/$p)
201                         # If the todo reordered commits, and our parent is marked for
202                         # rewriting, but hasn't been gotten to yet, assume the user meant to
203                         # drop it on top of the current HEAD
204                         if test -z "$new_p"
205                         then
206                                 new_p=$(git rev-parse HEAD)
207                         fi
209                         test $p != $new_p && fast_forward=f
210                         case "$new_parents" in
211                         *$new_p*)
212                                 ;; # do nothing; that parent is already there
213                         *)
214                                 new_parents="$new_parents $new_p"
215                                 ;;
216                         esac
217                 else
218                         if test -f "$DROPPED"/$p
219                         then
220                                 fast_forward=f
221                                 pend=" $(cat "$DROPPED"/$p)$pend"
222                         else
223                                 new_parents="$new_parents $p"
224                         fi
225                 fi
226         done
227         case $fast_forward in
228         t)
229                 output warn "Fast forward to $sha1"
230                 output git reset --hard $sha1 ||
231                         die "Cannot fast forward to $sha1"
232                 ;;
233         f)
234                 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
236                 if [ "$1" != "-n" ]
237                 then
238                         # detach HEAD to current parent
239                         output git checkout $first_parent 2> /dev/null ||
240                                 die "Cannot move HEAD to $first_parent"
241                 fi
243                 case "$new_parents" in
244                 ' '*' '*)
245                         test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
247                         # redo merge
248                         author_script=$(get_author_ident_from_commit $sha1)
249                         eval "$author_script"
250                         msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
251                         # No point in merging the first parent, that's HEAD
252                         new_parents=${new_parents# $first_parent}
253                         if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
254                                 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
255                                 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
256                                 output git merge $STRATEGY -m "$msg" \
257                                         $new_parents
258                         then
259                                 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
260                                 die_with_patch $sha1 "Error redoing merge $sha1"
261                         fi
262                         ;;
263                 *)
264                         output git cherry-pick "$@" ||
265                                 die_with_patch $sha1 "Could not pick $sha1"
266                         ;;
267                 esac
268                 ;;
269         esac
272 nth_string () {
273         case "$1" in
274         *1[0-9]|*[04-9]) echo "$1"th;;
275         *1) echo "$1"st;;
276         *2) echo "$1"nd;;
277         *3) echo "$1"rd;;
278         esac
281 make_squash_message () {
282         if test -f "$SQUASH_MSG"; then
283                 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
284                         < "$SQUASH_MSG" | sed -ne '$p')+1))
285                 echo "# This is a combination of $COUNT commits."
286                 sed -e 1d -e '2,/^./{
287                         /^$/d
288                 }' <"$SQUASH_MSG"
289         else
290                 COUNT=2
291                 echo "# This is a combination of two commits."
292                 echo "# The first commit's message is:"
293                 echo
294                 git cat-file commit HEAD | sed -e '1,/^$/d'
295         fi
296         echo
297         echo "# This is the $(nth_string $COUNT) commit message:"
298         echo
299         git cat-file commit $1 | sed -e '1,/^$/d'
302 peek_next_command () {
303         sed -n "1s/ .*$//p" < "$TODO"
306 do_next () {
307         rm -f "$DOTEST"/message "$DOTEST"/author-script \
308                 "$DOTEST"/amend || exit
309         read command sha1 rest < "$TODO"
310         case "$command" in
311         '#'*|''|noop)
312                 mark_action_done
313                 ;;
314         pick|p)
315                 comment_for_reflog pick
317                 mark_action_done
318                 pick_one $sha1 ||
319                         die_with_patch $sha1 "Could not apply $sha1... $rest"
320                 ;;
321         edit|e)
322                 comment_for_reflog edit
324                 mark_action_done
325                 pick_one $sha1 ||
326                         die_with_patch $sha1 "Could not apply $sha1... $rest"
327                 make_patch $sha1
328                 git rev-parse --verify HEAD > "$DOTEST"/amend
329                 warn "Stopped at $sha1... $rest"
330                 warn "You can amend the commit now, with"
331                 warn
332                 warn "  git commit --amend"
333                 warn
334                 warn "Once you are satisfied with your changes, run"
335                 warn
336                 warn "  git rebase --continue"
337                 warn
338                 exit 0
339                 ;;
340         squash|s)
341                 comment_for_reflog squash
343                 has_action "$DONE" ||
344                         die "Cannot 'squash' without a previous commit"
346                 mark_action_done
347                 make_squash_message $sha1 > "$MSG"
348                 failed=f
349                 author_script=$(get_author_ident_from_commit HEAD)
350                 output git reset --soft HEAD^
351                 pick_one -n $sha1 || failed=t
352                 case "$(peek_next_command)" in
353                 squash|s)
354                         EDIT_COMMIT=
355                         USE_OUTPUT=output
356                         MSG_OPT=-F
357                         MSG_FILE="$MSG"
358                         cp "$MSG" "$SQUASH_MSG"
359                         ;;
360                 *)
361                         EDIT_COMMIT=-e
362                         USE_OUTPUT=
363                         MSG_OPT=
364                         MSG_FILE=
365                         rm -f "$SQUASH_MSG" || exit
366                         cp "$MSG" "$GIT_DIR"/SQUASH_MSG
367                         rm -f "$GIT_DIR"/MERGE_MSG || exit
368                         ;;
369                 esac
370                 echo "$author_script" > "$DOTEST"/author-script
371                 if test $failed = f
372                 then
373                         # This is like --amend, but with a different message
374                         eval "$author_script"
375                         GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
376                         GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
377                         GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
378                         $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
379                 fi
380                 if test $failed = t
381                 then
382                         cp "$MSG" "$GIT_DIR"/MERGE_MSG
383                         warn
384                         warn "Could not apply $sha1... $rest"
385                         die_with_patch $sha1 ""
386                 fi
387                 ;;
388         *)
389                 warn "Unknown command: $command $sha1 $rest"
390                 die_with_patch $sha1 "Please fix this in the file $TODO."
391                 ;;
392         esac
393         test -s "$TODO" && return
395         comment_for_reflog finish &&
396         HEADNAME=$(cat "$DOTEST"/head-name) &&
397         OLDHEAD=$(cat "$DOTEST"/head) &&
398         SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
399         NEWHEAD=$(git rev-parse HEAD) &&
400         case $HEADNAME in
401         refs/*)
402                 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
403                 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
404                 git symbolic-ref HEAD $HEADNAME
405                 ;;
406         esac && {
407                 test ! -f "$DOTEST"/verbose ||
408                         git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
409         } &&
410         rm -rf "$DOTEST" &&
411         git gc --auto &&
412         warn "Successfully rebased and updated $HEADNAME."
414         exit
417 do_rest () {
418         while :
419         do
420                 do_next
421         done
424 # check if no other options are set
425 is_standalone () {
426         test $# -eq 2 -a "$2" = '--' &&
427         test -z "$ONTO" &&
428         test -z "$PRESERVE_MERGES" &&
429         test -z "$STRATEGY" &&
430         test -z "$VERBOSE"
433 get_saved_options () {
434         test -d "$REWRITTEN" && PRESERVE_MERGES=t
435         test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
436         test -f "$DOTEST"/verbose && VERBOSE=t
439 while test $# != 0
440 do
441         case "$1" in
442         --no-verify)
443                 OK_TO_SKIP_PRE_REBASE=yes
444                 ;;
445         --verify)
446                 ;;
447         --continue)
448                 is_standalone "$@" || usage
449                 get_saved_options
450                 comment_for_reflog continue
452                 test -d "$DOTEST" || die "No interactive rebase running"
454                 # Sanity check
455                 git rev-parse --verify HEAD >/dev/null ||
456                         die "Cannot read HEAD"
457                 git update-index --ignore-submodules --refresh &&
458                         git diff-files --quiet --ignore-submodules ||
459                         die "Working tree is dirty"
461                 # do we have anything to commit?
462                 if git diff-index --cached --quiet --ignore-submodules HEAD --
463                 then
464                         : Nothing to commit -- skip this
465                 else
466                         . "$DOTEST"/author-script ||
467                                 die "Cannot find the author identity"
468                         amend=
469                         if test -f "$DOTEST"/amend
470                         then
471                                 amend=$(git rev-parse --verify HEAD)
472                                 test "$amend" = $(cat "$DOTEST"/amend) ||
473                                 die "\
474 You have uncommitted changes in your working tree. Please, commit them
475 first and then run 'git rebase --continue' again."
476                                 git reset --soft HEAD^ ||
477                                 die "Cannot rewind the HEAD"
478                         fi
479                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
480                         git commit --no-verify -F "$DOTEST"/message -e || {
481                                 test -n "$amend" && git reset --soft $amend
482                                 die "Could not commit staged changes."
483                         }
484                 fi
486                 require_clean_work_tree
487                 do_rest
488                 ;;
489         --abort)
490                 is_standalone "$@" || usage
491                 get_saved_options
492                 comment_for_reflog abort
494                 git rerere clear
495                 test -d "$DOTEST" || die "No interactive rebase running"
497                 HEADNAME=$(cat "$DOTEST"/head-name)
498                 HEAD=$(cat "$DOTEST"/head)
499                 case $HEADNAME in
500                 refs/*)
501                         git symbolic-ref HEAD $HEADNAME
502                         ;;
503                 esac &&
504                 output git reset --hard $HEAD &&
505                 rm -rf "$DOTEST"
506                 exit
507                 ;;
508         --skip)
509                 is_standalone "$@" || usage
510                 get_saved_options
511                 comment_for_reflog skip
513                 git rerere clear
514                 test -d "$DOTEST" || die "No interactive rebase running"
516                 output git reset --hard && do_rest
517                 ;;
518         -s)
519                 case "$#,$1" in
520                 *,*=*)
521                         STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
522                 1,*)
523                         usage ;;
524                 *)
525                         STRATEGY="-s $2"
526                         shift ;;
527                 esac
528                 ;;
529         -m)
530                 # we use merge anyway
531                 ;;
532         -v)
533                 VERBOSE=t
534                 ;;
535         -p)
536                 PRESERVE_MERGES=t
537                 ;;
538         -i)
539                 # yeah, we know
540                 ;;
541         --onto)
542                 shift
543                 ONTO=$(git rev-parse --verify "$1") ||
544                         die "Does not point to a valid commit: $1"
545                 ;;
546         --)
547                 shift
548                 run_pre_rebase_hook ${1+"$@"}
549                 test $# -eq 1 -o $# -eq 2 || usage
550                 test -d "$DOTEST" &&
551                         die "Interactive rebase already started"
553                 git var GIT_COMMITTER_IDENT >/dev/null ||
554                         die "You need to set your committer info first"
556                 comment_for_reflog start
558                 require_clean_work_tree
560                 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
561                 test -z "$ONTO" && ONTO=$UPSTREAM
563                 if test ! -z "$2"
564                 then
565                         output git show-ref --verify --quiet "refs/heads/$2" ||
566                                 die "Invalid branchname: $2"
567                         output git checkout "$2" ||
568                                 die "Could not checkout $2"
569                 fi
571                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
572                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
574                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
575                 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
576                         echo "detached HEAD" > "$DOTEST"/head-name
578                 echo $HEAD > "$DOTEST"/head
579                 echo $UPSTREAM > "$DOTEST"/upstream
580                 echo $ONTO > "$DOTEST"/onto
581                 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
582                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
583                 if test t = "$PRESERVE_MERGES"
584                 then
585                         # $REWRITTEN contains files for each commit that is
586                         # reachable by at least one merge base of $HEAD and
587                         # $UPSTREAM. They are not necessarily rewritten, but
588                         # their children might be.
589                         # This ensures that commits on merged, but otherwise
590                         # unrelated side branches are left alone. (Think "X"
591                         # in the man page's example.)
592                         mkdir "$REWRITTEN" &&
593                         for c in $(git merge-base --all $HEAD $UPSTREAM)
594                         do
595                                 echo $ONTO > "$REWRITTEN"/$c ||
596                                         die "Could not init rewritten commits"
597                         done
598                         # No cherry-pick because our first pass is to determine
599                         # parents to rewrite and skipping dropped commits would
600                         # prematurely end our probe
601                         MERGES_OPTION=
602                         first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
603                 else
604                         MERGES_OPTION="--no-merges --cherry-pick"
605                 fi
607                 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
608                 SHORTHEAD=$(git rev-parse --short $HEAD)
609                 SHORTONTO=$(git rev-parse --short $ONTO)
610                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
611                         --abbrev=7 --reverse --left-right --topo-order \
612                         $UPSTREAM...$HEAD | \
613                         sed -n "s/^>//p" | while read shortsha1 rest
614                 do
615                         if test t != "$PRESERVE_MERGES"
616                         then
617                                 echo "pick $shortsha1 $rest" >> "$TODO"
618                         else
619                                 sha1=$(git rev-parse $shortsha1)
620                                 preserve=t
621                                 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
622                                 do
623                                         if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
624                                         then
625                                                 preserve=f
626                                         fi
627                                 done
628                                 if test f = "$preserve"
629                                 then
630                                         touch "$REWRITTEN"/$sha1
631                                         echo "pick $shortsha1 $rest" >> "$TODO"
632                                 fi
633                         fi
634                 done
636                 # Watch for commits that been dropped by --cherry-pick
637                 if test t = "$PRESERVE_MERGES"
638                 then
639                         mkdir "$DROPPED"
640                         # Save all non-cherry-picked changes
641                         git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
642                                 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
643                         # Now all commits and note which ones are missing in
644                         # not-cherry-picks and hence being dropped
645                         git rev-list $UPSTREAM..$HEAD |
646                         while read rev
647                         do
648                                 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
649                                 then
650                                         # Use -f2 because if rev-list is telling us this commit is
651                                         # not worthwhile, we don't want to track its multiple heads,
652                                         # just the history of its first-parent for others that will
653                                         # be rebasing on top of it
654                                         git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
655                                         short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
656                                         grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
657                                         rm "$REWRITTEN"/$rev
658                                 fi
659                         done
660                 fi
661                 test -s "$TODO" || echo noop >> "$TODO"
662                 cat >> "$TODO" << EOF
664 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
666 # Commands:
667 #  p, pick = use commit
668 #  e, edit = use commit, but stop for amending
669 #  s, squash = use commit, but meld into previous commit
671 # If you remove a line here THAT COMMIT WILL BE LOST.
672 # However, if you remove everything, the rebase will be aborted.
674 EOF
676                 has_action "$TODO" ||
677                         die_abort "Nothing to do"
679                 cp "$TODO" "$TODO".backup
680                 git_editor "$TODO" ||
681                         die "Could not execute editor"
683                 has_action "$TODO" ||
684                         die_abort "Nothing to do"
686                 git update-ref ORIG_HEAD $HEAD
687                 output git checkout $ONTO && do_rest
688                 ;;
689         esac
690         shift
691 done