Code

rebase-i-p: minimum fix to obvious issues
[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 "
31 . git-sh-setup
32 require_work_tree
34 DOTEST="$GIT_DIR/rebase-merge"
35 TODO="$DOTEST"/git-rebase-todo
36 DONE="$DOTEST"/done
37 MSG="$DOTEST"/message
38 SQUASH_MSG="$DOTEST"/message-squash
39 REWRITTEN="$DOTEST"/rewritten
40 DROPPED="$DOTEST"/dropped
41 PRESERVE_MERGES=
42 STRATEGY=
43 ONTO=
44 VERBOSE=
46 GIT_CHERRY_PICK_HELP="  After resolving the conflicts,
47 mark the corrected paths with 'git add <paths>', and
48 run 'git rebase --continue'"
49 export GIT_CHERRY_PICK_HELP
51 warn () {
52         echo "$*" >&2
53 }
55 output () {
56         case "$VERBOSE" in
57         '')
58                 output=$("$@" 2>&1 )
59                 status=$?
60                 test $status != 0 && printf "%s\n" "$output"
61                 return $status
62                 ;;
63         *)
64                 "$@"
65                 ;;
66         esac
67 }
69 run_pre_rebase_hook () {
70         if test -x "$GIT_DIR/hooks/pre-rebase"
71         then
72                 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || {
73                         echo >&2 "The pre-rebase hook refused to rebase."
74                         exit 1
75                 }
76         fi
77 }
79 require_clean_work_tree () {
80         # test if working tree is dirty
81         git rev-parse --verify HEAD > /dev/null &&
82         git update-index --ignore-submodules --refresh &&
83         git diff-files --quiet --ignore-submodules &&
84         git diff-index --cached --quiet HEAD --ignore-submodules -- ||
85         die "Working tree is dirty"
86 }
88 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
90 comment_for_reflog () {
91         case "$ORIG_REFLOG_ACTION" in
92         ''|rebase*)
93                 GIT_REFLOG_ACTION="rebase -i ($1)"
94                 export GIT_REFLOG_ACTION
95                 ;;
96         esac
97 }
99 last_count=
100 mark_action_done () {
101         sed -e 1q < "$TODO" >> "$DONE"
102         sed -e 1d < "$TODO" >> "$TODO".new
103         mv -f "$TODO".new "$TODO"
104         count=$(grep -c '^[^#]' < "$DONE")
105         total=$(($count+$(grep -c '^[^#]' < "$TODO")))
106         if test "$last_count" != "$count"
107         then
108                 last_count=$count
109                 printf "Rebasing (%d/%d)\r" $count $total
110                 test -z "$VERBOSE" || echo
111         fi
114 make_patch () {
115         parent_sha1=$(git rev-parse --verify "$1"^) ||
116                 die "Cannot get patch for $1^"
117         git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
118         test -f "$DOTEST"/message ||
119                 git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
120         test -f "$DOTEST"/author-script ||
121                 get_author_ident_from_commit "$1" > "$DOTEST"/author-script
124 die_with_patch () {
125         make_patch "$1"
126         git rerere
127         die "$2"
130 die_abort () {
131         rm -rf "$DOTEST"
132         die "$1"
135 has_action () {
136         grep '^[^#]' "$1" >/dev/null
139 pick_one () {
140         no_ff=
141         case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
142         output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
143         test -d "$REWRITTEN" &&
144                 pick_one_preserving_merges "$@" && return
145         parent_sha1=$(git rev-parse --verify $sha1^) ||
146                 die "Could not get the parent of $sha1"
147         current_sha1=$(git rev-parse --verify HEAD)
148         if test "$no_ff$current_sha1" = "$parent_sha1"; then
149                 output git reset --hard $sha1
150                 test "a$1" = a-n && output git reset --soft $current_sha1
151                 sha1=$(git rev-parse --short $sha1)
152                 output warn Fast forward to $sha1
153         else
154                 output git cherry-pick "$@"
155         fi
158 pick_one_preserving_merges () {
159         fast_forward=t
160         case "$1" in
161         -n)
162                 fast_forward=f
163                 sha1=$2
164                 ;;
165         *)
166                 sha1=$1
167                 ;;
168         esac
169         sha1=$(git rev-parse $sha1)
171         if test -f "$DOTEST"/current-commit
172         then
173                 if [ "$fast_forward" == "t" ]
174                 then
175                         cat "$DOTEST"/current-commit | while read current_commit
176                         do
177                                 git rev-parse HEAD > "$REWRITTEN"/$current_commit
178                         done
179                         rm "$DOTEST"/current-commit ||
180                         die "Cannot write current commit's replacement sha1"
181                 fi
182         fi
184         echo $sha1 >> "$DOTEST"/current-commit
186         # rewrite parents; if none were rewritten, we can fast-forward.
187         new_parents=
188         pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
189         while [ "$pend" != "" ]
190         do
191                 p=$(expr "$pend" : ' \([^ ]*\)')
192                 pend="${pend# $p}"
194                 if test -f "$REWRITTEN"/$p
195                 then
196                         new_p=$(cat "$REWRITTEN"/$p)
198                         # If the todo reordered commits, and our parent is marked for
199                         # rewriting, but hasn't been gotten to yet, assume the user meant to
200                         # drop it on top of the current HEAD
201                         if test -z "$new_p"
202                         then
203                                 new_p=$(git rev-parse HEAD)
204                         fi
206                         test $p != $new_p && fast_forward=f
207                         case "$new_parents" in
208                         *$new_p*)
209                                 ;; # do nothing; that parent is already there
210                         *)
211                                 new_parents="$new_parents $new_p"
212                                 ;;
213                         esac
214                 else
215                         if test -f "$DROPPED"/$p
216                         then
217                                 fast_forward=f
218                                 pend=" $(cat "$DROPPED"/$p)$pend"
219                         else
220                                 new_parents="$new_parents $p"
221                         fi
222                 fi
223         done
224         case $fast_forward in
225         t)
226                 output warn "Fast forward to $sha1"
227                 output git reset --hard $sha1 ||
228                         die "Cannot fast forward to $sha1"
229                 ;;
230         f)
231                 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
233                 if [ "$1" != "-n" ]
234                 then
235                         # detach HEAD to current parent
236                         output git checkout $first_parent 2> /dev/null ||
237                                 die "Cannot move HEAD to $first_parent"
238                 fi
240                 case "$new_parents" in
241                 ' '*' '*)
242                         test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
244                         # redo merge
245                         author_script=$(get_author_ident_from_commit $sha1)
246                         eval "$author_script"
247                         msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
248                         # No point in merging the first parent, that's HEAD
249                         new_parents=${new_parents# $first_parent}
250                         if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
251                                 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
252                                 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
253                                 output git merge $STRATEGY -m "$msg" \
254                                         $new_parents
255                         then
256                                 git rerere
257                                 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
258                                 die Error redoing merge $sha1
259                         fi
260                         ;;
261                 *)
262                         output git cherry-pick "$@" ||
263                                 die_with_patch $sha1 "Could not pick $sha1"
264                         ;;
265                 esac
266                 ;;
267         esac
270 nth_string () {
271         case "$1" in
272         *1[0-9]|*[04-9]) echo "$1"th;;
273         *1) echo "$1"st;;
274         *2) echo "$1"nd;;
275         *3) echo "$1"rd;;
276         esac
279 make_squash_message () {
280         if test -f "$SQUASH_MSG"; then
281                 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
282                         < "$SQUASH_MSG" | sed -ne '$p')+1))
283                 echo "# This is a combination of $COUNT commits."
284                 sed -e 1d -e '2,/^./{
285                         /^$/d
286                 }' <"$SQUASH_MSG"
287         else
288                 COUNT=2
289                 echo "# This is a combination of two commits."
290                 echo "# The first commit's message is:"
291                 echo
292                 git cat-file commit HEAD | sed -e '1,/^$/d'
293         fi
294         echo
295         echo "# This is the $(nth_string $COUNT) commit message:"
296         echo
297         git cat-file commit $1 | sed -e '1,/^$/d'
300 peek_next_command () {
301         sed -n "1s/ .*$//p" < "$TODO"
304 do_next () {
305         rm -f "$DOTEST"/message "$DOTEST"/author-script \
306                 "$DOTEST"/amend || exit
307         read command sha1 rest < "$TODO"
308         case "$command" in
309         '#'*|''|noop)
310                 mark_action_done
311                 ;;
312         pick|p)
313                 comment_for_reflog pick
315                 mark_action_done
316                 pick_one $sha1 ||
317                         die_with_patch $sha1 "Could not apply $sha1... $rest"
318                 ;;
319         edit|e)
320                 comment_for_reflog edit
322                 mark_action_done
323                 pick_one $sha1 ||
324                         die_with_patch $sha1 "Could not apply $sha1... $rest"
325                 make_patch $sha1
326                 git rev-parse --verify HEAD > "$DOTEST"/amend
327                 warn "Stopped at $sha1... $rest"
328                 warn "You can amend the commit now, with"
329                 warn
330                 warn "  git commit --amend"
331                 warn
332                 warn "Once you are satisfied with your changes, run"
333                 warn
334                 warn "  git rebase --continue"
335                 warn
336                 exit 0
337                 ;;
338         squash|s)
339                 comment_for_reflog squash
341                 has_action "$DONE" ||
342                         die "Cannot 'squash' without a previous commit"
344                 mark_action_done
345                 make_squash_message $sha1 > "$MSG"
346                 failed=f
347                 author_script=$(get_author_ident_from_commit HEAD)
348                 output git reset --soft HEAD^
349                 pick_one -n $sha1 || failed=t
350                 case "$(peek_next_command)" in
351                 squash|s)
352                         EDIT_COMMIT=
353                         USE_OUTPUT=output
354                         MSG_OPT=-F
355                         MSG_FILE="$MSG"
356                         cp "$MSG" "$SQUASH_MSG"
357                         ;;
358                 *)
359                         EDIT_COMMIT=-e
360                         USE_OUTPUT=
361                         MSG_OPT=
362                         MSG_FILE=
363                         rm -f "$SQUASH_MSG" || exit
364                         cp "$MSG" "$GIT_DIR"/SQUASH_MSG
365                         rm -f "$GIT_DIR"/MERGE_MSG || exit
366                         ;;
367                 esac
368                 echo "$author_script" > "$DOTEST"/author-script
369                 if test $failed = f
370                 then
371                         # This is like --amend, but with a different message
372                         eval "$author_script"
373                         GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
374                         GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
375                         GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
376                         $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
377                 fi
378                 if test $failed = t
379                 then
380                         cp "$MSG" "$GIT_DIR"/MERGE_MSG
381                         warn
382                         warn "Could not apply $sha1... $rest"
383                         die_with_patch $sha1 ""
384                 fi
385                 ;;
386         *)
387                 warn "Unknown command: $command $sha1 $rest"
388                 die_with_patch $sha1 "Please fix this in the file $TODO."
389                 ;;
390         esac
391         test -s "$TODO" && return
393         comment_for_reflog finish &&
394         HEADNAME=$(cat "$DOTEST"/head-name) &&
395         OLDHEAD=$(cat "$DOTEST"/head) &&
396         SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
397         NEWHEAD=$(git rev-parse HEAD) &&
398         case $HEADNAME in
399         refs/*)
400                 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
401                 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
402                 git symbolic-ref HEAD $HEADNAME
403                 ;;
404         esac && {
405                 test ! -f "$DOTEST"/verbose ||
406                         git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
407         } &&
408         rm -rf "$DOTEST" &&
409         git gc --auto &&
410         warn "Successfully rebased and updated $HEADNAME."
412         exit
415 do_rest () {
416         while :
417         do
418                 do_next
419         done
422 # check if no other options are set
423 is_standalone () {
424         test $# -eq 2 -a "$2" = '--' &&
425         test -z "$ONTO" &&
426         test -z "$PRESERVE_MERGES" &&
427         test -z "$STRATEGY" &&
428         test -z "$VERBOSE"
431 get_saved_options () {
432         test -d "$REWRITTEN" && PRESERVE_MERGES=t
433         test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
434         test -f "$DOTEST"/verbose && VERBOSE=t
437 while test $# != 0
438 do
439         case "$1" in
440         --continue)
441                 is_standalone "$@" || usage
442                 get_saved_options
443                 comment_for_reflog continue
445                 test -d "$DOTEST" || die "No interactive rebase running"
447                 # Sanity check
448                 git rev-parse --verify HEAD >/dev/null ||
449                         die "Cannot read HEAD"
450                 git update-index --ignore-submodules --refresh &&
451                         git diff-files --quiet --ignore-submodules ||
452                         die "Working tree is dirty"
454                 # do we have anything to commit?
455                 if git diff-index --cached --quiet --ignore-submodules HEAD --
456                 then
457                         : Nothing to commit -- skip this
458                 else
459                         . "$DOTEST"/author-script ||
460                                 die "Cannot find the author identity"
461                         amend=
462                         if test -f "$DOTEST"/amend
463                         then
464                                 amend=$(git rev-parse --verify HEAD)
465                                 test "$amend" = $(cat "$DOTEST"/amend) ||
466                                 die "\
467 You have uncommitted changes in your working tree. Please, commit them
468 first and then run 'git rebase --continue' again."
469                                 git reset --soft HEAD^ ||
470                                 die "Cannot rewind the HEAD"
471                         fi
472                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
473                         git commit --no-verify -F "$DOTEST"/message -e || {
474                                 test -n "$amend" && git reset --soft $amend
475                                 die "Could not commit staged changes."
476                         }
477                 fi
479                 require_clean_work_tree
480                 do_rest
481                 ;;
482         --abort)
483                 is_standalone "$@" || usage
484                 get_saved_options
485                 comment_for_reflog abort
487                 git rerere clear
488                 test -d "$DOTEST" || die "No interactive rebase running"
490                 HEADNAME=$(cat "$DOTEST"/head-name)
491                 HEAD=$(cat "$DOTEST"/head)
492                 case $HEADNAME in
493                 refs/*)
494                         git symbolic-ref HEAD $HEADNAME
495                         ;;
496                 esac &&
497                 output git reset --hard $HEAD &&
498                 rm -rf "$DOTEST"
499                 exit
500                 ;;
501         --skip)
502                 is_standalone "$@" || usage
503                 get_saved_options
504                 comment_for_reflog skip
506                 git rerere clear
507                 test -d "$DOTEST" || die "No interactive rebase running"
509                 output git reset --hard && do_rest
510                 ;;
511         -s)
512                 case "$#,$1" in
513                 *,*=*)
514                         STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
515                 1,*)
516                         usage ;;
517                 *)
518                         STRATEGY="-s $2"
519                         shift ;;
520                 esac
521                 ;;
522         -m)
523                 # we use merge anyway
524                 ;;
525         -v)
526                 VERBOSE=t
527                 ;;
528         -p)
529                 PRESERVE_MERGES=t
530                 ;;
531         -i)
532                 # yeah, we know
533                 ;;
534         --onto)
535                 shift
536                 ONTO=$(git rev-parse --verify "$1") ||
537                         die "Does not point to a valid commit: $1"
538                 ;;
539         --)
540                 shift
541                 run_pre_rebase_hook ${1+"$@"}
542                 test $# -eq 1 -o $# -eq 2 || usage
543                 test -d "$DOTEST" &&
544                         die "Interactive rebase already started"
546                 git var GIT_COMMITTER_IDENT >/dev/null ||
547                         die "You need to set your committer info first"
549                 comment_for_reflog start
551                 require_clean_work_tree
553                 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
554                 test -z "$ONTO" && ONTO=$UPSTREAM
556                 if test ! -z "$2"
557                 then
558                         output git show-ref --verify --quiet "refs/heads/$2" ||
559                                 die "Invalid branchname: $2"
560                         output git checkout "$2" ||
561                                 die "Could not checkout $2"
562                 fi
564                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
565                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
567                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
568                 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
569                         echo "detached HEAD" > "$DOTEST"/head-name
571                 echo $HEAD > "$DOTEST"/head
572                 echo $UPSTREAM > "$DOTEST"/upstream
573                 echo $ONTO > "$DOTEST"/onto
574                 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
575                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
576                 if test t = "$PRESERVE_MERGES"
577                 then
578                         # $REWRITTEN contains files for each commit that is
579                         # reachable by at least one merge base of $HEAD and
580                         # $UPSTREAM. They are not necessarily rewritten, but
581                         # their children might be.
582                         # This ensures that commits on merged, but otherwise
583                         # unrelated side branches are left alone. (Think "X"
584                         # in the man page's example.)
585                         mkdir "$REWRITTEN" &&
586                         for c in $(git merge-base --all $HEAD $UPSTREAM)
587                         do
588                                 echo $ONTO > "$REWRITTEN"/$c ||
589                                         die "Could not init rewritten commits"
590                         done
591                         # No cherry-pick because our first pass is to determine
592                         # parents to rewrite and skipping dropped commits would
593                         # prematurely end our probe
594                         MERGES_OPTION=
595                         first_after_upstream="$(git rev-list --reverse --first-parent $UPSTREAM..$HEAD | head -n 1)"
596                 else
597                         MERGES_OPTION="--no-merges --cherry-pick"
598                 fi
600                 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
601                 SHORTHEAD=$(git rev-parse --short $HEAD)
602                 SHORTONTO=$(git rev-parse --short $ONTO)
603                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
604                         --abbrev=7 --reverse --left-right --topo-order \
605                         $UPSTREAM...$HEAD | \
606                         sed -n "s/^>//p" | while read shortsha1 rest
607                 do
608                         if test t != "$PRESERVE_MERGES"
609                         then
610                                 echo "pick $shortsha1 $rest" >> "$TODO"
611                         else
612                                 sha1=$(git rev-parse $shortsha1)
613                                 preserve=t
614                                 for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
615                                 do
616                                         if test -f "$REWRITTEN"/$p -a \( $p != $UPSTREAM -o $sha1 = $first_after_upstream \)
617                                         then
618                                                 preserve=f
619                                         fi
620                                 done
621                                 if test f = "$preserve"
622                                 then
623                                         touch "$REWRITTEN"/$sha1
624                                         echo "pick $shortsha1 $rest" >> "$TODO"
625                                 fi
626                         fi
627                 done
629                 # Watch for commits that been dropped by --cherry-pick
630                 if test t = "$PRESERVE_MERGES"
631                 then
632                         mkdir "$DROPPED"
633                         # Save all non-cherry-picked changes
634                         git rev-list $UPSTREAM...$HEAD --left-right --cherry-pick | \
635                                 sed -n "s/^>//p" > "$DOTEST"/not-cherry-picks
636                         # Now all commits and note which ones are missing in
637                         # not-cherry-picks and hence being dropped
638                         git rev-list $UPSTREAM..$HEAD |
639                         while read rev
640                         do
641                                 if test -f "$REWRITTEN"/$rev -a "$(grep "$rev" "$DOTEST"/not-cherry-picks)" = ""
642                                 then
643                                         # Use -f2 because if rev-list is telling us this commit is
644                                         # not worthwhile, we don't want to track its multiple heads,
645                                         # just the history of its first-parent for others that will
646                                         # be rebasing on top of it
647                                         git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$rev
648                                         short=$(git rev-list -1 --abbrev-commit --abbrev=7 $rev)
649                                         grep -v "^[a-z][a-z]* $short" <"$TODO" > "${TODO}2" ; mv "${TODO}2" "$TODO"
650                                         rm "$REWRITTEN"/$rev
651                                 fi
652                         done
653                 fi
654                 test -s "$TODO" || echo noop >> "$TODO"
655                 cat >> "$TODO" << EOF
657 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
659 # Commands:
660 #  p, pick = use commit
661 #  e, edit = use commit, but stop for amending
662 #  s, squash = use commit, but meld into previous commit
664 # If you remove a line here THAT COMMIT WILL BE LOST.
665 # However, if you remove everything, the rebase will be aborted.
667 EOF
669                 has_action "$TODO" ||
670                         die_abort "Nothing to do"
672                 cp "$TODO" "$TODO".backup
673                 git_editor "$TODO" ||
674                         die "Could not execute editor"
676                 has_action "$TODO" ||
677                         die_abort "Nothing to do"
679                 git update-ref ORIG_HEAD $HEAD
680                 output git checkout $ONTO && do_rest
681                 ;;
682         esac
683         shift
684 done