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