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