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