rebase: introduce the --rebase-merges option
[git] / git-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano.
4 #
5
6 SUBDIRECTORY_OK=Yes
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_STUCKLONG=t
9 OPTIONS_SPEC="\
10 git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
11 git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
12 git rebase --continue | --abort | --skip | --edit-todo
13 --
14  Available options are
15 v,verbose!         display a diffstat of what changed upstream
16 q,quiet!           be quiet. implies --no-stat
17 autostash          automatically stash/stash pop before and after
18 fork-point         use 'merge-base --fork-point' to refine upstream
19 onto=!             rebase onto given branch instead of upstream
20 r,rebase-merges!   try to rebase merges instead of skipping them
21 p,preserve-merges! try to recreate merges instead of ignoring them
22 s,strategy=!       use the given merge strategy
23 no-ff!             cherry-pick all commits, even if unchanged
24 m,merge!           use merging strategies to rebase
25 i,interactive!     let the user edit the list of commits to rebase
26 x,exec=!           add exec lines after each commit of the editable list
27 k,keep-empty       preserve empty commits during rebase
28 allow-empty-message allow rebasing commits with empty messages
29 f,force-rebase!    force rebase even if branch is up to date
30 X,strategy-option=! pass the argument through to the merge strategy
31 stat!              display a diffstat of what changed upstream
32 n,no-stat!         do not show diffstat of what changed upstream
33 verify             allow pre-rebase hook to run
34 rerere-autoupdate  allow rerere to update index with resolved conflicts
35 root!              rebase all reachable commits up to the root(s)
36 autosquash         move commits that begin with squash!/fixup! under -i
37 committer-date-is-author-date! passed to 'git am'
38 ignore-date!       passed to 'git am'
39 signoff            passed to 'git am'
40 whitespace=!       passed to 'git apply'
41 ignore-whitespace! passed to 'git apply'
42 C=!                passed to 'git apply'
43 S,gpg-sign?        GPG-sign commits
44  Actions:
45 continue!          continue
46 abort!             abort and check out the original branch
47 skip!              skip current patch and continue
48 edit-todo!         edit the todo list during an interactive rebase
49 quit!              abort but keep HEAD where it is
50 show-current-patch! show the patch file being applied or merged
51 "
52 . git-sh-setup
53 set_reflog_action rebase
54 require_work_tree_exists
55 cd_to_toplevel
56
57 LF='
58 '
59 ok_to_skip_pre_rebase=
60 resolvemsg="
61 $(gettext 'Resolve all conflicts manually, mark them as resolved with
62 "git add/rm <conflicted_files>", then run "git rebase --continue".
63 You can instead skip this commit: run "git rebase --skip".
64 To abort and get back to the state before "git rebase", run "git rebase --abort".')
65 "
66 squash_onto=
67 unset onto
68 unset restrict_revision
69 cmd=
70 strategy=
71 strategy_opts=
72 do_merge=
73 merge_dir="$GIT_DIR"/rebase-merge
74 apply_dir="$GIT_DIR"/rebase-apply
75 verbose=
76 diffstat=
77 test "$(git config --bool rebase.stat)" = true && diffstat=t
78 autostash="$(git config --bool rebase.autostash || echo false)"
79 fork_point=auto
80 git_am_opt=
81 git_format_patch_opt=
82 rebase_root=
83 force_rebase=
84 allow_rerere_autoupdate=
85 # Non-empty if a rebase was in progress when 'git rebase' was invoked
86 in_progress=
87 # One of {am, merge, interactive}
88 type=
89 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
90 state_dir=
91 # One of {'', continue, skip, abort}, as parsed from command line
92 action=
93 rebase_merges=
94 preserve_merges=
95 autosquash=
96 keep_empty=
97 allow_empty_message=
98 signoff=
99 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
100 case "$(git config --bool commit.gpgsign)" in
101 true)   gpg_sign_opt=-S ;;
102 *)      gpg_sign_opt= ;;
103 esac
104
105 read_basic_state () {
106         test -f "$state_dir/head-name" &&
107         test -f "$state_dir/onto" &&
108         head_name=$(cat "$state_dir"/head-name) &&
109         onto=$(cat "$state_dir"/onto) &&
110         # We always write to orig-head, but interactive rebase used to write to
111         # head. Fall back to reading from head to cover for the case that the
112         # user upgraded git with an ongoing interactive rebase.
113         if test -f "$state_dir"/orig-head
114         then
115                 orig_head=$(cat "$state_dir"/orig-head)
116         else
117                 orig_head=$(cat "$state_dir"/head)
118         fi &&
119         GIT_QUIET=$(cat "$state_dir"/quiet) &&
120         test -f "$state_dir"/verbose && verbose=t
121         test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
122         test -f "$state_dir"/strategy_opts &&
123                 strategy_opts="$(cat "$state_dir"/strategy_opts)"
124         test -f "$state_dir"/allow_rerere_autoupdate &&
125                 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
126         test -f "$state_dir"/gpg_sign_opt &&
127                 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
128         test -f "$state_dir"/signoff && {
129                 signoff="$(cat "$state_dir"/signoff)"
130                 force_rebase=t
131         }
132 }
133
134 write_basic_state () {
135         echo "$head_name" > "$state_dir"/head-name &&
136         echo "$onto" > "$state_dir"/onto &&
137         echo "$orig_head" > "$state_dir"/orig-head &&
138         echo "$GIT_QUIET" > "$state_dir"/quiet &&
139         test t = "$verbose" && : > "$state_dir"/verbose
140         test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
141         test -n "$strategy_opts" && echo "$strategy_opts" > \
142                 "$state_dir"/strategy_opts
143         test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
144                 "$state_dir"/allow_rerere_autoupdate
145         test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
146         test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff
147 }
148
149 output () {
150         case "$verbose" in
151         '')
152                 output=$("$@" 2>&1 )
153                 status=$?
154                 test $status != 0 && printf "%s\n" "$output"
155                 return $status
156                 ;;
157         *)
158                 "$@"
159                 ;;
160         esac
161 }
162
163 move_to_original_branch () {
164         case "$head_name" in
165         refs/*)
166                 message="rebase finished: $head_name onto $onto"
167                 git update-ref -m "$message" \
168                         $head_name $(git rev-parse HEAD) $orig_head &&
169                 git symbolic-ref \
170                         -m "rebase finished: returning to $head_name" \
171                         HEAD $head_name ||
172                 die "$(eval_gettext "Could not move back to \$head_name")"
173                 ;;
174         esac
175 }
176
177 apply_autostash () {
178         if test -f "$state_dir/autostash"
179         then
180                 stash_sha1=$(cat "$state_dir/autostash")
181                 if git stash apply $stash_sha1 >/dev/null 2>&1
182                 then
183                         echo "$(gettext 'Applied autostash.')" >&2
184                 else
185                         git stash store -m "autostash" -q $stash_sha1 ||
186                         die "$(eval_gettext "Cannot store \$stash_sha1")"
187                         gettext 'Applying autostash resulted in conflicts.
188 Your changes are safe in the stash.
189 You can run "git stash pop" or "git stash drop" at any time.
190 ' >&2
191                 fi
192         fi
193 }
194
195 finish_rebase () {
196         rm -f "$(git rev-parse --git-path REBASE_HEAD)"
197         apply_autostash &&
198         { git gc --auto || true; } &&
199         rm -rf "$state_dir"
200 }
201
202 run_specific_rebase () {
203         if [ "$interactive_rebase" = implied ]; then
204                 GIT_EDITOR=:
205                 export GIT_EDITOR
206                 autosquash=
207         fi
208         . git-rebase--$type
209         git_rebase__$type${preserve_merges:+__preserve_merges}
210         ret=$?
211         if test $ret -eq 0
212         then
213                 finish_rebase
214         elif test $ret -eq 2 # special exit status for rebase -i
215         then
216                 apply_autostash &&
217                 rm -rf "$state_dir" &&
218                 die "Nothing to do"
219         fi
220         exit $ret
221 }
222
223 run_pre_rebase_hook () {
224         if test -z "$ok_to_skip_pre_rebase" &&
225            test -x "$(git rev-parse --git-path hooks/pre-rebase)"
226         then
227                 "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
228                 die "$(gettext "The pre-rebase hook refused to rebase.")"
229         fi
230 }
231
232 test -f "$apply_dir"/applying &&
233         die "$(gettext "It looks like 'git am' is in progress. Cannot rebase.")"
234
235 if test -d "$apply_dir"
236 then
237         type=am
238         state_dir="$apply_dir"
239 elif test -d "$merge_dir"
240 then
241         if test -f "$merge_dir"/interactive
242         then
243                 type=interactive
244                 interactive_rebase=explicit
245         else
246                 type=merge
247         fi
248         state_dir="$merge_dir"
249 fi
250 test -n "$type" && in_progress=t
251
252 total_argc=$#
253 while test $# != 0
254 do
255         case "$1" in
256         --no-verify)
257                 ok_to_skip_pre_rebase=yes
258                 ;;
259         --verify)
260                 ok_to_skip_pre_rebase=
261                 ;;
262         --continue|--skip|--abort|--quit|--edit-todo|--show-current-patch)
263                 test $total_argc -eq 2 || usage
264                 action=${1##--}
265                 ;;
266         --onto=*)
267                 onto="${1#--onto=}"
268                 ;;
269         --exec=*)
270                 cmd="${cmd}exec ${1#--exec=}${LF}"
271                 test -z "$interactive_rebase" && interactive_rebase=implied
272                 ;;
273         --interactive)
274                 interactive_rebase=explicit
275                 ;;
276         --keep-empty)
277                 keep_empty=yes
278                 ;;
279         --allow-empty-message)
280                 allow_empty_message=--allow-empty-message
281                 ;;
282         --no-keep-empty)
283                 keep_empty=
284                 ;;
285         --rebase-merges)
286                 rebase_merges=t
287                 test -z "$interactive_rebase" && interactive_rebase=implied
288                 ;;
289         --preserve-merges)
290                 preserve_merges=t
291                 test -z "$interactive_rebase" && interactive_rebase=implied
292                 ;;
293         --autosquash)
294                 autosquash=t
295                 ;;
296         --no-autosquash)
297                 autosquash=
298                 ;;
299         --fork-point)
300                 fork_point=t
301                 ;;
302         --no-fork-point)
303                 fork_point=
304                 ;;
305         --merge)
306                 do_merge=t
307                 ;;
308         --strategy-option=*)
309                 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
310                 do_merge=t
311                 test -z "$strategy" && strategy=recursive
312                 ;;
313         --strategy=*)
314                 strategy="${1#--strategy=}"
315                 do_merge=t
316                 ;;
317         --no-stat)
318                 diffstat=
319                 ;;
320         --stat)
321                 diffstat=t
322                 ;;
323         --autostash)
324                 autostash=true
325                 ;;
326         --no-autostash)
327                 autostash=false
328                 ;;
329         --verbose)
330                 verbose=t
331                 diffstat=t
332                 GIT_QUIET=
333                 ;;
334         --quiet)
335                 GIT_QUIET=t
336                 git_am_opt="$git_am_opt -q"
337                 verbose=
338                 diffstat=
339                 ;;
340         --whitespace=*)
341                 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
342                 case "${1#--whitespace=}" in
343                 fix|strip)
344                         force_rebase=t
345                         ;;
346                 esac
347                 ;;
348         --ignore-whitespace)
349                 git_am_opt="$git_am_opt $1"
350                 ;;
351         --signoff)
352                 signoff=--signoff
353                 ;;
354         --no-signoff)
355                 signoff=
356                 ;;
357         --committer-date-is-author-date|--ignore-date)
358                 git_am_opt="$git_am_opt $1"
359                 force_rebase=t
360                 ;;
361         -C*)
362                 git_am_opt="$git_am_opt $1"
363                 ;;
364         --root)
365                 rebase_root=t
366                 ;;
367         --force-rebase|--no-ff)
368                 force_rebase=t
369                 ;;
370         --rerere-autoupdate|--no-rerere-autoupdate)
371                 allow_rerere_autoupdate="$1"
372                 ;;
373         --gpg-sign)
374                 gpg_sign_opt=-S
375                 ;;
376         --gpg-sign=*)
377                 gpg_sign_opt="-S${1#--gpg-sign=}"
378                 ;;
379         --)
380                 shift
381                 break
382                 ;;
383         *)
384                 usage
385                 ;;
386         esac
387         shift
388 done
389 test $# -gt 2 && usage
390
391 if test -n "$action"
392 then
393         test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
394         # Only interactive rebase uses detailed reflog messages
395         if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
396         then
397                 GIT_REFLOG_ACTION="rebase -i ($action)"
398                 export GIT_REFLOG_ACTION
399         fi
400 fi
401
402 if test "$action" = "edit-todo" && test "$type" != "interactive"
403 then
404         die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
405 fi
406
407 case "$action" in
408 continue)
409         # Sanity check
410         git rev-parse --verify HEAD >/dev/null ||
411                 die "$(gettext "Cannot read HEAD")"
412         git update-index --ignore-submodules --refresh &&
413         git diff-files --quiet --ignore-submodules || {
414                 echo "$(gettext "You must edit all merge conflicts and then
415 mark them as resolved using git add")"
416                 exit 1
417         }
418         read_basic_state
419         run_specific_rebase
420         ;;
421 skip)
422         output git reset --hard HEAD || exit $?
423         read_basic_state
424         run_specific_rebase
425         ;;
426 abort)
427         git rerere clear
428         read_basic_state
429         case "$head_name" in
430         refs/*)
431                 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
432                 die "$(eval_gettext "Could not move back to \$head_name")"
433                 ;;
434         esac
435         output git reset --hard $orig_head
436         finish_rebase
437         exit
438         ;;
439 quit)
440         exec rm -rf "$state_dir"
441         ;;
442 edit-todo)
443         run_specific_rebase
444         ;;
445 show-current-patch)
446         run_specific_rebase
447         die "BUG: run_specific_rebase is not supposed to return here"
448         ;;
449 esac
450
451 # Make sure no rebase is in progress
452 if test -n "$in_progress"
453 then
454         state_dir_base=${state_dir##*/}
455         cmd_live_rebase="git rebase (--continue | --abort | --skip)"
456         cmd_clear_stale_rebase="rm -fr \"$state_dir\""
457         die "
458 $(eval_gettext 'It seems that there is already a $state_dir_base directory, and
459 I wonder if you are in the middle of another rebase.  If that is the
460 case, please try
461         $cmd_live_rebase
462 If that is not the case, please
463         $cmd_clear_stale_rebase
464 and run me again.  I am stopping in case you still have something
465 valuable there.')"
466 fi
467
468 if test -n "$rebase_root" && test -z "$onto"
469 then
470         test -z "$interactive_rebase" && interactive_rebase=implied
471 fi
472
473 if test -n "$keep_empty"
474 then
475         test -z "$interactive_rebase" && interactive_rebase=implied
476 fi
477
478 if test -n "$interactive_rebase"
479 then
480         type=interactive
481         state_dir="$merge_dir"
482 elif test -n "$do_merge"
483 then
484         type=merge
485         state_dir="$merge_dir"
486 else
487         type=am
488         state_dir="$apply_dir"
489 fi
490
491 if test -t 2 && test -z "$GIT_QUIET"
492 then
493         git_format_patch_opt="$git_format_patch_opt --progress"
494 fi
495
496 if test -n "$signoff"
497 then
498         test -n "$preserve_merges" &&
499                 die "$(gettext "error: cannot combine '--signoff' with '--preserve-merges'")"
500         git_am_opt="$git_am_opt $signoff"
501         force_rebase=t
502 fi
503
504 if test -z "$rebase_root"
505 then
506         case "$#" in
507         0)
508                 if ! upstream_name=$(git rev-parse --symbolic-full-name \
509                         --verify -q @{upstream} 2>/dev/null)
510                 then
511                         . git-parse-remote
512                         error_on_missing_default_upstream "rebase" "rebase" \
513                                 "against" "git rebase $(gettext '<branch>')"
514                 fi
515
516                 test "$fork_point" = auto && fork_point=t
517                 ;;
518         *)      upstream_name="$1"
519                 if test "$upstream_name" = "-"
520                 then
521                         upstream_name="@{-1}"
522                 fi
523                 shift
524                 ;;
525         esac
526         upstream=$(peel_committish "${upstream_name}") ||
527         die "$(eval_gettext "invalid upstream '\$upstream_name'")"
528         upstream_arg="$upstream_name"
529 else
530         if test -z "$onto"
531         then
532                 empty_tree=$(git hash-object -t tree /dev/null)
533                 onto=$(git commit-tree $empty_tree </dev/null)
534                 squash_onto="$onto"
535         fi
536         unset upstream_name
537         unset upstream
538         test $# -gt 1 && usage
539         upstream_arg=--root
540 fi
541
542 # Make sure the branch to rebase onto is valid.
543 onto_name=${onto-"$upstream_name"}
544 case "$onto_name" in
545 *...*)
546         if      left=${onto_name%...*} right=${onto_name#*...} &&
547                 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
548         then
549                 case "$onto" in
550                 ?*"$LF"?*)
551                         die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
552                         ;;
553                 '')
554                         die "$(eval_gettext "\$onto_name: there is no merge base")"
555                         ;;
556                 esac
557         else
558                 die "$(eval_gettext "\$onto_name: there is no merge base")"
559         fi
560         ;;
561 *)
562         onto=$(peel_committish "$onto_name") ||
563         die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
564         ;;
565 esac
566
567 # If the branch to rebase is given, that is the branch we will rebase
568 # $branch_name -- branch/commit being rebased, or HEAD (already detached)
569 # $orig_head -- commit object name of tip of the branch before rebasing
570 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
571 switch_to=
572 case "$#" in
573 1)
574         # Is it "rebase other $branchname" or "rebase other $commit"?
575         branch_name="$1"
576         switch_to="$1"
577
578         # Is it a local branch?
579         if git show-ref --verify --quiet -- "refs/heads/$branch_name" &&
580            orig_head=$(git rev-parse -q --verify "refs/heads/$branch_name")
581         then
582                 head_name="refs/heads/$branch_name"
583         # If not is it a valid ref (branch or commit)?
584         elif orig_head=$(git rev-parse -q --verify "$branch_name")
585         then
586                 head_name="detached HEAD"
587
588         else
589                 die "$(eval_gettext "fatal: no such branch/commit '\$branch_name'")"
590         fi
591         ;;
592 0)
593         # Do not need to switch branches, we are already on it.
594         if branch_name=$(git symbolic-ref -q HEAD)
595         then
596                 head_name=$branch_name
597                 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
598         else
599                 head_name="detached HEAD"
600                 branch_name=HEAD
601         fi
602         orig_head=$(git rev-parse --verify HEAD) || exit
603         ;;
604 *)
605         die "BUG: unexpected number of arguments left to parse"
606         ;;
607 esac
608
609 if test "$fork_point" = t
610 then
611         new_upstream=$(git merge-base --fork-point "$upstream_name" \
612                         "${switch_to:-HEAD}")
613         if test -n "$new_upstream"
614         then
615                 restrict_revision=$new_upstream
616         fi
617 fi
618
619 if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
620 then
621         stash_sha1=$(git stash create "autostash") ||
622         die "$(gettext 'Cannot autostash')"
623
624         mkdir -p "$state_dir" &&
625         echo $stash_sha1 >"$state_dir/autostash" &&
626         stash_abbrev=$(git rev-parse --short $stash_sha1) &&
627         echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
628         git reset --hard
629 fi
630
631 require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
632
633 # Now we are rebasing commits $upstream..$orig_head (or with --root,
634 # everything leading up to $orig_head) on top of $onto
635
636 # Check if we are already based on $onto with linear history,
637 # but this should be done only when upstream and onto are the same
638 # and if this is not an interactive rebase.
639 mb=$(git merge-base "$onto" "$orig_head")
640 if test "$type" != interactive && test "$upstream" = "$onto" &&
641         test "$mb" = "$onto" && test -z "$restrict_revision" &&
642         # linear history?
643         ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
644 then
645         if test -z "$force_rebase"
646         then
647                 # Lazily switch to the target branch if needed...
648                 test -z "$switch_to" ||
649                 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
650                         git checkout -q "$switch_to" --
651                 if test "$branch_name" = "HEAD" &&
652                          ! git symbolic-ref -q HEAD
653                 then
654                         say "$(eval_gettext "HEAD is up to date.")"
655                 else
656                         say "$(eval_gettext "Current branch \$branch_name is up to date.")"
657                 fi
658                 finish_rebase
659                 exit 0
660         else
661                 if test "$branch_name" = "HEAD" &&
662                          ! git symbolic-ref -q HEAD
663                 then
664                         say "$(eval_gettext "HEAD is up to date, rebase forced.")"
665                 else
666                         say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
667                 fi
668         fi
669 fi
670
671 # If a hook exists, give it a chance to interrupt
672 run_pre_rebase_hook "$upstream_arg" "$@"
673
674 if test -n "$diffstat"
675 then
676         if test -n "$verbose"
677         then
678                 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
679         fi
680         # We want color (if set), but no pager
681         GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
682 fi
683
684 test "$type" = interactive && run_specific_rebase
685
686 # Detach HEAD and reset the tree
687 say "$(gettext "First, rewinding head to replay your work on top of it...")"
688
689 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
690         git checkout -q "$onto^0" || die "could not detach HEAD"
691 git update-ref ORIG_HEAD $orig_head
692
693 # If the $onto is a proper descendant of the tip of the branch, then
694 # we just fast-forwarded.
695 if test "$mb" = "$orig_head"
696 then
697         say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
698         move_to_original_branch
699         finish_rebase
700         exit 0
701 fi
702
703 if test -n "$rebase_root"
704 then
705         revisions="$onto..$orig_head"
706 else
707         revisions="${restrict_revision-$upstream}..$orig_head"
708 fi
709
710 run_specific_rebase