Merge git://git.kernel.org/pub/scm/gitk/gitk
[git] / git-rebase--interactive.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2006 Johannes E. Schindelin
4
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
12
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 "
31
32 . git-sh-setup
33 require_work_tree
34
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=
47
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
52
53 warn () {
54         echo "$*" >&2
55 }
56
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 }
70
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 }
81
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 }
90
91 ORIG_REFLOG_ACTION="$GIT_REFLOG_ACTION"
92
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
100 }
101
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
115 }
116
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
125 }
126
127 die_with_patch () {
128         make_patch "$1"
129         git rerere
130         die "$2"
131 }
132
133 die_abort () {
134         rm -rf "$DOTEST"
135         die "$1"
136 }
137
138 has_action () {
139         grep '^[^#]' "$1" >/dev/null
140 }
141
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
159 }
160
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)
173
174         if test -f "$DOTEST"/current-commit
175         then
176                 current_commit=$(cat "$DOTEST"/current-commit) &&
177                 git rev-parse HEAD > "$REWRITTEN"/$current_commit &&
178                 rm "$DOTEST"/current-commit ||
179                 die "Cannot write current commit's replacement sha1"
180         fi
181
182         echo $sha1 > "$DOTEST"/current-commit
183
184         # rewrite parents; if none were rewritten, we can fast-forward.
185         new_parents=
186         pend=" $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)"
187         while [ "$pend" != "" ]
188         do
189                 p=$(expr "$pend" : ' \([^ ]*\)')
190                 pend="${pend# $p}"
191
192                 if test -f "$REWRITTEN"/$p
193                 then
194                         new_p=$(cat "$REWRITTEN"/$p)
195                         test $p != $new_p && fast_forward=f
196                         case "$new_parents" in
197                         *$new_p*)
198                                 ;; # do nothing; that parent is already there
199                         *)
200                                 new_parents="$new_parents $new_p"
201                                 ;;
202                         esac
203                 else
204                         if test -f "$DROPPED"/$p
205                         then
206                                 fast_forward=f
207                                 pend=" $(cat "$DROPPED"/$p)$pend"
208                         else
209                                 new_parents="$new_parents $p"
210                         fi
211                 fi
212         done
213         case $fast_forward in
214         t)
215                 output warn "Fast forward to $sha1"
216                 output git reset --hard $sha1 ||
217                         die "Cannot fast forward to $sha1"
218                 ;;
219         f)
220                 test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
221
222                 first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
223                 # detach HEAD to current parent
224                 output git checkout $first_parent 2> /dev/null ||
225                         die "Cannot move HEAD to $first_parent"
226
227                 case "$new_parents" in
228                 ' '*' '*)
229                         # redo merge
230                         author_script=$(get_author_ident_from_commit $sha1)
231                         eval "$author_script"
232                         msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
233                         # No point in merging the first parent, that's HEAD
234                         new_parents=${new_parents# $first_parent}
235                         if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
236                                 GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
237                                 GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
238                                 output git merge $STRATEGY -m "$msg" \
239                                         $new_parents
240                         then
241                                 git rerere
242                                 printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
243                                 die Error redoing merge $sha1
244                         fi
245                         ;;
246                 *)
247                         output git cherry-pick "$@" ||
248                                 die_with_patch $sha1 "Could not pick $sha1"
249                         ;;
250                 esac
251                 ;;
252         esac
253 }
254
255 nth_string () {
256         case "$1" in
257         *1[0-9]|*[04-9]) echo "$1"th;;
258         *1) echo "$1"st;;
259         *2) echo "$1"nd;;
260         *3) echo "$1"rd;;
261         esac
262 }
263
264 make_squash_message () {
265         if test -f "$SQUASH_MSG"; then
266                 COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
267                         < "$SQUASH_MSG" | sed -ne '$p')+1))
268                 echo "# This is a combination of $COUNT commits."
269                 sed -e 1d -e '2,/^./{
270                         /^$/d
271                 }' <"$SQUASH_MSG"
272         else
273                 COUNT=2
274                 echo "# This is a combination of two commits."
275                 echo "# The first commit's message is:"
276                 echo
277                 git cat-file commit HEAD | sed -e '1,/^$/d'
278         fi
279         echo
280         echo "# This is the $(nth_string $COUNT) commit message:"
281         echo
282         git cat-file commit $1 | sed -e '1,/^$/d'
283 }
284
285 peek_next_command () {
286         sed -n "1s/ .*$//p" < "$TODO"
287 }
288
289 do_next () {
290         rm -f "$DOTEST"/message "$DOTEST"/author-script \
291                 "$DOTEST"/amend || exit
292         read command sha1 rest < "$TODO"
293         case "$command" in
294         '#'*|''|noop)
295                 mark_action_done
296                 ;;
297         pick|p)
298                 comment_for_reflog pick
299
300                 mark_action_done
301                 pick_one $sha1 ||
302                         die_with_patch $sha1 "Could not apply $sha1... $rest"
303                 ;;
304         edit|e)
305                 comment_for_reflog edit
306
307                 mark_action_done
308                 pick_one $sha1 ||
309                         die_with_patch $sha1 "Could not apply $sha1... $rest"
310                 make_patch $sha1
311                 git rev-parse --verify HEAD > "$DOTEST"/amend
312                 warn "Stopped at $sha1... $rest"
313                 warn "You can amend the commit now, with"
314                 warn
315                 warn "  git commit --amend"
316                 warn
317                 warn "Once you are satisfied with your changes, run"
318                 warn
319                 warn "  git rebase --continue"
320                 warn
321                 exit 0
322                 ;;
323         squash|s)
324                 comment_for_reflog squash
325
326                 has_action "$DONE" ||
327                         die "Cannot 'squash' without a previous commit"
328
329                 mark_action_done
330                 make_squash_message $sha1 > "$MSG"
331                 failed=f
332                 author_script=$(get_author_ident_from_commit HEAD)
333                 output git reset --soft HEAD^
334                 pick_one -n $sha1 || failed=t
335                 case "$(peek_next_command)" in
336                 squash|s)
337                         EDIT_COMMIT=
338                         USE_OUTPUT=output
339                         MSG_OPT=-F
340                         MSG_FILE="$MSG"
341                         cp "$MSG" "$SQUASH_MSG"
342                         ;;
343                 *)
344                         EDIT_COMMIT=-e
345                         USE_OUTPUT=
346                         MSG_OPT=
347                         MSG_FILE=
348                         rm -f "$SQUASH_MSG" || exit
349                         cp "$MSG" "$GIT_DIR"/SQUASH_MSG
350                         rm -f "$GIT_DIR"/MERGE_MSG || exit
351                         ;;
352                 esac
353                 echo "$author_script" > "$DOTEST"/author-script
354                 if test $failed = f
355                 then
356                         # This is like --amend, but with a different message
357                         eval "$author_script"
358                         GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
359                         GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
360                         GIT_AUTHOR_DATE="$GIT_AUTHOR_DATE" \
361                         $USE_OUTPUT git commit --no-verify $MSG_OPT "$MSG_FILE" $EDIT_COMMIT || failed=t
362                 fi
363                 if test $failed = t
364                 then
365                         cp "$MSG" "$GIT_DIR"/MERGE_MSG
366                         warn
367                         warn "Could not apply $sha1... $rest"
368                         die_with_patch $sha1 ""
369                 fi
370                 ;;
371         *)
372                 warn "Unknown command: $command $sha1 $rest"
373                 die_with_patch $sha1 "Please fix this in the file $TODO."
374                 ;;
375         esac
376         test -s "$TODO" && return
377
378         comment_for_reflog finish &&
379         HEADNAME=$(cat "$DOTEST"/head-name) &&
380         OLDHEAD=$(cat "$DOTEST"/head) &&
381         SHORTONTO=$(git rev-parse --short $(cat "$DOTEST"/onto)) &&
382         if test -d "$REWRITTEN"
383         then
384                 test -f "$DOTEST"/current-commit &&
385                         current_commit=$(cat "$DOTEST"/current-commit) &&
386                         git rev-parse HEAD > "$REWRITTEN"/$current_commit
387                 if test -f "$REWRITTEN"/$OLDHEAD
388                 then
389                         NEWHEAD=$(cat "$REWRITTEN"/$OLDHEAD)
390                 else
391                         NEWHEAD=$OLDHEAD
392                 fi
393         else
394                 NEWHEAD=$(git rev-parse HEAD)
395         fi &&
396         case $HEADNAME in
397         refs/*)
398                 message="$GIT_REFLOG_ACTION: $HEADNAME onto $SHORTONTO)" &&
399                 git update-ref -m "$message" $HEADNAME $NEWHEAD $OLDHEAD &&
400                 git symbolic-ref HEAD $HEADNAME
401                 ;;
402         esac && {
403                 test ! -f "$DOTEST"/verbose ||
404                         git diff-tree --stat $(cat "$DOTEST"/head)..HEAD
405         } &&
406         rm -rf "$DOTEST" &&
407         git gc --auto &&
408         warn "Successfully rebased and updated $HEADNAME."
409
410         exit
411 }
412
413 do_rest () {
414         while :
415         do
416                 do_next
417         done
418 }
419
420 # check if no other options are set
421 is_standalone () {
422         test $# -eq 2 -a "$2" = '--' &&
423         test -z "$ONTO" &&
424         test -z "$PRESERVE_MERGES" &&
425         test -z "$STRATEGY" &&
426         test -z "$VERBOSE"
427 }
428
429 get_saved_options () {
430         test -d "$REWRITTEN" && PRESERVE_MERGES=t
431         test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
432         test -f "$DOTEST"/verbose && VERBOSE=t
433 }
434
435 while test $# != 0
436 do
437         case "$1" in
438         --no-verify)
439                 OK_TO_SKIP_PRE_REBASE=yes
440                 ;;
441         --verify)
442                 ;;
443         --continue)
444                 is_standalone "$@" || usage
445                 get_saved_options
446                 comment_for_reflog continue
447
448                 test -d "$DOTEST" || die "No interactive rebase running"
449
450                 # Sanity check
451                 git rev-parse --verify HEAD >/dev/null ||
452                         die "Cannot read HEAD"
453                 git update-index --ignore-submodules --refresh &&
454                         git diff-files --quiet --ignore-submodules ||
455                         die "Working tree is dirty"
456
457                 # do we have anything to commit?
458                 if git diff-index --cached --quiet --ignore-submodules HEAD --
459                 then
460                         : Nothing to commit -- skip this
461                 else
462                         . "$DOTEST"/author-script ||
463                                 die "Cannot find the author identity"
464                         amend=
465                         if test -f "$DOTEST"/amend
466                         then
467                                 amend=$(git rev-parse --verify HEAD)
468                                 test "$amend" = $(cat "$DOTEST"/amend) ||
469                                 die "\
470 You have uncommitted changes in your working tree. Please, commit them
471 first and then run 'git rebase --continue' again."
472                                 git reset --soft HEAD^ ||
473                                 die "Cannot rewind the HEAD"
474                         fi
475                         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE &&
476                         git commit --no-verify -F "$DOTEST"/message -e || {
477                                 test -n "$amend" && git reset --soft $amend
478                                 die "Could not commit staged changes."
479                         }
480                 fi
481
482                 require_clean_work_tree
483                 do_rest
484                 ;;
485         --abort)
486                 is_standalone "$@" || usage
487                 get_saved_options
488                 comment_for_reflog abort
489
490                 git rerere clear
491                 test -d "$DOTEST" || die "No interactive rebase running"
492
493                 HEADNAME=$(cat "$DOTEST"/head-name)
494                 HEAD=$(cat "$DOTEST"/head)
495                 case $HEADNAME in
496                 refs/*)
497                         git symbolic-ref HEAD $HEADNAME
498                         ;;
499                 esac &&
500                 output git reset --hard $HEAD &&
501                 rm -rf "$DOTEST"
502                 exit
503                 ;;
504         --skip)
505                 is_standalone "$@" || usage
506                 get_saved_options
507                 comment_for_reflog skip
508
509                 git rerere clear
510                 test -d "$DOTEST" || die "No interactive rebase running"
511
512                 output git reset --hard && do_rest
513                 ;;
514         -s)
515                 case "$#,$1" in
516                 *,*=*)
517                         STRATEGY="-s "$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
518                 1,*)
519                         usage ;;
520                 *)
521                         STRATEGY="-s $2"
522                         shift ;;
523                 esac
524                 ;;
525         -m)
526                 # we use merge anyway
527                 ;;
528         -v)
529                 VERBOSE=t
530                 ;;
531         -p)
532                 PRESERVE_MERGES=t
533                 ;;
534         -i)
535                 # yeah, we know
536                 ;;
537         --onto)
538                 shift
539                 ONTO=$(git rev-parse --verify "$1") ||
540                         die "Does not point to a valid commit: $1"
541                 ;;
542         --)
543                 shift
544                 run_pre_rebase_hook ${1+"$@"}
545                 test $# -eq 1 -o $# -eq 2 || usage
546                 test -d "$DOTEST" &&
547                         die "Interactive rebase already started"
548
549                 git var GIT_COMMITTER_IDENT >/dev/null ||
550                         die "You need to set your committer info first"
551
552                 comment_for_reflog start
553
554                 require_clean_work_tree
555
556                 UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
557                 test -z "$ONTO" && ONTO=$UPSTREAM
558
559                 if test ! -z "$2"
560                 then
561                         output git show-ref --verify --quiet "refs/heads/$2" ||
562                                 die "Invalid branchname: $2"
563                         output git checkout "$2" ||
564                                 die "Could not checkout $2"
565                 fi
566
567                 HEAD=$(git rev-parse --verify HEAD) || die "No HEAD?"
568                 mkdir "$DOTEST" || die "Could not create temporary $DOTEST"
569
570                 : > "$DOTEST"/interactive || die "Could not mark as interactive"
571                 git symbolic-ref HEAD > "$DOTEST"/head-name 2> /dev/null ||
572                         echo "detached HEAD" > "$DOTEST"/head-name
573
574                 echo $HEAD > "$DOTEST"/head
575                 echo $UPSTREAM > "$DOTEST"/upstream
576                 echo $ONTO > "$DOTEST"/onto
577                 test -z "$STRATEGY" || echo "$STRATEGY" > "$DOTEST"/strategy
578                 test t = "$VERBOSE" && : > "$DOTEST"/verbose
579                 if test t = "$PRESERVE_MERGES"
580                 then
581                         # $REWRITTEN contains files for each commit that is
582                         # reachable by at least one merge base of $HEAD and
583                         # $UPSTREAM. They are not necessarily rewritten, but
584                         # their children might be.
585                         # This ensures that commits on merged, but otherwise
586                         # unrelated side branches are left alone. (Think "X"
587                         # in the man page's example.)
588                         mkdir "$REWRITTEN" &&
589                         for c in $(git merge-base --all $HEAD $UPSTREAM)
590                         do
591                                 echo $ONTO > "$REWRITTEN"/$c ||
592                                         die "Could not init rewritten commits"
593                         done
594                         MERGES_OPTION=
595                 else
596                         MERGES_OPTION=--no-merges
597                 fi
598
599                 SHORTUPSTREAM=$(git rev-parse --short $UPSTREAM)
600                 SHORTHEAD=$(git rev-parse --short $HEAD)
601                 SHORTONTO=$(git rev-parse --short $ONTO)
602                 git rev-list $MERGES_OPTION --pretty=oneline --abbrev-commit \
603                         --abbrev=7 --reverse --left-right --cherry-pick \
604                         $UPSTREAM...$HEAD | \
605                         sed -n "s/^>/pick /p" > "$TODO"
606                 test -s "$TODO" || echo noop >> "$TODO"
607                 cat >> "$TODO" << EOF
608
609 # Rebase $SHORTUPSTREAM..$SHORTHEAD onto $SHORTONTO
610 #
611 # Commands:
612 #  p, pick = use commit
613 #  e, edit = use commit, but stop for amending
614 #  s, squash = use commit, but meld into previous commit
615 #
616 # If you remove a line here THAT COMMIT WILL BE LOST.
617 # However, if you remove everything, the rebase will be aborted.
618 #
619 EOF
620
621                 # Watch for commits that been dropped by --cherry-pick
622                 if test t = "$PRESERVE_MERGES"
623                 then
624                         mkdir "$DROPPED"
625                         # drop the --cherry-pick parameter this time
626                         git rev-list $MERGES_OPTION --abbrev-commit \
627                                 --abbrev=7 $UPSTREAM...$HEAD --left-right | \
628                                 sed -n "s/^>//p" | while read rev
629                         do
630                                 grep --quiet "$rev" "$TODO"
631                                 if [ $? -ne 0 ]
632                                 then
633                                         # Use -f2 because if rev-list is telling this commit is not
634                                         # worthwhile, we don't want to track its multiple heads,
635                                         # just the history of its first-parent for others that will
636                                         # be rebasing on top of us
637                                         full=$(git rev-parse $rev)
638                                         git rev-list --parents -1 $rev | cut -d' ' -f2 > "$DROPPED"/$full
639                                 fi
640                         done
641                 fi
642
643                 has_action "$TODO" ||
644                         die_abort "Nothing to do"
645
646                 cp "$TODO" "$TODO".backup
647                 git_editor "$TODO" ||
648                         die "Could not execute editor"
649
650                 has_action "$TODO" ||
651                         die_abort "Nothing to do"
652
653                 git update-ref ORIG_HEAD $HEAD
654                 output git checkout $ONTO && do_rest
655                 ;;
656         esac
657         shift
658 done