blob: 06c810b64fe04d3675820e38d7eeabe7589dab56 [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=
Nicolas Vigierb6e9e732014-02-10 01:03:36 +00008OPTIONS_STUCKLONG=t
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -05009OPTIONS_SPEC="\
Lucien Kongc2145382012-06-12 10:05:12 +020010git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
Junio C Hamano0cd993a2012-07-15 21:38:41 -070011git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040012git-rebase --continue | --abort | --skip | --edit-todo
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050013--
14 Available options are
15v,verbose! display a diffstat of what changed upstream
16q,quiet! be quiet. implies --no-stat
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053017autostash! automatically stash/stash pop before and after
John Keepingad8261d2013-12-09 23:16:16 +000018fork-point use 'merge-base --fork-point' to refine upstream
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050019onto=! rebase onto given branch instead of upstream
20p,preserve-merges! try to recreate merges instead of ignoring them
21s,strategy=! use the given merge strategy
22no-ff! cherry-pick all commits, even if unchanged
23m,merge! use merging strategies to rebase
24i,interactive! let the user edit the list of commits to rebase
Lucien Kongc2145382012-06-12 10:05:12 +020025x,exec=! add exec lines after each commit of the editable list
Neil Horman90e18182012-04-20 10:36:17 -040026k,keep-empty preserve empty commits during rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050027f,force-rebase! force rebase even if branch is up to date
28X,strategy-option=! pass the argument through to the merge strategy
29stat! display a diffstat of what changed upstream
30n,no-stat! do not show diffstat of what changed upstream
31verify allow pre-rebase hook to run
32rerere-autoupdate allow rerere to update index with resolved conflicts
33root! rebase all reachable commits up to the root(s)
34autosquash move commits that begin with squash!/fixup! under -i
35committer-date-is-author-date! passed to 'git am'
36ignore-date! passed to 'git am'
37whitespace=! passed to 'git apply'
38ignore-whitespace! passed to 'git apply'
39C=! passed to 'git apply'
Nicolas Vigier3ee5e542014-02-10 01:03:37 +000040S,gpg-sign? GPG-sign commits
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050041 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040042continue! continue
43abort! abort and check out the original branch
44skip! skip current patch and continue
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040045edit-todo! edit the todo list during an interactive rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050046"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080047. git-sh-setup
Jiang Xinc7108bf2012-07-25 22:53:08 +080048. git-sh-i18n
Shawn O. Pearcef9474132006-12-28 02:34:48 -050049set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040050require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080051cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070052
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080053LF='
54'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050055ok_to_skip_pre_rebase=
56resolvemsg="
Jiang Xinc7108bf2012-07-25 22:53:08 +080057$(gettext 'When you have resolved this problem, run "git rebase --continue".
58If you prefer to skip this patch, run "git rebase --skip" instead.
59To check out the original branch and stop rebasing, run "git rebase --abort".')
Seancc120052006-05-13 23:34:08 -040060"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050061unset onto
Lucien Kongc2145382012-06-12 10:05:12 +020062cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050063strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020064strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070065do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050066merge_dir="$GIT_DIR"/rebase-merge
67apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010068verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010069diffstat=
70test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053071autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000072fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020073git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010074rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010075force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080076allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050077# Non-empty if a rebase was in progress when 'git rebase' was invoked
78in_progress=
79# One of {am, merge, interactive}
80type=
81# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
82state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050083# One of {'', continue, skip, abort}, as parsed from command line
84action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050085preserve_merges=
86autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040087keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050088test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Nicolas Vigier3ee5e542014-02-10 01:03:37 +000089gpg_sign_opt=
Eric Wong58634db2006-06-21 03:04:41 -070090
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -050091read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +053092 test -f "$state_dir/head-name" &&
93 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -050094 head_name=$(cat "$state_dir"/head-name) &&
95 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -050096 # We always write to orig-head, but interactive rebase used to write to
97 # head. Fall back to reading from head to cover for the case that the
98 # user upgraded git with an ongoing interactive rebase.
99 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500100 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500101 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500102 else
103 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500104 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500105 GIT_QUIET=$(cat "$state_dir"/quiet) &&
106 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500107 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
108 test -f "$state_dir"/strategy_opts &&
109 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500110 test -f "$state_dir"/allow_rerere_autoupdate &&
111 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000112 test -f "$state_dir"/gpg_sign_opt &&
113 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500114}
115
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500116write_basic_state () {
117 echo "$head_name" > "$state_dir"/head-name &&
118 echo "$onto" > "$state_dir"/onto &&
119 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500120 echo "$GIT_QUIET" > "$state_dir"/quiet &&
121 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500122 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
123 test -n "$strategy_opts" && echo "$strategy_opts" > \
124 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500125 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
126 "$state_dir"/allow_rerere_autoupdate
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000127 test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500128}
129
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500130output () {
131 case "$verbose" in
132 '')
133 output=$("$@" 2>&1 )
134 status=$?
135 test $status != 0 && printf "%s\n" "$output"
136 return $status
137 ;;
138 *)
139 "$@"
140 ;;
141 esac
142}
143
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000144move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000145 case "$head_name" in
146 refs/*)
147 message="rebase finished: $head_name onto $onto"
148 git update-ref -m "$message" \
149 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400150 git symbolic-ref \
151 -m "rebase finished: returning to $head_name" \
152 HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800153 die "$(gettext "Could not move back to $head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000154 ;;
155 esac
156}
157
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400158apply_autostash () {
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530159 if test -f "$state_dir/autostash"
160 then
161 stash_sha1=$(cat "$state_dir/autostash")
162 if git stash apply $stash_sha1 2>&1 >/dev/null
163 then
164 echo "$(gettext 'Applied autostash.')"
165 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530166 git stash store -m "autostash" -q $stash_sha1 ||
167 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530168 gettext 'Applying autostash resulted in conflicts.
169Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200170You can run "git stash pop" or "git stash drop" at any time.
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530171'
172 fi
173 fi
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400174}
175
176finish_rebase () {
177 apply_autostash &&
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530178 git gc --auto &&
179 rm -rf "$state_dir"
180}
181
Kyle J. McKay8cd65962014-04-11 01:28:18 -0700182run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200183 if [ "$interactive_rebase" = implied ]; then
184 GIT_EDITOR=:
185 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000186 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200187 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500188 . git-rebase--$type
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530189 ret=$?
190 if test $ret -eq 0
191 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530192 finish_rebase
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400193 elif test $ret -eq 2 # special exit status for rebase -i
194 then
195 apply_autostash &&
196 rm -rf "$state_dir" &&
197 die "Nothing to do"
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530198 fi
199 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100200}
201
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900202run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500203 if test -z "$ok_to_skip_pre_rebase" &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900204 test -x "$GIT_DIR/hooks/pre-rebase"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900205 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700206 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800207 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900208 fi
209}
210
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500211test -f "$apply_dir"/applying &&
Jiang Xinc7108bf2012-07-25 22:53:08 +0800212 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200213
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500214if test -d "$apply_dir"
215then
216 type=am
217 state_dir="$apply_dir"
218elif test -d "$merge_dir"
219then
220 if test -f "$merge_dir"/interactive
221 then
222 type=interactive
223 interactive_rebase=explicit
224 else
225 type=merge
226 fi
227 state_dir="$merge_dir"
228fi
229test -n "$type" && in_progress=t
230
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500231total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200232while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800233do
234 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900235 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500236 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900237 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100238 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500239 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100240 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400241 --continue|--skip|--abort|--edit-todo)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500242 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500243 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400244 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000245 --onto=*)
246 onto="${1#--onto=}"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800247 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000248 --exec=*)
249 cmd="${cmd}exec ${1#--exec=}${LF}"
Lucien Kongc2145382012-06-12 10:05:12 +0200250 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000251 --interactive)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500252 interactive_rebase=explicit
253 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000254 --keep-empty)
Neil Horman90e18182012-04-20 10:36:17 -0400255 keep_empty=yes
256 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000257 --preserve-merges)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500258 preserve_merges=t
259 test -z "$interactive_rebase" && interactive_rebase=implied
260 ;;
261 --autosquash)
262 autosquash=t
263 ;;
264 --no-autosquash)
265 autosquash=
266 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000267 --fork-point)
268 fork_point=t
269 ;;
270 --no-fork-point)
271 fork_point=
272 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000273 --merge)
Eric Wong58634db2006-06-21 03:04:41 -0700274 do_merge=t
275 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000276 --strategy-option=*)
277 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200278 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500279 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200280 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000281 --strategy=*)
282 strategy="${1#--strategy=}"
Eric Wong58634db2006-06-21 03:04:41 -0700283 do_merge=t
284 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000285 --no-stat)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100286 diffstat=
287 ;;
288 --stat)
289 diffstat=t
290 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530291 --autostash)
292 autostash=true
293 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000294 --verbose)
Robert Shearmanb7587892006-10-03 17:29:31 +0100295 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100296 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700297 GIT_QUIET=
298 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000299 --quiet)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700300 GIT_QUIET=t
301 git_am_opt="$git_am_opt -q"
302 verbose=
303 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100304 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000305 --whitespace=*)
306 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
307 case "${1#--whitespace=}" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500308 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100309 force_rebase=t
310 ;;
311 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400312 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200313 --ignore-whitespace)
314 git_am_opt="$git_am_opt $1"
315 ;;
Michele Ballabio570ccad2009-03-18 21:53:49 +0100316 --committer-date-is-author-date|--ignore-date)
317 git_am_opt="$git_am_opt $1"
318 force_rebase=t
319 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000320 -C*)
321 git_am_opt="$git_am_opt $1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200322 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100323 --root)
324 rebase_root=t
325 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000326 --force-rebase|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100327 force_rebase=t
328 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800329 --rerere-autoupdate|--no-rerere-autoupdate)
330 allow_rerere_autoupdate="$1"
331 ;;
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000332 --gpg-sign)
333 gpg_sign_opt=-S
334 ;;
335 --gpg-sign=*)
336 gpg_sign_opt="-S${1#--gpg-sign=}"
337 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500338 --)
339 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800340 break
341 ;;
342 esac
343 shift
344done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500345test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800346
Lucien Kongc2145382012-06-12 10:05:12 +0200347if test -n "$cmd" &&
348 test "$interactive_rebase" != explicit
349then
Jiang Xin465d6a02012-07-25 22:53:09 +0800350 die "$(gettext "The --exec option must be used with the --interactive option")"
Lucien Kongc2145382012-06-12 10:05:12 +0200351fi
352
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500353if test -n "$action"
354then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800355 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500356 # Only interactive rebase uses detailed reflog messages
357 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
358 then
359 GIT_REFLOG_ACTION="rebase -i ($action)"
360 export GIT_REFLOG_ACTION
361 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500362fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500363
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400364if test "$action" = "edit-todo" && test "$type" != "interactive"
365then
366 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
367fi
368
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500369case "$action" in
370continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500371 # Sanity check
372 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800373 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500374 git update-index --ignore-submodules --refresh &&
375 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800376 echo "$(gettext "You must edit all merge conflicts and then
377mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500378 exit 1
379 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500380 read_basic_state
381 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500382 ;;
383skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500384 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500385 read_basic_state
386 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500387 ;;
388abort)
389 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500390 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500391 case "$head_name" in
392 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400393 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800394 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500395 ;;
396 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500397 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530398 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500399 exit
400 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400401edit-todo)
402 run_specific_rebase
403 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500404esac
405
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500406# Make sure no rebase is in progress
407if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500408then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800409 state_dir_base=${state_dir##*/}
410 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
411 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
412 die "
413$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200414I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500415case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800416 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500417If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800418 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200419and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800420valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800421fi
422
Chris Webbdf5df202012-06-26 22:55:23 +0100423if test -n "$rebase_root" && test -z "$onto"
424then
425 test -z "$interactive_rebase" && interactive_rebase=implied
426fi
427
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500428if test -n "$interactive_rebase"
429then
430 type=interactive
431 state_dir="$merge_dir"
432elif test -n "$do_merge"
433then
434 type=merge
435 state_dir="$merge_dir"
436else
437 type=am
438 state_dir="$apply_dir"
439fi
440
Thomas Rast190f5322009-01-05 18:35:16 +0100441if test -z "$rebase_root"
442then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500443 case "$#" in
444 0)
445 if ! upstream_name=$(git rev-parse --symbolic-full-name \
446 --verify -q @{upstream} 2>/dev/null)
447 then
448 . git-parse-remote
449 error_on_missing_default_upstream "rebase" "rebase" \
Carlos Martín Nieto3c023962012-03-04 05:41:26 +0100450 "against" "git rebase <branch>"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500451 fi
John Keepingad8261d2013-12-09 23:16:16 +0000452
453 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500454 ;;
455 *) upstream_name="$1"
Brian Gesiak4f407402014-03-19 20:02:15 +0900456 if test "$upstream_name" = "-"
457 then
458 upstream_name="@{-1}"
459 fi
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500460 shift
461 ;;
462 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530463 upstream=$(peel_committish "${upstream_name}") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800464 die "$(eval_gettext "invalid upstream \$upstream_name")"
Thomas Rast190f5322009-01-05 18:35:16 +0100465 upstream_arg="$upstream_name"
466else
Chris Webbdf5df202012-06-26 22:55:23 +0100467 if test -z "$onto"
468 then
Elia Pinto728fc792014-04-23 06:44:00 -0700469 empty_tree=$(git hash-object -t tree /dev/null)
470 onto=$(git commit-tree $empty_tree </dev/null)
Chris Webbdf5df202012-06-26 22:55:23 +0100471 squash_onto="$onto"
472 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100473 unset upstream_name
474 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700475 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500476 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100477fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100478
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700479# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500480onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900481case "$onto_name" in
482*...*)
483 if left=${onto_name%...*} right=${onto_name#*...} &&
484 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
485 then
486 case "$onto" in
487 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800488 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900489 ;;
490 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800491 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900492 ;;
493 esac
494 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800495 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900496 fi
497 ;;
498*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530499 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800500 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900501 ;;
502esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700503
Junio C Hamano0cb06642008-03-15 13:17:42 -0700504# If the branch to rebase is given, that is the branch we will rebase
505# $branch_name -- branch being rebased, or HEAD (already detached)
506# $orig_head -- commit object name of tip of the branch before rebasing
507# $head_name -- refs/heads/<that-branch> or "detached HEAD"
508switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800509case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01005101)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700511 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100512 branch_name="$1"
513 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700514
Thomas Rast190f5322009-01-05 18:35:16 +0100515 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500516 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800517 then
Thomas Rast190f5322009-01-05 18:35:16 +0100518 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500519 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700520 then
521 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800522 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800523 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800524 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800525 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07005260)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700527 # Do not need to switch branches, we are already on it.
Elia Pinto728fc792014-04-23 06:44:00 -0700528 if branch_name=$(git symbolic-ref -q HEAD)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700529 then
530 head_name=$branch_name
Elia Pinto728fc792014-04-23 06:44:00 -0700531 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
Junio C Hamano0cb06642008-03-15 13:17:42 -0700532 else
533 head_name="detached HEAD"
534 branch_name=HEAD ;# detached
535 fi
Phil Hordea709802013-04-23 18:51:14 -0400536 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700537 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700538*)
539 die "BUG: unexpected number of arguments left to parse"
540 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800541esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700542
John Keepingad8261d2013-12-09 23:16:16 +0000543if test "$fork_point" = t
544then
John Keepingbb3f4582014-01-09 19:47:34 +0000545 new_upstream=$(git merge-base --fork-point "$upstream_name" \
546 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000547 if test -n "$new_upstream"
548 then
549 upstream=$new_upstream
550 fi
551fi
552
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530553if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
554then
555 stash_sha1=$(git stash create "autostash") ||
556 die "$(gettext 'Cannot autostash')"
557
558 mkdir -p "$state_dir" &&
559 echo $stash_sha1 >"$state_dir/autostash" &&
560 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
561 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
562 git reset --hard
563fi
564
Jiang Xinc7108bf2012-07-25 22:53:08 +0800565require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500566
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500567# Now we are rebasing commits $upstream..$orig_head (or with --root,
568# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800569
Johannes Sixt1308c172007-07-04 22:09:10 +0200570# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500571# but this should be done only when upstream and onto are the same
572# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500573mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500574if test "$type" != interactive && test "$upstream" = "$onto" &&
575 test "$mb" = "$onto" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200576 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500577 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800578then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100579 if test -z "$force_rebase"
580 then
581 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530582 test -z "$switch_to" ||
583 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
584 git checkout "$switch_to" --
Jiang Xinc7108bf2012-07-25 22:53:08 +0800585 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530586 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100587 exit 0
588 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800589 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100590 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800591fi
592
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500593# If a hook exists, give it a chance to interrupt
594run_pre_rebase_hook "$upstream_arg" "$@"
595
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100596if test -n "$diffstat"
597then
598 if test -n "$verbose"
599 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800600 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100601 fi
602 # We want color (if set), but no pager
603 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
604fi
605
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500606test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500607
608# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800609say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530610
611GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
612 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500613git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500614
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800615# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300616# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500617if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100618then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800619 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000620 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530621 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100622 exit 0
623fi
624
Thomas Rast190f5322009-01-05 18:35:16 +0100625if test -n "$rebase_root"
626then
627 revisions="$onto..$orig_head"
628else
629 revisions="$upstream..$orig_head"
630fi
631
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500632run_specific_rebase