blob: 842d7d494a75583376610d88e376767aedf40b51 [file] [log] [blame]
Junio C Hamano59e6b232005-06-25 02:23:43 -07001#!/bin/sh
2#
3# Copyright (c) 2005 Junio C Hamano.
4#
5
Junio C Hamano533b7032007-01-12 12:52:03 -08006SUBDIRECTORY_OK=Yes
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -05007OPTIONS_KEEPDASHDASH=
Nicolas Vigierb6e9e732014-02-10 01:03:36 +00008OPTIONS_STUCKLONG=t
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -05009OPTIONS_SPEC="\
Lucien Kongc2145382012-06-12 10:05:12 +020010git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>]
Junio C Hamano0cd993a2012-07-15 21:38:41 -070011git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>]
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040012git-rebase --continue | --abort | --skip | --edit-todo
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050013--
14 Available options are
15v,verbose! display a diffstat of what changed upstream
16q,quiet! be quiet. implies --no-stat
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053017autostash! automatically stash/stash pop before and after
John Keepingad8261d2013-12-09 23:16:16 +000018fork-point use 'merge-base --fork-point' to refine upstream
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050019onto=! rebase onto given branch instead of upstream
20p,preserve-merges! try to recreate merges instead of ignoring them
21s,strategy=! use the given merge strategy
22no-ff! cherry-pick all commits, even if unchanged
23m,merge! use merging strategies to rebase
24i,interactive! let the user edit the list of commits to rebase
Lucien Kongc2145382012-06-12 10:05:12 +020025x,exec=! add exec lines after each commit of the editable list
Neil Horman90e18182012-04-20 10:36:17 -040026k,keep-empty preserve empty commits during rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050027f,force-rebase! force rebase even if branch is up to date
28X,strategy-option=! pass the argument through to the merge strategy
29stat! display a diffstat of what changed upstream
30n,no-stat! do not show diffstat of what changed upstream
31verify allow pre-rebase hook to run
32rerere-autoupdate allow rerere to update index with resolved conflicts
33root! rebase all reachable commits up to the root(s)
34autosquash move commits that begin with squash!/fixup! under -i
35committer-date-is-author-date! passed to 'git am'
36ignore-date! passed to 'git am'
37whitespace=! passed to 'git apply'
38ignore-whitespace! passed to 'git apply'
39C=! passed to 'git apply'
40 Actions:
Martin von Zweigbergk5960bc92011-07-13 23:47:06 -040041continue! continue
42abort! abort and check out the original branch
43skip! skip current patch and continue
Andrew Wongeb9a7cb2012-09-17 21:28:09 -040044edit-todo! edit the todo list during an interactive rebase
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -050045"
Junio C Hamanoae2b0f12005-11-24 00:12:11 -080046. git-sh-setup
Jiang Xinc7108bf2012-07-25 22:53:08 +080047. git-sh-i18n
Shawn O. Pearcef9474132006-12-28 02:34:48 -050048set_reflog_action rebase
Jeff King035b5bf2011-10-13 11:59:24 -040049require_work_tree_exists
Junio C Hamano533b7032007-01-12 12:52:03 -080050cd_to_toplevel
Junio C Hamano4282c4f2005-08-07 15:51:09 -070051
Junio C Hamano61dfa1b2009-11-20 03:02:44 -080052LF='
53'
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050054ok_to_skip_pre_rebase=
55resolvemsg="
Jiang Xinc7108bf2012-07-25 22:53:08 +080056$(gettext 'When you have resolved this problem, run "git rebase --continue".
57If you prefer to skip this patch, run "git rebase --skip" instead.
58To check out the original branch and stop rebasing, run "git rebase --abort".')
Seancc120052006-05-13 23:34:08 -040059"
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -050060unset onto
Lucien Kongc2145382012-06-12 10:05:12 +020061cmd=
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -050062strategy=
Mike Lundy93ce1902010-07-29 00:04:29 +020063strategy_opts=
Eric Wong58634db2006-06-21 03:04:41 -070064do_merge=
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -050065merge_dir="$GIT_DIR"/rebase-merge
66apply_dir="$GIT_DIR"/rebase-apply
Robert Shearmanb7587892006-10-03 17:29:31 +010067verbose=
Martin von Zweigbergk9474a022010-11-09 21:59:00 +010068diffstat=
69test "$(git config --bool rebase.stat)" = true && diffstat=t
Ramkumar Ramachandra58794772013-05-12 17:26:41 +053070autostash="$(git config --bool rebase.autostash || echo false)"
John Keepingad8261d2013-12-09 23:16:16 +000071fork_point=auto
Michael S. Tsirkin67dad682007-02-08 15:57:08 +020072git_am_opt=
Thomas Rast190f5322009-01-05 18:35:16 +010073rebase_root=
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +010074force_rebase=
Junio C Hamanocb6020b2009-12-04 00:20:48 -080075allow_rerere_autoupdate=
Martin von Zweigbergk99de0642011-02-06 13:43:34 -050076# Non-empty if a rebase was in progress when 'git rebase' was invoked
77in_progress=
78# One of {am, merge, interactive}
79type=
80# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
81state_dir=
Martin von Zweigbergk34262322011-02-06 13:43:35 -050082# One of {'', continue, skip, abort}, as parsed from command line
83action=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050084preserve_merges=
85autosquash=
Neil Horman90e18182012-04-20 10:36:17 -040086keep_empty=
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -050087test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
Eric Wong58634db2006-06-21 03:04:41 -070088
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -050089read_basic_state () {
Ramkumar Ramachandradc8ca912013-06-13 21:36:11 +053090 test -f "$state_dir/head-name" &&
91 test -f "$state_dir/onto" &&
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -050092 head_name=$(cat "$state_dir"/head-name) &&
93 onto=$(cat "$state_dir"/onto) &&
Martin von Zweigbergk84df4562011-02-06 13:43:53 -050094 # We always write to orig-head, but interactive rebase used to write to
95 # head. Fall back to reading from head to cover for the case that the
96 # user upgraded git with an ongoing interactive rebase.
97 if test -f "$state_dir"/orig-head
Martin von Zweigbergk2959c282011-02-06 13:43:52 -050098 then
Martin von Zweigbergk2959c282011-02-06 13:43:52 -050099 orig_head=$(cat "$state_dir"/orig-head)
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500100 else
101 orig_head=$(cat "$state_dir"/head)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500102 fi &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500103 GIT_QUIET=$(cat "$state_dir"/quiet) &&
104 test -f "$state_dir"/verbose && verbose=t
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500105 test -f "$state_dir"/strategy && strategy="$(cat "$state_dir"/strategy)"
106 test -f "$state_dir"/strategy_opts &&
107 strategy_opts="$(cat "$state_dir"/strategy_opts)"
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500108 test -f "$state_dir"/allow_rerere_autoupdate &&
109 allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)"
Martin von Zweigbergk02ac45f2011-02-06 13:43:31 -0500110}
111
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500112write_basic_state () {
113 echo "$head_name" > "$state_dir"/head-name &&
114 echo "$onto" > "$state_dir"/onto &&
115 echo "$orig_head" > "$state_dir"/orig-head &&
Martin von Zweigbergk7b37a7c2011-02-06 13:43:54 -0500116 echo "$GIT_QUIET" > "$state_dir"/quiet &&
117 test t = "$verbose" && : > "$state_dir"/verbose
Martin von Zweigbergk80ff4792011-02-06 13:43:55 -0500118 test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy
119 test -n "$strategy_opts" && echo "$strategy_opts" > \
120 "$state_dir"/strategy_opts
Martin von Zweigbergkb3e48472011-02-06 13:43:56 -0500121 test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \
122 "$state_dir"/allow_rerere_autoupdate
Martin von Zweigbergk84df4562011-02-06 13:43:53 -0500123}
124
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500125output () {
126 case "$verbose" in
127 '')
128 output=$("$@" 2>&1 )
129 status=$?
130 test $status != 0 && printf "%s\n" "$output"
131 return $status
132 ;;
133 *)
134 "$@"
135 ;;
136 esac
137}
138
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000139move_to_original_branch () {
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000140 case "$head_name" in
141 refs/*)
142 message="rebase finished: $head_name onto $onto"
143 git update-ref -m "$message" \
144 $head_name $(git rev-parse HEAD) $orig_head &&
Jeff King53f2ffa2011-05-27 16:16:14 -0400145 git symbolic-ref \
146 -m "rebase finished: returning to $head_name" \
147 HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800148 die "$(gettext "Could not move back to $head_name")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000149 ;;
150 esac
151}
152
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530153finish_rebase () {
154 if test -f "$state_dir/autostash"
155 then
156 stash_sha1=$(cat "$state_dir/autostash")
157 if git stash apply $stash_sha1 2>&1 >/dev/null
158 then
159 echo "$(gettext 'Applied autostash.')"
160 else
Ramkumar Ramachandra20351bb2013-06-15 18:43:26 +0530161 git stash store -m "autostash" -q $stash_sha1 ||
162 die "$(eval_gettext "Cannot store \$stash_sha1")"
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530163 gettext 'Applying autostash resulted in conflicts.
164Your changes are safe in the stash.
Ralf Thielowac1998d2013-07-29 06:24:43 +0200165You can run "git stash pop" or "git stash drop" at any time.
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530166'
167 fi
168 fi
169 git gc --auto &&
170 rm -rf "$state_dir"
171}
172
Matthieu Moy99855dd2013-09-09 10:53:15 +0200173run_specific_rebase_internal () {
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200174 if [ "$interactive_rebase" = implied ]; then
175 GIT_EDITOR=:
176 export GIT_EDITOR
Vincent van Ravesteijn8a6dae12012-05-24 13:57:26 +0000177 autosquash=
Andreas Ericssonf8cca012008-09-29 22:28:57 +0200178 fi
Matthieu Moy99855dd2013-09-09 10:53:15 +0200179 # On FreeBSD, the shell's "return" returns from the current
180 # function, not from the current file inclusion.
181 # run_specific_rebase_internal has the file inclusion as a
182 # last statement, so POSIX and FreeBSD's return will do the
183 # same thing.
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500184 . git-rebase--$type
Matthieu Moy99855dd2013-09-09 10:53:15 +0200185}
186
187run_specific_rebase () {
188 run_specific_rebase_internal
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530189 ret=$?
190 if test $ret -eq 0
191 then
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530192 finish_rebase
Ramkumar Ramachandraf5f758a2013-05-12 17:26:37 +0530193 fi
194 exit $ret
Johannes Schindelin1b1dce42007-06-25 01:11:14 +0100195}
196
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900197run_pre_rebase_hook () {
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500198 if test -z "$ok_to_skip_pre_rebase" &&
Nanako Shiraishic4427652008-10-06 14:14:29 +0900199 test -x "$GIT_DIR/hooks/pre-rebase"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900200 then
Stephen Boydbc2bbc42009-06-14 16:08:56 -0700201 "$GIT_DIR/hooks/pre-rebase" ${1+"$@"} ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800202 die "$(gettext "The pre-rebase hook refused to rebase.")"
Nanako Shiraishid70b4a82008-10-06 14:14:24 +0900203 fi
204}
205
Martin von Zweigbergk69a636a2011-02-06 13:43:30 -0500206test -f "$apply_dir"/applying &&
Jiang Xinc7108bf2012-07-25 22:53:08 +0800207 die "$(gettext "It looks like git-am is in progress. Cannot rebase.")"
Stephan Beyer9b752a62008-08-17 06:25:43 +0200208
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500209if test -d "$apply_dir"
210then
211 type=am
212 state_dir="$apply_dir"
213elif test -d "$merge_dir"
214then
215 if test -f "$merge_dir"/interactive
216 then
217 type=interactive
218 interactive_rebase=explicit
219 else
220 type=merge
221 fi
222 state_dir="$merge_dir"
223fi
224test -n "$type" && in_progress=t
225
Martin von Zweigbergk95135b02011-02-06 13:43:36 -0500226total_argc=$#
David Kastrup822f7c72007-09-23 22:42:08 +0200227while test $# != 0
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800228do
229 case "$1" in
Nanako Shiraishic4427652008-10-06 14:14:29 +0900230 --no-verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500231 ok_to_skip_pre_rebase=yes
Nanako Shiraishic4427652008-10-06 14:14:29 +0900232 ;;
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100233 --verify)
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500234 ok_to_skip_pre_rebase=
Martin von Zweigbergk7baf9c42010-11-22 21:21:01 +0100235 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400236 --continue|--skip|--abort|--edit-todo)
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500237 test $total_argc -eq 2 || usage
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500238 action=${1##--}
sean031321c2006-04-26 10:49:38 -0400239 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000240 --onto=*)
241 onto="${1#--onto=}"
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800242 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000243 --exec=*)
244 cmd="${cmd}exec ${1#--exec=}${LF}"
Lucien Kongc2145382012-06-12 10:05:12 +0200245 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000246 --interactive)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500247 interactive_rebase=explicit
248 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000249 --keep-empty)
Neil Horman90e18182012-04-20 10:36:17 -0400250 keep_empty=yes
251 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000252 --preserve-merges)
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500253 preserve_merges=t
254 test -z "$interactive_rebase" && interactive_rebase=implied
255 ;;
256 --autosquash)
257 autosquash=t
258 ;;
259 --no-autosquash)
260 autosquash=
261 ;;
John Keepingad8261d2013-12-09 23:16:16 +0000262 --fork-point)
263 fork_point=t
264 ;;
265 --no-fork-point)
266 fork_point=
267 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000268 --merge)
Eric Wong58634db2006-06-21 03:04:41 -0700269 do_merge=t
270 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000271 --strategy-option=*)
272 strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")"
Mike Lundy93ce1902010-07-29 00:04:29 +0200273 do_merge=t
Martin von Zweigbergk9765b6a2011-02-06 13:43:38 -0500274 test -z "$strategy" && strategy=recursive
Mike Lundy93ce1902010-07-29 00:04:29 +0200275 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000276 --strategy=*)
277 strategy="${1#--strategy=}"
Eric Wong58634db2006-06-21 03:04:41 -0700278 do_merge=t
279 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000280 --no-stat)
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100281 diffstat=
282 ;;
283 --stat)
284 diffstat=t
285 ;;
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530286 --autostash)
287 autostash=true
288 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000289 --verbose)
Robert Shearmanb7587892006-10-03 17:29:31 +0100290 verbose=t
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100291 diffstat=t
Stephen Boyd0e987a12009-06-16 15:33:01 -0700292 GIT_QUIET=
293 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000294 --quiet)
Stephen Boyd0e987a12009-06-16 15:33:01 -0700295 GIT_QUIET=t
296 git_am_opt="$git_am_opt -q"
297 verbose=
298 diffstat=
Robert Shearmanb7587892006-10-03 17:29:31 +0100299 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000300 --whitespace=*)
301 git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}"
302 case "${1#--whitespace=}" in
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500303 fix|strip)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100304 force_rebase=t
305 ;;
306 esac
J. Bruce Fields059f4462007-09-07 10:20:50 -0400307 ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200308 --ignore-whitespace)
309 git_am_opt="$git_am_opt $1"
310 ;;
Michele Ballabio570ccad2009-03-18 21:53:49 +0100311 --committer-date-is-author-date|--ignore-date)
312 git_am_opt="$git_am_opt $1"
313 force_rebase=t
314 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000315 -C*)
316 git_am_opt="$git_am_opt $1"
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200317 ;;
Thomas Rast190f5322009-01-05 18:35:16 +0100318 --root)
319 rebase_root=t
320 ;;
Nicolas Vigierb6e9e732014-02-10 01:03:36 +0000321 --force-rebase|--no-ff)
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100322 force_rebase=t
323 ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800324 --rerere-autoupdate|--no-rerere-autoupdate)
325 allow_rerere_autoupdate="$1"
326 ;;
Martin von Zweigbergk45e2acf2011-02-28 20:59:26 -0500327 --)
328 shift
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800329 break
330 ;;
331 esac
332 shift
333done
Jay Soffian51b2ead2009-02-18 08:44:02 -0500334test $# -gt 2 && usage
Junio C Hamano2db8aae2005-12-14 03:11:37 -0800335
Lucien Kongc2145382012-06-12 10:05:12 +0200336if test -n "$cmd" &&
337 test "$interactive_rebase" != explicit
338then
Jiang Xin465d6a02012-07-25 22:53:09 +0800339 die "$(gettext "The --exec option must be used with the --interactive option")"
Lucien Kongc2145382012-06-12 10:05:12 +0200340fi
341
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500342if test -n "$action"
343then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800344 test -z "$in_progress" && die "$(gettext "No rebase in progress?")"
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500345 # Only interactive rebase uses detailed reflog messages
346 if test "$type" = interactive && test "$GIT_REFLOG_ACTION" = rebase
347 then
348 GIT_REFLOG_ACTION="rebase -i ($action)"
349 export GIT_REFLOG_ACTION
350 fi
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500351fi
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500352
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400353if test "$action" = "edit-todo" && test "$type" != "interactive"
354then
355 die "$(gettext "The --edit-todo action can only be used during interactive rebase.")"
356fi
357
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500358case "$action" in
359continue)
Martin von Zweigbergk2959c282011-02-06 13:43:52 -0500360 # Sanity check
361 git rev-parse --verify HEAD >/dev/null ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800362 die "$(gettext "Cannot read HEAD")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500363 git update-index --ignore-submodules --refresh &&
364 git diff-files --quiet --ignore-submodules || {
Jiang Xinc7108bf2012-07-25 22:53:08 +0800365 echo "$(gettext "You must edit all merge conflicts and then
366mark them as resolved using git add")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500367 exit 1
368 }
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500369 read_basic_state
370 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500371 ;;
372skip)
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500373 output git reset --hard HEAD || exit $?
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500374 read_basic_state
375 run_specific_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500376 ;;
377abort)
378 git rerere clear
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500379 read_basic_state
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500380 case "$head_name" in
381 refs/*)
Csaba Henkea696192011-05-27 16:13:02 -0400382 git symbolic-ref -m "rebase: aborting" HEAD $head_name ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800383 die "$(eval_gettext "Could not move back to \$head_name")"
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500384 ;;
385 esac
Martin von Zweigbergk4974c2c2011-02-06 13:43:51 -0500386 output git reset --hard $orig_head
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530387 finish_rebase
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500388 exit
389 ;;
Andrew Wongeb9a7cb2012-09-17 21:28:09 -0400390edit-todo)
391 run_specific_rebase
392 ;;
Martin von Zweigbergk34262322011-02-06 13:43:35 -0500393esac
394
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500395# Make sure no rebase is in progress
396if test -n "$in_progress"
Jonathan Niederbffd7502010-05-31 17:51:32 -0500397then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800398 state_dir_base=${state_dir##*/}
399 cmd_live_rebase="git rebase (--continue | --abort | --skip)"
400 cmd_clear_stale_rebase="rm -fr \"$state_dir\""
401 die "
402$(eval_gettext 'It seems that there is already a $state_dir_base directory, and
Ralf Thielowe39beac2012-08-01 19:09:09 +0200403I wonder if you are in the middle of another rebase. If that is the
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500404case, please try
Jiang Xinc7108bf2012-07-25 22:53:08 +0800405 $cmd_live_rebase
Martin von Zweigbergk99de0642011-02-06 13:43:34 -0500406If that is not the case, please
Jiang Xinc7108bf2012-07-25 22:53:08 +0800407 $cmd_clear_stale_rebase
Stephan Beyer9b752a62008-08-17 06:25:43 +0200408and run me again. I am stopping in case you still have something
Jiang Xinc7108bf2012-07-25 22:53:08 +0800409valuable there.')"
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800410fi
411
Chris Webbdf5df202012-06-26 22:55:23 +0100412if test -n "$rebase_root" && test -z "$onto"
413then
414 test -z "$interactive_rebase" && interactive_rebase=implied
415fi
416
Martin von Zweigbergkcf432ca2011-02-06 13:43:39 -0500417if test -n "$interactive_rebase"
418then
419 type=interactive
420 state_dir="$merge_dir"
421elif test -n "$do_merge"
422then
423 type=merge
424 state_dir="$merge_dir"
425else
426 type=am
427 state_dir="$apply_dir"
428fi
429
Thomas Rast190f5322009-01-05 18:35:16 +0100430if test -z "$rebase_root"
431then
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500432 case "$#" in
433 0)
434 if ! upstream_name=$(git rev-parse --symbolic-full-name \
435 --verify -q @{upstream} 2>/dev/null)
436 then
437 . git-parse-remote
438 error_on_missing_default_upstream "rebase" "rebase" \
Carlos Martín Nieto3c023962012-03-04 05:41:26 +0100439 "against" "git rebase <branch>"
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500440 fi
John Keepingad8261d2013-12-09 23:16:16 +0000441
442 test "$fork_point" = auto && fork_point=t
Martin von Zweigbergk15a147e2011-02-09 20:54:02 -0500443 ;;
444 *) upstream_name="$1"
445 shift
446 ;;
447 esac
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530448 upstream=$(peel_committish "${upstream_name}") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800449 die "$(eval_gettext "invalid upstream \$upstream_name")"
Thomas Rast190f5322009-01-05 18:35:16 +0100450 upstream_arg="$upstream_name"
451else
Chris Webbdf5df202012-06-26 22:55:23 +0100452 if test -z "$onto"
453 then
454 empty_tree=`git hash-object -t tree /dev/null`
455 onto=`git commit-tree $empty_tree </dev/null`
456 squash_onto="$onto"
457 fi
Thomas Rast190f5322009-01-05 18:35:16 +0100458 unset upstream_name
459 unset upstream
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700460 test $# -gt 1 && usage
Martin von Zweigbergk46df82d2011-02-06 13:43:48 -0500461 upstream_arg=--root
Thomas Rast190f5322009-01-05 18:35:16 +0100462fi
Lukas Sandström32d99542005-12-15 00:36:35 +0100463
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700464# Make sure the branch to rebase onto is valid.
Martin von Zweigbergk6bb4e482011-02-06 13:43:37 -0500465onto_name=${onto-"$upstream_name"}
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900466case "$onto_name" in
467*...*)
468 if left=${onto_name%...*} right=${onto_name#*...} &&
469 onto=$(git merge-base --all ${left:-HEAD} ${right:-HEAD})
470 then
471 case "$onto" in
472 ?*"$LF"?*)
Jiang Xinc7108bf2012-07-25 22:53:08 +0800473 die "$(eval_gettext "\$onto_name: there are more than one merge bases")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900474 ;;
475 '')
Jiang Xinc7108bf2012-07-25 22:53:08 +0800476 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900477 ;;
478 esac
479 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800480 die "$(eval_gettext "\$onto_name: there is no merge base")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900481 fi
482 ;;
483*)
Ramkumar Ramachandra2e6e2762013-06-14 18:47:52 +0530484 onto=$(peel_committish "$onto_name") ||
Jiang Xinc7108bf2012-07-25 22:53:08 +0800485 die "$(eval_gettext "Does not point to a valid commit: \$onto_name")"
Nanako Shiraishi9f21e972010-01-07 20:05:02 +0900486 ;;
487esac
Junio C Hamanoa1bf91e2007-03-22 02:54:59 -0700488
Junio C Hamano0cb06642008-03-15 13:17:42 -0700489# If the branch to rebase is given, that is the branch we will rebase
490# $branch_name -- branch being rebased, or HEAD (already detached)
491# $orig_head -- commit object name of tip of the branch before rebasing
492# $head_name -- refs/heads/<that-branch> or "detached HEAD"
493switch_to=
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800494case "$#" in
Thomas Rast190f5322009-01-05 18:35:16 +01004951)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700496 # Is it "rebase other $branchname" or "rebase other $commit"?
Thomas Rast190f5322009-01-05 18:35:16 +0100497 branch_name="$1"
498 switch_to="$1"
Junio C Hamano0cb06642008-03-15 13:17:42 -0700499
Thomas Rast190f5322009-01-05 18:35:16 +0100500 if git show-ref --verify --quiet -- "refs/heads/$1" &&
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500501 orig_head=$(git rev-parse -q --verify "refs/heads/$1")
Junio C Hamanobcf31612007-01-20 19:11:29 -0800502 then
Thomas Rast190f5322009-01-05 18:35:16 +0100503 head_name="refs/heads/$1"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500504 elif orig_head=$(git rev-parse -q --verify "$1")
Junio C Hamano0cb06642008-03-15 13:17:42 -0700505 then
506 head_name="detached HEAD"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800507 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800508 die "$(eval_gettext "fatal: no such branch: \$branch_name")"
Junio C Hamanobcf31612007-01-20 19:11:29 -0800509 fi
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800510 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -07005110)
Junio C Hamano0cb06642008-03-15 13:17:42 -0700512 # Do not need to switch branches, we are already on it.
513 if branch_name=`git symbolic-ref -q HEAD`
514 then
515 head_name=$branch_name
516 branch_name=`expr "z$branch_name" : 'zrefs/heads/\(.*\)'`
517 else
518 head_name="detached HEAD"
519 branch_name=HEAD ;# detached
520 fi
Phil Hordea709802013-04-23 18:51:14 -0400521 orig_head=$(git rev-parse --verify HEAD) || exit
Junio C Hamano0cb06642008-03-15 13:17:42 -0700522 ;;
Martin von Zweigbergkf2b6a192012-06-26 07:51:55 -0700523*)
524 die "BUG: unexpected number of arguments left to parse"
525 ;;
Junio C Hamano7f59dbb2005-11-14 00:41:53 -0800526esac
Junio C Hamano99a92f92005-08-17 15:19:57 -0700527
John Keepingad8261d2013-12-09 23:16:16 +0000528if test "$fork_point" = t
529then
John Keepingbb3f4582014-01-09 19:47:34 +0000530 new_upstream=$(git merge-base --fork-point "$upstream_name" \
531 "${switch_to:-HEAD}")
John Keepingad8261d2013-12-09 23:16:16 +0000532 if test -n "$new_upstream"
533 then
534 upstream=$new_upstream
535 fi
536fi
537
Ramkumar Ramachandra58794772013-05-12 17:26:41 +0530538if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null
539then
540 stash_sha1=$(git stash create "autostash") ||
541 die "$(gettext 'Cannot autostash')"
542
543 mkdir -p "$state_dir" &&
544 echo $stash_sha1 >"$state_dir/autostash" &&
545 stash_abbrev=$(git rev-parse --short $stash_sha1) &&
546 echo "$(eval_gettext 'Created autostash: $stash_abbrev')" &&
547 git reset --hard
548fi
549
Jiang Xinc7108bf2012-07-25 22:53:08 +0800550require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")"
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500551
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500552# Now we are rebasing commits $upstream..$orig_head (or with --root,
553# everything leading up to $orig_head) on top of $onto
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800554
Johannes Sixt1308c172007-07-04 22:09:10 +0200555# Check if we are already based on $onto with linear history,
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500556# but this should be done only when upstream and onto are the same
557# and if this is not an interactive rebase.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500558mb=$(git merge-base "$onto" "$orig_head")
Martin von Zweigbergkcc1453e2011-02-06 13:43:44 -0500559if test "$type" != interactive && test "$upstream" = "$onto" &&
560 test "$mb" = "$onto" &&
Johannes Sixt1308c172007-07-04 22:09:10 +0200561 # linear history?
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500562 ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800563then
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100564 if test -z "$force_rebase"
565 then
566 # Lazily switch to the target branch if needed...
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530567 test -z "$switch_to" ||
568 GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \
569 git checkout "$switch_to" --
Jiang Xinc7108bf2012-07-25 22:53:08 +0800570 say "$(eval_gettext "Current branch \$branch_name is up to date.")"
Ramkumar Ramachandra96e2b992013-06-13 21:36:13 +0530571 finish_rebase
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100572 exit 0
573 else
Jiang Xinc7108bf2012-07-25 22:53:08 +0800574 say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")"
Sverre Rabbelierb2f82e02009-02-13 23:48:01 +0100575 fi
Junio C Hamano7f4bd5d2005-11-28 13:00:31 -0800576fi
577
Martin von Zweigbergk8f9bfb62011-02-06 13:43:41 -0500578# If a hook exists, give it a chance to interrupt
579run_pre_rebase_hook "$upstream_arg" "$@"
580
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100581if test -n "$diffstat"
582then
583 if test -n "$verbose"
584 then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800585 echo "$(eval_gettext "Changes from \$mb to \$onto:")"
Tor Arne Vestbøa9c38212009-03-01 23:11:38 +0100586 fi
587 # We want color (if set), but no pager
588 GIT_PAGER='' git diff --stat --summary "$mb" "$onto"
589fi
590
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500591test "$type" = interactive && run_specific_rebase
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500592
593# Detach HEAD and reset the tree
Jiang Xinc7108bf2012-07-25 22:53:08 +0800594say "$(gettext "First, rewinding head to replay your work on top of it...")"
Ramkumar Ramachandra4b03df22013-06-16 14:15:12 +0530595
596GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \
597 git checkout -q "$onto^0" || die "could not detach HEAD"
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500598git update-ref ORIG_HEAD $orig_head
Martin von Zweigbergkf4107d92011-02-06 13:43:45 -0500599
Junio C Hamanoe646c9c2006-02-14 14:42:05 -0800600# If the $onto is a proper descendant of the tip of the branch, then
Felipe Contrerasa75d7b52009-10-24 11:31:32 +0300601# we just fast-forwarded.
Martin von Zweigbergkcb82a052011-02-06 13:43:46 -0500602if test "$mb" = "$orig_head"
Lukas Sandström32d99542005-12-15 00:36:35 +0100603then
Jiang Xinc7108bf2012-07-25 22:53:08 +0800604 say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")"
Johannes Schindelin6fd2f5e2007-11-08 18:19:08 +0000605 move_to_original_branch
Ramkumar Ramachandraaf2f0eb2013-06-13 21:36:12 +0530606 finish_rebase
Lukas Sandström32d99542005-12-15 00:36:35 +0100607 exit 0
608fi
609
Thomas Rast190f5322009-01-05 18:35:16 +0100610if test -n "$rebase_root"
611then
612 revisions="$onto..$orig_head"
613else
614 revisions="$upstream..$orig_head"
615fi
616
Martin von Zweigbergkfa99c1e2011-02-06 13:43:47 -0500617run_specific_rebase