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