]>
Commit | Line | Data |
---|---|---|
59e6b23a JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2005 Junio C Hamano. | |
4 | # | |
5 | ||
0e987a12 | 6 | USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--onto <newbase>] [<upstream>|--root] [<branch>] [--quiet | -q]' |
031321c6 SE |
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. | |
69a60af5 | 12 | |
031321c6 SE |
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 | |
cc120056 SE |
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 | |
51ef1daa JS |
17 | original <branch> and remove the .git/rebase-apply working files, use the |
18 | command git rebase --abort instead. | |
69a60af5 | 19 | |
031321c6 | 20 | Note that if <branch> is not specified on the command line, the |
702088af | 21 | currently checked out branch is used. |
e646c9c8 | 22 | |
031321c6 | 23 | Example: 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 | |
30 | SUBDIRECTORY_OK=Yes | |
8f321a39 | 31 | OPTIONS_SPEC= |
ae2b0f15 | 32 | . git-sh-setup |
f9474132 | 33 | set_reflog_action rebase |
7eff28a9 | 34 | require_work_tree |
533b7039 | 35 | cd_to_toplevel |
4282c4fb | 36 | |
61dfa1bb JH |
37 | LF=' |
38 | ' | |
c4427656 | 39 | OK_TO_SKIP_PRE_REBASE= |
cc120056 SE |
40 | RESOLVEMSG=" |
41 | When you have resolved this problem run \"git rebase --continue\". | |
42 | If you would prefer to skip this patch, instead run \"git rebase --skip\". | |
43 | To restore the original branch and stop rebasing run \"git rebase --abort\". | |
44 | " | |
e646c9c8 | 45 | unset newbase |
a06f678e | 46 | strategy=recursive |
58634dbf | 47 | do_merge= |
28ed6e7b | 48 | dotest="$GIT_DIR"/rebase-merge |
58634dbf | 49 | prec=4 |
b758789c | 50 | verbose= |
a9c3821c | 51 | diffstat=$(git config --bool rebase.stat) |
67dad687 | 52 | git_am_opt= |
190f5323 | 53 | rebase_root= |
b2f82e05 | 54 | force_rebase= |
cb6020bb | 55 | allow_rerere_autoupdate= |
58634dbf EW |
56 | |
57 | continue_merge () { | |
58 | test -n "$prev_head" || die "prev_head must be defined" | |
59 | test -d "$dotest" || die "$dotest directory does not exist" | |
60 | ||
5be60078 | 61 | unmerged=$(git ls-files -u) |
58634dbf EW |
62 | if test -n "$unmerged" |
63 | then | |
64 | echo "You still have unmerged paths in your index" | |
9b07873a | 65 | echo "did you forget to use git add?" |
66eb64cb | 66 | die "$RESOLVEMSG" |
58634dbf EW |
67 | fi |
68 | ||
889a50e9 | 69 | cmt=`cat "$dotest/current"` |
6848d58c | 70 | if ! git diff-index --quiet --ignore-submodules HEAD -- |
58634dbf | 71 | then |
e637122e | 72 | if ! git commit --no-verify -C "$cmt" |
f0ef0596 EW |
73 | then |
74 | echo "Commit failed, please do not call \"git commit\"" | |
75 | echo "directly, but instead do one of the following: " | |
76 | die "$RESOLVEMSG" | |
77 | fi | |
0e987a12 SB |
78 | if test -z "$GIT_QUIET" |
79 | then | |
80 | printf "Committed: %0${prec}d " $msgnum | |
81 | fi | |
96e19488 | 82 | echo "$cmt $(git rev-parse HEAD^0)" >> "$dotest/rewritten" |
58634dbf | 83 | else |
0e987a12 SB |
84 | if test -z "$GIT_QUIET" |
85 | then | |
86 | printf "Already applied: %0${prec}d " $msgnum | |
87 | fi | |
88 | fi | |
89 | if test -z "$GIT_QUIET" | |
90 | then | |
91 | git rev-list --pretty=oneline -1 "$cmt" | sed -e 's/^[^ ]* //' | |
58634dbf EW |
92 | fi |
93 | ||
5be60078 | 94 | prev_head=`git rev-parse HEAD^0` |
58634dbf | 95 | # save the resulting commit so we can read-tree on it later |
58634dbf EW |
96 | echo "$prev_head" > "$dotest/prev_head" |
97 | ||
98 | # onto the next patch: | |
99 | msgnum=$(($msgnum + 1)) | |
5887ac82 | 100 | echo "$msgnum" >"$dotest/msgnum" |
58634dbf EW |
101 | } |
102 | ||
103 | call_merge () { | |
889a50e9 | 104 | cmt="$(cat "$dotest/cmt.$1")" |
58634dbf | 105 | echo "$cmt" > "$dotest/current" |
5be60078 | 106 | hd=$(git rev-parse --verify HEAD) |
6fd2f5e6 | 107 | cmt_name=$(git symbolic-ref HEAD 2> /dev/null || echo HEAD) |
889a50e9 JS |
108 | msgnum=$(cat "$dotest/msgnum") |
109 | end=$(cat "$dotest/end") | |
0bb733c9 | 110 | eval GITHEAD_$cmt='"${cmt_name##refs/heads/}~$(($end - $msgnum))"' |
889a50e9 | 111 | eval GITHEAD_$hd='$(cat "$dotest/onto_name")' |
0bb733c9 | 112 | export GITHEAD_$cmt GITHEAD_$hd |
0e987a12 SB |
113 | if test -n "$GIT_QUIET" |
114 | then | |
115 | export GIT_MERGE_VERBOSITY=1 | |
116 | fi | |
0bb733c9 | 117 | git-merge-$strategy "$cmt^" -- "$hd" "$cmt" |
58634dbf EW |
118 | rv=$? |
119 | case "$rv" in | |
120 | 0) | |
0bb733c9 | 121 | unset GITHEAD_$cmt GITHEAD_$hd |
9e4bc7dd | 122 | return |
58634dbf EW |
123 | ;; |
124 | 1) | |
cb6020bb | 125 | git rerere $allow_rerere_autoupdate |
66eb64cb | 126 | die "$RESOLVEMSG" |
58634dbf EW |
127 | ;; |
128 | 2) | |
129 | echo "Strategy: $rv $strategy failed, try another" 1>&2 | |
66eb64cb | 130 | die "$RESOLVEMSG" |
58634dbf EW |
131 | ;; |
132 | *) | |
133 | die "Unknown exit code ($rv) from command:" \ | |
134 | "git-merge-$strategy $cmt^ -- HEAD $cmt" | |
135 | ;; | |
136 | esac | |
137 | } | |
138 | ||
6fd2f5e6 JS |
139 | move_to_original_branch () { |
140 | test -z "$head_name" && | |
141 | head_name="$(cat "$dotest"/head-name)" && | |
142 | onto="$(cat "$dotest"/onto)" && | |
143 | orig_head="$(cat "$dotest"/orig-head)" | |
144 | case "$head_name" in | |
145 | refs/*) | |
146 | message="rebase finished: $head_name onto $onto" | |
147 | git update-ref -m "$message" \ | |
148 | $head_name $(git rev-parse HEAD) $orig_head && | |
149 | git symbolic-ref HEAD $head_name || | |
150 | die "Could not move back to $head_name" | |
151 | ;; | |
152 | esac | |
153 | } | |
154 | ||
58634dbf | 155 | finish_rb_merge () { |
6fd2f5e6 | 156 | move_to_original_branch |
96e19488 TR |
157 | if test -x "$GIT_DIR"/hooks/post-rewrite && |
158 | test -s "$dotest"/rewritten; then | |
159 | "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten | |
160 | fi | |
58634dbf | 161 | rm -r "$dotest" |
0e987a12 | 162 | say All done. |
58634dbf EW |
163 | } |
164 | ||
1b1dce4b | 165 | is_interactive () { |
f8cca019 AE |
166 | while test $# != 0 |
167 | do | |
168 | case "$1" in | |
169 | -i|--interactive) | |
170 | interactive_rebase=explicit | |
171 | break | |
172 | ;; | |
173 | -p|--preserve-merges) | |
174 | interactive_rebase=implied | |
175 | ;; | |
176 | esac | |
1b1dce4b | 177 | shift |
f8cca019 AE |
178 | done |
179 | ||
180 | if [ "$interactive_rebase" = implied ]; then | |
181 | GIT_EDITOR=: | |
182 | export GIT_EDITOR | |
183 | fi | |
184 | ||
185 | test -n "$interactive_rebase" || test -f "$dotest"/interactive | |
1b1dce4b JS |
186 | } |
187 | ||
d70b4a8f | 188 | run_pre_rebase_hook () { |
c4427656 NS |
189 | if test -z "$OK_TO_SKIP_PRE_REBASE" && |
190 | test -x "$GIT_DIR/hooks/pre-rebase" | |
d70b4a8f | 191 | then |
bc2bbc45 SB |
192 | "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || |
193 | die "The pre-rebase hook refused to rebase." | |
d70b4a8f NS |
194 | fi |
195 | } | |
196 | ||
9b752a6e SB |
197 | test -f "$GIT_DIR"/rebase-apply/applying && |
198 | die 'It looks like git-am is in progress. Cannot rebase.' | |
199 | ||
1b1dce4b JS |
200 | is_interactive "$@" && exec git-rebase--interactive "$@" |
201 | ||
9b752a6e SB |
202 | if test $# -eq 0 |
203 | then | |
204 | test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || usage | |
205 | test -d "$dotest" -o -f "$GIT_DIR"/rebase-apply/rebasing && | |
206 | die 'A rebase is in progress, try --continue, --skip or --abort.' | |
207 | die "No arguments given and $GIT_DIR/rebase-apply already exists." | |
208 | fi | |
209 | ||
822f7c73 | 210 | while test $# != 0 |
e646c9c8 JH |
211 | do |
212 | case "$1" in | |
c4427656 NS |
213 | --no-verify) |
214 | OK_TO_SKIP_PRE_REBASE=yes | |
215 | ;; | |
031321c6 | 216 | --continue) |
51ef1daa | 217 | test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || |
cd5320f2 SB |
218 | die "No rebase in progress?" |
219 | ||
6848d58c | 220 | git diff-files --quiet --ignore-submodules || { |
06aff47b | 221 | echo "You must edit all merge conflicts and then" |
9b07873a | 222 | echo "mark them as resolved using git add" |
031321c6 | 223 | exit 1 |
06aff47b | 224 | } |
58634dbf EW |
225 | if test -d "$dotest" |
226 | then | |
889a50e9 JS |
227 | prev_head=$(cat "$dotest/prev_head") |
228 | end=$(cat "$dotest/end") | |
229 | msgnum=$(cat "$dotest/msgnum") | |
230 | onto=$(cat "$dotest/onto") | |
0e987a12 | 231 | GIT_QUIET=$(cat "$dotest/quiet") |
58634dbf EW |
232 | continue_merge |
233 | while test "$msgnum" -le "$end" | |
234 | do | |
235 | call_merge "$msgnum" | |
236 | continue_merge | |
237 | done | |
238 | finish_rb_merge | |
239 | exit | |
240 | fi | |
51ef1daa JS |
241 | head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) && |
242 | onto=$(cat "$GIT_DIR"/rebase-apply/onto) && | |
243 | orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) && | |
0e987a12 | 244 | GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet) |
3f735b66 JS |
245 | git am --resolved --3way --resolvemsg="$RESOLVEMSG" && |
246 | move_to_original_branch | |
cc120056 SE |
247 | exit |
248 | ;; | |
249 | --skip) | |
51ef1daa | 250 | test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || |
cd5320f2 SB |
251 | die "No rebase in progress?" |
252 | ||
fb6e4e1f | 253 | git reset --hard HEAD || exit $? |
58634dbf EW |
254 | if test -d "$dotest" |
255 | then | |
b4372ef1 | 256 | git rerere clear |
889a50e9 JS |
257 | prev_head=$(cat "$dotest/prev_head") |
258 | end=$(cat "$dotest/end") | |
259 | msgnum=$(cat "$dotest/msgnum") | |
d5e673b6 | 260 | msgnum=$(($msgnum + 1)) |
889a50e9 | 261 | onto=$(cat "$dotest/onto") |
0e987a12 | 262 | GIT_QUIET=$(cat "$dotest/quiet") |
d5e673b6 EW |
263 | while test "$msgnum" -le "$end" |
264 | do | |
265 | call_merge "$msgnum" | |
266 | continue_merge | |
267 | done | |
268 | finish_rb_merge | |
269 | exit | |
58634dbf | 270 | fi |
51ef1daa JS |
271 | head_name=$(cat "$GIT_DIR"/rebase-apply/head-name) && |
272 | onto=$(cat "$GIT_DIR"/rebase-apply/onto) && | |
273 | orig_head=$(cat "$GIT_DIR"/rebase-apply/orig-head) && | |
0e987a12 | 274 | GIT_QUIET=$(cat "$GIT_DIR"/rebase-apply/quiet) |
6fd2f5e6 JS |
275 | git am -3 --skip --resolvemsg="$RESOLVEMSG" && |
276 | move_to_original_branch | |
031321c6 SE |
277 | exit |
278 | ;; | |
279 | --abort) | |
51ef1daa | 280 | test -d "$dotest" -o -d "$GIT_DIR"/rebase-apply || |
cd5320f2 SB |
281 | die "No rebase in progress?" |
282 | ||
b4372ef1 | 283 | git rerere clear |
58634dbf EW |
284 | if test -d "$dotest" |
285 | then | |
0e987a12 | 286 | GIT_QUIET=$(cat "$dotest/quiet") |
6fd2f5e6 | 287 | move_to_original_branch |
cd5320f2 | 288 | else |
51ef1daa | 289 | dotest="$GIT_DIR"/rebase-apply |
0e987a12 | 290 | GIT_QUIET=$(cat "$dotest/quiet") |
6fd2f5e6 | 291 | move_to_original_branch |
58634dbf | 292 | fi |
97b88dd5 | 293 | git reset --hard $(cat "$dotest/orig-head") |
48411d22 | 294 | rm -r "$dotest" |
031321c6 SE |
295 | exit |
296 | ;; | |
e646c9c8 JH |
297 | --onto) |
298 | test 2 -le "$#" || usage | |
299 | newbase="$2" | |
300 | shift | |
301 | ;; | |
58634dbf EW |
302 | -M|-m|--m|--me|--mer|--merg|--merge) |
303 | do_merge=t | |
304 | ;; | |
305 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ | |
306 | --strateg=*|--strategy=*|\ | |
307 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) | |
308 | case "$#,$1" in | |
309 | *,*=*) | |
8096fae7 | 310 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
58634dbf EW |
311 | 1,*) |
312 | usage ;; | |
313 | *) | |
314 | strategy="$2" | |
315 | shift ;; | |
316 | esac | |
317 | do_merge=t | |
318 | ;; | |
a9c3821c TAV |
319 | -n|--no-stat) |
320 | diffstat= | |
321 | ;; | |
322 | --stat) | |
323 | diffstat=t | |
324 | ;; | |
b758789c RS |
325 | -v|--verbose) |
326 | verbose=t | |
a9c3821c | 327 | diffstat=t |
0e987a12 SB |
328 | GIT_QUIET= |
329 | ;; | |
330 | -q|--quiet) | |
331 | GIT_QUIET=t | |
332 | git_am_opt="$git_am_opt -q" | |
333 | verbose= | |
334 | diffstat= | |
b758789c | 335 | ;; |
059f446d BF |
336 | --whitespace=*) |
337 | git_am_opt="$git_am_opt $1" | |
b2f82e05 SR |
338 | case "$1" in |
339 | --whitespace=fix|--whitespace=strip) | |
340 | force_rebase=t | |
341 | ;; | |
342 | esac | |
059f446d | 343 | ;; |
86c91f91 GB |
344 | --ignore-whitespace) |
345 | git_am_opt="$git_am_opt $1" | |
346 | ;; | |
570ccad3 MB |
347 | --committer-date-is-author-date|--ignore-date) |
348 | git_am_opt="$git_am_opt $1" | |
349 | force_rebase=t | |
350 | ;; | |
67dad687 | 351 | -C*) |
059f446d | 352 | git_am_opt="$git_am_opt $1" |
67dad687 | 353 | ;; |
190f5323 TR |
354 | --root) |
355 | rebase_root=t | |
356 | ;; | |
3f7d9928 | 357 | -f|--f|--fo|--for|--forc|force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase) |
b2f82e05 SR |
358 | force_rebase=t |
359 | ;; | |
cb6020bb JH |
360 | --rerere-autoupdate|--no-rerere-autoupdate) |
361 | allow_rerere_autoupdate="$1" | |
362 | ;; | |
e646c9c8 JH |
363 | -*) |
364 | usage | |
365 | ;; | |
366 | *) | |
367 | break | |
368 | ;; | |
369 | esac | |
370 | shift | |
371 | done | |
51b2ead0 | 372 | test $# -gt 2 && usage |
2db8aaec | 373 | |
51ef1daa | 374 | # Make sure we do not have $GIT_DIR/rebase-apply |
58634dbf | 375 | if test -z "$do_merge" |
7f4bd5d8 | 376 | then |
9b752a6e | 377 | if mkdir "$GIT_DIR"/rebase-apply 2>/dev/null |
58634dbf | 378 | then |
51ef1daa | 379 | rmdir "$GIT_DIR"/rebase-apply |
58634dbf EW |
380 | else |
381 | echo >&2 ' | |
9b752a6e SB |
382 | It seems that I cannot create a rebase-apply directory, and |
383 | I wonder if you are in the middle of patch application or another | |
51ef1daa JS |
384 | rebase. If that is not the case, please |
385 | rm -fr '"$GIT_DIR"'/rebase-apply | |
9b752a6e | 386 | and run me again. I am stopping in case you still have something |
51ef1daa | 387 | valuable there.' |
58634dbf EW |
388 | exit 1 |
389 | fi | |
390 | else | |
391 | if test -d "$dotest" | |
392 | then | |
28ed6e7b | 393 | die "previous rebase directory $dotest still exists." \ |
9b752a6e | 394 | 'Try git rebase (--continue | --abort | --skip)' |
58634dbf | 395 | fi |
7f4bd5d8 JH |
396 | fi |
397 | ||
7f59dbbb | 398 | # The tree must be really really clean. |
4cfbe06f MM |
399 | if ! git update-index --ignore-submodules --refresh > /dev/null; then |
400 | echo >&2 "cannot rebase: you have unstaged changes" | |
0c3d3ac3 | 401 | git diff-files --name-status -r --ignore-submodules -- >&2 |
4cfbe06f | 402 | exit 1 |
07e62b73 | 403 | fi |
6848d58c | 404 | diff=$(git diff-index --cached --name-status -r --ignore-submodules HEAD --) |
32d99544 | 405 | case "$diff" in |
07e62b73 JK |
406 | ?*) echo >&2 "cannot rebase: your index contains uncommitted changes" |
407 | echo >&2 "$diff" | |
7f59dbbb JH |
408 | exit 1 |
409 | ;; | |
410 | esac | |
99a92f92 | 411 | |
190f5323 TR |
412 | if test -z "$rebase_root" |
413 | then | |
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" | |
421 | else | |
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" | |
427 | fi | |
32d99544 | 428 | |
a1bf91e0 JH |
429 | # Make sure the branch to rebase onto is valid. |
430 | onto_name=${newbase-"$upstream_name"} | |
9f21e97d NS |
431 | case "$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 | ;; |
451 | esac | |
a1bf91e0 | 452 | |
9a111c91 | 453 | # If a hook exists, give it a chance to interrupt |
190f5323 | 454 | run_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" | |
460 | switch_to= | |
59e6b23a | 461 | case "$#" in |
190f5323 | 462 | 1) |
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 | |
475 | usage | |
476 | fi | |
e646c9c8 JH |
477 | ;; |
478 | *) | |
0cb06644 | 479 | # Do not need to switch branches, we are already on it. |
bcf31618 JH |
480 | if branch_name=`git symbolic-ref -q HEAD` |
481 | then | |
0cb06644 | 482 | head_name=$branch_name |
bcf31618 JH |
483 | branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'` |
484 | else | |
0cb06644 | 485 | head_name="detached HEAD" |
bcf31618 JH |
486 | branch_name=HEAD ;# detached |
487 | fi | |
0cb06644 | 488 | branch=$(git rev-parse --verify "${branch_name}^0") || exit |
e646c9c8 | 489 | ;; |
59e6b23a | 490 | esac |
0cb06644 | 491 | orig_head=$branch |
59e6b23a | 492 | |
190f5323 TR |
493 | # Now we are rebasing commits $upstream..$branch (or with --root, |
494 | # everything leading up to $branch) on top of $onto | |
e646c9c8 | 495 | |
1308c17b JS |
496 | # Check if we are already based on $onto with linear history, |
497 | # but this should be done only when upstream and onto are the same. | |
5be60078 | 498 | mb=$(git merge-base "$onto" "$branch") |
1308c17b JS |
499 | if test "$upstream" = "$onto" && test "$mb" = "$onto" && |
500 | # linear history? | |
e1622bfc | 501 | ! (git rev-list --parents "$onto".."$branch" | sane_grep " .* ") > /dev/null |
7f4bd5d8 | 502 | then |
b2f82e05 SR |
503 | if test -z "$force_rebase" |
504 | then | |
505 | # Lazily switch to the target branch if needed... | |
506 | test -z "$switch_to" || git checkout "$switch_to" | |
0e987a12 | 507 | say "Current branch $branch_name is up to date." |
b2f82e05 SR |
508 | exit 0 |
509 | else | |
0e987a12 | 510 | say "Current branch $branch_name is up to date, rebase forced." |
b2f82e05 | 511 | fi |
7f4bd5d8 JH |
512 | fi |
513 | ||
0cb06644 | 514 | # Detach HEAD and reset the tree |
0e987a12 | 515 | say "First, rewinding head to replay your work on top of it..." |
324c2c31 | 516 | git checkout -q "$onto^0" || die "could not detach HEAD" |
22e40795 | 517 | git update-ref ORIG_HEAD $branch |
32d99544 | 518 | |
a9c3821c TAV |
519 | if test -n "$diffstat" |
520 | then | |
521 | if test -n "$verbose" | |
522 | then | |
523 | echo "Changes from $mb to $onto:" | |
524 | fi | |
525 | # We want color (if set), but no pager | |
526 | GIT_PAGER='' git diff --stat --summary "$mb" "$onto" | |
527 | fi | |
528 | ||
e646c9c8 | 529 | # If the $onto is a proper descendant of the tip of the branch, then |
a75d7b54 | 530 | # we just fast-forwarded. |
83c31614 | 531 | if test "$mb" = "$branch" |
32d99544 | 532 | then |
0e987a12 | 533 | say "Fast-forwarded $branch_name to $onto_name." |
6fd2f5e6 | 534 | move_to_original_branch |
32d99544 LS |
535 | exit 0 |
536 | fi | |
537 | ||
190f5323 TR |
538 | if test -n "$rebase_root" |
539 | then | |
540 | revisions="$onto..$orig_head" | |
541 | else | |
542 | revisions="$upstream..$orig_head" | |
543 | fi | |
544 | ||
58634dbf EW |
545 | if test -z "$do_merge" |
546 | then | |
0cb06644 | 547 | git format-patch -k --stdout --full-index --ignore-if-in-upstream \ |
190f5323 | 548 | $root_flag "$revisions" | |
3041c324 | 549 | git am $git_am_opt --rebasing --resolvemsg="$RESOLVEMSG" && |
6fd2f5e6 JS |
550 | move_to_original_branch |
551 | ret=$? | |
51ef1daa JS |
552 | test 0 != $ret -a -d "$GIT_DIR"/rebase-apply && |
553 | echo $head_name > "$GIT_DIR"/rebase-apply/head-name && | |
554 | echo $onto > "$GIT_DIR"/rebase-apply/onto && | |
0e987a12 SB |
555 | echo $orig_head > "$GIT_DIR"/rebase-apply/orig-head && |
556 | echo "$GIT_QUIET" > "$GIT_DIR"/rebase-apply/quiet | |
6fd2f5e6 | 557 | exit $ret |
58634dbf EW |
558 | fi |
559 | ||
560 | # start doing a rebase with git-merge | |
561 | # this is rename-aware if the recursive (default) strategy is used | |
562 | ||
563 | mkdir -p "$dotest" | |
564 | echo "$onto" > "$dotest/onto" | |
0bb733c9 | 565 | echo "$onto_name" > "$dotest/onto_name" |
6fd2f5e6 | 566 | prev_head=$orig_head |
58634dbf | 567 | echo "$prev_head" > "$dotest/prev_head" |
6fd2f5e6 JS |
568 | echo "$orig_head" > "$dotest/orig-head" |
569 | echo "$head_name" > "$dotest/head-name" | |
0e987a12 | 570 | echo "$GIT_QUIET" > "$dotest/quiet" |
58634dbf EW |
571 | |
572 | msgnum=0 | |
190f5323 | 573 | for cmt in `git rev-list --reverse --no-merges "$revisions"` |
58634dbf EW |
574 | do |
575 | msgnum=$(($msgnum + 1)) | |
5887ac82 | 576 | echo "$cmt" > "$dotest/cmt.$msgnum" |
58634dbf EW |
577 | done |
578 | ||
5887ac82 JH |
579 | echo 1 >"$dotest/msgnum" |
580 | echo $msgnum >"$dotest/end" | |
58634dbf EW |
581 | |
582 | end=$msgnum | |
583 | msgnum=1 | |
584 | ||
585 | while test "$msgnum" -le "$end" | |
586 | do | |
587 | call_merge "$msgnum" | |
588 | continue_merge | |
589 | done | |
cc120056 | 590 | |
58634dbf | 591 | finish_rb_merge |