]>
Commit | Line | Data |
---|---|---|
59e6b23a JH |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2005 Junio C Hamano. | |
4 | # | |
5 | ||
bffd750a | 6 | USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--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 | ' | |
6bb4e485 MZ |
39 | ok_to_skip_pre_rebase= |
40 | resolvemsg=" | |
cc120056 SE |
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 | " | |
6bb4e485 | 45 | unset onto |
9765b6ab | 46 | strategy= |
93ce190c | 47 | strategy_opts= |
58634dbf | 48 | do_merge= |
69a636ad MZ |
49 | merge_dir="$GIT_DIR"/rebase-merge |
50 | apply_dir="$GIT_DIR"/rebase-apply | |
b758789c | 51 | verbose= |
9474a029 MZ |
52 | diffstat= |
53 | test "$(git config --bool rebase.stat)" = true && diffstat=t | |
67dad687 | 54 | git_am_opt= |
190f5323 | 55 | rebase_root= |
b2f82e05 | 56 | force_rebase= |
cb6020bb | 57 | allow_rerere_autoupdate= |
99de0640 MZ |
58 | # Non-empty if a rebase was in progress when 'git rebase' was invoked |
59 | in_progress= | |
60 | # One of {am, merge, interactive} | |
61 | type= | |
62 | # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge} | |
63 | state_dir= | |
34262322 MZ |
64 | # One of {'', continue, skip, abort}, as parsed from command line |
65 | action= | |
cf432ca0 MZ |
66 | preserve_merges= |
67 | autosquash= | |
68 | test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t | |
58634dbf | 69 | |
fa99c1e1 | 70 | read_basic_state () { |
02ac45fa MZ |
71 | head_name=$(cat "$state_dir"/head-name) && |
72 | onto=$(cat "$state_dir"/onto) && | |
84df4560 MZ |
73 | # We always write to orig-head, but interactive rebase used to write to |
74 | # head. Fall back to reading from head to cover for the case that the | |
75 | # user upgraded git with an ongoing interactive rebase. | |
76 | if test -f "$state_dir"/orig-head | |
2959c283 | 77 | then |
2959c283 | 78 | orig_head=$(cat "$state_dir"/orig-head) |
84df4560 MZ |
79 | else |
80 | orig_head=$(cat "$state_dir"/head) | |
2959c283 | 81 | fi && |
7b37a7c6 MZ |
82 | GIT_QUIET=$(cat "$state_dir"/quiet) && |
83 | test -f "$state_dir"/verbose && verbose=t | |
02ac45fa MZ |
84 | } |
85 | ||
84df4560 MZ |
86 | write_basic_state () { |
87 | echo "$head_name" > "$state_dir"/head-name && | |
88 | echo "$onto" > "$state_dir"/onto && | |
89 | echo "$orig_head" > "$state_dir"/orig-head && | |
7b37a7c6 MZ |
90 | echo "$GIT_QUIET" > "$state_dir"/quiet && |
91 | test t = "$verbose" && : > "$state_dir"/verbose | |
84df4560 MZ |
92 | } |
93 | ||
4974c2ca MZ |
94 | output () { |
95 | case "$verbose" in | |
96 | '') | |
97 | output=$("$@" 2>&1 ) | |
98 | status=$? | |
99 | test $status != 0 && printf "%s\n" "$output" | |
100 | return $status | |
101 | ;; | |
102 | *) | |
103 | "$@" | |
104 | ;; | |
105 | esac | |
106 | } | |
107 | ||
6fd2f5e6 | 108 | move_to_original_branch () { |
6fd2f5e6 JS |
109 | case "$head_name" in |
110 | refs/*) | |
111 | message="rebase finished: $head_name onto $onto" | |
112 | git update-ref -m "$message" \ | |
113 | $head_name $(git rev-parse HEAD) $orig_head && | |
114 | git symbolic-ref HEAD $head_name || | |
115 | die "Could not move back to $head_name" | |
116 | ;; | |
117 | esac | |
118 | } | |
119 | ||
fa99c1e1 | 120 | run_specific_rebase () { |
f8cca019 AE |
121 | if [ "$interactive_rebase" = implied ]; then |
122 | GIT_EDITOR=: | |
123 | export GIT_EDITOR | |
124 | fi | |
46df82d5 | 125 | . git-rebase--$type |
1b1dce4b JS |
126 | } |
127 | ||
d70b4a8f | 128 | run_pre_rebase_hook () { |
6bb4e485 | 129 | if test -z "$ok_to_skip_pre_rebase" && |
c4427656 | 130 | test -x "$GIT_DIR/hooks/pre-rebase" |
d70b4a8f | 131 | then |
bc2bbc45 SB |
132 | "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} || |
133 | die "The pre-rebase hook refused to rebase." | |
d70b4a8f NS |
134 | fi |
135 | } | |
136 | ||
69a636ad | 137 | test -f "$apply_dir"/applying && |
9b752a6e SB |
138 | die 'It looks like git-am is in progress. Cannot rebase.' |
139 | ||
99de0640 MZ |
140 | if test -d "$apply_dir" |
141 | then | |
142 | type=am | |
143 | state_dir="$apply_dir" | |
144 | elif test -d "$merge_dir" | |
145 | then | |
146 | if test -f "$merge_dir"/interactive | |
147 | then | |
148 | type=interactive | |
149 | interactive_rebase=explicit | |
150 | else | |
151 | type=merge | |
152 | fi | |
153 | state_dir="$merge_dir" | |
154 | fi | |
155 | test -n "$type" && in_progress=t | |
156 | ||
95135b06 | 157 | total_argc=$# |
822f7c73 | 158 | while test $# != 0 |
e646c9c8 JH |
159 | do |
160 | case "$1" in | |
c4427656 | 161 | --no-verify) |
6bb4e485 | 162 | ok_to_skip_pre_rebase=yes |
c4427656 | 163 | ;; |
7baf9c4b | 164 | --verify) |
6bb4e485 | 165 | ok_to_skip_pre_rebase= |
7baf9c4b | 166 | ;; |
34262322 | 167 | --continue|--skip|--abort) |
95135b06 | 168 | test $total_argc -eq 1 || usage |
34262322 | 169 | action=${1##--} |
031321c6 | 170 | ;; |
e646c9c8 JH |
171 | --onto) |
172 | test 2 -le "$#" || usage | |
6bb4e485 | 173 | onto="$2" |
e646c9c8 JH |
174 | shift |
175 | ;; | |
cf432ca0 MZ |
176 | -i|--interactive) |
177 | interactive_rebase=explicit | |
178 | ;; | |
179 | -p|--preserve-merges) | |
180 | preserve_merges=t | |
181 | test -z "$interactive_rebase" && interactive_rebase=implied | |
182 | ;; | |
183 | --autosquash) | |
184 | autosquash=t | |
185 | ;; | |
186 | --no-autosquash) | |
187 | autosquash= | |
188 | ;; | |
58634dbf EW |
189 | -M|-m|--m|--me|--mer|--merg|--merge) |
190 | do_merge=t | |
191 | ;; | |
93ce190c ML |
192 | -X*|--strategy-option*) |
193 | case "$#,$1" in | |
194 | 1,-X|1,--strategy-option) | |
195 | usage ;; | |
196 | *,-X|*,--strategy-option) | |
197 | newopt="$2" | |
198 | shift ;; | |
199 | *,--strategy-option=*) | |
200 | newopt="$(expr " $1" : ' --strategy-option=\(.*\)')" ;; | |
201 | *,-X*) | |
202 | newopt="$(expr " $1" : ' -X\(.*\)')" ;; | |
203 | 1,*) | |
204 | usage ;; | |
205 | esac | |
206 | strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$newopt")" | |
207 | do_merge=t | |
9765b6ab | 208 | test -z "$strategy" && strategy=recursive |
93ce190c | 209 | ;; |
58634dbf EW |
210 | -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\ |
211 | --strateg=*|--strategy=*|\ | |
212 | -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy) | |
213 | case "$#,$1" in | |
214 | *,*=*) | |
8096fae7 | 215 | strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;; |
58634dbf EW |
216 | 1,*) |
217 | usage ;; | |
218 | *) | |
219 | strategy="$2" | |
220 | shift ;; | |
221 | esac | |
222 | do_merge=t | |
223 | ;; | |
a9c3821c TAV |
224 | -n|--no-stat) |
225 | diffstat= | |
226 | ;; | |
227 | --stat) | |
228 | diffstat=t | |
229 | ;; | |
b758789c RS |
230 | -v|--verbose) |
231 | verbose=t | |
a9c3821c | 232 | diffstat=t |
0e987a12 SB |
233 | GIT_QUIET= |
234 | ;; | |
235 | -q|--quiet) | |
236 | GIT_QUIET=t | |
237 | git_am_opt="$git_am_opt -q" | |
238 | verbose= | |
239 | diffstat= | |
b758789c | 240 | ;; |
059f446d BF |
241 | --whitespace=*) |
242 | git_am_opt="$git_am_opt $1" | |
b2f82e05 SR |
243 | case "$1" in |
244 | --whitespace=fix|--whitespace=strip) | |
245 | force_rebase=t | |
246 | ;; | |
247 | esac | |
059f446d | 248 | ;; |
86c91f91 GB |
249 | --ignore-whitespace) |
250 | git_am_opt="$git_am_opt $1" | |
251 | ;; | |
570ccad3 MB |
252 | --committer-date-is-author-date|--ignore-date) |
253 | git_am_opt="$git_am_opt $1" | |
254 | force_rebase=t | |
255 | ;; | |
67dad687 | 256 | -C*) |
059f446d | 257 | git_am_opt="$git_am_opt $1" |
67dad687 | 258 | ;; |
190f5323 TR |
259 | --root) |
260 | rebase_root=t | |
261 | ;; | |
1bc6bf6d | 262 | -f|--f|--fo|--for|--forc|--force|--force-r|--force-re|--force-reb|--force-reba|--force-rebas|--force-rebase|--no-ff) |
b2f82e05 SR |
263 | force_rebase=t |
264 | ;; | |
cb6020bb JH |
265 | --rerere-autoupdate|--no-rerere-autoupdate) |
266 | allow_rerere_autoupdate="$1" | |
267 | ;; | |
e646c9c8 JH |
268 | -*) |
269 | usage | |
270 | ;; | |
271 | *) | |
272 | break | |
273 | ;; | |
274 | esac | |
275 | shift | |
276 | done | |
51b2ead0 | 277 | test $# -gt 2 && usage |
2db8aaec | 278 | |
cf432ca0 MZ |
279 | if test -n "$action" |
280 | then | |
281 | test -z "$in_progress" && die "No rebase in progress?" | |
2959c283 MZ |
282 | # Only interactive rebase uses detailed reflog messages |
283 | if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase | |
284 | then | |
285 | GIT_REFLOG_ACTION="rebase -i ($action)" | |
286 | export GIT_REFLOG_ACTION | |
287 | fi | |
cf432ca0 | 288 | fi |
34262322 MZ |
289 | |
290 | case "$action" in | |
291 | continue) | |
2959c283 MZ |
292 | # Sanity check |
293 | git rev-parse --verify HEAD >/dev/null || | |
294 | die "Cannot read HEAD" | |
34262322 MZ |
295 | git update-index --ignore-submodules --refresh && |
296 | git diff-files --quiet --ignore-submodules || { | |
297 | echo "You must edit all merge conflicts and then" | |
298 | echo "mark them as resolved using git add" | |
299 | exit 1 | |
300 | } | |
fa99c1e1 MZ |
301 | read_basic_state |
302 | run_specific_rebase | |
34262322 MZ |
303 | ;; |
304 | skip) | |
4974c2ca | 305 | output git reset --hard HEAD || exit $? |
fa99c1e1 MZ |
306 | read_basic_state |
307 | run_specific_rebase | |
34262322 MZ |
308 | ;; |
309 | abort) | |
310 | git rerere clear | |
fa99c1e1 | 311 | read_basic_state |
34262322 MZ |
312 | case "$head_name" in |
313 | refs/*) | |
314 | git symbolic-ref HEAD $head_name || | |
315 | die "Could not move back to $head_name" | |
316 | ;; | |
317 | esac | |
4974c2ca | 318 | output git reset --hard $orig_head |
34262322 MZ |
319 | rm -r "$state_dir" |
320 | exit | |
321 | ;; | |
322 | esac | |
323 | ||
99de0640 MZ |
324 | # Make sure no rebase is in progress |
325 | if test -n "$in_progress" | |
7f4bd5d8 | 326 | then |
99de0640 MZ |
327 | die ' |
328 | It seems that there is already a '"${state_dir##*/}"' directory, and | |
329 | I wonder if you are in the middle of another rebase. If that is the | |
330 | case, please try | |
331 | git rebase (--continue | --abort | --skip) | |
332 | If that is not the case, please | |
333 | rm -fr '"$state_dir"' | |
9b752a6e | 334 | and run me again. I am stopping in case you still have something |
51ef1daa | 335 | valuable there.' |
7f4bd5d8 JH |
336 | fi |
337 | ||
99de0640 MZ |
338 | test $# -eq 0 && test -z "$rebase_root" && usage |
339 | ||
cf432ca0 MZ |
340 | if test -n "$interactive_rebase" |
341 | then | |
342 | type=interactive | |
343 | state_dir="$merge_dir" | |
344 | elif test -n "$do_merge" | |
345 | then | |
346 | type=merge | |
347 | state_dir="$merge_dir" | |
348 | else | |
349 | type=am | |
350 | state_dir="$apply_dir" | |
351 | fi | |
352 | ||
190f5323 TR |
353 | if test -z "$rebase_root" |
354 | then | |
355 | # The upstream head must be given. Make sure it is valid. | |
356 | upstream_name="$1" | |
357 | shift | |
358 | upstream=`git rev-parse --verify "${upstream_name}^0"` || | |
359 | die "invalid upstream $upstream_name" | |
190f5323 TR |
360 | upstream_arg="$upstream_name" |
361 | else | |
71786f54 | 362 | test -z "$onto" && die "You must specify --onto when using --root" |
190f5323 TR |
363 | unset upstream_name |
364 | unset upstream | |
46df82d5 | 365 | upstream_arg=--root |
190f5323 | 366 | fi |
32d99544 | 367 | |
a1bf91e0 | 368 | # Make sure the branch to rebase onto is valid. |
6bb4e485 | 369 | onto_name=${onto-"$upstream_name"} |
9f21e97d NS |
370 | case "$onto_name" in |
371 | *...*) | |
372 | if left=${onto_name%...*} right=${onto_name#*...} && | |
373 | onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD}) | |
374 | then | |
375 | case "$onto" in | |
376 | ?*"$LF"?*) | |
377 | die "$onto_name: there are more than one merge bases" | |
378 | ;; | |
379 | '') | |
380 | die "$onto_name: there is no merge base" | |
381 | ;; | |
382 | esac | |
383 | else | |
61dfa1bb | 384 | die "$onto_name: there is no merge base" |
9f21e97d NS |
385 | fi |
386 | ;; | |
387 | *) | |
71786f54 MZ |
388 | onto=$(git rev-parse --verify "${onto_name}^0") || |
389 | die "Does not point to a valid commit: $1" | |
9f21e97d NS |
390 | ;; |
391 | esac | |
a1bf91e0 | 392 | |
0cb06644 JH |
393 | # If the branch to rebase is given, that is the branch we will rebase |
394 | # $branch_name -- branch being rebased, or HEAD (already detached) | |
395 | # $orig_head -- commit object name of tip of the branch before rebasing | |
396 | # $head_name -- refs/heads/<that-branch> or "detached HEAD" | |
397 | switch_to= | |
59e6b23a | 398 | case "$#" in |
190f5323 | 399 | 1) |
0cb06644 | 400 | # Is it "rebase other $branchname" or "rebase other $commit"? |
190f5323 TR |
401 | branch_name="$1" |
402 | switch_to="$1" | |
0cb06644 | 403 | |
190f5323 | 404 | if git show-ref --verify --quiet -- "refs/heads/$1" && |
cb82a05d | 405 | orig_head=$(git rev-parse -q --verify "refs/heads/$1") |
0cb06644 | 406 | then |
190f5323 | 407 | head_name="refs/heads/$1" |
cb82a05d | 408 | elif orig_head=$(git rev-parse -q --verify "$1") |
0cb06644 JH |
409 | then |
410 | head_name="detached HEAD" | |
411 | else | |
4ac5356c | 412 | echo >&2 "fatal: no such branch: $1" |
0cb06644 JH |
413 | usage |
414 | fi | |
e646c9c8 JH |
415 | ;; |
416 | *) | |
0cb06644 | 417 | # Do not need to switch branches, we are already on it. |
bcf31618 JH |
418 | if branch_name=`git symbolic-ref -q HEAD` |
419 | then | |
0cb06644 | 420 | head_name=$branch_name |
bcf31618 JH |
421 | branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'` |
422 | else | |
0cb06644 | 423 | head_name="detached HEAD" |
bcf31618 JH |
424 | branch_name=HEAD ;# detached |
425 | fi | |
cb82a05d | 426 | orig_head=$(git rev-parse --verify "${branch_name}^0") || exit |
e646c9c8 | 427 | ;; |
59e6b23a JH |
428 | esac |
429 | ||
8f9bfb64 MZ |
430 | require_clean_work_tree "rebase" "Please commit or stash them." |
431 | ||
cb82a05d MZ |
432 | # Now we are rebasing commits $upstream..$orig_head (or with --root, |
433 | # everything leading up to $orig_head) on top of $onto | |
e646c9c8 | 434 | |
1308c17b | 435 | # Check if we are already based on $onto with linear history, |
cc1453e1 MZ |
436 | # but this should be done only when upstream and onto are the same |
437 | # and if this is not an interactive rebase. | |
cb82a05d | 438 | mb=$(git merge-base "$onto" "$orig_head") |
cc1453e1 MZ |
439 | if test "$type" != interactive && test "$upstream" = "$onto" && |
440 | test "$mb" = "$onto" && | |
1308c17b | 441 | # linear history? |
cb82a05d | 442 | ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null |
7f4bd5d8 | 443 | then |
b2f82e05 SR |
444 | if test -z "$force_rebase" |
445 | then | |
446 | # Lazily switch to the target branch if needed... | |
3b21a438 | 447 | test -z "$switch_to" || git checkout "$switch_to" -- |
0e987a12 | 448 | say "Current branch $branch_name is up to date." |
b2f82e05 SR |
449 | exit 0 |
450 | else | |
0e987a12 | 451 | say "Current branch $branch_name is up to date, rebase forced." |
b2f82e05 | 452 | fi |
7f4bd5d8 JH |
453 | fi |
454 | ||
8f9bfb64 MZ |
455 | # If a hook exists, give it a chance to interrupt |
456 | run_pre_rebase_hook "$upstream_arg" "$@" | |
457 | ||
a9c3821c TAV |
458 | if test -n "$diffstat" |
459 | then | |
460 | if test -n "$verbose" | |
461 | then | |
462 | echo "Changes from $mb to $onto:" | |
463 | fi | |
464 | # We want color (if set), but no pager | |
465 | GIT_PAGER='' git diff --stat --summary "$mb" "$onto" | |
466 | fi | |
467 | ||
fa99c1e1 | 468 | test "$type" = interactive && run_specific_rebase |
f4107d9c MZ |
469 | |
470 | # Detach HEAD and reset the tree | |
471 | say "First, rewinding head to replay your work on top of it..." | |
472 | git checkout -q "$onto^0" || die "could not detach HEAD" | |
cb82a05d | 473 | git update-ref ORIG_HEAD $orig_head |
f4107d9c | 474 | |
e646c9c8 | 475 | # If the $onto is a proper descendant of the tip of the branch, then |
a75d7b54 | 476 | # we just fast-forwarded. |
cb82a05d | 477 | if test "$mb" = "$orig_head" |
32d99544 | 478 | then |
0e987a12 | 479 | say "Fast-forwarded $branch_name to $onto_name." |
6fd2f5e6 | 480 | move_to_original_branch |
32d99544 LS |
481 | exit 0 |
482 | fi | |
483 | ||
190f5323 TR |
484 | if test -n "$rebase_root" |
485 | then | |
486 | revisions="$onto..$orig_head" | |
487 | else | |
488 | revisions="$upstream..$orig_head" | |
489 | fi | |
490 | ||
fa99c1e1 | 491 | run_specific_rebase |