]> git.ipfire.org Git - thirdparty/git.git/blame - git-rebase.sh
rebase: remove $branch as synonym for $orig_head
[thirdparty/git.git] / git-rebase.sh
CommitLineData
59e6b23a
JH
1#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
bffd750a 6USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] (<upstream>|--root) [<branch>] [--quiet | -q]'
031321c6
SE
7LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8same name. When the --onto option is provided the new branch starts
9out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10It then attempts to create a new commit for each commit from the original
11<branch> that does not exist in the <upstream> branch.
69a60af5 12
031321c6
SE
13It is possible that a merge failure will prevent this process from being
14completely automatic. You will have to resolve any such merge failure
cc120056
SE
15and run git rebase --continue. Another option is to bypass the commit
16that caused the merge failure with git rebase --skip. To restore the
51ef1daa
JS
17original <branch> and remove the .git/rebase-apply working files, use the
18command git rebase --abort instead.
69a60af5 19
031321c6 20Note that if <branch> is not specified on the command line, the
702088af 21currently checked out branch is used.
e646c9c8 22
031321c6 23Example: git-rebase master~1 topic
e646c9c8 24
031321c6
SE
25 A---B---C topic A'\''--B'\''--C'\'' topic
26 / --> /
27 D---E---F---G master D---E---F---G master
e646c9c8 28'
533b7039
JH
29
30SUBDIRECTORY_OK=Yes
8f321a39 31OPTIONS_SPEC=
ae2b0f15 32. git-sh-setup
f9474132 33set_reflog_action rebase
7eff28a9 34require_work_tree
533b7039 35cd_to_toplevel
4282c4fb 36
61dfa1bb
JH
37LF='
38'
6bb4e485
MZ
39ok_to_skip_pre_rebase=
40resolvemsg="
cc120056
SE
41When you have resolved this problem run \"git rebase --continue\".
42If you would prefer to skip this patch, instead run \"git rebase --skip\".
43To restore the original branch and stop rebasing run \"git rebase --abort\".
44"
6bb4e485 45unset onto
9765b6ab 46strategy=
93ce190c 47strategy_opts=
58634dbf 48do_merge=
69a636ad
MZ
49merge_dir="$GIT_DIR"/rebase-merge
50apply_dir="$GIT_DIR"/rebase-apply
58634dbf 51prec=4
b758789c 52verbose=
9474a029
MZ
53diffstat=
54test "$(git config --bool rebase.stat)" = true && diffstat=t
67dad687 55git_am_opt=
190f5323 56rebase_root=
b2f82e05 57force_rebase=
cb6020bb 58allow_rerere_autoupdate=
99de0640
MZ
59# Non-empty if a rebase was in progress when 'git rebase' was invoked
60in_progress=
61# One of {am, merge, interactive}
62type=
63# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
64state_dir=
34262322
MZ
65# One of {'', continue, skip, abort}, as parsed from command line
66action=
cf432ca0
MZ
67preserve_merges=
68autosquash=
69test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
58634dbf 70
02ac45fa 71read_state () {
99de0640 72 if test "$type" = merge
02ac45fa 73 then
99de0640
MZ
74 onto_name=$(cat "$state_dir"/onto_name) &&
75 end=$(cat "$state_dir"/end) &&
76 msgnum=$(cat "$state_dir"/msgnum)
02ac45fa
MZ
77 fi &&
78 head_name=$(cat "$state_dir"/head-name) &&
79 onto=$(cat "$state_dir"/onto) &&
80 orig_head=$(cat "$state_dir"/orig-head) &&
81 GIT_QUIET=$(cat "$state_dir"/quiet)
82}
83
58634dbf 84continue_merge () {
69a636ad 85 test -d "$merge_dir" || die "$merge_dir directory does not exist"
58634dbf 86
5be60078 87 unmerged=$(git ls-files -u)
58634dbf
EW
88 if test -n "$unmerged"
89 then
90 echo "You still have unmerged paths in your index"
9b07873a 91 echo "did you forget to use git add?"
6bb4e485 92 die "$resolvemsg"
58634dbf
EW
93 fi
94
69a636ad 95 cmt=`cat "$merge_dir/current"`
6848d58c 96 if ! git diff-index --quiet --ignore-submodules HEAD --
58634dbf 97 then
e637122e 98 if ! git commit --no-verify -C "$cmt"
f0ef0596
EW
99 then
100 echo "Commit failed, please do not call \"git commit\""
101 echo "directly, but instead do one of the following: "
6bb4e485 102 die "$resolvemsg"
f0ef0596 103 fi
0e987a12
SB
104 if test -z "$GIT_QUIET"
105 then
106 printf "Committed: %0${prec}d " $msgnum
107 fi
69a636ad 108 echo "$cmt $(git rev-parse HEAD^0)" >> "$merge_dir/rewritten"
58634dbf 109 else
0e987a12
SB
110 if test -z "$GIT_QUIET"
111 then
112 printf "Already applied: %0${prec}d " $msgnum
113 fi
114 fi
0aa958d4 115 test -z "$GIT_QUIET" &&
31d87385 116 GIT_PAGER='' git log --format=%s -1 "$cmt"
58634dbf 117
58634dbf
EW
118 # onto the next patch:
119 msgnum=$(($msgnum + 1))
69a636ad 120 echo "$msgnum" >"$merge_dir/msgnum"
58634dbf
EW
121}
122
123call_merge () {
69a636ad
MZ
124 cmt="$(cat "$merge_dir/cmt.$1")"
125 echo "$cmt" > "$merge_dir/current"
5be60078 126 hd=$(git rev-parse --verify HEAD)
6fd2f5e6 127 cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD)
69a636ad 128 msgnum=$(cat "$merge_dir/msgnum")
0bb733c9 129 eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"'
10ffca7d 130 eval GITHEAD_$hd='$onto_name'
0bb733c9 131 export GITHEAD_$cmt GITHEAD_$hd
0e987a12
SB
132 if test -n "$GIT_QUIET"
133 then
69ae92bd 134 GIT_MERGE_VERBOSITY=1 && export GIT_MERGE_VERBOSITY
0e987a12 135 fi
9765b6ab 136 test -z "$strategy" && strategy=recursive
93ce190c 137 eval 'git-merge-$strategy' $strategy_opts '"$cmt^" -- "$hd" "$cmt"'
58634dbf
EW
138 rv=$?
139 case "$rv" in
140 0)
0bb733c9 141 unset GITHEAD_$cmt GITHEAD_$hd
9e4bc7dd 142 return
58634dbf
EW
143 ;;
144 1)
cb6020bb 145 git rerere $allow_rerere_autoupdate
6bb4e485 146 die "$resolvemsg"
58634dbf
EW
147 ;;
148 2)
149 echo "Strategy: $rv $strategy failed, try another" 1>&2
6bb4e485 150 die "$resolvemsg"
58634dbf
EW
151 ;;
152 *)
153 die "Unknown exit code ($rv) from command:" \
154 "git-merge-$strategy $cmt^ -- HEAD $cmt"
155 ;;
156 esac
157}
158
6fd2f5e6 159move_to_original_branch () {
6fd2f5e6
JS
160 case "$head_name" in
161 refs/*)
162 message="rebase finished: $head_name onto $onto"
163 git update-ref -m "$message" \
164 $head_name $(git rev-parse HEAD) $orig_head &&
165 git symbolic-ref HEAD $head_name ||
166 die "Could not move back to $head_name"
167 ;;
168 esac
169}
170
58634dbf 171finish_rb_merge () {
6fd2f5e6 172 move_to_original_branch
69a636ad 173 git notes copy --for-rewrite=rebase < "$merge_dir"/rewritten
96e19488 174 if test -x "$GIT_DIR"/hooks/post-rewrite &&
69a636ad
MZ
175 test -s "$merge_dir"/rewritten; then
176 "$GIT_DIR"/hooks/post-rewrite rebase < "$merge_dir"/rewritten
96e19488 177 fi
69a636ad 178 rm -r "$merge_dir"
0e987a12 179 say All done.
58634dbf
EW
180}
181
cf432ca0 182run_interactive_rebase () {
f8cca019
AE
183 if [ "$interactive_rebase" = implied ]; then
184 GIT_EDITOR=:
185 export GIT_EDITOR
186 fi
cc1453e1 187 . git-rebase--interactive
1b1dce4b
JS
188}
189
d70b4a8f 190run_pre_rebase_hook () {
6bb4e485 191 if test -z "$ok_to_skip_pre_rebase" &&
c4427656 192 test -x "$GIT_DIR/hooks/pre-rebase"
d70b4a8f 193 then
bc2bbc45
SB
194 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
195 die "The pre-rebase hook refused to rebase."
d70b4a8f
NS
196 fi
197}
198
69a636ad 199test -f "$apply_dir"/applying &&
9b752a6e
SB
200 die 'It looks like git-am is in progress. Cannot rebase.'
201
99de0640
MZ
202if test -d "$apply_dir"
203then
204 type=am
205 state_dir="$apply_dir"
206elif test -d "$merge_dir"
207then
208 if test -f "$merge_dir"/interactive
209 then
210 type=interactive
211 interactive_rebase=explicit
212 else
213 type=merge
214 fi
215 state_dir="$merge_dir"
216fi
217test -n "$type" && in_progress=t
218
95135b06 219total_argc=$#
822f7c73 220while test $# != 0
e646c9c8
JH
221do
222 case "$1" in
c4427656 223 --no-verify)
6bb4e485 224 ok_to_skip_pre_rebase=yes
c4427656 225 ;;
7baf9c4b 226 --verify)
6bb4e485 227 ok_to_skip_pre_rebase=
7baf9c4b 228 ;;
34262322 229 --continue|--skip|--abort)
95135b06 230 test $total_argc -eq 1 || usage
34262322 231 action=${1##--}
031321c6 232 ;;
e646c9c8
JH
233 --onto)
234 test 2 -le "$#" || usage
6bb4e485 235 onto="$2"
e646c9c8
JH
236 shift
237 ;;
cf432ca0
MZ
238 -i|--interactive)
239 interactive_rebase=explicit
240 ;;
241 -p|--preserve-merges)
242 preserve_merges=t
243 test -z "$interactive_rebase" && interactive_rebase=implied
244 ;;
245 --autosquash)
246 autosquash=t
247 ;;
248 --no-autosquash)
249 autosquash=
250 ;;
58634dbf
EW
251 -M|-m|--m|--me|--mer|--merg|--merge)
252 do_merge=t
253 ;;
93ce190c
ML
254 -X*|--strategy-option*)
255 case "$#,$1" in
256 1,-X|1,--strategy-option)
257 usage ;;
258 *,-X|*,--strategy-option)
259 newopt="$2"
260 shift ;;
261 *,--strategy-option=*)
262 newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;;
263 *,-X*)
264 newopt="$(expr " $1" : ' -X\(.*\)')" ;;
265 1,*)
266 usage ;;
267 esac
268 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")"
269 do_merge=t
9765b6ab 270 test -z "$strategy" && strategy=recursive
93ce190c 271 ;;
58634dbf
EW
272 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
273 --strateg=*|--strategy=*|\
274 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
275 case "$#,$1" in
276 *,*=*)
8096fae7 277 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
58634dbf
EW
278 1,*)
279 usage ;;
280 *)
281 strategy="$2"
282 shift ;;
283 esac
284 do_merge=t
285 ;;
a9c3821c
TAV
286 -n|--no-stat)
287 diffstat=
288 ;;
289 --stat)
290 diffstat=t
291 ;;
b758789c
RS
292 -v|--verbose)
293 verbose=t
a9c3821c 294 diffstat=t
0e987a12
SB
295 GIT_QUIET=
296 ;;
297 -q|--quiet)
298 GIT_QUIET=t
299 git_am_opt="$git_am_opt -q"
300 verbose=
301 diffstat=
b758789c 302 ;;
059f446d
BF
303 --whitespace=*)
304 git_am_opt="$git_am_opt $1"
b2f82e05
SR
305 case "$1" in
306 --whitespace=fix|--whitespace=strip)
307 force_rebase=t
308 ;;
309 esac
059f446d 310 ;;
86c91f91
GB
311 --ignore-whitespace)
312 git_am_opt="$git_am_opt $1"
313 ;;
570ccad3
MB
314 --committer-date-is-author-date|--ignore-date)
315 git_am_opt="$git_am_opt $1"
316 force_rebase=t
317 ;;
67dad687 318 -C*)
059f446d 319 git_am_opt="$git_am_opt $1"
67dad687 320 ;;
190f5323
TR
321 --root)
322 rebase_root=t
323 ;;
1bc6bf6d 324 -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff)
b2f82e05
SR
325 force_rebase=t
326 ;;
cb6020bb
JH
327 --rerere-autoupdate|--no-rerere-autoupdate)
328 allow_rerere_autoupdate="$1"
329 ;;
e646c9c8
JH
330 -*)
331 usage
332 ;;
333 *)
334 break
335 ;;
336 esac
337 shift
338done
51b2ead0 339test $# -gt 2 && usage
2db8aaec 340
cf432ca0
MZ
341if test -n "$action"
342then
343 test -z "$in_progress" && die "No rebase in progress?"
344 test "$type" = interactive && run_interactive_rebase
345fi
34262322
MZ
346
347case "$action" in
348continue)
349 git update-index --ignore-submodules --refresh &&
350 git diff-files --quiet --ignore-submodules || {
351 echo "You must edit all merge conflicts and then"
352 echo "mark them as resolved using git add"
353 exit 1
354 }
355 read_state
356 if test -d "$merge_dir"
357 then
358 continue_merge
359 while test "$msgnum" -le "$end"
360 do
361 call_merge "$msgnum"
362 continue_merge
363 done
364 finish_rb_merge
365 exit
366 fi
6bb4e485 367 git am --resolved --3way --resolvemsg="$resolvemsg" &&
34262322
MZ
368 move_to_original_branch
369 exit
370 ;;
371skip)
372 git reset --hard HEAD || exit $?
373 read_state
374 if test -d "$merge_dir"
375 then
376 git rerere clear
377 msgnum=$(($msgnum + 1))
378 while test "$msgnum" -le "$end"
379 do
380 call_merge "$msgnum"
381 continue_merge
382 done
383 finish_rb_merge
384 exit
385 fi
6bb4e485 386 git am -3 --skip --resolvemsg="$resolvemsg" &&
34262322
MZ
387 move_to_original_branch
388 exit
389 ;;
390abort)
391 git rerere clear
392 read_state
393 case "$head_name" in
394 refs/*)
395 git symbolic-ref HEAD $head_name ||
396 die "Could not move back to $head_name"
397 ;;
398 esac
399 git reset --hard $orig_head
400 rm -r "$state_dir"
401 exit
402 ;;
403esac
404
99de0640
MZ
405# Make sure no rebase is in progress
406if test -n "$in_progress"
7f4bd5d8 407then
99de0640
MZ
408 die '
409It seems that there is already a '"${state_dir##*/}"' directory, and
410I wonder if you are in the middle of another rebase. If that is the
411case, please try
412 git rebase (--continue | --abort | --skip)
413If that is not the case, please
414 rm -fr '"$state_dir"'
9b752a6e 415and run me again. I am stopping in case you still have something
51ef1daa 416valuable there.'
7f4bd5d8
JH
417fi
418
99de0640
MZ
419test $# -eq 0 && test -z "$rebase_root" && usage
420
cf432ca0
MZ
421if test -n "$interactive_rebase"
422then
423 type=interactive
424 state_dir="$merge_dir"
425elif test -n "$do_merge"
426then
427 type=merge
428 state_dir="$merge_dir"
429else
430 type=am
431 state_dir="$apply_dir"
432fi
433
190f5323
TR
434if test -z "$rebase_root"
435then
436 # The upstream head must be given. Make sure it is valid.
437 upstream_name="$1"
438 shift
439 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
440 die "invalid upstream $upstream_name"
441 unset root_flag
442 upstream_arg="$upstream_name"
443else
71786f54 444 test -z "$onto" && die "You must specify --onto when using --root"
190f5323
TR
445 unset upstream_name
446 unset upstream
447 root_flag="--root"
448 upstream_arg="$root_flag"
449fi
32d99544 450
a1bf91e0 451# Make sure the branch to rebase onto is valid.
6bb4e485 452onto_name=${onto-"$upstream_name"}
9f21e97d
NS
453case "$onto_name" in
454*...*)
455 if left=${onto_name%...*} right=${onto_name#*...} &&
456 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
457 then
458 case "$onto" in
459 ?*"$LF"?*)
460 die "$onto_name: there are more than one merge bases"
461 ;;
462 '')
463 die "$onto_name: there is no merge base"
464 ;;
465 esac
466 else
61dfa1bb 467 die "$onto_name: there is no merge base"
9f21e97d
NS
468 fi
469 ;;
470*)
71786f54
MZ
471 onto=$(git rev-parse --verify "${onto_name}^0") ||
472 die "Does not point to a valid commit: $1"
9f21e97d
NS
473 ;;
474esac
a1bf91e0 475
0cb06644
JH
476# If the branch to rebase is given, that is the branch we will rebase
477# $branch_name -- branch being rebased, or HEAD (already detached)
478# $orig_head -- commit object name of tip of the branch before rebasing
479# $head_name -- refs/heads/<that-branch> or "detached HEAD"
480switch_to=
59e6b23a 481case "$#" in
190f5323 4821)
0cb06644 483 # Is it "rebase other $branchname" or "rebase other $commit"?
190f5323
TR
484 branch_name="$1"
485 switch_to="$1"
0cb06644 486
190f5323 487 if git show-ref --verify --quiet -- "refs/heads/$1" &&
cb82a05d 488 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
0cb06644 489 then
190f5323 490 head_name="refs/heads/$1"
cb82a05d 491 elif orig_head=$(git rev-parse -q --verify "$1")
0cb06644
JH
492 then
493 head_name="detached HEAD"
494 else
4ac5356c 495 echo >&2 "fatal: no such branch: $1"
0cb06644
JH
496 usage
497 fi
e646c9c8
JH
498 ;;
499*)
0cb06644 500 # Do not need to switch branches, we are already on it.
bcf31618
JH
501 if branch_name=`git symbolic-ref -q HEAD`
502 then
0cb06644 503 head_name=$branch_name
bcf31618
JH
504 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
505 else
0cb06644 506 head_name="detached HEAD"
bcf31618
JH
507 branch_name=HEAD ;# detached
508 fi
cb82a05d 509 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
e646c9c8 510 ;;
59e6b23a
JH
511esac
512
8f9bfb64
MZ
513require_clean_work_tree "rebase" "Please commit or stash them."
514
cb82a05d
MZ
515# Now we are rebasing commits $upstream..$orig_head (or with --root,
516# everything leading up to $orig_head) on top of $onto
e646c9c8 517
1308c17b 518# Check if we are already based on $onto with linear history,
cc1453e1
MZ
519# but this should be done only when upstream and onto are the same
520# and if this is not an interactive rebase.
cb82a05d 521mb=$(git merge-base "$onto" "$orig_head")
cc1453e1
MZ
522if test "$type" != interactive && test "$upstream" = "$onto" &&
523 test "$mb" = "$onto" &&
1308c17b 524 # linear history?
cb82a05d 525 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
7f4bd5d8 526then
b2f82e05
SR
527 if test -z "$force_rebase"
528 then
529 # Lazily switch to the target branch if needed...
3b21a438 530 test -z "$switch_to" || git checkout "$switch_to" --
0e987a12 531 say "Current branch $branch_name is up to date."
b2f82e05
SR
532 exit 0
533 else
0e987a12 534 say "Current branch $branch_name is up to date, rebase forced."
b2f82e05 535 fi
7f4bd5d8
JH
536fi
537
8f9bfb64
MZ
538# If a hook exists, give it a chance to interrupt
539run_pre_rebase_hook "$upstream_arg" "$@"
540
a9c3821c
TAV
541if test -n "$diffstat"
542then
543 if test -n "$verbose"
544 then
545 echo "Changes from $mb to $onto:"
546 fi
547 # We want color (if set), but no pager
548 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
549fi
550
f4107d9c
MZ
551test "$type" = interactive && run_interactive_rebase
552
553# Detach HEAD and reset the tree
554say "First, rewinding head to replay your work on top of it..."
555git checkout -q "$onto^0" || die "could not detach HEAD"
cb82a05d 556git update-ref ORIG_HEAD $orig_head
f4107d9c 557
e646c9c8 558# If the $onto is a proper descendant of the tip of the branch, then
a75d7b54 559# we just fast-forwarded.
cb82a05d 560if test "$mb" = "$orig_head"
32d99544 561then
0e987a12 562 say "Fast-forwarded $branch_name to $onto_name."
6fd2f5e6 563 move_to_original_branch
32d99544
LS
564 exit 0
565fi
566
190f5323
TR
567if test -n "$rebase_root"
568then
569 revisions="$onto..$orig_head"
570else
571 revisions="$upstream..$orig_head"
572fi
573
58634dbf
EW
574if test -z "$do_merge"
575then
0cb06644 576 git format-patch -k --stdout --full-index --ignore-if-in-upstream \
5b220a68 577 --src-prefix=a/ --dst-prefix=b/ \
840b3ca7 578 --no-renames $root_flag "$revisions" |
6bb4e485 579 git am $git_am_opt --rebasing --resolvemsg="$resolvemsg" &&
6fd2f5e6
JS
580 move_to_original_branch
581 ret=$?
69a636ad
MZ
582 test 0 != $ret -a -d "$apply_dir" &&
583 echo $head_name > "$apply_dir/head-name" &&
584 echo $onto > "$apply_dir/onto" &&
585 echo $orig_head > "$apply_dir/orig-head" &&
586 echo "$GIT_QUIET" > "$apply_dir/quiet"
6fd2f5e6 587 exit $ret
58634dbf
EW
588fi
589
590# start doing a rebase with git-merge
591# this is rename-aware if the recursive (default) strategy is used
592
69a636ad 593mkdir -p "$merge_dir"
69a636ad 594echo "$onto_name" > "$merge_dir/onto_name"
69a636ad 595echo "$head_name" > "$merge_dir/head-name"
02ac45fa
MZ
596echo "$onto" > "$merge_dir/onto"
597echo "$orig_head" > "$merge_dir/orig-head"
69a636ad 598echo "$GIT_QUIET" > "$merge_dir/quiet"
58634dbf
EW
599
600msgnum=0
190f5323 601for cmt in `git rev-list --reverse --no-merges "$revisions"`
58634dbf
EW
602do
603 msgnum=$(($msgnum + 1))
69a636ad 604 echo "$cmt" > "$merge_dir/cmt.$msgnum"
58634dbf
EW
605done
606
69a636ad
MZ
607echo 1 >"$merge_dir/msgnum"
608echo $msgnum >"$merge_dir/end"
58634dbf
EW
609
610end=$msgnum
611msgnum=1
612
613while test "$msgnum" -le "$end"
614do
615 call_merge "$msgnum"
616 continue_merge
617done
cc120056 618
58634dbf 619finish_rb_merge