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