blob: 24a2840033c175ff5399de82a1a777c75c0e85dd [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
Neil Horman90e18182012-04-20 10:36:17 -040046k,keep-empty preserve empty commits during rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050047f,force-rebase! force rebase even if branch is up to date
48X,strategy-option=! pass the argument through to the merge strategy
49stat! display a diffstat of what changed upstream
50n,no-stat! do not show diffstat of what changed upstream
51verify allow pre-rebase hook to run
52rerere-autoupdate allow rerere to update index with resolved conflicts
53root! rebase all reachable commits up to the root(s)
54autosquash move commits that begin with squash!/fixup! under -i
55committer-date-is-author-date! passed to 'git am'
56ignore-date! passed to 'git am'
57whitespace=! passed to 'git apply'
58ignore-whitespace! passed to 'git apply'
59C=! passed to 'git apply'
60 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040061continue! continue
62abort! abort and check out the original branch
63skip! skip current patch and continue
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050064"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080065. git-sh-setup
Shawn O. Pearcef9474132006-12-28 02:34:48 -050066set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040067require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080068cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070069
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080070LF='
71'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050072ok_to_skip_pre_rebase=
73resolvemsg="
Seancc120052006-05-13 23:34:08 -040074When you have resolved this problem run \"git rebase --continue\".
75If you would prefer to skip this patch, instead run \"git rebase --skip\".
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040076To check out the original branch and stop rebasing run \"git rebase --abort\".
Seancc120052006-05-13 23:34:08 -040077"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050078unset onto
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050079strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020080strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070081do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050082merge_dir="$GIT_DIR"/rebase-merge
83apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010084verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010085diffstat=
86test "$(git config --bool rebase.stat)" = true && diffstat=t
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020087git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010088rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010089force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080090allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050091# Non-empty if a rebase was in progress when 'git rebase' was invoked
92in_progress=
93# One of {am, merge, interactive}
94type=
95# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
96state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050097# One of {'', continue, skip, abort}, as parsed from command line
98action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050099preserve_merges=
100autosquash=
Neil Horman90e18182012-04-20 10:36:17 -0400101keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500102test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Eric Wong58634db2006-06-21 03:04:41 -0700103
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500104read_basic_state () {
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500105 head_name=$(cat "$state_dir"/head-name) &&
106 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500107 # We always write to orig-head, but interactive rebase used to write to
108 # head. Fall back to reading from head to cover for the case that the
109 # user upgraded git with an ongoing interactive rebase.
110 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500111 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500112 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500113 else
114 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500115 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500116 GIT_QUIET=$(cat "$state_dir"/quiet) &&
117 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500118 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
119 test -f "$state_dir"/strategy_opts &&
120 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500121 test -f "$state_dir"/allow_rerere_autoupdate &&
122 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500123}
124
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500125write_basic_state () {
126 echo "$head_name" > "$state_dir"/head-name &&
127 echo "$onto" > "$state_dir"/onto &&
128 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500129 echo "$GIT_QUIET" > "$state_dir"/quiet &&
130 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500131 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
132 test -n "$strategy_opts" && echo "$strategy_opts" > \
133 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500134 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
135 "$state_dir"/allow_rerere_autoupdate
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500136}
137
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500138output () {
139 case "$verbose" in
140 '')
141 output=$("$@" 2>&1 )
142 status=$?
143 test $status != 0 && printf "%s\n" "$output"
144 return $status
145 ;;
146 *)
147 "$@"
148 ;;
149 esac
150}
151
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000152move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000153 case "$head_name" in
154 refs/*)
155 message="rebase finished: $head_name onto $onto"
156 git update-ref -m "$message" \
157 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400158 git symbolic-ref \
159 -m "rebase finished: returning to $head_name" \
160 HEAD $head_name ||
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000161 die "Could not move back to $head_name"
162 ;;
163 esac
164}
165
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500166run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200167 if [ "$interactive_rebase" = implied ]; then
168 GIT_EDITOR=:
169 export GIT_EDITOR
170 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500171 . git-rebase--$type
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100172}
173
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900174run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500175 if test -z "$ok_to_skip_pre_rebase" &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900176 test -x "$GIT_DIR/hooks/pre-rebase"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900177 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700178 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
179 die "The pre-rebase hook refused to rebase."
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900180 fi
181}
182
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500183test -f "$apply_dir"/applying &&
Stephan Beyer9b752a62008-08-17 06:25:43 +0200184 die 'It looks like git-am is in progress. Cannot rebase.'
185
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500186if test -d "$apply_dir"
187then
188 type=am
189 state_dir="$apply_dir"
190elif test -d "$merge_dir"
191then
192 if test -f "$merge_dir"/interactive
193 then
194 type=interactive
195 interactive_rebase=explicit
196 else
197 type=merge
198 fi
199 state_dir="$merge_dir"
200fi
201test -n "$type" && in_progress=t
202
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500203total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200204while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800205do
206 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900207 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500208 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900209 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100210 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500211 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100212 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500213 --continue|--skip|--abort)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500214 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500215 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400216 ;;
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800217 --onto)
218 test 2 -le "$#" || usage
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500219 onto="$2"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800220 shift
221 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500222 -i)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500223 interactive_rebase=explicit
224 ;;
Neil Horman90e18182012-04-20 10:36:17 -0400225 -k)
226 keep_empty=yes
227 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500228 -p)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500229 preserve_merges=t
230 test -z "$interactive_rebase" && interactive_rebase=implied
231 ;;
232 --autosquash)
233 autosquash=t
234 ;;
235 --no-autosquash)
236 autosquash=
237 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500238 -M|-m)
Eric Wong58634db2006-06-21 03:04:41 -0700239 do_merge=t
240 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500241 -X)
242 shift
243 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$1")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200244 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500245 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200246 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500247 -s)
248 shift
249 strategy="$1"
Eric Wong58634db2006-06-21 03:04:41 -0700250 do_merge=t
251 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500252 -n)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100253 diffstat=
254 ;;
255 --stat)
256 diffstat=t
257 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500258 -v)
Robert Shearmanb7587892006-10-03 17:29:31 +0100259 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100260 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700261 GIT_QUIET=
262 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500263 -q)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700264 GIT_QUIET=t
265 git_am_opt="$git_am_opt -q"
266 verbose=
267 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100268 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500269 --whitespace)
270 shift
271 git_am_opt="$git_am_opt --whitespace=$1"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100272 case "$1" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500273 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100274 force_rebase=t
275 ;;
276 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400277 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200278 --ignore-whitespace)
279 git_am_opt="$git_am_opt $1"
280 ;;
Michele Ballabio570ccad2009-03-18 21:53:49 +0100281 --committer-date-is-author-date|--ignore-date)
282 git_am_opt="$git_am_opt $1"
283 force_rebase=t
284 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500285 -C)
286 shift
287 git_am_opt="$git_am_opt -C$1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200288 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100289 --root)
290 rebase_root=t
291 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500292 -f|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100293 force_rebase=t
294 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800295 --rerere-autoupdate|--no-rerere-autoupdate)
296 allow_rerere_autoupdate="$1"
297 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500298 --)
299 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800300 break
301 ;;
302 esac
303 shift
304done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500305test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800306
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500307if test -n "$action"
308then
309 test -z "$in_progress" && die "No rebase in progress?"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500310 # Only interactive rebase uses detailed reflog messages
311 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
312 then
313 GIT_REFLOG_ACTION="rebase -i ($action)"
314 export GIT_REFLOG_ACTION
315 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500316fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500317
318case "$action" in
319continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500320 # Sanity check
321 git rev-parse --verify HEAD >/dev/null ||
322 die "Cannot read HEAD"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500323 git update-index --ignore-submodules --refresh &&
324 git diff-files --quiet --ignore-submodules || {
325 echo "You must edit all merge conflicts and then"
326 echo "mark them as resolved using git add"
327 exit 1
328 }
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 ;;
332skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500333 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500334 read_basic_state
335 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500336 ;;
337abort)
338 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500339 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500340 case "$head_name" in
341 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400342 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500343 die "Could not move back to $head_name"
344 ;;
345 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500346 output git reset --hard $orig_head
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500347 rm -r "$state_dir"
348 exit
349 ;;
350esac
351
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500352# Make sure no rebase is in progress
353if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500354then
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500355 die '
356It seems that there is already a '"${state_dir##*/}"' directory, and
357I wonder if you are in the middle of another rebase. If that is the
358case, please try
359 git rebase (--continue | --abort | --skip)
360If that is not the case, please
361 rm -fr '"$state_dir"'
Stephan Beyer9b752a62008-08-17 06:25:43 +0200362and run me again. I am stopping in case you still have something
Johannes Schindelin51ef1da2008-07-21 12:51:02 +0200363valuable there.'
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800364fi
365
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500366if test -n "$interactive_rebase"
367then
368 type=interactive
369 state_dir="$merge_dir"
370elif test -n "$do_merge"
371then
372 type=merge
373 state_dir="$merge_dir"
374else
375 type=am
376 state_dir="$apply_dir"
377fi
378
Thomas Rast190f5322009-01-05 18:35:16 +0100379if test -z "$rebase_root"
380then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500381 case "$#" in
382 0)
383 if ! upstream_name=$(git rev-parse --symbolic-full-name \
384 --verify -q @{upstream} 2>/dev/null)
385 then
386 . git-parse-remote
387 error_on_missing_default_upstream "rebase" "rebase" \
Carlos Martín Nieto3c023962012-03-04 05:41:26 +0100388 "against" "git rebase <branch>"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500389 fi
390 ;;
391 *) upstream_name="$1"
392 shift
393 ;;
394 esac
Thomas Rast190f5322009-01-05 18:35:16 +0100395 upstream=`git rev-parse --verify "${upstream_name}^0"` ||
396 die "invalid upstream $upstream_name"
Thomas Rast190f5322009-01-05 18:35:16 +0100397 upstream_arg="$upstream_name"
398else
Martin von Zweigbergk71786f52011-02-06 13:43:42 -0500399 test -z "$onto" && die "You must specify --onto when using --root"
Thomas Rast190f5322009-01-05 18:35:16 +0100400 unset upstream_name
401 unset upstream
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500402 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100403fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100404
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700405# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500406onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900407case "$onto_name" in
408*...*)
409 if left=${onto_name%...*} right=${onto_name#*...} &&
410 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
411 then
412 case "$onto" in
413 ?*"$LF"?*)
414 die "$onto_name: there are more than one merge bases"
415 ;;
416 '')
417 die "$onto_name: there is no merge base"
418 ;;
419 esac
420 else
Junio C Hamano61dfa1b2009-11-20 03:02:44 -0800421 die "$onto_name: there is no merge base"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900422 fi
423 ;;
424*)
Martin von Zweigbergk71786f52011-02-06 13:43:42 -0500425 onto=$(git rev-parse --verify "${onto_name}^0") ||
426 die "Does not point to a valid commit: $1"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900427 ;;
428esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700429
Junio C Hamano0cb06642008-03-15 13:17:42 -0700430# If the branch to rebase is given, that is the branch we will rebase
431# $branch_name -- branch being rebased, or HEAD (already detached)
432# $orig_head -- commit object name of tip of the branch before rebasing
433# $head_name -- refs/heads/<that-branch> or "detached HEAD"
434switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800435case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01004361)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700437 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100438 branch_name="$1"
439 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700440
Thomas Rast190f5322009-01-05 18:35:16 +0100441 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500442 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800443 then
Thomas Rast190f5322009-01-05 18:35:16 +0100444 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500445 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700446 then
447 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800448 else
Johannes Sixt34840db2011-06-28 14:46:14 +0200449 die "fatal: no such branch: $1"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800450 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800451 ;;
Junio C Hamano0cb06642008-03-15 13:17:42 -0700452*)
453 # Do not need to switch branches, we are already on it.
454 if branch_name=`git symbolic-ref -q HEAD`
455 then
456 head_name=$branch_name
457 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
458 else
459 head_name="detached HEAD"
460 branch_name=HEAD ;# detached
461 fi
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500462 orig_head=$(git rev-parse --verify "${branch_name}^0") || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700463 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800464esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700465
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500466require_clean_work_tree "rebase" "Please commit or stash them."
467
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500468# Now we are rebasing commits $upstream..$orig_head (or with --root,
469# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800470
Johannes Sixt1308c172007-07-04 22:09:10 +0200471# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500472# but this should be done only when upstream and onto are the same
473# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500474mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500475if test "$type" != interactive && test "$upstream" = "$onto" &&
476 test "$mb" = "$onto" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200477 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500478 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800479then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100480 if test -z "$force_rebase"
481 then
482 # Lazily switch to the target branch if needed...
Jeff King3b21a432011-01-26 19:26:59 -0500483 test -z "$switch_to" || git checkout "$switch_to" --
Stephen Boyd0e987a12009-06-16 15:33:01 -0700484 say "Current branch $branch_name is up to date."
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100485 exit 0
486 else
Stephen Boyd0e987a12009-06-16 15:33:01 -0700487 say "Current branch $branch_name is up to date, rebase forced."
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100488 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800489fi
490
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500491# If a hook exists, give it a chance to interrupt
492run_pre_rebase_hook "$upstream_arg" "$@"
493
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100494if test -n "$diffstat"
495then
496 if test -n "$verbose"
497 then
498 echo "Changes from $mb to $onto:"
499 fi
500 # We want color (if set), but no pager
501 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
502fi
503
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500504test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500505
506# Detach HEAD and reset the tree
507say "First, rewinding head to replay your work on top of it..."
508git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500509git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500510
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800511# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300512# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500513if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100514then
Stephen Boyd0e987a12009-06-16 15:33:01 -0700515 say "Fast-forwarded $branch_name to $onto_name."
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000516 move_to_original_branch
Lukas Sandström32d99542005-12-15 00:36:35 +0100517 exit 0
518fi
519
Thomas Rast190f5322009-01-05 18:35:16 +0100520if test -n "$rebase_root"
521then
522 revisions="$onto..$orig_head"
523else
524 revisions="$upstream..$orig_head"
525fi
526
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500527run_specific_rebase