Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 1 | /* |
| 2 | * "git rebase" builtin command |
| 3 | * |
| 4 | * Copyright (c) 2018 Pratik Karki |
| 5 | */ |
| 6 | |
Nguyễn Thái Ngọc Duy | f8adbec | 2019-01-24 15:29:12 +0700 | [diff] [blame] | 7 | #define USE_THE_INDEX_COMPATIBILITY_MACROS |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 8 | #include "builtin.h" |
| 9 | #include "run-command.h" |
| 10 | #include "exec-cmd.h" |
Jeff King | dbbcd44 | 2020-07-28 16:23:39 -0400 | [diff] [blame] | 11 | #include "strvec.h" |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 12 | #include "dir.h" |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 13 | #include "packfile.h" |
| 14 | #include "refs.h" |
| 15 | #include "quote.h" |
| 16 | #include "config.h" |
| 17 | #include "cache-tree.h" |
| 18 | #include "unpack-trees.h" |
| 19 | #include "lockfile.h" |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 20 | #include "parse-options.h" |
Pratik Karki | 075bc85 | 2018-09-04 14:27:09 -0700 | [diff] [blame] | 21 | #include "commit.h" |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 22 | #include "diff.h" |
Pratik Karki | e0333e5 | 2018-09-04 14:27:14 -0700 | [diff] [blame] | 23 | #include "wt-status.h" |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 24 | #include "revision.h" |
Junio C Hamano | e0720a3 | 2018-11-02 11:04:53 +0900 | [diff] [blame] | 25 | #include "commit-reach.h" |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 26 | #include "rerere.h" |
Johannes Schindelin | 5aec927 | 2018-11-12 15:26:01 -0800 | [diff] [blame] | 27 | #include "branch.h" |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 28 | #include "sequencer.h" |
| 29 | #include "rebase-interactive.h" |
Denton Liu | b309a97 | 2020-04-07 10:28:00 -0400 | [diff] [blame] | 30 | #include "reset.h" |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 31 | |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 32 | #define DEFAULT_REFLOG_ACTION "rebase" |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 33 | |
| 34 | static char const * const builtin_rebase_usage[] = { |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 35 | N_("git rebase [-i] [options] [--exec <cmd>] " |
| 36 | "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"), |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 37 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " |
| 38 | "--root [<branch>]"), |
| 39 | N_("git rebase --continue | --abort | --skip | --edit-todo"), |
| 40 | NULL |
| 41 | }; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 42 | |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 43 | static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") |
| 44 | static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive") |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 45 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") |
| 46 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") |
| 47 | |
| 48 | enum rebase_type { |
| 49 | REBASE_UNSPECIFIED = -1, |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 50 | REBASE_APPLY, |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 51 | REBASE_MERGE, |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 52 | REBASE_PRESERVE_MERGES |
| 53 | }; |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 54 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 55 | enum empty_type { |
| 56 | EMPTY_UNSPECIFIED = -1, |
| 57 | EMPTY_DROP, |
| 58 | EMPTY_KEEP, |
| 59 | EMPTY_ASK |
| 60 | }; |
| 61 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 62 | struct rebase_options { |
| 63 | enum rebase_type type; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 64 | enum empty_type empty; |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 65 | const char *default_backend; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 66 | const char *state_dir; |
| 67 | struct commit *upstream; |
| 68 | const char *upstream_name; |
Pratik Karki | 06e4775 | 2018-09-04 14:27:10 -0700 | [diff] [blame] | 69 | const char *upstream_arg; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 70 | char *head_name; |
| 71 | struct object_id orig_head; |
| 72 | struct commit *onto; |
| 73 | const char *onto_name; |
| 74 | const char *revisions; |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 75 | const char *switch_to; |
Johannes Schindelin | e1fac53 | 2019-07-31 08:18:49 -0700 | [diff] [blame] | 76 | int root, root_with_onto; |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 77 | struct object_id *squash_onto; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 78 | struct commit *restrict_revision; |
| 79 | int dont_finish_rebase; |
Pratik Karki | b4c8eb0 | 2018-09-04 14:27:12 -0700 | [diff] [blame] | 80 | enum { |
| 81 | REBASE_NO_QUIET = 1<<0, |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 82 | REBASE_VERBOSE = 1<<1, |
| 83 | REBASE_DIFFSTAT = 1<<2, |
Pratik Karki | 1ed9c14 | 2018-09-04 14:27:17 -0700 | [diff] [blame] | 84 | REBASE_FORCE = 1<<3, |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 85 | REBASE_INTERACTIVE_EXPLICIT = 1<<4, |
Pratik Karki | b4c8eb0 | 2018-09-04 14:27:12 -0700 | [diff] [blame] | 86 | } flags; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 87 | struct strvec git_am_opts; |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 88 | const char *action; |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 89 | int signoff; |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 90 | int allow_rerere_autoupdate; |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 91 | int keep_empty; |
Pratik Karki | 051910a | 2018-09-04 14:59:58 -0700 | [diff] [blame] | 92 | int autosquash; |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 93 | char *gpg_sign_opt; |
Pratik Karki | 6defce2 | 2018-09-04 15:00:02 -0700 | [diff] [blame] | 94 | int autostash; |
Phillip Wood | 7573cec | 2020-08-17 18:40:02 +0100 | [diff] [blame] | 95 | int committer_date_is_author_date; |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 96 | int ignore_date; |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 97 | char *cmd; |
Pratik Karki | 9b3a448 | 2018-09-04 15:00:05 -0700 | [diff] [blame] | 98 | int allow_empty_message; |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 99 | int rebase_merges, rebase_cousins; |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 100 | char *strategy, *strategy_opts; |
Pratik Karki | cda614e | 2018-08-08 21:21:33 +0545 | [diff] [blame] | 101 | struct strbuf git_format_patch_opt; |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 102 | int reschedule_failed_exec; |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 103 | int reapply_cherry_picks; |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 104 | int fork_point; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 105 | }; |
| 106 | |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 107 | #define REBASE_OPTIONS_INIT { \ |
| 108 | .type = REBASE_UNSPECIFIED, \ |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 109 | .empty = EMPTY_UNSPECIFIED, \ |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 110 | .keep_empty = 1, \ |
Elijah Newren | 2ac0d62 | 2020-02-15 21:36:40 +0000 | [diff] [blame] | 111 | .default_backend = "merge", \ |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 112 | .flags = REBASE_NO_QUIET, \ |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 113 | .git_am_opts = STRVEC_INIT, \ |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 114 | .git_format_patch_opt = STRBUF_INIT, \ |
| 115 | .fork_point = -1, \ |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static struct replay_opts get_replay_opts(const struct rebase_options *opts) |
| 119 | { |
| 120 | struct replay_opts replay = REPLAY_OPTS_INIT; |
| 121 | |
| 122 | replay.action = REPLAY_INTERACTIVE_REBASE; |
Elijah Newren | 14c4586 | 2020-11-02 23:45:34 +0000 | [diff] [blame] | 123 | replay.strategy = NULL; |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 124 | sequencer_init_config(&replay); |
| 125 | |
| 126 | replay.signoff = opts->signoff; |
| 127 | replay.allow_ff = !(opts->flags & REBASE_FORCE); |
| 128 | if (opts->allow_rerere_autoupdate) |
| 129 | replay.allow_rerere_auto = opts->allow_rerere_autoupdate; |
| 130 | replay.allow_empty = 1; |
| 131 | replay.allow_empty_message = opts->allow_empty_message; |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 132 | replay.drop_redundant_commits = (opts->empty == EMPTY_DROP); |
| 133 | replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP); |
Elijah Newren | 55d2b6d | 2020-02-15 21:36:28 +0000 | [diff] [blame] | 134 | replay.quiet = !(opts->flags & REBASE_NO_QUIET); |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 135 | replay.verbose = opts->flags & REBASE_VERBOSE; |
| 136 | replay.reschedule_failed_exec = opts->reschedule_failed_exec; |
Phillip Wood | 7573cec | 2020-08-17 18:40:02 +0100 | [diff] [blame] | 137 | replay.committer_date_is_author_date = |
| 138 | opts->committer_date_is_author_date; |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 139 | replay.ignore_date = opts->ignore_date; |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 140 | replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt); |
Elijah Newren | 14c4586 | 2020-11-02 23:45:34 +0000 | [diff] [blame] | 141 | if (opts->strategy) |
Andrzej Hunt | b54cf3a | 2021-07-25 15:08:29 +0200 | [diff] [blame] | 142 | replay.strategy = xstrdup_or_null(opts->strategy); |
Elijah Newren | 14c4586 | 2020-11-02 23:45:34 +0000 | [diff] [blame] | 143 | else if (!replay.strategy && replay.default_strategy) { |
| 144 | replay.strategy = replay.default_strategy; |
| 145 | replay.default_strategy = NULL; |
| 146 | } |
Rohit Ashiwal | ef484ad | 2020-07-13 11:10:41 +0100 | [diff] [blame] | 147 | |
Rohit Ashiwal | ba51d2f | 2019-11-01 19:29:58 +0530 | [diff] [blame] | 148 | if (opts->strategy_opts) |
Junio C Hamano | 4d92452 | 2020-01-12 12:27:41 -0800 | [diff] [blame] | 149 | parse_strategy_opts(&replay, opts->strategy_opts); |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 150 | |
Alban Gruin | a2dd67f | 2019-11-24 18:43:31 +0100 | [diff] [blame] | 151 | if (opts->squash_onto) { |
| 152 | oidcpy(&replay.squash_onto, opts->squash_onto); |
| 153 | replay.have_squash_onto = 1; |
| 154 | } |
| 155 | |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 156 | return replay; |
| 157 | } |
| 158 | |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 159 | enum action { |
| 160 | ACTION_NONE = 0, |
| 161 | ACTION_CONTINUE, |
| 162 | ACTION_SKIP, |
| 163 | ACTION_ABORT, |
| 164 | ACTION_QUIT, |
| 165 | ACTION_EDIT_TODO, |
| 166 | ACTION_SHOW_CURRENT_PATCH, |
| 167 | ACTION_SHORTEN_OIDS, |
| 168 | ACTION_EXPAND_OIDS, |
| 169 | ACTION_CHECK_TODO_LIST, |
| 170 | ACTION_REARRANGE_SQUASH, |
| 171 | ACTION_ADD_EXEC |
| 172 | }; |
| 173 | |
| 174 | static const char *action_names[] = { "undefined", |
| 175 | "continue", |
| 176 | "skip", |
| 177 | "abort", |
| 178 | "quit", |
| 179 | "edit_todo", |
| 180 | "show_current_patch" }; |
| 181 | |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 182 | static int add_exec_commands(struct string_list *commands) |
| 183 | { |
| 184 | const char *todo_file = rebase_path_todo(); |
| 185 | struct todo_list todo_list = TODO_LIST_INIT; |
| 186 | int res; |
| 187 | |
| 188 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
| 189 | return error_errno(_("could not read '%s'."), todo_file); |
| 190 | |
| 191 | if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf, |
| 192 | &todo_list)) { |
| 193 | todo_list_release(&todo_list); |
| 194 | return error(_("unusable todo list: '%s'"), todo_file); |
| 195 | } |
| 196 | |
| 197 | todo_list_add_exec_commands(&todo_list, commands); |
| 198 | res = todo_list_write_to_file(the_repository, &todo_list, |
| 199 | todo_file, NULL, NULL, -1, 0); |
| 200 | todo_list_release(&todo_list); |
| 201 | |
| 202 | if (res) |
| 203 | return error_errno(_("could not write '%s'."), todo_file); |
| 204 | return 0; |
| 205 | } |
| 206 | |
| 207 | static int rearrange_squash_in_todo_file(void) |
| 208 | { |
| 209 | const char *todo_file = rebase_path_todo(); |
| 210 | struct todo_list todo_list = TODO_LIST_INIT; |
| 211 | int res = 0; |
| 212 | |
| 213 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
| 214 | return error_errno(_("could not read '%s'."), todo_file); |
| 215 | if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf, |
| 216 | &todo_list)) { |
| 217 | todo_list_release(&todo_list); |
| 218 | return error(_("unusable todo list: '%s'"), todo_file); |
| 219 | } |
| 220 | |
| 221 | res = todo_list_rearrange_squash(&todo_list); |
| 222 | if (!res) |
| 223 | res = todo_list_write_to_file(the_repository, &todo_list, |
| 224 | todo_file, NULL, NULL, -1, 0); |
| 225 | |
| 226 | todo_list_release(&todo_list); |
| 227 | |
| 228 | if (res) |
| 229 | return error_errno(_("could not write '%s'."), todo_file); |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | static int transform_todo_file(unsigned flags) |
| 234 | { |
| 235 | const char *todo_file = rebase_path_todo(); |
| 236 | struct todo_list todo_list = TODO_LIST_INIT; |
| 237 | int res; |
| 238 | |
| 239 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
| 240 | return error_errno(_("could not read '%s'."), todo_file); |
| 241 | |
| 242 | if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf, |
| 243 | &todo_list)) { |
| 244 | todo_list_release(&todo_list); |
| 245 | return error(_("unusable todo list: '%s'"), todo_file); |
| 246 | } |
| 247 | |
| 248 | res = todo_list_write_to_file(the_repository, &todo_list, todo_file, |
| 249 | NULL, NULL, -1, flags); |
| 250 | todo_list_release(&todo_list); |
| 251 | |
| 252 | if (res) |
| 253 | return error_errno(_("could not write '%s'."), todo_file); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int edit_todo_file(unsigned flags) |
| 258 | { |
| 259 | const char *todo_file = rebase_path_todo(); |
| 260 | struct todo_list todo_list = TODO_LIST_INIT, |
| 261 | new_todo = TODO_LIST_INIT; |
| 262 | int res = 0; |
| 263 | |
| 264 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) |
| 265 | return error_errno(_("could not read '%s'."), todo_file); |
| 266 | |
| 267 | strbuf_stripspace(&todo_list.buf, 1); |
| 268 | res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags); |
| 269 | if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file, |
| 270 | NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS))) |
| 271 | res = error_errno(_("could not write '%s'"), todo_file); |
| 272 | |
| 273 | todo_list_release(&todo_list); |
| 274 | todo_list_release(&new_todo); |
| 275 | |
| 276 | return res; |
| 277 | } |
| 278 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 279 | static int get_revision_ranges(struct commit *upstream, struct commit *onto, |
Phillip Wood | 8843302 | 2020-11-04 15:29:40 +0000 | [diff] [blame] | 280 | struct object_id *orig_head, char **revisions, |
| 281 | char **shortrevisions) |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 282 | { |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 283 | struct commit *base_rev = upstream ? upstream : onto; |
| 284 | const char *shorthead; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 285 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 286 | *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid), |
Phillip Wood | 8843302 | 2020-11-04 15:29:40 +0000 | [diff] [blame] | 287 | oid_to_hex(orig_head)); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 288 | |
Alban Gruin | 767a9c4 | 2020-01-24 16:05:00 +0100 | [diff] [blame] | 289 | shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 290 | |
| 291 | if (upstream) { |
| 292 | const char *shortrev; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 293 | |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 294 | shortrev = find_unique_abbrev(&base_rev->object.oid, |
| 295 | DEFAULT_ABBREV); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 296 | |
| 297 | *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); |
| 298 | } else |
| 299 | *shortrevisions = xstrdup(shorthead); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | static int init_basic_state(struct replay_opts *opts, const char *head_name, |
Phillip Wood | a2bb10d | 2020-11-04 15:29:39 +0000 | [diff] [blame] | 305 | struct commit *onto, |
| 306 | const struct object_id *orig_head) |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 307 | { |
| 308 | FILE *interactive; |
| 309 | |
Phillip Wood | c44c246 | 2019-04-17 15:30:38 +0100 | [diff] [blame] | 310 | if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir())) |
| 311 | return error_errno(_("could not create temporary %s"), merge_dir()); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 312 | |
| 313 | delete_reflog("REBASE_HEAD"); |
| 314 | |
| 315 | interactive = fopen(path_interactive(), "w"); |
| 316 | if (!interactive) |
| 317 | return error_errno(_("could not mark as interactive")); |
| 318 | fclose(interactive); |
| 319 | |
| 320 | return write_basic_state(opts, head_name, onto, orig_head); |
| 321 | } |
| 322 | |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 323 | static void split_exec_commands(const char *cmd, struct string_list *commands) |
| 324 | { |
| 325 | if (cmd && *cmd) { |
| 326 | string_list_split(commands, cmd, '\n', -1); |
| 327 | |
| 328 | /* rebase.c adds a new line to cmd after every command, |
| 329 | * so here the last command is always empty */ |
| 330 | string_list_remove_empty_items(commands, 0); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 335 | { |
| 336 | int ret; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 337 | char *revisions = NULL, *shortrevisions = NULL; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 338 | struct strvec make_script_args = STRVEC_INIT; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 339 | struct todo_list todo_list = TODO_LIST_INIT; |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 340 | struct replay_opts replay = get_replay_opts(opts); |
| 341 | struct string_list commands = STRING_LIST_INIT_DUP; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 342 | |
Alban Gruin | 767a9c4 | 2020-01-24 16:05:00 +0100 | [diff] [blame] | 343 | if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head, |
Phillip Wood | 8843302 | 2020-11-04 15:29:40 +0000 | [diff] [blame] | 344 | &revisions, &shortrevisions)) |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 345 | return -1; |
| 346 | |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 347 | if (init_basic_state(&replay, |
| 348 | opts->head_name ? opts->head_name : "detached HEAD", |
Phillip Wood | a2bb10d | 2020-11-04 15:29:39 +0000 | [diff] [blame] | 349 | opts->onto, &opts->orig_head)) { |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 350 | free(revisions); |
| 351 | free(shortrevisions); |
| 352 | |
| 353 | return -1; |
| 354 | } |
| 355 | |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 356 | if (!opts->upstream && opts->squash_onto) |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 357 | write_file(path_squash_onto(), "%s\n", |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 358 | oid_to_hex(opts->squash_onto)); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 359 | |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 360 | strvec_pushl(&make_script_args, "", revisions, NULL); |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 361 | if (opts->restrict_revision) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 362 | strvec_pushf(&make_script_args, "^%s", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 363 | oid_to_hex(&opts->restrict_revision->object.oid)); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 364 | |
| 365 | ret = sequencer_make_script(the_repository, &todo_list.buf, |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 366 | make_script_args.nr, make_script_args.v, |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 367 | flags); |
| 368 | |
| 369 | if (ret) |
| 370 | error(_("could not generate todo list")); |
| 371 | else { |
| 372 | discard_cache(); |
| 373 | if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf, |
| 374 | &todo_list)) |
| 375 | BUG("unusable todo list"); |
| 376 | |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 377 | split_exec_commands(opts->cmd, &commands); |
| 378 | ret = complete_action(the_repository, &replay, flags, |
Phillip Wood | f3e27a0 | 2020-11-04 15:29:38 +0000 | [diff] [blame] | 379 | shortrevisions, opts->onto_name, opts->onto, |
| 380 | &opts->orig_head, &commands, opts->autosquash, |
| 381 | &todo_list); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Phillip Wood | 0ea0847 | 2019-04-17 15:30:42 +0100 | [diff] [blame] | 384 | string_list_clear(&commands, 0); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 385 | free(revisions); |
| 386 | free(shortrevisions); |
| 387 | todo_list_release(&todo_list); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 388 | strvec_clear(&make_script_args); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 393 | static int run_sequencer_rebase(struct rebase_options *opts, |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 394 | enum action command) |
| 395 | { |
| 396 | unsigned flags = 0; |
| 397 | int abbreviate_commands = 0, ret = 0; |
| 398 | |
| 399 | git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands); |
| 400 | |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 401 | flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0; |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 402 | flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; |
| 403 | flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0; |
| 404 | flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; |
Johannes Schindelin | e1fac53 | 2019-07-31 08:18:49 -0700 | [diff] [blame] | 405 | flags |= opts->root_with_onto ? TODO_LIST_ROOT_WITH_ONTO : 0; |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 406 | flags |= command == ACTION_SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0; |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 407 | flags |= opts->reapply_cherry_picks ? TODO_LIST_REAPPLY_CHERRY_PICKS : 0; |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 408 | |
| 409 | switch (command) { |
| 410 | case ACTION_NONE: { |
| 411 | if (!opts->onto && !opts->upstream) |
| 412 | die(_("a base commit must be provided with --upstream or --onto")); |
| 413 | |
| 414 | ret = do_interactive_rebase(opts, flags); |
| 415 | break; |
| 416 | } |
| 417 | case ACTION_SKIP: { |
| 418 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
| 419 | |
| 420 | rerere_clear(the_repository, &merge_rr); |
| 421 | } |
| 422 | /* fallthrough */ |
| 423 | case ACTION_CONTINUE: { |
| 424 | struct replay_opts replay_opts = get_replay_opts(opts); |
| 425 | |
| 426 | ret = sequencer_continue(the_repository, &replay_opts); |
| 427 | break; |
| 428 | } |
| 429 | case ACTION_EDIT_TODO: |
| 430 | ret = edit_todo_file(flags); |
| 431 | break; |
| 432 | case ACTION_SHOW_CURRENT_PATCH: { |
| 433 | struct child_process cmd = CHILD_PROCESS_INIT; |
| 434 | |
| 435 | cmd.git_cmd = 1; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 436 | strvec_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL); |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 437 | ret = run_command(&cmd); |
| 438 | |
| 439 | break; |
| 440 | } |
| 441 | case ACTION_SHORTEN_OIDS: |
| 442 | case ACTION_EXPAND_OIDS: |
| 443 | ret = transform_todo_file(flags); |
| 444 | break; |
| 445 | case ACTION_CHECK_TODO_LIST: |
| 446 | ret = check_todo_list_from_file(the_repository); |
| 447 | break; |
| 448 | case ACTION_REARRANGE_SQUASH: |
| 449 | ret = rearrange_squash_in_todo_file(); |
| 450 | break; |
| 451 | case ACTION_ADD_EXEC: { |
| 452 | struct string_list commands = STRING_LIST_INIT_DUP; |
| 453 | |
| 454 | split_exec_commands(opts->cmd, &commands); |
| 455 | ret = add_exec_commands(&commands); |
| 456 | string_list_clear(&commands, 0); |
| 457 | break; |
| 458 | } |
| 459 | default: |
| 460 | BUG("invalid command '%d'", command); |
| 461 | } |
| 462 | |
| 463 | return ret; |
| 464 | } |
| 465 | |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 466 | static void imply_merge(struct rebase_options *opts, const char *option); |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 467 | static int parse_opt_keep_empty(const struct option *opt, const char *arg, |
| 468 | int unset) |
| 469 | { |
| 470 | struct rebase_options *opts = opt->value; |
| 471 | |
| 472 | BUG_ON_OPT_ARG(arg); |
| 473 | |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 474 | imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty"); |
| 475 | opts->keep_empty = !unset; |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 476 | opts->type = REBASE_MERGE; |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 477 | return 0; |
| 478 | } |
| 479 | |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 480 | static const char * const builtin_rebase_interactive_usage[] = { |
| 481 | N_("git rebase--interactive [<options>]"), |
| 482 | NULL |
| 483 | }; |
| 484 | |
| 485 | int cmd_rebase__interactive(int argc, const char **argv, const char *prefix) |
| 486 | { |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 487 | struct rebase_options opts = REBASE_OPTIONS_INIT; |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 488 | struct object_id squash_onto = *null_oid(); |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 489 | enum action command = ACTION_NONE; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 490 | struct option options[] = { |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 491 | OPT_NEGBIT(0, "ff", &opts.flags, N_("allow fast-forward"), |
| 492 | REBASE_FORCE), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 493 | OPT_CALLBACK_F('k', "keep-empty", &options, NULL, |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 494 | N_("keep commits which start empty"), |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 495 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 496 | parse_opt_keep_empty), |
Elijah Newren | 22a69fd | 2020-01-16 06:14:15 +0000 | [diff] [blame] | 497 | OPT_BOOL_F(0, "allow-empty-message", &opts.allow_empty_message, |
| 498 | N_("allow commits with empty messages"), |
| 499 | PARSE_OPT_HIDDEN), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 500 | OPT_BOOL(0, "rebase-merges", &opts.rebase_merges, N_("rebase merge commits")), |
| 501 | OPT_BOOL(0, "rebase-cousins", &opts.rebase_cousins, |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 502 | N_("keep original branch points of cousins")), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 503 | OPT_BOOL(0, "autosquash", &opts.autosquash, |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 504 | N_("move commits that begin with squash!/fixup!")), |
| 505 | OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 506 | OPT_BIT('v', "verbose", &opts.flags, |
| 507 | N_("display a diffstat of what changed upstream"), |
| 508 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 509 | OPT_CMDMODE(0, "continue", &command, N_("continue rebase"), |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 510 | ACTION_CONTINUE), |
| 511 | OPT_CMDMODE(0, "skip", &command, N_("skip commit"), ACTION_SKIP), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 512 | OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"), |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 513 | ACTION_EDIT_TODO), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 514 | OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"), |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 515 | ACTION_SHOW_CURRENT_PATCH), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 516 | OPT_CMDMODE(0, "shorten-ids", &command, |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 517 | N_("shorten commit ids in the todo list"), ACTION_SHORTEN_OIDS), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 518 | OPT_CMDMODE(0, "expand-ids", &command, |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 519 | N_("expand commit ids in the todo list"), ACTION_EXPAND_OIDS), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 520 | OPT_CMDMODE(0, "check-todo-list", &command, |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 521 | N_("check the todo list"), ACTION_CHECK_TODO_LIST), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 522 | OPT_CMDMODE(0, "rearrange-squash", &command, |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 523 | N_("rearrange fixup/squash lines"), ACTION_REARRANGE_SQUASH), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 524 | OPT_CMDMODE(0, "add-exec-commands", &command, |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 525 | N_("insert exec commands in todo list"), ACTION_ADD_EXEC), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 526 | { OPTION_CALLBACK, 0, "onto", &opts.onto, N_("onto"), N_("onto"), |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 527 | PARSE_OPT_NONEG, parse_opt_commit, 0 }, |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 528 | { OPTION_CALLBACK, 0, "restrict-revision", &opts.restrict_revision, |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 529 | N_("restrict-revision"), N_("restrict revision"), |
| 530 | PARSE_OPT_NONEG, parse_opt_commit, 0 }, |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 531 | { OPTION_CALLBACK, 0, "squash-onto", &squash_onto, N_("squash-onto"), |
| 532 | N_("squash onto"), PARSE_OPT_NONEG, parse_opt_object_id, 0 }, |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 533 | { OPTION_CALLBACK, 0, "upstream", &opts.upstream, N_("upstream"), |
Phillip Wood | 7d3488e | 2019-04-17 15:30:39 +0100 | [diff] [blame] | 534 | N_("the upstream commit"), PARSE_OPT_NONEG, parse_opt_commit, |
| 535 | 0 }, |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 536 | OPT_STRING(0, "head-name", &opts.head_name, N_("head-name"), N_("head name")), |
| 537 | { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign_opt, N_("key-id"), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 538 | N_("GPG-sign commits"), |
| 539 | PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
| 540 | OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"), |
| 541 | N_("rebase strategy")), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 542 | OPT_STRING(0, "strategy-opts", &opts.strategy_opts, N_("strategy-opts"), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 543 | N_("strategy options")), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 544 | OPT_STRING(0, "switch-to", &opts.switch_to, N_("switch-to"), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 545 | N_("the branch or commit to checkout")), |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 546 | OPT_STRING(0, "onto-name", &opts.onto_name, N_("onto-name"), N_("onto name")), |
| 547 | OPT_STRING(0, "cmd", &opts.cmd, N_("cmd"), N_("the command to run")), |
| 548 | OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_autoupdate), |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 549 | OPT_BOOL(0, "reschedule-failed-exec", &opts.reschedule_failed_exec, |
| 550 | N_("automatically re-schedule any `exec` that fails")), |
| 551 | OPT_END() |
| 552 | }; |
| 553 | |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 554 | opts.rebase_cousins = -1; |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 555 | |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 556 | if (argc == 1) |
| 557 | usage_with_options(builtin_rebase_interactive_usage, options); |
| 558 | |
Junio C Hamano | c0e78f7 | 2019-06-13 13:19:34 -0700 | [diff] [blame] | 559 | argc = parse_options(argc, argv, prefix, options, |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 560 | builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0); |
| 561 | |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 562 | if (!is_null_oid(&squash_onto)) |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 563 | opts.squash_onto = &squash_onto; |
Phillip Wood | 3389853 | 2019-04-17 15:30:40 +0100 | [diff] [blame] | 564 | |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 565 | if (opts.rebase_cousins >= 0 && !opts.rebase_merges) |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 566 | warning(_("--[no-]rebase-cousins has no effect without " |
| 567 | "--rebase-merges")); |
| 568 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 569 | return !!run_sequencer_rebase(&opts, command); |
Phillip Wood | 0609b74 | 2019-04-17 15:30:37 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 572 | static int is_merge(struct rebase_options *opts) |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 573 | { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 574 | return opts->type == REBASE_MERGE || |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 575 | opts->type == REBASE_PRESERVE_MERGES; |
| 576 | } |
| 577 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 578 | static void imply_merge(struct rebase_options *opts, const char *option) |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 579 | { |
| 580 | switch (opts->type) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 581 | case REBASE_APPLY: |
Elijah Newren | 50ed761 | 2020-04-11 02:44:26 +0000 | [diff] [blame] | 582 | die(_("%s requires the merge backend"), option); |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 583 | break; |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 584 | case REBASE_MERGE: |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 585 | case REBASE_PRESERVE_MERGES: |
| 586 | break; |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 587 | default: |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 588 | opts->type = REBASE_MERGE; /* implied */ |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 593 | /* Returns the filename prefixed by the state_dir */ |
| 594 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) |
| 595 | { |
| 596 | static struct strbuf path = STRBUF_INIT; |
| 597 | static size_t prefix_len; |
| 598 | |
| 599 | if (!prefix_len) { |
| 600 | strbuf_addf(&path, "%s/", opts->state_dir); |
| 601 | prefix_len = path.len; |
| 602 | } |
| 603 | |
| 604 | strbuf_setlen(&path, prefix_len); |
| 605 | strbuf_addstr(&path, filename); |
| 606 | return path.buf; |
| 607 | } |
| 608 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 609 | /* Initialize the rebase options from the state directory. */ |
| 610 | static int read_basic_state(struct rebase_options *opts) |
| 611 | { |
| 612 | struct strbuf head_name = STRBUF_INIT; |
| 613 | struct strbuf buf = STRBUF_INIT; |
| 614 | struct object_id oid; |
| 615 | |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 616 | if (!read_oneliner(&head_name, state_dir_path("head-name", opts), |
| 617 | READ_ONELINER_WARN_MISSING) || |
| 618 | !read_oneliner(&buf, state_dir_path("onto", opts), |
| 619 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 620 | return -1; |
| 621 | opts->head_name = starts_with(head_name.buf, "refs/") ? |
| 622 | xstrdup(head_name.buf) : NULL; |
| 623 | strbuf_release(&head_name); |
| 624 | if (get_oid(buf.buf, &oid)) |
| 625 | return error(_("could not get 'onto': '%s'"), buf.buf); |
| 626 | opts->onto = lookup_commit_or_die(&oid, buf.buf); |
| 627 | |
| 628 | /* |
| 629 | * We always write to orig-head, but interactive rebase used to write to |
| 630 | * head. Fall back to reading from head to cover for the case that the |
| 631 | * user upgraded git with an ongoing interactive rebase. |
| 632 | */ |
| 633 | strbuf_reset(&buf); |
| 634 | if (file_exists(state_dir_path("orig-head", opts))) { |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 635 | if (!read_oneliner(&buf, state_dir_path("orig-head", opts), |
| 636 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 637 | return -1; |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 638 | } else if (!read_oneliner(&buf, state_dir_path("head", opts), |
| 639 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 640 | return -1; |
| 641 | if (get_oid(buf.buf, &opts->orig_head)) |
| 642 | return error(_("invalid orig-head: '%s'"), buf.buf); |
| 643 | |
Elijah Newren | 899b49c | 2018-12-11 08:11:36 -0800 | [diff] [blame] | 644 | if (file_exists(state_dir_path("quiet", opts))) |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 645 | opts->flags &= ~REBASE_NO_QUIET; |
| 646 | else |
| 647 | opts->flags |= REBASE_NO_QUIET; |
| 648 | |
| 649 | if (file_exists(state_dir_path("verbose", opts))) |
| 650 | opts->flags |= REBASE_VERBOSE; |
| 651 | |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 652 | if (file_exists(state_dir_path("signoff", opts))) { |
| 653 | opts->signoff = 1; |
| 654 | opts->flags |= REBASE_FORCE; |
| 655 | } |
| 656 | |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 657 | if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) { |
| 658 | strbuf_reset(&buf); |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 659 | if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts), |
| 660 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 661 | return -1; |
| 662 | if (!strcmp(buf.buf, "--rerere-autoupdate")) |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 663 | opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE; |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 664 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 665 | opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE; |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 666 | else |
| 667 | warning(_("ignoring invalid allow_rerere_autoupdate: " |
| 668 | "'%s'"), buf.buf); |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 669 | } |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 670 | |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 671 | if (file_exists(state_dir_path("gpg_sign_opt", opts))) { |
| 672 | strbuf_reset(&buf); |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 673 | if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts), |
| 674 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 675 | return -1; |
| 676 | free(opts->gpg_sign_opt); |
| 677 | opts->gpg_sign_opt = xstrdup(buf.buf); |
| 678 | } |
| 679 | |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 680 | if (file_exists(state_dir_path("strategy", opts))) { |
| 681 | strbuf_reset(&buf); |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 682 | if (!read_oneliner(&buf, state_dir_path("strategy", opts), |
| 683 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 684 | return -1; |
| 685 | free(opts->strategy); |
| 686 | opts->strategy = xstrdup(buf.buf); |
| 687 | } |
| 688 | |
| 689 | if (file_exists(state_dir_path("strategy_opts", opts))) { |
| 690 | strbuf_reset(&buf); |
Denton Liu | efcf6cf | 2020-04-07 10:27:55 -0400 | [diff] [blame] | 691 | if (!read_oneliner(&buf, state_dir_path("strategy_opts", opts), |
| 692 | READ_ONELINER_WARN_MISSING)) |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 693 | return -1; |
| 694 | free(opts->strategy_opts); |
| 695 | opts->strategy_opts = xstrdup(buf.buf); |
| 696 | } |
| 697 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 698 | strbuf_release(&buf); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
Phillip Wood | 28dc09d | 2019-04-17 15:30:35 +0100 | [diff] [blame] | 703 | static int rebase_write_basic_state(struct rebase_options *opts) |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 704 | { |
| 705 | write_file(state_dir_path("head-name", opts), "%s", |
| 706 | opts->head_name ? opts->head_name : "detached HEAD"); |
| 707 | write_file(state_dir_path("onto", opts), "%s", |
| 708 | opts->onto ? oid_to_hex(&opts->onto->object.oid) : ""); |
| 709 | write_file(state_dir_path("orig-head", opts), "%s", |
| 710 | oid_to_hex(&opts->orig_head)); |
Elijah Newren | 8a997ed | 2020-02-15 21:36:27 +0000 | [diff] [blame] | 711 | if (!(opts->flags & REBASE_NO_QUIET)) |
| 712 | write_file(state_dir_path("quiet", opts), "%s", ""); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 713 | if (opts->flags & REBASE_VERBOSE) |
| 714 | write_file(state_dir_path("verbose", opts), "%s", ""); |
| 715 | if (opts->strategy) |
| 716 | write_file(state_dir_path("strategy", opts), "%s", |
| 717 | opts->strategy); |
| 718 | if (opts->strategy_opts) |
| 719 | write_file(state_dir_path("strategy_opts", opts), "%s", |
| 720 | opts->strategy_opts); |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 721 | if (opts->allow_rerere_autoupdate > 0) |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 722 | write_file(state_dir_path("allow_rerere_autoupdate", opts), |
| 723 | "-%s-rerere-autoupdate", |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 724 | opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ? |
| 725 | "" : "-no"); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 726 | if (opts->gpg_sign_opt) |
| 727 | write_file(state_dir_path("gpg_sign_opt", opts), "%s", |
| 728 | opts->gpg_sign_opt); |
| 729 | if (opts->signoff) |
Elijah Newren | 4fe7e43 | 2019-12-20 18:53:56 +0000 | [diff] [blame] | 730 | write_file(state_dir_path("signoff", opts), "--signoff"); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 731 | |
| 732 | return 0; |
| 733 | } |
| 734 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 735 | static int finish_rebase(struct rebase_options *opts) |
| 736 | { |
| 737 | struct strbuf dir = STRBUF_INIT; |
Phillip Wood | d3fce47 | 2019-05-14 19:03:47 +0100 | [diff] [blame] | 738 | int ret = 0; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 739 | |
| 740 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); |
Elijah Newren | 5291828 | 2021-03-20 00:03:52 +0000 | [diff] [blame] | 741 | unlink(git_path_auto_merge(the_repository)); |
Denton Liu | 86ed00a | 2020-04-07 10:27:58 -0400 | [diff] [blame] | 742 | apply_autostash(state_dir_path("autostash", opts)); |
Derrick Stolee | 2d511cf | 2019-05-17 11:41:49 -0700 | [diff] [blame] | 743 | close_object_store(the_repository->objects); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 744 | /* |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 745 | * We ignore errors in 'git maintenance run --auto', since the |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 746 | * user should see them. |
| 747 | */ |
Derrick Stolee | a95ce12 | 2020-09-17 18:11:44 +0000 | [diff] [blame] | 748 | run_auto_maintenance(!(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE))); |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 749 | if (opts->type == REBASE_MERGE) { |
Phillip Wood | d559f50 | 2019-05-14 19:03:49 +0100 | [diff] [blame] | 750 | struct replay_opts replay = REPLAY_OPTS_INIT; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 751 | |
Phillip Wood | d559f50 | 2019-05-14 19:03:49 +0100 | [diff] [blame] | 752 | replay.action = REPLAY_INTERACTIVE_REBASE; |
| 753 | ret = sequencer_remove_state(&replay); |
| 754 | } else { |
| 755 | strbuf_addstr(&dir, opts->state_dir); |
| 756 | if (remove_dir_recursively(&dir, 0)) |
| 757 | ret = error(_("could not remove '%s'"), |
| 758 | opts->state_dir); |
| 759 | strbuf_release(&dir); |
| 760 | } |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 761 | |
Phillip Wood | d3fce47 | 2019-05-14 19:03:47 +0100 | [diff] [blame] | 762 | return ret; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static struct commit *peel_committish(const char *name) |
| 766 | { |
| 767 | struct object *obj; |
| 768 | struct object_id oid; |
| 769 | |
| 770 | if (get_oid(name, &oid)) |
| 771 | return NULL; |
| 772 | obj = parse_object(the_repository, &oid); |
| 773 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); |
| 774 | } |
| 775 | |
| 776 | static void add_var(struct strbuf *buf, const char *name, const char *value) |
| 777 | { |
| 778 | if (!value) |
| 779 | strbuf_addf(buf, "unset %s; ", name); |
| 780 | else { |
| 781 | strbuf_addf(buf, "%s=", name); |
| 782 | sq_quote_buf(buf, value); |
| 783 | strbuf_addstr(buf, "; "); |
| 784 | } |
| 785 | } |
| 786 | |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 787 | static int move_to_original_branch(struct rebase_options *opts) |
| 788 | { |
| 789 | struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; |
| 790 | int ret; |
| 791 | |
| 792 | if (!opts->head_name) |
| 793 | return 0; /* nothing to move back to */ |
| 794 | |
| 795 | if (!opts->onto) |
| 796 | BUG("move_to_original_branch without onto"); |
| 797 | |
| 798 | strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s", |
| 799 | opts->head_name, oid_to_hex(&opts->onto->object.oid)); |
| 800 | strbuf_addf(&head_reflog, "rebase finished: returning to %s", |
| 801 | opts->head_name); |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 802 | ret = reset_head(the_repository, NULL, "", opts->head_name, |
| 803 | RESET_HEAD_REFS_ONLY, |
| 804 | orig_head_reflog.buf, head_reflog.buf, |
| 805 | DEFAULT_REFLOG_ACTION); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 806 | |
| 807 | strbuf_release(&orig_head_reflog); |
| 808 | strbuf_release(&head_reflog); |
| 809 | return ret; |
| 810 | } |
| 811 | |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 812 | static const char *resolvemsg = |
| 813 | N_("Resolve all conflicts manually, mark them as resolved with\n" |
| 814 | "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n" |
| 815 | "You can instead skip this commit: run \"git rebase --skip\".\n" |
| 816 | "To abort and get back to the state before \"git rebase\", run " |
| 817 | "\"git rebase --abort\"."); |
| 818 | |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 819 | static int run_am(struct rebase_options *opts) |
| 820 | { |
| 821 | struct child_process am = CHILD_PROCESS_INIT; |
| 822 | struct child_process format_patch = CHILD_PROCESS_INIT; |
| 823 | struct strbuf revisions = STRBUF_INIT; |
| 824 | int status; |
| 825 | char *rebased_patches; |
| 826 | |
| 827 | am.git_cmd = 1; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 828 | strvec_push(&am.args, "am"); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 829 | |
| 830 | if (opts->action && !strcmp("continue", opts->action)) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 831 | strvec_push(&am.args, "--resolved"); |
| 832 | strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 833 | if (opts->gpg_sign_opt) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 834 | strvec_push(&am.args, opts->gpg_sign_opt); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 835 | status = run_command(&am); |
| 836 | if (status) |
| 837 | return status; |
| 838 | |
| 839 | return move_to_original_branch(opts); |
| 840 | } |
| 841 | if (opts->action && !strcmp("skip", opts->action)) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 842 | strvec_push(&am.args, "--skip"); |
| 843 | strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 844 | status = run_command(&am); |
| 845 | if (status) |
| 846 | return status; |
| 847 | |
| 848 | return move_to_original_branch(opts); |
| 849 | } |
| 850 | if (opts->action && !strcmp("show-current-patch", opts->action)) { |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 851 | strvec_push(&am.args, "--show-current-patch"); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 852 | return run_command(&am); |
| 853 | } |
| 854 | |
| 855 | strbuf_addf(&revisions, "%s...%s", |
| 856 | oid_to_hex(opts->root ? |
| 857 | /* this is now equivalent to !opts->upstream */ |
| 858 | &opts->onto->object.oid : |
| 859 | &opts->upstream->object.oid), |
| 860 | oid_to_hex(&opts->orig_head)); |
| 861 | |
| 862 | rebased_patches = xstrdup(git_path("rebased-patches")); |
| 863 | format_patch.out = open(rebased_patches, |
| 864 | O_WRONLY | O_CREAT | O_TRUNC, 0666); |
| 865 | if (format_patch.out < 0) { |
| 866 | status = error_errno(_("could not open '%s' for writing"), |
| 867 | rebased_patches); |
| 868 | free(rebased_patches); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 869 | strvec_clear(&am.args); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 870 | return status; |
| 871 | } |
| 872 | |
| 873 | format_patch.git_cmd = 1; |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 874 | strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 875 | "--full-index", "--cherry-pick", "--right-only", |
| 876 | "--src-prefix=a/", "--dst-prefix=b/", "--no-renames", |
| 877 | "--no-cover-letter", "--pretty=mboxrd", "--topo-order", |
| 878 | "--no-base", NULL); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 879 | if (opts->git_format_patch_opt.len) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 880 | strvec_split(&format_patch.args, |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 881 | opts->git_format_patch_opt.buf); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 882 | strvec_push(&format_patch.args, revisions.buf); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 883 | if (opts->restrict_revision) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 884 | strvec_pushf(&format_patch.args, "^%s", |
Jeff King | f6d8942 | 2020-07-28 16:26:31 -0400 | [diff] [blame] | 885 | oid_to_hex(&opts->restrict_revision->object.oid)); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 886 | |
| 887 | status = run_command(&format_patch); |
| 888 | if (status) { |
| 889 | unlink(rebased_patches); |
| 890 | free(rebased_patches); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 891 | strvec_clear(&am.args); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 892 | |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 893 | reset_head(the_repository, &opts->orig_head, "checkout", |
| 894 | opts->head_name, 0, |
| 895 | "HEAD", NULL, DEFAULT_REFLOG_ACTION); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 896 | error(_("\ngit encountered an error while preparing the " |
| 897 | "patches to replay\n" |
| 898 | "these revisions:\n" |
| 899 | "\n %s\n\n" |
| 900 | "As a result, git cannot rebase them."), |
| 901 | opts->revisions); |
| 902 | |
| 903 | strbuf_release(&revisions); |
| 904 | return status; |
| 905 | } |
| 906 | strbuf_release(&revisions); |
| 907 | |
| 908 | am.in = open(rebased_patches, O_RDONLY); |
| 909 | if (am.in < 0) { |
| 910 | status = error_errno(_("could not open '%s' for reading"), |
| 911 | rebased_patches); |
| 912 | free(rebased_patches); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 913 | strvec_clear(&am.args); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 914 | return status; |
| 915 | } |
| 916 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 917 | strvec_pushv(&am.args, opts->git_am_opts.v); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 918 | strvec_push(&am.args, "--rebasing"); |
| 919 | strvec_pushf(&am.args, "--resolvemsg=%s", resolvemsg); |
| 920 | strvec_push(&am.args, "--patch-format=mboxrd"); |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 921 | if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 922 | strvec_push(&am.args, "--rerere-autoupdate"); |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 923 | else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 924 | strvec_push(&am.args, "--no-rerere-autoupdate"); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 925 | if (opts->gpg_sign_opt) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 926 | strvec_push(&am.args, opts->gpg_sign_opt); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 927 | status = run_command(&am); |
| 928 | unlink(rebased_patches); |
| 929 | free(rebased_patches); |
| 930 | |
| 931 | if (!status) { |
| 932 | return move_to_original_branch(opts); |
| 933 | } |
| 934 | |
| 935 | if (is_directory(opts->state_dir)) |
Phillip Wood | 28dc09d | 2019-04-17 15:30:35 +0100 | [diff] [blame] | 936 | rebase_write_basic_state(opts); |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 937 | |
| 938 | return status; |
| 939 | } |
| 940 | |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 941 | static int run_specific_rebase(struct rebase_options *opts, enum action action) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 942 | { |
| 943 | const char *argv[] = { NULL, NULL }; |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 944 | struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 945 | int status; |
| 946 | const char *backend, *backend_func; |
| 947 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 948 | if (opts->type == REBASE_MERGE) { |
| 949 | /* Run sequencer-based rebase */ |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 950 | setenv("GIT_CHERRY_PICK_HELP", resolvemsg, 1); |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 951 | if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 952 | setenv("GIT_SEQUENCE_EDITOR", ":", 1); |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 953 | opts->autosquash = 0; |
| 954 | } |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 955 | if (opts->gpg_sign_opt) { |
| 956 | /* remove the leading "-S" */ |
| 957 | char *tmp = xstrdup(opts->gpg_sign_opt + 2); |
| 958 | free(opts->gpg_sign_opt); |
| 959 | opts->gpg_sign_opt = tmp; |
| 960 | } |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 961 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 962 | status = run_sequencer_rebase(opts, action); |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 963 | goto finished_rebase; |
| 964 | } |
| 965 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 966 | if (opts->type == REBASE_APPLY) { |
Johannes Schindelin | 2185362 | 2019-01-18 07:09:27 -0800 | [diff] [blame] | 967 | status = run_am(opts); |
| 968 | goto finished_rebase; |
| 969 | } |
| 970 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 971 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); |
| 972 | add_var(&script_snippet, "state_dir", opts->state_dir); |
| 973 | |
| 974 | add_var(&script_snippet, "upstream_name", opts->upstream_name); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 975 | add_var(&script_snippet, "upstream", opts->upstream ? |
| 976 | oid_to_hex(&opts->upstream->object.oid) : NULL); |
Pratik Karki | d4c569f | 2018-09-04 14:27:20 -0700 | [diff] [blame] | 977 | add_var(&script_snippet, "head_name", |
| 978 | opts->head_name ? opts->head_name : "detached HEAD"); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 979 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 980 | add_var(&script_snippet, "onto", opts->onto ? |
| 981 | oid_to_hex(&opts->onto->object.oid) : NULL); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 982 | add_var(&script_snippet, "onto_name", opts->onto_name); |
| 983 | add_var(&script_snippet, "revisions", opts->revisions); |
| 984 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? |
| 985 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 986 | sq_quote_argv_pretty(&buf, opts->git_am_opts.v); |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 987 | add_var(&script_snippet, "git_am_opt", buf.buf); |
| 988 | strbuf_release(&buf); |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 989 | add_var(&script_snippet, "verbose", |
| 990 | opts->flags & REBASE_VERBOSE ? "t" : ""); |
| 991 | add_var(&script_snippet, "diffstat", |
| 992 | opts->flags & REBASE_DIFFSTAT ? "t" : ""); |
Pratik Karki | 1ed9c14 | 2018-09-04 14:27:17 -0700 | [diff] [blame] | 993 | add_var(&script_snippet, "force_rebase", |
| 994 | opts->flags & REBASE_FORCE ? "t" : ""); |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 995 | if (opts->switch_to) |
| 996 | add_var(&script_snippet, "switch_to", opts->switch_to); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 997 | add_var(&script_snippet, "action", opts->action ? opts->action : ""); |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 998 | add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : ""); |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 999 | add_var(&script_snippet, "allow_rerere_autoupdate", |
Pratik Karki | ead98c1 | 2018-09-04 14:59:52 -0700 | [diff] [blame] | 1000 | opts->allow_rerere_autoupdate ? |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 1001 | opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ? |
| 1002 | "--rerere-autoupdate" : "--no-rerere-autoupdate" : ""); |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 1003 | add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); |
Pratik Karki | 051910a | 2018-09-04 14:59:58 -0700 | [diff] [blame] | 1004 | add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1005 | add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 1006 | add_var(&script_snippet, "cmd", opts->cmd); |
Pratik Karki | 9b3a448 | 2018-09-04 15:00:05 -0700 | [diff] [blame] | 1007 | add_var(&script_snippet, "allow_empty_message", |
| 1008 | opts->allow_empty_message ? "--allow-empty-message" : ""); |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 1009 | add_var(&script_snippet, "rebase_merges", |
| 1010 | opts->rebase_merges ? "t" : ""); |
| 1011 | add_var(&script_snippet, "rebase_cousins", |
| 1012 | opts->rebase_cousins ? "t" : ""); |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1013 | add_var(&script_snippet, "strategy", opts->strategy); |
| 1014 | add_var(&script_snippet, "strategy_opts", opts->strategy_opts); |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1015 | add_var(&script_snippet, "rebase_root", opts->root ? "t" : ""); |
| 1016 | add_var(&script_snippet, "squash_onto", |
| 1017 | opts->squash_onto ? oid_to_hex(opts->squash_onto) : ""); |
Pratik Karki | cda614e | 2018-08-08 21:21:33 +0545 | [diff] [blame] | 1018 | add_var(&script_snippet, "git_format_patch_opt", |
| 1019 | opts->git_format_patch_opt.buf); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1020 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1021 | if (is_merge(opts) && |
Pratik Karki | 3dba9d0 | 2018-08-08 21:21:34 +0545 | [diff] [blame] | 1022 | !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) { |
| 1023 | strbuf_addstr(&script_snippet, |
Phillip Wood | 891d4a0 | 2019-01-28 10:27:56 +0000 | [diff] [blame] | 1024 | "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; "); |
Pratik Karki | 3dba9d0 | 2018-08-08 21:21:34 +0545 | [diff] [blame] | 1025 | opts->autosquash = 0; |
| 1026 | } |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1027 | |
| 1028 | switch (opts->type) { |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1029 | case REBASE_PRESERVE_MERGES: |
| 1030 | backend = "git-rebase--preserve-merges"; |
| 1031 | backend_func = "git_rebase__preserve_merges"; |
| 1032 | break; |
| 1033 | default: |
| 1034 | BUG("Unhandled rebase type %d", opts->type); |
| 1035 | break; |
| 1036 | } |
| 1037 | |
| 1038 | strbuf_addf(&script_snippet, |
Johannes Schindelin | 082ef75 | 2019-05-14 04:22:34 -0700 | [diff] [blame] | 1039 | ". git-sh-setup && . %s && %s", backend, backend_func); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1040 | argv[0] = script_snippet.buf; |
| 1041 | |
| 1042 | status = run_command_v_opt(argv, RUN_USING_SHELL); |
Johannes Schindelin | bc24382 | 2018-10-05 08:54:38 -0700 | [diff] [blame] | 1043 | finished_rebase: |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1044 | if (opts->dont_finish_rebase) |
| 1045 | ; /* do nothing */ |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1046 | else if (opts->type == REBASE_MERGE) |
| 1047 | ; /* merge backend cleans up after itself */ |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1048 | else if (status == 0) { |
| 1049 | if (!file_exists(state_dir_path("stopped-sha", opts))) |
| 1050 | finish_rebase(opts); |
| 1051 | } else if (status == 2) { |
| 1052 | struct strbuf dir = STRBUF_INIT; |
| 1053 | |
Denton Liu | 86ed00a | 2020-04-07 10:27:58 -0400 | [diff] [blame] | 1054 | apply_autostash(state_dir_path("autostash", opts)); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1055 | strbuf_addstr(&dir, opts->state_dir); |
| 1056 | remove_dir_recursively(&dir, 0); |
| 1057 | strbuf_release(&dir); |
| 1058 | die("Nothing to do"); |
| 1059 | } |
| 1060 | |
| 1061 | strbuf_release(&script_snippet); |
| 1062 | |
| 1063 | return status ? -1 : 0; |
| 1064 | } |
| 1065 | |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1066 | static int rebase_config(const char *var, const char *value, void *data) |
| 1067 | { |
| 1068 | struct rebase_options *opts = data; |
| 1069 | |
| 1070 | if (!strcmp(var, "rebase.stat")) { |
| 1071 | if (git_config_bool(var, value)) |
| 1072 | opts->flags |= REBASE_DIFFSTAT; |
| 1073 | else |
Johannes Schindelin | 4c785c0 | 2019-05-21 10:50:20 -0700 | [diff] [blame] | 1074 | opts->flags &= ~REBASE_DIFFSTAT; |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
| 1077 | |
Pratik Karki | 051910a | 2018-09-04 14:59:58 -0700 | [diff] [blame] | 1078 | if (!strcmp(var, "rebase.autosquash")) { |
| 1079 | opts->autosquash = git_config_bool(var, value); |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1083 | if (!strcmp(var, "commit.gpgsign")) { |
| 1084 | free(opts->gpg_sign_opt); |
| 1085 | opts->gpg_sign_opt = git_config_bool(var, value) ? |
| 1086 | xstrdup("-S") : NULL; |
| 1087 | return 0; |
| 1088 | } |
| 1089 | |
Pratik Karki | 6defce2 | 2018-09-04 15:00:02 -0700 | [diff] [blame] | 1090 | if (!strcmp(var, "rebase.autostash")) { |
| 1091 | opts->autostash = git_config_bool(var, value); |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Johannes Schindelin | 969de3f | 2018-12-10 11:04:59 -0800 | [diff] [blame] | 1095 | if (!strcmp(var, "rebase.reschedulefailedexec")) { |
| 1096 | opts->reschedule_failed_exec = git_config_bool(var, value); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 1100 | if (!strcmp(var, "rebase.forkpoint")) { |
| 1101 | opts->fork_point = git_config_bool(var, value) ? -1 : 0; |
| 1102 | return 0; |
| 1103 | } |
| 1104 | |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1105 | if (!strcmp(var, "rebase.backend")) { |
| 1106 | return git_config_string(&opts->default_backend, var, value); |
| 1107 | } |
| 1108 | |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1109 | return git_default_config(var, value, data); |
| 1110 | } |
| 1111 | |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1112 | /* |
| 1113 | * Determines whether the commits in from..to are linear, i.e. contain |
| 1114 | * no merge commits. This function *expects* `from` to be an ancestor of |
| 1115 | * `to`. |
| 1116 | */ |
| 1117 | static int is_linear_history(struct commit *from, struct commit *to) |
| 1118 | { |
| 1119 | while (to && to != from) { |
| 1120 | parse_commit(to); |
| 1121 | if (!to->parents) |
| 1122 | return 1; |
| 1123 | if (to->parents->next) |
| 1124 | return 0; |
| 1125 | to = to->parents->item; |
| 1126 | } |
| 1127 | return 1; |
| 1128 | } |
| 1129 | |
Denton Liu | c0efb4c | 2019-08-27 01:37:59 -0400 | [diff] [blame] | 1130 | static int can_fast_forward(struct commit *onto, struct commit *upstream, |
Denton Liu | 4effc5b | 2019-08-27 01:38:01 -0400 | [diff] [blame] | 1131 | struct commit *restrict_revision, |
Denton Liu | c0efb4c | 2019-08-27 01:37:59 -0400 | [diff] [blame] | 1132 | struct object_id *head_oid, struct object_id *merge_base) |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1133 | { |
| 1134 | struct commit *head = lookup_commit(the_repository, head_oid); |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1135 | struct commit_list *merge_bases = NULL; |
| 1136 | int res = 0; |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (!head) |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1139 | goto done; |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1140 | |
| 1141 | merge_bases = get_merge_bases(onto, head); |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1142 | if (!merge_bases || merge_bases->next) { |
brian m. carlson | 1422844 | 2021-04-26 01:02:56 +0000 | [diff] [blame] | 1143 | oidcpy(merge_base, null_oid()); |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1144 | goto done; |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1145 | } |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1146 | |
| 1147 | oidcpy(merge_base, &merge_bases->item->object.oid); |
| 1148 | if (!oideq(merge_base, &onto->object.oid)) |
| 1149 | goto done; |
| 1150 | |
Denton Liu | 4effc5b | 2019-08-27 01:38:01 -0400 | [diff] [blame] | 1151 | if (restrict_revision && !oideq(&restrict_revision->object.oid, merge_base)) |
| 1152 | goto done; |
| 1153 | |
Denton Liu | c0efb4c | 2019-08-27 01:37:59 -0400 | [diff] [blame] | 1154 | if (!upstream) |
| 1155 | goto done; |
| 1156 | |
| 1157 | free_commit_list(merge_bases); |
| 1158 | merge_bases = get_merge_bases(upstream, head); |
| 1159 | if (!merge_bases || merge_bases->next) |
| 1160 | goto done; |
| 1161 | |
| 1162 | if (!oideq(&onto->object.oid, &merge_bases->item->object.oid)) |
| 1163 | goto done; |
| 1164 | |
Denton Liu | 2b318aa | 2019-08-27 01:37:56 -0400 | [diff] [blame] | 1165 | res = 1; |
| 1166 | |
| 1167 | done: |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1168 | free_commit_list(merge_bases); |
| 1169 | return res && is_linear_history(onto, head); |
| 1170 | } |
| 1171 | |
Elijah Newren | 52eb738 | 2020-02-15 21:36:34 +0000 | [diff] [blame] | 1172 | static int parse_opt_am(const struct option *opt, const char *arg, int unset) |
| 1173 | { |
| 1174 | struct rebase_options *opts = opt->value; |
| 1175 | |
| 1176 | BUG_ON_OPT_NEG(unset); |
| 1177 | BUG_ON_OPT_ARG(arg); |
| 1178 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1179 | opts->type = REBASE_APPLY; |
Elijah Newren | 52eb738 | 2020-02-15 21:36:34 +0000 | [diff] [blame] | 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 1184 | /* -i followed by -m is still -i */ |
| 1185 | static int parse_opt_merge(const struct option *opt, const char *arg, int unset) |
| 1186 | { |
| 1187 | struct rebase_options *opts = opt->value; |
| 1188 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 1189 | BUG_ON_OPT_NEG(unset); |
| 1190 | BUG_ON_OPT_ARG(arg); |
| 1191 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1192 | if (!is_merge(opts)) |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 1193 | opts->type = REBASE_MERGE; |
| 1194 | |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
| 1198 | /* -i followed by -p is still explicitly interactive, but -p alone is not */ |
| 1199 | static int parse_opt_interactive(const struct option *opt, const char *arg, |
| 1200 | int unset) |
| 1201 | { |
| 1202 | struct rebase_options *opts = opt->value; |
| 1203 | |
Jeff King | 517fe80 | 2018-11-05 01:45:42 -0500 | [diff] [blame] | 1204 | BUG_ON_OPT_NEG(unset); |
| 1205 | BUG_ON_OPT_ARG(arg); |
| 1206 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1207 | opts->type = REBASE_MERGE; |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 1208 | opts->flags |= REBASE_INTERACTIVE_EXPLICIT; |
| 1209 | |
| 1210 | return 0; |
| 1211 | } |
| 1212 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1213 | static enum empty_type parse_empty_value(const char *value) |
| 1214 | { |
| 1215 | if (!strcasecmp(value, "drop")) |
| 1216 | return EMPTY_DROP; |
| 1217 | else if (!strcasecmp(value, "keep")) |
| 1218 | return EMPTY_KEEP; |
| 1219 | else if (!strcasecmp(value, "ask")) |
| 1220 | return EMPTY_ASK; |
| 1221 | |
| 1222 | die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value); |
| 1223 | } |
| 1224 | |
| 1225 | static int parse_opt_empty(const struct option *opt, const char *arg, int unset) |
| 1226 | { |
| 1227 | struct rebase_options *options = opt->value; |
| 1228 | enum empty_type value = parse_empty_value(arg); |
| 1229 | |
| 1230 | BUG_ON_OPT_NEG(unset); |
| 1231 | |
| 1232 | options->empty = value; |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
Pratik Karki | 8f5986d | 2018-08-08 21:21:30 +0545 | [diff] [blame] | 1236 | static void NORETURN error_on_missing_default_upstream(void) |
| 1237 | { |
| 1238 | struct branch *current_branch = branch_get(NULL); |
| 1239 | |
| 1240 | printf(_("%s\n" |
| 1241 | "Please specify which branch you want to rebase against.\n" |
| 1242 | "See git-rebase(1) for details.\n" |
| 1243 | "\n" |
| 1244 | " git rebase '<branch>'\n" |
| 1245 | "\n"), |
| 1246 | current_branch ? _("There is no tracking information for " |
| 1247 | "the current branch.") : |
| 1248 | _("You are not currently on a branch.")); |
| 1249 | |
| 1250 | if (current_branch) { |
| 1251 | const char *remote = current_branch->remote_name; |
| 1252 | |
| 1253 | if (!remote) |
| 1254 | remote = _("<remote>"); |
| 1255 | |
| 1256 | printf(_("If you wish to set tracking information for this " |
| 1257 | "branch you can do so with:\n" |
| 1258 | "\n" |
| 1259 | " git branch --set-upstream-to=%s/<branch> %s\n" |
| 1260 | "\n"), |
| 1261 | remote, current_branch->name); |
| 1262 | } |
| 1263 | exit(1); |
| 1264 | } |
| 1265 | |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1266 | static void set_reflog_action(struct rebase_options *options) |
| 1267 | { |
| 1268 | const char *env; |
| 1269 | struct strbuf buf = STRBUF_INIT; |
| 1270 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1271 | if (!is_merge(options)) |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1272 | return; |
| 1273 | |
| 1274 | env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); |
| 1275 | if (env && strcmp("rebase", env)) |
| 1276 | return; /* only override it if it is "rebase" */ |
| 1277 | |
Elijah Newren | c2417d3 | 2020-02-15 21:36:36 +0000 | [diff] [blame] | 1278 | strbuf_addf(&buf, "rebase (%s)", options->action); |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1279 | setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1); |
| 1280 | strbuf_release(&buf); |
| 1281 | } |
| 1282 | |
Phillip Wood | c762aad | 2019-01-29 18:43:27 +0000 | [diff] [blame] | 1283 | static int check_exec_cmd(const char *cmd) |
| 1284 | { |
| 1285 | if (strchr(cmd, '\n')) |
| 1286 | return error(_("exec commands cannot contain newlines")); |
| 1287 | |
| 1288 | /* Does the command consist purely of whitespace? */ |
| 1289 | if (!cmd[strspn(cmd, " \t\r\f\v")]) |
| 1290 | return error(_("empty exec command")); |
| 1291 | |
| 1292 | return 0; |
| 1293 | } |
| 1294 | |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 1295 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
| 1296 | { |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 1297 | struct rebase_options options = REBASE_OPTIONS_INIT; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1298 | const char *branch_name; |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1299 | int ret, flags, total_argc, in_progress = 0; |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 1300 | int keep_base = 0; |
Pratik Karki | 06e4775 | 2018-09-04 14:27:10 -0700 | [diff] [blame] | 1301 | int ok_to_skip_pre_rebase = 0; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1302 | struct strbuf msg = STRBUF_INIT; |
| 1303 | struct strbuf revisions = STRBUF_INIT; |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 1304 | struct strbuf buf = STRBUF_INIT; |
Pratik Karki | 075bc85 | 2018-09-04 14:27:09 -0700 | [diff] [blame] | 1305 | struct object_id merge_base; |
Rohit Ashiwal | ef484ad | 2020-07-13 11:10:41 +0100 | [diff] [blame] | 1306 | int ignore_whitespace = 0; |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 1307 | enum action action = ACTION_NONE; |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1308 | const char *gpg_sign = NULL; |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 1309 | struct string_list exec = STRING_LIST_INIT_NODUP; |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 1310 | const char *rebase_merges = NULL; |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1311 | struct string_list strategy_options = STRING_LIST_INIT_NODUP; |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1312 | struct object_id squash_onto; |
| 1313 | char *squash_onto_name = NULL; |
Johannes Schindelin | 906b639 | 2019-07-01 04:58:15 -0700 | [diff] [blame] | 1314 | int reschedule_failed_exec = -1; |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1315 | int allow_preemptive_ff = 1; |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1316 | struct option builtin_rebase_options[] = { |
| 1317 | OPT_STRING(0, "onto", &options.onto_name, |
| 1318 | N_("revision"), |
| 1319 | N_("rebase onto given branch instead of upstream")), |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 1320 | OPT_BOOL(0, "keep-base", &keep_base, |
| 1321 | N_("use the merge-base of upstream and branch as the current base")), |
Pratik Karki | 06e4775 | 2018-09-04 14:27:10 -0700 | [diff] [blame] | 1322 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, |
| 1323 | N_("allow pre-rebase hook to run")), |
Pratik Karki | b4c8eb0 | 2018-09-04 14:27:12 -0700 | [diff] [blame] | 1324 | OPT_NEGBIT('q', "quiet", &options.flags, |
| 1325 | N_("be quiet. implies --no-stat"), |
Elijah Newren | 55d2b6d | 2020-02-15 21:36:28 +0000 | [diff] [blame] | 1326 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1327 | OPT_BIT('v', "verbose", &options.flags, |
| 1328 | N_("display a diffstat of what changed upstream"), |
| 1329 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), |
| 1330 | {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, |
| 1331 | N_("do not show diffstat of what changed upstream"), |
| 1332 | PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 1333 | OPT_BOOL(0, "signoff", &options.signoff, |
Bradley M. Kuhn | 3abd4a6 | 2020-10-19 18:03:55 -0700 | [diff] [blame] | 1334 | N_("add a Signed-off-by trailer to each commit")), |
Phillip Wood | 7573cec | 2020-08-17 18:40:02 +0100 | [diff] [blame] | 1335 | OPT_BOOL(0, "committer-date-is-author-date", |
| 1336 | &options.committer_date_is_author_date, |
| 1337 | N_("make committer date match author date")), |
Rohit Ashiwal | 2712669 | 2020-08-17 18:40:04 +0100 | [diff] [blame] | 1338 | OPT_BOOL(0, "reset-author-date", &options.ignore_date, |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 1339 | N_("ignore author date and use current date")), |
Rohit Ashiwal | 2712669 | 2020-08-17 18:40:04 +0100 | [diff] [blame] | 1340 | OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date, |
| 1341 | N_("synonym of --reset-author-date")), |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1342 | OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"), |
| 1343 | N_("passed to 'git apply'"), 0), |
Rohit Ashiwal | ef484ad | 2020-07-13 11:10:41 +0100 | [diff] [blame] | 1344 | OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace, |
| 1345 | N_("ignore changes in whitespace")), |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1346 | OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts, |
| 1347 | N_("action"), N_("passed to 'git apply'"), 0), |
Pratik Karki | 1ed9c14 | 2018-09-04 14:27:17 -0700 | [diff] [blame] | 1348 | OPT_BIT('f', "force-rebase", &options.flags, |
| 1349 | N_("cherry-pick all commits, even if unchanged"), |
| 1350 | REBASE_FORCE), |
| 1351 | OPT_BIT(0, "no-ff", &options.flags, |
| 1352 | N_("cherry-pick all commits, even if unchanged"), |
| 1353 | REBASE_FORCE), |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1354 | OPT_CMDMODE(0, "continue", &action, N_("continue"), |
| 1355 | ACTION_CONTINUE), |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1356 | OPT_CMDMODE(0, "skip", &action, |
| 1357 | N_("skip current patch and continue"), ACTION_SKIP), |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1358 | OPT_CMDMODE(0, "abort", &action, |
| 1359 | N_("abort and check out the original branch"), |
| 1360 | ACTION_ABORT), |
Pratik Karki | 5a61494 | 2018-08-08 20:51:19 +0545 | [diff] [blame] | 1361 | OPT_CMDMODE(0, "quit", &action, |
| 1362 | N_("abort but keep HEAD where it is"), ACTION_QUIT), |
Pratik Karki | 51e9ea6 | 2018-08-08 20:51:20 +0545 | [diff] [blame] | 1363 | OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list " |
| 1364 | "during an interactive rebase"), ACTION_EDIT_TODO), |
| 1365 | OPT_CMDMODE(0, "show-current-patch", &action, |
| 1366 | N_("show the patch file being applied or merged"), |
| 1367 | ACTION_SHOW_CURRENT_PATCH), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1368 | OPT_CALLBACK_F(0, "apply", &options, NULL, |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1369 | N_("use apply strategies to rebase"), |
Elijah Newren | 52eb738 | 2020-02-15 21:36:34 +0000 | [diff] [blame] | 1370 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1371 | parse_opt_am), |
| 1372 | OPT_CALLBACK_F('m', "merge", &options, NULL, |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 1373 | N_("use merging strategies to rebase"), |
| 1374 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1375 | parse_opt_merge), |
| 1376 | OPT_CALLBACK_F('i', "interactive", &options, NULL, |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 1377 | N_("let the user edit the list of commits to rebase"), |
| 1378 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1379 | parse_opt_interactive), |
Denton Liu | feebd2d | 2019-10-18 16:55:56 -0700 | [diff] [blame] | 1380 | OPT_SET_INT_F('p', "preserve-merges", &options.type, |
| 1381 | N_("(DEPRECATED) try to recreate merges instead of " |
| 1382 | "ignoring them"), |
| 1383 | REBASE_PRESERVE_MERGES, PARSE_OPT_HIDDEN), |
Phillip Wood | 6023c92 | 2019-04-17 15:30:36 +0100 | [diff] [blame] | 1384 | OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate), |
Jiang Xin | 937d143 | 2020-03-11 14:55:27 +0800 | [diff] [blame] | 1385 | OPT_CALLBACK_F(0, "empty", &options, "{drop,keep,ask}", |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1386 | N_("how to handle commits that become empty"), |
| 1387 | PARSE_OPT_NONEG, parse_opt_empty), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1388 | OPT_CALLBACK_F('k', "keep-empty", &options, NULL, |
Elijah Newren | b9cbd29 | 2020-04-11 02:44:25 +0000 | [diff] [blame] | 1389 | N_("keep commits which start empty"), |
Elijah Newren | d48e5e2 | 2020-02-15 21:36:24 +0000 | [diff] [blame] | 1390 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 1391 | parse_opt_keep_empty), |
Pratik Karki | 051910a | 2018-09-04 14:59:58 -0700 | [diff] [blame] | 1392 | OPT_BOOL(0, "autosquash", &options.autosquash, |
| 1393 | N_("move commits that begin with " |
| 1394 | "squash!/fixup! under -i")), |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1395 | { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"), |
| 1396 | N_("GPG-sign commits"), |
| 1397 | PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, |
Denton Liu | a03b555 | 2020-04-07 10:28:07 -0400 | [diff] [blame] | 1398 | OPT_AUTOSTASH(&options.autostash), |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 1399 | OPT_STRING_LIST('x', "exec", &exec, N_("exec"), |
| 1400 | N_("add exec lines after each commit of the " |
| 1401 | "editable list")), |
Elijah Newren | 22a69fd | 2020-01-16 06:14:15 +0000 | [diff] [blame] | 1402 | OPT_BOOL_F(0, "allow-empty-message", |
| 1403 | &options.allow_empty_message, |
| 1404 | N_("allow rebasing commits with empty messages"), |
| 1405 | PARSE_OPT_HIDDEN), |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 1406 | {OPTION_STRING, 'r', "rebase-merges", &rebase_merges, |
| 1407 | N_("mode"), |
| 1408 | N_("try to rebase merges instead of skipping them"), |
| 1409 | PARSE_OPT_OPTARG, NULL, (intptr_t)""}, |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 1410 | OPT_BOOL(0, "fork-point", &options.fork_point, |
Pratik Karki | 92d0d74 | 2018-09-04 15:00:09 -0700 | [diff] [blame] | 1411 | N_("use 'merge-base --fork-point' to refine upstream")), |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1412 | OPT_STRING('s', "strategy", &options.strategy, |
| 1413 | N_("strategy"), N_("use the given merge strategy")), |
| 1414 | OPT_STRING_LIST('X', "strategy-option", &strategy_options, |
| 1415 | N_("option"), |
| 1416 | N_("pass the argument through to the merge " |
| 1417 | "strategy")), |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1418 | OPT_BOOL(0, "root", &options.root, |
| 1419 | N_("rebase all reachable commits up to the root(s)")), |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 1420 | OPT_BOOL(0, "reschedule-failed-exec", |
Johannes Schindelin | 906b639 | 2019-07-01 04:58:15 -0700 | [diff] [blame] | 1421 | &reschedule_failed_exec, |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 1422 | N_("automatically re-schedule any `exec` that fails")), |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 1423 | OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks, |
| 1424 | N_("apply all changes, even those already present upstream")), |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1425 | OPT_END(), |
| 1426 | }; |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1427 | int i; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1428 | |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1429 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 1430 | usage_with_options(builtin_rebase_usage, |
| 1431 | builtin_rebase_options); |
| 1432 | |
Phillip Wood | 73fdc53 | 2019-04-17 15:30:41 +0100 | [diff] [blame] | 1433 | options.allow_empty_message = 1; |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1434 | git_config(rebase_config, &options); |
Đoàn Trần Công Danh | c241371 | 2020-04-03 17:28:02 +0700 | [diff] [blame] | 1435 | /* options.gpg_sign_opt will be either "-S" or NULL */ |
| 1436 | gpg_sign = options.gpg_sign_opt ? "" : NULL; |
| 1437 | FREE_AND_NULL(options.gpg_sign_opt); |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 1438 | |
Pratik Karki | 0eabf4b | 2018-08-08 20:51:22 +0545 | [diff] [blame] | 1439 | strbuf_reset(&buf); |
| 1440 | strbuf_addf(&buf, "%s/applying", apply_dir()); |
| 1441 | if(file_exists(buf.buf)) |
| 1442 | die(_("It looks like 'git am' is in progress. Cannot rebase.")); |
| 1443 | |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 1444 | if (is_directory(apply_dir())) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1445 | options.type = REBASE_APPLY; |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 1446 | options.state_dir = apply_dir(); |
| 1447 | } else if (is_directory(merge_dir())) { |
| 1448 | strbuf_reset(&buf); |
| 1449 | strbuf_addf(&buf, "%s/rewritten", merge_dir()); |
| 1450 | if (is_directory(buf.buf)) { |
| 1451 | options.type = REBASE_PRESERVE_MERGES; |
| 1452 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; |
| 1453 | } else { |
| 1454 | strbuf_reset(&buf); |
| 1455 | strbuf_addf(&buf, "%s/interactive", merge_dir()); |
| 1456 | if(file_exists(buf.buf)) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1457 | options.type = REBASE_MERGE; |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 1458 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; |
| 1459 | } else |
| 1460 | options.type = REBASE_MERGE; |
| 1461 | } |
| 1462 | options.state_dir = merge_dir(); |
| 1463 | } |
| 1464 | |
| 1465 | if (options.type != REBASE_UNSPECIFIED) |
| 1466 | in_progress = 1; |
| 1467 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1468 | total_argc = argc; |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1469 | argc = parse_options(argc, argv, prefix, |
| 1470 | builtin_rebase_options, |
| 1471 | builtin_rebase_usage, 0); |
| 1472 | |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 1473 | if (action != ACTION_NONE && total_argc != 2) { |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1474 | usage_with_options(builtin_rebase_usage, |
| 1475 | builtin_rebase_options); |
| 1476 | } |
| 1477 | |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1478 | if (argc > 2) |
| 1479 | usage_with_options(builtin_rebase_usage, |
| 1480 | builtin_rebase_options); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1481 | |
Johannes Schindelin | 427c3bd | 2019-03-11 12:57:35 -0700 | [diff] [blame] | 1482 | if (options.type == REBASE_PRESERVE_MERGES) |
| 1483 | warning(_("git rebase --preserve-merges is deprecated. " |
| 1484 | "Use --rebase-merges instead.")); |
| 1485 | |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 1486 | if (keep_base) { |
| 1487 | if (options.onto_name) |
| 1488 | die(_("cannot combine '--keep-base' with '--onto'")); |
| 1489 | if (options.root) |
| 1490 | die(_("cannot combine '--keep-base' with '--root'")); |
| 1491 | } |
| 1492 | |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 1493 | if (options.root && options.fork_point > 0) |
Elijah Newren | a35413c | 2020-04-27 17:59:49 +0000 | [diff] [blame] | 1494 | die(_("cannot combine '--root' with '--fork-point'")); |
| 1495 | |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 1496 | if (action != ACTION_NONE && !in_progress) |
Pratik Karki | d732a57 | 2018-08-08 20:51:21 +0545 | [diff] [blame] | 1497 | die(_("No rebase in progress?")); |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1498 | setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0); |
Pratik Karki | d732a57 | 2018-08-08 20:51:21 +0545 | [diff] [blame] | 1499 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1500 | if (action == ACTION_EDIT_TODO && !is_merge(&options)) |
Pratik Karki | 51e9ea6 | 2018-08-08 20:51:20 +0545 | [diff] [blame] | 1501 | die(_("The --edit-todo action can only be used during " |
| 1502 | "interactive rebase.")); |
| 1503 | |
Jeff Hostetler | b3a5d5a | 2019-02-22 14:25:10 -0800 | [diff] [blame] | 1504 | if (trace2_is_enabled()) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1505 | if (is_merge(&options)) |
Jeff Hostetler | b3a5d5a | 2019-02-22 14:25:10 -0800 | [diff] [blame] | 1506 | trace2_cmd_mode("interactive"); |
| 1507 | else if (exec.nr) |
| 1508 | trace2_cmd_mode("interactive-exec"); |
| 1509 | else |
| 1510 | trace2_cmd_mode(action_names[action]); |
| 1511 | } |
| 1512 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1513 | switch (action) { |
| 1514 | case ACTION_CONTINUE: { |
| 1515 | struct object_id head; |
| 1516 | struct lock_file lock_file = LOCK_INIT; |
| 1517 | int fd; |
| 1518 | |
| 1519 | options.action = "continue"; |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1520 | set_reflog_action(&options); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1521 | |
| 1522 | /* Sanity check */ |
| 1523 | if (get_oid("HEAD", &head)) |
| 1524 | die(_("Cannot read HEAD")); |
| 1525 | |
| 1526 | fd = hold_locked_index(&lock_file, 0); |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 1527 | if (repo_read_index(the_repository) < 0) |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1528 | die(_("could not read index")); |
| 1529 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, |
| 1530 | NULL); |
| 1531 | if (0 <= fd) |
Nguyễn Thái Ngọc Duy | 1b0d968 | 2019-01-12 09:13:27 +0700 | [diff] [blame] | 1532 | repo_update_index_if_able(the_repository, &lock_file); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1533 | rollback_lock_file(&lock_file); |
| 1534 | |
Nguyễn Thái Ngọc Duy | 5b02ca3 | 2018-11-10 06:48:49 +0100 | [diff] [blame] | 1535 | if (has_unstaged_changes(the_repository, 1)) { |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1536 | puts(_("You must edit all merge conflicts and then\n" |
| 1537 | "mark them as resolved using git add")); |
| 1538 | exit(1); |
| 1539 | } |
| 1540 | if (read_basic_state(&options)) |
| 1541 | exit(1); |
| 1542 | goto run_rebase; |
| 1543 | } |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1544 | case ACTION_SKIP: { |
| 1545 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
| 1546 | |
| 1547 | options.action = "skip"; |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1548 | set_reflog_action(&options); |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1549 | |
Nguyễn Thái Ngọc Duy | 55e6b35 | 2018-11-10 06:49:09 +0100 | [diff] [blame] | 1550 | rerere_clear(the_repository, &merge_rr); |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1551 | string_list_clear(&merge_rr, 1); |
| 1552 | |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 1553 | if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD, |
| 1554 | NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1555 | die(_("could not discard worktree changes")); |
Nguyễn Thái Ngọc Duy | f4a4b9a | 2019-03-29 17:38:59 +0700 | [diff] [blame] | 1556 | remove_branch_state(the_repository, 0); |
Pratik Karki | 122420c | 2018-08-08 20:51:17 +0545 | [diff] [blame] | 1557 | if (read_basic_state(&options)) |
| 1558 | exit(1); |
| 1559 | goto run_rebase; |
| 1560 | } |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1561 | case ACTION_ABORT: { |
| 1562 | struct string_list merge_rr = STRING_LIST_INIT_DUP; |
| 1563 | options.action = "abort"; |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1564 | set_reflog_action(&options); |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1565 | |
Nguyễn Thái Ngọc Duy | 55e6b35 | 2018-11-10 06:49:09 +0100 | [diff] [blame] | 1566 | rerere_clear(the_repository, &merge_rr); |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1567 | string_list_clear(&merge_rr, 1); |
| 1568 | |
| 1569 | if (read_basic_state(&options)) |
| 1570 | exit(1); |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 1571 | if (reset_head(the_repository, &options.orig_head, "reset", |
Johannes Schindelin | bac2a1e | 2018-11-12 03:44:32 -0800 | [diff] [blame] | 1572 | options.head_name, RESET_HEAD_HARD, |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 1573 | NULL, NULL, DEFAULT_REFLOG_ACTION) < 0) |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1574 | die(_("could not move back to %s"), |
| 1575 | oid_to_hex(&options.orig_head)); |
Nguyễn Thái Ngọc Duy | f4a4b9a | 2019-03-29 17:38:59 +0700 | [diff] [blame] | 1576 | remove_branch_state(the_repository, 0); |
Phillip Wood | d3fce47 | 2019-05-14 19:03:47 +0100 | [diff] [blame] | 1577 | ret = !!finish_rebase(&options); |
Pratik Karki | 5e5d961 | 2018-08-08 20:51:18 +0545 | [diff] [blame] | 1578 | goto cleanup; |
| 1579 | } |
Pratik Karki | 5a61494 | 2018-08-08 20:51:19 +0545 | [diff] [blame] | 1580 | case ACTION_QUIT: { |
Denton Liu | 9b2df3e | 2020-04-28 05:31:31 -0400 | [diff] [blame] | 1581 | save_autostash(state_dir_path("autostash", &options)); |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1582 | if (options.type == REBASE_MERGE) { |
Phillip Wood | d559f50 | 2019-05-14 19:03:49 +0100 | [diff] [blame] | 1583 | struct replay_opts replay = REPLAY_OPTS_INIT; |
| 1584 | |
| 1585 | replay.action = REPLAY_INTERACTIVE_REBASE; |
| 1586 | ret = !!sequencer_remove_state(&replay); |
| 1587 | } else { |
| 1588 | strbuf_reset(&buf); |
| 1589 | strbuf_addstr(&buf, options.state_dir); |
| 1590 | ret = !!remove_dir_recursively(&buf, 0); |
| 1591 | if (ret) |
| 1592 | error(_("could not remove '%s'"), |
| 1593 | options.state_dir); |
| 1594 | } |
Pratik Karki | 5a61494 | 2018-08-08 20:51:19 +0545 | [diff] [blame] | 1595 | goto cleanup; |
| 1596 | } |
Pratik Karki | 51e9ea6 | 2018-08-08 20:51:20 +0545 | [diff] [blame] | 1597 | case ACTION_EDIT_TODO: |
| 1598 | options.action = "edit-todo"; |
| 1599 | options.dont_finish_rebase = 1; |
| 1600 | goto run_rebase; |
| 1601 | case ACTION_SHOW_CURRENT_PATCH: |
| 1602 | options.action = "show-current-patch"; |
| 1603 | options.dont_finish_rebase = 1; |
| 1604 | goto run_rebase; |
Phillip Wood | 297b1e1 | 2019-04-17 15:30:43 +0100 | [diff] [blame] | 1605 | case ACTION_NONE: |
Pratik Karki | 51e9ea6 | 2018-08-08 20:51:20 +0545 | [diff] [blame] | 1606 | break; |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1607 | default: |
Pratik Karki | 51e9ea6 | 2018-08-08 20:51:20 +0545 | [diff] [blame] | 1608 | BUG("action: %d", action); |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 1609 | } |
| 1610 | |
Pratik Karki | c54dacb | 2018-09-04 14:27:18 -0700 | [diff] [blame] | 1611 | /* Make sure no rebase is in progress */ |
| 1612 | if (in_progress) { |
| 1613 | const char *last_slash = strrchr(options.state_dir, '/'); |
| 1614 | const char *state_dir_base = |
| 1615 | last_slash ? last_slash + 1 : options.state_dir; |
| 1616 | const char *cmd_live_rebase = |
| 1617 | "git rebase (--continue | --abort | --skip)"; |
| 1618 | strbuf_reset(&buf); |
| 1619 | strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); |
| 1620 | die(_("It seems that there is already a %s directory, and\n" |
| 1621 | "I wonder if you are in the middle of another rebase. " |
| 1622 | "If that is the\n" |
| 1623 | "case, please try\n\t%s\n" |
| 1624 | "If that is not the case, please\n\t%s\n" |
| 1625 | "and run me again. I am stopping in case you still " |
| 1626 | "have something\n" |
| 1627 | "valuable there.\n"), |
| 1628 | state_dir_base, cmd_live_rebase, buf.buf); |
| 1629 | } |
| 1630 | |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1631 | if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) || |
| 1632 | (action != ACTION_NONE) || |
| 1633 | (exec.nr > 0) || |
| 1634 | options.autosquash) { |
| 1635 | allow_preemptive_ff = 0; |
| 1636 | } |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 1637 | if (options.committer_date_is_author_date || options.ignore_date) |
Phillip Wood | 7573cec | 2020-08-17 18:40:02 +0100 | [diff] [blame] | 1638 | options.flags |= REBASE_FORCE; |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1639 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1640 | for (i = 0; i < options.git_am_opts.nr; i++) { |
| 1641 | const char *option = options.git_am_opts.v[i], *p; |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 1642 | if (!strcmp(option, "--whitespace=fix") || |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1643 | !strcmp(option, "--whitespace=strip")) |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1644 | allow_preemptive_ff = 0; |
Johannes Schindelin | 04519d7 | 2018-11-14 08:25:31 -0800 | [diff] [blame] | 1645 | else if (skip_prefix(option, "-C", &p)) { |
| 1646 | while (*p) |
| 1647 | if (!isdigit(*(p++))) |
| 1648 | die(_("switch `C' expects a " |
| 1649 | "numerical value")); |
| 1650 | } else if (skip_prefix(option, "--whitespace=", &p)) { |
| 1651 | if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") && |
| 1652 | strcmp(p, "error") && strcmp(p, "error-all")) |
| 1653 | die("Invalid whitespace option: '%s'", p); |
| 1654 | } |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1655 | } |
| 1656 | |
Phillip Wood | c762aad | 2019-01-29 18:43:27 +0000 | [diff] [blame] | 1657 | for (i = 0; i < exec.nr; i++) |
| 1658 | if (check_exec_cmd(exec.items[i].string)) |
| 1659 | exit(1); |
| 1660 | |
Pratik Karki | b4c8eb0 | 2018-09-04 14:27:12 -0700 | [diff] [blame] | 1661 | if (!(options.flags & REBASE_NO_QUIET)) |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1662 | strvec_push(&options.git_am_opts, "-q"); |
Pratik Karki | 53f9e5b | 2018-09-04 14:59:56 -0700 | [diff] [blame] | 1663 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1664 | if (options.empty != EMPTY_UNSPECIFIED) |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1665 | imply_merge(&options, "--empty"); |
Pratik Karki | 002ee2f | 2018-09-04 14:59:57 -0700 | [diff] [blame] | 1666 | |
Jonathan Tan | 0fcb4f6 | 2020-04-11 02:44:27 +0000 | [diff] [blame] | 1667 | if (options.reapply_cherry_picks) |
| 1668 | imply_merge(&options, "--reapply-cherry-picks"); |
| 1669 | |
Đoàn Trần Công Danh | c241371 | 2020-04-03 17:28:02 +0700 | [diff] [blame] | 1670 | if (gpg_sign) |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1671 | options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 1672 | |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 1673 | if (exec.nr) { |
| 1674 | int i; |
| 1675 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1676 | imply_merge(&options, "--exec"); |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 1677 | |
| 1678 | strbuf_reset(&buf); |
| 1679 | for (i = 0; i < exec.nr; i++) |
| 1680 | strbuf_addf(&buf, "exec %s\n", exec.items[i].string); |
| 1681 | options.cmd = xstrdup(buf.buf); |
| 1682 | } |
| 1683 | |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 1684 | if (rebase_merges) { |
| 1685 | if (!*rebase_merges) |
| 1686 | ; /* default mode; do nothing */ |
| 1687 | else if (!strcmp("rebase-cousins", rebase_merges)) |
| 1688 | options.rebase_cousins = 1; |
| 1689 | else if (strcmp("no-rebase-cousins", rebase_merges)) |
| 1690 | die(_("Unknown mode: %s"), rebase_merges); |
| 1691 | options.rebase_merges = 1; |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1692 | imply_merge(&options, "--rebase-merges"); |
Pratik Karki | 3c3588c | 2018-09-04 15:00:07 -0700 | [diff] [blame] | 1693 | } |
| 1694 | |
Rohit Ashiwal | ef484ad | 2020-07-13 11:10:41 +0100 | [diff] [blame] | 1695 | if (options.type == REBASE_APPLY) { |
| 1696 | if (ignore_whitespace) |
Junio C Hamano | 9c31b19 | 2020-09-03 12:37:01 -0700 | [diff] [blame] | 1697 | strvec_push(&options.git_am_opts, |
| 1698 | "--ignore-whitespace"); |
Phillip Wood | 7573cec | 2020-08-17 18:40:02 +0100 | [diff] [blame] | 1699 | if (options.committer_date_is_author_date) |
Junio C Hamano | 9c31b19 | 2020-09-03 12:37:01 -0700 | [diff] [blame] | 1700 | strvec_push(&options.git_am_opts, |
| 1701 | "--committer-date-is-author-date"); |
Phillip Wood | a3894aa | 2020-08-17 18:40:03 +0100 | [diff] [blame] | 1702 | if (options.ignore_date) |
Junio C Hamano | 9c31b19 | 2020-09-03 12:37:01 -0700 | [diff] [blame] | 1703 | strvec_push(&options.git_am_opts, "--ignore-date"); |
Rohit Ashiwal | ef484ad | 2020-07-13 11:10:41 +0100 | [diff] [blame] | 1704 | } else { |
| 1705 | /* REBASE_MERGE and PRESERVE_MERGES */ |
| 1706 | if (ignore_whitespace) { |
| 1707 | string_list_append(&strategy_options, |
| 1708 | "ignore-space-change"); |
| 1709 | } |
| 1710 | } |
| 1711 | |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1712 | if (strategy_options.nr) { |
| 1713 | int i; |
| 1714 | |
| 1715 | if (!options.strategy) |
| 1716 | options.strategy = "recursive"; |
| 1717 | |
| 1718 | strbuf_reset(&buf); |
| 1719 | for (i = 0; i < strategy_options.nr; i++) |
| 1720 | strbuf_addf(&buf, " --%s", |
| 1721 | strategy_options.items[i].string); |
| 1722 | options.strategy_opts = xstrdup(buf.buf); |
| 1723 | } |
| 1724 | |
| 1725 | if (options.strategy) { |
| 1726 | options.strategy = xstrdup(options.strategy); |
| 1727 | switch (options.type) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1728 | case REBASE_APPLY: |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1729 | die(_("--strategy requires --merge or --interactive")); |
| 1730 | case REBASE_MERGE: |
Pratik Karki | ba1905a | 2018-09-04 15:00:11 -0700 | [diff] [blame] | 1731 | case REBASE_PRESERVE_MERGES: |
| 1732 | /* compatible */ |
| 1733 | break; |
| 1734 | case REBASE_UNSPECIFIED: |
| 1735 | options.type = REBASE_MERGE; |
| 1736 | break; |
| 1737 | default: |
| 1738 | BUG("unhandled rebase type (%d)", options.type); |
| 1739 | } |
| 1740 | } |
| 1741 | |
Elijah Newren | 68aa495 | 2018-12-11 08:11:39 -0800 | [diff] [blame] | 1742 | if (options.type == REBASE_MERGE) |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1743 | imply_merge(&options, "--merge"); |
Elijah Newren | 68aa495 | 2018-12-11 08:11:39 -0800 | [diff] [blame] | 1744 | |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1745 | if (options.root && !options.onto_name) |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1746 | imply_merge(&options, "--root without --onto"); |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1747 | |
Pratik Karki | cda614e | 2018-08-08 21:21:33 +0545 | [diff] [blame] | 1748 | if (isatty(2) && options.flags & REBASE_NO_QUIET) |
| 1749 | strbuf_addstr(&options.git_format_patch_opt, " --progress"); |
| 1750 | |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1751 | if (options.git_am_opts.nr || options.type == REBASE_APPLY) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1752 | /* all am options except -q are compatible only with --apply */ |
Jeff King | d70a9eb | 2020-07-28 20:37:20 -0400 | [diff] [blame] | 1753 | for (i = options.git_am_opts.nr - 1; i >= 0; i--) |
| 1754 | if (strcmp(options.git_am_opts.v[i], "-q")) |
Johannes Schindelin | f576968 | 2018-11-14 08:25:29 -0800 | [diff] [blame] | 1755 | break; |
Pratik Karki | b361bd7 | 2018-08-08 21:21:35 +0545 | [diff] [blame] | 1756 | |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1757 | if (i >= 0) { |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1758 | if (is_merge(&options)) |
| 1759 | die(_("cannot combine apply options with " |
| 1760 | "merge options")); |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1761 | else |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1762 | options.type = REBASE_APPLY; |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1763 | } |
Pratik Karki | b361bd7 | 2018-08-08 21:21:35 +0545 | [diff] [blame] | 1764 | } |
| 1765 | |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1766 | if (options.type == REBASE_UNSPECIFIED) { |
| 1767 | if (!strcmp(options.default_backend, "merge")) |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1768 | imply_merge(&options, "--merge"); |
| 1769 | else if (!strcmp(options.default_backend, "apply")) |
| 1770 | options.type = REBASE_APPLY; |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1771 | else |
| 1772 | die(_("Unknown rebase backend: %s"), |
| 1773 | options.default_backend); |
Elijah Newren | 8af14f0 | 2020-02-15 21:36:33 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
Elijah Newren | 14c4586 | 2020-11-02 23:45:34 +0000 | [diff] [blame] | 1776 | if (options.type == REBASE_MERGE && |
| 1777 | !options.strategy && |
| 1778 | getenv("GIT_TEST_MERGE_ALGORITHM")) |
| 1779 | options.strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM")); |
| 1780 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1781 | switch (options.type) { |
| 1782 | case REBASE_MERGE: |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1783 | case REBASE_PRESERVE_MERGES: |
| 1784 | options.state_dir = merge_dir(); |
| 1785 | break; |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1786 | case REBASE_APPLY: |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1787 | options.state_dir = apply_dir(); |
| 1788 | break; |
| 1789 | default: |
Elijah Newren | 8295ed6 | 2020-02-15 21:36:39 +0000 | [diff] [blame] | 1790 | BUG("options.type was just set above; should be unreachable."); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1791 | } |
| 1792 | |
Elijah Newren | e98c426 | 2020-02-15 21:36:25 +0000 | [diff] [blame] | 1793 | if (options.empty == EMPTY_UNSPECIFIED) { |
| 1794 | if (options.flags & REBASE_INTERACTIVE_EXPLICIT) |
| 1795 | options.empty = EMPTY_ASK; |
| 1796 | else if (exec.nr > 0) |
| 1797 | options.empty = EMPTY_KEEP; |
| 1798 | else |
| 1799 | options.empty = EMPTY_DROP; |
| 1800 | } |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 1801 | if (reschedule_failed_exec > 0 && !is_merge(&options)) |
Johannes Schindelin | 906b639 | 2019-07-01 04:58:15 -0700 | [diff] [blame] | 1802 | die(_("--reschedule-failed-exec requires " |
| 1803 | "--exec or --interactive")); |
| 1804 | if (reschedule_failed_exec >= 0) |
| 1805 | options.reschedule_failed_exec = reschedule_failed_exec; |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 1806 | |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 1807 | if (options.signoff) { |
| 1808 | if (options.type == REBASE_PRESERVE_MERGES) |
| 1809 | die("cannot combine '--signoff' with " |
| 1810 | "'--preserve-merges'"); |
Jeff King | 22f9b7f | 2020-07-28 16:24:27 -0400 | [diff] [blame] | 1811 | strvec_push(&options.git_am_opts, "--signoff"); |
Pratik Karki | 73d51ed | 2018-09-04 14:59:50 -0700 | [diff] [blame] | 1812 | options.flags |= REBASE_FORCE; |
| 1813 | } |
| 1814 | |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 1815 | if (options.type == REBASE_PRESERVE_MERGES) { |
Pratik Karki | b361bd7 | 2018-08-08 21:21:35 +0545 | [diff] [blame] | 1816 | /* |
| 1817 | * Note: incompatibility with --signoff handled in signoff block above |
| 1818 | * Note: incompatibility with --interactive is just a strong warning; |
| 1819 | * git-rebase.txt caveats with "unless you know what you are doing" |
| 1820 | */ |
| 1821 | if (options.rebase_merges) |
Elijah Newren | c913c59 | 2018-12-11 08:11:32 -0800 | [diff] [blame] | 1822 | die(_("cannot combine '--preserve-merges' with " |
Pratik Karki | b361bd7 | 2018-08-08 21:21:35 +0545 | [diff] [blame] | 1823 | "'--rebase-merges'")); |
| 1824 | |
Johannes Schindelin | d421afa | 2018-12-10 11:04:58 -0800 | [diff] [blame] | 1825 | if (options.reschedule_failed_exec) |
| 1826 | die(_("error: cannot combine '--preserve-merges' with " |
| 1827 | "'--reschedule-failed-exec'")); |
| 1828 | } |
| 1829 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1830 | if (!options.root) { |
Pratik Karki | 8f5986d | 2018-08-08 21:21:30 +0545 | [diff] [blame] | 1831 | if (argc < 1) { |
| 1832 | struct branch *branch; |
| 1833 | |
| 1834 | branch = branch_get(NULL); |
| 1835 | options.upstream_name = branch_get_upstream(branch, |
| 1836 | NULL); |
| 1837 | if (!options.upstream_name) |
| 1838 | error_on_missing_default_upstream(); |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 1839 | if (options.fork_point < 0) |
| 1840 | options.fork_point = 1; |
Pratik Karki | 8f5986d | 2018-08-08 21:21:30 +0545 | [diff] [blame] | 1841 | } else { |
Pratik Karki | f28d40d | 2018-09-04 14:27:07 -0700 | [diff] [blame] | 1842 | options.upstream_name = argv[0]; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1843 | argc--; |
| 1844 | argv++; |
| 1845 | if (!strcmp(options.upstream_name, "-")) |
| 1846 | options.upstream_name = "@{-1}"; |
| 1847 | } |
| 1848 | options.upstream = peel_committish(options.upstream_name); |
| 1849 | if (!options.upstream) |
| 1850 | die(_("invalid upstream '%s'"), options.upstream_name); |
Pratik Karki | 06e4775 | 2018-09-04 14:27:10 -0700 | [diff] [blame] | 1851 | options.upstream_arg = options.upstream_name; |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1852 | } else { |
| 1853 | if (!options.onto_name) { |
| 1854 | if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, |
| 1855 | &squash_onto, NULL, NULL) < 0) |
| 1856 | die(_("Could not create new root commit")); |
| 1857 | options.squash_onto = &squash_onto; |
| 1858 | options.onto_name = squash_onto_name = |
| 1859 | xstrdup(oid_to_hex(&squash_onto)); |
Johannes Schindelin | e1fac53 | 2019-07-31 08:18:49 -0700 | [diff] [blame] | 1860 | } else |
| 1861 | options.root_with_onto = 1; |
| 1862 | |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 1863 | options.upstream_name = NULL; |
| 1864 | options.upstream = NULL; |
| 1865 | if (argc > 1) |
| 1866 | usage_with_options(builtin_rebase_usage, |
| 1867 | builtin_rebase_options); |
| 1868 | options.upstream_arg = "--root"; |
| 1869 | } |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1870 | |
| 1871 | /* Make sure the branch to rebase onto is valid. */ |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 1872 | if (keep_base) { |
| 1873 | strbuf_reset(&buf); |
| 1874 | strbuf_addstr(&buf, options.upstream_name); |
| 1875 | strbuf_addstr(&buf, "..."); |
| 1876 | options.onto_name = xstrdup(buf.buf); |
| 1877 | } else if (!options.onto_name) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1878 | options.onto_name = options.upstream_name; |
| 1879 | if (strstr(options.onto_name, "...")) { |
Denton Liu | 414d924 | 2019-08-27 01:38:06 -0400 | [diff] [blame] | 1880 | if (get_oid_mb(options.onto_name, &merge_base) < 0) { |
| 1881 | if (keep_base) |
| 1882 | die(_("'%s': need exactly one merge base with branch"), |
| 1883 | options.upstream_name); |
| 1884 | else |
| 1885 | die(_("'%s': need exactly one merge base"), |
| 1886 | options.onto_name); |
| 1887 | } |
Pratik Karki | 075bc85 | 2018-09-04 14:27:09 -0700 | [diff] [blame] | 1888 | options.onto = lookup_commit_or_die(&merge_base, |
| 1889 | options.onto_name); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1890 | } else { |
| 1891 | options.onto = peel_committish(options.onto_name); |
| 1892 | if (!options.onto) |
| 1893 | die(_("Does not point to a valid commit '%s'"), |
| 1894 | options.onto_name); |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * If the branch to rebase is given, that is the branch we will rebase |
| 1899 | * branch_name -- branch/commit being rebased, or |
| 1900 | * HEAD (already detached) |
| 1901 | * orig_head -- commit object name of tip of the branch before rebasing |
Pratik Karki | d4c569f | 2018-09-04 14:27:20 -0700 | [diff] [blame] | 1902 | * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1903 | */ |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1904 | if (argc == 1) { |
| 1905 | /* Is it "rebase other branchname" or "rebase other commit"? */ |
| 1906 | branch_name = argv[0]; |
| 1907 | options.switch_to = argv[0]; |
| 1908 | |
| 1909 | /* Is it a local branch? */ |
| 1910 | strbuf_reset(&buf); |
| 1911 | strbuf_addf(&buf, "refs/heads/%s", branch_name); |
Eric Sunshine | b5cabb4 | 2020-02-23 05:14:07 -0500 | [diff] [blame] | 1912 | if (!read_ref(buf.buf, &options.orig_head)) { |
| 1913 | die_if_checked_out(buf.buf, 1); |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1914 | options.head_name = xstrdup(buf.buf); |
| 1915 | /* If not is it a valid ref (branch or commit)? */ |
René Scharfe | ca5120c | 2021-01-01 00:55:12 +0100 | [diff] [blame] | 1916 | } else if (!get_oid(branch_name, &options.orig_head) && |
| 1917 | lookup_commit_reference(the_repository, |
| 1918 | &options.orig_head)) |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1919 | options.head_name = NULL; |
| 1920 | else |
| 1921 | die(_("fatal: no such branch/commit '%s'"), |
| 1922 | branch_name); |
| 1923 | } else if (argc == 0) { |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1924 | /* Do not need to switch branches, we are already on it. */ |
| 1925 | options.head_name = |
| 1926 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, |
| 1927 | &flags)); |
| 1928 | if (!options.head_name) |
| 1929 | die(_("No such ref: %s"), "HEAD"); |
| 1930 | if (flags & REF_ISSYMREF) { |
| 1931 | if (!skip_prefix(options.head_name, |
| 1932 | "refs/heads/", &branch_name)) |
| 1933 | branch_name = options.head_name; |
| 1934 | |
| 1935 | } else { |
SZEDER Gábor | 7762f44 | 2019-04-06 18:34:21 +0700 | [diff] [blame] | 1936 | FREE_AND_NULL(options.head_name); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1937 | branch_name = "HEAD"; |
| 1938 | } |
| 1939 | if (get_oid("HEAD", &options.orig_head)) |
| 1940 | die(_("Could not resolve HEAD to a revision")); |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1941 | } else |
| 1942 | BUG("unexpected number of arguments left to parse"); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1943 | |
Alex Henrie | 2803d80 | 2021-02-23 00:18:40 -0700 | [diff] [blame] | 1944 | if (options.fork_point > 0) { |
Pratik Karki | 92d0d74 | 2018-09-04 15:00:09 -0700 | [diff] [blame] | 1945 | struct commit *head = |
| 1946 | lookup_commit_reference(the_repository, |
| 1947 | &options.orig_head); |
| 1948 | options.restrict_revision = |
| 1949 | get_fork_point(options.upstream_name, head); |
| 1950 | } |
| 1951 | |
Nguyễn Thái Ngọc Duy | e1ff0a3 | 2019-01-12 09:13:26 +0700 | [diff] [blame] | 1952 | if (repo_read_index(the_repository) < 0) |
Pratik Karki | e0333e5 | 2018-09-04 14:27:14 -0700 | [diff] [blame] | 1953 | die(_("could not read index")); |
| 1954 | |
Pratik Karki | 6defce2 | 2018-09-04 15:00:02 -0700 | [diff] [blame] | 1955 | if (options.autostash) { |
Denton Liu | 9bb3dea | 2020-04-07 10:28:02 -0400 | [diff] [blame] | 1956 | create_autostash(the_repository, state_dir_path("autostash", &options), |
| 1957 | DEFAULT_REFLOG_ACTION); |
Pratik Karki | 6defce2 | 2018-09-04 15:00:02 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Nguyễn Thái Ngọc Duy | 5b02ca3 | 2018-11-10 06:48:49 +0100 | [diff] [blame] | 1960 | if (require_clean_work_tree(the_repository, "rebase", |
Pratik Karki | e0333e5 | 2018-09-04 14:27:14 -0700 | [diff] [blame] | 1961 | _("Please commit or stash them."), 1, 1)) { |
| 1962 | ret = 1; |
| 1963 | goto cleanup; |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 1964 | } |
| 1965 | |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1966 | /* |
| 1967 | * Now we are rebasing commits upstream..orig_head (or with --root, |
| 1968 | * everything leading up to orig_head) on top of onto. |
| 1969 | */ |
| 1970 | |
| 1971 | /* |
| 1972 | * Check if we are already based on onto with linear history, |
Denton Liu | c0efb4c | 2019-08-27 01:37:59 -0400 | [diff] [blame] | 1973 | * in which case we could fast-forward without replacing the commits |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1974 | * with new commits recreated by replaying their changes. |
| 1975 | * |
| 1976 | * Note that can_fast_forward() initializes merge_base, so we have to |
| 1977 | * call it before checking allow_preemptive_ff. |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1978 | */ |
Denton Liu | 4effc5b | 2019-08-27 01:38:01 -0400 | [diff] [blame] | 1979 | if (can_fast_forward(options.onto, options.upstream, options.restrict_revision, |
| 1980 | &options.orig_head, &merge_base) && |
Elijah Newren | befb89c | 2020-02-15 21:36:31 +0000 | [diff] [blame] | 1981 | allow_preemptive_ff) { |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 1982 | int flag; |
| 1983 | |
Pratik Karki | 1ed9c14 | 2018-09-04 14:27:17 -0700 | [diff] [blame] | 1984 | if (!(options.flags & REBASE_FORCE)) { |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1985 | /* Lazily switch to the target branch if needed... */ |
| 1986 | if (options.switch_to) { |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1987 | strbuf_reset(&buf); |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 1988 | strbuf_addf(&buf, "%s: checkout %s", |
| 1989 | getenv(GIT_REFLOG_ACTION_ENVIRONMENT), |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1990 | options.switch_to); |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 1991 | if (reset_head(the_repository, |
| 1992 | &options.orig_head, "checkout", |
Orgad Shaneh | 8581df6 | 2018-12-29 23:37:59 +0200 | [diff] [blame] | 1993 | options.head_name, |
| 1994 | RESET_HEAD_RUN_POST_CHECKOUT_HOOK, |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 1995 | NULL, buf.buf, |
| 1996 | DEFAULT_REFLOG_ACTION) < 0) { |
Pratik Karki | e65123a | 2018-09-04 14:27:21 -0700 | [diff] [blame] | 1997 | ret = !!error(_("could not switch to " |
| 1998 | "%s"), |
| 1999 | options.switch_to); |
| 2000 | goto cleanup; |
| 2001 | } |
| 2002 | } |
| 2003 | |
Pratik Karki | 1ed9c14 | 2018-09-04 14:27:17 -0700 | [diff] [blame] | 2004 | if (!(options.flags & REBASE_NO_QUIET)) |
| 2005 | ; /* be quiet */ |
| 2006 | else if (!strcmp(branch_name, "HEAD") && |
| 2007 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) |
| 2008 | puts(_("HEAD is up to date.")); |
| 2009 | else |
| 2010 | printf(_("Current branch %s is up to date.\n"), |
| 2011 | branch_name); |
| 2012 | ret = !!finish_rebase(&options); |
| 2013 | goto cleanup; |
| 2014 | } else if (!(options.flags & REBASE_NO_QUIET)) |
Pratik Karki | 9a48a61 | 2018-09-04 14:27:16 -0700 | [diff] [blame] | 2015 | ; /* be quiet */ |
| 2016 | else if (!strcmp(branch_name, "HEAD") && |
| 2017 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) |
| 2018 | puts(_("HEAD is up to date, rebase forced.")); |
| 2019 | else |
| 2020 | printf(_("Current branch %s is up to date, rebase " |
| 2021 | "forced.\n"), branch_name); |
| 2022 | } |
| 2023 | |
Pratik Karki | 06e4775 | 2018-09-04 14:27:10 -0700 | [diff] [blame] | 2024 | /* If a hook exists, give it a chance to interrupt*/ |
| 2025 | if (!ok_to_skip_pre_rebase && |
| 2026 | run_hook_le(NULL, "pre-rebase", options.upstream_arg, |
| 2027 | argc ? argv[0] : NULL, NULL)) |
| 2028 | die(_("The pre-rebase hook refused to rebase.")); |
| 2029 | |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 2030 | if (options.flags & REBASE_DIFFSTAT) { |
| 2031 | struct diff_options opts; |
| 2032 | |
Johannes Schindelin | 8797f0f | 2018-11-29 05:01:54 -0800 | [diff] [blame] | 2033 | if (options.flags & REBASE_VERBOSE) { |
| 2034 | if (is_null_oid(&merge_base)) |
| 2035 | printf(_("Changes to %s:\n"), |
| 2036 | oid_to_hex(&options.onto->object.oid)); |
| 2037 | else |
| 2038 | printf(_("Changes from %s to %s:\n"), |
| 2039 | oid_to_hex(&merge_base), |
| 2040 | oid_to_hex(&options.onto->object.oid)); |
| 2041 | } |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 2042 | |
| 2043 | /* We want color (if set), but no pager */ |
| 2044 | diff_setup(&opts); |
| 2045 | opts.stat_width = -1; /* use full terminal width */ |
| 2046 | opts.stat_graph_width = -1; /* respect statGraphWidth config */ |
| 2047 | opts.output_format |= |
| 2048 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; |
| 2049 | opts.detect_rename = DIFF_DETECT_RENAME; |
| 2050 | diff_setup_done(&opts); |
Johannes Schindelin | 8797f0f | 2018-11-29 05:01:54 -0800 | [diff] [blame] | 2051 | diff_tree_oid(is_null_oid(&merge_base) ? |
| 2052 | the_hash_algo->empty_tree : &merge_base, |
| 2053 | &options.onto->object.oid, "", &opts); |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 2054 | diffcore_std(&opts); |
| 2055 | diff_flush(&opts); |
| 2056 | } |
| 2057 | |
Elijah Newren | 10cdb9f | 2020-02-15 21:36:41 +0000 | [diff] [blame] | 2058 | if (is_merge(&options)) |
Pratik Karki | 361badd | 2018-09-04 14:59:49 -0700 | [diff] [blame] | 2059 | goto run_rebase; |
| 2060 | |
Pratik Karki | bff014d | 2018-09-04 14:27:13 -0700 | [diff] [blame] | 2061 | /* Detach HEAD and reset the tree */ |
| 2062 | if (options.flags & REBASE_NO_QUIET) |
| 2063 | printf(_("First, rewinding head to replay your work on top of " |
| 2064 | "it...\n")); |
| 2065 | |
Johannes Schindelin | 13a5a9f | 2018-11-29 11:09:21 -0800 | [diff] [blame] | 2066 | strbuf_addf(&msg, "%s: checkout %s", |
| 2067 | getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name); |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 2068 | if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL, |
Johannes Schindelin | cbea646 | 2019-07-24 14:14:59 -0700 | [diff] [blame] | 2069 | RESET_HEAD_DETACH | RESET_ORIG_HEAD | |
Junio C Hamano | 9fbcc3d | 2019-03-20 15:16:05 +0900 | [diff] [blame] | 2070 | RESET_HEAD_RUN_POST_CHECKOUT_HOOK, |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 2071 | NULL, msg.buf, DEFAULT_REFLOG_ACTION)) |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 2072 | die(_("Could not detach HEAD")); |
| 2073 | strbuf_release(&msg); |
| 2074 | |
Pratik Karki | 7eecfa5 | 2018-08-08 21:21:32 +0545 | [diff] [blame] | 2075 | /* |
| 2076 | * If the onto is a proper descendant of the tip of the branch, then |
| 2077 | * we just fast-forwarded. |
| 2078 | */ |
| 2079 | strbuf_reset(&msg); |
SZEDER Gábor | d2c4e62 | 2019-04-06 18:34:22 +0700 | [diff] [blame] | 2080 | if (oideq(&merge_base, &options.orig_head)) { |
Ralf Thielow | eff199a | 2018-11-30 19:11:45 +0100 | [diff] [blame] | 2081 | printf(_("Fast-forwarded %s to %s.\n"), |
Pratik Karki | 7eecfa5 | 2018-08-08 21:21:32 +0545 | [diff] [blame] | 2082 | branch_name, options.onto_name); |
| 2083 | strbuf_addf(&msg, "rebase finished: %s onto %s", |
| 2084 | options.head_name ? options.head_name : "detached HEAD", |
| 2085 | oid_to_hex(&options.onto->object.oid)); |
Denton Liu | f213f06 | 2020-04-07 10:27:59 -0400 | [diff] [blame] | 2086 | reset_head(the_repository, NULL, "Fast-forwarded", options.head_name, |
| 2087 | RESET_HEAD_REFS_ONLY, "HEAD", msg.buf, |
| 2088 | DEFAULT_REFLOG_ACTION); |
Pratik Karki | 7eecfa5 | 2018-08-08 21:21:32 +0545 | [diff] [blame] | 2089 | strbuf_release(&msg); |
| 2090 | ret = !!finish_rebase(&options); |
| 2091 | goto cleanup; |
| 2092 | } |
| 2093 | |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 2094 | strbuf_addf(&revisions, "%s..%s", |
| 2095 | options.root ? oid_to_hex(&options.onto->object.oid) : |
| 2096 | (options.restrict_revision ? |
| 2097 | oid_to_hex(&options.restrict_revision->object.oid) : |
| 2098 | oid_to_hex(&options.upstream->object.oid)), |
| 2099 | oid_to_hex(&options.orig_head)); |
| 2100 | |
| 2101 | options.revisions = revisions.buf; |
| 2102 | |
Pratik Karki | f957362 | 2018-08-08 20:51:16 +0545 | [diff] [blame] | 2103 | run_rebase: |
Phillip Wood | 460bc3c | 2019-04-17 15:30:44 +0100 | [diff] [blame] | 2104 | ret = !!run_specific_rebase(&options, action); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 2105 | |
Pratik Karki | e0333e5 | 2018-09-04 14:27:14 -0700 | [diff] [blame] | 2106 | cleanup: |
Phillip Wood | 7372eae | 2019-05-14 19:03:46 +0100 | [diff] [blame] | 2107 | strbuf_release(&buf); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 2108 | strbuf_release(&revisions); |
| 2109 | free(options.head_name); |
Pratik Karki | 12026a4 | 2018-09-04 15:00:00 -0700 | [diff] [blame] | 2110 | free(options.gpg_sign_opt); |
Pratik Karki | 68e46d7 | 2018-09-04 15:00:04 -0700 | [diff] [blame] | 2111 | free(options.cmd); |
Andrzej Hunt | b54cf3a | 2021-07-25 15:08:29 +0200 | [diff] [blame] | 2112 | free(options.strategy); |
Andrzej Hunt | 805b789 | 2021-04-25 14:16:18 +0000 | [diff] [blame] | 2113 | strbuf_release(&options.git_format_patch_opt); |
Pratik Karki | 9dba809 | 2018-09-04 15:00:12 -0700 | [diff] [blame] | 2114 | free(squash_onto_name); |
Pratik Karki | ac7f467 | 2018-08-07 01:16:11 +0545 | [diff] [blame] | 2115 | return ret; |
Pratik Karki | 55071ea | 2018-08-07 01:16:09 +0545 | [diff] [blame] | 2116 | } |