Junio C Hamano | 59e6b23 | 2005-06-25 02:23:43 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2005 Junio C Hamano. |
| 4 | # |
| 5 | |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 6 | SUBDIRECTORY_OK=Yes |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 7 | OPTIONS_KEEPDASHDASH= |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 8 | OPTIONS_STUCKLONG=t |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 9 | OPTIONS_SPEC="\ |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 10 | git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] [<upstream>] [<branch>] |
Junio C Hamano | 0cd993a | 2012-07-15 21:38:41 -0700 | [diff] [blame] | 11 | git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] --root [<branch>] |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 12 | git-rebase --continue | --abort | --skip | --edit-todo |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 13 | -- |
| 14 | Available options are |
| 15 | v,verbose! display a diffstat of what changed upstream |
| 16 | q,quiet! be quiet. implies --no-stat |
John Keeping | 619e360 | 2015-09-10 23:30:51 +0100 | [diff] [blame] | 17 | autostash automatically stash/stash pop before and after |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 18 | fork-point use 'merge-base --fork-point' to refine upstream |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 19 | onto=! rebase onto given branch instead of upstream |
| 20 | p,preserve-merges! try to recreate merges instead of ignoring them |
| 21 | s,strategy=! use the given merge strategy |
| 22 | no-ff! cherry-pick all commits, even if unchanged |
| 23 | m,merge! use merging strategies to rebase |
| 24 | i,interactive! let the user edit the list of commits to rebase |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 25 | x,exec=! add exec lines after each commit of the editable list |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 26 | k,keep-empty preserve empty commits during rebase |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 27 | f,force-rebase! force rebase even if branch is up to date |
| 28 | X,strategy-option=! pass the argument through to the merge strategy |
| 29 | stat! display a diffstat of what changed upstream |
| 30 | n,no-stat! do not show diffstat of what changed upstream |
| 31 | verify allow pre-rebase hook to run |
| 32 | rerere-autoupdate allow rerere to update index with resolved conflicts |
| 33 | root! rebase all reachable commits up to the root(s) |
| 34 | autosquash move commits that begin with squash!/fixup! under -i |
| 35 | committer-date-is-author-date! passed to 'git am' |
| 36 | ignore-date! passed to 'git am' |
Giuseppe Bilotta | 9f79524 | 2017-04-18 11:29:05 +0200 | [diff] [blame] | 37 | signoff passed to 'git am' |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 38 | whitespace=! passed to 'git apply' |
| 39 | ignore-whitespace! passed to 'git apply' |
| 40 | C=! passed to 'git apply' |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 41 | S,gpg-sign? GPG-sign commits |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 42 | Actions: |
Martin von Zweigbergk | 5960bc9 | 2011-07-13 23:47:06 -0400 | [diff] [blame] | 43 | continue! continue |
| 44 | abort! abort and check out the original branch |
| 45 | skip! skip current patch and continue |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 46 | edit-todo! edit the todo list during an interactive rebase |
Nguyễn Thái Ngọc Duy | 9512177 | 2016-11-12 09:00:41 +0700 | [diff] [blame] | 47 | quit! abort but keep HEAD where it is |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 48 | " |
Junio C Hamano | ae2b0f1 | 2005-11-24 00:12:11 -0800 | [diff] [blame] | 49 | . git-sh-setup |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 50 | set_reflog_action rebase |
Jeff King | 035b5bf | 2011-10-13 11:59:24 -0400 | [diff] [blame] | 51 | require_work_tree_exists |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 52 | cd_to_toplevel |
Junio C Hamano | 4282c4f | 2005-08-07 15:51:09 -0700 | [diff] [blame] | 53 | |
Junio C Hamano | 61dfa1b | 2009-11-20 03:02:44 -0800 | [diff] [blame] | 54 | LF=' |
| 55 | ' |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 56 | ok_to_skip_pre_rebase= |
| 57 | resolvemsg=" |
William Duclot | 5fdacc1 | 2017-07-16 21:42:20 +0200 | [diff] [blame^] | 58 | $(gettext 'Resolve all conflicts manually, mark them as resolved with |
| 59 | "git add/rm <conflicted_files>", then run "git rebase --continue". |
| 60 | You can instead skip this commit: run "git rebase --skip". |
| 61 | To abort and get back to the state before "git rebase", run "git rebase --abort".') |
Sean | cc12005 | 2006-05-13 23:34:08 -0400 | [diff] [blame] | 62 | " |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 63 | unset onto |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 64 | unset restrict_revision |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 65 | cmd= |
Martin von Zweigbergk | 9765b6a | 2011-02-06 13:43:38 -0500 | [diff] [blame] | 66 | strategy= |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 67 | strategy_opts= |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 68 | do_merge= |
Martin von Zweigbergk | 69a636a | 2011-02-06 13:43:30 -0500 | [diff] [blame] | 69 | merge_dir="$GIT_DIR"/rebase-merge |
| 70 | apply_dir="$GIT_DIR"/rebase-apply |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 71 | verbose= |
Martin von Zweigbergk | 9474a02 | 2010-11-09 21:59:00 +0100 | [diff] [blame] | 72 | diffstat= |
| 73 | test "$(git config --bool rebase.stat)" = true && diffstat=t |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 74 | autostash="$(git config --bool rebase.autostash || echo false)" |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 75 | fork_point=auto |
Michael S. Tsirkin | 67dad68 | 2007-02-08 15:57:08 +0200 | [diff] [blame] | 76 | git_am_opt= |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 77 | rebase_root= |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 78 | force_rebase= |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 79 | allow_rerere_autoupdate= |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 80 | # Non-empty if a rebase was in progress when 'git rebase' was invoked |
| 81 | in_progress= |
| 82 | # One of {am, merge, interactive} |
| 83 | type= |
| 84 | # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge} |
| 85 | state_dir= |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 86 | # One of {'', continue, skip, abort}, as parsed from command line |
| 87 | action= |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 88 | preserve_merges= |
| 89 | autosquash= |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 90 | keep_empty= |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 91 | test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t |
Junio C Hamano | 6694856 | 2016-05-02 14:58:45 -0700 | [diff] [blame] | 92 | case "$(git config --bool commit.gpgsign)" in |
| 93 | true) gpg_sign_opt=-S ;; |
| 94 | *) gpg_sign_opt= ;; |
| 95 | esac |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 96 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 97 | read_basic_state () { |
Ramkumar Ramachandra | dc8ca91 | 2013-06-13 21:36:11 +0530 | [diff] [blame] | 98 | test -f "$state_dir/head-name" && |
| 99 | test -f "$state_dir/onto" && |
Martin von Zweigbergk | 02ac45f | 2011-02-06 13:43:31 -0500 | [diff] [blame] | 100 | head_name=$(cat "$state_dir"/head-name) && |
| 101 | onto=$(cat "$state_dir"/onto) && |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 102 | # 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 Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 106 | then |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 107 | orig_head=$(cat "$state_dir"/orig-head) |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 108 | else |
| 109 | orig_head=$(cat "$state_dir"/head) |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 110 | fi && |
Martin von Zweigbergk | 7b37a7c | 2011-02-06 13:43:54 -0500 | [diff] [blame] | 111 | GIT_QUIET=$(cat "$state_dir"/quiet) && |
| 112 | test -f "$state_dir"/verbose && verbose=t |
Martin von Zweigbergk | 80ff479 | 2011-02-06 13:43:55 -0500 | [diff] [blame] | 113 | 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 Zweigbergk | b3e4847 | 2011-02-06 13:43:56 -0500 | [diff] [blame] | 116 | test -f "$state_dir"/allow_rerere_autoupdate && |
| 117 | allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)" |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 118 | test -f "$state_dir"/gpg_sign_opt && |
| 119 | gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)" |
Martin von Zweigbergk | 02ac45f | 2011-02-06 13:43:31 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 122 | write_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 Zweigbergk | 7b37a7c | 2011-02-06 13:43:54 -0500 | [diff] [blame] | 126 | echo "$GIT_QUIET" > "$state_dir"/quiet && |
| 127 | test t = "$verbose" && : > "$state_dir"/verbose |
Martin von Zweigbergk | 80ff479 | 2011-02-06 13:43:55 -0500 | [diff] [blame] | 128 | test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy |
| 129 | test -n "$strategy_opts" && echo "$strategy_opts" > \ |
| 130 | "$state_dir"/strategy_opts |
Martin von Zweigbergk | b3e4847 | 2011-02-06 13:43:56 -0500 | [diff] [blame] | 131 | test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \ |
| 132 | "$state_dir"/allow_rerere_autoupdate |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 133 | test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 136 | output () { |
| 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 Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 150 | move_to_original_branch () { |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 151 | 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 King | 53f2ffa | 2011-05-27 16:16:14 -0400 | [diff] [blame] | 156 | git symbolic-ref \ |
| 157 | -m "rebase finished: returning to $head_name" \ |
| 158 | HEAD $head_name || |
Vasco Almeida | 24a6df4 | 2016-06-17 20:20:58 +0000 | [diff] [blame] | 159 | die "$(eval_gettext "Could not move back to \$head_name")" |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 160 | ;; |
| 161 | esac |
| 162 | } |
| 163 | |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 164 | apply_autostash () { |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 165 | 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 Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 170 | echo "$(gettext 'Applied autostash.')" >&2 |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 171 | else |
Ramkumar Ramachandra | 20351bb | 2013-06-15 18:43:26 +0530 | [diff] [blame] | 172 | git stash store -m "autostash" -q $stash_sha1 || |
| 173 | die "$(eval_gettext "Cannot store \$stash_sha1")" |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 174 | gettext 'Applying autostash resulted in conflicts. |
| 175 | Your changes are safe in the stash. |
Ralf Thielow | ac1998d | 2013-07-29 06:24:43 +0200 | [diff] [blame] | 176 | You can run "git stash pop" or "git stash drop" at any time. |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 177 | ' >&2 |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 178 | fi |
| 179 | fi |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | finish_rebase () { |
| 183 | apply_autostash && |
Jeff King | 8c24f5b | 2016-01-13 13:47:18 -0500 | [diff] [blame] | 184 | { git gc --auto || true; } && |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 185 | rm -rf "$state_dir" |
| 186 | } |
| 187 | |
Kyle J. McKay | 8cd6596 | 2014-04-11 01:28:18 -0700 | [diff] [blame] | 188 | run_specific_rebase () { |
Andreas Ericsson | f8cca01 | 2008-09-29 22:28:57 +0200 | [diff] [blame] | 189 | if [ "$interactive_rebase" = implied ]; then |
| 190 | GIT_EDITOR=: |
| 191 | export GIT_EDITOR |
Vincent van Ravesteijn | 8a6dae1 | 2012-05-24 13:57:26 +0000 | [diff] [blame] | 192 | autosquash= |
Andreas Ericsson | f8cca01 | 2008-09-29 22:28:57 +0200 | [diff] [blame] | 193 | fi |
Martin von Zweigbergk | 46df82d | 2011-02-06 13:43:48 -0500 | [diff] [blame] | 194 | . git-rebase--$type |
Ramkumar Ramachandra | f5f758a | 2013-05-12 17:26:37 +0530 | [diff] [blame] | 195 | ret=$? |
| 196 | if test $ret -eq 0 |
| 197 | then |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 198 | finish_rebase |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 199 | 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 Ramachandra | f5f758a | 2013-05-12 17:26:37 +0530 | [diff] [blame] | 204 | fi |
| 205 | exit $ret |
Johannes Schindelin | 1b1dce4 | 2007-06-25 01:11:14 +0100 | [diff] [blame] | 206 | } |
| 207 | |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 208 | run_pre_rebase_hook () { |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 209 | if test -z "$ok_to_skip_pre_rebase" && |
Nguyễn Thái Ngọc Duy | b849b95 | 2014-11-30 15:24:38 +0700 | [diff] [blame] | 210 | test -x "$(git rev-parse --git-path hooks/pre-rebase)" |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 211 | then |
Nguyễn Thái Ngọc Duy | b849b95 | 2014-11-30 15:24:38 +0700 | [diff] [blame] | 212 | "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 213 | die "$(gettext "The pre-rebase hook refused to rebase.")" |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 214 | fi |
| 215 | } |
| 216 | |
Martin von Zweigbergk | 69a636a | 2011-02-06 13:43:30 -0500 | [diff] [blame] | 217 | test -f "$apply_dir"/applying && |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 218 | die "$(gettext "It looks like git-am is in progress. Cannot rebase.")" |
Stephan Beyer | 9b752a6 | 2008-08-17 06:25:43 +0200 | [diff] [blame] | 219 | |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 220 | if test -d "$apply_dir" |
| 221 | then |
| 222 | type=am |
| 223 | state_dir="$apply_dir" |
| 224 | elif test -d "$merge_dir" |
| 225 | then |
| 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" |
| 234 | fi |
| 235 | test -n "$type" && in_progress=t |
| 236 | |
Martin von Zweigbergk | 95135b0 | 2011-02-06 13:43:36 -0500 | [diff] [blame] | 237 | total_argc=$# |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 238 | while test $# != 0 |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 239 | do |
| 240 | case "$1" in |
Nanako Shiraishi | c442765 | 2008-10-06 14:14:29 +0900 | [diff] [blame] | 241 | --no-verify) |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 242 | ok_to_skip_pre_rebase=yes |
Nanako Shiraishi | c442765 | 2008-10-06 14:14:29 +0900 | [diff] [blame] | 243 | ;; |
Martin von Zweigbergk | 7baf9c4 | 2010-11-22 21:21:01 +0100 | [diff] [blame] | 244 | --verify) |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 245 | ok_to_skip_pre_rebase= |
Martin von Zweigbergk | 7baf9c4 | 2010-11-22 21:21:01 +0100 | [diff] [blame] | 246 | ;; |
Nguyễn Thái Ngọc Duy | 9512177 | 2016-11-12 09:00:41 +0700 | [diff] [blame] | 247 | --continue|--skip|--abort|--quit|--edit-todo) |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 248 | test $total_argc -eq 2 || usage |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 249 | action=${1##--} |
sean | 031321c | 2006-04-26 10:49:38 -0400 | [diff] [blame] | 250 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 251 | --onto=*) |
| 252 | onto="${1#--onto=}" |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 253 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 254 | --exec=*) |
| 255 | cmd="${cmd}exec ${1#--exec=}${LF}" |
Stefan Beller | 78ec240 | 2016-03-18 14:26:17 -0700 | [diff] [blame] | 256 | test -z "$interactive_rebase" && interactive_rebase=implied |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 257 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 258 | --interactive) |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 259 | interactive_rebase=explicit |
| 260 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 261 | --keep-empty) |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 262 | keep_empty=yes |
| 263 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 264 | --preserve-merges) |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 265 | 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 Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 274 | --fork-point) |
| 275 | fork_point=t |
| 276 | ;; |
| 277 | --no-fork-point) |
| 278 | fork_point= |
| 279 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 280 | --merge) |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 281 | do_merge=t |
| 282 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 283 | --strategy-option=*) |
| 284 | strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")" |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 285 | do_merge=t |
Martin von Zweigbergk | 9765b6a | 2011-02-06 13:43:38 -0500 | [diff] [blame] | 286 | test -z "$strategy" && strategy=recursive |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 287 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 288 | --strategy=*) |
| 289 | strategy="${1#--strategy=}" |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 290 | do_merge=t |
| 291 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 292 | --no-stat) |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 293 | diffstat= |
| 294 | ;; |
| 295 | --stat) |
| 296 | diffstat=t |
| 297 | ;; |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 298 | --autostash) |
| 299 | autostash=true |
| 300 | ;; |
John Keeping | 619e360 | 2015-09-10 23:30:51 +0100 | [diff] [blame] | 301 | --no-autostash) |
| 302 | autostash=false |
| 303 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 304 | --verbose) |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 305 | verbose=t |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 306 | diffstat=t |
Stephen Boyd | 0e987a1 | 2009-06-16 15:33:01 -0700 | [diff] [blame] | 307 | GIT_QUIET= |
| 308 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 309 | --quiet) |
Stephen Boyd | 0e987a1 | 2009-06-16 15:33:01 -0700 | [diff] [blame] | 310 | GIT_QUIET=t |
| 311 | git_am_opt="$git_am_opt -q" |
| 312 | verbose= |
| 313 | diffstat= |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 314 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 315 | --whitespace=*) |
| 316 | git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}" |
| 317 | case "${1#--whitespace=}" in |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 318 | fix|strip) |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 319 | force_rebase=t |
| 320 | ;; |
| 321 | esac |
J. Bruce Fields | 059f446 | 2007-09-07 10:20:50 -0400 | [diff] [blame] | 322 | ;; |
Giuseppe Bilotta | 86c91f9 | 2009-08-04 13:16:49 +0200 | [diff] [blame] | 323 | --ignore-whitespace) |
| 324 | git_am_opt="$git_am_opt $1" |
| 325 | ;; |
Giuseppe Bilotta | 9f79524 | 2017-04-18 11:29:05 +0200 | [diff] [blame] | 326 | --committer-date-is-author-date|--ignore-date|--signoff|--no-signoff) |
Michele Ballabio | 570ccad | 2009-03-18 21:53:49 +0100 | [diff] [blame] | 327 | git_am_opt="$git_am_opt $1" |
| 328 | force_rebase=t |
| 329 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 330 | -C*) |
| 331 | git_am_opt="$git_am_opt $1" |
Michael S. Tsirkin | 67dad68 | 2007-02-08 15:57:08 +0200 | [diff] [blame] | 332 | ;; |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 333 | --root) |
| 334 | rebase_root=t |
| 335 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 336 | --force-rebase|--no-ff) |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 337 | force_rebase=t |
| 338 | ;; |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 339 | --rerere-autoupdate|--no-rerere-autoupdate) |
| 340 | allow_rerere_autoupdate="$1" |
| 341 | ;; |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 342 | --gpg-sign) |
| 343 | gpg_sign_opt=-S |
| 344 | ;; |
| 345 | --gpg-sign=*) |
| 346 | gpg_sign_opt="-S${1#--gpg-sign=}" |
| 347 | ;; |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 348 | --) |
| 349 | shift |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 350 | break |
| 351 | ;; |
| 352 | esac |
| 353 | shift |
| 354 | done |
Jay Soffian | 51b2ead | 2009-02-18 08:44:02 -0500 | [diff] [blame] | 355 | test $# -gt 2 && usage |
Junio C Hamano | 2db8aae | 2005-12-14 03:11:37 -0800 | [diff] [blame] | 356 | |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 357 | if test -n "$action" |
| 358 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 359 | test -z "$in_progress" && die "$(gettext "No rebase in progress?")" |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 360 | # 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 Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 366 | fi |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 367 | |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 368 | if test "$action" = "edit-todo" && test "$type" != "interactive" |
| 369 | then |
| 370 | die "$(gettext "The --edit-todo action can only be used during interactive rebase.")" |
| 371 | fi |
| 372 | |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 373 | case "$action" in |
| 374 | continue) |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 375 | # Sanity check |
| 376 | git rev-parse --verify HEAD >/dev/null || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 377 | die "$(gettext "Cannot read HEAD")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 378 | git update-index --ignore-submodules --refresh && |
| 379 | git diff-files --quiet --ignore-submodules || { |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 380 | echo "$(gettext "You must edit all merge conflicts and then |
| 381 | mark them as resolved using git add")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 382 | exit 1 |
| 383 | } |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 384 | read_basic_state |
| 385 | run_specific_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 386 | ;; |
| 387 | skip) |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 388 | output git reset --hard HEAD || exit $? |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 389 | read_basic_state |
| 390 | run_specific_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 391 | ;; |
| 392 | abort) |
| 393 | git rerere clear |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 394 | read_basic_state |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 395 | case "$head_name" in |
| 396 | refs/*) |
Csaba Henk | ea69619 | 2011-05-27 16:13:02 -0400 | [diff] [blame] | 397 | git symbolic-ref -m "rebase: aborting" HEAD $head_name || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 398 | die "$(eval_gettext "Could not move back to \$head_name")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 399 | ;; |
| 400 | esac |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 401 | output git reset --hard $orig_head |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 402 | finish_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 403 | exit |
| 404 | ;; |
Nguyễn Thái Ngọc Duy | 9512177 | 2016-11-12 09:00:41 +0700 | [diff] [blame] | 405 | quit) |
| 406 | exec rm -rf "$state_dir" |
| 407 | ;; |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 408 | edit-todo) |
| 409 | run_specific_rebase |
| 410 | ;; |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 411 | esac |
| 412 | |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 413 | # Make sure no rebase is in progress |
| 414 | if test -n "$in_progress" |
Jonathan Nieder | bffd750 | 2010-05-31 17:51:32 -0500 | [diff] [blame] | 415 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 416 | 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 Thielow | e39beac | 2012-08-01 19:09:09 +0200 | [diff] [blame] | 421 | I wonder if you are in the middle of another rebase. If that is the |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 422 | case, please try |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 423 | $cmd_live_rebase |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 424 | If that is not the case, please |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 425 | $cmd_clear_stale_rebase |
Stephan Beyer | 9b752a6 | 2008-08-17 06:25:43 +0200 | [diff] [blame] | 426 | and run me again. I am stopping in case you still have something |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 427 | valuable there.')" |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 428 | fi |
| 429 | |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 430 | if test -n "$rebase_root" && test -z "$onto" |
| 431 | then |
| 432 | test -z "$interactive_rebase" && interactive_rebase=implied |
| 433 | fi |
| 434 | |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 435 | if test -n "$interactive_rebase" |
| 436 | then |
| 437 | type=interactive |
| 438 | state_dir="$merge_dir" |
| 439 | elif test -n "$do_merge" |
| 440 | then |
| 441 | type=merge |
| 442 | state_dir="$merge_dir" |
| 443 | else |
| 444 | type=am |
| 445 | state_dir="$apply_dir" |
| 446 | fi |
| 447 | |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 448 | if test -z "$rebase_root" |
| 449 | then |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 450 | 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 Almeida | c36d8ee | 2016-06-17 20:20:59 +0000 | [diff] [blame] | 457 | "against" "git rebase $(gettext '<branch>')" |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 458 | fi |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 459 | |
| 460 | test "$fork_point" = auto && fork_point=t |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 461 | ;; |
| 462 | *) upstream_name="$1" |
Brian Gesiak | 4f40740 | 2014-03-19 20:02:15 +0900 | [diff] [blame] | 463 | if test "$upstream_name" = "-" |
| 464 | then |
| 465 | upstream_name="@{-1}" |
| 466 | fi |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 467 | shift |
| 468 | ;; |
| 469 | esac |
Ramkumar Ramachandra | 2e6e276 | 2013-06-14 18:47:52 +0530 | [diff] [blame] | 470 | upstream=$(peel_committish "${upstream_name}") || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 471 | die "$(eval_gettext "invalid upstream \$upstream_name")" |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 472 | upstream_arg="$upstream_name" |
| 473 | else |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 474 | if test -z "$onto" |
| 475 | then |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 476 | empty_tree=$(git hash-object -t tree /dev/null) |
| 477 | onto=$(git commit-tree $empty_tree </dev/null) |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 478 | squash_onto="$onto" |
| 479 | fi |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 480 | unset upstream_name |
| 481 | unset upstream |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 482 | test $# -gt 1 && usage |
Martin von Zweigbergk | 46df82d | 2011-02-06 13:43:48 -0500 | [diff] [blame] | 483 | upstream_arg=--root |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 484 | fi |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 485 | |
Junio C Hamano | a1bf91e | 2007-03-22 02:54:59 -0700 | [diff] [blame] | 486 | # Make sure the branch to rebase onto is valid. |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 487 | onto_name=${onto-"$upstream_name"} |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 488 | case "$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 Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 495 | die "$(eval_gettext "\$onto_name: there are more than one merge bases")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 496 | ;; |
| 497 | '') |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 498 | die "$(eval_gettext "\$onto_name: there is no merge base")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 499 | ;; |
| 500 | esac |
| 501 | else |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 502 | die "$(eval_gettext "\$onto_name: there is no merge base")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 503 | fi |
| 504 | ;; |
| 505 | *) |
Ramkumar Ramachandra | 2e6e276 | 2013-06-14 18:47:52 +0530 | [diff] [blame] | 506 | onto=$(peel_committish "$onto_name") || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 507 | die "$(eval_gettext "Does not point to a valid commit: \$onto_name")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 508 | ;; |
| 509 | esac |
Junio C Hamano | a1bf91e | 2007-03-22 02:54:59 -0700 | [diff] [blame] | 510 | |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 511 | # 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" |
| 515 | switch_to= |
Junio C Hamano | 7f59dbb | 2005-11-14 00:41:53 -0800 | [diff] [blame] | 516 | case "$#" in |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 517 | 1) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 518 | # Is it "rebase other $branchname" or "rebase other $commit"? |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 519 | branch_name="$1" |
| 520 | switch_to="$1" |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 521 | |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 522 | if git show-ref --verify --quiet -- "refs/heads/$1" && |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 523 | orig_head=$(git rev-parse -q --verify "refs/heads/$1") |
Junio C Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 524 | then |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 525 | head_name="refs/heads/$1" |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 526 | elif orig_head=$(git rev-parse -q --verify "$1") |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 527 | then |
| 528 | head_name="detached HEAD" |
Junio C Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 529 | else |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 530 | die "$(eval_gettext "fatal: no such branch: \$branch_name")" |
Junio C Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 531 | fi |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 532 | ;; |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 533 | 0) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 534 | # Do not need to switch branches, we are already on it. |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 535 | if branch_name=$(git symbolic-ref -q HEAD) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 536 | then |
| 537 | head_name=$branch_name |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 538 | branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)') |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 539 | else |
| 540 | head_name="detached HEAD" |
| 541 | branch_name=HEAD ;# detached |
| 542 | fi |
Phil Hord | ea70980 | 2013-04-23 18:51:14 -0400 | [diff] [blame] | 543 | orig_head=$(git rev-parse --verify HEAD) || exit |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 544 | ;; |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 545 | *) |
| 546 | die "BUG: unexpected number of arguments left to parse" |
| 547 | ;; |
Junio C Hamano | 7f59dbb | 2005-11-14 00:41:53 -0800 | [diff] [blame] | 548 | esac |
Junio C Hamano | 99a92f9 | 2005-08-17 15:19:57 -0700 | [diff] [blame] | 549 | |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 550 | if test "$fork_point" = t |
| 551 | then |
John Keeping | bb3f458 | 2014-01-09 19:47:34 +0000 | [diff] [blame] | 552 | new_upstream=$(git merge-base --fork-point "$upstream_name" \ |
| 553 | "${switch_to:-HEAD}") |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 554 | if test -n "$new_upstream" |
| 555 | then |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 556 | restrict_revision=$new_upstream |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 557 | fi |
| 558 | fi |
| 559 | |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 560 | if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null |
| 561 | then |
| 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 |
| 570 | fi |
| 571 | |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 572 | require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")" |
Martin von Zweigbergk | 8f9bfb6 | 2011-02-06 13:43:41 -0500 | [diff] [blame] | 573 | |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 574 | # Now we are rebasing commits $upstream..$orig_head (or with --root, |
| 575 | # everything leading up to $orig_head) on top of $onto |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 576 | |
Johannes Sixt | 1308c17 | 2007-07-04 22:09:10 +0200 | [diff] [blame] | 577 | # Check if we are already based on $onto with linear history, |
Martin von Zweigbergk | cc1453e | 2011-02-06 13:43:44 -0500 | [diff] [blame] | 578 | # but this should be done only when upstream and onto are the same |
| 579 | # and if this is not an interactive rebase. |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 580 | mb=$(git merge-base "$onto" "$orig_head") |
Martin von Zweigbergk | cc1453e | 2011-02-06 13:43:44 -0500 | [diff] [blame] | 581 | if test "$type" != interactive && test "$upstream" = "$onto" && |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 582 | test "$mb" = "$onto" && test -z "$restrict_revision" && |
Johannes Sixt | 1308c17 | 2007-07-04 22:09:10 +0200 | [diff] [blame] | 583 | # linear history? |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 584 | ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 585 | then |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 586 | if test -z "$force_rebase" |
| 587 | then |
| 588 | # Lazily switch to the target branch if needed... |
Ramkumar Ramachandra | 4b03df2 | 2013-06-16 14:15:12 +0530 | [diff] [blame] | 589 | test -z "$switch_to" || |
| 590 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \ |
Jeff King | 22946a9 | 2015-04-28 01:17:37 -0400 | [diff] [blame] | 591 | git checkout -q "$switch_to" -- |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 592 | say "$(eval_gettext "Current branch \$branch_name is up to date.")" |
Ramkumar Ramachandra | 96e2b99 | 2013-06-13 21:36:13 +0530 | [diff] [blame] | 593 | finish_rebase |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 594 | exit 0 |
| 595 | else |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 596 | say "$(eval_gettext "Current branch \$branch_name is up to date, rebase forced.")" |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 597 | fi |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 598 | fi |
| 599 | |
Martin von Zweigbergk | 8f9bfb6 | 2011-02-06 13:43:41 -0500 | [diff] [blame] | 600 | # If a hook exists, give it a chance to interrupt |
| 601 | run_pre_rebase_hook "$upstream_arg" "$@" |
| 602 | |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 603 | if test -n "$diffstat" |
| 604 | then |
| 605 | if test -n "$verbose" |
| 606 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 607 | echo "$(eval_gettext "Changes from \$mb to \$onto:")" |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 608 | fi |
| 609 | # We want color (if set), but no pager |
| 610 | GIT_PAGER='' git diff --stat --summary "$mb" "$onto" |
| 611 | fi |
| 612 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 613 | test "$type" = interactive && run_specific_rebase |
Martin von Zweigbergk | f4107d9 | 2011-02-06 13:43:45 -0500 | [diff] [blame] | 614 | |
| 615 | # Detach HEAD and reset the tree |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 616 | say "$(gettext "First, rewinding head to replay your work on top of it...")" |
Ramkumar Ramachandra | 4b03df2 | 2013-06-16 14:15:12 +0530 | [diff] [blame] | 617 | |
| 618 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \ |
| 619 | git checkout -q "$onto^0" || die "could not detach HEAD" |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 620 | git update-ref ORIG_HEAD $orig_head |
Martin von Zweigbergk | f4107d9 | 2011-02-06 13:43:45 -0500 | [diff] [blame] | 621 | |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 622 | # If the $onto is a proper descendant of the tip of the branch, then |
Felipe Contreras | a75d7b5 | 2009-10-24 11:31:32 +0300 | [diff] [blame] | 623 | # we just fast-forwarded. |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 624 | if test "$mb" = "$orig_head" |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 625 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 626 | say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")" |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 627 | move_to_original_branch |
Ramkumar Ramachandra | af2f0eb | 2013-06-13 21:36:12 +0530 | [diff] [blame] | 628 | finish_rebase |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 629 | exit 0 |
| 630 | fi |
| 631 | |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 632 | if test -n "$rebase_root" |
| 633 | then |
| 634 | revisions="$onto..$orig_head" |
| 635 | else |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 636 | revisions="${restrict_revision-$upstream}..$orig_head" |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 637 | fi |
| 638 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 639 | run_specific_rebase |