blob: 00ca7b99fef35e21d24af844e6dbaa92cab5834f [file] [log] [blame]
Junio C Hamano59e6b232005-06-25 02:23:43 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -05006USAGE='[--interactive | -i] [-v] [--force-rebase | -f] [--no-ff] [--onto <newbase>] [<upstream>|--root] [<branch>] [--quiet | -q]'
sean031321c2006-04-26 10:49:38 -04007LONG_USAGE='git-rebase replaces <branch> with a new branch of the
8same name. When the --onto option is provided the new branch starts
9out with a HEAD equal to <newbase>, otherwise it is equal to <upstream>
10It then attempts to create a new commit for each commit from the original
11<branch> that does not exist in the <upstream> branch.
Carl Worth69a60af2006-02-21 17:10:12 -080012
sean031321c2006-04-26 10:49:38 -040013It is possible that a merge failure will prevent this process from being
14completely automatic. You will have to resolve any such merge failure
Seancc120052006-05-13 23:34:08 -040015and run git rebase --continue. Another option is to bypass the commit
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040016that caused the merge failure with git rebase --skip. To check out the
Johannes Schindelin51ef1da2008-07-21 12:51:02 +020017original <branch> and remove the .git/rebase-apply working files, use the
18command git rebase --abort instead.
Carl Worth69a60af2006-02-21 17:10:12 -080019
sean031321c2006-04-26 10:49:38 -040020Note that if <branch> is not specified on the command line, the
SZEDER Gábor702088a2008-03-10 15:38:33 +010021currently checked out branch is used.
Junio C Hamanoe646c9c2006-02-14 14:42:05 -080022
sean031321c2006-04-26 10:49:38 -040023Example: git-rebase master~1 topic
Junio C Hamanoe646c9c2006-02-14 14:42:05 -080024
Jon Seymour285c6cb2011-08-05 23:31:29 +100025 A---B---C topic A'\''--B'\''--C'\'' topic
sean031321c2006-04-26 10:49:38 -040026 / --> /
27 D---E---F---G master D---E---F---G master
Junio C Hamanoe646c9c2006-02-14 14:42:05 -080028'
Junio C Hamano533b7032007-01-12 12:52:03 -080029
30SUBDIRECTORY_OK=Yes
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050031OPTIONS_KEEPDASHDASH=
32OPTIONS_SPEC="\
33git rebase [-i] [options] [--onto <newbase>] [<upstream>] [<branch>]
34git rebase [-i] [options] --onto <newbase> --root [<branch>]
35git-rebase [-i] --continue | --abort | --skip
36--
37 Available options are
38v,verbose! display a diffstat of what changed upstream
39q,quiet! be quiet. implies --no-stat
40onto=! rebase onto given branch instead of upstream
41p,preserve-merges! try to recreate merges instead of ignoring them
42s,strategy=! use the given merge strategy
43no-ff! cherry-pick all commits, even if unchanged
44m,merge! use merging strategies to rebase
45i,interactive! let the user edit the list of commits to rebase
46f,force-rebase! force rebase even if branch is up to date
47X,strategy-option=! pass the argument through to the merge strategy
48stat! display a diffstat of what changed upstream
49n,no-stat! do not show diffstat of what changed upstream
50verify allow pre-rebase hook to run
51rerere-autoupdate allow rerere to update index with resolved conflicts
52root! rebase all reachable commits up to the root(s)
53autosquash move commits that begin with squash!/fixup! under -i
54committer-date-is-author-date! passed to 'git am'
55ignore-date! passed to 'git am'
56whitespace=! passed to 'git apply'
57ignore-whitespace! passed to 'git apply'
58C=! passed to 'git apply'
59 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040060continue! continue
61abort! abort and check out the original branch
62skip! skip current patch and continue
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050063"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080064. git-sh-setup
Shawn O. Pearcef9474132006-12-28 02:34:48 -050065set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040066require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080067cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070068
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080069LF='
70'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050071ok_to_skip_pre_rebase=
72resolvemsg="
Seancc120052006-05-13 23:34:08 -040073When you have resolved this problem run \"git rebase --continue\".
74If you would prefer to skip this patch, instead run \"git rebase --skip\".
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040075To check out the original branch and stop rebasing run \"git rebase --abort\".
Seancc120052006-05-13 23:34:08 -040076"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050077unset onto
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050078strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020079strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070080do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050081merge_dir="$GIT_DIR"/rebase-merge
82apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010083verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010084diffstat=
85test "$(git config --bool rebase.stat)" = true && diffstat=t
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020086git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010087rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010088force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080089allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050090# Non-empty if a rebase was in progress when 'git rebase' was invoked
91in_progress=
92# One of {am, merge, interactive}
93type=
94# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
95state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050096# One of {'', continue, skip, abort}, as parsed from command line
97action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050098preserve_merges=
99autosquash=
100test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Eric Wong58634db2006-06-21 03:04:41 -0700101
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500102read_basic_state () {
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500103 head_name=$(cat "$state_dir"/head-name) &&
104 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500105 # We always write to orig-head, but interactive rebase used to write to
106 # head. Fall back to reading from head to cover for the case that the
107 # user upgraded git with an ongoing interactive rebase.
108 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500109 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500110 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500111 else
112 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500113 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500114 GIT_QUIET=$(cat "$state_dir"/quiet) &&
115 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500116 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
117 test -f "$state_dir"/strategy_opts &&
118 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500119 test -f "$state_dir"/allow_rerere_autoupdate &&
120 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500121}
122
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500123write_basic_state () {
124 echo "$head_name" > "$state_dir"/head-name &&
125 echo "$onto" > "$state_dir"/onto &&
126 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500127 echo "$GIT_QUIET" > "$state_dir"/quiet &&
128 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500129 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
130 test -n "$strategy_opts" && echo "$strategy_opts" > \
131 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500132 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
133 "$state_dir"/allow_rerere_autoupdate
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500134}
135
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500136output () {
137 case "$verbose" in
138 '')
139 output=$("$@" 2>&1 )
140 status=$?
141 test $status != 0 && printf "%s\n" "$output"
142 return $status
143 ;;
144 *)
145 "$@"
146 ;;
147 esac
148}
149
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000150move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000151 case "$head_name" in
152 refs/*)
153 message="rebase finished: $head_name onto $onto"
154 git update-ref -m "$message" \
155 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400156 git symbolic-ref \
157 -m "rebase finished: returning to $head_name" \
158 HEAD $head_name ||
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000159 die "Could not move back to $head_name"
160 ;;
161 esac
162}
163
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500164run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200165 if [ "$interactive_rebase" = implied ]; then
166 GIT_EDITOR=:
167 export GIT_EDITOR
168 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500169 . git-rebase--$type
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100170}
171
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900172run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500173 if test -z "$ok_to_skip_pre_rebase" &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900174 test -x "$GIT_DIR/hooks/pre-rebase"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900175 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700176 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
177 die "The pre-rebase hook refused to rebase."
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900178 fi
179}
180
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500181test -f "$apply_dir"/applying &&
Stephan Beyer9b752a62008-08-17 06:25:43 +0200182 die 'It looks like git-am is in progress. Cannot rebase.'
183
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500184if test -d "$apply_dir"
185then
186 type=am
187 state_dir="$apply_dir"
188elif test -d "$merge_dir"
189then
190 if test -f "$merge_dir"/interactive
191 then
192 type=interactive
193 interactive_rebase=explicit
194 else
195 type=merge
196 fi
197 state_dir="$merge_dir"
198fi
199test -n "$type" && in_progress=t
200
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500201total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200202while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800203do
204 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900205 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500206 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900207 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100208 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500209 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100210 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500211 --continue|--skip|--abort)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500212 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500213 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400214 ;;
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800215 --onto)
216 test 2 -le "$#" || usage
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500217 onto="$2"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800218 shift
219 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500220 -i)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500221 interactive_rebase=explicit
222 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500223 -p)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500224 preserve_merges=t
225 test -z "$interactive_rebase" && interactive_rebase=implied
226 ;;
227 --autosquash)
228 autosquash=t
229 ;;
230 --no-autosquash)
231 autosquash=
232 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500233 -M|-m)
Eric Wong58634db2006-06-21 03:04:41 -0700234 do_merge=t
235 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500236 -X)
237 shift
238 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$1")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200239 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500240 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200241 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500242 -s)
243 shift
244 strategy="$1"
Eric Wong58634db2006-06-21 03:04:41 -0700245 do_merge=t
246 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500247 -n)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100248 diffstat=
249 ;;
250 --stat)
251 diffstat=t
252 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500253 -v)
Robert Shearmanb7587892006-10-03 17:29:31 +0100254 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100255 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700256 GIT_QUIET=
257 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500258 -q)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700259 GIT_QUIET=t
260 git_am_opt="$git_am_opt -q"
261 verbose=
262 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100263 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500264 --whitespace)
265 shift
266 git_am_opt="$git_am_opt --whitespace=$1"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100267 case "$1" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500268 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100269 force_rebase=t
270 ;;
271 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400272 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200273 --ignore-whitespace)
274 git_am_opt="$git_am_opt $1"
275 ;;
Michele Ballabio570ccad2009-03-18 21:53:49 +0100276 --committer-date-is-author-date|--ignore-date)
277 git_am_opt="$git_am_opt $1"
278 force_rebase=t
279 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500280 -C)
281 shift
282 git_am_opt="$git_am_opt -C$1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200283 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100284 --root)
285 rebase_root=t
286 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500287 -f|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100288 force_rebase=t
289 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800290 --rerere-autoupdate|--no-rerere-autoupdate)
291 allow_rerere_autoupdate="$1"
292 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500293 --)
294 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800295 break
296 ;;
297 esac
298 shift
299done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500300test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800301
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500302if test -n "$action"
303then
304 test -z "$in_progress" && die "No rebase in progress?"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500305 # Only interactive rebase uses detailed reflog messages
306 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
307 then
308 GIT_REFLOG_ACTION="rebase -i ($action)"
309 export GIT_REFLOG_ACTION
310 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500311fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500312
313case "$action" in
314continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500315 # Sanity check
316 git rev-parse --verify HEAD >/dev/null ||
317 die "Cannot read HEAD"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500318 git update-index --ignore-submodules --refresh &&
319 git diff-files --quiet --ignore-submodules || {
320 echo "You must edit all merge conflicts and then"
321 echo "mark them as resolved using git add"
322 exit 1
323 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500324 read_basic_state
325 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500326 ;;
327skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500328 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500329 read_basic_state
330 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500331 ;;
332abort)
333 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500334 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500335 case "$head_name" in
336 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400337 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500338 die "Could not move back to $head_name"
339 ;;
340 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500341 output git reset --hard $orig_head
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500342 rm -r "$state_dir"
343 exit
344 ;;
345esac
346
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500347# Make sure no rebase is in progress
348if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500349then
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500350 die '
351It seems that there is already a '"${state_dir##*/}"' directory, and
352I wonder if you are in the middle of another rebase. If that is the
353case, please try
354 git rebase (--continue | --abort | --skip)
355If that is not the case, please
356 rm -fr '"$state_dir"'
Stephan Beyer9b752a62008-08-17 06:25:43 +0200357and run me again. I am stopping in case you still have something
Johannes Schindelin51ef1da2008-07-21 12:51:02 +0200358valuable there.'
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800359fi
360
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500361if test -n "$interactive_rebase"
362then
363 type=interactive
364 state_dir="$merge_dir"
365elif test -n "$do_merge"
366then
367 type=merge
368 state_dir="$merge_dir"
369else
370 type=am
371 state_dir="$apply_dir"
372fi
373
Thomas Rast190f5322009-01-05 18:35:16 +0100374if test -z "$rebase_root"
375then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500376 case "$#" in
377 0)
378 if ! upstream_name=$(git rev-parse --symbolic-full-name \
379 --verify -q @{upstream} 2>/dev/null)
380 then
381 . git-parse-remote
382 error_on_missing_default_upstream "rebase" "rebase" \
383 "against" "git rebase <upstream branch>"
384 fi
385 ;;
386 *) upstream_name="$1"
387 shift
388 ;;
389 esac
Thomas Rast190f5322009-01-05 18:35:16 +0100390 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
391 die "invalid upstream $upstream_name"
Thomas Rast190f5322009-01-05 18:35:16 +0100392 upstream_arg="$upstream_name"
393else
Martin von Zweigbergk71786f52011-02-06 13:43:42 -0500394 test -z "$onto" && die "You must specify --onto when using --root"
Thomas Rast190f5322009-01-05 18:35:16 +0100395 unset upstream_name
396 unset upstream
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500397 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100398fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100399
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700400# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500401onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900402case "$onto_name" in
403*...*)
404 if left=${onto_name%...*} right=${onto_name#*...} &&
405 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
406 then
407 case "$onto" in
408 ?*"$LF"?*)
409 die "$onto_name: there are more than one merge bases"
410 ;;
411 '')
412 die "$onto_name: there is no merge base"
413 ;;
414 esac
415 else
Junio C Hamano61dfa1b2009-11-20 03:02:44 -0800416 die "$onto_name: there is no merge base"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900417 fi
418 ;;
419*)
Martin von Zweigbergk71786f52011-02-06 13:43:42 -0500420 onto=$(git rev-parse --verify "${onto_name}^0") ||
421 die "Does not point to a valid commit: $1"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900422 ;;
423esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700424
Junio C Hamano0cb06642008-03-15 13:17:42 -0700425# If the branch to rebase is given, that is the branch we will rebase
426# $branch_name -- branch being rebased, or HEAD (already detached)
427# $orig_head -- commit object name of tip of the branch before rebasing
428# $head_name -- refs/heads/<that-branch> or "detached HEAD"
429switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800430case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01004311)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700432 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100433 branch_name="$1"
434 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700435
Thomas Rast190f5322009-01-05 18:35:16 +0100436 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500437 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800438 then
Thomas Rast190f5322009-01-05 18:35:16 +0100439 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500440 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700441 then
442 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800443 else
Johannes Sixt34840db2011-06-28 14:46:14 +0200444 die "fatal: no such branch: $1"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800445 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800446 ;;
Junio C Hamano0cb06642008-03-15 13:17:42 -0700447*)
448 # Do not need to switch branches, we are already on it.
449 if branch_name=`git symbolic-ref -q HEAD`
450 then
451 head_name=$branch_name
452 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
453 else
454 head_name="detached HEAD"
455 branch_name=HEAD ;# detached
456 fi
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500457 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700458 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800459esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700460
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500461require_clean_work_tree "rebase" "Please commit or stash them."
462
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500463# Now we are rebasing commits $upstream..$orig_head (or with --root,
464# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800465
Johannes Sixt1308c172007-07-04 22:09:10 +0200466# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500467# but this should be done only when upstream and onto are the same
468# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500469mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500470if test "$type" != interactive && test "$upstream" = "$onto" &&
471 test "$mb" = "$onto" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200472 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500473 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800474then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100475 if test -z "$force_rebase"
476 then
477 # Lazily switch to the target branch if needed...
Jeff King3b21a432011-01-26 19:26:59 -0500478 test -z "$switch_to" || git checkout "$switch_to" --
Stephen Boyd0e987a12009-06-16 15:33:01 -0700479 say "Current branch $branch_name is up to date."
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100480 exit 0
481 else
Stephen Boyd0e987a12009-06-16 15:33:01 -0700482 say "Current branch $branch_name is up to date, rebase forced."
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100483 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800484fi
485
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500486# If a hook exists, give it a chance to interrupt
487run_pre_rebase_hook "$upstream_arg" "$@"
488
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100489if test -n "$diffstat"
490then
491 if test -n "$verbose"
492 then
493 echo "Changes from $mb to $onto:"
494 fi
495 # We want color (if set), but no pager
496 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
497fi
498
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500499test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500500
501# Detach HEAD and reset the tree
502say "First, rewinding head to replay your work on top of it..."
503git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500504git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500505
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800506# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300507# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500508if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100509then
Stephen Boyd0e987a12009-06-16 15:33:01 -0700510 say "Fast-forwarded $branch_name to $onto_name."
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000511 move_to_original_branch
Lukas Sandström32d99542005-12-15 00:36:35 +0100512 exit 0
513fi
514
Thomas Rast190f5322009-01-05 18:35:16 +0100515if test -n "$rebase_root"
516then
517 revisions="$onto..$orig_head"
518else
519 revisions="$upstream..$orig_head"
520fi
521
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500522run_specific_rebase