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