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>] |
Kaartic Sivaraam | 82cb775 | 2017-11-20 20:56:52 +0530 | [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 |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 20 | r,rebase-merges? try to rebase merges instead of skipping them |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 21 | p,preserve-merges! try to recreate merges instead of ignoring them |
| 22 | s,strategy=! use the given merge strategy |
| 23 | no-ff! cherry-pick all commits, even if unchanged |
| 24 | m,merge! use merging strategies to rebase |
| 25 | i,interactive! let the user edit the list of commits to rebase |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 26 | x,exec=! add exec lines after each commit of the editable list |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 27 | k,keep-empty preserve empty commits during rebase |
Genki Sky | a6c612b | 2018-02-04 15:08:13 -0500 | [diff] [blame] | 28 | allow-empty-message allow rebasing commits with empty messages |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 29 | f,force-rebase! force rebase even if branch is up to date |
| 30 | X,strategy-option=! pass the argument through to the merge strategy |
| 31 | stat! display a diffstat of what changed upstream |
| 32 | n,no-stat! do not show diffstat of what changed upstream |
| 33 | verify allow pre-rebase hook to run |
| 34 | rerere-autoupdate allow rerere to update index with resolved conflicts |
| 35 | root! rebase all reachable commits up to the root(s) |
| 36 | autosquash move commits that begin with squash!/fixup! under -i |
| 37 | committer-date-is-author-date! passed to 'git am' |
| 38 | ignore-date! passed to 'git am' |
Giuseppe Bilotta | 9f79524 | 2017-04-18 11:29:05 +0200 | [diff] [blame] | 39 | signoff passed to 'git am' |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 40 | whitespace=! passed to 'git apply' |
| 41 | ignore-whitespace! passed to 'git apply' |
| 42 | C=! passed to 'git apply' |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 43 | S,gpg-sign? GPG-sign commits |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 44 | Actions: |
Martin von Zweigbergk | 5960bc9 | 2011-07-13 23:47:06 -0400 | [diff] [blame] | 45 | continue! continue |
| 46 | abort! abort and check out the original branch |
| 47 | skip! skip current patch and continue |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 48 | 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] | 49 | quit! abort but keep HEAD where it is |
Nguyễn Thái Ngọc Duy | 6633529 | 2018-02-11 16:43:27 +0700 | [diff] [blame] | 50 | show-current-patch! show the patch file being applied or merged |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 51 | " |
Junio C Hamano | ae2b0f1 | 2005-11-24 00:12:11 -0800 | [diff] [blame] | 52 | . git-sh-setup |
Shawn O. Pearce | f947413 | 2006-12-28 02:34:48 -0500 | [diff] [blame] | 53 | set_reflog_action rebase |
Jeff King | 035b5bf | 2011-10-13 11:59:24 -0400 | [diff] [blame] | 54 | require_work_tree_exists |
Junio C Hamano | 533b703 | 2007-01-12 12:52:03 -0800 | [diff] [blame] | 55 | cd_to_toplevel |
Junio C Hamano | 4282c4f | 2005-08-07 15:51:09 -0700 | [diff] [blame] | 56 | |
Junio C Hamano | 61dfa1b | 2009-11-20 03:02:44 -0800 | [diff] [blame] | 57 | LF=' |
| 58 | ' |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 59 | ok_to_skip_pre_rebase= |
| 60 | resolvemsg=" |
William Duclot | 5fdacc1 | 2017-07-16 21:42:20 +0200 | [diff] [blame] | 61 | $(gettext 'Resolve all conflicts manually, mark them as resolved with |
| 62 | "git add/rm <conflicted_files>", then run "git rebase --continue". |
| 63 | You can instead skip this commit: run "git rebase --skip". |
| 64 | 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] | 65 | " |
Phillip Wood | bb2ac4f | 2018-03-20 10:03:13 +0000 | [diff] [blame] | 66 | squash_onto= |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 67 | unset onto |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 68 | unset restrict_revision |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 69 | cmd= |
Martin von Zweigbergk | 9765b6a | 2011-02-06 13:43:38 -0500 | [diff] [blame] | 70 | strategy= |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 71 | strategy_opts= |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 72 | do_merge= |
Martin von Zweigbergk | 69a636a | 2011-02-06 13:43:30 -0500 | [diff] [blame] | 73 | merge_dir="$GIT_DIR"/rebase-merge |
| 74 | apply_dir="$GIT_DIR"/rebase-apply |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 75 | verbose= |
Martin von Zweigbergk | 9474a02 | 2010-11-09 21:59:00 +0100 | [diff] [blame] | 76 | diffstat= |
| 77 | test "$(git config --bool rebase.stat)" = true && diffstat=t |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 78 | autostash="$(git config --bool rebase.autostash || echo false)" |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 79 | fork_point=auto |
Michael S. Tsirkin | 67dad68 | 2007-02-08 15:57:08 +0200 | [diff] [blame] | 80 | git_am_opt= |
Kevin Willford | 9eaa858 | 2017-08-10 14:32:56 -0400 | [diff] [blame] | 81 | git_format_patch_opt= |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 82 | rebase_root= |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 83 | force_rebase= |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 84 | allow_rerere_autoupdate= |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 85 | # Non-empty if a rebase was in progress when 'git rebase' was invoked |
| 86 | in_progress= |
| 87 | # One of {am, merge, interactive} |
| 88 | type= |
| 89 | # One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge} |
| 90 | state_dir= |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 91 | # One of {'', continue, skip, abort}, as parsed from command line |
| 92 | action= |
Johannes Schindelin | 8f6aed7 | 2018-04-25 14:29:04 +0200 | [diff] [blame] | 93 | rebase_merges= |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 94 | rebase_cousins= |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 95 | preserve_merges= |
| 96 | autosquash= |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 97 | keep_empty= |
Genki Sky | a6c612b | 2018-02-04 15:08:13 -0500 | [diff] [blame] | 98 | allow_empty_message= |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 99 | signoff= |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 100 | test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t |
Junio C Hamano | 6694856 | 2016-05-02 14:58:45 -0700 | [diff] [blame] | 101 | case "$(git config --bool commit.gpgsign)" in |
| 102 | true) gpg_sign_opt=-S ;; |
| 103 | *) gpg_sign_opt= ;; |
| 104 | esac |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 105 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 106 | read_basic_state () { |
Ramkumar Ramachandra | dc8ca91 | 2013-06-13 21:36:11 +0530 | [diff] [blame] | 107 | test -f "$state_dir/head-name" && |
| 108 | test -f "$state_dir/onto" && |
Martin von Zweigbergk | 02ac45f | 2011-02-06 13:43:31 -0500 | [diff] [blame] | 109 | head_name=$(cat "$state_dir"/head-name) && |
| 110 | onto=$(cat "$state_dir"/onto) && |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 111 | # 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 Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 115 | then |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 116 | orig_head=$(cat "$state_dir"/orig-head) |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 117 | else |
| 118 | orig_head=$(cat "$state_dir"/head) |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 119 | fi && |
Martin von Zweigbergk | 7b37a7c | 2011-02-06 13:43:54 -0500 | [diff] [blame] | 120 | GIT_QUIET=$(cat "$state_dir"/quiet) && |
| 121 | test -f "$state_dir"/verbose && verbose=t |
Martin von Zweigbergk | 80ff479 | 2011-02-06 13:43:55 -0500 | [diff] [blame] | 122 | 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 Zweigbergk | b3e4847 | 2011-02-06 13:43:56 -0500 | [diff] [blame] | 125 | test -f "$state_dir"/allow_rerere_autoupdate && |
| 126 | allow_rerere_autoupdate="$(cat "$state_dir"/allow_rerere_autoupdate)" |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 127 | test -f "$state_dir"/gpg_sign_opt && |
| 128 | gpg_sign_opt="$(cat "$state_dir"/gpg_sign_opt)" |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 129 | test -f "$state_dir"/signoff && { |
| 130 | signoff="$(cat "$state_dir"/signoff)" |
| 131 | force_rebase=t |
| 132 | } |
Martin von Zweigbergk | 02ac45f | 2011-02-06 13:43:31 -0500 | [diff] [blame] | 133 | } |
| 134 | |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 135 | write_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 Zweigbergk | 7b37a7c | 2011-02-06 13:43:54 -0500 | [diff] [blame] | 139 | echo "$GIT_QUIET" > "$state_dir"/quiet && |
| 140 | test t = "$verbose" && : > "$state_dir"/verbose |
Martin von Zweigbergk | 80ff479 | 2011-02-06 13:43:55 -0500 | [diff] [blame] | 141 | test -n "$strategy" && echo "$strategy" > "$state_dir"/strategy |
| 142 | test -n "$strategy_opts" && echo "$strategy_opts" > \ |
| 143 | "$state_dir"/strategy_opts |
Martin von Zweigbergk | b3e4847 | 2011-02-06 13:43:56 -0500 | [diff] [blame] | 144 | test -n "$allow_rerere_autoupdate" && echo "$allow_rerere_autoupdate" > \ |
| 145 | "$state_dir"/allow_rerere_autoupdate |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 146 | test -n "$gpg_sign_opt" && echo "$gpg_sign_opt" > "$state_dir"/gpg_sign_opt |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 147 | test -n "$signoff" && echo "$signoff" >"$state_dir"/signoff |
Martin von Zweigbergk | 84df456 | 2011-02-06 13:43:53 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 150 | output () { |
| 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 Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 164 | move_to_original_branch () { |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 165 | 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 King | 53f2ffa | 2011-05-27 16:16:14 -0400 | [diff] [blame] | 170 | git symbolic-ref \ |
| 171 | -m "rebase finished: returning to $head_name" \ |
| 172 | HEAD $head_name || |
Vasco Almeida | 24a6df4 | 2016-06-17 20:20:58 +0000 | [diff] [blame] | 173 | die "$(eval_gettext "Could not move back to \$head_name")" |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 174 | ;; |
| 175 | esac |
| 176 | } |
| 177 | |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 178 | apply_autostash () { |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 179 | if test -f "$state_dir/autostash" |
| 180 | then |
| 181 | stash_sha1=$(cat "$state_dir/autostash") |
Todd Zullinger | eadf1c8 | 2017-11-13 16:20:09 -0500 | [diff] [blame] | 182 | if git stash apply $stash_sha1 >/dev/null 2>&1 |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 183 | then |
Johannes Schindelin | cdb866b | 2017-06-19 18:56:02 +0100 | [diff] [blame] | 184 | echo "$(gettext 'Applied autostash.')" >&2 |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 185 | else |
Ramkumar Ramachandra | 20351bb | 2013-06-15 18:43:26 +0530 | [diff] [blame] | 186 | git stash store -m "autostash" -q $stash_sha1 || |
| 187 | die "$(eval_gettext "Cannot store \$stash_sha1")" |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 188 | gettext 'Applying autostash resulted in conflicts. |
| 189 | Your changes are safe in the stash. |
Ralf Thielow | ac1998d | 2013-07-29 06:24:43 +0200 | [diff] [blame] | 190 | 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] | 191 | ' >&2 |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 192 | fi |
| 193 | fi |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | finish_rebase () { |
Nguyễn Thái Ngọc Duy | fbd7a23 | 2018-02-11 16:43:28 +0700 | [diff] [blame] | 197 | rm -f "$(git rev-parse --git-path REBASE_HEAD)" |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 198 | apply_autostash && |
Jeff King | 8c24f5b | 2016-01-13 13:47:18 -0500 | [diff] [blame] | 199 | { git gc --auto || true; } && |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 200 | rm -rf "$state_dir" |
| 201 | } |
| 202 | |
Kyle J. McKay | 8cd6596 | 2014-04-11 01:28:18 -0700 | [diff] [blame] | 203 | run_specific_rebase () { |
Andreas Ericsson | f8cca01 | 2008-09-29 22:28:57 +0200 | [diff] [blame] | 204 | if [ "$interactive_rebase" = implied ]; then |
| 205 | GIT_EDITOR=: |
| 206 | export GIT_EDITOR |
Vincent van Ravesteijn | 8a6dae1 | 2012-05-24 13:57:26 +0000 | [diff] [blame] | 207 | autosquash= |
Andreas Ericsson | f8cca01 | 2008-09-29 22:28:57 +0200 | [diff] [blame] | 208 | fi |
Martin von Zweigbergk | 46df82d | 2011-02-06 13:43:48 -0500 | [diff] [blame] | 209 | . git-rebase--$type |
Wink Saville | 950b487 | 2018-03-23 14:25:26 -0700 | [diff] [blame] | 210 | git_rebase__$type${preserve_merges:+__preserve_merges} |
Ramkumar Ramachandra | f5f758a | 2013-05-12 17:26:37 +0530 | [diff] [blame] | 211 | ret=$? |
| 212 | if test $ret -eq 0 |
| 213 | then |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 214 | finish_rebase |
Ramkumar Ramachandra | e4244eb | 2014-05-19 18:05:20 -0400 | [diff] [blame] | 215 | 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 Ramachandra | f5f758a | 2013-05-12 17:26:37 +0530 | [diff] [blame] | 220 | fi |
| 221 | exit $ret |
Johannes Schindelin | 1b1dce4 | 2007-06-25 01:11:14 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 224 | run_pre_rebase_hook () { |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 225 | 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] | 226 | test -x "$(git rev-parse --git-path hooks/pre-rebase)" |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 227 | then |
Nguyễn Thái Ngọc Duy | b849b95 | 2014-11-30 15:24:38 +0700 | [diff] [blame] | 228 | "$(git rev-parse --git-path hooks/pre-rebase)" ${1+"$@"} || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 229 | die "$(gettext "The pre-rebase hook refused to rebase.")" |
Nanako Shiraishi | d70b4a8 | 2008-10-06 14:14:24 +0900 | [diff] [blame] | 230 | fi |
| 231 | } |
| 232 | |
Martin von Zweigbergk | 69a636a | 2011-02-06 13:43:30 -0500 | [diff] [blame] | 233 | test -f "$apply_dir"/applying && |
Kaartic Sivaraam | 82cb775 | 2017-11-20 20:56:52 +0530 | [diff] [blame] | 234 | die "$(gettext "It looks like 'git am' is in progress. Cannot rebase.")" |
Stephan Beyer | 9b752a6 | 2008-08-17 06:25:43 +0200 | [diff] [blame] | 235 | |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 236 | if test -d "$apply_dir" |
| 237 | then |
| 238 | type=am |
| 239 | state_dir="$apply_dir" |
| 240 | elif test -d "$merge_dir" |
| 241 | then |
| 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" |
| 250 | fi |
| 251 | test -n "$type" && in_progress=t |
| 252 | |
Martin von Zweigbergk | 95135b0 | 2011-02-06 13:43:36 -0500 | [diff] [blame] | 253 | total_argc=$# |
David Kastrup | 822f7c7 | 2007-09-23 22:42:08 +0200 | [diff] [blame] | 254 | while test $# != 0 |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 255 | do |
| 256 | case "$1" in |
Nanako Shiraishi | c442765 | 2008-10-06 14:14:29 +0900 | [diff] [blame] | 257 | --no-verify) |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 258 | ok_to_skip_pre_rebase=yes |
Nanako Shiraishi | c442765 | 2008-10-06 14:14:29 +0900 | [diff] [blame] | 259 | ;; |
Martin von Zweigbergk | 7baf9c4 | 2010-11-22 21:21:01 +0100 | [diff] [blame] | 260 | --verify) |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 261 | ok_to_skip_pre_rebase= |
Martin von Zweigbergk | 7baf9c4 | 2010-11-22 21:21:01 +0100 | [diff] [blame] | 262 | ;; |
Nguyễn Thái Ngọc Duy | 6633529 | 2018-02-11 16:43:27 +0700 | [diff] [blame] | 263 | --continue|--skip|--abort|--quit|--edit-todo|--show-current-patch) |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 264 | test $total_argc -eq 2 || usage |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 265 | action=${1##--} |
sean | 031321c | 2006-04-26 10:49:38 -0400 | [diff] [blame] | 266 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 267 | --onto=*) |
| 268 | onto="${1#--onto=}" |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 269 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 270 | --exec=*) |
| 271 | cmd="${cmd}exec ${1#--exec=}${LF}" |
Stefan Beller | 78ec240 | 2016-03-18 14:26:17 -0700 | [diff] [blame] | 272 | test -z "$interactive_rebase" && interactive_rebase=implied |
Lucien Kong | c214538 | 2012-06-12 10:05:12 +0200 | [diff] [blame] | 273 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 274 | --interactive) |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 275 | interactive_rebase=explicit |
| 276 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 277 | --keep-empty) |
Neil Horman | 90e1818 | 2012-04-20 10:36:17 -0400 | [diff] [blame] | 278 | keep_empty=yes |
| 279 | ;; |
Genki Sky | a6c612b | 2018-02-04 15:08:13 -0500 | [diff] [blame] | 280 | --allow-empty-message) |
| 281 | allow_empty_message=--allow-empty-message |
| 282 | ;; |
Phillip Wood | 3d94616 | 2018-03-20 10:03:15 +0000 | [diff] [blame] | 283 | --no-keep-empty) |
| 284 | keep_empty= |
| 285 | ;; |
Johannes Schindelin | 8f6aed7 | 2018-04-25 14:29:04 +0200 | [diff] [blame] | 286 | --rebase-merges) |
| 287 | rebase_merges=t |
| 288 | test -z "$interactive_rebase" && interactive_rebase=implied |
| 289 | ;; |
Johannes Schindelin | 7543f6f | 2018-04-25 14:29:40 +0200 | [diff] [blame] | 290 | --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 Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 299 | --preserve-merges) |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 300 | 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 Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 309 | --fork-point) |
| 310 | fork_point=t |
| 311 | ;; |
| 312 | --no-fork-point) |
| 313 | fork_point= |
| 314 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 315 | --merge) |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 316 | do_merge=t |
| 317 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 318 | --strategy-option=*) |
| 319 | strategy_opts="$strategy_opts $(git rev-parse --sq-quote "--${1#--strategy-option=}")" |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 320 | do_merge=t |
Martin von Zweigbergk | 9765b6a | 2011-02-06 13:43:38 -0500 | [diff] [blame] | 321 | test -z "$strategy" && strategy=recursive |
Mike Lundy | 93ce190 | 2010-07-29 00:04:29 +0200 | [diff] [blame] | 322 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 323 | --strategy=*) |
| 324 | strategy="${1#--strategy=}" |
Eric Wong | 58634db | 2006-06-21 03:04:41 -0700 | [diff] [blame] | 325 | do_merge=t |
| 326 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 327 | --no-stat) |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 328 | diffstat= |
| 329 | ;; |
| 330 | --stat) |
| 331 | diffstat=t |
| 332 | ;; |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 333 | --autostash) |
| 334 | autostash=true |
| 335 | ;; |
John Keeping | 619e360 | 2015-09-10 23:30:51 +0100 | [diff] [blame] | 336 | --no-autostash) |
| 337 | autostash=false |
| 338 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 339 | --verbose) |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 340 | verbose=t |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 341 | diffstat=t |
Stephen Boyd | 0e987a1 | 2009-06-16 15:33:01 -0700 | [diff] [blame] | 342 | GIT_QUIET= |
| 343 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 344 | --quiet) |
Stephen Boyd | 0e987a1 | 2009-06-16 15:33:01 -0700 | [diff] [blame] | 345 | GIT_QUIET=t |
| 346 | git_am_opt="$git_am_opt -q" |
| 347 | verbose= |
| 348 | diffstat= |
Robert Shearman | b758789 | 2006-10-03 17:29:31 +0100 | [diff] [blame] | 349 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 350 | --whitespace=*) |
| 351 | git_am_opt="$git_am_opt --whitespace=${1#--whitespace=}" |
| 352 | case "${1#--whitespace=}" in |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 353 | fix|strip) |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 354 | force_rebase=t |
| 355 | ;; |
| 356 | esac |
J. Bruce Fields | 059f446 | 2007-09-07 10:20:50 -0400 | [diff] [blame] | 357 | ;; |
Giuseppe Bilotta | 86c91f9 | 2009-08-04 13:16:49 +0200 | [diff] [blame] | 358 | --ignore-whitespace) |
| 359 | git_am_opt="$git_am_opt $1" |
| 360 | ;; |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 361 | --signoff) |
| 362 | signoff=--signoff |
| 363 | ;; |
| 364 | --no-signoff) |
| 365 | signoff= |
| 366 | ;; |
| 367 | --committer-date-is-author-date|--ignore-date) |
Michele Ballabio | 570ccad | 2009-03-18 21:53:49 +0100 | [diff] [blame] | 368 | git_am_opt="$git_am_opt $1" |
| 369 | force_rebase=t |
| 370 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 371 | -C*) |
| 372 | git_am_opt="$git_am_opt $1" |
Michael S. Tsirkin | 67dad68 | 2007-02-08 15:57:08 +0200 | [diff] [blame] | 373 | ;; |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 374 | --root) |
| 375 | rebase_root=t |
| 376 | ;; |
Nicolas Vigier | b6e9e73 | 2014-02-10 01:03:36 +0000 | [diff] [blame] | 377 | --force-rebase|--no-ff) |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 378 | force_rebase=t |
| 379 | ;; |
Junio C Hamano | cb6020b | 2009-12-04 00:20:48 -0800 | [diff] [blame] | 380 | --rerere-autoupdate|--no-rerere-autoupdate) |
| 381 | allow_rerere_autoupdate="$1" |
| 382 | ;; |
Nicolas Vigier | 3ee5e54 | 2014-02-10 01:03:37 +0000 | [diff] [blame] | 383 | --gpg-sign) |
| 384 | gpg_sign_opt=-S |
| 385 | ;; |
| 386 | --gpg-sign=*) |
| 387 | gpg_sign_opt="-S${1#--gpg-sign=}" |
| 388 | ;; |
Martin von Zweigbergk | 45e2acf | 2011-02-28 20:59:26 -0500 | [diff] [blame] | 389 | --) |
| 390 | shift |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 391 | break |
| 392 | ;; |
Brandon Casey | 697bc88 | 2017-09-17 15:28:17 -0700 | [diff] [blame] | 393 | *) |
| 394 | usage |
| 395 | ;; |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 396 | esac |
| 397 | shift |
| 398 | done |
Jay Soffian | 51b2ead | 2009-02-18 08:44:02 -0500 | [diff] [blame] | 399 | test $# -gt 2 && usage |
Junio C Hamano | 2db8aae | 2005-12-14 03:11:37 -0800 | [diff] [blame] | 400 | |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 401 | if test -n "$action" |
| 402 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 403 | test -z "$in_progress" && die "$(gettext "No rebase in progress?")" |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 404 | # 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 Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 410 | fi |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 411 | |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 412 | if test "$action" = "edit-todo" && test "$type" != "interactive" |
| 413 | then |
| 414 | die "$(gettext "The --edit-todo action can only be used during interactive rebase.")" |
| 415 | fi |
| 416 | |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 417 | case "$action" in |
| 418 | continue) |
Martin von Zweigbergk | 2959c28 | 2011-02-06 13:43:52 -0500 | [diff] [blame] | 419 | # Sanity check |
| 420 | git rev-parse --verify HEAD >/dev/null || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 421 | die "$(gettext "Cannot read HEAD")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 422 | git update-index --ignore-submodules --refresh && |
| 423 | git diff-files --quiet --ignore-submodules || { |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 424 | echo "$(gettext "You must edit all merge conflicts and then |
| 425 | mark them as resolved using git add")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 426 | exit 1 |
| 427 | } |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 428 | read_basic_state |
| 429 | run_specific_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 430 | ;; |
| 431 | skip) |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 432 | output git reset --hard HEAD || exit $? |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 433 | read_basic_state |
| 434 | run_specific_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 435 | ;; |
| 436 | abort) |
| 437 | git rerere clear |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 438 | read_basic_state |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 439 | case "$head_name" in |
| 440 | refs/*) |
Csaba Henk | ea69619 | 2011-05-27 16:13:02 -0400 | [diff] [blame] | 441 | git symbolic-ref -m "rebase: aborting" HEAD $head_name || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 442 | die "$(eval_gettext "Could not move back to \$head_name")" |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 443 | ;; |
| 444 | esac |
Martin von Zweigbergk | 4974c2c | 2011-02-06 13:43:51 -0500 | [diff] [blame] | 445 | output git reset --hard $orig_head |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 446 | finish_rebase |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 447 | exit |
| 448 | ;; |
Nguyễn Thái Ngọc Duy | 9512177 | 2016-11-12 09:00:41 +0700 | [diff] [blame] | 449 | quit) |
| 450 | exec rm -rf "$state_dir" |
| 451 | ;; |
Andrew Wong | eb9a7cb | 2012-09-17 21:28:09 -0400 | [diff] [blame] | 452 | edit-todo) |
| 453 | run_specific_rebase |
| 454 | ;; |
Nguyễn Thái Ngọc Duy | 6633529 | 2018-02-11 16:43:27 +0700 | [diff] [blame] | 455 | show-current-patch) |
| 456 | run_specific_rebase |
| 457 | die "BUG: run_specific_rebase is not supposed to return here" |
| 458 | ;; |
Martin von Zweigbergk | 3426232 | 2011-02-06 13:43:35 -0500 | [diff] [blame] | 459 | esac |
| 460 | |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 461 | # Make sure no rebase is in progress |
| 462 | if test -n "$in_progress" |
Jonathan Nieder | bffd750 | 2010-05-31 17:51:32 -0500 | [diff] [blame] | 463 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 464 | 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 Thielow | e39beac | 2012-08-01 19:09:09 +0200 | [diff] [blame] | 469 | 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] | 470 | case, please try |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 471 | $cmd_live_rebase |
Martin von Zweigbergk | 99de064 | 2011-02-06 13:43:34 -0500 | [diff] [blame] | 472 | If that is not the case, please |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 473 | $cmd_clear_stale_rebase |
Stephan Beyer | 9b752a6 | 2008-08-17 06:25:43 +0200 | [diff] [blame] | 474 | 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] | 475 | valuable there.')" |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 476 | fi |
| 477 | |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 478 | if test -n "$rebase_root" && test -z "$onto" |
| 479 | then |
| 480 | test -z "$interactive_rebase" && interactive_rebase=implied |
| 481 | fi |
| 482 | |
Phillip Wood | da27a6f | 2018-03-20 11:10:57 +0000 | [diff] [blame] | 483 | if test -n "$keep_empty" |
| 484 | then |
| 485 | test -z "$interactive_rebase" && interactive_rebase=implied |
| 486 | fi |
| 487 | |
Martin von Zweigbergk | cf432ca | 2011-02-06 13:43:39 -0500 | [diff] [blame] | 488 | if test -n "$interactive_rebase" |
| 489 | then |
| 490 | type=interactive |
| 491 | state_dir="$merge_dir" |
| 492 | elif test -n "$do_merge" |
| 493 | then |
| 494 | type=merge |
| 495 | state_dir="$merge_dir" |
| 496 | else |
| 497 | type=am |
| 498 | state_dir="$apply_dir" |
| 499 | fi |
| 500 | |
Kevin Willford | 9eaa858 | 2017-08-10 14:32:56 -0400 | [diff] [blame] | 501 | if test -t 2 && test -z "$GIT_QUIET" |
| 502 | then |
| 503 | git_format_patch_opt="$git_format_patch_opt --progress" |
| 504 | fi |
| 505 | |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 506 | if test -n "$signoff" |
| 507 | then |
Phillip Wood | b79966a | 2018-03-20 11:10:56 +0000 | [diff] [blame] | 508 | test -n "$preserve_merges" && |
| 509 | die "$(gettext "error: cannot combine '--signoff' with '--preserve-merges'")" |
Phillip Wood | a852ec7 | 2018-03-20 11:10:55 +0000 | [diff] [blame] | 510 | git_am_opt="$git_am_opt $signoff" |
| 511 | force_rebase=t |
| 512 | fi |
| 513 | |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 514 | if test -z "$rebase_root" |
| 515 | then |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 516 | 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 Almeida | c36d8ee | 2016-06-17 20:20:59 +0000 | [diff] [blame] | 523 | "against" "git rebase $(gettext '<branch>')" |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 524 | fi |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 525 | |
| 526 | test "$fork_point" = auto && fork_point=t |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 527 | ;; |
| 528 | *) upstream_name="$1" |
Brian Gesiak | 4f40740 | 2014-03-19 20:02:15 +0900 | [diff] [blame] | 529 | if test "$upstream_name" = "-" |
| 530 | then |
| 531 | upstream_name="@{-1}" |
| 532 | fi |
Martin von Zweigbergk | 15a147e | 2011-02-09 20:54:02 -0500 | [diff] [blame] | 533 | shift |
| 534 | ;; |
| 535 | esac |
Ramkumar Ramachandra | 2e6e276 | 2013-06-14 18:47:52 +0530 | [diff] [blame] | 536 | upstream=$(peel_committish "${upstream_name}") || |
Kaartic Sivaraam | ca7de7b | 2017-12-16 14:33:18 +0530 | [diff] [blame] | 537 | die "$(eval_gettext "invalid upstream '\$upstream_name'")" |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 538 | upstream_arg="$upstream_name" |
| 539 | else |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 540 | if test -z "$onto" |
| 541 | then |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 542 | empty_tree=$(git hash-object -t tree /dev/null) |
| 543 | onto=$(git commit-tree $empty_tree </dev/null) |
Chris Webb | df5df20 | 2012-06-26 22:55:23 +0100 | [diff] [blame] | 544 | squash_onto="$onto" |
| 545 | fi |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 546 | unset upstream_name |
| 547 | unset upstream |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 548 | test $# -gt 1 && usage |
Martin von Zweigbergk | 46df82d | 2011-02-06 13:43:48 -0500 | [diff] [blame] | 549 | upstream_arg=--root |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 550 | fi |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 551 | |
Junio C Hamano | a1bf91e | 2007-03-22 02:54:59 -0700 | [diff] [blame] | 552 | # Make sure the branch to rebase onto is valid. |
Martin von Zweigbergk | 6bb4e48 | 2011-02-06 13:43:37 -0500 | [diff] [blame] | 553 | onto_name=${onto-"$upstream_name"} |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 554 | case "$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 Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 561 | die "$(eval_gettext "\$onto_name: there are more than one merge bases")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 562 | ;; |
| 563 | '') |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 564 | die "$(eval_gettext "\$onto_name: there is no merge base")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 565 | ;; |
| 566 | esac |
| 567 | else |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 568 | die "$(eval_gettext "\$onto_name: there is no merge base")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 569 | fi |
| 570 | ;; |
| 571 | *) |
Ramkumar Ramachandra | 2e6e276 | 2013-06-14 18:47:52 +0530 | [diff] [blame] | 572 | onto=$(peel_committish "$onto_name") || |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 573 | die "$(eval_gettext "Does not point to a valid commit: \$onto_name")" |
Nanako Shiraishi | 9f21e97 | 2010-01-07 20:05:02 +0900 | [diff] [blame] | 574 | ;; |
| 575 | esac |
Junio C Hamano | a1bf91e | 2007-03-22 02:54:59 -0700 | [diff] [blame] | 576 | |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 577 | # If the branch to rebase is given, that is the branch we will rebase |
Kaartic Sivaraam | 3a9156a | 2017-12-16 14:33:17 +0530 | [diff] [blame] | 578 | # $branch_name -- branch/commit being rebased, or HEAD (already detached) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 579 | # $orig_head -- commit object name of tip of the branch before rebasing |
| 580 | # $head_name -- refs/heads/<that-branch> or "detached HEAD" |
| 581 | switch_to= |
Junio C Hamano | 7f59dbb | 2005-11-14 00:41:53 -0800 | [diff] [blame] | 582 | case "$#" in |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 583 | 1) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 584 | # Is it "rebase other $branchname" or "rebase other $commit"? |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 585 | branch_name="$1" |
| 586 | switch_to="$1" |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 587 | |
Kaartic Sivaraam | 3a9156a | 2017-12-16 14:33:17 +0530 | [diff] [blame] | 588 | # 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 Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 591 | then |
Kaartic Sivaraam | 3a9156a | 2017-12-16 14:33:17 +0530 | [diff] [blame] | 592 | 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 Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 595 | then |
| 596 | head_name="detached HEAD" |
Kaartic Sivaraam | 3a9156a | 2017-12-16 14:33:17 +0530 | [diff] [blame] | 597 | |
Junio C Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 598 | else |
Kaartic Sivaraam | ca7de7b | 2017-12-16 14:33:18 +0530 | [diff] [blame] | 599 | die "$(eval_gettext "fatal: no such branch/commit '\$branch_name'")" |
Junio C Hamano | bcf3161 | 2007-01-20 19:11:29 -0800 | [diff] [blame] | 600 | fi |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 601 | ;; |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 602 | 0) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 603 | # Do not need to switch branches, we are already on it. |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 604 | if branch_name=$(git symbolic-ref -q HEAD) |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 605 | then |
| 606 | head_name=$branch_name |
Elia Pinto | 728fc79 | 2014-04-23 06:44:00 -0700 | [diff] [blame] | 607 | branch_name=$(expr "z$branch_name" : 'zrefs/heads/\(.*\)') |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 608 | else |
| 609 | head_name="detached HEAD" |
Kaartic Sivaraam | 3a9156a | 2017-12-16 14:33:17 +0530 | [diff] [blame] | 610 | branch_name=HEAD |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 611 | fi |
Phil Hord | ea70980 | 2013-04-23 18:51:14 -0400 | [diff] [blame] | 612 | orig_head=$(git rev-parse --verify HEAD) || exit |
Junio C Hamano | 0cb0664 | 2008-03-15 13:17:42 -0700 | [diff] [blame] | 613 | ;; |
Martin von Zweigbergk | f2b6a19 | 2012-06-26 07:51:55 -0700 | [diff] [blame] | 614 | *) |
| 615 | die "BUG: unexpected number of arguments left to parse" |
| 616 | ;; |
Junio C Hamano | 7f59dbb | 2005-11-14 00:41:53 -0800 | [diff] [blame] | 617 | esac |
Junio C Hamano | 99a92f9 | 2005-08-17 15:19:57 -0700 | [diff] [blame] | 618 | |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 619 | if test "$fork_point" = t |
| 620 | then |
John Keeping | bb3f458 | 2014-01-09 19:47:34 +0000 | [diff] [blame] | 621 | new_upstream=$(git merge-base --fork-point "$upstream_name" \ |
| 622 | "${switch_to:-HEAD}") |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 623 | if test -n "$new_upstream" |
| 624 | then |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 625 | restrict_revision=$new_upstream |
John Keeping | ad8261d | 2013-12-09 23:16:16 +0000 | [diff] [blame] | 626 | fi |
| 627 | fi |
| 628 | |
Ramkumar Ramachandra | 5879477 | 2013-05-12 17:26:41 +0530 | [diff] [blame] | 629 | if test "$autostash" = true && ! (require_clean_work_tree) 2>/dev/null |
| 630 | then |
| 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 |
| 639 | fi |
| 640 | |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 641 | require_clean_work_tree "rebase" "$(gettext "Please commit or stash them.")" |
Martin von Zweigbergk | 8f9bfb6 | 2011-02-06 13:43:41 -0500 | [diff] [blame] | 642 | |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 643 | # Now we are rebasing commits $upstream..$orig_head (or with --root, |
| 644 | # everything leading up to $orig_head) on top of $onto |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 645 | |
Johannes Sixt | 1308c17 | 2007-07-04 22:09:10 +0200 | [diff] [blame] | 646 | # Check if we are already based on $onto with linear history, |
Martin von Zweigbergk | cc1453e | 2011-02-06 13:43:44 -0500 | [diff] [blame] | 647 | # but this should be done only when upstream and onto are the same |
| 648 | # and if this is not an interactive rebase. |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 649 | mb=$(git merge-base "$onto" "$orig_head") |
Martin von Zweigbergk | cc1453e | 2011-02-06 13:43:44 -0500 | [diff] [blame] | 650 | if test "$type" != interactive && test "$upstream" = "$onto" && |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 651 | test "$mb" = "$onto" && test -z "$restrict_revision" && |
Johannes Sixt | 1308c17 | 2007-07-04 22:09:10 +0200 | [diff] [blame] | 652 | # linear history? |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 653 | ! (git rev-list --parents "$onto".."$orig_head" | sane_grep " .* ") > /dev/null |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 654 | then |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 655 | if test -z "$force_rebase" |
| 656 | then |
| 657 | # Lazily switch to the target branch if needed... |
Ramkumar Ramachandra | 4b03df2 | 2013-06-16 14:15:12 +0530 | [diff] [blame] | 658 | test -z "$switch_to" || |
| 659 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $switch_to" \ |
Jeff King | 22946a9 | 2015-04-28 01:17:37 -0400 | [diff] [blame] | 660 | git checkout -q "$switch_to" -- |
Kaartic Sivaraam | 08e6670 | 2017-12-16 14:33:19 +0530 | [diff] [blame] | 661 | 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 Ramachandra | 96e2b99 | 2013-06-13 21:36:13 +0530 | [diff] [blame] | 668 | finish_rebase |
Sverre Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 669 | exit 0 |
| 670 | else |
Kaartic Sivaraam | 08e6670 | 2017-12-16 14:33:19 +0530 | [diff] [blame] | 671 | 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 Rabbelier | b2f82e0 | 2009-02-13 23:48:01 +0100 | [diff] [blame] | 678 | fi |
Junio C Hamano | 7f4bd5d | 2005-11-28 13:00:31 -0800 | [diff] [blame] | 679 | fi |
| 680 | |
Martin von Zweigbergk | 8f9bfb6 | 2011-02-06 13:43:41 -0500 | [diff] [blame] | 681 | # If a hook exists, give it a chance to interrupt |
| 682 | run_pre_rebase_hook "$upstream_arg" "$@" |
| 683 | |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 684 | if test -n "$diffstat" |
| 685 | then |
| 686 | if test -n "$verbose" |
| 687 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 688 | echo "$(eval_gettext "Changes from \$mb to \$onto:")" |
Tor Arne Vestbø | a9c3821 | 2009-03-01 23:11:38 +0100 | [diff] [blame] | 689 | fi |
| 690 | # We want color (if set), but no pager |
| 691 | GIT_PAGER='' git diff --stat --summary "$mb" "$onto" |
| 692 | fi |
| 693 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 694 | test "$type" = interactive && run_specific_rebase |
Martin von Zweigbergk | f4107d9 | 2011-02-06 13:43:45 -0500 | [diff] [blame] | 695 | |
| 696 | # Detach HEAD and reset the tree |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 697 | 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] | 698 | |
| 699 | GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name" \ |
| 700 | git checkout -q "$onto^0" || die "could not detach HEAD" |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 701 | git update-ref ORIG_HEAD $orig_head |
Martin von Zweigbergk | f4107d9 | 2011-02-06 13:43:45 -0500 | [diff] [blame] | 702 | |
Junio C Hamano | e646c9c | 2006-02-14 14:42:05 -0800 | [diff] [blame] | 703 | # 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] | 704 | # we just fast-forwarded. |
Martin von Zweigbergk | cb82a05 | 2011-02-06 13:43:46 -0500 | [diff] [blame] | 705 | if test "$mb" = "$orig_head" |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 706 | then |
Jiang Xin | c7108bf | 2012-07-25 22:53:08 +0800 | [diff] [blame] | 707 | say "$(eval_gettext "Fast-forwarded \$branch_name to \$onto_name.")" |
Johannes Schindelin | 6fd2f5e | 2007-11-08 18:19:08 +0000 | [diff] [blame] | 708 | move_to_original_branch |
Ramkumar Ramachandra | af2f0eb | 2013-06-13 21:36:12 +0530 | [diff] [blame] | 709 | finish_rebase |
Lukas Sandström | 32d9954 | 2005-12-15 00:36:35 +0100 | [diff] [blame] | 710 | exit 0 |
| 711 | fi |
| 712 | |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 713 | if test -n "$rebase_root" |
| 714 | then |
| 715 | revisions="$onto..$orig_head" |
| 716 | else |
John Keeping | 1e0dacd | 2014-07-16 20:23:49 +0100 | [diff] [blame] | 717 | revisions="${restrict_revision-$upstream}..$orig_head" |
Thomas Rast | 190f532 | 2009-01-05 18:35:16 +0100 | [diff] [blame] | 718 | fi |
| 719 | |
Martin von Zweigbergk | fa99c1e | 2011-02-06 13:43:47 -0500 | [diff] [blame] | 720 | run_specific_rebase |