rebase: extract am code to new source file
[git/dkf.git] / git-rebase.sh
blobc60221b5a4f4d57237af6dfc77f303107bc1491b
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 orig_head=$(cat "$state_dir"/orig-head) &&
74 GIT_QUIET=$(cat "$state_dir"/quiet)
77 move_to_original_branch () {
78 case "$head_name" in
79 refs/*)
80 message="rebase finished: $head_name onto $onto"
81 git update-ref -m "$message" \
82 $head_name $(git rev-parse HEAD) $orig_head &&
83 git symbolic-ref HEAD $head_name ||
84 die "Could not move back to $head_name"
86 esac
89 run_specific_rebase () {
90 if [ "$interactive_rebase" = implied ]; then
91 GIT_EDITOR=:
92 export GIT_EDITOR
94 . git-rebase--$type
97 run_pre_rebase_hook () {
98 if test -z "$ok_to_skip_pre_rebase" &&
99 test -x "$GIT_DIR/hooks/pre-rebase"
100 then
101 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
102 die "The pre-rebase hook refused to rebase."
106 test -f "$apply_dir"/applying &&
107 die 'It looks like git-am is in progress. Cannot rebase.'
109 if test -d "$apply_dir"
110 then
111 type=am
112 state_dir="$apply_dir"
113 elif test -d "$merge_dir"
114 then
115 if test -f "$merge_dir"/interactive
116 then
117 type=interactive
118 interactive_rebase=explicit
119 else
120 type=merge
122 state_dir="$merge_dir"
124 test -n "$type" && in_progress=t
126 total_argc=$#
127 while test $# != 0
129 case "$1" in
130 --no-verify)
131 ok_to_skip_pre_rebase=yes
133 --verify)
134 ok_to_skip_pre_rebase=
136 --continue|--skip|--abort)
137 test $total_argc -eq 1 || usage
138 action=${1##--}
140 --onto)
141 test 2 -le "$#" || usage
142 onto="$2"
143 shift
145 -i|--interactive)
146 interactive_rebase=explicit
148 -p|--preserve-merges)
149 preserve_merges=t
150 test -z "$interactive_rebase" && interactive_rebase=implied
152 --autosquash)
153 autosquash=t
155 --no-autosquash)
156 autosquash=
158 -M|-m|--m|--me|--mer|--merg|--merge)
159 do_merge=t
161 -X*|--strategy-option*)
162 case "$#,$1" in
163 1,-X|1,--strategy-option)
164 usage ;;
165 *,-X|*,--strategy-option)
166 newopt="$2"
167 shift ;;
168 *,--strategy-option=*)
169 newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
170 *,-X*)
171 newopt="$(expr " $1" : ' -X\(.*\)')" ;;
172 1,*)
173 usage ;;
174 esac
175 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
176 do_merge=t
177 test -z "$strategy" && strategy=recursive
179 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
180 --strateg=*|--strategy=*|\
181 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
182 case "$#,$1" in
183 *,*=*)
184 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
185 1,*)
186 usage ;;
188 strategy="$2"
189 shift ;;
190 esac
191 do_merge=t
193 -n|--no-stat)
194 diffstat=
196 --stat)
197 diffstat=t
199 -v|--verbose)
200 verbose=t
201 diffstat=t
202 GIT_QUIET=
204 -q|--quiet)
205 GIT_QUIET=t
206 git_am_opt="$git_am_opt -q"
207 verbose=
208 diffstat=
210 --whitespace=*)
211 git_am_opt="$git_am_opt $1"
212 case "$1" in
213 --whitespace=fix|--whitespace=strip)
214 force_rebase=t
216 esac
218 --ignore-whitespace)
219 git_am_opt="$git_am_opt $1"
221 --committer-date-is-author-date|--ignore-date)
222 git_am_opt="$git_am_opt $1"
223 force_rebase=t
225 -C*)
226 git_am_opt="$git_am_opt $1"
228 --root)
229 rebase_root=t
231 -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
232 force_rebase=t
234 --rerere-autoupdate|--no-rerere-autoupdate)
235 allow_rerere_autoupdate="$1"
238 usage
241 break
243 esac
244 shift
245 done
246 test $# -gt 2 && usage
248 if test -n "$action"
249 then
250 test -z "$in_progress" && die "No rebase in progress?"
251 test "$type" = interactive && run_specific_rebase
254 case "$action" in
255 continue)
256 git update-index --ignore-submodules --refresh &&
257 git diff-files --quiet --ignore-submodules || {
258 echo "You must edit all merge conflicts and then"
259 echo "mark them as resolved using git add"
260 exit 1
262 read_basic_state
263 run_specific_rebase
265 skip)
266 git reset --hard HEAD || exit $?
267 read_basic_state
268 run_specific_rebase
270 abort)
271 git rerere clear
272 read_basic_state
273 case "$head_name" in
274 refs/*)
275 git symbolic-ref HEAD $head_name ||
276 die "Could not move back to $head_name"
278 esac
279 git reset --hard $orig_head
280 rm -r "$state_dir"
281 exit
283 esac
285 # Make sure no rebase is in progress
286 if test -n "$in_progress"
287 then
288 die '
289 It seems that there is already a '"${state_dir##*/}"' directory, and
290 I wonder if you are in the middle of another rebase. If that is the
291 case, please try
292 git rebase (--continue | --abort | --skip)
293 If that is not the case, please
294 rm -fr '"$state_dir"'
295 and run me again. I am stopping in case you still have something
296 valuable there.'
299 test $# -eq 0 && test -z "$rebase_root" && usage
301 if test -n "$interactive_rebase"
302 then
303 type=interactive
304 state_dir="$merge_dir"
305 elif test -n "$do_merge"
306 then
307 type=merge
308 state_dir="$merge_dir"
309 else
310 type=am
311 state_dir="$apply_dir"
314 if test -z "$rebase_root"
315 then
316 # The upstream head must be given. Make sure it is valid.
317 upstream_name="$1"
318 shift
319 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
320 die "invalid upstream $upstream_name"
321 upstream_arg="$upstream_name"
322 else
323 test -z "$onto" && die "You must specify --onto when using --root"
324 unset upstream_name
325 unset upstream
326 upstream_arg=--root
329 # Make sure the branch to rebase onto is valid.
330 onto_name=${onto-"$upstream_name"}
331 case "$onto_name" in
332 *...*)
333 if left=${onto_name%...*} right=${onto_name#*...} &&
334 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
335 then
336 case "$onto" in
337 ?*"$LF"?*)
338 die "$onto_name: there are more than one merge bases"
341 die "$onto_name: there is no merge base"
343 esac
344 else
345 die "$onto_name: there is no merge base"
349 onto=$(git rev-parse --verify "${onto_name}^0") ||
350 die "Does not point to a valid commit: $1"
352 esac
354 # If the branch to rebase is given, that is the branch we will rebase
355 # $branch_name -- branch being rebased, or HEAD (already detached)
356 # $orig_head -- commit object name of tip of the branch before rebasing
357 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
358 switch_to=
359 case "$#" in
361 # Is it "rebase other $branchname" or "rebase other $commit"?
362 branch_name="$1"
363 switch_to="$1"
365 if git show-ref --verify --quiet -- "refs/heads/$1" &&
366 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
367 then
368 head_name="refs/heads/$1"
369 elif orig_head=$(git rev-parse -q --verify "$1")
370 then
371 head_name="detached HEAD"
372 else
373 echo >&2 "fatal: no such branch: $1"
374 usage
378 # Do not need to switch branches, we are already on it.
379 if branch_name=`git symbolic-ref -q HEAD`
380 then
381 head_name=$branch_name
382 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
383 else
384 head_name="detached HEAD"
385 branch_name=HEAD ;# detached
387 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
389 esac
391 require_clean_work_tree "rebase" "Please commit or stash them."
393 # Now we are rebasing commits $upstream..$orig_head (or with --root,
394 # everything leading up to $orig_head) on top of $onto
396 # Check if we are already based on $onto with linear history,
397 # but this should be done only when upstream and onto are the same
398 # and if this is not an interactive rebase.
399 mb=$(git merge-base "$onto" "$orig_head")
400 if test "$type" != interactive && test "$upstream" = "$onto" &&
401 test "$mb" = "$onto" &&
402 # linear history?
403 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
404 then
405 if test -z "$force_rebase"
406 then
407 # Lazily switch to the target branch if needed...
408 test -z "$switch_to" || git checkout "$switch_to" --
409 say "Current branch $branch_name is up to date."
410 exit 0
411 else
412 say "Current branch $branch_name is up to date, rebase forced."
416 # If a hook exists, give it a chance to interrupt
417 run_pre_rebase_hook "$upstream_arg" "$@"
419 if test -n "$diffstat"
420 then
421 if test -n "$verbose"
422 then
423 echo "Changes from $mb to $onto:"
425 # We want color (if set), but no pager
426 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
429 test "$type" = interactive && run_specific_rebase
431 # Detach HEAD and reset the tree
432 say "First, rewinding head to replay your work on top of it..."
433 git checkout -q "$onto^0" || die "could not detach HEAD"
434 git update-ref ORIG_HEAD $orig_head
436 # If the $onto is a proper descendant of the tip of the branch, then
437 # we just fast-forwarded.
438 if test "$mb" = "$orig_head"
439 then
440 say "Fast-forwarded $branch_name to $onto_name."
441 move_to_original_branch
442 exit 0
445 if test -n "$rebase_root"
446 then
447 revisions="$onto..$orig_head"
448 else
449 revisions="$upstream..$orig_head"
452 run_specific_rebase