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