Merge branch 'mh/p4' into next
[git/mjg.git] / git-rebase.sh
bloba040ab51cc68f4ee8d173eda17907447360e4a73
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano.
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
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='
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\".
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 test -f "$state_dir"/allow_rerere_autoupdate &&
88 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
91 write_basic_state () {
92 echo "$head_name" > "$state_dir"/head-name &&
93 echo "$onto" > "$state_dir"/onto &&
94 echo "$orig_head" > "$state_dir"/orig-head &&
95 echo "$GIT_QUIET" > "$state_dir"/quiet &&
96 test t = "$verbose" && : > "$state_dir"/verbose
97 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
98 test -n "$strategy_opts" && echo "$strategy_opts" > \
99 "$state_dir"/strategy_opts
100 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
101 "$state_dir"/allow_rerere_autoupdate
104 output () {
105 case "$verbose" in
107 output=$("$@" 2>&1 )
108 status=$?
109 test $status != 0 && printf "%s\n" "$output"
110 return $status
113 "$@"
115 esac
118 move_to_original_branch () {
119 case "$head_name" in
120 refs/*)
121 message="rebase finished: $head_name onto $onto"
122 git update-ref -m "$message" \
123 $head_name $(git rev-parse HEAD) $orig_head &&
124 git symbolic-ref HEAD $head_name ||
125 die "Could not move back to $head_name"
127 esac
130 run_specific_rebase () {
131 if [ "$interactive_rebase" = implied ]; then
132 GIT_EDITOR=:
133 export GIT_EDITOR
135 . git-rebase--$type
138 run_pre_rebase_hook () {
139 if test -z "$ok_to_skip_pre_rebase" &&
140 test -x "$GIT_DIR/hooks/pre-rebase"
141 then
142 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
143 die "The pre-rebase hook refused to rebase."
147 test -f "$apply_dir"/applying &&
148 die 'It looks like git-am is in progress. Cannot rebase.'
150 if test -d "$apply_dir"
151 then
152 type=am
153 state_dir="$apply_dir"
154 elif test -d "$merge_dir"
155 then
156 if test -f "$merge_dir"/interactive
157 then
158 type=interactive
159 interactive_rebase=explicit
160 else
161 type=merge
163 state_dir="$merge_dir"
165 test -n "$type" && in_progress=t
167 total_argc=$#
168 while test $# != 0
170 case "$1" in
171 --no-verify)
172 ok_to_skip_pre_rebase=yes
174 --verify)
175 ok_to_skip_pre_rebase=
177 --continue|--skip|--abort)
178 test $total_argc -eq 1 || usage
179 action=${1##--}
181 --onto)
182 test 2 -le "$#" || usage
183 onto="$2"
184 shift
186 -i|--interactive)
187 interactive_rebase=explicit
189 -p|--preserve-merges)
190 preserve_merges=t
191 test -z "$interactive_rebase" && interactive_rebase=implied
193 --autosquash)
194 autosquash=t
196 --no-autosquash)
197 autosquash=
199 -M|-m|--m|--me|--mer|--merg|--merge)
200 do_merge=t
202 -X*|--strategy-option*)
203 case "$#,$1" in
204 1,-X|1,--strategy-option)
205 usage ;;
206 *,-X|*,--strategy-option)
207 newopt="$2"
208 shift ;;
209 *,--strategy-option=*)
210 newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
211 *,-X*)
212 newopt="$(expr " $1" : ' -X\(.*\)')" ;;
213 1,*)
214 usage ;;
215 esac
216 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
217 do_merge=t
218 test -z "$strategy" && strategy=recursive
220 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
221 --strateg=*|--strategy=*|\
222 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
223 case "$#,$1" in
224 *,*=*)
225 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
226 1,*)
227 usage ;;
229 strategy="$2"
230 shift ;;
231 esac
232 do_merge=t
234 -n|--no-stat)
235 diffstat=
237 --stat)
238 diffstat=t
240 -v|--verbose)
241 verbose=t
242 diffstat=t
243 GIT_QUIET=
245 -q|--quiet)
246 GIT_QUIET=t
247 git_am_opt="$git_am_opt -q"
248 verbose=
249 diffstat=
251 --whitespace=*)
252 git_am_opt="$git_am_opt $1"
253 case "$1" in
254 --whitespace=fix|--whitespace=strip)
255 force_rebase=t
257 esac
259 --ignore-whitespace)
260 git_am_opt="$git_am_opt $1"
262 --committer-date-is-author-date|--ignore-date)
263 git_am_opt="$git_am_opt $1"
264 force_rebase=t
266 -C*)
267 git_am_opt="$git_am_opt $1"
269 --root)
270 rebase_root=t
272 -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
273 force_rebase=t
275 --rerere-autoupdate|--no-rerere-autoupdate)
276 allow_rerere_autoupdate="$1"
279 usage
282 break
284 esac
285 shift
286 done
287 test $# -gt 2 && usage
289 if test -n "$action"
290 then
291 test -z "$in_progress" && die "No rebase in progress?"
292 # Only interactive rebase uses detailed reflog messages
293 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
294 then
295 GIT_REFLOG_ACTION="rebase -i ($action)"
296 export GIT_REFLOG_ACTION
300 case "$action" in
301 continue)
302 # Sanity check
303 git rev-parse --verify HEAD >/dev/null ||
304 die "Cannot read HEAD"
305 git update-index --ignore-submodules --refresh &&
306 git diff-files --quiet --ignore-submodules || {
307 echo "You must edit all merge conflicts and then"
308 echo "mark them as resolved using git add"
309 exit 1
311 read_basic_state
312 run_specific_rebase
314 skip)
315 output git reset --hard HEAD || exit $?
316 read_basic_state
317 run_specific_rebase
319 abort)
320 git rerere clear
321 read_basic_state
322 case "$head_name" in
323 refs/*)
324 git symbolic-ref HEAD $head_name ||
325 die "Could not move back to $head_name"
327 esac
328 output git reset --hard $orig_head
329 rm -r "$state_dir"
330 exit
332 esac
334 # Make sure no rebase is in progress
335 if test -n "$in_progress"
336 then
337 die '
338 It seems that there is already a '"${state_dir##*/}"' directory, and
339 I wonder if you are in the middle of another rebase. If that is the
340 case, please try
341 git rebase (--continue | --abort | --skip)
342 If that is not the case, please
343 rm -fr '"$state_dir"'
344 and run me again. I am stopping in case you still have something
345 valuable there.'
348 if test -n "$interactive_rebase"
349 then
350 type=interactive
351 state_dir="$merge_dir"
352 elif test -n "$do_merge"
353 then
354 type=merge
355 state_dir="$merge_dir"
356 else
357 type=am
358 state_dir="$apply_dir"
361 if test -z "$rebase_root"
362 then
363 case "$#" in
365 if ! upstream_name=$(git rev-parse --symbolic-full-name \
366 --verify -q @{upstream} 2>/dev/null)
367 then
368 . git-parse-remote
369 error_on_missing_default_upstream "rebase" "rebase" \
370 "against" "git rebase <upstream branch>"
373 *) upstream_name="$1"
374 shift
376 esac
377 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
378 die "invalid upstream $upstream_name"
379 upstream_arg="$upstream_name"
380 else
381 test -z "$onto" && die "You must specify --onto when using --root"
382 unset upstream_name
383 unset upstream
384 upstream_arg=--root
387 # Make sure the branch to rebase onto is valid.
388 onto_name=${onto-"$upstream_name"}
389 case "$onto_name" in
390 *...*)
391 if left=${onto_name%...*} right=${onto_name#*...} &&
392 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
393 then
394 case "$onto" in
395 ?*"$LF"?*)
396 die "$onto_name: there are more than one merge bases"
399 die "$onto_name: there is no merge base"
401 esac
402 else
403 die "$onto_name: there is no merge base"
407 onto=$(git rev-parse --verify "${onto_name}^0") ||
408 die "Does not point to a valid commit: $1"
410 esac
412 # If the branch to rebase is given, that is the branch we will rebase
413 # $branch_name -- branch being rebased, or HEAD (already detached)
414 # $orig_head -- commit object name of tip of the branch before rebasing
415 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
416 switch_to=
417 case "$#" in
419 # Is it "rebase other $branchname" or "rebase other $commit"?
420 branch_name="$1"
421 switch_to="$1"
423 if git show-ref --verify --quiet -- "refs/heads/$1" &&
424 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
425 then
426 head_name="refs/heads/$1"
427 elif orig_head=$(git rev-parse -q --verify "$1")
428 then
429 head_name="detached HEAD"
430 else
431 echo >&2 "fatal: no such branch: $1"
432 usage
436 # Do not need to switch branches, we are already on it.
437 if branch_name=`git symbolic-ref -q HEAD`
438 then
439 head_name=$branch_name
440 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
441 else
442 head_name="detached HEAD"
443 branch_name=HEAD ;# detached
445 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
447 esac
449 require_clean_work_tree "rebase" "Please commit or stash them."
451 # Now we are rebasing commits $upstream..$orig_head (or with --root,
452 # everything leading up to $orig_head) on top of $onto
454 # Check if we are already based on $onto with linear history,
455 # but this should be done only when upstream and onto are the same
456 # and if this is not an interactive rebase.
457 mb=$(git merge-base "$onto" "$orig_head")
458 if test "$type" != interactive && test "$upstream" = "$onto" &&
459 test "$mb" = "$onto" &&
460 # linear history?
461 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
462 then
463 if test -z "$force_rebase"
464 then
465 # Lazily switch to the target branch if needed...
466 test -z "$switch_to" || git checkout "$switch_to" --
467 say "Current branch $branch_name is up to date."
468 exit 0
469 else
470 say "Current branch $branch_name is up to date, rebase forced."
474 # If a hook exists, give it a chance to interrupt
475 run_pre_rebase_hook "$upstream_arg" "$@"
477 if test -n "$diffstat"
478 then
479 if test -n "$verbose"
480 then
481 echo "Changes from $mb to $onto:"
483 # We want color (if set), but no pager
484 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
487 test "$type" = interactive && run_specific_rebase
489 # Detach HEAD and reset the tree
490 say "First, rewinding head to replay your work on top of it..."
491 git checkout -q "$onto^0" || die "could not detach HEAD"
492 git update-ref ORIG_HEAD $orig_head
494 # If the $onto is a proper descendant of the tip of the branch, then
495 # we just fast-forwarded.
496 if test "$mb" = "$orig_head"
497 then
498 say "Fast-forwarded $branch_name to $onto_name."
499 move_to_original_branch
500 exit 0
503 if test -n "$rebase_root"
504 then
505 revisions="$onto..$orig_head"
506 else
507 revisions="$upstream..$orig_head"
510 run_specific_rebase