rebase -i: test "Nothing to do" case with autostash
[git/debian.git] / git-rebase.sh
blob9eb276ba203ae552d1b64fe49814b7e8d8f91e1b
1 #!/bin/sh
3 # Copyright (c) 2005 Junio C Hamano.
6 SUBDIRECTORY_OK=Yes
7 OPTIONS_KEEPDASHDASH=
8 OPTIONS_SPEC="\
9 git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
10 git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
11 git-rebase --continue | --abort | --skip | --edit-todo
13 Available options are
14 v,verbose! display a diffstat of what changed upstream
15 q,quiet! be quiet. implies --no-stat
16 autostash! automatically stash/stash pop before and after
17 onto=! rebase onto given branch instead of upstream
18 p,preserve-merges! try to recreate merges instead of ignoring them
19 s,strategy=! use the given merge strategy
20 no-ff! cherry-pick all commits, even if unchanged
21 m,merge! use merging strategies to rebase
22 i,interactive! let the user edit the list of commits to rebase
23 x,exec=! add exec lines after each commit of the editable list
24 k,keep-empty preserve empty commits during rebase
25 f,force-rebase! force rebase even if branch is up to date
26 X,strategy-option=! pass the argument through to the merge strategy
27 stat! display a diffstat of what changed upstream
28 n,no-stat! do not show diffstat of what changed upstream
29 verify allow pre-rebase hook to run
30 rerere-autoupdate allow rerere to update index with resolved conflicts
31 root! rebase all reachable commits up to the root(s)
32 autosquash move commits that begin with squash!/fixup! under -i
33 committer-date-is-author-date! passed to 'git am'
34 ignore-date! passed to 'git am'
35 whitespace=! passed to 'git apply'
36 ignore-whitespace! passed to 'git apply'
37 C=! passed to 'git apply'
38 Actions:
39 continue! continue
40 abort! abort and check out the original branch
41 skip! skip current patch and continue
42 edit-todo! edit the todo list during an interactive rebase
44 . git-sh-setup
45 . git-sh-i18n
46 set_reflog_action rebase
47 require_work_tree_exists
48 cd_to_toplevel
50 LF='
52 ok_to_skip_pre_rebase=
53 resolvemsg="
54 $(gettext 'When you have resolved this problem, run "git rebase --continue".
55 If you prefer to skip this patch, run "git rebase --skip" instead.
56 To check out the original branch and stop rebasing, run "git rebase --abort".')
58 unset onto
59 cmd=
60 strategy=
61 strategy_opts=
62 do_merge=
63 merge_dir="$GIT_DIR"/rebase-merge
64 apply_dir="$GIT_DIR"/rebase-apply
65 verbose=
66 diffstat=
67 test "$(git config --bool rebase.stat)" = true && diffstat=t
68 autostash="$(git config --bool rebase.autostash || echo false)"
69 git_am_opt=
70 rebase_root=
71 force_rebase=
72 allow_rerere_autoupdate=
73 # Non-empty if a rebase was in progress when 'git rebase' was invoked
74 in_progress=
75 # One of {am, merge, interactive}
76 type=
77 # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
78 state_dir=
79 # One of {'', continue, skip, abort}, as parsed from command line
80 action=
81 preserve_merges=
82 autosquash=
83 keep_empty=
84 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
86 read_basic_state () {
87 test -f "$state_dir/head-name" &&
88 test -f "$state_dir/onto" &&
89 head_name=$(cat "$state_dir"/head-name) &&
90 onto=$(cat "$state_dir"/onto) &&
91 # We always write to orig-head, but interactive rebase used to write to
92 # head. Fall back to reading from head to cover for the case that the
93 # user upgraded git with an ongoing interactive rebase.
94 if test -f "$state_dir"/orig-head
95 then
96 orig_head=$(cat "$state_dir"/orig-head)
97 else
98 orig_head=$(cat "$state_dir"/head)
99 fi &&
100 GIT_QUIET=$(cat "$state_dir"/quiet) &&
101 test -f "$state_dir"/verbose && verbose=t
102 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
103 test -f "$state_dir"/strategy_opts &&
104 strategy_opts="$(cat "$state_dir"/strategy_opts)"
105 test -f "$state_dir"/allow_rerere_autoupdate &&
106 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
109 write_basic_state () {
110 echo "$head_name" > "$state_dir"/head-name &&
111 echo "$onto" > "$state_dir"/onto &&
112 echo "$orig_head" > "$state_dir"/orig-head &&
113 echo "$GIT_QUIET" > "$state_dir"/quiet &&
114 test t = "$verbose" && : > "$state_dir"/verbose
115 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
116 test -n "$strategy_opts" && echo "$strategy_opts" > \
117 "$state_dir"/strategy_opts
118 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
119 "$state_dir"/allow_rerere_autoupdate
122 output () {
123 case "$verbose" in
125 output=$("$@" 2>&1 )
126 status=$?
127 test $status != 0 && printf "%s\n" "$output"
128 return $status
131 "$@"
133 esac
136 move_to_original_branch () {
137 case "$head_name" in
138 refs/*)
139 message="rebase finished: $head_name onto $onto"
140 git update-ref -m "$message" \
141 $head_name $(git rev-parse HEAD) $orig_head &&
142 git symbolic-ref \
143 -m "rebase finished: returning to $head_name" \
144 HEAD $head_name ||
145 die "$(gettext "Could not move back to $head_name")"
147 esac
150 apply_autostash () {
151 if test -f "$state_dir/autostash"
152 then
153 stash_sha1=$(cat "$state_dir/autostash")
154 if git stash apply $stash_sha1 2>&1 >/dev/null
155 then
156 echo "$(gettext 'Applied autostash.')"
157 else
158 ref_stash=refs/stash &&
159 >>"$GIT_DIR/logs/$ref_stash" &&
160 git update-ref -m "autostash" $ref_stash $stash_sha1 ||
161 die "$(eval_gettext 'Cannot store $stash_sha1')"
163 gettext 'Applying autostash resulted in conflicts.
164 Your changes are safe in the stash.
165 You can run "git stash pop" or "git stash drop" at any time.
171 finish_rebase () {
172 apply_autostash &&
173 git gc --auto &&
174 rm -rf "$state_dir"
177 run_specific_rebase () {
178 if [ "$interactive_rebase" = implied ]; then
179 GIT_EDITOR=:
180 export GIT_EDITOR
181 autosquash=
183 . git-rebase--$type
184 ret=$?
185 if test $ret -eq 0
186 then
187 finish_rebase
188 elif test $ret -eq 2 # special exit status for rebase -i
189 then
190 apply_autostash &&
191 rm -rf "$state_dir" &&
192 die "Nothing to do"
194 exit $ret
197 run_pre_rebase_hook () {
198 if test -z "$ok_to_skip_pre_rebase" &&
199 test -x "$GIT_DIR/hooks/pre-rebase"
200 then
201 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
202 die "$(gettext "The pre-rebase hook refused to rebase.")"
206 test -f "$apply_dir"/applying &&
207 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
209 if test -d "$apply_dir"
210 then
211 type=am
212 state_dir="$apply_dir"
213 elif test -d "$merge_dir"
214 then
215 if test -f "$merge_dir"/interactive
216 then
217 type=interactive
218 interactive_rebase=explicit
219 else
220 type=merge
222 state_dir="$merge_dir"
224 test -n "$type" && in_progress=t
226 total_argc=$#
227 while test $# != 0
229 case "$1" in
230 --no-verify)
231 ok_to_skip_pre_rebase=yes
233 --verify)
234 ok_to_skip_pre_rebase=
236 --continue|--skip|--abort|--edit-todo)
237 test $total_argc -eq 2 || usage
238 action=${1##--}
240 --onto)
241 test 2 -le "$#" || usage
242 onto="$2"
243 shift
246 test 2 -le "$#" || usage
247 cmd="${cmd}exec $2${LF}"
248 shift
251 interactive_rebase=explicit
254 keep_empty=yes
257 preserve_merges=t
258 test -z "$interactive_rebase" && interactive_rebase=implied
260 --autosquash)
261 autosquash=t
263 --no-autosquash)
264 autosquash=
266 -M|-m)
267 do_merge=t
270 shift
271 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$1")"
272 do_merge=t
273 test -z "$strategy" && strategy=recursive
276 shift
277 strategy="$1"
278 do_merge=t
281 diffstat=
283 --stat)
284 diffstat=t
286 --autostash)
287 autostash=true
290 verbose=t
291 diffstat=t
292 GIT_QUIET=
295 GIT_QUIET=t
296 git_am_opt="$git_am_opt -q"
297 verbose=
298 diffstat=
300 --whitespace)
301 shift
302 git_am_opt="$git_am_opt --whitespace=$1"
303 case "$1" in
304 fix|strip)
305 force_rebase=t
307 esac
309 --ignore-whitespace)
310 git_am_opt="$git_am_opt $1"
312 --committer-date-is-author-date|--ignore-date)
313 git_am_opt="$git_am_opt $1"
314 force_rebase=t
317 shift
318 git_am_opt="$git_am_opt -C$1"
320 --root)
321 rebase_root=t
323 -f|--no-ff)
324 force_rebase=t
326 --rerere-autoupdate|--no-rerere-autoupdate)
327 allow_rerere_autoupdate="$1"
330 shift
331 break
333 esac
334 shift
335 done
336 test $# -gt 2 && usage
338 if test -n "$cmd" &&
339 test "$interactive_rebase" != explicit
340 then
341 die "$(gettext "The --exec option must be used with the --interactive option")"
344 if test -n "$action"
345 then
346 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
347 # Only interactive rebase uses detailed reflog messages
348 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
349 then
350 GIT_REFLOG_ACTION="rebase -i ($action)"
351 export GIT_REFLOG_ACTION
355 if test "$action" = "edit-todo" && test "$type" != "interactive"
356 then
357 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
360 case "$action" in
361 continue)
362 # Sanity check
363 git rev-parse --verify HEAD >/dev/null ||
364 die "$(gettext "Cannot read HEAD")"
365 git update-index --ignore-submodules --refresh &&
366 git diff-files --quiet --ignore-submodules || {
367 echo "$(gettext "You must edit all merge conflicts and then
368 mark them as resolved using git add")"
369 exit 1
371 read_basic_state
372 run_specific_rebase
374 skip)
375 output git reset --hard HEAD || exit $?
376 read_basic_state
377 run_specific_rebase
379 abort)
380 git rerere clear
381 read_basic_state
382 case "$head_name" in
383 refs/*)
384 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
385 die "$(eval_gettext "Could not move back to \$head_name")"
387 esac
388 output git reset --hard $orig_head
389 finish_rebase
390 exit
392 edit-todo)
393 run_specific_rebase
395 esac
397 # Make sure no rebase is in progress
398 if test -n "$in_progress"
399 then
400 state_dir_base=${state_dir##*/}
401 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
402 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
403 die "
404 $(eval_gettext 'It seems that there is already a $state_dir_base directory, and
405 I wonder if you are in the middle of another rebase. If that is the
406 case, please try
407 $cmd_live_rebase
408 If that is not the case, please
409 $cmd_clear_stale_rebase
410 and run me again. I am stopping in case you still have something
411 valuable there.')"
414 if test -n "$rebase_root" && test -z "$onto"
415 then
416 test -z "$interactive_rebase" && interactive_rebase=implied
419 if test -n "$interactive_rebase"
420 then
421 type=interactive
422 state_dir="$merge_dir"
423 elif test -n "$do_merge"
424 then
425 type=merge
426 state_dir="$merge_dir"
427 else
428 type=am
429 state_dir="$apply_dir"
432 if test -z "$rebase_root"
433 then
434 case "$#" in
436 if ! upstream_name=$(git rev-parse --symbolic-full-name \
437 --verify -q @{upstream} 2>/dev/null)
438 then
439 . git-parse-remote
440 error_on_missing_default_upstream "rebase" "rebase" \
441 "against" "git rebase <branch>"
444 *) upstream_name="$1"
445 shift
447 esac
448 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
449 die "$(eval_gettext "invalid upstream \$upstream_name")"
450 upstream_arg="$upstream_name"
451 else
452 if test -z "$onto"
453 then
454 empty_tree=`git hash-object -t tree /dev/null`
455 onto=`git commit-tree $empty_tree </dev/null`
456 squash_onto="$onto"
458 unset upstream_name
459 unset upstream
460 test $# -gt 1 && usage
461 upstream_arg=--root
464 # Make sure the branch to rebase onto is valid.
465 onto_name=${onto-"$upstream_name"}
466 case "$onto_name" in
467 *...*)
468 if left=${onto_name%...*} right=${onto_name#*...} &&
469 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
470 then
471 case "$onto" in
472 ?*"$LF"?*)
473 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
476 die "$(eval_gettext "\$onto_name: there is no merge base")"
478 esac
479 else
480 die "$(eval_gettext "\$onto_name: there is no merge base")"
484 onto=$(git rev-parse --verify "${onto_name}^0") ||
485 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
487 esac
489 # If the branch to rebase is given, that is the branch we will rebase
490 # $branch_name -- branch being rebased, or HEAD (already detached)
491 # $orig_head -- commit object name of tip of the branch before rebasing
492 # $head_name -- refs/heads/<that-branch> or "detached HEAD"
493 switch_to=
494 case "$#" in
496 # Is it "rebase other $branchname" or "rebase other $commit"?
497 branch_name="$1"
498 switch_to="$1"
500 if git show-ref --verify --quiet -- "refs/heads/$1" &&
501 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
502 then
503 head_name="refs/heads/$1"
504 elif orig_head=$(git rev-parse -q --verify "$1")
505 then
506 head_name="detached HEAD"
507 else
508 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
512 # Do not need to switch branches, we are already on it.
513 if branch_name=`git symbolic-ref -q HEAD`
514 then
515 head_name=$branch_name
516 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
517 else
518 head_name="detached HEAD"
519 branch_name=HEAD ;# detached
521 orig_head=$(git rev-parse --verify HEAD) || exit
524 die "BUG: unexpected number of arguments left to parse"
526 esac
528 if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
529 then
530 stash_sha1=$(git stash create "autostash") ||
531 die "$(gettext 'Cannot autostash')"
533 mkdir -p "$state_dir" &&
534 echo $stash_sha1 >"$state_dir/autostash" &&
535 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
536 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
537 git reset --hard
540 require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
542 # Now we are rebasing commits $upstream..$orig_head (or with --root,
543 # everything leading up to $orig_head) on top of $onto
545 # Check if we are already based on $onto with linear history,
546 # but this should be done only when upstream and onto are the same
547 # and if this is not an interactive rebase.
548 mb=$(git merge-base "$onto" "$orig_head")
549 if test "$type" != interactive && test "$upstream" = "$onto" &&
550 test "$mb" = "$onto" &&
551 # linear history?
552 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
553 then
554 if test -z "$force_rebase"
555 then
556 # Lazily switch to the target branch if needed...
557 test -z "$switch_to" || git checkout "$switch_to" --
558 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
559 finish_rebase
560 exit 0
561 else
562 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
566 # If a hook exists, give it a chance to interrupt
567 run_pre_rebase_hook "$upstream_arg" "$@"
569 if test -n "$diffstat"
570 then
571 if test -n "$verbose"
572 then
573 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
575 # We want color (if set), but no pager
576 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
579 test "$type" = interactive && run_specific_rebase
581 # Detach HEAD and reset the tree
582 say "$(gettext "First, rewinding head to replay your work on top of it...")"
583 git checkout -q "$onto^0" || die "could not detach HEAD"
584 git update-ref ORIG_HEAD $orig_head
586 # If the $onto is a proper descendant of the tip of the branch, then
587 # we just fast-forwarded.
588 if test "$mb" = "$orig_head"
589 then
590 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
591 move_to_original_branch
592 finish_rebase
593 exit 0
596 if test -n "$rebase_root"
597 then
598 revisions="$onto..$orig_head"
599 else
600 revisions="$upstream..$orig_head"
603 run_specific_rebase