blob: 07e2bd48dc60811db7ee5e44c127aa0480c0ec1d [file] [log] [blame]
Junio C Hamano59e6b232005-06-25 02:23:43 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
Junio C Hamano533b7032007-01-12 12:52:03 -08006SUBDIRECTORY_OK=Yes
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -05007OPTIONS_KEEPDASHDASH=
8OPTIONS_SPEC="\
Lucien Kongc2145382012-06-12 10:05:12 +02009git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
Junio C Hamano0cd993a2012-07-15 21:38:41 -070010git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040011git-rebase --continue | --abort | --skip | --edit-todo
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050012--
13 Available options are
14v,verbose! display a diffstat of what changed upstream
15q,quiet! be quiet. implies --no-stat
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053016autostash! automatically stash/stash pop before and after
John Keepingad8261d2013-12-09 23:16:16 +000017fork-point use 'merge-base --fork-point' to refine upstream
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050018onto=! rebase onto given branch instead of upstream
19p,preserve-merges! try to recreate merges instead of ignoring them
20s,strategy=! use the given merge strategy
21no-ff! cherry-pick all commits, even if unchanged
22m,merge! use merging strategies to rebase
23i,interactive! let the user edit the list of commits to rebase
Lucien Kongc2145382012-06-12 10:05:12 +020024x,exec=! add exec lines after each commit of the editable list
Neil Horman90e18182012-04-20 10:36:17 -040025k,keep-empty preserve empty commits during rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050026f,force-rebase! force rebase even if branch is up to date
27X,strategy-option=! pass the argument through to the merge strategy
28stat! display a diffstat of what changed upstream
29n,no-stat! do not show diffstat of what changed upstream
30verify allow pre-rebase hook to run
31rerere-autoupdate allow rerere to update index with resolved conflicts
32root! rebase all reachable commits up to the root(s)
33autosquash move commits that begin with squash!/fixup! under -i
34committer-date-is-author-date! passed to 'git am'
35ignore-date! passed to 'git am'
36whitespace=! passed to 'git apply'
37ignore-whitespace! passed to 'git apply'
38C=! passed to 'git apply'
39 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040040continue! continue
41abort! abort and check out the original branch
42skip! skip current patch and continue
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040043edit-todo! edit the todo list during an interactive rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050044"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080045. git-sh-setup
Jiang Xinc7108bf2012-07-25 22:53:08 +080046. git-sh-i18n
Shawn O. Pearcef9474132006-12-28 02:34:48 -050047set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040048require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080049cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070050
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080051LF='
52'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050053ok_to_skip_pre_rebase=
54resolvemsg="
Jiang Xinc7108bf2012-07-25 22:53:08 +080055$(gettext 'When you have resolved this problem, run "git rebase --continue".
56If you prefer to skip this patch, run "git rebase --skip" instead.
57To check out the original branch and stop rebasing, run "git rebase --abort".')
Seancc120052006-05-13 23:34:08 -040058"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050059unset onto
Lucien Kongc2145382012-06-12 10:05:12 +020060cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050061strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020062strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070063do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050064merge_dir="$GIT_DIR"/rebase-merge
65apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010066verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010067diffstat=
68test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053069autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000070fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020071git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010072rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010073force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080074allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050075# Non-empty if a rebase was in progress when 'git rebase' was invoked
76in_progress=
77# One of {am, merge, interactive}
78type=
79# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
80state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050081# One of {'', continue, skip, abort}, as parsed from command line
82action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050083preserve_merges=
84autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040085keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050086test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Eric Wong58634db2006-06-21 03:04:41 -070087
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -050088read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +053089 test -f "$state_dir/head-name" &&
90 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -050091 head_name=$(cat "$state_dir"/head-name) &&
92 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -050093 # We always write to orig-head, but interactive rebase used to write to
94 # head. Fall back to reading from head to cover for the case that the
95 # user upgraded git with an ongoing interactive rebase.
96 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -050097 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -050098 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -050099 else
100 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500101 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500102 GIT_QUIET=$(cat "$state_dir"/quiet) &&
103 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500104 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
105 test -f "$state_dir"/strategy_opts &&
106 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500107 test -f "$state_dir"/allow_rerere_autoupdate &&
108 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500109}
110
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500111write_basic_state () {
112 echo "$head_name" > "$state_dir"/head-name &&
113 echo "$onto" > "$state_dir"/onto &&
114 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500115 echo "$GIT_QUIET" > "$state_dir"/quiet &&
116 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500117 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
118 test -n "$strategy_opts" && echo "$strategy_opts" > \
119 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500120 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
121 "$state_dir"/allow_rerere_autoupdate
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500122}
123
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500124output () {
125 case "$verbose" in
126 '')
127 output=$("$@" 2>&1 )
128 status=$?
129 test $status != 0 && printf "%s\n" "$output"
130 return $status
131 ;;
132 *)
133 "$@"
134 ;;
135 esac
136}
137
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000138move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000139 case "$head_name" in
140 refs/*)
141 message="rebase finished: $head_name onto $onto"
142 git update-ref -m "$message" \
143 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400144 git symbolic-ref \
145 -m "rebase finished: returning to $head_name" \
146 HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800147 die "$(gettext "Could not move back to $head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000148 ;;
149 esac
150}
151
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530152finish_rebase () {
153 if test -f "$state_dir/autostash"
154 then
155 stash_sha1=$(cat "$state_dir/autostash")
156 if git stash apply $stash_sha1 2>&1 >/dev/null
157 then
158 echo "$(gettext 'Applied autostash.')"
159 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530160 git stash store -m "autostash" -q $stash_sha1 ||
161 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530162 gettext 'Applying autostash resulted in conflicts.
163Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200164You can run "git stash pop" or "git stash drop" at any time.
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530165'
166 fi
167 fi
168 git gc --auto &&
169 rm -rf "$state_dir"
170}
171
Kyle J. McKay8cd65962014-04-11 01:28:18 -0700172run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200173 if [ "$interactive_rebase" = implied ]; then
174 GIT_EDITOR=:
175 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000176 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200177 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500178 . git-rebase--$type
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530179 ret=$?
180 if test $ret -eq 0
181 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530182 finish_rebase
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530183 fi
184 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100185}
186
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900187run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500188 if test -z "$ok_to_skip_pre_rebase" &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900189 test -x "$GIT_DIR/hooks/pre-rebase"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900190 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700191 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800192 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900193 fi
194}
195
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500196test -f "$apply_dir"/applying &&
Jiang Xinc7108bf2012-07-25 22:53:08 +0800197 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200198
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500199if test -d "$apply_dir"
200then
201 type=am
202 state_dir="$apply_dir"
203elif test -d "$merge_dir"
204then
205 if test -f "$merge_dir"/interactive
206 then
207 type=interactive
208 interactive_rebase=explicit
209 else
210 type=merge
211 fi
212 state_dir="$merge_dir"
213fi
214test -n "$type" && in_progress=t
215
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500216total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200217while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800218do
219 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900220 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500221 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900222 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100223 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500224 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100225 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400226 --continue|--skip|--abort|--edit-todo)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500227 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500228 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400229 ;;
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800230 --onto)
231 test 2 -le "$#" || usage
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500232 onto="$2"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800233 shift
234 ;;
Lucien Kongc2145382012-06-12 10:05:12 +0200235 -x)
236 test 2 -le "$#" || usage
237 cmd="${cmd}exec $2${LF}"
238 shift
239 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500240 -i)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500241 interactive_rebase=explicit
242 ;;
Neil Horman90e18182012-04-20 10:36:17 -0400243 -k)
244 keep_empty=yes
245 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500246 -p)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500247 preserve_merges=t
248 test -z "$interactive_rebase" && interactive_rebase=implied
249 ;;
250 --autosquash)
251 autosquash=t
252 ;;
253 --no-autosquash)
254 autosquash=
255 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000256 --fork-point)
257 fork_point=t
258 ;;
259 --no-fork-point)
260 fork_point=
261 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500262 -M|-m)
Eric Wong58634db2006-06-21 03:04:41 -0700263 do_merge=t
264 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500265 -X)
266 shift
267 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--$1")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200268 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500269 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200270 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500271 -s)
272 shift
273 strategy="$1"
Eric Wong58634db2006-06-21 03:04:41 -0700274 do_merge=t
275 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500276 -n)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100277 diffstat=
278 ;;
279 --stat)
280 diffstat=t
281 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530282 --autostash)
283 autostash=true
284 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500285 -v)
Robert Shearmanb7587892006-10-03 17:29:31 +0100286 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100287 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700288 GIT_QUIET=
289 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500290 -q)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700291 GIT_QUIET=t
292 git_am_opt="$git_am_opt -q"
293 verbose=
294 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100295 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500296 --whitespace)
297 shift
298 git_am_opt="$git_am_opt --whitespace=$1"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100299 case "$1" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500300 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100301 force_rebase=t
302 ;;
303 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400304 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200305 --ignore-whitespace)
306 git_am_opt="$git_am_opt $1"
307 ;;
Michele Ballabio570ccad2009-03-18 21:53:49 +0100308 --committer-date-is-author-date|--ignore-date)
309 git_am_opt="$git_am_opt $1"
310 force_rebase=t
311 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500312 -C)
313 shift
314 git_am_opt="$git_am_opt -C$1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200315 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100316 --root)
317 rebase_root=t
318 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500319 -f|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100320 force_rebase=t
321 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800322 --rerere-autoupdate|--no-rerere-autoupdate)
323 allow_rerere_autoupdate="$1"
324 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500325 --)
326 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800327 break
328 ;;
329 esac
330 shift
331done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500332test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800333
Lucien Kongc2145382012-06-12 10:05:12 +0200334if test -n "$cmd" &&
335 test "$interactive_rebase" != explicit
336then
Jiang Xin465d6a02012-07-25 22:53:09 +0800337 die "$(gettext "The --exec option must be used with the --interactive option")"
Lucien Kongc2145382012-06-12 10:05:12 +0200338fi
339
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500340if test -n "$action"
341then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800342 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500343 # Only interactive rebase uses detailed reflog messages
344 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
345 then
346 GIT_REFLOG_ACTION="rebase -i ($action)"
347 export GIT_REFLOG_ACTION
348 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500349fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500350
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400351if test "$action" = "edit-todo" && test "$type" != "interactive"
352then
353 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
354fi
355
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500356case "$action" in
357continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500358 # Sanity check
359 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800360 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500361 git update-index --ignore-submodules --refresh &&
362 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800363 echo "$(gettext "You must edit all merge conflicts and then
364mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500365 exit 1
366 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500367 read_basic_state
368 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500369 ;;
370skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500371 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500372 read_basic_state
373 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500374 ;;
375abort)
376 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500377 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500378 case "$head_name" in
379 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400380 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800381 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500382 ;;
383 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500384 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530385 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500386 exit
387 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400388edit-todo)
389 run_specific_rebase
390 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500391esac
392
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500393# Make sure no rebase is in progress
394if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500395then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800396 state_dir_base=${state_dir##*/}
397 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
398 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
399 die "
400$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200401I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500402case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800403 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500404If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800405 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200406and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800407valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800408fi
409
Chris Webbdf5df202012-06-26 22:55:23 +0100410if test -n "$rebase_root" && test -z "$onto"
411then
412 test -z "$interactive_rebase" && interactive_rebase=implied
413fi
414
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500415if test -n "$interactive_rebase"
416then
417 type=interactive
418 state_dir="$merge_dir"
419elif test -n "$do_merge"
420then
421 type=merge
422 state_dir="$merge_dir"
423else
424 type=am
425 state_dir="$apply_dir"
426fi
427
Thomas Rast190f5322009-01-05 18:35:16 +0100428if test -z "$rebase_root"
429then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500430 case "$#" in
431 0)
432 if ! upstream_name=$(git rev-parse --symbolic-full-name \
433 --verify -q @{upstream} 2>/dev/null)
434 then
435 . git-parse-remote
436 error_on_missing_default_upstream "rebase" "rebase" \
Carlos Martín Nieto3c023962012-03-04 05:41:26 +0100437 "against" "git rebase <branch>"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500438 fi
John Keepingad8261d2013-12-09 23:16:16 +0000439
440 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500441 ;;
442 *) upstream_name="$1"
443 shift
444 ;;
445 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530446 upstream=$(peel_committish "${upstream_name}") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800447 die "$(eval_gettext "invalid upstream \$upstream_name")"
Thomas Rast190f5322009-01-05 18:35:16 +0100448 upstream_arg="$upstream_name"
449else
Chris Webbdf5df202012-06-26 22:55:23 +0100450 if test -z "$onto"
451 then
452 empty_tree=`git hash-object -t tree /dev/null`
453 onto=`git commit-tree $empty_tree </dev/null`
454 squash_onto="$onto"
455 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100456 unset upstream_name
457 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700458 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500459 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100460fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100461
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700462# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500463onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900464case "$onto_name" in
465*...*)
466 if left=${onto_name%...*} right=${onto_name#*...} &&
467 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
468 then
469 case "$onto" in
470 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800471 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900472 ;;
473 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800474 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900475 ;;
476 esac
477 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800478 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900479 fi
480 ;;
481*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530482 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800483 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900484 ;;
485esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700486
Junio C Hamano0cb06642008-03-15 13:17:42 -0700487# If the branch to rebase is given, that is the branch we will rebase
488# $branch_name -- branch being rebased, or HEAD (already detached)
489# $orig_head -- commit object name of tip of the branch before rebasing
490# $head_name -- refs/heads/<that-branch> or "detached HEAD"
491switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800492case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01004931)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700494 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100495 branch_name="$1"
496 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700497
Thomas Rast190f5322009-01-05 18:35:16 +0100498 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500499 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800500 then
Thomas Rast190f5322009-01-05 18:35:16 +0100501 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500502 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700503 then
504 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800505 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800506 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800507 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800508 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07005090)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700510 # Do not need to switch branches, we are already on it.
511 if branch_name=`git symbolic-ref -q HEAD`
512 then
513 head_name=$branch_name
514 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
515 else
516 head_name="detached HEAD"
517 branch_name=HEAD ;# detached
518 fi
Phil Hordea709802013-04-23 18:51:14 -0400519 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700520 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700521*)
522 die "BUG: unexpected number of arguments left to parse"
523 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800524esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700525
John Keepingad8261d2013-12-09 23:16:16 +0000526if test "$fork_point" = t
527then
John Keepingbb3f4582014-01-09 19:47:34 +0000528 new_upstream=$(git merge-base --fork-point "$upstream_name" \
529 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000530 if test -n "$new_upstream"
531 then
532 upstream=$new_upstream
533 fi
534fi
535
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530536if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
537then
538 stash_sha1=$(git stash create "autostash") ||
539 die "$(gettext 'Cannot autostash')"
540
541 mkdir -p "$state_dir" &&
542 echo $stash_sha1 >"$state_dir/autostash" &&
543 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
544 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
545 git reset --hard
546fi
547
Jiang Xinc7108bf2012-07-25 22:53:08 +0800548require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500549
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500550# Now we are rebasing commits $upstream..$orig_head (or with --root,
551# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800552
Johannes Sixt1308c172007-07-04 22:09:10 +0200553# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500554# but this should be done only when upstream and onto are the same
555# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500556mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500557if test "$type" != interactive && test "$upstream" = "$onto" &&
558 test "$mb" = "$onto" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200559 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500560 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800561then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100562 if test -z "$force_rebase"
563 then
564 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530565 test -z "$switch_to" ||
566 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
567 git checkout "$switch_to" --
Jiang Xinc7108bf2012-07-25 22:53:08 +0800568 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530569 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100570 exit 0
571 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800572 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100573 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800574fi
575
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500576# If a hook exists, give it a chance to interrupt
577run_pre_rebase_hook "$upstream_arg" "$@"
578
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100579if test -n "$diffstat"
580then
581 if test -n "$verbose"
582 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800583 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100584 fi
585 # We want color (if set), but no pager
586 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
587fi
588
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500589test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500590
591# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800592say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530593
594GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
595 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500596git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500597
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800598# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300599# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500600if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100601then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800602 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000603 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530604 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100605 exit 0
606fi
607
Thomas Rast190f5322009-01-05 18:35:16 +0100608if test -n "$rebase_root"
609then
610 revisions="$onto..$orig_head"
611else
612 revisions="$upstream..$orig_head"
613fi
614
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500615run_specific_rebase