Code

rebase: remember strategy and strategy options
[git.git] / git-rebase.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Junio C Hamano.
4 #
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.
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.
20 Note that if <branch> is not specified on the command line, the
21 currently checked out branch is used.
23 Example:       git-rebase master~1 topic
25         A---B---C topic                   A'\''--B'\''--C'\'' topic
26        /                   -->           /
27   D---E---F---G master          D---E---F---G master
28 '
30 SUBDIRECTORY_OK=Yes
31 OPTIONS_SPEC=
32 . git-sh-setup
33 set_reflog_action rebase
34 require_work_tree
35 cd_to_toplevel
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 verbose=
52 diffstat=
53 test "$(git config --bool rebase.stat)" = true && diffstat=t
54 git_am_opt=
55 rebase_root=
56 force_rebase=
57 allow_rerere_autoupdate=
58 # Non-empty if a rebase was in progress when 'git rebase' was invoked
59 in_progress=
60 # One of {am, merge, interactive}
61 type=
62 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
63 state_dir=
64 # One of {'', continue, skip, abort}, as parsed from command line
65 action=
66 preserve_merges=
67 autosquash=
68 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
70 read_basic_state () {
71         head_name=$(cat "$state_dir"/head-name) &&
72         onto=$(cat "$state_dir"/onto) &&
73         # We always write to orig-head, but interactive rebase used to write to
74         # head. Fall back to reading from head to cover for the case that the
75         # user upgraded git with an ongoing interactive rebase.
76         if test -f "$state_dir"/orig-head
77         then
78                 orig_head=$(cat "$state_dir"/orig-head)
79         else
80                 orig_head=$(cat "$state_dir"/head)
81         fi &&
82         GIT_QUIET=$(cat "$state_dir"/quiet) &&
83         test -f "$state_dir"/verbose && verbose=t
84         test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
85         test -f "$state_dir"/strategy_opts &&
86                 strategy_opts="$(cat "$state_dir"/strategy_opts)"
87 }
89 write_basic_state () {
90         echo "$head_name" > "$state_dir"/head-name &&
91         echo "$onto" > "$state_dir"/onto &&
92         echo "$orig_head" > "$state_dir"/orig-head &&
93         echo "$GIT_QUIET" > "$state_dir"/quiet &&
94         test t = "$verbose" && : > "$state_dir"/verbose
95         test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
96         test -n "$strategy_opts" && echo "$strategy_opts" > \
97                 "$state_dir"/strategy_opts
98 }
100 output () {
101         case "$verbose" in
102         '')
103                 output=$("$@" 2>&1 )
104                 status=$?
105                 test $status != 0 && printf "%s\n" "$output"
106                 return $status
107                 ;;
108         *)
109                 "$@"
110                 ;;
111         esac
114 move_to_original_branch () {
115         case "$head_name" in
116         refs/*)
117                 message="rebase finished: $head_name onto $onto"
118                 git update-ref -m "$message" \
119                         $head_name $(git rev-parse HEAD) $orig_head &&
120                 git symbolic-ref HEAD $head_name ||
121                 die "Could not move back to $head_name"
122                 ;;
123         esac
126 run_specific_rebase () {
127         if [ "$interactive_rebase" = implied ]; then
128                 GIT_EDITOR=:
129                 export GIT_EDITOR
130         fi
131         . git-rebase--$type
134 run_pre_rebase_hook () {
135         if test -z "$ok_to_skip_pre_rebase" &&
136            test -x "$GIT_DIR/hooks/pre-rebase"
137         then
138                 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
139                 die "The pre-rebase hook refused to rebase."
140         fi
143 test -f "$apply_dir"/applying &&
144         die 'It looks like git-am is in progress. Cannot rebase.'
146 if test -d "$apply_dir"
147 then
148         type=am
149         state_dir="$apply_dir"
150 elif test -d "$merge_dir"
151 then
152         if test -f "$merge_dir"/interactive
153         then
154                 type=interactive
155                 interactive_rebase=explicit
156         else
157                 type=merge
158         fi
159         state_dir="$merge_dir"
160 fi
161 test -n "$type" && in_progress=t
163 total_argc=$#
164 while test $# != 0
165 do
166         case "$1" in
167         --no-verify)
168                 ok_to_skip_pre_rebase=yes
169                 ;;
170         --verify)
171                 ok_to_skip_pre_rebase=
172                 ;;
173         --continue|--skip|--abort)
174                 test $total_argc -eq 1 || usage
175                 action=${1##--}
176                 ;;
177         --onto)
178                 test 2 -le "$#" || usage
179                 onto="$2"
180                 shift
181                 ;;
182         -i|--interactive)
183                 interactive_rebase=explicit
184                 ;;
185         -p|--preserve-merges)
186                 preserve_merges=t
187                 test -z "$interactive_rebase" && interactive_rebase=implied
188                 ;;
189         --autosquash)
190                 autosquash=t
191                 ;;
192         --no-autosquash)
193                 autosquash=
194                 ;;
195         -M|-m|--m|--me|--mer|--merg|--merge)
196                 do_merge=t
197                 ;;
198         -X*|--strategy-option*)
199                 case "$#,$1" in
200                 1,-X|1,--strategy-option)
201                         usage ;;
202                 *,-X|*,--strategy-option)
203                         newopt="$2"
204                         shift ;;
205                 *,--strategy-option=*)
206                         newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
207                 *,-X*)
208                         newopt="$(expr " $1" : ' -X\(.*\)')" ;;
209                 1,*)
210                         usage ;;
211                 esac
212                 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
213                 do_merge=t
214                 test -z "$strategy" && strategy=recursive
215                 ;;
216         -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
217                 --strateg=*|--strategy=*|\
218         -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
219                 case "$#,$1" in
220                 *,*=*)
221                         strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
222                 1,*)
223                         usage ;;
224                 *)
225                         strategy="$2"
226                         shift ;;
227                 esac
228                 do_merge=t
229                 ;;
230         -n|--no-stat)
231                 diffstat=
232                 ;;
233         --stat)
234                 diffstat=t
235                 ;;
236         -v|--verbose)
237                 verbose=t
238                 diffstat=t
239                 GIT_QUIET=
240                 ;;
241         -q|--quiet)
242                 GIT_QUIET=t
243                 git_am_opt="$git_am_opt -q"
244                 verbose=
245                 diffstat=
246                 ;;
247         --whitespace=*)
248                 git_am_opt="$git_am_opt $1"
249                 case "$1" in
250                 --whitespace=fix|--whitespace=strip)
251                         force_rebase=t
252                         ;;
253                 esac
254                 ;;
255         --ignore-whitespace)
256                 git_am_opt="$git_am_opt $1"
257                 ;;
258         --committer-date-is-author-date|--ignore-date)
259                 git_am_opt="$git_am_opt $1"
260                 force_rebase=t
261                 ;;
262         -C*)
263                 git_am_opt="$git_am_opt $1"
264                 ;;
265         --root)
266                 rebase_root=t
267                 ;;
268         -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
269                 force_rebase=t
270                 ;;
271         --rerere-autoupdate|--no-rerere-autoupdate)
272                 allow_rerere_autoupdate="$1"
273                 ;;
274         -*)
275                 usage
276                 ;;
277         *)
278                 break
279                 ;;
280         esac
281         shift
282 done
283 test $# -gt 2 && usage
285 if test -n "$action"
286 then
287         test -z "$in_progress" && die "No rebase in progress?"
288         # Only interactive rebase uses detailed reflog messages
289         if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
290         then
291                 GIT_REFLOG_ACTION="rebase -i ($action)"
292                 export GIT_REFLOG_ACTION
293         fi
294 fi
296 case "$action" in
297 continue)
298         # Sanity check
299         git rev-parse --verify HEAD >/dev/null ||
300                 die "Cannot read HEAD"
301         git update-index --ignore-submodules --refresh &&
302         git diff-files --quiet --ignore-submodules || {
303                 echo "You must edit all merge conflicts and then"
304                 echo "mark them as resolved using git add"
305                 exit 1
306         }
307         read_basic_state
308         run_specific_rebase
309         ;;
310 skip)
311         output git reset --hard HEAD || exit $?
312         read_basic_state
313         run_specific_rebase
314         ;;
315 abort)
316         git rerere clear
317         read_basic_state
318         case "$head_name" in
319         refs/*)
320                 git symbolic-ref HEAD $head_name ||
321                 die "Could not move back to $head_name"
322                 ;;
323         esac
324         output git reset --hard $orig_head
325         rm -r "$state_dir"
326         exit
327         ;;
328 esac
330 # Make sure no rebase is in progress
331 if test -n "$in_progress"
332 then
333         die '
334 It seems that there is already a '"${state_dir##*/}"' directory, and
335 I wonder if you are in the middle of another rebase.  If that is the
336 case, please try
337         git rebase (--continue | --abort | --skip)
338 If that is not the case, please
339         rm -fr '"$state_dir"'
340 and run me again.  I am stopping in case you still have something
341 valuable there.'
342 fi
344 test $# -eq 0 && test -z "$rebase_root" && usage
346 if test -n "$interactive_rebase"
347 then
348         type=interactive
349         state_dir="$merge_dir"
350 elif test -n "$do_merge"
351 then
352         type=merge
353         state_dir="$merge_dir"
354 else
355         type=am
356         state_dir="$apply_dir"
357 fi
359 if test -z "$rebase_root"
360 then
361         # The upstream head must be given.  Make sure it is valid.
362         upstream_name="$1"
363         shift
364         upstream=`git rev-parse --verify "${upstream_name}^0"` ||
365         die "invalid upstream $upstream_name"
366         upstream_arg="$upstream_name"
367 else
368         test -z "$onto" && die "You must specify --onto when using --root"
369         unset upstream_name
370         unset upstream
371         upstream_arg=--root
372 fi
374 # Make sure the branch to rebase onto is valid.
375 onto_name=${onto-"$upstream_name"}
376 case "$onto_name" in
377 *...*)
378         if      left=${onto_name%...*} right=${onto_name#*...} &&
379                 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
380         then
381                 case "$onto" in
382                 ?*"$LF"?*)
383                         die "$onto_name: there are more than one merge bases"
384                         ;;
385                 '')
386                         die "$onto_name: there is no merge base"
387                         ;;
388                 esac
389         else
390                 die "$onto_name: there is no merge base"
391         fi
392         ;;
393 *)
394         onto=$(git rev-parse --verify "${onto_name}^0") ||
395         die "Does not point to a valid commit: $1"
396         ;;
397 esac
399 # If the branch to rebase is given, that is the branch we will rebase
400 # $branch_name -- branch being rebased, or HEAD (already detached)
401 # $orig_head -- commit object name of tip of the branch before rebasing
402 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
403 switch_to=
404 case "$#" in
405 1)
406         # Is it "rebase other $branchname" or "rebase other $commit"?
407         branch_name="$1"
408         switch_to="$1"
410         if git show-ref --verify --quiet -- "refs/heads/$1" &&
411            orig_head=$(git rev-parse -q --verify "refs/heads/$1")
412         then
413                 head_name="refs/heads/$1"
414         elif orig_head=$(git rev-parse -q --verify "$1")
415         then
416                 head_name="detached HEAD"
417         else
418                 echo >&2 "fatal: no such branch: $1"
419                 usage
420         fi
421         ;;
422 *)
423         # Do not need to switch branches, we are already on it.
424         if branch_name=`git symbolic-ref -q HEAD`
425         then
426                 head_name=$branch_name
427                 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
428         else
429                 head_name="detached HEAD"
430                 branch_name=HEAD ;# detached
431         fi
432         orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
433         ;;
434 esac
436 require_clean_work_tree "rebase" "Please commit or stash them."
438 # Now we are rebasing commits $upstream..$orig_head (or with --root,
439 # everything leading up to $orig_head) on top of $onto
441 # Check if we are already based on $onto with linear history,
442 # but this should be done only when upstream and onto are the same
443 # and if this is not an interactive rebase.
444 mb=$(git merge-base "$onto" "$orig_head")
445 if test "$type" != interactive && test "$upstream" = "$onto" &&
446         test "$mb" = "$onto" &&
447         # linear history?
448         ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
449 then
450         if test -z "$force_rebase"
451         then
452                 # Lazily switch to the target branch if needed...
453                 test -z "$switch_to" || git checkout "$switch_to" --
454                 say "Current branch $branch_name is up to date."
455                 exit 0
456         else
457                 say "Current branch $branch_name is up to date, rebase forced."
458         fi
459 fi
461 # If a hook exists, give it a chance to interrupt
462 run_pre_rebase_hook "$upstream_arg" "$@"
464 if test -n "$diffstat"
465 then
466         if test -n "$verbose"
467         then
468                 echo "Changes from $mb to $onto:"
469         fi
470         # We want color (if set), but no pager
471         GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
472 fi
474 test "$type" = interactive && run_specific_rebase
476 # Detach HEAD and reset the tree
477 say "First, rewinding head to replay your work on top of it..."
478 git checkout -q "$onto^0" || die "could not detach HEAD"
479 git update-ref ORIG_HEAD $orig_head
481 # If the $onto is a proper descendant of the tip of the branch, then
482 # we just fast-forwarded.
483 if test "$mb" = "$orig_head"
484 then
485         say "Fast-forwarded $branch_name to $onto_name."
486         move_to_original_branch
487         exit 0
488 fi
490 if test -n "$rebase_root"
491 then
492         revisions="$onto..$orig_head"
493 else
494         revisions="$upstream..$orig_head"
495 fi
497 run_specific_rebase