rebase: align variable content
[alt-git.git] / git-rebase.sh
blob3eac5a48b66350d25c32f8ff293a31b898a265c0
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 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=
68 read_state () {
69 if test "$type" = merge
70 then
71 onto_name=$(cat "$state_dir"/onto_name) &&
72 end=$(cat "$state_dir"/end) &&
73 msgnum=$(cat "$state_dir"/msgnum)
74 fi &&
75 head_name=$(cat "$state_dir"/head-name) &&
76 onto=$(cat "$state_dir"/onto) &&
77 orig_head=$(cat "$state_dir"/orig-head) &&
78 GIT_QUIET=$(cat "$state_dir"/quiet)
81 continue_merge () {
82 test -d "$merge_dir" || die "$merge_dir directory does not exist"
84 unmerged=$(git ls-files -u)
85 if test -n "$unmerged"
86 then
87 echo "You still have unmerged paths in your index"
88 echo "did you forget to use git add?"
89 die "$resolvemsg"
92 cmt=`cat "$merge_dir/current"`
93 if ! git diff-index --quiet --ignore-submodules HEAD --
94 then
95 if ! git commit --no-verify -C "$cmt"
96 then
97 echo "Commit failed, please do not call \"git commit\""
98 echo "directly, but instead do one of the following: "
99 die "$resolvemsg"
101 if test -z "$GIT_QUIET"
102 then
103 printf "Committed: %0${prec}d " $msgnum
105 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
106 else
107 if test -z "$GIT_QUIET"
108 then
109 printf "Already applied: %0${prec}d " $msgnum
112 test -z "$GIT_QUIET" &&
113 GIT_PAGER='' git log --format=%s -1 "$cmt"
115 # onto the next patch:
116 msgnum=$(($msgnum + 1))
117 echo "$msgnum" >"$merge_dir/msgnum"
120 call_merge () {
121 cmt="$(cat "$merge_dir/cmt.$1")"
122 echo "$cmt" > "$merge_dir/current"
123 hd=$(git rev-parse --verify HEAD)
124 cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
125 msgnum=$(cat "$merge_dir/msgnum")
126 eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
127 eval GITHEAD_$hd='$onto_name'
128 export GITHEAD_$cmt GITHEAD_$hd
129 if test -n "$GIT_QUIET"
130 then
131 GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
133 test -z "$strategy" && strategy=recursive
134 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
135 rv=$?
136 case "$rv" in
138 unset GITHEAD_$cmt GITHEAD_$hd
139 return
142 git rerere $allow_rerere_autoupdate
143 die "$resolvemsg"
146 echo "Strategy: $rv $strategy failed, try another" 1>&2
147 die "$resolvemsg"
150 die "Unknown exit code ($rv) from command:" \
151 "git-merge-$strategy $cmt^ -- HEAD $cmt"
153 esac
156 move_to_original_branch () {
157 case "$head_name" in
158 refs/*)
159 message="rebase finished: $head_name onto $onto"
160 git update-ref -m "$message" \
161 $head_name $(git rev-parse HEAD) $orig_head &&
162 git symbolic-ref HEAD $head_name ||
163 die "Could not move back to $head_name"
165 esac
168 finish_rb_merge () {
169 move_to_original_branch
170 git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
171 if test -x "$GIT_DIR"/hooks/post-rewrite &&
172 test -s "$merge_dir"/rewritten; then
173 "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
175 rm -r "$merge_dir"
176 say All done.
179 is_interactive () {
180 while test $# != 0
182 case "$1" in
183 -i|--interactive)
184 interactive_rebase=explicit
185 break
187 -p|--preserve-merges)
188 interactive_rebase=implied
190 esac
191 shift
192 done
194 if [ "$interactive_rebase" = implied ]; then
195 GIT_EDITOR=:
196 export GIT_EDITOR
199 test -n "$interactive_rebase" || test -f "$merge_dir"/interactive
202 run_pre_rebase_hook () {
203 if test -z "$ok_to_skip_pre_rebase" &&
204 test -x "$GIT_DIR/hooks/pre-rebase"
205 then
206 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
207 die "The pre-rebase hook refused to rebase."
211 test -f "$apply_dir"/applying &&
212 die 'It looks like git-am is in progress. Cannot rebase.'
214 is_interactive "$@" && exec git-rebase--interactive "$@"
216 if test -d "$apply_dir"
217 then
218 type=am
219 state_dir="$apply_dir"
220 elif test -d "$merge_dir"
221 then
222 if test -f "$merge_dir"/interactive
223 then
224 type=interactive
225 interactive_rebase=explicit
226 else
227 type=merge
229 state_dir="$merge_dir"
231 test -n "$type" && in_progress=t
233 total_argc=$#
234 while test $# != 0
236 case "$1" in
237 --no-verify)
238 ok_to_skip_pre_rebase=yes
240 --verify)
241 ok_to_skip_pre_rebase=
243 --continue|--skip|--abort)
244 test $total_argc -eq 1 || usage
245 action=${1##--}
247 --onto)
248 test 2 -le "$#" || usage
249 onto="$2"
250 shift
252 -M|-m|--m|--me|--mer|--merg|--merge)
253 do_merge=t
255 -X*|--strategy-option*)
256 case "$#,$1" in
257 1,-X|1,--strategy-option)
258 usage ;;
259 *,-X|*,--strategy-option)
260 newopt="$2"
261 shift ;;
262 *,--strategy-option=*)
263 newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
264 *,-X*)
265 newopt="$(expr " $1" : ' -X\(.*\)')" ;;
266 1,*)
267 usage ;;
268 esac
269 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
270 do_merge=t
271 test -z "$strategy" && strategy=recursive
273 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
274 --strateg=*|--strategy=*|\
275 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
276 case "$#,$1" in
277 *,*=*)
278 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
279 1,*)
280 usage ;;
282 strategy="$2"
283 shift ;;
284 esac
285 do_merge=t
287 -n|--no-stat)
288 diffstat=
290 --stat)
291 diffstat=t
293 -v|--verbose)
294 verbose=t
295 diffstat=t
296 GIT_QUIET=
298 -q|--quiet)
299 GIT_QUIET=t
300 git_am_opt="$git_am_opt -q"
301 verbose=
302 diffstat=
304 --whitespace=*)
305 git_am_opt="$git_am_opt $1"
306 case "$1" in
307 --whitespace=fix|--whitespace=strip)
308 force_rebase=t
310 esac
312 --ignore-whitespace)
313 git_am_opt="$git_am_opt $1"
315 --committer-date-is-author-date|--ignore-date)
316 git_am_opt="$git_am_opt $1"
317 force_rebase=t
319 -C*)
320 git_am_opt="$git_am_opt $1"
322 --root)
323 rebase_root=t
325 -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
326 force_rebase=t
328 --rerere-autoupdate|--no-rerere-autoupdate)
329 allow_rerere_autoupdate="$1"
332 usage
335 break
337 esac
338 shift
339 done
340 test $# -gt 2 && usage
342 test -n "$action" && test -z "$in_progress" && die "No rebase in progress?"
344 case "$action" in
345 continue)
346 git update-index --ignore-submodules --refresh &&
347 git diff-files --quiet --ignore-submodules || {
348 echo "You must edit all merge conflicts and then"
349 echo "mark them as resolved using git add"
350 exit 1
352 read_state
353 if test -d "$merge_dir"
354 then
355 continue_merge
356 while test "$msgnum" -le "$end"
358 call_merge "$msgnum"
359 continue_merge
360 done
361 finish_rb_merge
362 exit
364 git am --resolved --3way --resolvemsg="$resolvemsg" &&
365 move_to_original_branch
366 exit
368 skip)
369 git reset --hard HEAD || exit $?
370 read_state
371 if test -d "$merge_dir"
372 then
373 git rerere clear
374 msgnum=$(($msgnum + 1))
375 while test "$msgnum" -le "$end"
377 call_merge "$msgnum"
378 continue_merge
379 done
380 finish_rb_merge
381 exit
383 git am -3 --skip --resolvemsg="$resolvemsg" &&
384 move_to_original_branch
385 exit
387 abort)
388 git rerere clear
389 read_state
390 case "$head_name" in
391 refs/*)
392 git symbolic-ref HEAD $head_name ||
393 die "Could not move back to $head_name"
395 esac
396 git reset --hard $orig_head
397 rm -r "$state_dir"
398 exit
400 esac
402 # Make sure no rebase is in progress
403 if test -n "$in_progress"
404 then
405 die '
406 It seems that there is already a '"${state_dir##*/}"' directory, and
407 I wonder if you are in the middle of another rebase. If that is the
408 case, please try
409 git rebase (--continue | --abort | --skip)
410 If that is not the case, please
411 rm -fr '"$state_dir"'
412 and run me again. I am stopping in case you still have something
413 valuable there.'
416 test $# -eq 0 && test -z "$rebase_root" && usage
418 require_clean_work_tree "rebase" "Please commit or stash them."
420 if test -z "$rebase_root"
421 then
422 # The upstream head must be given. Make sure it is valid.
423 upstream_name="$1"
424 shift
425 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
426 die "invalid upstream $upstream_name"
427 unset root_flag
428 upstream_arg="$upstream_name"
429 else
430 test -z "$onto" && die "--root must be used with --onto"
431 unset upstream_name
432 unset upstream
433 root_flag="--root"
434 upstream_arg="$root_flag"
437 # Make sure the branch to rebase onto is valid.
438 onto_name=${onto-"$upstream_name"}
439 case "$onto_name" in
440 *...*)
441 if left=${onto_name%...*} right=${onto_name#*...} &&
442 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
443 then
444 case "$onto" in
445 ?*"$LF"?*)
446 die "$onto_name: there are more than one merge bases"
449 die "$onto_name: there is no merge base"
451 esac
452 else
453 die "$onto_name: there is no merge base"
457 onto=$(git rev-parse --verify "${onto_name}^0") || exit
459 esac
461 # If a hook exists, give it a chance to interrupt
462 run_pre_rebase_hook "$upstream_arg" "$@"
464 # If the branch to rebase is given, that is the branch we will rebase
465 # $branch_name -- branch being rebased, or HEAD (already detached)
466 # $orig_head -- commit object name of tip of the branch before rebasing
467 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
468 switch_to=
469 case "$#" in
471 # Is it "rebase other $branchname" or "rebase other $commit"?
472 branch_name="$1"
473 switch_to="$1"
475 if git show-ref --verify --quiet -- "refs/heads/$1" &&
476 branch=$(git rev-parse -q --verify "refs/heads/$1")
477 then
478 head_name="refs/heads/$1"
479 elif branch=$(git rev-parse -q --verify "$1")
480 then
481 head_name="detached HEAD"
482 else
483 echo >&2 "fatal: no such branch: $1"
484 usage
488 # Do not need to switch branches, we are already on it.
489 if branch_name=`git symbolic-ref -q HEAD`
490 then
491 head_name=$branch_name
492 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
493 else
494 head_name="detached HEAD"
495 branch_name=HEAD ;# detached
497 branch=$(git rev-parse --verify "${branch_name}^0") || exit
499 esac
500 orig_head=$branch
502 # Now we are rebasing commits $upstream..$branch (or with --root,
503 # everything leading up to $branch) on top of $onto
505 # Check if we are already based on $onto with linear history,
506 # but this should be done only when upstream and onto are the same.
507 mb=$(git merge-base "$onto" "$branch")
508 if test "$upstream" = "$onto" && test "$mb" = "$onto" &&
509 # linear history?
510 ! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null
511 then
512 if test -z "$force_rebase"
513 then
514 # Lazily switch to the target branch if needed...
515 test -z "$switch_to" || git checkout "$switch_to" --
516 say "Current branch $branch_name is up to date."
517 exit 0
518 else
519 say "Current branch $branch_name is up to date, rebase forced."
523 # Detach HEAD and reset the tree
524 say "First, rewinding head to replay your work on top of it..."
525 git checkout -q "$onto^0" || die "could not detach HEAD"
526 git update-ref ORIG_HEAD $branch
528 if test -n "$diffstat"
529 then
530 if test -n "$verbose"
531 then
532 echo "Changes from $mb to $onto:"
534 # We want color (if set), but no pager
535 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
538 # If the $onto is a proper descendant of the tip of the branch, then
539 # we just fast-forwarded.
540 if test "$mb" = "$branch"
541 then
542 say "Fast-forwarded $branch_name to $onto_name."
543 move_to_original_branch
544 exit 0
547 if test -n "$rebase_root"
548 then
549 revisions="$onto..$orig_head"
550 else
551 revisions="$upstream..$orig_head"
554 if test -z "$do_merge"
555 then
556 git format-patch -k --stdout --full-index --ignore-if-in-upstream \
557 --src-prefix=a/ --dst-prefix=b/ \
558 --no-renames $root_flag "$revisions" |
559 git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" &&
560 move_to_original_branch
561 ret=$?
562 test 0 != $ret -a -d "$apply_dir" &&
563 echo $head_name > "$apply_dir/head-name" &&
564 echo $onto > "$apply_dir/onto" &&
565 echo $orig_head > "$apply_dir/orig-head" &&
566 echo "$GIT_QUIET" > "$apply_dir/quiet"
567 exit $ret
570 # start doing a rebase with git-merge
571 # this is rename-aware if the recursive (default) strategy is used
573 mkdir -p "$merge_dir"
574 echo "$onto_name" > "$merge_dir/onto_name"
575 echo "$head_name" > "$merge_dir/head-name"
576 echo "$onto" > "$merge_dir/onto"
577 echo "$orig_head" > "$merge_dir/orig-head"
578 echo "$GIT_QUIET" > "$merge_dir/quiet"
580 msgnum=0
581 for cmt in `git rev-list --reverse --no-merges "$revisions"`
583 msgnum=$(($msgnum + 1))
584 echo "$cmt" > "$merge_dir/cmt.$msgnum"
585 done
587 echo 1 >"$merge_dir/msgnum"
588 echo $msgnum >"$merge_dir/end"
590 end=$msgnum
591 msgnum=1
593 while test "$msgnum" -le "$end"
595 call_merge "$msgnum"
596 continue_merge
597 done
599 finish_rb_merge