blob: 40be59ecc4704da05a0e153f1709d4af2c62cd18 [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>]
Kaartic Sivaraam82cb7752017-11-20 20:56:52 +053012git 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
Johannes Schindelin7543f6f2018-04-25 14:29:40 +020020r,rebase-merges? try to rebase merges instead of skipping them
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050021p,preserve-merges! try to recreate merges instead of ignoring them
22s,strategy=! use the given merge strategy
23no-ff! cherry-pick all commits, even if unchanged
24m,merge! use merging strategies to rebase
25i,interactive! let the user edit the list of commits to rebase
Lucien Kongc2145382012-06-12 10:05:12 +020026x,exec=! add exec lines after each commit of the editable list
Neil Horman90e18182012-04-20 10:36:17 -040027k,keep-empty preserve empty commits during rebase
Genki Skya6c612b2018-02-04 15:08:13 -050028allow-empty-message allow rebasing commits with empty messages
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050029f,force-rebase! force rebase even if branch is up to date
30X,strategy-option=! pass the argument through to the merge strategy
31stat! display a diffstat of what changed upstream
32n,no-stat! do not show diffstat of what changed upstream
33verify allow pre-rebase hook to run
34rerere-autoupdate allow rerere to update index with resolved conflicts
35root! rebase all reachable commits up to the root(s)
36autosquash move commits that begin with squash!/fixup! under -i
37committer-date-is-author-date! passed to 'git am'
38ignore-date! passed to 'git am'
Giuseppe Bilotta9f795242017-04-18 11:29:05 +020039signoff passed to 'git am'
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050040whitespace=! passed to 'git apply'
41ignore-whitespace! passed to 'git apply'
42C=! passed to 'git apply'
Nicolas Vigier3ee5e542014-02-10 01:03:37 +000043S,gpg-sign? GPG-sign commits
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050044 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040045continue! continue
46abort! abort and check out the original branch
47skip! skip current patch and continue
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040048edit-todo! edit the todo list during an interactive rebase
Nguyễn Thái Ngọc Duy95121772016-11-12 09:00:41 +070049quit! abort but keep HEAD where it is
Nguyễn Thái Ngọc Duy66335292018-02-11 16:43:27 +070050show-current-patch! show the patch file being applied or merged
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050051"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080052. git-sh-setup
Shawn O. Pearcef9474132006-12-28 02:34:48 -050053set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040054require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080055cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070056
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080057LF='
58'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050059ok_to_skip_pre_rebase=
60resolvemsg="
William Duclot5fdacc12017-07-16 21:42:20 +020061$(gettext 'Resolve all conflicts manually, mark them as resolved with
62"git add/rm <conflicted_files>", then run "git rebase --continue".
63You can instead skip this commit: run "git rebase --skip".
64To abort and get back to the state before "git rebase", run "git rebase --abort".')
Seancc120052006-05-13 23:34:08 -040065"
Phillip Woodbb2ac4f2018-03-20 10:03:13 +000066squash_onto=
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050067unset onto
John Keeping1e0dacd2014-07-16 20:23:49 +010068unset restrict_revision
Lucien Kongc2145382012-06-12 10:05:12 +020069cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050070strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020071strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070072do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050073merge_dir="$GIT_DIR"/rebase-merge
74apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010075verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010076diffstat=
77test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053078autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000079fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020080git_am_opt=
Kevin Willford9eaa8582017-08-10 14:32:56 -040081git_format_patch_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010082rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010083force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080084allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050085# Non-empty if a rebase was in progress when 'git rebase' was invoked
86in_progress=
87# One of {am, merge, interactive}
88type=
89# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
90state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050091# One of {'', continue, skip, abort}, as parsed from command line
92action=
Johannes Schindelin8f6aed72018-04-25 14:29:04 +020093rebase_merges=
Johannes Schindelin7543f6f2018-04-25 14:29:40 +020094rebase_cousins=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050095preserve_merges=
96autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040097keep_empty=
Genki Skya6c612b2018-02-04 15:08:13 -050098allow_empty_message=
Phillip Wooda852ec72018-03-20 11:10:55 +000099signoff=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500100test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Junio C Hamano66948562016-05-02 14:58:45 -0700101case "$(git config --bool commit.gpgsign)" in
102true) gpg_sign_opt=-S ;;
103*) gpg_sign_opt= ;;
104esac
Eric Wong58634db2006-06-21 03:04:41 -0700105
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500106read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +0530107 test -f "$state_dir/head-name" &&
108 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500109 head_name=$(cat "$state_dir"/head-name) &&
110 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500111 # We always write to orig-head, but interactive rebase used to write to
112 # head. Fall back to reading from head to cover for the case that the
113 # user upgraded git with an ongoing interactive rebase.
114 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500115 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500116 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500117 else
118 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500119 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500120 GIT_QUIET=$(cat "$state_dir"/quiet) &&
121 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500122 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
123 test -f "$state_dir"/strategy_opts &&
124 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500125 test -f "$state_dir"/allow_rerere_autoupdate &&
126 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000127 test -f "$state_dir"/gpg_sign_opt &&
128 gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)"
Phillip Wooda852ec72018-03-20 11:10:55 +0000129 test -f "$state_dir"/signoff && {
130 signoff="$(cat "$state_dir"/signoff)"
131 force_rebase=t
132 }
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500133}
134
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500135write_basic_state () {
136 echo "$head_name" > "$state_dir"/head-name &&
137 echo "$onto" > "$state_dir"/onto &&
138 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500139 echo "$GIT_QUIET" > "$state_dir"/quiet &&
140 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500141 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
142 test -n "$strategy_opts" && echo "$strategy_opts" > \
143 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500144 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
145 "$state_dir"/allow_rerere_autoupdate
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000146 test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt
Phillip Wooda852ec72018-03-20 11:10:55 +0000147 test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500148}
149
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500150output () {
151 case "$verbose" in
152 '')
153 output=$("$@" 2>&1 )
154 status=$?
155 test $status != 0 && printf "%s\n" "$output"
156 return $status
157 ;;
158 *)
159 "$@"
160 ;;
161 esac
162}
163
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000164move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000165 case "$head_name" in
166 refs/*)
167 message="rebase finished: $head_name onto $onto"
168 git update-ref -m "$message" \
169 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400170 git symbolic-ref \
171 -m "rebase finished: returning to $head_name" \
172 HEAD $head_name ||
Vasco Almeida24a6df42016-06-17 20:20:58 +0000173 die "$(eval_gettext "Could not move back to \$head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000174 ;;
175 esac
176}
177
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400178apply_autostash () {
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530179 if test -f "$state_dir/autostash"
180 then
181 stash_sha1=$(cat "$state_dir/autostash")
Todd Zullingereadf1c82017-11-13 16:20:09 -0500182 if git stash apply $stash_sha1 >/dev/null 2>&1
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530183 then
Johannes Schindelincdb866b2017-06-19 18:56:02 +0100184 echo "$(gettext 'Applied autostash.')" >&2
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530185 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530186 git stash store -m "autostash" -q $stash_sha1 ||
187 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530188 gettext 'Applying autostash resulted in conflicts.
189Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200190You can run "git stash pop" or "git stash drop" at any time.
Johannes Schindelincdb866b2017-06-19 18:56:02 +0100191' >&2
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530192 fi
193 fi
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400194}
195
196finish_rebase () {
Nguyễn Thái Ngọc Duyfbd7a232018-02-11 16:43:28 +0700197 rm -f "$(git rev-parse --git-path REBASE_HEAD)"
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400198 apply_autostash &&
Jeff King8c24f5b2016-01-13 13:47:18 -0500199 { git gc --auto || true; } &&
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530200 rm -rf "$state_dir"
201}
202
Kyle J. McKay8cd65962014-04-11 01:28:18 -0700203run_specific_rebase () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200204 if [ "$interactive_rebase" = implied ]; then
205 GIT_EDITOR=:
206 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000207 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200208 fi
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500209 . git-rebase--$type
Wink Saville950b4872018-03-23 14:25:26 -0700210 git_rebase__$type${preserve_merges:+__preserve_merges}
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530211 ret=$?
212 if test $ret -eq 0
213 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530214 finish_rebase
Ramkumar Ramachandrae4244eb2014-05-19 18:05:20 -0400215 elif test $ret -eq 2 # special exit status for rebase -i
216 then
217 apply_autostash &&
218 rm -rf "$state_dir" &&
219 die "Nothing to do"
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530220 fi
221 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100222}
223
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900224run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500225 if test -z "$ok_to_skip_pre_rebase" &&
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700226 test -x "$(git rev-parse --git-path hooks/pre-rebase)"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900227 then
Nguyễn Thái Ngọc Duyb849b952014-11-30 15:24:38 +0700228 "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800229 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900230 fi
231}
232
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500233test -f "$apply_dir"/applying &&
Kaartic Sivaraam82cb7752017-11-20 20:56:52 +0530234 die "$(gettext "It looks like 'git am' is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200235
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500236if test -d "$apply_dir"
237then
238 type=am
239 state_dir="$apply_dir"
240elif test -d "$merge_dir"
241then
242 if test -f "$merge_dir"/interactive
243 then
244 type=interactive
245 interactive_rebase=explicit
246 else
247 type=merge
248 fi
249 state_dir="$merge_dir"
250fi
251test -n "$type" && in_progress=t
252
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500253total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200254while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800255do
256 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900257 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500258 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900259 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100260 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500261 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100262 ;;
Nguyễn Thái Ngọc Duy66335292018-02-11 16:43:27 +0700263 --continue|--skip|--abort|--quit|--edit-todo|--show-current-patch)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500264 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500265 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400266 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000267 --onto=*)
268 onto="${1#--onto=}"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800269 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000270 --exec=*)
271 cmd="${cmd}exec ${1#--exec=}${LF}"
Stefan Beller78ec2402016-03-18 14:26:17 -0700272 test -z "$interactive_rebase" && interactive_rebase=implied
Lucien Kongc2145382012-06-12 10:05:12 +0200273 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000274 --interactive)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500275 interactive_rebase=explicit
276 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000277 --keep-empty)
Neil Horman90e18182012-04-20 10:36:17 -0400278 keep_empty=yes
279 ;;
Genki Skya6c612b2018-02-04 15:08:13 -0500280 --allow-empty-message)
281 allow_empty_message=--allow-empty-message
282 ;;
Phillip Wood3d946162018-03-20 10:03:15 +0000283 --no-keep-empty)
284 keep_empty=
285 ;;
Johannes Schindelin8f6aed72018-04-25 14:29:04 +0200286 --rebase-merges)
287 rebase_merges=t
288 test -z "$interactive_rebase" && interactive_rebase=implied
289 ;;
Johannes Schindelin7543f6f2018-04-25 14:29:40 +0200290 --rebase-merges=*)
291 rebase_merges=t
292 case "${1#*=}" in
293 rebase-cousins) rebase_cousins=t;;
294 no-rebase-cousins) rebase_cousins=;;
295 *) die "Unknown mode: $1";;
296 esac
297 test -z "$interactive_rebase" && interactive_rebase=implied
298 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000299 --preserve-merges)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500300 preserve_merges=t
301 test -z "$interactive_rebase" && interactive_rebase=implied
302 ;;
303 --autosquash)
304 autosquash=t
305 ;;
306 --no-autosquash)
307 autosquash=
308 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000309 --fork-point)
310 fork_point=t
311 ;;
312 --no-fork-point)
313 fork_point=
314 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000315 --merge)
Eric Wong58634db2006-06-21 03:04:41 -0700316 do_merge=t
317 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000318 --strategy-option=*)
319 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200320 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500321 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200322 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000323 --strategy=*)
324 strategy="${1#--strategy=}"
Eric Wong58634db2006-06-21 03:04:41 -0700325 do_merge=t
326 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000327 --no-stat)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100328 diffstat=
329 ;;
330 --stat)
331 diffstat=t
332 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530333 --autostash)
334 autostash=true
335 ;;
John Keeping619e3602015-09-10 23:30:51 +0100336 --no-autostash)
337 autostash=false
338 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000339 --verbose)
Robert Shearmanb7587892006-10-03 17:29:31 +0100340 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100341 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700342 GIT_QUIET=
343 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000344 --quiet)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700345 GIT_QUIET=t
346 git_am_opt="$git_am_opt -q"
347 verbose=
348 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100349 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000350 --whitespace=*)
351 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
352 case "${1#--whitespace=}" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500353 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100354 force_rebase=t
355 ;;
356 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400357 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200358 --ignore-whitespace)
359 git_am_opt="$git_am_opt $1"
360 ;;
Phillip Wooda852ec72018-03-20 11:10:55 +0000361 --signoff)
362 signoff=--signoff
363 ;;
364 --no-signoff)
365 signoff=
366 ;;
367 --committer-date-is-author-date|--ignore-date)
Michele Ballabio570ccad2009-03-18 21:53:49 +0100368 git_am_opt="$git_am_opt $1"
369 force_rebase=t
370 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000371 -C*)
372 git_am_opt="$git_am_opt $1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200373 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100374 --root)
375 rebase_root=t
376 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000377 --force-rebase|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100378 force_rebase=t
379 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800380 --rerere-autoupdate|--no-rerere-autoupdate)
381 allow_rerere_autoupdate="$1"
382 ;;
Nicolas Vigier3ee5e542014-02-10 01:03:37 +0000383 --gpg-sign)
384 gpg_sign_opt=-S
385 ;;
386 --gpg-sign=*)
387 gpg_sign_opt="-S${1#--gpg-sign=}"
388 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500389 --)
390 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800391 break
392 ;;
Brandon Casey697bc882017-09-17 15:28:17 -0700393 *)
394 usage
395 ;;
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800396 esac
397 shift
398done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500399test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800400
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500401if test -n "$action"
402then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800403 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500404 # Only interactive rebase uses detailed reflog messages
405 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
406 then
407 GIT_REFLOG_ACTION="rebase -i ($action)"
408 export GIT_REFLOG_ACTION
409 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500410fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500411
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400412if test "$action" = "edit-todo" && test "$type" != "interactive"
413then
414 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
415fi
416
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500417case "$action" in
418continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500419 # Sanity check
420 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800421 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500422 git update-index --ignore-submodules --refresh &&
423 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800424 echo "$(gettext "You must edit all merge conflicts and then
425mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500426 exit 1
427 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500428 read_basic_state
429 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500430 ;;
431skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500432 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500433 read_basic_state
434 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500435 ;;
436abort)
437 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500438 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500439 case "$head_name" in
440 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400441 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800442 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500443 ;;
444 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500445 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530446 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500447 exit
448 ;;
Nguyễn Thái Ngọc Duy95121772016-11-12 09:00:41 +0700449quit)
450 exec rm -rf "$state_dir"
451 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400452edit-todo)
453 run_specific_rebase
454 ;;
Nguyễn Thái Ngọc Duy66335292018-02-11 16:43:27 +0700455show-current-patch)
456 run_specific_rebase
457 die "BUG: run_specific_rebase is not supposed to return here"
458 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500459esac
460
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500461# Make sure no rebase is in progress
462if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500463then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800464 state_dir_base=${state_dir##*/}
465 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
466 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
467 die "
468$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200469I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500470case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800471 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500472If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800473 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200474and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800475valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800476fi
477
Chris Webbdf5df202012-06-26 22:55:23 +0100478if test -n "$rebase_root" && test -z "$onto"
479then
480 test -z "$interactive_rebase" && interactive_rebase=implied
481fi
482
Phillip Woodda27a6f2018-03-20 11:10:57 +0000483if test -n "$keep_empty"
484then
485 test -z "$interactive_rebase" && interactive_rebase=implied
486fi
487
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500488if test -n "$interactive_rebase"
489then
490 type=interactive
491 state_dir="$merge_dir"
492elif test -n "$do_merge"
493then
494 type=merge
495 state_dir="$merge_dir"
496else
497 type=am
498 state_dir="$apply_dir"
499fi
500
Kevin Willford9eaa8582017-08-10 14:32:56 -0400501if test -t 2 && test -z "$GIT_QUIET"
502then
503 git_format_patch_opt="$git_format_patch_opt --progress"
504fi
505
Phillip Wooda852ec72018-03-20 11:10:55 +0000506if test -n "$signoff"
507then
Phillip Woodb79966a2018-03-20 11:10:56 +0000508 test -n "$preserve_merges" &&
509 die "$(gettext "error: cannot combine '--signoff' with '--preserve-merges'")"
Phillip Wooda852ec72018-03-20 11:10:55 +0000510 git_am_opt="$git_am_opt $signoff"
511 force_rebase=t
512fi
513
Thomas Rast190f5322009-01-05 18:35:16 +0100514if test -z "$rebase_root"
515then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500516 case "$#" in
517 0)
518 if ! upstream_name=$(git rev-parse --symbolic-full-name \
519 --verify -q @{upstream} 2>/dev/null)
520 then
521 . git-parse-remote
522 error_on_missing_default_upstream "rebase" "rebase" \
Vasco Almeidac36d8ee2016-06-17 20:20:59 +0000523 "against" "git rebase $(gettext '<branch>')"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500524 fi
John Keepingad8261d2013-12-09 23:16:16 +0000525
526 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500527 ;;
528 *) upstream_name="$1"
Brian Gesiak4f407402014-03-19 20:02:15 +0900529 if test "$upstream_name" = "-"
530 then
531 upstream_name="@{-1}"
532 fi
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500533 shift
534 ;;
535 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530536 upstream=$(peel_committish "${upstream_name}") ||
Kaartic Sivaraamca7de7b2017-12-16 14:33:18 +0530537 die "$(eval_gettext "invalid upstream '\$upstream_name'")"
Thomas Rast190f5322009-01-05 18:35:16 +0100538 upstream_arg="$upstream_name"
539else
Chris Webbdf5df202012-06-26 22:55:23 +0100540 if test -z "$onto"
541 then
Elia Pinto728fc792014-04-23 06:44:00 -0700542 empty_tree=$(git hash-object -t tree /dev/null)
543 onto=$(git commit-tree $empty_tree </dev/null)
Chris Webbdf5df202012-06-26 22:55:23 +0100544 squash_onto="$onto"
545 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100546 unset upstream_name
547 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700548 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500549 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100550fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100551
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700552# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500553onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900554case "$onto_name" in
555*...*)
556 if left=${onto_name%...*} right=${onto_name#*...} &&
557 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
558 then
559 case "$onto" in
560 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800561 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900562 ;;
563 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800564 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900565 ;;
566 esac
567 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800568 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900569 fi
570 ;;
571*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530572 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800573 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900574 ;;
575esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700576
Junio C Hamano0cb06642008-03-15 13:17:42 -0700577# If the branch to rebase is given, that is the branch we will rebase
Kaartic Sivaraam3a9156a2017-12-16 14:33:17 +0530578# $branch_name -- branch/commit being rebased, or HEAD (already detached)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700579# $orig_head -- commit object name of tip of the branch before rebasing
580# $head_name -- refs/heads/<that-branch> or "detached HEAD"
581switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800582case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01005831)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700584 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100585 branch_name="$1"
586 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700587
Kaartic Sivaraam3a9156a2017-12-16 14:33:17 +0530588 # Is it a local branch?
589 if git show-ref --verify --quiet -- "refs/heads/$branch_name" &&
590 orig_head=$(git rev-parse -q --verify "refs/heads/$branch_name")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800591 then
Kaartic Sivaraam3a9156a2017-12-16 14:33:17 +0530592 head_name="refs/heads/$branch_name"
593 # If not is it a valid ref (branch or commit)?
594 elif orig_head=$(git rev-parse -q --verify "$branch_name")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700595 then
596 head_name="detached HEAD"
Kaartic Sivaraam3a9156a2017-12-16 14:33:17 +0530597
Junio C Hamanobcf31612007-01-20 19:11:29 -0800598 else
Kaartic Sivaraamca7de7b2017-12-16 14:33:18 +0530599 die "$(eval_gettext "fatal: no such branch/commit '\$branch_name'")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800600 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800601 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07006020)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700603 # Do not need to switch branches, we are already on it.
Elia Pinto728fc792014-04-23 06:44:00 -0700604 if branch_name=$(git symbolic-ref -q HEAD)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700605 then
606 head_name=$branch_name
Elia Pinto728fc792014-04-23 06:44:00 -0700607 branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)')
Junio C Hamano0cb06642008-03-15 13:17:42 -0700608 else
609 head_name="detached HEAD"
Kaartic Sivaraam3a9156a2017-12-16 14:33:17 +0530610 branch_name=HEAD
Junio C Hamano0cb06642008-03-15 13:17:42 -0700611 fi
Phil Hordea709802013-04-23 18:51:14 -0400612 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700613 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700614*)
615 die "BUG: unexpected number of arguments left to parse"
616 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800617esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700618
John Keepingad8261d2013-12-09 23:16:16 +0000619if test "$fork_point" = t
620then
John Keepingbb3f4582014-01-09 19:47:34 +0000621 new_upstream=$(git merge-base --fork-point "$upstream_name" \
622 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000623 if test -n "$new_upstream"
624 then
John Keeping1e0dacd2014-07-16 20:23:49 +0100625 restrict_revision=$new_upstream
John Keepingad8261d2013-12-09 23:16:16 +0000626 fi
627fi
628
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530629if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
630then
631 stash_sha1=$(git stash create "autostash") ||
632 die "$(gettext 'Cannot autostash')"
633
634 mkdir -p "$state_dir" &&
635 echo $stash_sha1 >"$state_dir/autostash" &&
636 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
637 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
638 git reset --hard
639fi
640
Jiang Xinc7108bf2012-07-25 22:53:08 +0800641require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500642
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500643# Now we are rebasing commits $upstream..$orig_head (or with --root,
644# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800645
Johannes Sixt1308c172007-07-04 22:09:10 +0200646# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500647# but this should be done only when upstream and onto are the same
648# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500649mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500650if test "$type" != interactive && test "$upstream" = "$onto" &&
John Keeping1e0dacd2014-07-16 20:23:49 +0100651 test "$mb" = "$onto" && test -z "$restrict_revision" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200652 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500653 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800654then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100655 if test -z "$force_rebase"
656 then
657 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530658 test -z "$switch_to" ||
659 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
Jeff King22946a92015-04-28 01:17:37 -0400660 git checkout -q "$switch_to" --
Kaartic Sivaraam08e66702017-12-16 14:33:19 +0530661 if test "$branch_name" = "HEAD" &&
662 ! git symbolic-ref -q HEAD
663 then
664 say "$(eval_gettext "HEAD is up to date.")"
665 else
666 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
667 fi
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530668 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100669 exit 0
670 else
Kaartic Sivaraam08e66702017-12-16 14:33:19 +0530671 if test "$branch_name" = "HEAD" &&
672 ! git symbolic-ref -q HEAD
673 then
674 say "$(eval_gettext "HEAD is up to date, rebase forced.")"
675 else
676 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
677 fi
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100678 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800679fi
680
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500681# If a hook exists, give it a chance to interrupt
682run_pre_rebase_hook "$upstream_arg" "$@"
683
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100684if test -n "$diffstat"
685then
686 if test -n "$verbose"
687 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800688 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100689 fi
690 # We want color (if set), but no pager
691 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
692fi
693
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500694test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500695
696# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800697say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530698
699GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
700 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500701git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500702
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800703# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300704# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500705if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100706then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800707 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000708 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530709 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100710 exit 0
711fi
712
Thomas Rast190f5322009-01-05 18:35:16 +0100713if test -n "$rebase_root"
714then
715 revisions="$onto..$orig_head"
716else
John Keeping1e0dacd2014-07-16 20:23:49 +0100717 revisions="${restrict_revision-$upstream}..$orig_head"
Thomas Rast190f5322009-01-05 18:35:16 +0100718fi
719
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500720run_specific_rebase