]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase.sh
Sync with 2.16.6
[thirdparty/git.git] / git-rebase.sh
CommitLineData
59e6b23a
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
533b7039 6SUBDIRECTORY_OK=Yes
45e2acf3 7OPTIONS_KEEPDASHDASH=
b6e9e73e 8OPTIONS_STUCKLONG=t
45e2acf3 9OPTIONS_SPEC="\
c2145384 10git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
0cd993a7 11git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
82cb775c 12git rebase --continue | --abort | --skip | --edit-todo
45e2acf3
MZ
13--
14 Available options are
15v,verbose! display a diffstat of what changed upstream
16q,quiet! be quiet. implies --no-stat
619e3604 17autostash automatically stash/stash pop before and after
ad8261d2 18fork-point use 'merge-base --fork-point' to refine upstream
45e2acf3
MZ
19onto=! rebase onto given branch instead of upstream
20p,preserve-merges! try to recreate merges instead of ignoring them
21s,strategy=! use the given merge strategy
22no-ff! cherry-pick all commits, even if unchanged
23m,merge! use merging strategies to rebase
24i,interactive! let the user edit the list of commits to rebase
c2145384 25x,exec=! add exec lines after each commit of the editable list
90e1818f 26k,keep-empty preserve empty commits during rebase
a6c612b5 27allow-empty-message allow rebasing commits with empty messages
45e2acf3
MZ
28f,force-rebase! force rebase even if branch is up to date
29X,strategy-option=! pass the argument through to the merge strategy
30stat! display a diffstat of what changed upstream
31n,no-stat! do not show diffstat of what changed upstream
32verify allow pre-rebase hook to run
33rerere-autoupdate allow rerere to update index with resolved conflicts
34root! rebase all reachable commits up to the root(s)
35autosquash move commits that begin with squash!/fixup! under -i
36committer-date-is-author-date! passed to 'git am'
37ignore-date! passed to 'git am'
9f79524a 38signoff passed to 'git am'
45e2acf3
MZ
39whitespace=! passed to 'git apply'
40ignore-whitespace! passed to 'git apply'
41C=! passed to 'git apply'
3ee5e540 42S,gpg-sign? GPG-sign commits
45e2acf3 43 Actions:
5960bc9d
MZ
44continue! continue
45abort! abort and check out the original branch
46skip! skip current patch and continue
eb9a7cb4 47edit-todo! edit the todo list during an interactive rebase
9512177b 48quit! abort but keep HEAD where it is
66335298 49show-current-patch! show the patch file being applied or merged
45e2acf3 50"
ae2b0f15 51. git-sh-setup
f9474132 52set_reflog_action rebase
035b5bf6 53require_work_tree_exists
533b7039 54cd_to_toplevel
4282c4fb 55
61dfa1bb
JH
56LF='
57'
6bb4e485
MZ
58ok_to_skip_pre_rebase=
59resolvemsg="
5fdacc17
WD
60$(gettext 'Resolve all conflicts manually, mark them as resolved with
61"git add/rm <conflicted_files>", then run "git rebase --continue".
62You can instead skip this commit: run "git rebase --skip".
63To abort and get back to the state before "git rebase", run "git rebase --abort".')
cc120056 64"
6bb4e485 65unset onto
1e0dacdb 66unset restrict_revision
c2145384 67cmd=
9765b6ab 68strategy=
93ce190c 69strategy_opts=
58634dbf 70do_merge=
69a636ad
MZ
71merge_dir="$GIT_DIR"/rebase-merge
72apply_dir="$GIT_DIR"/rebase-apply
b758789c 73verbose=
9474a029
MZ
74diffstat=
75test "$(git config --bool rebase.stat)" = true && diffstat=t
58794775 76autostash="$(git config --bool rebase.autostash || echo false)"
ad8261d2 77fork_point=auto
67dad687 78git_am_opt=
9eaa858e 79git_format_patch_opt=
190f5323 80rebase_root=
b2f82e05 81force_rebase=
cb6020bb 82allow_rerere_autoupdate=
99de0640
MZ
83# Non-empty if a rebase was in progress when 'git rebase' was invoked
84in_progress=
85# One of {am, merge, interactive}
86type=
87# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
88state_dir=
34262322
MZ
89# One of {'', continue, skip, abort}, as parsed from command line
90action=
cf432ca0
MZ
91preserve_merges=
92autosquash=
90e1818f 93keep_empty=
a6c612b5 94allow_empty_message=
cf432ca0 95test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
66948561
JH
96case "$(git config --bool commit.gpgsign)" in
97true) gpg_sign_opt=-S ;;
98*) gpg_sign_opt= ;;
99esac
58634dbf 100
fa99c1e1 101read_basic_state () {
dc8ca912
RR
102 test -f "$state_dir/head-name" &&
103 test -f "$state_dir/onto" &&
02ac45fa
MZ
104 head_name=$(cat "$state_dir"/head-name) &&
105 onto=$(cat "$state_dir"/onto) &&
84df4560
MZ
106 # We always write to orig-head, but interactive rebase used to write to
107 # head. Fall back to reading from head to cover for the case that the
108 # user upgraded git with an ongoing interactive rebase.
109 if test -f "$state_dir"/orig-head
2959c283 110 then
2959c283 111 orig_head=$(cat "$state_dir"/orig-head)
84df4560
MZ
112 else
113 orig_head=$(cat "$state_dir"/head)
2959c283 114 fi &&
7b37a7c6
MZ
115 GIT_QUIET=$(cat "$state_dir"/quiet) &&
116 test -f "$state_dir"/verbose && verbose=t
80ff4795
MZ
117 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
118 test -f "$state_dir"/strategy_opts &&
119 strategy_opts="$(cat "$state_dir"/strategy_opts)"
b3e4847e
MZ
120 test -f "$state_dir"/allow_rerere_autoupdate &&
121 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
3ee5e540
NV
122 test -f "$state_dir"/gpg_sign_opt &&
123 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
02ac45fa
MZ
124}
125
84df4560
MZ
126write_basic_state () {
127 echo "$head_name" > "$state_dir"/head-name &&
128 echo "$onto" > "$state_dir"/onto &&
129 echo "$orig_head" > "$state_dir"/orig-head &&
7b37a7c6
MZ
130 echo "$GIT_QUIET" > "$state_dir"/quiet &&
131 test t = "$verbose" && : > "$state_dir"/verbose
80ff4795
MZ
132 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
133 test -n "$strategy_opts" && echo "$strategy_opts" > \
134 "$state_dir"/strategy_opts
b3e4847e
MZ
135 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
136 "$state_dir"/allow_rerere_autoupdate
3ee5e540 137 test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
84df4560
MZ
138}
139
4974c2ca
MZ
140output () {
141 case "$verbose" in
142 '')
143 output=$("$@" 2>&1 )
144 status=$?
145 test $status != 0 && printf "%s\n" "$output"
146 return $status
147 ;;
148 *)
149 "$@"
150 ;;
151 esac
152}
153
6fd2f5e6 154move_to_original_branch () {
6fd2f5e6
JS
155 case "$head_name" in
156 refs/*)
157 message="rebase finished: $head_name onto $onto"
158 git update-ref -m "$message" \
159 $head_name $(git rev-parse HEAD) $orig_head &&
53f2ffa8
JK
160 git symbolic-ref \
161 -m "rebase finished: returning to $head_name" \
162 HEAD $head_name ||
24a6df48 163 die "$(eval_gettext "Could not move back to \$head_name")"
6fd2f5e6
JS
164 ;;
165 esac
166}
167
e4244eb3 168apply_autostash () {
58794775
RR
169 if test -f "$state_dir/autostash"
170 then
171 stash_sha1=$(cat "$state_dir/autostash")
eadf1c8f 172 if git stash apply $stash_sha1 >/dev/null 2>&1
58794775 173 then
cdb866b3 174 echo "$(gettext 'Applied autostash.')" >&2
58794775 175 else
20351bb0
RR
176 git stash store -m "autostash" -q $stash_sha1 ||
177 die "$(eval_gettext "Cannot store \$stash_sha1")"
58794775
RR
178 gettext 'Applying autostash resulted in conflicts.
179Your changes are safe in the stash.
ac1998de 180You can run "git stash pop" or "git stash drop" at any time.
cdb866b3 181' >&2
58794775
RR
182 fi
183 fi
e4244eb3
RR
184}
185
186finish_rebase () {
fbd7a232 187 rm -f "$(git rev-parse --git-path REBASE_HEAD)"
e4244eb3 188 apply_autostash &&
8c24f5b0 189 { git gc --auto || true; } &&
58794775
RR
190 rm -rf "$state_dir"
191}
192
8cd65967 193run_specific_rebase () {
f8cca019
AE
194 if [ "$interactive_rebase" = implied ]; then
195 GIT_EDITOR=:
196 export GIT_EDITOR
8a6dae10 197 autosquash=
f8cca019 198 fi
46df82d5 199 . git-rebase--$type
f5f758a5
RR
200 ret=$?
201 if test $ret -eq 0
202 then
58794775 203 finish_rebase
e4244eb3
RR
204 elif test $ret -eq 2 # special exit status for rebase -i
205 then
206 apply_autostash &&
207 rm -rf "$state_dir" &&
208 die "Nothing to do"
f5f758a5
RR
209 fi
210 exit $ret
1b1dce4b
JS
211}
212
d70b4a8f 213run_pre_rebase_hook () {
6bb4e485 214 if test -z "$ok_to_skip_pre_rebase" &&
b849b954 215 test -x "$(git rev-parse --git-path hooks/pre-rebase)"
d70b4a8f 216 then
b849b954 217 "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
c7108bf9 218 die "$(gettext "The pre-rebase hook refused to rebase.")"
d70b4a8f
NS
219 fi
220}
221
69a636ad 222test -f "$apply_dir"/applying &&
82cb775c 223 die "$(gettext "It looks like 'git am' is in progress. Cannot rebase.")"
9b752a6e 224
99de0640
MZ
225if test -d "$apply_dir"
226then
227 type=am
228 state_dir="$apply_dir"
229elif test -d "$merge_dir"
230then
231 if test -f "$merge_dir"/interactive
232 then
233 type=interactive
234 interactive_rebase=explicit
235 else
236 type=merge
237 fi
238 state_dir="$merge_dir"
239fi
240test -n "$type" && in_progress=t
241
95135b06 242total_argc=$#
822f7c73 243while test $# != 0
e646c9c8
JH
244do
245 case "$1" in
c4427656 246 --no-verify)
6bb4e485 247 ok_to_skip_pre_rebase=yes
c4427656 248 ;;
7baf9c4b 249 --verify)
6bb4e485 250 ok_to_skip_pre_rebase=
7baf9c4b 251 ;;
66335298 252 --continue|--skip|--abort|--quit|--edit-todo|--show-current-patch)
45e2acf3 253 test $total_argc -eq 2 || usage
34262322 254 action=${1##--}
031321c6 255 ;;
b6e9e73e
NV
256 --onto=*)
257 onto="${1#--onto=}"
e646c9c8 258 ;;
b6e9e73e
NV
259 --exec=*)
260 cmd="${cmd}exec ${1#--exec=}${LF}"
78ec2400 261 test -z "$interactive_rebase" && interactive_rebase=implied
c2145384 262 ;;
b6e9e73e 263 --interactive)
cf432ca0
MZ
264 interactive_rebase=explicit
265 ;;
b6e9e73e 266 --keep-empty)
90e1818f
NH
267 keep_empty=yes
268 ;;
a6c612b5
GS
269 --allow-empty-message)
270 allow_empty_message=--allow-empty-message
271 ;;
b6e9e73e 272 --preserve-merges)
cf432ca0
MZ
273 preserve_merges=t
274 test -z "$interactive_rebase" && interactive_rebase=implied
275 ;;
276 --autosquash)
277 autosquash=t
278 ;;
279 --no-autosquash)
280 autosquash=
281 ;;
ad8261d2
JK
282 --fork-point)
283 fork_point=t
284 ;;
285 --no-fork-point)
286 fork_point=
287 ;;
b6e9e73e 288 --merge)
58634dbf
EW
289 do_merge=t
290 ;;
b6e9e73e
NV
291 --strategy-option=*)
292 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
93ce190c 293 do_merge=t
9765b6ab 294 test -z "$strategy" && strategy=recursive
93ce190c 295 ;;
b6e9e73e
NV
296 --strategy=*)
297 strategy="${1#--strategy=}"
58634dbf
EW
298 do_merge=t
299 ;;
b6e9e73e 300 --no-stat)
a9c3821c
TAV
301 diffstat=
302 ;;
303 --stat)
304 diffstat=t
305 ;;
58794775
RR
306 --autostash)
307 autostash=true
308 ;;
619e3604
JK
309 --no-autostash)
310 autostash=false
311 ;;
b6e9e73e 312 --verbose)
b758789c 313 verbose=t
a9c3821c 314 diffstat=t
0e987a12
SB
315 GIT_QUIET=
316 ;;
b6e9e73e 317 --quiet)
0e987a12
SB
318 GIT_QUIET=t
319 git_am_opt="$git_am_opt -q"
320 verbose=
321 diffstat=
b758789c 322 ;;
b6e9e73e
NV
323 --whitespace=*)
324 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
325 case "${1#--whitespace=}" in
45e2acf3 326 fix|strip)
b2f82e05
SR
327 force_rebase=t
328 ;;
329 esac
059f446d 330 ;;
86c91f91
GB
331 --ignore-whitespace)
332 git_am_opt="$git_am_opt $1"
333 ;;
9f79524a 334 --committer-date-is-author-date|--ignore-date|--signoff|--no-signoff)
570ccad3
MB
335 git_am_opt="$git_am_opt $1"
336 force_rebase=t
337 ;;
b6e9e73e
NV
338 -C*)
339 git_am_opt="$git_am_opt $1"
67dad687 340 ;;
190f5323
TR
341 --root)
342 rebase_root=t
343 ;;
b6e9e73e 344 --force-rebase|--no-ff)
b2f82e05
SR
345 force_rebase=t
346 ;;
cb6020bb
JH
347 --rerere-autoupdate|--no-rerere-autoupdate)
348 allow_rerere_autoupdate="$1"
349 ;;
3ee5e540
NV
350 --gpg-sign)
351 gpg_sign_opt=-S
352 ;;
353 --gpg-sign=*)
354 gpg_sign_opt="-S${1#--gpg-sign=}"
355 ;;
45e2acf3
MZ
356 --)
357 shift
e646c9c8
JH
358 break
359 ;;
697bc885
BC
360 *)
361 usage
362 ;;
e646c9c8
JH
363 esac
364 shift
365done
51b2ead0 366test $# -gt 2 && usage
2db8aaec 367
cf432ca0
MZ
368if test -n "$action"
369then
c7108bf9 370 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
2959c283
MZ
371 # Only interactive rebase uses detailed reflog messages
372 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
373 then
374 GIT_REFLOG_ACTION="rebase -i ($action)"
375 export GIT_REFLOG_ACTION
376 fi
cf432ca0 377fi
34262322 378
eb9a7cb4
AW
379if test "$action" = "edit-todo" && test "$type" != "interactive"
380then
381 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
382fi
383
34262322
MZ
384case "$action" in
385continue)
2959c283
MZ
386 # Sanity check
387 git rev-parse --verify HEAD >/dev/null ||
c7108bf9 388 die "$(gettext "Cannot read HEAD")"
34262322
MZ
389 git update-index --ignore-submodules --refresh &&
390 git diff-files --quiet --ignore-submodules || {
c7108bf9
JX
391 echo "$(gettext "You must edit all merge conflicts and then
392mark them as resolved using git add")"
34262322
MZ
393 exit 1
394 }
fa99c1e1
MZ
395 read_basic_state
396 run_specific_rebase
34262322
MZ
397 ;;
398skip)
4974c2ca 399 output git reset --hard HEAD || exit $?
fa99c1e1
MZ
400 read_basic_state
401 run_specific_rebase
34262322
MZ
402 ;;
403abort)
404 git rerere clear
fa99c1e1 405 read_basic_state
34262322
MZ
406 case "$head_name" in
407 refs/*)
ea69619c 408 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
c7108bf9 409 die "$(eval_gettext "Could not move back to \$head_name")"
34262322
MZ
410 ;;
411 esac
4974c2ca 412 output git reset --hard $orig_head
58794775 413 finish_rebase
34262322
MZ
414 exit
415 ;;
9512177b
NTND
416quit)
417 exec rm -rf "$state_dir"
418 ;;
eb9a7cb4
AW
419edit-todo)
420 run_specific_rebase
421 ;;
66335298
NTND
422show-current-patch)
423 run_specific_rebase
424 die "BUG: run_specific_rebase is not supposed to return here"
425 ;;
34262322
MZ
426esac
427
99de0640
MZ
428# Make sure no rebase is in progress
429if test -n "$in_progress"
7f4bd5d8 430then
c7108bf9
JX
431 state_dir_base=${state_dir##*/}
432 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
433 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
434 die "
435$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
e39beac6 436I wonder if you are in the middle of another rebase. If that is the
99de0640 437case, please try
c7108bf9 438 $cmd_live_rebase
99de0640 439If that is not the case, please
c7108bf9 440 $cmd_clear_stale_rebase
9b752a6e 441and run me again. I am stopping in case you still have something
c7108bf9 442valuable there.')"
7f4bd5d8
JH
443fi
444
df5df20c
CW
445if test -n "$rebase_root" && test -z "$onto"
446then
447 test -z "$interactive_rebase" && interactive_rebase=implied
448fi
449
cf432ca0
MZ
450if test -n "$interactive_rebase"
451then
452 type=interactive
453 state_dir="$merge_dir"
454elif test -n "$do_merge"
455then
456 type=merge
457 state_dir="$merge_dir"
458else
459 type=am
460 state_dir="$apply_dir"
461fi
462
9eaa858e
KW
463if test -t 2 && test -z "$GIT_QUIET"
464then
465 git_format_patch_opt="$git_format_patch_opt --progress"
466fi
467
190f5323
TR
468if test -z "$rebase_root"
469then
15a147e6
MZ
470 case "$#" in
471 0)
472 if ! upstream_name=$(git rev-parse --symbolic-full-name \
473 --verify -q @{upstream} 2>/dev/null)
474 then
475 . git-parse-remote
476 error_on_missing_default_upstream "rebase" "rebase" \
c36d8eee 477 "against" "git rebase $(gettext '<branch>')"
15a147e6 478 fi
ad8261d2
JK
479
480 test "$fork_point" = auto && fork_point=t
15a147e6
MZ
481 ;;
482 *) upstream_name="$1"
4f407407
BG
483 if test "$upstream_name" = "-"
484 then
485 upstream_name="@{-1}"
486 fi
15a147e6
MZ
487 shift
488 ;;
489 esac
2e6e276d 490 upstream=$(peel_committish "${upstream_name}") ||
ca7de7b1 491 die "$(eval_gettext "invalid upstream '\$upstream_name'")"
190f5323
TR
492 upstream_arg="$upstream_name"
493else
df5df20c
CW
494 if test -z "$onto"
495 then
728fc79c
EP
496 empty_tree=$(git hash-object -t tree /dev/null)
497 onto=$(git commit-tree $empty_tree </dev/null)
df5df20c
CW
498 squash_onto="$onto"
499 fi
190f5323
TR
500 unset upstream_name
501 unset upstream
f2b6a199 502 test $# -gt 1 && usage
46df82d5 503 upstream_arg=--root
190f5323 504fi
32d99544 505
a1bf91e0 506# Make sure the branch to rebase onto is valid.
6bb4e485 507onto_name=${onto-"$upstream_name"}
9f21e97d
NS
508case "$onto_name" in
509*...*)
510 if left=${onto_name%...*} right=${onto_name#*...} &&
511 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
512 then
513 case "$onto" in
514 ?*"$LF"?*)
c7108bf9 515 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
9f21e97d
NS
516 ;;
517 '')
c7108bf9 518 die "$(eval_gettext "\$onto_name: there is no merge base")"
9f21e97d
NS
519 ;;
520 esac
521 else
c7108bf9 522 die "$(eval_gettext "\$onto_name: there is no merge base")"
9f21e97d
NS
523 fi
524 ;;
525*)
2e6e276d 526 onto=$(peel_committish "$onto_name") ||
c7108bf9 527 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
9f21e97d
NS
528 ;;
529esac
a1bf91e0 530
0cb06644 531# If the branch to rebase is given, that is the branch we will rebase
3a9156ad 532# $branch_name -- branch/commit being rebased, or HEAD (already detached)
0cb06644
JH
533# $orig_head -- commit object name of tip of the branch before rebasing
534# $head_name -- refs/heads/<that-branch> or "detached HEAD"
535switch_to=
59e6b23a 536case "$#" in
190f5323 5371)
0cb06644 538 # Is it "rebase other $branchname" or "rebase other $commit"?
190f5323
TR
539 branch_name="$1"
540 switch_to="$1"
0cb06644 541
3a9156ad
KS
542 # Is it a local branch?
543 if git show-ref --verify --quiet -- "refs/heads/$branch_name" &&
544 orig_head=$(git rev-parse -q --verify "refs/heads/$branch_name")
0cb06644 545 then
3a9156ad
KS
546 head_name="refs/heads/$branch_name"
547 # If not is it a valid ref (branch or commit)?
548 elif orig_head=$(git rev-parse -q --verify "$branch_name")
0cb06644
JH
549 then
550 head_name="detached HEAD"
3a9156ad 551
0cb06644 552 else
ca7de7b1 553 die "$(eval_gettext "fatal: no such branch/commit '\$branch_name'")"
0cb06644 554 fi
e646c9c8 555 ;;
f2b6a199 5560)
0cb06644 557 # Do not need to switch branches, we are already on it.
728fc79c 558 if branch_name=$(git symbolic-ref -q HEAD)
bcf31618 559 then
0cb06644 560 head_name=$branch_name
728fc79c 561 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
bcf31618 562 else
0cb06644 563 head_name="detached HEAD"
3a9156ad 564 branch_name=HEAD
bcf31618 565 fi
ea709800 566 orig_head=$(git rev-parse --verify HEAD) || exit
e646c9c8 567 ;;
f2b6a199
MZ
568*)
569 die "BUG: unexpected number of arguments left to parse"
570 ;;
59e6b23a
JH
571esac
572
ad8261d2
JK
573if test "$fork_point" = t
574then
bb3f4583
JK
575 new_upstream=$(git merge-base --fork-point "$upstream_name" \
576 "${switch_to:-HEAD}")
ad8261d2
JK
577 if test -n "$new_upstream"
578 then
1e0dacdb 579 restrict_revision=$new_upstream
ad8261d2
JK
580 fi
581fi
582
58794775
RR
583if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
584then
585 stash_sha1=$(git stash create "autostash") ||
586 die "$(gettext 'Cannot autostash')"
587
588 mkdir -p "$state_dir" &&
589 echo $stash_sha1 >"$state_dir/autostash" &&
590 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
591 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
592 git reset --hard
593fi
594
c7108bf9 595require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
8f9bfb64 596
cb82a05d
MZ
597# Now we are rebasing commits $upstream..$orig_head (or with --root,
598# everything leading up to $orig_head) on top of $onto
e646c9c8 599
1308c17b 600# Check if we are already based on $onto with linear history,
cc1453e1
MZ
601# but this should be done only when upstream and onto are the same
602# and if this is not an interactive rebase.
cb82a05d 603mb=$(git merge-base "$onto" "$orig_head")
cc1453e1 604if test "$type" != interactive && test "$upstream" = "$onto" &&
1e0dacdb 605 test "$mb" = "$onto" && test -z "$restrict_revision" &&
1308c17b 606 # linear history?
cb82a05d 607 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
7f4bd5d8 608then
b2f82e05
SR
609 if test -z "$force_rebase"
610 then
611 # Lazily switch to the target branch if needed...
4b03df21
RR
612 test -z "$switch_to" ||
613 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
22946a94 614 git checkout -q "$switch_to" --
08e66700
KS
615 if test "$branch_name" = "HEAD" &&
616 ! git symbolic-ref -q HEAD
617 then
618 say "$(eval_gettext "HEAD is up to date.")"
619 else
620 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
621 fi
96e2b99e 622 finish_rebase
b2f82e05
SR
623 exit 0
624 else
08e66700
KS
625 if test "$branch_name" = "HEAD" &&
626 ! git symbolic-ref -q HEAD
627 then
628 say "$(eval_gettext "HEAD is up to date, rebase forced.")"
629 else
630 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
631 fi
b2f82e05 632 fi
7f4bd5d8
JH
633fi
634
8f9bfb64
MZ
635# If a hook exists, give it a chance to interrupt
636run_pre_rebase_hook "$upstream_arg" "$@"
637
a9c3821c
TAV
638if test -n "$diffstat"
639then
640 if test -n "$verbose"
641 then
c7108bf9 642 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
a9c3821c
TAV
643 fi
644 # We want color (if set), but no pager
645 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
646fi
647
fa99c1e1 648test "$type" = interactive && run_specific_rebase
f4107d9c
MZ
649
650# Detach HEAD and reset the tree
c7108bf9 651say "$(gettext "First, rewinding head to replay your work on top of it...")"
4b03df21
RR
652
653GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
654 git checkout -q "$onto^0" || die "could not detach HEAD"
cb82a05d 655git update-ref ORIG_HEAD $orig_head
f4107d9c 656
e646c9c8 657# If the $onto is a proper descendant of the tip of the branch, then
a75d7b54 658# we just fast-forwarded.
cb82a05d 659if test "$mb" = "$orig_head"
32d99544 660then
c7108bf9 661 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
6fd2f5e6 662 move_to_original_branch
af2f0ebc 663 finish_rebase
32d99544
LS
664 exit 0
665fi
666
190f5323
TR
667if test -n "$rebase_root"
668then
669 revisions="$onto..$orig_head"
670else
1e0dacdb 671 revisions="${restrict_revision-$upstream}..$orig_head"
190f5323
TR
672fi
673
fa99c1e1 674run_specific_rebase