blob: f8b3d1fd9752a37c5424e06ba36da4648d0aba1b [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
Nguyễn Thái Ngọc Duy95121772016-11-12 09:00:41 +070047quit! abort but keep HEAD where it is
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050048"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080049. git-sh-setup
Shawn O. Pearcef9474132006-12-28 02:34:48 -050050set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040051require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080052cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070053
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080054LF='
55'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050056ok_to_skip_pre_rebase=
57resolvemsg="
William Duclot5fdacc12017-07-16 21:42:20 +020058$(gettext 'Resolve all conflicts manually, mark them as resolved with
59"git add/rm <conflicted_files>", then run "git rebase --continue".
60You can instead skip this commit: run "git rebase --skip".
61To abort and get back to the state before "git rebase", run "git rebase --abort".')
Seancc120052006-05-13 23:34:08 -040062"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050063unset onto
John Keeping1e0dacd2014-07-16 20:23:49 +010064unset restrict_revision
Lucien Kongc2145382012-06-12 10:05:12 +020065cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050066strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020067strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070068do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050069merge_dir="$GIT_DIR"/rebase-merge
70apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010071verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010072diffstat=
73test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053074autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000075fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020076git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010077rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010078force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080079allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050080# Non-empty if a rebase was in progress when 'git rebase' was invoked
81in_progress=
82# One of {am, merge, interactive}
83type=
84# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
85state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050086# One of {'', continue, skip, abort}, as parsed from command line
87action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050088preserve_merges=
89autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040090keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050091test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Junio C Hamano66948562016-05-02 14:58:45 -070092case "$(git config --bool commit.gpgsign)" in
93true) gpg_sign_opt=-S ;;
94*) gpg_sign_opt= ;;
95esac
Eric Wong58634db2006-06-21 03:04:41 -070096
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -050097read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +053098 test -f "$state_dir/head-name" &&
99 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500100 head_name=$(cat "$state_dir"/head-name) &&
101 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500102 # We always write to orig-head, but interactive rebase used to write to
103 # head. Fall back to reading from head to cover for the case that the
104 # user upgraded git with an ongoing interactive rebase.
105 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500106 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500107 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500108 else
109 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500110 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500111 GIT_QUIET=$(cat "$state_dir"/quiet) &&
112 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500113 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
114 test -f "$state_dir"/strategy_opts &&
115 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500116 test -f "$state_dir"/allow_rerere_autoupdate &&
117 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000118 test -f "$state_dir"/gpg_sign_opt &&
119 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500120}
121
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500122write_basic_state () {
123 echo "$head_name" > "$state_dir"/head-name &&
124 echo "$onto" > "$state_dir"/onto &&
125 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500126 echo "$GIT_QUIET" > "$state_dir"/quiet &&
127 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500128 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
129 test -n "$strategy_opts" && echo "$strategy_opts" > \
130 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500131 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
132 "$state_dir"/allow_rerere_autoupdate
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000133 test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500134}
135
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500136output () {
137 case "$verbose" in
138 '')
139 output=$("$@" 2>&1 )
140 status=$?
141 test $status != 0 && printf "%s\n" "$output"
142 return $status
143 ;;
144 *)
145 "$@"
146 ;;
147 esac
148}
149
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000150move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000151 case "$head_name" in
152 refs/*)
153 message="rebase finished: $head_name onto $onto"
154 git update-ref -m "$message" \
155 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400156 git symbolic-ref \
157 -m "rebase finished: returning to $head_name" \
158 HEAD $head_name ||
Vasco Almeida24a6df42016-06-17 20:20:58 +0000159 die "$(eval_gettext "Could not move back to \$head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000160 ;;
161 esac
162}
163
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400164apply_autostash () {
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530165 if test -f "$state_dir/autostash"
166 then
167 stash_sha1=$(cat "$state_dir/autostash")
168 if git stash apply $stash_sha1 2>&1 >/dev/null
169 then
Johannes Schindelincdb866b2017-06-19 18:56:02 +0100170 echo "$(gettext 'Applied autostash.')" >&2
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530171 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530172 git stash store -m "autostash" -q $stash_sha1 ||
173 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530174 gettext 'Applying autostash resulted in conflicts.
175Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200176You can run "git stash pop" or "git stash drop" at any time.
Johannes Schindelincdb866b2017-06-19 18:56:02 +0100177' >&2
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530178 fi
179 fi
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400180}
181
182finish_rebase () {
183 apply_autostash &&
Jeff King8c24f5b2016-01-13 13:47:18 -0500184 { git gc --auto || true; } &&
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530185 rm -rf "$state_dir"
186}
187
Kyle J. McKay8cd65962014-04-11 01:28:18 -0700188run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200189 if [ "$interactive_rebase" = implied ]; then
190 GIT_EDITOR=:
191 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000192 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200193 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500194 . git-rebase--$type
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530195 ret=$?
196 if test $ret -eq 0
197 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530198 finish_rebase
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400199 elif test $ret -eq 2 # special exit status for rebase -i
200 then
201 apply_autostash &&
202 rm -rf "$state_dir" &&
203 die "Nothing to do"
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530204 fi
205 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100206}
207
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900208run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500209 if test -z "$ok_to_skip_pre_rebase" &&
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700210 test -x "$(git rev-parse --git-path hooks/pre-rebase)"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900211 then
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700212 "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800213 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900214 fi
215}
216
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500217test -f "$apply_dir"/applying &&
Jiang Xinc7108bf2012-07-25 22:53:08 +0800218 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200219
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500220if test -d "$apply_dir"
221then
222 type=am
223 state_dir="$apply_dir"
224elif test -d "$merge_dir"
225then
226 if test -f "$merge_dir"/interactive
227 then
228 type=interactive
229 interactive_rebase=explicit
230 else
231 type=merge
232 fi
233 state_dir="$merge_dir"
234fi
235test -n "$type" && in_progress=t
236
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500237total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200238while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800239do
240 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900241 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500242 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900243 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100244 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500245 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100246 ;;
Nguyễn Thái Ngọc Duy95121772016-11-12 09:00:41 +0700247 --continue|--skip|--abort|--quit|--edit-todo)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500248 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500249 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400250 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000251 --onto=*)
252 onto="${1#--onto=}"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800253 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000254 --exec=*)
255 cmd="${cmd}exec ${1#--exec=}${LF}"
Stefan Beller78ec2402016-03-18 14:26:17 -0700256 test -z "$interactive_rebase" && interactive_rebase=implied
Lucien Kongc2145382012-06-12 10:05:12 +0200257 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000258 --interactive)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500259 interactive_rebase=explicit
260 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000261 --keep-empty)
Neil Horman90e18182012-04-20 10:36:17 -0400262 keep_empty=yes
263 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000264 --preserve-merges)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500265 preserve_merges=t
266 test -z "$interactive_rebase" && interactive_rebase=implied
267 ;;
268 --autosquash)
269 autosquash=t
270 ;;
271 --no-autosquash)
272 autosquash=
273 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000274 --fork-point)
275 fork_point=t
276 ;;
277 --no-fork-point)
278 fork_point=
279 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000280 --merge)
Eric Wong58634db2006-06-21 03:04:41 -0700281 do_merge=t
282 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000283 --strategy-option=*)
284 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200285 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500286 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200287 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000288 --strategy=*)
289 strategy="${1#--strategy=}"
Eric Wong58634db2006-06-21 03:04:41 -0700290 do_merge=t
291 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000292 --no-stat)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100293 diffstat=
294 ;;
295 --stat)
296 diffstat=t
297 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530298 --autostash)
299 autostash=true
300 ;;
John Keeping619e3602015-09-10 23:30:51 +0100301 --no-autostash)
302 autostash=false
303 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000304 --verbose)
Robert Shearmanb7587892006-10-03 17:29:31 +0100305 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100306 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700307 GIT_QUIET=
308 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000309 --quiet)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700310 GIT_QUIET=t
311 git_am_opt="$git_am_opt -q"
312 verbose=
313 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100314 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000315 --whitespace=*)
316 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
317 case "${1#--whitespace=}" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500318 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100319 force_rebase=t
320 ;;
321 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400322 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200323 --ignore-whitespace)
324 git_am_opt="$git_am_opt $1"
325 ;;
Giuseppe Bilotta9f795242017-04-18 11:29:05 +0200326 --committer-date-is-author-date|--ignore-date|--signoff|--no-signoff)
Michele Ballabio570ccad2009-03-18 21:53:49 +0100327 git_am_opt="$git_am_opt $1"
328 force_rebase=t
329 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000330 -C*)
331 git_am_opt="$git_am_opt $1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200332 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100333 --root)
334 rebase_root=t
335 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000336 --force-rebase|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100337 force_rebase=t
338 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800339 --rerere-autoupdate|--no-rerere-autoupdate)
340 allow_rerere_autoupdate="$1"
341 ;;
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000342 --gpg-sign)
343 gpg_sign_opt=-S
344 ;;
345 --gpg-sign=*)
346 gpg_sign_opt="-S${1#--gpg-sign=}"
347 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500348 --)
349 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800350 break
351 ;;
352 esac
353 shift
354done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500355test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800356
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500357if test -n "$action"
358then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800359 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500360 # Only interactive rebase uses detailed reflog messages
361 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
362 then
363 GIT_REFLOG_ACTION="rebase -i ($action)"
364 export GIT_REFLOG_ACTION
365 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500366fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500367
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400368if test "$action" = "edit-todo" && test "$type" != "interactive"
369then
370 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
371fi
372
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500373case "$action" in
374continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500375 # Sanity check
376 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800377 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500378 git update-index --ignore-submodules --refresh &&
379 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800380 echo "$(gettext "You must edit all merge conflicts and then
381mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500382 exit 1
383 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500384 read_basic_state
385 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500386 ;;
387skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500388 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500389 read_basic_state
390 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500391 ;;
392abort)
393 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500394 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500395 case "$head_name" in
396 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400397 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800398 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500399 ;;
400 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500401 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530402 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500403 exit
404 ;;
Nguyễn Thái Ngọc Duy95121772016-11-12 09:00:41 +0700405quit)
406 exec rm -rf "$state_dir"
407 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400408edit-todo)
409 run_specific_rebase
410 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500411esac
412
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500413# Make sure no rebase is in progress
414if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500415then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800416 state_dir_base=${state_dir##*/}
417 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
418 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
419 die "
420$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200421I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500422case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800423 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500424If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800425 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200426and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800427valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800428fi
429
Chris Webbdf5df202012-06-26 22:55:23 +0100430if test -n "$rebase_root" && test -z "$onto"
431then
432 test -z "$interactive_rebase" && interactive_rebase=implied
433fi
434
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500435if test -n "$interactive_rebase"
436then
437 type=interactive
438 state_dir="$merge_dir"
439elif test -n "$do_merge"
440then
441 type=merge
442 state_dir="$merge_dir"
443else
444 type=am
445 state_dir="$apply_dir"
446fi
447
Thomas Rast190f5322009-01-05 18:35:16 +0100448if test -z "$rebase_root"
449then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500450 case "$#" in
451 0)
452 if ! upstream_name=$(git rev-parse --symbolic-full-name \
453 --verify -q @{upstream} 2>/dev/null)
454 then
455 . git-parse-remote
456 error_on_missing_default_upstream "rebase" "rebase" \
Vasco Almeidac36d8ee2016-06-17 20:20:59 +0000457 "against" "git rebase $(gettext '<branch>')"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500458 fi
John Keepingad8261d2013-12-09 23:16:16 +0000459
460 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500461 ;;
462 *) upstream_name="$1"
Brian Gesiak4f407402014-03-19 20:02:15 +0900463 if test "$upstream_name" = "-"
464 then
465 upstream_name="@{-1}"
466 fi
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500467 shift
468 ;;
469 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530470 upstream=$(peel_committish "${upstream_name}") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800471 die "$(eval_gettext "invalid upstream \$upstream_name")"
Thomas Rast190f5322009-01-05 18:35:16 +0100472 upstream_arg="$upstream_name"
473else
Chris Webbdf5df202012-06-26 22:55:23 +0100474 if test -z "$onto"
475 then
Elia Pinto728fc792014-04-23 06:44:00 -0700476 empty_tree=$(git hash-object -t tree /dev/null)
477 onto=$(git commit-tree $empty_tree </dev/null)
Chris Webbdf5df202012-06-26 22:55:23 +0100478 squash_onto="$onto"
479 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100480 unset upstream_name
481 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700482 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500483 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100484fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100485
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700486# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500487onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900488case "$onto_name" in
489*...*)
490 if left=${onto_name%...*} right=${onto_name#*...} &&
491 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
492 then
493 case "$onto" in
494 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800495 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900496 ;;
497 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800498 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900499 ;;
500 esac
501 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800502 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900503 fi
504 ;;
505*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530506 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800507 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900508 ;;
509esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700510
Junio C Hamano0cb06642008-03-15 13:17:42 -0700511# If the branch to rebase is given, that is the branch we will rebase
512# $branch_name -- branch being rebased, or HEAD (already detached)
513# $orig_head -- commit object name of tip of the branch before rebasing
514# $head_name -- refs/heads/<that-branch> or "detached HEAD"
515switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800516case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01005171)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700518 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100519 branch_name="$1"
520 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700521
Thomas Rast190f5322009-01-05 18:35:16 +0100522 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500523 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800524 then
Thomas Rast190f5322009-01-05 18:35:16 +0100525 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500526 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700527 then
528 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800529 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800530 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800531 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800532 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07005330)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700534 # Do not need to switch branches, we are already on it.
Elia Pinto728fc792014-04-23 06:44:00 -0700535 if branch_name=$(git symbolic-ref -q HEAD)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700536 then
537 head_name=$branch_name
Elia Pinto728fc792014-04-23 06:44:00 -0700538 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
Junio C Hamano0cb06642008-03-15 13:17:42 -0700539 else
540 head_name="detached HEAD"
541 branch_name=HEAD ;# detached
542 fi
Phil Hordea709802013-04-23 18:51:14 -0400543 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700544 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700545*)
546 die "BUG: unexpected number of arguments left to parse"
547 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800548esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700549
John Keepingad8261d2013-12-09 23:16:16 +0000550if test "$fork_point" = t
551then
John Keepingbb3f4582014-01-09 19:47:34 +0000552 new_upstream=$(git merge-base --fork-point "$upstream_name" \
553 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000554 if test -n "$new_upstream"
555 then
John Keeping1e0dacd2014-07-16 20:23:49 +0100556 restrict_revision=$new_upstream
John Keepingad8261d2013-12-09 23:16:16 +0000557 fi
558fi
559
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530560if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
561then
562 stash_sha1=$(git stash create "autostash") ||
563 die "$(gettext 'Cannot autostash')"
564
565 mkdir -p "$state_dir" &&
566 echo $stash_sha1 >"$state_dir/autostash" &&
567 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
568 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
569 git reset --hard
570fi
571
Jiang Xinc7108bf2012-07-25 22:53:08 +0800572require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500573
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500574# Now we are rebasing commits $upstream..$orig_head (or with --root,
575# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800576
Johannes Sixt1308c172007-07-04 22:09:10 +0200577# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500578# but this should be done only when upstream and onto are the same
579# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500580mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500581if test "$type" != interactive && test "$upstream" = "$onto" &&
John Keeping1e0dacd2014-07-16 20:23:49 +0100582 test "$mb" = "$onto" && test -z "$restrict_revision" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200583 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500584 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800585then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100586 if test -z "$force_rebase"
587 then
588 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530589 test -z "$switch_to" ||
590 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
Jeff King22946a92015-04-28 01:17:37 -0400591 git checkout -q "$switch_to" --
Jiang Xinc7108bf2012-07-25 22:53:08 +0800592 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530593 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100594 exit 0
595 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800596 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100597 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800598fi
599
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500600# If a hook exists, give it a chance to interrupt
601run_pre_rebase_hook "$upstream_arg" "$@"
602
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100603if test -n "$diffstat"
604then
605 if test -n "$verbose"
606 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800607 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100608 fi
609 # We want color (if set), but no pager
610 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
611fi
612
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500613test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500614
615# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800616say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530617
618GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
619 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500620git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500621
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800622# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300623# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500624if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100625then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800626 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000627 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530628 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100629 exit 0
630fi
631
Thomas Rast190f5322009-01-05 18:35:16 +0100632if test -n "$rebase_root"
633then
634 revisions="$onto..$orig_head"
635else
John Keeping1e0dacd2014-07-16 20:23:49 +0100636 revisions="${restrict_revision-$upstream}..$orig_head"
Thomas Rast190f5322009-01-05 18:35:16 +0100637fi
638
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500639run_specific_rebase