rebase -i: support --stat
[git] / git-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
6 USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] (<upstream>|--root) [<branch>] [--quiet | -q]'
7 LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8 same name.  When the --onto option is provided the new branch starts
9 out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10 It then attempts to create a new commit for each commit from the original
11 <branch> that does not exist in the <upstream> branch.
12
13 It is possible that a merge failure will prevent this process from being
14 completely automatic.  You will have to resolve any such merge failure
15 and run git rebase --continue.  Another option is to bypass the commit
16 that caused the merge failure with git rebase --skip.  To restore the
17 original <branch> and remove the .git/rebase-apply working files, use the
18 command git rebase --abort instead.
19
20 Note that if <branch> is not specified on the command line, the
21 currently checked out branch is used.
22
23 Example:       git-rebase master~1 topic
24
25         A---B---C topic                   A'\''--B'\''--C'\'' topic
26        /                   -->           /
27   D---E---F---G master          D---E---F---G master
28 '
29
30 SUBDIRECTORY_OK=Yes
31 OPTIONS_SPEC=
32 . git-sh-setup
33 set_reflog_action rebase
34 require_work_tree
35 cd_to_toplevel
36
37 LF='
38 '
39 ok_to_skip_pre_rebase=
40 resolvemsg="
41 When you have resolved this problem run \"git rebase --continue\".
42 If you would prefer to skip this patch, instead run \"git rebase --skip\".
43 To restore the original branch and stop rebasing run \"git rebase --abort\".
44 "
45 unset onto
46 strategy=
47 strategy_opts=
48 do_merge=
49 merge_dir="$GIT_DIR"/rebase-merge
50 apply_dir="$GIT_DIR"/rebase-apply
51 prec=4
52 verbose=
53 diffstat=
54 test "$(git config --bool rebase.stat)" = true && diffstat=t
55 git_am_opt=
56 rebase_root=
57 force_rebase=
58 allow_rerere_autoupdate=
59 # Non-empty if a rebase was in progress when 'git rebase' was invoked
60 in_progress=
61 # One of {am, merge, interactive}
62 type=
63 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
64 state_dir=
65 # One of {'', continue, skip, abort}, as parsed from command line
66 action=
67 preserve_merges=
68 autosquash=
69 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
70
71 read_state () {
72         if test "$type" = merge
73         then
74                 onto_name=$(cat "$state_dir"/onto_name) &&
75                 end=$(cat "$state_dir"/end) &&
76                 msgnum=$(cat "$state_dir"/msgnum)
77         fi &&
78         head_name=$(cat "$state_dir"/head-name) &&
79         onto=$(cat "$state_dir"/onto) &&
80         orig_head=$(cat "$state_dir"/orig-head) &&
81         GIT_QUIET=$(cat "$state_dir"/quiet)
82 }
83
84 continue_merge () {
85         test -d "$merge_dir" || die "$merge_dir directory does not exist"
86
87         unmerged=$(git ls-files -u)
88         if test -n "$unmerged"
89         then
90                 echo "You still have unmerged paths in your index"
91                 echo "did you forget to use git add?"
92                 die "$resolvemsg"
93         fi
94
95         cmt=`cat "$merge_dir/current"`
96         if ! git diff-index --quiet --ignore-submodules HEAD --
97         then
98                 if ! git commit --no-verify -C "$cmt"
99                 then
100                         echo "Commit failed, please do not call \"git commit\""
101                         echo "directly, but instead do one of the following: "
102                         die "$resolvemsg"
103                 fi
104                 if test -z "$GIT_QUIET"
105                 then
106                         printf "Committed: %0${prec}d " $msgnum
107                 fi
108                 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
109         else
110                 if test -z "$GIT_QUIET"
111                 then
112                         printf "Already applied: %0${prec}d " $msgnum
113                 fi
114         fi
115         test -z "$GIT_QUIET" &&
116         GIT_PAGER='' git log --format=%s -1 "$cmt"
117
118         # onto the next patch:
119         msgnum=$(($msgnum + 1))
120         echo "$msgnum" >"$merge_dir/msgnum"
121 }
122
123 call_merge () {
124         cmt="$(cat "$merge_dir/cmt.$1")"
125         echo "$cmt" > "$merge_dir/current"
126         hd=$(git rev-parse --verify HEAD)
127         cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
128         msgnum=$(cat "$merge_dir/msgnum")
129         eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
130         eval GITHEAD_$hd='$onto_name'
131         export GITHEAD_$cmt GITHEAD_$hd
132         if test -n "$GIT_QUIET"
133         then
134                 GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
135         fi
136         test -z "$strategy" && strategy=recursive
137         eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
138         rv=$?
139         case "$rv" in
140         0)
141                 unset GITHEAD_$cmt GITHEAD_$hd
142                 return
143                 ;;
144         1)
145                 git rerere $allow_rerere_autoupdate
146                 die "$resolvemsg"
147                 ;;
148         2)
149                 echo "Strategy: $rv $strategy failed, try another" 1>&2
150                 die "$resolvemsg"
151                 ;;
152         *)
153                 die "Unknown exit code ($rv) from command:" \
154                         "git-merge-$strategy $cmt^ -- HEAD $cmt"
155                 ;;
156         esac
157 }
158
159 move_to_original_branch () {
160         case "$head_name" in
161         refs/*)
162                 message="rebase finished: $head_name onto $onto"
163                 git update-ref -m "$message" \
164                         $head_name $(git rev-parse HEAD) $orig_head &&
165                 git symbolic-ref HEAD $head_name ||
166                 die "Could not move back to $head_name"
167                 ;;
168         esac
169 }
170
171 finish_rb_merge () {
172         move_to_original_branch
173         git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
174         if test -x "$GIT_DIR"/hooks/post-rewrite &&
175                 test -s "$merge_dir"/rewritten; then
176                 "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
177         fi
178         rm -r "$merge_dir"
179         say All done.
180 }
181
182 run_interactive_rebase () {
183         if [ "$interactive_rebase" = implied ]; then
184                 GIT_EDITOR=:
185                 export GIT_EDITOR
186         fi
187         . git-rebase--interactive
188 }
189
190 run_pre_rebase_hook () {
191         if test -z "$ok_to_skip_pre_rebase" &&
192            test -x "$GIT_DIR/hooks/pre-rebase"
193         then
194                 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
195                 die "The pre-rebase hook refused to rebase."
196         fi
197 }
198
199 test -f "$apply_dir"/applying &&
200         die 'It looks like git-am is in progress. Cannot rebase.'
201
202 if test -d "$apply_dir"
203 then
204         type=am
205         state_dir="$apply_dir"
206 elif test -d "$merge_dir"
207 then
208         if test -f "$merge_dir"/interactive
209         then
210                 type=interactive
211                 interactive_rebase=explicit
212         else
213                 type=merge
214         fi
215         state_dir="$merge_dir"
216 fi
217 test -n "$type" && in_progress=t
218
219 total_argc=$#
220 while test $# != 0
221 do
222         case "$1" in
223         --no-verify)
224                 ok_to_skip_pre_rebase=yes
225                 ;;
226         --verify)
227                 ok_to_skip_pre_rebase=
228                 ;;
229         --continue|--skip|--abort)
230                 test $total_argc -eq 1 || usage
231                 action=${1##--}
232                 ;;
233         --onto)
234                 test 2 -le "$#" || usage
235                 onto="$2"
236                 shift
237                 ;;
238         -i|--interactive)
239                 interactive_rebase=explicit
240                 ;;
241         -p|--preserve-merges)
242                 preserve_merges=t
243                 test -z "$interactive_rebase" && interactive_rebase=implied
244                 ;;
245         --autosquash)
246                 autosquash=t
247                 ;;
248         --no-autosquash)
249                 autosquash=
250                 ;;
251         -M|-m|--m|--me|--mer|--merg|--merge)
252                 do_merge=t
253                 ;;
254         -X*|--strategy-option*)
255                 case "$#,$1" in
256                 1,-X|1,--strategy-option)
257                         usage ;;
258                 *,-X|*,--strategy-option)
259                         newopt="$2"
260                         shift ;;
261                 *,--strategy-option=*)
262                         newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
263                 *,-X*)
264                         newopt="$(expr " $1" : ' -X\(.*\)')" ;;
265                 1,*)
266                         usage ;;
267                 esac
268                 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
269                 do_merge=t
270                 test -z "$strategy" && strategy=recursive
271                 ;;
272         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
273                 --strateg=*|--strategy=*|\
274         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
275                 case "$#,$1" in
276                 *,*=*)
277                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
278                 1,*)
279                         usage ;;
280                 *)
281                         strategy="$2"
282                         shift ;;
283                 esac
284                 do_merge=t
285                 ;;
286         -n|--no-stat)
287                 diffstat=
288                 ;;
289         --stat)
290                 diffstat=t
291                 ;;
292         -v|--verbose)
293                 verbose=t
294                 diffstat=t
295                 GIT_QUIET=
296                 ;;
297         -q|--quiet)
298                 GIT_QUIET=t
299                 git_am_opt="$git_am_opt -q"
300                 verbose=
301                 diffstat=
302                 ;;
303         --whitespace=*)
304                 git_am_opt="$git_am_opt $1"
305                 case "$1" in
306                 --whitespace=fix|--whitespace=strip)
307                         force_rebase=t
308                         ;;
309                 esac
310                 ;;
311         --ignore-whitespace)
312                 git_am_opt="$git_am_opt $1"
313                 ;;
314         --committer-date-is-author-date|--ignore-date)
315                 git_am_opt="$git_am_opt $1"
316                 force_rebase=t
317                 ;;
318         -C*)
319                 git_am_opt="$git_am_opt $1"
320                 ;;
321         --root)
322                 rebase_root=t
323                 ;;
324         -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
325                 force_rebase=t
326                 ;;
327         --rerere-autoupdate|--no-rerere-autoupdate)
328                 allow_rerere_autoupdate="$1"
329                 ;;
330         -*)
331                 usage
332                 ;;
333         *)
334                 break
335                 ;;
336         esac
337         shift
338 done
339 test $# -gt 2 && usage
340
341 if test -n "$action"
342 then
343         test -z "$in_progress" && die "No rebase in progress?"
344         test "$type" = interactive && run_interactive_rebase
345 fi
346
347 case "$action" in
348 continue)
349         git update-index --ignore-submodules --refresh &&
350         git diff-files --quiet --ignore-submodules || {
351                 echo "You must edit all merge conflicts and then"
352                 echo "mark them as resolved using git add"
353                 exit 1
354         }
355         read_state
356         if test -d "$merge_dir"
357         then
358                 continue_merge
359                 while test "$msgnum" -le "$end"
360                 do
361                         call_merge "$msgnum"
362                         continue_merge
363                 done
364                 finish_rb_merge
365                 exit
366         fi
367         git am --resolved --3way --resolvemsg="$resolvemsg" &&
368         move_to_original_branch
369         exit
370         ;;
371 skip)
372         git reset --hard HEAD || exit $?
373         read_state
374         if test -d "$merge_dir"
375         then
376                 git rerere clear
377                 msgnum=$(($msgnum + 1))
378                 while test "$msgnum" -le "$end"
379                 do
380                         call_merge "$msgnum"
381                         continue_merge
382                 done
383                 finish_rb_merge
384                 exit
385         fi
386         git am -3 --skip --resolvemsg="$resolvemsg" &&
387         move_to_original_branch
388         exit
389         ;;
390 abort)
391         git rerere clear
392         read_state
393         case "$head_name" in
394         refs/*)
395                 git symbolic-ref HEAD $head_name ||
396                 die "Could not move back to $head_name"
397                 ;;
398         esac
399         git reset --hard $orig_head
400         rm -r "$state_dir"
401         exit
402         ;;
403 esac
404
405 # Make sure no rebase is in progress
406 if test -n "$in_progress"
407 then
408         die '
409 It seems that there is already a '"${state_dir##*/}"' directory, and
410 I wonder if you are in the middle of another rebase.  If that is the
411 case, please try
412         git rebase (--continue | --abort | --skip)
413 If that is not the case, please
414         rm -fr '"$state_dir"'
415 and run me again.  I am stopping in case you still have something
416 valuable there.'
417 fi
418
419 test $# -eq 0 && test -z "$rebase_root" && usage
420
421 if test -n "$interactive_rebase"
422 then
423         type=interactive
424         state_dir="$merge_dir"
425 elif test -n "$do_merge"
426 then
427         type=merge
428         state_dir="$merge_dir"
429 else
430         type=am
431         state_dir="$apply_dir"
432 fi
433
434 if test -z "$rebase_root"
435 then
436         # The upstream head must be given.  Make sure it is valid.
437         upstream_name="$1"
438         shift
439         upstream=`git rev-parse --verify "${upstream_name}^0"` ||
440         die "invalid upstream $upstream_name"
441         unset root_flag
442         upstream_arg="$upstream_name"
443 else
444         test -z "$onto" && die "You must specify --onto when using --root"
445         unset upstream_name
446         unset upstream
447         root_flag="--root"
448         upstream_arg="$root_flag"
449 fi
450
451 # Make sure the branch to rebase onto is valid.
452 onto_name=${onto-"$upstream_name"}
453 case "$onto_name" in
454 *...*)
455         if      left=${onto_name%...*} right=${onto_name#*...} &&
456                 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
457         then
458                 case "$onto" in
459                 ?*"$LF"?*)
460                         die "$onto_name: there are more than one merge bases"
461                         ;;
462                 '')
463                         die "$onto_name: there is no merge base"
464                         ;;
465                 esac
466         else
467                 die "$onto_name: there is no merge base"
468         fi
469         ;;
470 *)
471         onto=$(git rev-parse --verify "${onto_name}^0") ||
472         die "Does not point to a valid commit: $1"
473         ;;
474 esac
475
476 # If the branch to rebase is given, that is the branch we will rebase
477 # $branch_name -- branch being rebased, or HEAD (already detached)
478 # $orig_head -- commit object name of tip of the branch before rebasing
479 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
480 switch_to=
481 case "$#" in
482 1)
483         # Is it "rebase other $branchname" or "rebase other $commit"?
484         branch_name="$1"
485         switch_to="$1"
486
487         if git show-ref --verify --quiet -- "refs/heads/$1" &&
488            branch=$(git rev-parse -q --verify "refs/heads/$1")
489         then
490                 head_name="refs/heads/$1"
491         elif branch=$(git rev-parse -q --verify "$1")
492         then
493                 head_name="detached HEAD"
494         else
495                 echo >&2 "fatal: no such branch: $1"
496                 usage
497         fi
498         ;;
499 *)
500         # Do not need to switch branches, we are already on it.
501         if branch_name=`git symbolic-ref -q HEAD`
502         then
503                 head_name=$branch_name
504                 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
505         else
506                 head_name="detached HEAD"
507                 branch_name=HEAD ;# detached
508         fi
509         branch=$(git rev-parse --verify "${branch_name}^0") || exit
510         ;;
511 esac
512 orig_head=$branch
513
514 require_clean_work_tree "rebase" "Please commit or stash them."
515
516 # Now we are rebasing commits $upstream..$branch (or with --root,
517 # everything leading up to $branch) on top of $onto
518
519 # Check if we are already based on $onto with linear history,
520 # but this should be done only when upstream and onto are the same
521 # and if this is not an interactive rebase.
522 mb=$(git merge-base "$onto" "$branch")
523 if test "$type" != interactive && test "$upstream" = "$onto" &&
524         test "$mb" = "$onto" &&
525         # linear history?
526         ! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
527 then
528         if test -z "$force_rebase"
529         then
530                 # Lazily switch to the target branch if needed...
531                 test -z "$switch_to" || git checkout "$switch_to" --
532                 say "Current branch $branch_name is up to date."
533                 exit 0
534         else
535                 say "Current branch $branch_name is up to date, rebase forced."
536         fi
537 fi
538
539 # If a hook exists, give it a chance to interrupt
540 run_pre_rebase_hook "$upstream_arg" "$@"
541
542 if test -n "$diffstat"
543 then
544         if test -n "$verbose"
545         then
546                 echo "Changes from $mb to $onto:"
547         fi
548         # We want color (if set), but no pager
549         GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
550 fi
551
552 test "$type" = interactive && run_interactive_rebase
553
554 # Detach HEAD and reset the tree
555 say "First, rewinding head to replay your work on top of it..."
556 git checkout -q "$onto^0" || die "could not detach HEAD"
557 git update-ref ORIG_HEAD $branch
558
559 # If the $onto is a proper descendant of the tip of the branch, then
560 # we just fast-forwarded.
561 if test "$mb" = "$branch"
562 then
563         say "Fast-forwarded $branch_name to $onto_name."
564         move_to_original_branch
565         exit 0
566 fi
567
568 if test -n "$rebase_root"
569 then
570         revisions="$onto..$orig_head"
571 else
572         revisions="$upstream..$orig_head"
573 fi
574
575 if test -z "$do_merge"
576 then
577         git format-patch -k --stdout --full-index --ignore-if-in-upstream \
578                 --src-prefix=a/ --dst-prefix=b/ \
579                 --no-renames $root_flag "$revisions" |
580         git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" &&
581         move_to_original_branch
582         ret=$?
583         test 0 != $ret -a -d "$apply_dir" &&
584                 echo $head_name > "$apply_dir/head-name" &&
585                 echo $onto > "$apply_dir/onto" &&
586                 echo $orig_head > "$apply_dir/orig-head" &&
587                 echo "$GIT_QUIET" > "$apply_dir/quiet"
588         exit $ret
589 fi
590
591 # start doing a rebase with git-merge
592 # this is rename-aware if the recursive (default) strategy is used
593
594 mkdir -p "$merge_dir"
595 echo "$onto_name" > "$merge_dir/onto_name"
596 echo "$head_name" > "$merge_dir/head-name"
597 echo "$onto" > "$merge_dir/onto"
598 echo "$orig_head" > "$merge_dir/orig-head"
599 echo "$GIT_QUIET" > "$merge_dir/quiet"
600
601 msgnum=0
602 for cmt in `git rev-list --reverse --no-merges "$revisions"`
603 do
604         msgnum=$(($msgnum + 1))
605         echo "$cmt" > "$merge_dir/cmt.$msgnum"
606 done
607
608 echo 1 >"$merge_dir/msgnum"
609 echo $msgnum >"$merge_dir/end"
610
611 end=$msgnum
612 msgnum=1
613
614 while test "$msgnum" -le "$end"
615 do
616         call_merge "$msgnum"
617         continue_merge
618 done
619
620 finish_rb_merge