]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* | |
| 2 | * "git rebase" builtin command | |
| 3 | * | |
| 4 | * Copyright (c) 2018 Pratik Karki | |
| 5 | */ | |
| 6 | ||
| 7 | #define USE_THE_REPOSITORY_VARIABLE | |
| 8 | #define DISABLE_SIGN_COMPARE_WARNINGS | |
| 9 | ||
| 10 | #include "builtin.h" | |
| 11 | ||
| 12 | #include "abspath.h" | |
| 13 | #include "environment.h" | |
| 14 | #include "gettext.h" | |
| 15 | #include "hex.h" | |
| 16 | #include "run-command.h" | |
| 17 | #include "strvec.h" | |
| 18 | #include "dir.h" | |
| 19 | #include "refs.h" | |
| 20 | #include "config.h" | |
| 21 | #include "unpack-trees.h" | |
| 22 | #include "lockfile.h" | |
| 23 | #include "object-file.h" | |
| 24 | #include "object-name.h" | |
| 25 | #include "parse-options.h" | |
| 26 | #include "path.h" | |
| 27 | #include "commit.h" | |
| 28 | #include "diff.h" | |
| 29 | #include "wt-status.h" | |
| 30 | #include "revision.h" | |
| 31 | #include "commit-reach.h" | |
| 32 | #include "rerere.h" | |
| 33 | #include "branch.h" | |
| 34 | #include "sequencer.h" | |
| 35 | #include "rebase-interactive.h" | |
| 36 | #include "reset.h" | |
| 37 | #include "trace2.h" | |
| 38 | #include "hook.h" | |
| 39 | ||
| 40 | static char const * const builtin_rebase_usage[] = { | |
| 41 | N_("git rebase [-i] [options] [--exec <cmd>] " | |
| 42 | "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"), | |
| 43 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
| 44 | "--root [<branch>]"), | |
| 45 | "git rebase --continue | --abort | --skip | --edit-todo", | |
| 46 | NULL | |
| 47 | }; | |
| 48 | ||
| 49 | static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto") | |
| 50 | static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive") | |
| 51 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") | |
| 52 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") | |
| 53 | ||
| 54 | enum rebase_type { | |
| 55 | REBASE_UNSPECIFIED = -1, | |
| 56 | REBASE_APPLY, | |
| 57 | REBASE_MERGE | |
| 58 | }; | |
| 59 | ||
| 60 | enum empty_type { | |
| 61 | EMPTY_UNSPECIFIED = -1, | |
| 62 | EMPTY_DROP, | |
| 63 | EMPTY_KEEP, | |
| 64 | EMPTY_STOP | |
| 65 | }; | |
| 66 | ||
| 67 | enum action { | |
| 68 | ACTION_NONE = 0, | |
| 69 | ACTION_CONTINUE, | |
| 70 | ACTION_SKIP, | |
| 71 | ACTION_ABORT, | |
| 72 | ACTION_QUIT, | |
| 73 | ACTION_EDIT_TODO, | |
| 74 | ACTION_SHOW_CURRENT_PATCH | |
| 75 | }; | |
| 76 | ||
| 77 | static const char *action_names[] = { | |
| 78 | "undefined", | |
| 79 | "continue", | |
| 80 | "skip", | |
| 81 | "abort", | |
| 82 | "quit", | |
| 83 | "edit_todo", | |
| 84 | "show_current_patch" | |
| 85 | }; | |
| 86 | ||
| 87 | struct rebase_options { | |
| 88 | enum rebase_type type; | |
| 89 | enum empty_type empty; | |
| 90 | char *default_backend; | |
| 91 | const char *state_dir; | |
| 92 | struct commit *upstream; | |
| 93 | const char *upstream_name; | |
| 94 | const char *upstream_arg; | |
| 95 | char *head_name; | |
| 96 | struct commit *orig_head; | |
| 97 | struct commit *onto; | |
| 98 | const char *onto_name; | |
| 99 | const char *revisions; | |
| 100 | const char *switch_to; | |
| 101 | int root, root_with_onto; | |
| 102 | struct object_id *squash_onto; | |
| 103 | struct commit *restrict_revision; | |
| 104 | int dont_finish_rebase; | |
| 105 | enum { | |
| 106 | REBASE_NO_QUIET = 1<<0, | |
| 107 | REBASE_VERBOSE = 1<<1, | |
| 108 | REBASE_DIFFSTAT = 1<<2, | |
| 109 | REBASE_FORCE = 1<<3, | |
| 110 | REBASE_INTERACTIVE_EXPLICIT = 1<<4, | |
| 111 | } flags; | |
| 112 | struct strvec git_am_opts; | |
| 113 | enum action action; | |
| 114 | char *reflog_action; | |
| 115 | int signoff; | |
| 116 | int allow_rerere_autoupdate; | |
| 117 | int keep_empty; | |
| 118 | int autosquash; | |
| 119 | char *gpg_sign_opt; | |
| 120 | int autostash; | |
| 121 | int committer_date_is_author_date; | |
| 122 | int ignore_date; | |
| 123 | struct string_list exec; | |
| 124 | int allow_empty_message; | |
| 125 | int rebase_merges, rebase_cousins; | |
| 126 | char *strategy; | |
| 127 | struct string_list strategy_opts; | |
| 128 | struct strbuf git_format_patch_opt; | |
| 129 | int reschedule_failed_exec; | |
| 130 | int reapply_cherry_picks; | |
| 131 | int fork_point; | |
| 132 | int update_refs; | |
| 133 | int config_autosquash; | |
| 134 | int config_rebase_merges; | |
| 135 | int config_update_refs; | |
| 136 | }; | |
| 137 | ||
| 138 | #define REBASE_OPTIONS_INIT { \ | |
| 139 | .type = REBASE_UNSPECIFIED, \ | |
| 140 | .empty = EMPTY_UNSPECIFIED, \ | |
| 141 | .keep_empty = 1, \ | |
| 142 | .default_backend = xstrdup("merge"), \ | |
| 143 | .flags = REBASE_NO_QUIET, \ | |
| 144 | .git_am_opts = STRVEC_INIT, \ | |
| 145 | .exec = STRING_LIST_INIT_NODUP, \ | |
| 146 | .git_format_patch_opt = STRBUF_INIT, \ | |
| 147 | .fork_point = -1, \ | |
| 148 | .reapply_cherry_picks = -1, \ | |
| 149 | .allow_empty_message = 1, \ | |
| 150 | .autosquash = -1, \ | |
| 151 | .rebase_merges = -1, \ | |
| 152 | .config_rebase_merges = -1, \ | |
| 153 | .update_refs = -1, \ | |
| 154 | .config_update_refs = -1, \ | |
| 155 | .strategy_opts = STRING_LIST_INIT_NODUP,\ | |
| 156 | } | |
| 157 | ||
| 158 | static void rebase_options_release(struct rebase_options *opts) | |
| 159 | { | |
| 160 | free(opts->default_backend); | |
| 161 | free(opts->reflog_action); | |
| 162 | free(opts->head_name); | |
| 163 | strvec_clear(&opts->git_am_opts); | |
| 164 | free(opts->gpg_sign_opt); | |
| 165 | string_list_clear(&opts->exec, 0); | |
| 166 | free(opts->strategy); | |
| 167 | string_list_clear(&opts->strategy_opts, 0); | |
| 168 | strbuf_release(&opts->git_format_patch_opt); | |
| 169 | } | |
| 170 | ||
| 171 | static struct replay_opts get_replay_opts(const struct rebase_options *opts) | |
| 172 | { | |
| 173 | struct replay_opts replay = REPLAY_OPTS_INIT; | |
| 174 | ||
| 175 | replay.action = REPLAY_INTERACTIVE_REBASE; | |
| 176 | replay.strategy = NULL; | |
| 177 | sequencer_init_config(&replay); | |
| 178 | ||
| 179 | replay.signoff = opts->signoff; | |
| 180 | replay.allow_ff = !(opts->flags & REBASE_FORCE); | |
| 181 | if (opts->allow_rerere_autoupdate) | |
| 182 | replay.allow_rerere_auto = opts->allow_rerere_autoupdate; | |
| 183 | replay.allow_empty = 1; | |
| 184 | replay.allow_empty_message = opts->allow_empty_message; | |
| 185 | replay.drop_redundant_commits = (opts->empty == EMPTY_DROP); | |
| 186 | replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP); | |
| 187 | replay.quiet = !(opts->flags & REBASE_NO_QUIET); | |
| 188 | replay.verbose = opts->flags & REBASE_VERBOSE; | |
| 189 | replay.reschedule_failed_exec = opts->reschedule_failed_exec; | |
| 190 | replay.committer_date_is_author_date = | |
| 191 | opts->committer_date_is_author_date; | |
| 192 | replay.ignore_date = opts->ignore_date; | |
| 193 | free(replay.gpg_sign); | |
| 194 | replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt); | |
| 195 | replay.reflog_action = xstrdup(opts->reflog_action); | |
| 196 | if (opts->strategy) | |
| 197 | replay.strategy = xstrdup_or_null(opts->strategy); | |
| 198 | else if (!replay.strategy && replay.default_strategy) { | |
| 199 | replay.strategy = replay.default_strategy; | |
| 200 | replay.default_strategy = NULL; | |
| 201 | } | |
| 202 | ||
| 203 | for (size_t i = 0; i < opts->strategy_opts.nr; i++) | |
| 204 | strvec_push(&replay.xopts, opts->strategy_opts.items[i].string); | |
| 205 | ||
| 206 | if (opts->squash_onto) { | |
| 207 | oidcpy(&replay.squash_onto, opts->squash_onto); | |
| 208 | replay.have_squash_onto = 1; | |
| 209 | } | |
| 210 | ||
| 211 | return replay; | |
| 212 | } | |
| 213 | ||
| 214 | static int edit_todo_file(unsigned flags, struct replay_opts *opts) | |
| 215 | { | |
| 216 | const char *todo_file = rebase_path_todo(); | |
| 217 | struct todo_list todo_list = TODO_LIST_INIT, | |
| 218 | new_todo = TODO_LIST_INIT; | |
| 219 | int res = 0; | |
| 220 | ||
| 221 | if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0) | |
| 222 | return error_errno(_("could not read '%s'."), todo_file); | |
| 223 | ||
| 224 | strbuf_stripspace(&todo_list.buf, comment_line_str); | |
| 225 | res = edit_todo_list(the_repository, opts, &todo_list, &new_todo, | |
| 226 | NULL, NULL, flags); | |
| 227 | if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file, | |
| 228 | NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS))) | |
| 229 | res = error_errno(_("could not write '%s'"), todo_file); | |
| 230 | ||
| 231 | todo_list_release(&todo_list); | |
| 232 | todo_list_release(&new_todo); | |
| 233 | ||
| 234 | return res; | |
| 235 | } | |
| 236 | ||
| 237 | static int get_revision_ranges(struct commit *upstream, struct commit *onto, | |
| 238 | struct object_id *orig_head, char **revisions, | |
| 239 | char **shortrevisions) | |
| 240 | { | |
| 241 | struct commit *base_rev = upstream ? upstream : onto; | |
| 242 | const char *shorthead; | |
| 243 | ||
| 244 | *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid), | |
| 245 | oid_to_hex(orig_head)); | |
| 246 | ||
| 247 | shorthead = repo_find_unique_abbrev(the_repository, orig_head, | |
| 248 | DEFAULT_ABBREV); | |
| 249 | ||
| 250 | if (upstream) { | |
| 251 | const char *shortrev; | |
| 252 | ||
| 253 | shortrev = repo_find_unique_abbrev(the_repository, | |
| 254 | &base_rev->object.oid, | |
| 255 | DEFAULT_ABBREV); | |
| 256 | ||
| 257 | *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead); | |
| 258 | } else | |
| 259 | *shortrevisions = xstrdup(shorthead); | |
| 260 | ||
| 261 | return 0; | |
| 262 | } | |
| 263 | ||
| 264 | static int init_basic_state(struct replay_opts *opts, const char *head_name, | |
| 265 | struct commit *onto, | |
| 266 | const struct object_id *orig_head) | |
| 267 | { | |
| 268 | FILE *interactive; | |
| 269 | ||
| 270 | if (!is_directory(merge_dir()) && | |
| 271 | safe_create_dir_in_gitdir(the_repository, merge_dir())) | |
| 272 | return error_errno(_("could not create temporary %s"), merge_dir()); | |
| 273 | ||
| 274 | refs_delete_reflog(get_main_ref_store(the_repository), "REBASE_HEAD"); | |
| 275 | ||
| 276 | interactive = fopen(path_interactive(), "w"); | |
| 277 | if (!interactive) | |
| 278 | return error_errno(_("could not mark as interactive")); | |
| 279 | fclose(interactive); | |
| 280 | ||
| 281 | return write_basic_state(opts, head_name, onto, orig_head); | |
| 282 | } | |
| 283 | ||
| 284 | static int do_interactive_rebase(struct rebase_options *opts, unsigned flags) | |
| 285 | { | |
| 286 | int ret = -1; | |
| 287 | char *revisions = NULL, *shortrevisions = NULL; | |
| 288 | struct strvec make_script_args = STRVEC_INIT; | |
| 289 | struct todo_list todo_list = TODO_LIST_INIT; | |
| 290 | struct replay_opts replay = get_replay_opts(opts); | |
| 291 | ||
| 292 | if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head->object.oid, | |
| 293 | &revisions, &shortrevisions)) | |
| 294 | goto cleanup; | |
| 295 | ||
| 296 | strvec_pushl(&make_script_args, "", revisions, NULL); | |
| 297 | if (opts->restrict_revision) | |
| 298 | strvec_pushf(&make_script_args, "^%s", | |
| 299 | oid_to_hex(&opts->restrict_revision->object.oid)); | |
| 300 | ||
| 301 | ret = sequencer_make_script(the_repository, &todo_list.buf, | |
| 302 | &make_script_args, flags); | |
| 303 | if (ret) { | |
| 304 | error(_("could not generate todo list")); | |
| 305 | goto cleanup; | |
| 306 | } | |
| 307 | ||
| 308 | if (init_basic_state(&replay, | |
| 309 | opts->head_name ? opts->head_name : "detached HEAD", | |
| 310 | opts->onto, &opts->orig_head->object.oid)) | |
| 311 | goto cleanup; | |
| 312 | ||
| 313 | if (!opts->upstream && opts->squash_onto) | |
| 314 | write_file(path_squash_onto(), "%s\n", | |
| 315 | oid_to_hex(opts->squash_onto)); | |
| 316 | ||
| 317 | discard_index(the_repository->index); | |
| 318 | if (todo_list_parse_insn_buffer(the_repository, &replay, | |
| 319 | todo_list.buf.buf, &todo_list)) | |
| 320 | BUG("unusable todo list"); | |
| 321 | ||
| 322 | ret = complete_action(the_repository, &replay, flags, | |
| 323 | shortrevisions, opts->onto_name, opts->onto, | |
| 324 | &opts->orig_head->object.oid, &opts->exec, | |
| 325 | opts->autosquash, opts->update_refs, &todo_list); | |
| 326 | ||
| 327 | cleanup: | |
| 328 | replay_opts_release(&replay); | |
| 329 | free(revisions); | |
| 330 | free(shortrevisions); | |
| 331 | todo_list_release(&todo_list); | |
| 332 | strvec_clear(&make_script_args); | |
| 333 | ||
| 334 | return ret; | |
| 335 | } | |
| 336 | ||
| 337 | static int run_sequencer_rebase(struct rebase_options *opts) | |
| 338 | { | |
| 339 | unsigned flags = 0; | |
| 340 | int abbreviate_commands = 0, ret = 0; | |
| 341 | ||
| 342 | repo_config_get_bool(the_repository, "rebase.abbreviatecommands", &abbreviate_commands); | |
| 343 | ||
| 344 | flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0; | |
| 345 | flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0; | |
| 346 | flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0; | |
| 347 | flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0; | |
| 348 | flags |= opts->root_with_onto ? TODO_LIST_ROOT_WITH_ONTO : 0; | |
| 349 | flags |= opts->reapply_cherry_picks ? TODO_LIST_REAPPLY_CHERRY_PICKS : 0; | |
| 350 | flags |= opts->flags & REBASE_NO_QUIET ? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS : 0; | |
| 351 | ||
| 352 | switch (opts->action) { | |
| 353 | case ACTION_NONE: { | |
| 354 | if (!opts->onto && !opts->upstream) | |
| 355 | die(_("a base commit must be provided with --upstream or --onto")); | |
| 356 | ||
| 357 | ret = do_interactive_rebase(opts, flags); | |
| 358 | break; | |
| 359 | } | |
| 360 | case ACTION_SKIP: { | |
| 361 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
| 362 | ||
| 363 | rerere_clear(the_repository, &merge_rr); | |
| 364 | } | |
| 365 | /* fallthrough */ | |
| 366 | case ACTION_CONTINUE: { | |
| 367 | struct replay_opts replay_opts = get_replay_opts(opts); | |
| 368 | ||
| 369 | ret = sequencer_continue(the_repository, &replay_opts); | |
| 370 | replay_opts_release(&replay_opts); | |
| 371 | break; | |
| 372 | } | |
| 373 | case ACTION_EDIT_TODO: { | |
| 374 | struct replay_opts replay_opts = get_replay_opts(opts); | |
| 375 | ||
| 376 | ret = edit_todo_file(flags, &replay_opts); | |
| 377 | replay_opts_release(&replay_opts); | |
| 378 | break; | |
| 379 | } | |
| 380 | case ACTION_SHOW_CURRENT_PATCH: { | |
| 381 | struct child_process cmd = CHILD_PROCESS_INIT; | |
| 382 | ||
| 383 | cmd.git_cmd = 1; | |
| 384 | strvec_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL); | |
| 385 | ret = run_command(&cmd); | |
| 386 | ||
| 387 | break; | |
| 388 | } | |
| 389 | default: | |
| 390 | BUG("invalid command '%d'", opts->action); | |
| 391 | } | |
| 392 | ||
| 393 | return ret; | |
| 394 | } | |
| 395 | ||
| 396 | static int is_merge(struct rebase_options *opts) | |
| 397 | { | |
| 398 | return opts->type == REBASE_MERGE; | |
| 399 | } | |
| 400 | ||
| 401 | static void imply_merge(struct rebase_options *opts, const char *option) | |
| 402 | { | |
| 403 | switch (opts->type) { | |
| 404 | case REBASE_APPLY: | |
| 405 | die(_("%s requires the merge backend"), option); | |
| 406 | break; | |
| 407 | case REBASE_MERGE: | |
| 408 | break; | |
| 409 | default: | |
| 410 | opts->type = REBASE_MERGE; /* implied */ | |
| 411 | break; | |
| 412 | } | |
| 413 | } | |
| 414 | ||
| 415 | /* Returns the filename prefixed by the state_dir */ | |
| 416 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) | |
| 417 | { | |
| 418 | static struct strbuf path = STRBUF_INIT; | |
| 419 | static size_t prefix_len; | |
| 420 | ||
| 421 | if (!prefix_len) { | |
| 422 | strbuf_addf(&path, "%s/", opts->state_dir); | |
| 423 | prefix_len = path.len; | |
| 424 | } | |
| 425 | ||
| 426 | strbuf_setlen(&path, prefix_len); | |
| 427 | strbuf_addstr(&path, filename); | |
| 428 | return path.buf; | |
| 429 | } | |
| 430 | ||
| 431 | /* Initialize the rebase options from the state directory. */ | |
| 432 | static int read_basic_state(struct rebase_options *opts) | |
| 433 | { | |
| 434 | struct strbuf head_name = STRBUF_INIT; | |
| 435 | struct strbuf buf = STRBUF_INIT; | |
| 436 | struct object_id oid; | |
| 437 | ||
| 438 | if (!read_oneliner(&head_name, state_dir_path("head-name", opts), | |
| 439 | READ_ONELINER_WARN_MISSING) || | |
| 440 | !read_oneliner(&buf, state_dir_path("onto", opts), | |
| 441 | READ_ONELINER_WARN_MISSING)) | |
| 442 | return -1; | |
| 443 | opts->head_name = starts_with(head_name.buf, "refs/") ? | |
| 444 | xstrdup(head_name.buf) : NULL; | |
| 445 | strbuf_release(&head_name); | |
| 446 | if (get_oid_hex(buf.buf, &oid) || | |
| 447 | !(opts->onto = lookup_commit_object(the_repository, &oid))) | |
| 448 | return error(_("invalid onto: '%s'"), buf.buf); | |
| 449 | ||
| 450 | /* | |
| 451 | * We always write to orig-head, but interactive rebase used to write to | |
| 452 | * head. Fall back to reading from head to cover for the case that the | |
| 453 | * user upgraded git with an ongoing interactive rebase. | |
| 454 | */ | |
| 455 | strbuf_reset(&buf); | |
| 456 | if (file_exists(state_dir_path("orig-head", opts))) { | |
| 457 | if (!read_oneliner(&buf, state_dir_path("orig-head", opts), | |
| 458 | READ_ONELINER_WARN_MISSING)) | |
| 459 | return -1; | |
| 460 | } else if (!read_oneliner(&buf, state_dir_path("head", opts), | |
| 461 | READ_ONELINER_WARN_MISSING)) | |
| 462 | return -1; | |
| 463 | if (get_oid_hex(buf.buf, &oid) || | |
| 464 | !(opts->orig_head = lookup_commit_object(the_repository, &oid))) | |
| 465 | return error(_("invalid orig-head: '%s'"), buf.buf); | |
| 466 | ||
| 467 | if (file_exists(state_dir_path("quiet", opts))) | |
| 468 | opts->flags &= ~REBASE_NO_QUIET; | |
| 469 | else | |
| 470 | opts->flags |= REBASE_NO_QUIET; | |
| 471 | ||
| 472 | if (file_exists(state_dir_path("verbose", opts))) | |
| 473 | opts->flags |= REBASE_VERBOSE; | |
| 474 | ||
| 475 | if (file_exists(state_dir_path("signoff", opts))) { | |
| 476 | opts->signoff = 1; | |
| 477 | opts->flags |= REBASE_FORCE; | |
| 478 | } | |
| 479 | ||
| 480 | if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) { | |
| 481 | strbuf_reset(&buf); | |
| 482 | if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts), | |
| 483 | READ_ONELINER_WARN_MISSING)) | |
| 484 | return -1; | |
| 485 | if (!strcmp(buf.buf, "--rerere-autoupdate")) | |
| 486 | opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE; | |
| 487 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
| 488 | opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE; | |
| 489 | else | |
| 490 | warning(_("ignoring invalid allow_rerere_autoupdate: " | |
| 491 | "'%s'"), buf.buf); | |
| 492 | } | |
| 493 | ||
| 494 | if (file_exists(state_dir_path("gpg_sign_opt", opts))) { | |
| 495 | strbuf_reset(&buf); | |
| 496 | if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts), | |
| 497 | READ_ONELINER_WARN_MISSING)) | |
| 498 | return -1; | |
| 499 | free(opts->gpg_sign_opt); | |
| 500 | opts->gpg_sign_opt = xstrdup(buf.buf); | |
| 501 | } | |
| 502 | ||
| 503 | strbuf_release(&buf); | |
| 504 | ||
| 505 | return 0; | |
| 506 | } | |
| 507 | ||
| 508 | static int rebase_write_basic_state(struct rebase_options *opts) | |
| 509 | { | |
| 510 | write_file(state_dir_path("head-name", opts), "%s", | |
| 511 | opts->head_name ? opts->head_name : "detached HEAD"); | |
| 512 | write_file(state_dir_path("onto", opts), "%s", | |
| 513 | opts->onto ? oid_to_hex(&opts->onto->object.oid) : ""); | |
| 514 | write_file(state_dir_path("orig-head", opts), "%s", | |
| 515 | oid_to_hex(&opts->orig_head->object.oid)); | |
| 516 | if (!(opts->flags & REBASE_NO_QUIET)) | |
| 517 | write_file(state_dir_path("quiet", opts), "%s", ""); | |
| 518 | if (opts->flags & REBASE_VERBOSE) | |
| 519 | write_file(state_dir_path("verbose", opts), "%s", ""); | |
| 520 | if (opts->allow_rerere_autoupdate > 0) | |
| 521 | write_file(state_dir_path("allow_rerere_autoupdate", opts), | |
| 522 | "-%s-rerere-autoupdate", | |
| 523 | opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ? | |
| 524 | "" : "-no"); | |
| 525 | if (opts->gpg_sign_opt) | |
| 526 | write_file(state_dir_path("gpg_sign_opt", opts), "%s", | |
| 527 | opts->gpg_sign_opt); | |
| 528 | if (opts->signoff) | |
| 529 | write_file(state_dir_path("signoff", opts), "--signoff"); | |
| 530 | ||
| 531 | return 0; | |
| 532 | } | |
| 533 | ||
| 534 | static int cleanup_autostash(struct rebase_options *opts) | |
| 535 | { | |
| 536 | int ret; | |
| 537 | struct strbuf dir = STRBUF_INIT; | |
| 538 | const char *path = state_dir_path("autostash", opts); | |
| 539 | ||
| 540 | if (!file_exists(path)) | |
| 541 | return 0; | |
| 542 | ret = apply_autostash(path); | |
| 543 | strbuf_addstr(&dir, opts->state_dir); | |
| 544 | if (remove_dir_recursively(&dir, 0)) | |
| 545 | ret = error_errno(_("could not remove '%s'"), opts->state_dir); | |
| 546 | strbuf_release(&dir); | |
| 547 | ||
| 548 | return ret; | |
| 549 | } | |
| 550 | ||
| 551 | static int finish_rebase(struct rebase_options *opts) | |
| 552 | { | |
| 553 | struct strbuf dir = STRBUF_INIT; | |
| 554 | int ret = 0; | |
| 555 | ||
| 556 | refs_delete_ref(get_main_ref_store(the_repository), NULL, | |
| 557 | "REBASE_HEAD", NULL, REF_NO_DEREF); | |
| 558 | refs_delete_ref(get_main_ref_store(the_repository), NULL, | |
| 559 | "AUTO_MERGE", NULL, REF_NO_DEREF); | |
| 560 | apply_autostash(state_dir_path("autostash", opts)); | |
| 561 | /* | |
| 562 | * We ignore errors in 'git maintenance run --auto', since the | |
| 563 | * user should see them. | |
| 564 | */ | |
| 565 | run_auto_maintenance(!(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE))); | |
| 566 | if (opts->type == REBASE_MERGE) { | |
| 567 | struct replay_opts replay = REPLAY_OPTS_INIT; | |
| 568 | ||
| 569 | replay.action = REPLAY_INTERACTIVE_REBASE; | |
| 570 | ret = sequencer_remove_state(&replay); | |
| 571 | replay_opts_release(&replay); | |
| 572 | } else { | |
| 573 | strbuf_addstr(&dir, opts->state_dir); | |
| 574 | if (remove_dir_recursively(&dir, 0)) | |
| 575 | ret = error(_("could not remove '%s'"), | |
| 576 | opts->state_dir); | |
| 577 | strbuf_release(&dir); | |
| 578 | } | |
| 579 | ||
| 580 | return ret; | |
| 581 | } | |
| 582 | ||
| 583 | static int move_to_original_branch(struct rebase_options *opts) | |
| 584 | { | |
| 585 | struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; | |
| 586 | struct reset_head_opts ropts = { 0 }; | |
| 587 | int ret; | |
| 588 | ||
| 589 | if (!opts->head_name) | |
| 590 | return 0; /* nothing to move back to */ | |
| 591 | ||
| 592 | if (!opts->onto) | |
| 593 | BUG("move_to_original_branch without onto"); | |
| 594 | ||
| 595 | strbuf_addf(&branch_reflog, "%s (finish): %s onto %s", | |
| 596 | opts->reflog_action, | |
| 597 | opts->head_name, oid_to_hex(&opts->onto->object.oid)); | |
| 598 | strbuf_addf(&head_reflog, "%s (finish): returning to %s", | |
| 599 | opts->reflog_action, opts->head_name); | |
| 600 | ropts.branch = opts->head_name; | |
| 601 | ropts.flags = RESET_HEAD_REFS_ONLY; | |
| 602 | ropts.branch_msg = branch_reflog.buf; | |
| 603 | ropts.head_msg = head_reflog.buf; | |
| 604 | ret = reset_head(the_repository, &ropts); | |
| 605 | ||
| 606 | strbuf_release(&branch_reflog); | |
| 607 | strbuf_release(&head_reflog); | |
| 608 | return ret; | |
| 609 | } | |
| 610 | ||
| 611 | static int run_am(struct rebase_options *opts) | |
| 612 | { | |
| 613 | struct child_process am = CHILD_PROCESS_INIT; | |
| 614 | struct child_process format_patch = CHILD_PROCESS_INIT; | |
| 615 | int status; | |
| 616 | char *rebased_patches; | |
| 617 | ||
| 618 | am.git_cmd = 1; | |
| 619 | strvec_push(&am.args, "am"); | |
| 620 | strvec_pushf(&am.env, GIT_REFLOG_ACTION_ENVIRONMENT "=%s (pick)", | |
| 621 | opts->reflog_action); | |
| 622 | if (opts->action == ACTION_CONTINUE) { | |
| 623 | strvec_push(&am.args, "--resolved"); | |
| 624 | strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); | |
| 625 | if (opts->gpg_sign_opt) | |
| 626 | strvec_push(&am.args, opts->gpg_sign_opt); | |
| 627 | status = run_command(&am); | |
| 628 | if (status) | |
| 629 | return status; | |
| 630 | ||
| 631 | return move_to_original_branch(opts); | |
| 632 | } | |
| 633 | if (opts->action == ACTION_SKIP) { | |
| 634 | strvec_push(&am.args, "--skip"); | |
| 635 | strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); | |
| 636 | status = run_command(&am); | |
| 637 | if (status) | |
| 638 | return status; | |
| 639 | ||
| 640 | return move_to_original_branch(opts); | |
| 641 | } | |
| 642 | if (opts->action == ACTION_SHOW_CURRENT_PATCH) { | |
| 643 | strvec_push(&am.args, "--show-current-patch"); | |
| 644 | return run_command(&am); | |
| 645 | } | |
| 646 | ||
| 647 | rebased_patches = repo_git_path(the_repository, "rebased-patches"); | |
| 648 | format_patch.out = open(rebased_patches, | |
| 649 | O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
| 650 | if (format_patch.out < 0) { | |
| 651 | status = error_errno(_("could not open '%s' for writing"), | |
| 652 | rebased_patches); | |
| 653 | free(rebased_patches); | |
| 654 | child_process_clear(&am); | |
| 655 | return status; | |
| 656 | } | |
| 657 | ||
| 658 | format_patch.git_cmd = 1; | |
| 659 | strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout", | |
| 660 | "--full-index", "--cherry-pick", "--right-only", | |
| 661 | "--default-prefix", "--no-renames", | |
| 662 | "--no-cover-letter", "--pretty=mboxrd", "--topo-order", | |
| 663 | "--no-base", NULL); | |
| 664 | if (opts->git_format_patch_opt.len) | |
| 665 | strvec_split(&format_patch.args, | |
| 666 | opts->git_format_patch_opt.buf); | |
| 667 | strvec_pushf(&format_patch.args, "%s...%s", | |
| 668 | oid_to_hex(opts->root ? | |
| 669 | /* this is now equivalent to !opts->upstream */ | |
| 670 | &opts->onto->object.oid : | |
| 671 | &opts->upstream->object.oid), | |
| 672 | oid_to_hex(&opts->orig_head->object.oid)); | |
| 673 | if (opts->restrict_revision) | |
| 674 | strvec_pushf(&format_patch.args, "^%s", | |
| 675 | oid_to_hex(&opts->restrict_revision->object.oid)); | |
| 676 | ||
| 677 | status = run_command(&format_patch); | |
| 678 | if (status) { | |
| 679 | struct reset_head_opts ropts = { 0 }; | |
| 680 | unlink(rebased_patches); | |
| 681 | free(rebased_patches); | |
| 682 | child_process_clear(&am); | |
| 683 | ||
| 684 | ropts.oid = &opts->orig_head->object.oid; | |
| 685 | ropts.branch = opts->head_name; | |
| 686 | ropts.default_reflog_action = opts->reflog_action; | |
| 687 | reset_head(the_repository, &ropts); | |
| 688 | error(_("\ngit encountered an error while preparing the " | |
| 689 | "patches to replay\n" | |
| 690 | "these revisions:\n" | |
| 691 | "\n %s\n\n" | |
| 692 | "As a result, git cannot rebase them."), | |
| 693 | opts->revisions); | |
| 694 | ||
| 695 | return status; | |
| 696 | } | |
| 697 | ||
| 698 | am.in = open(rebased_patches, O_RDONLY); | |
| 699 | if (am.in < 0) { | |
| 700 | status = error_errno(_("could not open '%s' for reading"), | |
| 701 | rebased_patches); | |
| 702 | free(rebased_patches); | |
| 703 | child_process_clear(&am); | |
| 704 | return status; | |
| 705 | } | |
| 706 | ||
| 707 | strvec_pushv(&am.args, opts->git_am_opts.v); | |
| 708 | strvec_push(&am.args, "--rebasing"); | |
| 709 | strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); | |
| 710 | strvec_push(&am.args, "--patch-format=mboxrd"); | |
| 711 | if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE) | |
| 712 | strvec_push(&am.args, "--rerere-autoupdate"); | |
| 713 | else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE) | |
| 714 | strvec_push(&am.args, "--no-rerere-autoupdate"); | |
| 715 | if (opts->gpg_sign_opt) | |
| 716 | strvec_push(&am.args, opts->gpg_sign_opt); | |
| 717 | status = run_command(&am); | |
| 718 | unlink(rebased_patches); | |
| 719 | free(rebased_patches); | |
| 720 | ||
| 721 | if (!status) { | |
| 722 | return move_to_original_branch(opts); | |
| 723 | } | |
| 724 | ||
| 725 | if (is_directory(opts->state_dir)) | |
| 726 | rebase_write_basic_state(opts); | |
| 727 | ||
| 728 | return status; | |
| 729 | } | |
| 730 | ||
| 731 | static int run_specific_rebase(struct rebase_options *opts) | |
| 732 | { | |
| 733 | int status; | |
| 734 | ||
| 735 | if (opts->type == REBASE_MERGE) { | |
| 736 | /* Run sequencer-based rebase */ | |
| 737 | if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) | |
| 738 | setenv("GIT_SEQUENCE_EDITOR", ":", 1); | |
| 739 | if (opts->gpg_sign_opt) { | |
| 740 | /* remove the leading "-S" */ | |
| 741 | char *tmp = xstrdup(opts->gpg_sign_opt + 2); | |
| 742 | free(opts->gpg_sign_opt); | |
| 743 | opts->gpg_sign_opt = tmp; | |
| 744 | } | |
| 745 | ||
| 746 | status = run_sequencer_rebase(opts); | |
| 747 | } else if (opts->type == REBASE_APPLY) | |
| 748 | status = run_am(opts); | |
| 749 | else | |
| 750 | BUG("Unhandled rebase type %d", opts->type); | |
| 751 | ||
| 752 | if (opts->dont_finish_rebase) | |
| 753 | ; /* do nothing */ | |
| 754 | else if (opts->type == REBASE_MERGE) | |
| 755 | ; /* merge backend cleans up after itself */ | |
| 756 | else if (status == 0) { | |
| 757 | if (!file_exists(state_dir_path("stopped-sha", opts))) | |
| 758 | finish_rebase(opts); | |
| 759 | } else if (status == 2) { | |
| 760 | struct strbuf dir = STRBUF_INIT; | |
| 761 | ||
| 762 | apply_autostash(state_dir_path("autostash", opts)); | |
| 763 | strbuf_addstr(&dir, opts->state_dir); | |
| 764 | remove_dir_recursively(&dir, 0); | |
| 765 | strbuf_release(&dir); | |
| 766 | die("Nothing to do"); | |
| 767 | } | |
| 768 | ||
| 769 | return status ? -1 : 0; | |
| 770 | } | |
| 771 | ||
| 772 | static void parse_rebase_merges_value(struct rebase_options *options, const char *value) | |
| 773 | { | |
| 774 | if (!strcmp("no-rebase-cousins", value)) | |
| 775 | options->rebase_cousins = 0; | |
| 776 | else if (!strcmp("rebase-cousins", value)) | |
| 777 | options->rebase_cousins = 1; | |
| 778 | else | |
| 779 | die(_("Unknown rebase-merges mode: %s"), value); | |
| 780 | } | |
| 781 | ||
| 782 | static int rebase_config(const char *var, const char *value, | |
| 783 | const struct config_context *ctx, void *data) | |
| 784 | { | |
| 785 | struct rebase_options *opts = data; | |
| 786 | ||
| 787 | if (!strcmp(var, "rebase.stat")) { | |
| 788 | if (git_config_bool(var, value)) | |
| 789 | opts->flags |= REBASE_DIFFSTAT; | |
| 790 | else | |
| 791 | opts->flags &= ~REBASE_DIFFSTAT; | |
| 792 | return 0; | |
| 793 | } | |
| 794 | ||
| 795 | if (!strcmp(var, "rebase.autosquash")) { | |
| 796 | opts->config_autosquash = git_config_bool(var, value); | |
| 797 | return 0; | |
| 798 | } | |
| 799 | ||
| 800 | if (!strcmp(var, "commit.gpgsign")) { | |
| 801 | free(opts->gpg_sign_opt); | |
| 802 | opts->gpg_sign_opt = git_config_bool(var, value) ? | |
| 803 | xstrdup("-S") : NULL; | |
| 804 | return 0; | |
| 805 | } | |
| 806 | ||
| 807 | if (!strcmp(var, "rebase.autostash")) { | |
| 808 | opts->autostash = git_config_bool(var, value); | |
| 809 | return 0; | |
| 810 | } | |
| 811 | ||
| 812 | if (!strcmp(var, "rebase.rebasemerges")) { | |
| 813 | opts->config_rebase_merges = git_parse_maybe_bool(value); | |
| 814 | if (opts->config_rebase_merges < 0) { | |
| 815 | opts->config_rebase_merges = 1; | |
| 816 | parse_rebase_merges_value(opts, value); | |
| 817 | } else { | |
| 818 | opts->rebase_cousins = 0; | |
| 819 | } | |
| 820 | return 0; | |
| 821 | } | |
| 822 | ||
| 823 | if (!strcmp(var, "rebase.updaterefs")) { | |
| 824 | opts->config_update_refs = git_config_bool(var, value); | |
| 825 | return 0; | |
| 826 | } | |
| 827 | ||
| 828 | if (!strcmp(var, "rebase.reschedulefailedexec")) { | |
| 829 | opts->reschedule_failed_exec = git_config_bool(var, value); | |
| 830 | return 0; | |
| 831 | } | |
| 832 | ||
| 833 | if (!strcmp(var, "rebase.forkpoint")) { | |
| 834 | opts->fork_point = git_config_bool(var, value) ? -1 : 0; | |
| 835 | return 0; | |
| 836 | } | |
| 837 | ||
| 838 | if (!strcmp(var, "rebase.backend")) { | |
| 839 | FREE_AND_NULL(opts->default_backend); | |
| 840 | return git_config_string(&opts->default_backend, var, value); | |
| 841 | } | |
| 842 | ||
| 843 | return git_default_config(var, value, ctx, data); | |
| 844 | } | |
| 845 | ||
| 846 | static int checkout_up_to_date(struct rebase_options *options) | |
| 847 | { | |
| 848 | struct strbuf buf = STRBUF_INIT; | |
| 849 | struct reset_head_opts ropts = { 0 }; | |
| 850 | int ret = 0; | |
| 851 | ||
| 852 | strbuf_addf(&buf, "%s: checkout %s", | |
| 853 | options->reflog_action, options->switch_to); | |
| 854 | ropts.oid = &options->orig_head->object.oid; | |
| 855 | ropts.branch = options->head_name; | |
| 856 | ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK; | |
| 857 | if (!ropts.branch) | |
| 858 | ropts.flags |= RESET_HEAD_DETACH; | |
| 859 | ropts.head_msg = buf.buf; | |
| 860 | if (reset_head(the_repository, &ropts) < 0) | |
| 861 | ret = error(_("could not switch to %s"), options->switch_to); | |
| 862 | strbuf_release(&buf); | |
| 863 | ||
| 864 | return ret; | |
| 865 | } | |
| 866 | ||
| 867 | /* | |
| 868 | * Determines whether the commits in from..to are linear, i.e. contain | |
| 869 | * no merge commits. This function *expects* `from` to be an ancestor of | |
| 870 | * `to`. | |
| 871 | */ | |
| 872 | static int is_linear_history(struct commit *from, struct commit *to) | |
| 873 | { | |
| 874 | while (to && to != from) { | |
| 875 | repo_parse_commit(the_repository, to); | |
| 876 | if (!to->parents) | |
| 877 | return 1; | |
| 878 | if (to->parents->next) | |
| 879 | return 0; | |
| 880 | to = to->parents->item; | |
| 881 | } | |
| 882 | return 1; | |
| 883 | } | |
| 884 | ||
| 885 | static int can_fast_forward(struct commit *onto, struct commit *upstream, | |
| 886 | struct commit *restrict_revision, | |
| 887 | struct commit *head, struct object_id *branch_base) | |
| 888 | { | |
| 889 | struct commit_list *merge_bases = NULL; | |
| 890 | int res = 0; | |
| 891 | ||
| 892 | if (is_null_oid(branch_base)) | |
| 893 | goto done; /* fill_branch_base() found multiple merge bases */ | |
| 894 | ||
| 895 | if (!oideq(branch_base, &onto->object.oid)) | |
| 896 | goto done; | |
| 897 | ||
| 898 | if (restrict_revision && !oideq(&restrict_revision->object.oid, branch_base)) | |
| 899 | goto done; | |
| 900 | ||
| 901 | if (!upstream) | |
| 902 | goto done; | |
| 903 | ||
| 904 | if (repo_get_merge_bases(the_repository, upstream, head, &merge_bases) < 0) | |
| 905 | exit(128); | |
| 906 | if (!merge_bases || merge_bases->next) | |
| 907 | goto done; | |
| 908 | ||
| 909 | if (!oideq(&onto->object.oid, &merge_bases->item->object.oid)) | |
| 910 | goto done; | |
| 911 | ||
| 912 | res = 1; | |
| 913 | ||
| 914 | done: | |
| 915 | free_commit_list(merge_bases); | |
| 916 | return res && is_linear_history(onto, head); | |
| 917 | } | |
| 918 | ||
| 919 | static void fill_branch_base(struct rebase_options *options, | |
| 920 | struct object_id *branch_base) | |
| 921 | { | |
| 922 | struct commit_list *merge_bases = NULL; | |
| 923 | ||
| 924 | if (repo_get_merge_bases(the_repository, options->onto, | |
| 925 | options->orig_head, &merge_bases) < 0) | |
| 926 | exit(128); | |
| 927 | if (!merge_bases || merge_bases->next) | |
| 928 | oidcpy(branch_base, null_oid(the_hash_algo)); | |
| 929 | else | |
| 930 | oidcpy(branch_base, &merge_bases->item->object.oid); | |
| 931 | ||
| 932 | free_commit_list(merge_bases); | |
| 933 | } | |
| 934 | ||
| 935 | static int parse_opt_am(const struct option *opt, const char *arg, int unset) | |
| 936 | { | |
| 937 | struct rebase_options *opts = opt->value; | |
| 938 | ||
| 939 | BUG_ON_OPT_NEG(unset); | |
| 940 | BUG_ON_OPT_ARG(arg); | |
| 941 | ||
| 942 | if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_APPLY) | |
| 943 | die(_("apply options and merge options cannot be used together")); | |
| 944 | ||
| 945 | opts->type = REBASE_APPLY; | |
| 946 | ||
| 947 | return 0; | |
| 948 | } | |
| 949 | ||
| 950 | /* -i followed by -m is still -i */ | |
| 951 | static int parse_opt_merge(const struct option *opt, const char *arg, int unset) | |
| 952 | { | |
| 953 | struct rebase_options *opts = opt->value; | |
| 954 | ||
| 955 | BUG_ON_OPT_NEG(unset); | |
| 956 | BUG_ON_OPT_ARG(arg); | |
| 957 | ||
| 958 | if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE) | |
| 959 | die(_("apply options and merge options cannot be used together")); | |
| 960 | ||
| 961 | opts->type = REBASE_MERGE; | |
| 962 | ||
| 963 | return 0; | |
| 964 | } | |
| 965 | ||
| 966 | /* -i followed by -r is still explicitly interactive, but -r alone is not */ | |
| 967 | static int parse_opt_interactive(const struct option *opt, const char *arg, | |
| 968 | int unset) | |
| 969 | { | |
| 970 | struct rebase_options *opts = opt->value; | |
| 971 | ||
| 972 | BUG_ON_OPT_NEG(unset); | |
| 973 | BUG_ON_OPT_ARG(arg); | |
| 974 | ||
| 975 | if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE) | |
| 976 | die(_("apply options and merge options cannot be used together")); | |
| 977 | ||
| 978 | opts->type = REBASE_MERGE; | |
| 979 | opts->flags |= REBASE_INTERACTIVE_EXPLICIT; | |
| 980 | ||
| 981 | return 0; | |
| 982 | } | |
| 983 | ||
| 984 | static enum empty_type parse_empty_value(const char *value) | |
| 985 | { | |
| 986 | if (!strcasecmp(value, "drop")) | |
| 987 | return EMPTY_DROP; | |
| 988 | else if (!strcasecmp(value, "keep")) | |
| 989 | return EMPTY_KEEP; | |
| 990 | else if (!strcasecmp(value, "stop")) | |
| 991 | return EMPTY_STOP; | |
| 992 | else if (!strcasecmp(value, "ask")) { | |
| 993 | warning(_("--empty=ask is deprecated; use '--empty=stop' instead.")); | |
| 994 | return EMPTY_STOP; | |
| 995 | } | |
| 996 | ||
| 997 | die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value); | |
| 998 | } | |
| 999 | ||
| 1000 | static int parse_opt_keep_empty(const struct option *opt, const char *arg, | |
| 1001 | int unset) | |
| 1002 | { | |
| 1003 | struct rebase_options *opts = opt->value; | |
| 1004 | ||
| 1005 | BUG_ON_OPT_ARG(arg); | |
| 1006 | ||
| 1007 | imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty"); | |
| 1008 | opts->keep_empty = !unset; | |
| 1009 | return 0; | |
| 1010 | } | |
| 1011 | ||
| 1012 | static int parse_opt_empty(const struct option *opt, const char *arg, int unset) | |
| 1013 | { | |
| 1014 | struct rebase_options *options = opt->value; | |
| 1015 | enum empty_type value = parse_empty_value(arg); | |
| 1016 | ||
| 1017 | BUG_ON_OPT_NEG(unset); | |
| 1018 | ||
| 1019 | options->empty = value; | |
| 1020 | return 0; | |
| 1021 | } | |
| 1022 | ||
| 1023 | static int parse_opt_rebase_merges(const struct option *opt, const char *arg, int unset) | |
| 1024 | { | |
| 1025 | struct rebase_options *options = opt->value; | |
| 1026 | ||
| 1027 | options->rebase_merges = !unset; | |
| 1028 | options->rebase_cousins = 0; | |
| 1029 | ||
| 1030 | if (arg) { | |
| 1031 | if (!*arg) { | |
| 1032 | warning(_("--rebase-merges with an empty string " | |
| 1033 | "argument is deprecated and will stop " | |
| 1034 | "working in a future version of Git. Use " | |
| 1035 | "--rebase-merges without an argument " | |
| 1036 | "instead, which does the same thing.")); | |
| 1037 | return 0; | |
| 1038 | } | |
| 1039 | parse_rebase_merges_value(options, arg); | |
| 1040 | } | |
| 1041 | ||
| 1042 | return 0; | |
| 1043 | } | |
| 1044 | ||
| 1045 | static void NORETURN error_on_missing_default_upstream(void) | |
| 1046 | { | |
| 1047 | struct branch *current_branch = branch_get(NULL); | |
| 1048 | ||
| 1049 | printf(_("%s\n" | |
| 1050 | "Please specify which branch you want to rebase against.\n" | |
| 1051 | "See git-rebase(1) for details.\n" | |
| 1052 | "\n" | |
| 1053 | " git rebase '<branch>'\n" | |
| 1054 | "\n"), | |
| 1055 | current_branch ? _("There is no tracking information for " | |
| 1056 | "the current branch.") : | |
| 1057 | _("You are not currently on a branch.")); | |
| 1058 | ||
| 1059 | if (current_branch) { | |
| 1060 | const char *remote = current_branch->remote_name; | |
| 1061 | ||
| 1062 | if (!remote) | |
| 1063 | remote = _("<remote>"); | |
| 1064 | ||
| 1065 | printf(_("If you wish to set tracking information for this " | |
| 1066 | "branch you can do so with:\n" | |
| 1067 | "\n" | |
| 1068 | " git branch --set-upstream-to=%s/<branch> %s\n" | |
| 1069 | "\n"), | |
| 1070 | remote, current_branch->name); | |
| 1071 | } | |
| 1072 | exit(1); | |
| 1073 | } | |
| 1074 | ||
| 1075 | static int check_exec_cmd(const char *cmd) | |
| 1076 | { | |
| 1077 | if (strchr(cmd, '\n')) | |
| 1078 | return error(_("exec commands cannot contain newlines")); | |
| 1079 | ||
| 1080 | /* Does the command consist purely of whitespace? */ | |
| 1081 | if (!cmd[strspn(cmd, " \t\r\f\v")]) | |
| 1082 | return error(_("empty exec command")); | |
| 1083 | ||
| 1084 | return 0; | |
| 1085 | } | |
| 1086 | ||
| 1087 | int cmd_rebase(int argc, | |
| 1088 | const char **argv, | |
| 1089 | const char *prefix, | |
| 1090 | struct repository *repo UNUSED) | |
| 1091 | { | |
| 1092 | struct rebase_options options = REBASE_OPTIONS_INIT; | |
| 1093 | const char *branch_name; | |
| 1094 | const char *strategy_opt = NULL; | |
| 1095 | int ret, flags, total_argc, in_progress = 0; | |
| 1096 | int keep_base = 0; | |
| 1097 | int ok_to_skip_pre_rebase = 0; | |
| 1098 | struct strbuf msg = STRBUF_INIT; | |
| 1099 | struct strbuf revisions = STRBUF_INIT; | |
| 1100 | struct strbuf buf = STRBUF_INIT; | |
| 1101 | struct object_id branch_base; | |
| 1102 | int ignore_whitespace = 0; | |
| 1103 | const char *gpg_sign = NULL; | |
| 1104 | struct object_id squash_onto; | |
| 1105 | char *squash_onto_name = NULL; | |
| 1106 | char *keep_base_onto_name = NULL; | |
| 1107 | int reschedule_failed_exec = -1; | |
| 1108 | int allow_preemptive_ff = 1; | |
| 1109 | int preserve_merges_selected = 0; | |
| 1110 | struct reset_head_opts ropts = { 0 }; | |
| 1111 | struct option builtin_rebase_options[] = { | |
| 1112 | OPT_STRING(0, "onto", &options.onto_name, | |
| 1113 | N_("revision"), | |
| 1114 | N_("rebase onto given branch instead of upstream")), | |
| 1115 | OPT_BOOL(0, "keep-base", &keep_base, | |
| 1116 | N_("use the merge-base of upstream and branch as the current base")), | |
| 1117 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, | |
| 1118 | N_("allow pre-rebase hook to run")), | |
| 1119 | OPT_NEGBIT('q', "quiet", &options.flags, | |
| 1120 | N_("be quiet. implies --no-stat"), | |
| 1121 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), | |
| 1122 | OPT_BIT('v', "verbose", &options.flags, | |
| 1123 | N_("display a diffstat of what changed upstream"), | |
| 1124 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), | |
| 1125 | { | |
| 1126 | .type = OPTION_NEGBIT, | |
| 1127 | .short_name = 'n', | |
| 1128 | .long_name = "no-stat", | |
| 1129 | .value = &options.flags, | |
| 1130 | .precision = sizeof(options.flags), | |
| 1131 | .help = N_("do not show diffstat of what changed upstream"), | |
| 1132 | .flags = PARSE_OPT_NOARG, | |
| 1133 | .defval = REBASE_DIFFSTAT, | |
| 1134 | }, | |
| 1135 | OPT_BOOL(0, "signoff", &options.signoff, | |
| 1136 | N_("add a Signed-off-by trailer to each commit")), | |
| 1137 | OPT_BOOL(0, "committer-date-is-author-date", | |
| 1138 | &options.committer_date_is_author_date, | |
| 1139 | N_("make committer date match author date")), | |
| 1140 | OPT_BOOL(0, "reset-author-date", &options.ignore_date, | |
| 1141 | N_("ignore author date and use current date")), | |
| 1142 | OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date, | |
| 1143 | N_("synonym of --reset-author-date")), | |
| 1144 | OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"), | |
| 1145 | N_("passed to 'git apply'"), 0), | |
| 1146 | OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace, | |
| 1147 | N_("ignore changes in whitespace")), | |
| 1148 | OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts, | |
| 1149 | N_("action"), N_("passed to 'git apply'"), 0), | |
| 1150 | OPT_BIT('f', "force-rebase", &options.flags, | |
| 1151 | N_("cherry-pick all commits, even if unchanged"), | |
| 1152 | REBASE_FORCE), | |
| 1153 | OPT_BIT(0, "no-ff", &options.flags, | |
| 1154 | N_("cherry-pick all commits, even if unchanged"), | |
| 1155 | REBASE_FORCE), | |
| 1156 | OPT_CMDMODE(0, "continue", &options.action, N_("continue"), | |
| 1157 | ACTION_CONTINUE), | |
| 1158 | OPT_CMDMODE(0, "skip", &options.action, | |
| 1159 | N_("skip current patch and continue"), ACTION_SKIP), | |
| 1160 | OPT_CMDMODE(0, "abort", &options.action, | |
| 1161 | N_("abort and check out the original branch"), | |
| 1162 | ACTION_ABORT), | |
| 1163 | OPT_CMDMODE(0, "quit", &options.action, | |
| 1164 | N_("abort but keep HEAD where it is"), ACTION_QUIT), | |
| 1165 | OPT_CMDMODE(0, "edit-todo", &options.action, N_("edit the todo list " | |
| 1166 | "during an interactive rebase"), ACTION_EDIT_TODO), | |
| 1167 | OPT_CMDMODE(0, "show-current-patch", &options.action, | |
| 1168 | N_("show the patch file being applied or merged"), | |
| 1169 | ACTION_SHOW_CURRENT_PATCH), | |
| 1170 | OPT_CALLBACK_F(0, "apply", &options, NULL, | |
| 1171 | N_("use apply strategies to rebase"), | |
| 1172 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
| 1173 | parse_opt_am), | |
| 1174 | OPT_CALLBACK_F('m', "merge", &options, NULL, | |
| 1175 | N_("use merging strategies to rebase"), | |
| 1176 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
| 1177 | parse_opt_merge), | |
| 1178 | OPT_CALLBACK_F('i', "interactive", &options, NULL, | |
| 1179 | N_("let the user edit the list of commits to rebase"), | |
| 1180 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
| 1181 | parse_opt_interactive), | |
| 1182 | OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected, | |
| 1183 | N_("(REMOVED) was: try to recreate merges " | |
| 1184 | "instead of ignoring them"), | |
| 1185 | 1, PARSE_OPT_HIDDEN), | |
| 1186 | OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate), | |
| 1187 | OPT_CALLBACK_F(0, "empty", &options, "(drop|keep|stop)", | |
| 1188 | N_("how to handle commits that become empty"), | |
| 1189 | PARSE_OPT_NONEG, parse_opt_empty), | |
| 1190 | OPT_CALLBACK_F('k', "keep-empty", &options, NULL, | |
| 1191 | N_("keep commits which start empty"), | |
| 1192 | PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, | |
| 1193 | parse_opt_keep_empty), | |
| 1194 | OPT_BOOL(0, "autosquash", &options.autosquash, | |
| 1195 | N_("move commits that begin with " | |
| 1196 | "squash!/fixup! under -i")), | |
| 1197 | OPT_BOOL(0, "update-refs", &options.update_refs, | |
| 1198 | N_("update branches that point to commits " | |
| 1199 | "that are being rebased")), | |
| 1200 | { | |
| 1201 | .type = OPTION_STRING, | |
| 1202 | .short_name = 'S', | |
| 1203 | .long_name = "gpg-sign", | |
| 1204 | .value = &gpg_sign, | |
| 1205 | .argh = N_("key-id"), | |
| 1206 | .help = N_("GPG-sign commits"), | |
| 1207 | .flags = PARSE_OPT_OPTARG, | |
| 1208 | .defval = (intptr_t) "", | |
| 1209 | }, | |
| 1210 | OPT_AUTOSTASH(&options.autostash), | |
| 1211 | OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"), | |
| 1212 | N_("add exec lines after each commit of the " | |
| 1213 | "editable list")), | |
| 1214 | OPT_BOOL_F(0, "allow-empty-message", | |
| 1215 | &options.allow_empty_message, | |
| 1216 | N_("allow rebasing commits with empty messages"), | |
| 1217 | PARSE_OPT_HIDDEN), | |
| 1218 | OPT_CALLBACK_F('r', "rebase-merges", &options, N_("mode"), | |
| 1219 | N_("try to rebase merges instead of skipping them"), | |
| 1220 | PARSE_OPT_OPTARG, parse_opt_rebase_merges), | |
| 1221 | OPT_BOOL(0, "fork-point", &options.fork_point, | |
| 1222 | N_("use 'merge-base --fork-point' to refine upstream")), | |
| 1223 | OPT_STRING('s', "strategy", &strategy_opt, | |
| 1224 | N_("strategy"), N_("use the given merge strategy")), | |
| 1225 | OPT_STRING_LIST('X', "strategy-option", &options.strategy_opts, | |
| 1226 | N_("option"), | |
| 1227 | N_("pass the argument through to the merge " | |
| 1228 | "strategy")), | |
| 1229 | OPT_BOOL(0, "root", &options.root, | |
| 1230 | N_("rebase all reachable commits up to the root(s)")), | |
| 1231 | OPT_BOOL(0, "reschedule-failed-exec", | |
| 1232 | &reschedule_failed_exec, | |
| 1233 | N_("automatically re-schedule any `exec` that fails")), | |
| 1234 | OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks, | |
| 1235 | N_("apply all changes, even those already present upstream")), | |
| 1236 | OPT_END(), | |
| 1237 | }; | |
| 1238 | int i; | |
| 1239 | ||
| 1240 | show_usage_with_options_if_asked(argc, argv, | |
| 1241 | builtin_rebase_usage, | |
| 1242 | builtin_rebase_options); | |
| 1243 | ||
| 1244 | #ifndef WITH_BREAKING_CHANGES | |
| 1245 | warn_on_auto_comment_char = true; | |
| 1246 | #endif /* !WITH_BREAKING_CHANGES */ | |
| 1247 | prepare_repo_settings(the_repository); | |
| 1248 | the_repository->settings.command_requires_full_index = 0; | |
| 1249 | ||
| 1250 | repo_config(the_repository, rebase_config, &options); | |
| 1251 | /* options.gpg_sign_opt will be either "-S" or NULL */ | |
| 1252 | gpg_sign = options.gpg_sign_opt ? "" : NULL; | |
| 1253 | FREE_AND_NULL(options.gpg_sign_opt); | |
| 1254 | ||
| 1255 | strbuf_reset(&buf); | |
| 1256 | strbuf_addf(&buf, "%s/applying", apply_dir()); | |
| 1257 | if(file_exists(buf.buf)) | |
| 1258 | die(_("It looks like 'git am' is in progress. Cannot rebase.")); | |
| 1259 | ||
| 1260 | if (is_directory(apply_dir())) { | |
| 1261 | options.type = REBASE_APPLY; | |
| 1262 | options.state_dir = apply_dir(); | |
| 1263 | } else if (is_directory(merge_dir())) { | |
| 1264 | strbuf_reset(&buf); | |
| 1265 | strbuf_addf(&buf, "%s/rewritten", merge_dir()); | |
| 1266 | if (!(options.action == ACTION_ABORT) && is_directory(buf.buf)) { | |
| 1267 | die(_("`rebase --preserve-merges` (-p) is no longer supported.\n" | |
| 1268 | "Use `git rebase --abort` to terminate current rebase.\n" | |
| 1269 | "Or downgrade to v2.33, or earlier, to complete the rebase.")); | |
| 1270 | } else { | |
| 1271 | strbuf_reset(&buf); | |
| 1272 | strbuf_addf(&buf, "%s/interactive", merge_dir()); | |
| 1273 | options.type = REBASE_MERGE; | |
| 1274 | if (file_exists(buf.buf)) | |
| 1275 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
| 1276 | } | |
| 1277 | options.state_dir = merge_dir(); | |
| 1278 | } | |
| 1279 | ||
| 1280 | if (options.type != REBASE_UNSPECIFIED) | |
| 1281 | in_progress = 1; | |
| 1282 | ||
| 1283 | total_argc = argc; | |
| 1284 | argc = parse_options(argc, argv, prefix, | |
| 1285 | builtin_rebase_options, | |
| 1286 | builtin_rebase_usage, 0); | |
| 1287 | ||
| 1288 | if (preserve_merges_selected) | |
| 1289 | die(_("--preserve-merges was replaced by --rebase-merges\n" | |
| 1290 | "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n" | |
| 1291 | "which is no longer supported; use 'merges' instead")); | |
| 1292 | ||
| 1293 | if (options.action != ACTION_NONE && total_argc != 2) { | |
| 1294 | usage_with_options(builtin_rebase_usage, | |
| 1295 | builtin_rebase_options); | |
| 1296 | } | |
| 1297 | ||
| 1298 | if (argc > 2) | |
| 1299 | usage_with_options(builtin_rebase_usage, | |
| 1300 | builtin_rebase_options); | |
| 1301 | ||
| 1302 | if (keep_base) { | |
| 1303 | if (options.onto_name) | |
| 1304 | die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto"); | |
| 1305 | if (options.root) | |
| 1306 | die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root"); | |
| 1307 | /* | |
| 1308 | * --keep-base defaults to --no-fork-point to keep the | |
| 1309 | * base the same. | |
| 1310 | */ | |
| 1311 | if (options.fork_point < 0) | |
| 1312 | options.fork_point = 0; | |
| 1313 | } | |
| 1314 | if (options.root && options.fork_point > 0) | |
| 1315 | die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point"); | |
| 1316 | ||
| 1317 | if (options.action != ACTION_NONE && !in_progress) | |
| 1318 | die(_("no rebase in progress")); | |
| 1319 | ||
| 1320 | if (options.action == ACTION_EDIT_TODO && !is_merge(&options)) | |
| 1321 | die(_("The --edit-todo action can only be used during " | |
| 1322 | "interactive rebase.")); | |
| 1323 | ||
| 1324 | if (trace2_is_enabled()) { | |
| 1325 | if (is_merge(&options)) | |
| 1326 | trace2_cmd_mode("interactive"); | |
| 1327 | else if (options.exec.nr) | |
| 1328 | trace2_cmd_mode("interactive-exec"); | |
| 1329 | else | |
| 1330 | trace2_cmd_mode(action_names[options.action]); | |
| 1331 | } | |
| 1332 | ||
| 1333 | options.reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
| 1334 | options.reflog_action = | |
| 1335 | xstrdup(options.reflog_action ? options.reflog_action : "rebase"); | |
| 1336 | ||
| 1337 | switch (options.action) { | |
| 1338 | case ACTION_CONTINUE: { | |
| 1339 | struct object_id head; | |
| 1340 | struct lock_file lock_file = LOCK_INIT; | |
| 1341 | int fd; | |
| 1342 | ||
| 1343 | /* Sanity check */ | |
| 1344 | if (repo_get_oid(the_repository, "HEAD", &head)) | |
| 1345 | die(_("Cannot read HEAD")); | |
| 1346 | ||
| 1347 | fd = repo_hold_locked_index(the_repository, &lock_file, 0); | |
| 1348 | if (repo_read_index(the_repository) < 0) | |
| 1349 | die(_("could not read index")); | |
| 1350 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, | |
| 1351 | NULL); | |
| 1352 | if (0 <= fd) | |
| 1353 | repo_update_index_if_able(the_repository, &lock_file); | |
| 1354 | rollback_lock_file(&lock_file); | |
| 1355 | ||
| 1356 | if (has_unstaged_changes(the_repository, 1)) { | |
| 1357 | puts(_("You must edit all merge conflicts and then\n" | |
| 1358 | "mark them as resolved using git add")); | |
| 1359 | exit(1); | |
| 1360 | } | |
| 1361 | if (read_basic_state(&options)) | |
| 1362 | exit(1); | |
| 1363 | goto run_rebase; | |
| 1364 | } | |
| 1365 | case ACTION_SKIP: { | |
| 1366 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
| 1367 | ||
| 1368 | rerere_clear(the_repository, &merge_rr); | |
| 1369 | string_list_clear(&merge_rr, 1); | |
| 1370 | ropts.flags = RESET_HEAD_HARD; | |
| 1371 | if (reset_head(the_repository, &ropts) < 0) | |
| 1372 | die(_("could not discard worktree changes")); | |
| 1373 | remove_branch_state(the_repository, 0); | |
| 1374 | if (read_basic_state(&options)) | |
| 1375 | exit(1); | |
| 1376 | goto run_rebase; | |
| 1377 | } | |
| 1378 | case ACTION_ABORT: { | |
| 1379 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
| 1380 | struct strbuf head_msg = STRBUF_INIT; | |
| 1381 | ||
| 1382 | rerere_clear(the_repository, &merge_rr); | |
| 1383 | string_list_clear(&merge_rr, 1); | |
| 1384 | ||
| 1385 | if (read_basic_state(&options)) | |
| 1386 | exit(1); | |
| 1387 | ||
| 1388 | strbuf_addf(&head_msg, "%s (abort): returning to %s", | |
| 1389 | options.reflog_action, | |
| 1390 | options.head_name ? options.head_name | |
| 1391 | : oid_to_hex(&options.orig_head->object.oid)); | |
| 1392 | ropts.oid = &options.orig_head->object.oid; | |
| 1393 | ropts.head_msg = head_msg.buf; | |
| 1394 | ropts.branch = options.head_name; | |
| 1395 | ropts.flags = RESET_HEAD_HARD; | |
| 1396 | if (reset_head(the_repository, &ropts) < 0) | |
| 1397 | die(_("could not move back to %s"), | |
| 1398 | oid_to_hex(&options.orig_head->object.oid)); | |
| 1399 | strbuf_release(&head_msg); | |
| 1400 | remove_branch_state(the_repository, 0); | |
| 1401 | ret = finish_rebase(&options); | |
| 1402 | goto cleanup; | |
| 1403 | } | |
| 1404 | case ACTION_QUIT: { | |
| 1405 | save_autostash(state_dir_path("autostash", &options)); | |
| 1406 | if (options.type == REBASE_MERGE) { | |
| 1407 | struct replay_opts replay = REPLAY_OPTS_INIT; | |
| 1408 | ||
| 1409 | replay.action = REPLAY_INTERACTIVE_REBASE; | |
| 1410 | ret = sequencer_remove_state(&replay); | |
| 1411 | replay_opts_release(&replay); | |
| 1412 | } else { | |
| 1413 | strbuf_reset(&buf); | |
| 1414 | strbuf_addstr(&buf, options.state_dir); | |
| 1415 | ret = remove_dir_recursively(&buf, 0); | |
| 1416 | if (ret) | |
| 1417 | error(_("could not remove '%s'"), | |
| 1418 | options.state_dir); | |
| 1419 | } | |
| 1420 | goto cleanup; | |
| 1421 | } | |
| 1422 | case ACTION_EDIT_TODO: | |
| 1423 | options.dont_finish_rebase = 1; | |
| 1424 | goto run_rebase; | |
| 1425 | case ACTION_SHOW_CURRENT_PATCH: | |
| 1426 | options.dont_finish_rebase = 1; | |
| 1427 | goto run_rebase; | |
| 1428 | case ACTION_NONE: | |
| 1429 | break; | |
| 1430 | default: | |
| 1431 | BUG("action: %d", options.action); | |
| 1432 | } | |
| 1433 | ||
| 1434 | /* Make sure no rebase is in progress */ | |
| 1435 | if (in_progress) { | |
| 1436 | const char *last_slash = strrchr(options.state_dir, '/'); | |
| 1437 | const char *state_dir_base = | |
| 1438 | last_slash ? last_slash + 1 : options.state_dir; | |
| 1439 | const char *cmd_live_rebase = | |
| 1440 | "git rebase (--continue | --abort | --skip)"; | |
| 1441 | strbuf_reset(&buf); | |
| 1442 | strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); | |
| 1443 | die(_("It seems that there is already a %s directory, and\n" | |
| 1444 | "I wonder if you are in the middle of another rebase. " | |
| 1445 | "If that is the\n" | |
| 1446 | "case, please try\n\t%s\n" | |
| 1447 | "If that is not the case, please\n\t%s\n" | |
| 1448 | "and run me again. I am stopping in case you still " | |
| 1449 | "have something\n" | |
| 1450 | "valuable there.\n"), | |
| 1451 | state_dir_base, cmd_live_rebase, buf.buf); | |
| 1452 | } | |
| 1453 | ||
| 1454 | if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) || | |
| 1455 | (options.action != ACTION_NONE) || | |
| 1456 | (options.exec.nr > 0) || | |
| 1457 | options.autosquash == 1) { | |
| 1458 | allow_preemptive_ff = 0; | |
| 1459 | } | |
| 1460 | if (options.committer_date_is_author_date || options.ignore_date) | |
| 1461 | options.flags |= REBASE_FORCE; | |
| 1462 | ||
| 1463 | for (i = 0; i < options.git_am_opts.nr; i++) { | |
| 1464 | const char *option = options.git_am_opts.v[i], *p; | |
| 1465 | if (!strcmp(option, "--whitespace=fix") || | |
| 1466 | !strcmp(option, "--whitespace=strip")) | |
| 1467 | allow_preemptive_ff = 0; | |
| 1468 | else if (skip_prefix(option, "-C", &p)) { | |
| 1469 | while (*p) | |
| 1470 | if (!isdigit(*(p++))) | |
| 1471 | die(_("switch `C' expects a " | |
| 1472 | "numerical value")); | |
| 1473 | } else if (skip_prefix(option, "--whitespace=", &p)) { | |
| 1474 | if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") && | |
| 1475 | strcmp(p, "error") && strcmp(p, "error-all")) | |
| 1476 | die("Invalid whitespace option: '%s'", p); | |
| 1477 | } | |
| 1478 | } | |
| 1479 | ||
| 1480 | for (i = 0; i < options.exec.nr; i++) | |
| 1481 | if (check_exec_cmd(options.exec.items[i].string)) | |
| 1482 | exit(1); | |
| 1483 | ||
| 1484 | if (!(options.flags & REBASE_NO_QUIET)) | |
| 1485 | strvec_push(&options.git_am_opts, "-q"); | |
| 1486 | ||
| 1487 | if (options.empty != EMPTY_UNSPECIFIED) | |
| 1488 | imply_merge(&options, "--empty"); | |
| 1489 | ||
| 1490 | if (options.reapply_cherry_picks < 0) | |
| 1491 | /* | |
| 1492 | * We default to --no-reapply-cherry-picks unless | |
| 1493 | * --keep-base is given; when --keep-base is given, we want | |
| 1494 | * to default to --reapply-cherry-picks. | |
| 1495 | */ | |
| 1496 | options.reapply_cherry_picks = keep_base; | |
| 1497 | else if (!keep_base) | |
| 1498 | /* | |
| 1499 | * The apply backend always searches for and drops cherry | |
| 1500 | * picks. This is often not wanted with --keep-base, so | |
| 1501 | * --keep-base allows --reapply-cherry-picks to be | |
| 1502 | * simulated by altering the upstream such that | |
| 1503 | * cherry-picks cannot be detected and thus all commits are | |
| 1504 | * reapplied. Thus, --[no-]reapply-cherry-picks is | |
| 1505 | * supported when --keep-base is specified, but not when | |
| 1506 | * --keep-base is left out. | |
| 1507 | */ | |
| 1508 | imply_merge(&options, options.reapply_cherry_picks ? | |
| 1509 | "--reapply-cherry-picks" : | |
| 1510 | "--no-reapply-cherry-picks"); | |
| 1511 | ||
| 1512 | if (gpg_sign) | |
| 1513 | options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); | |
| 1514 | ||
| 1515 | if (options.exec.nr) | |
| 1516 | imply_merge(&options, "--exec"); | |
| 1517 | ||
| 1518 | if (options.type == REBASE_APPLY) { | |
| 1519 | if (ignore_whitespace) | |
| 1520 | strvec_push(&options.git_am_opts, | |
| 1521 | "--ignore-whitespace"); | |
| 1522 | if (options.committer_date_is_author_date) | |
| 1523 | strvec_push(&options.git_am_opts, | |
| 1524 | "--committer-date-is-author-date"); | |
| 1525 | if (options.ignore_date) | |
| 1526 | strvec_push(&options.git_am_opts, "--ignore-date"); | |
| 1527 | } else { | |
| 1528 | /* REBASE_MERGE */ | |
| 1529 | if (ignore_whitespace) { | |
| 1530 | string_list_append(&options.strategy_opts, | |
| 1531 | "ignore-space-change"); | |
| 1532 | } | |
| 1533 | } | |
| 1534 | ||
| 1535 | if (strategy_opt) | |
| 1536 | options.strategy = xstrdup(strategy_opt); | |
| 1537 | else if (options.strategy_opts.nr && !options.strategy) | |
| 1538 | options.strategy = xstrdup("ort"); | |
| 1539 | if (options.strategy) | |
| 1540 | imply_merge(&options, "--strategy"); | |
| 1541 | ||
| 1542 | if (options.root && !options.onto_name) | |
| 1543 | imply_merge(&options, "--root without --onto"); | |
| 1544 | ||
| 1545 | if (isatty(2) && options.flags & REBASE_NO_QUIET) | |
| 1546 | strbuf_addstr(&options.git_format_patch_opt, " --progress"); | |
| 1547 | ||
| 1548 | if (options.git_am_opts.nr || options.type == REBASE_APPLY) { | |
| 1549 | /* all am options except -q are compatible only with --apply */ | |
| 1550 | for (i = options.git_am_opts.nr - 1; i >= 0; i--) | |
| 1551 | if (strcmp(options.git_am_opts.v[i], "-q")) | |
| 1552 | break; | |
| 1553 | ||
| 1554 | if (i >= 0 || options.type == REBASE_APPLY) { | |
| 1555 | if (is_merge(&options)) | |
| 1556 | die(_("apply options and merge options " | |
| 1557 | "cannot be used together")); | |
| 1558 | else if (options.rebase_merges == -1 && options.config_rebase_merges == 1) | |
| 1559 | die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges")); | |
| 1560 | else if (options.update_refs == -1 && options.config_update_refs == 1) | |
| 1561 | die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs")); | |
| 1562 | else | |
| 1563 | options.type = REBASE_APPLY; | |
| 1564 | } | |
| 1565 | } | |
| 1566 | ||
| 1567 | if (options.update_refs == 1) | |
| 1568 | imply_merge(&options, "--update-refs"); | |
| 1569 | options.update_refs = (options.update_refs >= 0) ? options.update_refs : | |
| 1570 | ((options.config_update_refs >= 0) ? options.config_update_refs : 0); | |
| 1571 | ||
| 1572 | if (options.rebase_merges == 1) | |
| 1573 | imply_merge(&options, "--rebase-merges"); | |
| 1574 | options.rebase_merges = (options.rebase_merges >= 0) ? options.rebase_merges : | |
| 1575 | ((options.config_rebase_merges >= 0) ? options.config_rebase_merges : 0); | |
| 1576 | ||
| 1577 | if (options.autosquash == 1) { | |
| 1578 | imply_merge(&options, "--autosquash"); | |
| 1579 | } else if (options.autosquash == -1) { | |
| 1580 | options.autosquash = | |
| 1581 | options.config_autosquash && | |
| 1582 | (options.flags & REBASE_INTERACTIVE_EXPLICIT); | |
| 1583 | } | |
| 1584 | ||
| 1585 | if (options.type == REBASE_UNSPECIFIED) { | |
| 1586 | if (!strcmp(options.default_backend, "merge")) | |
| 1587 | options.type = REBASE_MERGE; | |
| 1588 | else if (!strcmp(options.default_backend, "apply")) | |
| 1589 | options.type = REBASE_APPLY; | |
| 1590 | else | |
| 1591 | die(_("Unknown rebase backend: %s"), | |
| 1592 | options.default_backend); | |
| 1593 | } | |
| 1594 | ||
| 1595 | switch (options.type) { | |
| 1596 | case REBASE_MERGE: | |
| 1597 | options.state_dir = merge_dir(); | |
| 1598 | break; | |
| 1599 | case REBASE_APPLY: | |
| 1600 | options.state_dir = apply_dir(); | |
| 1601 | break; | |
| 1602 | default: | |
| 1603 | BUG("options.type was just set above; should be unreachable."); | |
| 1604 | } | |
| 1605 | ||
| 1606 | if (options.empty == EMPTY_UNSPECIFIED) { | |
| 1607 | if (options.flags & REBASE_INTERACTIVE_EXPLICIT) | |
| 1608 | options.empty = EMPTY_STOP; | |
| 1609 | else if (options.exec.nr > 0) | |
| 1610 | options.empty = EMPTY_KEEP; | |
| 1611 | else | |
| 1612 | options.empty = EMPTY_DROP; | |
| 1613 | } | |
| 1614 | if (reschedule_failed_exec > 0 && !is_merge(&options)) | |
| 1615 | die(_("--reschedule-failed-exec requires " | |
| 1616 | "--exec or --interactive")); | |
| 1617 | if (reschedule_failed_exec >= 0) | |
| 1618 | options.reschedule_failed_exec = reschedule_failed_exec; | |
| 1619 | ||
| 1620 | if (options.signoff) { | |
| 1621 | strvec_push(&options.git_am_opts, "--signoff"); | |
| 1622 | options.flags |= REBASE_FORCE; | |
| 1623 | } | |
| 1624 | ||
| 1625 | if (!options.root) { | |
| 1626 | if (argc < 1) { | |
| 1627 | struct branch *branch; | |
| 1628 | ||
| 1629 | branch = branch_get(NULL); | |
| 1630 | options.upstream_name = branch_get_upstream(branch, | |
| 1631 | NULL); | |
| 1632 | if (!options.upstream_name) | |
| 1633 | error_on_missing_default_upstream(); | |
| 1634 | if (options.fork_point < 0) | |
| 1635 | options.fork_point = 1; | |
| 1636 | } else { | |
| 1637 | options.upstream_name = argv[0]; | |
| 1638 | argc--; | |
| 1639 | argv++; | |
| 1640 | if (!strcmp(options.upstream_name, "-")) | |
| 1641 | options.upstream_name = "@{-1}"; | |
| 1642 | } | |
| 1643 | options.upstream = | |
| 1644 | lookup_commit_reference_by_name(options.upstream_name); | |
| 1645 | if (!options.upstream) | |
| 1646 | die(_("invalid upstream '%s'"), options.upstream_name); | |
| 1647 | options.upstream_arg = options.upstream_name; | |
| 1648 | } else { | |
| 1649 | if (!options.onto_name) { | |
| 1650 | if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, | |
| 1651 | &squash_onto, NULL, NULL) < 0) | |
| 1652 | die(_("Could not create new root commit")); | |
| 1653 | options.squash_onto = &squash_onto; | |
| 1654 | options.onto_name = squash_onto_name = | |
| 1655 | xstrdup(oid_to_hex(&squash_onto)); | |
| 1656 | } else | |
| 1657 | options.root_with_onto = 1; | |
| 1658 | ||
| 1659 | options.upstream_name = NULL; | |
| 1660 | options.upstream = NULL; | |
| 1661 | if (argc > 1) | |
| 1662 | usage_with_options(builtin_rebase_usage, | |
| 1663 | builtin_rebase_options); | |
| 1664 | options.upstream_arg = "--root"; | |
| 1665 | } | |
| 1666 | ||
| 1667 | /* | |
| 1668 | * If the branch to rebase is given, that is the branch we will rebase | |
| 1669 | * branch_name -- branch/commit being rebased, or | |
| 1670 | * HEAD (already detached) | |
| 1671 | * orig_head -- commit object name of tip of the branch before rebasing | |
| 1672 | * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) | |
| 1673 | */ | |
| 1674 | if (argc == 1) { | |
| 1675 | /* Is it "rebase other branchname" or "rebase other commit"? */ | |
| 1676 | struct object_id branch_oid; | |
| 1677 | branch_name = argv[0]; | |
| 1678 | options.switch_to = argv[0]; | |
| 1679 | ||
| 1680 | /* Is it a local branch? */ | |
| 1681 | strbuf_reset(&buf); | |
| 1682 | strbuf_addf(&buf, "refs/heads/%s", branch_name); | |
| 1683 | if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) { | |
| 1684 | die_if_checked_out(buf.buf, 1); | |
| 1685 | options.head_name = xstrdup(buf.buf); | |
| 1686 | options.orig_head = | |
| 1687 | lookup_commit_object(the_repository, | |
| 1688 | &branch_oid); | |
| 1689 | /* If not is it a valid ref (branch or commit)? */ | |
| 1690 | } else { | |
| 1691 | options.orig_head = | |
| 1692 | lookup_commit_reference_by_name(branch_name); | |
| 1693 | options.head_name = NULL; | |
| 1694 | } | |
| 1695 | if (!options.orig_head) | |
| 1696 | die(_("no such branch/commit '%s'"), branch_name); | |
| 1697 | } else if (argc == 0) { | |
| 1698 | /* Do not need to switch branches, we are already on it. */ | |
| 1699 | options.head_name = | |
| 1700 | xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, | |
| 1701 | &flags)); | |
| 1702 | if (!options.head_name) | |
| 1703 | die(_("No such ref: %s"), "HEAD"); | |
| 1704 | if (flags & REF_ISSYMREF) { | |
| 1705 | if (!skip_prefix(options.head_name, | |
| 1706 | "refs/heads/", &branch_name)) | |
| 1707 | branch_name = options.head_name; | |
| 1708 | ||
| 1709 | } else { | |
| 1710 | FREE_AND_NULL(options.head_name); | |
| 1711 | branch_name = "HEAD"; | |
| 1712 | } | |
| 1713 | options.orig_head = lookup_commit_reference_by_name("HEAD"); | |
| 1714 | if (!options.orig_head) | |
| 1715 | die(_("Could not resolve HEAD to a commit")); | |
| 1716 | } else | |
| 1717 | BUG("unexpected number of arguments left to parse"); | |
| 1718 | ||
| 1719 | /* Make sure the branch to rebase onto is valid. */ | |
| 1720 | if (keep_base) { | |
| 1721 | strbuf_reset(&buf); | |
| 1722 | strbuf_addstr(&buf, options.upstream_name); | |
| 1723 | strbuf_addstr(&buf, "..."); | |
| 1724 | strbuf_addstr(&buf, branch_name); | |
| 1725 | options.onto_name = keep_base_onto_name = xstrdup(buf.buf); | |
| 1726 | } else if (!options.onto_name) | |
| 1727 | options.onto_name = options.upstream_name; | |
| 1728 | if (strstr(options.onto_name, "...")) { | |
| 1729 | if (repo_get_oid_mb(the_repository, options.onto_name, &branch_base) < 0) { | |
| 1730 | if (keep_base) | |
| 1731 | die(_("'%s': need exactly one merge base with branch"), | |
| 1732 | options.upstream_name); | |
| 1733 | else | |
| 1734 | die(_("'%s': need exactly one merge base"), | |
| 1735 | options.onto_name); | |
| 1736 | } | |
| 1737 | options.onto = lookup_commit_or_die(&branch_base, | |
| 1738 | options.onto_name); | |
| 1739 | } else { | |
| 1740 | options.onto = | |
| 1741 | lookup_commit_reference_by_name(options.onto_name); | |
| 1742 | if (!options.onto) | |
| 1743 | die(_("Does not point to a valid commit '%s'"), | |
| 1744 | options.onto_name); | |
| 1745 | fill_branch_base(&options, &branch_base); | |
| 1746 | } | |
| 1747 | ||
| 1748 | if (keep_base && options.reapply_cherry_picks) | |
| 1749 | options.upstream = options.onto; | |
| 1750 | ||
| 1751 | if (options.fork_point > 0) | |
| 1752 | options.restrict_revision = | |
| 1753 | get_fork_point(options.upstream_name, options.orig_head); | |
| 1754 | ||
| 1755 | if (repo_read_index(the_repository) < 0) | |
| 1756 | die(_("could not read index")); | |
| 1757 | ||
| 1758 | if (options.autostash) | |
| 1759 | create_autostash(the_repository, | |
| 1760 | state_dir_path("autostash", &options)); | |
| 1761 | ||
| 1762 | ||
| 1763 | if (require_clean_work_tree(the_repository, "rebase", | |
| 1764 | _("Please commit or stash them."), 1, 1)) { | |
| 1765 | ret = -1; | |
| 1766 | goto cleanup_autostash; | |
| 1767 | } | |
| 1768 | ||
| 1769 | /* | |
| 1770 | * Now we are rebasing commits upstream..orig_head (or with --root, | |
| 1771 | * everything leading up to orig_head) on top of onto. | |
| 1772 | */ | |
| 1773 | ||
| 1774 | /* | |
| 1775 | * Check if we are already based on onto with linear history, | |
| 1776 | * in which case we could fast-forward without replacing the commits | |
| 1777 | * with new commits recreated by replaying their changes. | |
| 1778 | */ | |
| 1779 | if (allow_preemptive_ff && | |
| 1780 | can_fast_forward(options.onto, options.upstream, options.restrict_revision, | |
| 1781 | options.orig_head, &branch_base)) { | |
| 1782 | int flag; | |
| 1783 | ||
| 1784 | if (!(options.flags & REBASE_FORCE)) { | |
| 1785 | /* Lazily switch to the target branch if needed... */ | |
| 1786 | if (options.switch_to) { | |
| 1787 | ret = checkout_up_to_date(&options); | |
| 1788 | if (ret) | |
| 1789 | goto cleanup_autostash; | |
| 1790 | } | |
| 1791 | ||
| 1792 | if (!(options.flags & REBASE_NO_QUIET)) | |
| 1793 | ; /* be quiet */ | |
| 1794 | else if (!strcmp(branch_name, "HEAD") && | |
| 1795 | refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) | |
| 1796 | puts(_("HEAD is up to date.")); | |
| 1797 | else | |
| 1798 | printf(_("Current branch %s is up to date.\n"), | |
| 1799 | branch_name); | |
| 1800 | ret = finish_rebase(&options); | |
| 1801 | goto cleanup; | |
| 1802 | } else if (!(options.flags & REBASE_NO_QUIET)) | |
| 1803 | ; /* be quiet */ | |
| 1804 | else if (!strcmp(branch_name, "HEAD") && | |
| 1805 | refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) | |
| 1806 | puts(_("HEAD is up to date, rebase forced.")); | |
| 1807 | else | |
| 1808 | printf(_("Current branch %s is up to date, rebase " | |
| 1809 | "forced.\n"), branch_name); | |
| 1810 | } | |
| 1811 | ||
| 1812 | /* If a hook exists, give it a chance to interrupt*/ | |
| 1813 | if (!ok_to_skip_pre_rebase && | |
| 1814 | run_hooks_l(the_repository, "pre-rebase", options.upstream_arg, | |
| 1815 | argc ? argv[0] : NULL, NULL)) { | |
| 1816 | ret = error(_("The pre-rebase hook refused to rebase.")); | |
| 1817 | goto cleanup_autostash; | |
| 1818 | } | |
| 1819 | ||
| 1820 | if (options.flags & REBASE_DIFFSTAT) { | |
| 1821 | struct diff_options opts; | |
| 1822 | ||
| 1823 | if (options.flags & REBASE_VERBOSE) { | |
| 1824 | if (is_null_oid(&branch_base)) | |
| 1825 | printf(_("Changes to %s:\n"), | |
| 1826 | oid_to_hex(&options.onto->object.oid)); | |
| 1827 | else | |
| 1828 | printf(_("Changes from %s to %s:\n"), | |
| 1829 | oid_to_hex(&branch_base), | |
| 1830 | oid_to_hex(&options.onto->object.oid)); | |
| 1831 | } | |
| 1832 | ||
| 1833 | /* We want color (if set), but no pager */ | |
| 1834 | repo_diff_setup(the_repository, &opts); | |
| 1835 | init_diffstat_widths(&opts); | |
| 1836 | opts.output_format |= | |
| 1837 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
| 1838 | opts.detect_rename = DIFF_DETECT_RENAME; | |
| 1839 | diff_setup_done(&opts); | |
| 1840 | diff_tree_oid(is_null_oid(&branch_base) ? | |
| 1841 | the_hash_algo->empty_tree : &branch_base, | |
| 1842 | &options.onto->object.oid, "", &opts); | |
| 1843 | diffcore_std(&opts); | |
| 1844 | diff_flush(&opts); | |
| 1845 | } | |
| 1846 | ||
| 1847 | if (is_merge(&options)) | |
| 1848 | goto run_rebase; | |
| 1849 | ||
| 1850 | /* Detach HEAD and reset the tree */ | |
| 1851 | if (options.flags & REBASE_NO_QUIET) | |
| 1852 | printf(_("First, rewinding head to replay your work on top of " | |
| 1853 | "it...\n")); | |
| 1854 | ||
| 1855 | strbuf_addf(&msg, "%s (start): checkout %s", | |
| 1856 | options.reflog_action, options.onto_name); | |
| 1857 | ropts.oid = &options.onto->object.oid; | |
| 1858 | ropts.orig_head = &options.orig_head->object.oid; | |
| 1859 | ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD | | |
| 1860 | RESET_HEAD_RUN_POST_CHECKOUT_HOOK; | |
| 1861 | ropts.head_msg = msg.buf; | |
| 1862 | ropts.default_reflog_action = options.reflog_action; | |
| 1863 | if (reset_head(the_repository, &ropts)) { | |
| 1864 | ret = error(_("Could not detach HEAD")); | |
| 1865 | goto cleanup_autostash; | |
| 1866 | } | |
| 1867 | ||
| 1868 | /* | |
| 1869 | * If the onto is a proper descendant of the tip of the branch, then | |
| 1870 | * we just fast-forwarded. | |
| 1871 | */ | |
| 1872 | if (oideq(&branch_base, &options.orig_head->object.oid)) { | |
| 1873 | printf(_("Fast-forwarded %s to %s.\n"), | |
| 1874 | branch_name, options.onto_name); | |
| 1875 | move_to_original_branch(&options); | |
| 1876 | ret = finish_rebase(&options); | |
| 1877 | goto cleanup; | |
| 1878 | } | |
| 1879 | ||
| 1880 | strbuf_addf(&revisions, "%s..%s", | |
| 1881 | options.root ? oid_to_hex(&options.onto->object.oid) : | |
| 1882 | (options.restrict_revision ? | |
| 1883 | oid_to_hex(&options.restrict_revision->object.oid) : | |
| 1884 | oid_to_hex(&options.upstream->object.oid)), | |
| 1885 | oid_to_hex(&options.orig_head->object.oid)); | |
| 1886 | ||
| 1887 | options.revisions = revisions.buf; | |
| 1888 | ||
| 1889 | run_rebase: | |
| 1890 | ret = run_specific_rebase(&options); | |
| 1891 | ||
| 1892 | cleanup: | |
| 1893 | strbuf_release(&buf); | |
| 1894 | strbuf_release(&msg); | |
| 1895 | strbuf_release(&revisions); | |
| 1896 | rebase_options_release(&options); | |
| 1897 | free(squash_onto_name); | |
| 1898 | free(keep_base_onto_name); | |
| 1899 | return !!ret; | |
| 1900 | ||
| 1901 | cleanup_autostash: | |
| 1902 | ret |= !!cleanup_autostash(&options); | |
| 1903 | goto cleanup; | |
| 1904 | } |