blob: 0bced9b0959f51f448bd7dd1947b5b658499951b [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
John Keeping619e3602015-09-10 23:30:51 +010017autostash 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'
Giuseppe Bilotta9f795242017-04-18 11:29:05 +020037signoff passed to 'git am'
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050038whitespace=! passed to 'git apply'
39ignore-whitespace! passed to 'git apply'
40C=! passed to 'git apply'
Nicolas Vigier3ee5e542014-02-10 01:03:37 +000041S,gpg-sign? GPG-sign commits
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050042 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040043continue! continue
44abort! abort and check out the original branch
45skip! skip current patch and continue
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040046edit-todo! edit the todo list during an interactive rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050047"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080048. git-sh-setup
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
John Keeping1e0dacd2014-07-16 20:23:49 +010062unset restrict_revision
Lucien Kongc2145382012-06-12 10:05:12 +020063cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050064strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020065strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070066do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050067merge_dir="$GIT_DIR"/rebase-merge
68apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010069verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010070diffstat=
71test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053072autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000073fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020074git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010075rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010076force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080077allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050078# Non-empty if a rebase was in progress when 'git rebase' was invoked
79in_progress=
80# One of {am, merge, interactive}
81type=
82# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
83state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050084# One of {'', continue, skip, abort}, as parsed from command line
85action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050086preserve_merges=
87autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040088keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050089test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Junio C Hamano66948562016-05-02 14:58:45 -070090case "$(git config --bool commit.gpgsign)" in
91true) gpg_sign_opt=-S ;;
92*) gpg_sign_opt= ;;
93esac
Eric Wong58634db2006-06-21 03:04:41 -070094
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -050095read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +053096 test -f "$state_dir/head-name" &&
97 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -050098 head_name=$(cat "$state_dir"/head-name) &&
99 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500100 # We always write to orig-head, but interactive rebase used to write to
101 # head. Fall back to reading from head to cover for the case that the
102 # user upgraded git with an ongoing interactive rebase.
103 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500104 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500105 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500106 else
107 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500108 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500109 GIT_QUIET=$(cat "$state_dir"/quiet) &&
110 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500111 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
112 test -f "$state_dir"/strategy_opts &&
113 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500114 test -f "$state_dir"/allow_rerere_autoupdate &&
115 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000116 test -f "$state_dir"/gpg_sign_opt &&
117 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500118}
119
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500120write_basic_state () {
121 echo "$head_name" > "$state_dir"/head-name &&
122 echo "$onto" > "$state_dir"/onto &&
123 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500124 echo "$GIT_QUIET" > "$state_dir"/quiet &&
125 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500126 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
127 test -n "$strategy_opts" && echo "$strategy_opts" > \
128 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500129 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
130 "$state_dir"/allow_rerere_autoupdate
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000131 test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500132}
133
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500134output () {
135 case "$verbose" in
136 '')
137 output=$("$@" 2>&1 )
138 status=$?
139 test $status != 0 && printf "%s\n" "$output"
140 return $status
141 ;;
142 *)
143 "$@"
144 ;;
145 esac
146}
147
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000148move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000149 case "$head_name" in
150 refs/*)
151 message="rebase finished: $head_name onto $onto"
152 git update-ref -m "$message" \
153 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400154 git symbolic-ref \
155 -m "rebase finished: returning to $head_name" \
156 HEAD $head_name ||
Vasco Almeida24a6df42016-06-17 20:20:58 +0000157 die "$(eval_gettext "Could not move back to \$head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000158 ;;
159 esac
160}
161
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400162apply_autostash () {
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530163 if test -f "$state_dir/autostash"
164 then
165 stash_sha1=$(cat "$state_dir/autostash")
166 if git stash apply $stash_sha1 2>&1 >/dev/null
167 then
168 echo "$(gettext 'Applied autostash.')"
169 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530170 git stash store -m "autostash" -q $stash_sha1 ||
171 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530172 gettext 'Applying autostash resulted in conflicts.
173Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200174You can run "git stash pop" or "git stash drop" at any time.
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530175'
176 fi
177 fi
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400178}
179
180finish_rebase () {
181 apply_autostash &&
Jeff King8c24f5b2016-01-13 13:47:18 -0500182 { git gc --auto || true; } &&
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530183 rm -rf "$state_dir"
184}
185
Kyle J. McKay8cd65962014-04-11 01:28:18 -0700186run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200187 if [ "$interactive_rebase" = implied ]; then
188 GIT_EDITOR=:
189 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000190 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200191 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500192 . git-rebase--$type
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530193 ret=$?
194 if test $ret -eq 0
195 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530196 finish_rebase
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400197 elif test $ret -eq 2 # special exit status for rebase -i
198 then
199 apply_autostash &&
200 rm -rf "$state_dir" &&
201 die "Nothing to do"
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530202 fi
203 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100204}
205
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900206run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500207 if test -z "$ok_to_skip_pre_rebase" &&
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700208 test -x "$(git rev-parse --git-path hooks/pre-rebase)"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900209 then
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700210 "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800211 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900212 fi
213}
214
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500215test -f "$apply_dir"/applying &&
Jiang Xinc7108bf2012-07-25 22:53:08 +0800216 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200217
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500218if test -d "$apply_dir"
219then
220 type=am
221 state_dir="$apply_dir"
222elif test -d "$merge_dir"
223then
224 if test -f "$merge_dir"/interactive
225 then
226 type=interactive
227 interactive_rebase=explicit
228 else
229 type=merge
230 fi
231 state_dir="$merge_dir"
232fi
233test -n "$type" && in_progress=t
234
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500235total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200236while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800237do
238 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900239 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500240 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900241 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100242 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500243 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100244 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400245 --continue|--skip|--abort|--edit-todo)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500246 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500247 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400248 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000249 --onto=*)
250 onto="${1#--onto=}"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800251 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000252 --exec=*)
253 cmd="${cmd}exec ${1#--exec=}${LF}"
Stefan Beller78ec2402016-03-18 14:26:17 -0700254 test -z "$interactive_rebase" && interactive_rebase=implied
Lucien Kongc2145382012-06-12 10:05:12 +0200255 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000256 --interactive)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500257 interactive_rebase=explicit
258 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000259 --keep-empty)
Neil Horman90e18182012-04-20 10:36:17 -0400260 keep_empty=yes
261 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000262 --preserve-merges)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500263 preserve_merges=t
264 test -z "$interactive_rebase" && interactive_rebase=implied
265 ;;
266 --autosquash)
267 autosquash=t
268 ;;
269 --no-autosquash)
270 autosquash=
271 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000272 --fork-point)
273 fork_point=t
274 ;;
275 --no-fork-point)
276 fork_point=
277 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000278 --merge)
Eric Wong58634db2006-06-21 03:04:41 -0700279 do_merge=t
280 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000281 --strategy-option=*)
282 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200283 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500284 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200285 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000286 --strategy=*)
287 strategy="${1#--strategy=}"
Eric Wong58634db2006-06-21 03:04:41 -0700288 do_merge=t
289 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000290 --no-stat)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100291 diffstat=
292 ;;
293 --stat)
294 diffstat=t
295 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530296 --autostash)
297 autostash=true
298 ;;
John Keeping619e3602015-09-10 23:30:51 +0100299 --no-autostash)
300 autostash=false
301 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000302 --verbose)
Robert Shearmanb7587892006-10-03 17:29:31 +0100303 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100304 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700305 GIT_QUIET=
306 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000307 --quiet)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700308 GIT_QUIET=t
309 git_am_opt="$git_am_opt -q"
310 verbose=
311 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100312 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000313 --whitespace=*)
314 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
315 case "${1#--whitespace=}" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500316 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100317 force_rebase=t
318 ;;
319 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400320 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200321 --ignore-whitespace)
322 git_am_opt="$git_am_opt $1"
323 ;;
Giuseppe Bilotta9f795242017-04-18 11:29:05 +0200324 --committer-date-is-author-date|--ignore-date|--signoff|--no-signoff)
Michele Ballabio570ccad2009-03-18 21:53:49 +0100325 git_am_opt="$git_am_opt $1"
326 force_rebase=t
327 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000328 -C*)
329 git_am_opt="$git_am_opt $1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200330 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100331 --root)
332 rebase_root=t
333 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000334 --force-rebase|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100335 force_rebase=t
336 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800337 --rerere-autoupdate|--no-rerere-autoupdate)
338 allow_rerere_autoupdate="$1"
339 ;;
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000340 --gpg-sign)
341 gpg_sign_opt=-S
342 ;;
343 --gpg-sign=*)
344 gpg_sign_opt="-S${1#--gpg-sign=}"
345 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500346 --)
347 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800348 break
349 ;;
350 esac
351 shift
352done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500353test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800354
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500355if test -n "$action"
356then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800357 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500358 # Only interactive rebase uses detailed reflog messages
359 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
360 then
361 GIT_REFLOG_ACTION="rebase -i ($action)"
362 export GIT_REFLOG_ACTION
363 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500364fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500365
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400366if test "$action" = "edit-todo" && test "$type" != "interactive"
367then
368 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
369fi
370
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500371case "$action" in
372continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500373 # Sanity check
374 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800375 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500376 git update-index --ignore-submodules --refresh &&
377 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800378 echo "$(gettext "You must edit all merge conflicts and then
379mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500380 exit 1
381 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500382 read_basic_state
383 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500384 ;;
385skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500386 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500387 read_basic_state
388 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500389 ;;
390abort)
391 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500392 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500393 case "$head_name" in
394 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400395 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800396 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500397 ;;
398 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500399 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530400 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500401 exit
402 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400403edit-todo)
404 run_specific_rebase
405 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500406esac
407
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500408# Make sure no rebase is in progress
409if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500410then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800411 state_dir_base=${state_dir##*/}
412 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
413 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
414 die "
415$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200416I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500417case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800418 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500419If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800420 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200421and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800422valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800423fi
424
Chris Webbdf5df202012-06-26 22:55:23 +0100425if test -n "$rebase_root" && test -z "$onto"
426then
427 test -z "$interactive_rebase" && interactive_rebase=implied
428fi
429
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500430if test -n "$interactive_rebase"
431then
432 type=interactive
433 state_dir="$merge_dir"
434elif test -n "$do_merge"
435then
436 type=merge
437 state_dir="$merge_dir"
438else
439 type=am
440 state_dir="$apply_dir"
441fi
442
Thomas Rast190f5322009-01-05 18:35:16 +0100443if test -z "$rebase_root"
444then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500445 case "$#" in
446 0)
447 if ! upstream_name=$(git rev-parse --symbolic-full-name \
448 --verify -q @{upstream} 2>/dev/null)
449 then
450 . git-parse-remote
451 error_on_missing_default_upstream "rebase" "rebase" \
Vasco Almeidac36d8ee2016-06-17 20:20:59 +0000452 "against" "git rebase $(gettext '<branch>')"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500453 fi
John Keepingad8261d2013-12-09 23:16:16 +0000454
455 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500456 ;;
457 *) upstream_name="$1"
Brian Gesiak4f407402014-03-19 20:02:15 +0900458 if test "$upstream_name" = "-"
459 then
460 upstream_name="@{-1}"
461 fi
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500462 shift
463 ;;
464 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530465 upstream=$(peel_committish "${upstream_name}") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800466 die "$(eval_gettext "invalid upstream \$upstream_name")"
Thomas Rast190f5322009-01-05 18:35:16 +0100467 upstream_arg="$upstream_name"
468else
Chris Webbdf5df202012-06-26 22:55:23 +0100469 if test -z "$onto"
470 then
Elia Pinto728fc792014-04-23 06:44:00 -0700471 empty_tree=$(git hash-object -t tree /dev/null)
472 onto=$(git commit-tree $empty_tree </dev/null)
Chris Webbdf5df202012-06-26 22:55:23 +0100473 squash_onto="$onto"
474 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100475 unset upstream_name
476 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700477 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500478 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100479fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100480
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700481# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500482onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900483case "$onto_name" in
484*...*)
485 if left=${onto_name%...*} right=${onto_name#*...} &&
486 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
487 then
488 case "$onto" in
489 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800490 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900491 ;;
492 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800493 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900494 ;;
495 esac
496 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800497 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900498 fi
499 ;;
500*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530501 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800502 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900503 ;;
504esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700505
Junio C Hamano0cb06642008-03-15 13:17:42 -0700506# If the branch to rebase is given, that is the branch we will rebase
507# $branch_name -- branch being rebased, or HEAD (already detached)
508# $orig_head -- commit object name of tip of the branch before rebasing
509# $head_name -- refs/heads/<that-branch> or "detached HEAD"
510switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800511case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01005121)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700513 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100514 branch_name="$1"
515 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700516
Thomas Rast190f5322009-01-05 18:35:16 +0100517 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500518 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800519 then
Thomas Rast190f5322009-01-05 18:35:16 +0100520 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500521 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700522 then
523 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800524 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800525 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800526 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800527 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07005280)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700529 # Do not need to switch branches, we are already on it.
Elia Pinto728fc792014-04-23 06:44:00 -0700530 if branch_name=$(git symbolic-ref -q HEAD)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700531 then
532 head_name=$branch_name
Elia Pinto728fc792014-04-23 06:44:00 -0700533 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
Junio C Hamano0cb06642008-03-15 13:17:42 -0700534 else
535 head_name="detached HEAD"
536 branch_name=HEAD ;# detached
537 fi
Phil Hordea709802013-04-23 18:51:14 -0400538 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700539 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700540*)
541 die "BUG: unexpected number of arguments left to parse"
542 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800543esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700544
John Keepingad8261d2013-12-09 23:16:16 +0000545if test "$fork_point" = t
546then
John Keepingbb3f4582014-01-09 19:47:34 +0000547 new_upstream=$(git merge-base --fork-point "$upstream_name" \
548 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000549 if test -n "$new_upstream"
550 then
John Keeping1e0dacd2014-07-16 20:23:49 +0100551 restrict_revision=$new_upstream
John Keepingad8261d2013-12-09 23:16:16 +0000552 fi
553fi
554
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530555if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
556then
557 stash_sha1=$(git stash create "autostash") ||
558 die "$(gettext 'Cannot autostash')"
559
560 mkdir -p "$state_dir" &&
561 echo $stash_sha1 >"$state_dir/autostash" &&
562 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
563 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
564 git reset --hard
565fi
566
Jiang Xinc7108bf2012-07-25 22:53:08 +0800567require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500568
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500569# Now we are rebasing commits $upstream..$orig_head (or with --root,
570# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800571
Johannes Sixt1308c172007-07-04 22:09:10 +0200572# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500573# but this should be done only when upstream and onto are the same
574# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500575mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500576if test "$type" != interactive && test "$upstream" = "$onto" &&
John Keeping1e0dacd2014-07-16 20:23:49 +0100577 test "$mb" = "$onto" && test -z "$restrict_revision" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200578 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500579 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800580then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100581 if test -z "$force_rebase"
582 then
583 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530584 test -z "$switch_to" ||
585 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
Jeff King22946a92015-04-28 01:17:37 -0400586 git checkout -q "$switch_to" --
Jiang Xinc7108bf2012-07-25 22:53:08 +0800587 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530588 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100589 exit 0
590 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800591 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100592 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800593fi
594
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500595# If a hook exists, give it a chance to interrupt
596run_pre_rebase_hook "$upstream_arg" "$@"
597
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100598if test -n "$diffstat"
599then
600 if test -n "$verbose"
601 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800602 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100603 fi
604 # We want color (if set), but no pager
605 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
606fi
607
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500608test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500609
610# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800611say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530612
613GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
614 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500615git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500616
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800617# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300618# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500619if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100620then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800621 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000622 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530623 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100624 exit 0
625fi
626
Thomas Rast190f5322009-01-05 18:35:16 +0100627if test -n "$rebase_root"
628then
629 revisions="$onto..$orig_head"
630else
John Keeping1e0dacd2014-07-16 20:23:49 +0100631 revisions="${restrict_revision-$upstream}..$orig_head"
Thomas Rast190f5322009-01-05 18:35:16 +0100632fi
633
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500634run_specific_rebase