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