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