Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/builtin/rebase.c # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / builtin / rebase.c` 1938 lines ยท 58376 bytes Raw Blame History 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 ] " 43 "[--onto | --keep-base] [ []]"), 44 N_("git rebase [-i] [options] [--exec ] [--onto ] " 45 "--root []"), 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_working_tree_options 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_WORKING_TREE_REFS_ONLY | 611 RESET_WORKING_TREE_UPDATE_HEAD; 612 ropts.branch_msg = branch_reflog.buf; 613 ropts.head_msg = head_reflog.buf; 614 ret = reset_working_tree(the_repository, &ropts); 615 616 strbuf_release(&branch_reflog); 617 strbuf_release(&head_reflog); 618 return ret; 619 } 620 621 static int run_am(struct rebase_options *opts) 622 { 623 struct child_process am = CHILD_PROCESS_INIT; 624 struct child_process format_patch = CHILD_PROCESS_INIT; 625 int status; 626 char *rebased_patches; 627 628 am.git_cmd = 1; 629 strvec_push(&am.args, "am"); 630 strvec_pushf(&am.env, GIT_REFLOG_ACTION_ENVIRONMENT "=%s (pick)", 631 opts->reflog_action); 632 if (opts->action == ACTION_CONTINUE) { 633 strvec_push(&am.args, "--resolved"); 634 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); 635 if (opts->gpg_sign_opt) 636 strvec_push(&am.args, opts->gpg_sign_opt); 637 status = run_command(&am); 638 if (status) 639 return status; 640 641 return move_to_original_branch(opts); 642 } 643 if (opts->action == ACTION_SKIP) { 644 strvec_push(&am.args, "--skip"); 645 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); 646 status = run_command(&am); 647 if (status) 648 return status; 649 650 return move_to_original_branch(opts); 651 } 652 if (opts->action == ACTION_SHOW_CURRENT_PATCH) { 653 strvec_push(&am.args, "--show-current-patch"); 654 return run_command(&am); 655 } 656 657 rebased_patches = repo_git_path(the_repository, "rebased-patches"); 658 format_patch.out = open(rebased_patches, 659 O_WRONLY | O_CREAT | O_TRUNC, 0666); 660 if (format_patch.out < 0) { 661 status = error_errno(_("could not open '%s' for writing"), 662 rebased_patches); 663 free(rebased_patches); 664 child_process_clear(&am); 665 return status; 666 } 667 668 format_patch.git_cmd = 1; 669 strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout", 670 "--full-index", "--cherry-pick", "--right-only", 671 "--default-prefix", "--no-renames", 672 "--no-cover-letter", "--pretty=mboxrd", "--topo-order", 673 "--no-base", NULL); 674 if (opts->git_format_patch_opt.len) 675 strvec_split(&format_patch.args, 676 opts->git_format_patch_opt.buf); 677 strvec_pushf(&format_patch.args, "%s...%s", 678 oid_to_hex(opts->root ? 679 /* this is now equivalent to !opts->upstream */ 680 &opts->onto->object.oid : 681 &opts->upstream->object.oid), 682 oid_to_hex(&opts->orig_head->object.oid)); 683 if (opts->restrict_revision) 684 strvec_pushf(&format_patch.args, "^%s", 685 oid_to_hex(&opts->restrict_revision->object.oid)); 686 687 status = run_command(&format_patch); 688 if (status) { 689 struct reset_working_tree_options ropts = { 0 }; 690 unlink(rebased_patches); 691 free(rebased_patches); 692 child_process_clear(&am); 693 694 ropts.oid = &opts->orig_head->object.oid; 695 ropts.branch = opts->head_name; 696 ropts.default_reflog_action = opts->reflog_action; 697 ropts.flags = RESET_WORKING_TREE_UPDATE_HEAD; 698 reset_working_tree(the_repository, &ropts); 699 error(_("\ngit encountered an error while preparing the " 700 "patches to replay\n" 701 "these revisions:\n" 702 "\n %s\n\n" 703 "As a result, git cannot rebase them."), 704 opts->revisions); 705 706 return status; 707 } 708 709 am.in = open(rebased_patches, O_RDONLY); 710 if (am.in < 0) { 711 status = error_errno(_("could not open '%s' for reading"), 712 rebased_patches); 713 free(rebased_patches); 714 child_process_clear(&am); 715 return status; 716 } 717 718 strvec_pushv(&am.args, opts->git_am_opts.v); 719 strvec_push(&am.args, "--rebasing"); 720 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg); 721 strvec_push(&am.args, "--patch-format=mboxrd"); 722 if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE) 723 strvec_push(&am.args, "--rerere-autoupdate"); 724 else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE) 725 strvec_push(&am.args, "--no-rerere-autoupdate"); 726 if (opts->gpg_sign_opt) 727 strvec_push(&am.args, opts->gpg_sign_opt); 728 status = run_command(&am); 729 unlink(rebased_patches); 730 free(rebased_patches); 731 732 if (!status) { 733 return move_to_original_branch(opts); 734 } 735 736 if (is_directory(opts->state_dir)) 737 rebase_write_basic_state(opts); 738 739 return status; 740 } 741 742 static int run_specific_rebase(struct rebase_options *opts) 743 { 744 int status; 745 746 if (opts->type == REBASE_MERGE) { 747 /* Run sequencer-based rebase */ 748 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) 749 setenv("GIT_SEQUENCE_EDITOR", ":", 1); 750 if (opts->gpg_sign_opt) { 751 /* remove the leading "-S" */ 752 char *tmp = xstrdup(opts->gpg_sign_opt + 2); 753 free(opts->gpg_sign_opt); 754 opts->gpg_sign_opt = tmp; 755 } 756 757 status = run_sequencer_rebase(opts); 758 } else if (opts->type == REBASE_APPLY) 759 status = run_am(opts); 760 else 761 BUG("Unhandled rebase type %d", opts->type); 762 763 if (opts->dont_finish_rebase) 764 ; /* do nothing */ 765 else if (opts->type == REBASE_MERGE) { 766 int quiet = !(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE)); 767 768 /* 769 * The sequencer cleans up after itself. Its state directory 770 * is gone once it is done, and stays while it is stopped. 771 */ 772 if (status == 0 && !is_directory(opts->state_dir)) 773 run_auto_maintenance(the_repository, quiet); 774 } else if (status == 0) { 775 if (!file_exists(state_dir_path("stopped-sha", opts))) 776 finish_rebase(opts); 777 } else if (status == 2) { 778 struct strbuf dir = STRBUF_INIT; 779 780 apply_autostash(state_dir_path("autostash", opts)); 781 strbuf_addstr(&dir, opts->state_dir); 782 remove_dir_recursively(&dir, 0); 783 strbuf_release(&dir); 784 die("Nothing to do"); 785 } 786 787 return status ? -1 : 0; 788 } 789 790 static void parse_rebase_merges_value(struct rebase_options *options, const char *value) 791 { 792 if (!strcmp("no-rebase-cousins", value)) 793 options->rebase_cousins = 0; 794 else if (!strcmp("rebase-cousins", value)) 795 options->rebase_cousins = 1; 796 else 797 die(_("Unknown rebase-merges mode: %s"), value); 798 } 799 800 static int rebase_config(const char *var, const char *value, 801 const struct config_context *ctx, void *data) 802 { 803 struct rebase_options *opts = data; 804 805 if (!strcmp(var, "rebase.stat")) { 806 if (git_config_bool(var, value)) 807 opts->flags |= REBASE_DIFFSTAT; 808 else 809 opts->flags &= ~REBASE_DIFFSTAT; 810 return 0; 811 } 812 813 if (!strcmp(var, "rebase.autosquash")) { 814 opts->config_autosquash = git_config_bool(var, value); 815 return 0; 816 } 817 818 if (!strcmp(var, "commit.gpgsign")) { 819 free(opts->gpg_sign_opt); 820 opts->gpg_sign_opt = git_config_bool(var, value) ? 821 xstrdup("-S") : NULL; 822 return 0; 823 } 824 825 if (!strcmp(var, "rebase.autostash")) { 826 opts->autostash = git_config_bool(var, value); 827 return 0; 828 } 829 830 if (!strcmp(var, "rebase.rebasemerges")) { 831 opts->config_rebase_merges = git_parse_maybe_bool(value); 832 if (opts->config_rebase_merges < 0) { 833 opts->config_rebase_merges = 1; 834 parse_rebase_merges_value(opts, value); 835 } else { 836 opts->rebase_cousins = 0; 837 } 838 return 0; 839 } 840 841 if (!strcmp(var, "rebase.updaterefs")) { 842 opts->config_update_refs = git_config_bool(var, value); 843 return 0; 844 } 845 846 if (!strcmp(var, "rebase.reschedulefailedexec")) { 847 opts->reschedule_failed_exec = git_config_bool(var, value); 848 return 0; 849 } 850 851 if (!strcmp(var, "rebase.forkpoint")) { 852 opts->fork_point = git_config_bool(var, value) ? -1 : 0; 853 return 0; 854 } 855 856 if (!strcmp(var, "rebase.backend")) { 857 FREE_AND_NULL(opts->default_backend); 858 return git_config_string(&opts->default_backend, var, value); 859 } 860 861 return git_default_config(var, value, ctx, data); 862 } 863 864 static int checkout_up_to_date(struct rebase_options *options) 865 { 866 struct strbuf buf = STRBUF_INIT; 867 struct reset_working_tree_options ropts = { 0 }; 868 int ret = 0; 869 870 strbuf_addf(&buf, "%s: checkout %s", 871 options->reflog_action, options->switch_to); 872 ropts.oid = &options->orig_head->object.oid; 873 ropts.branch = options->head_name; 874 ropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK | 875 RESET_WORKING_TREE_UPDATE_HEAD; 876 if (!ropts.branch) 877 ropts.flags |= RESET_WORKING_TREE_DETACH; 878 ropts.head_msg = buf.buf; 879 if (reset_working_tree(the_repository, &ropts) < 0) 880 ret = error(_("could not switch to %s"), options->switch_to); 881 strbuf_release(&buf); 882 883 return ret; 884 } 885 886 /* 887 * Determines whether the commits in from..to are linear, i.e. contain 888 * no merge commits. This function *expects* `from` to be an ancestor of 889 * `to`. 890 */ 891 static int is_linear_history(struct commit *from, struct commit *to) 892 { 893 while (to && to != from) { 894 repo_parse_commit(the_repository, to); 895 if (!to->parents) 896 return 1; 897 if (to->parents->next) 898 return 0; 899 to = to->parents->item; 900 } 901 return 1; 902 } 903 904 static int can_fast_forward(struct commit *onto, struct commit *upstream, 905 struct commit *restrict_revision, 906 struct commit *head, struct object_id *branch_base) 907 { 908 struct commit_list *merge_bases = NULL; 909 int res = 0; 910 911 if (is_null_oid(branch_base)) 912 goto done; /* fill_branch_base() found multiple merge bases */ 913 914 if (!oideq(branch_base, &onto->object.oid)) 915 goto done; 916 917 if (restrict_revision && !oideq(&restrict_revision->object.oid, branch_base)) 918 goto done; 919 920 if (!upstream) 921 goto done; 922 923 if (repo_get_merge_bases(the_repository, upstream, head, &merge_bases) < 0) 924 exit(128); 925 if (!merge_bases || merge_bases->next) 926 goto done; 927 928 if (!oideq(&onto->object.oid, &merge_bases->item->object.oid)) 929 goto done; 930 931 res = 1; 932 933 done: 934 commit_list_free(merge_bases); 935 return res && is_linear_history(onto, head); 936 } 937 938 static void fill_branch_base(struct rebase_options *options, 939 struct object_id *branch_base) 940 { 941 struct commit_list *merge_bases = NULL; 942 943 if (repo_get_merge_bases(the_repository, options->onto, 944 options->orig_head, &merge_bases) < 0) 945 exit(128); 946 if (!merge_bases || merge_bases->next) 947 oidcpy(branch_base, null_oid(the_hash_algo)); 948 else 949 oidcpy(branch_base, &merge_bases->item->object.oid); 950 951 commit_list_free(merge_bases); 952 } 953 954 static int parse_opt_am(const struct option *opt, const char *arg, int unset) 955 { 956 struct rebase_options *opts = opt->value; 957 958 BUG_ON_OPT_NEG(unset); 959 BUG_ON_OPT_ARG(arg); 960 961 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_APPLY) 962 die(_("apply options and merge options cannot be used together")); 963 964 opts->type = REBASE_APPLY; 965 966 return 0; 967 } 968 969 /* -i followed by -m is still -i */ 970 static int parse_opt_merge(const struct option *opt, const char *arg, int unset) 971 { 972 struct rebase_options *opts = opt->value; 973 974 BUG_ON_OPT_NEG(unset); 975 BUG_ON_OPT_ARG(arg); 976 977 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE) 978 die(_("apply options and merge options cannot be used together")); 979 980 opts->type = REBASE_MERGE; 981 982 return 0; 983 } 984 985 /* -i followed by -r is still explicitly interactive, but -r alone is not */ 986 static int parse_opt_interactive(const struct option *opt, const char *arg, 987 int unset) 988 { 989 struct rebase_options *opts = opt->value; 990 991 BUG_ON_OPT_NEG(unset); 992 BUG_ON_OPT_ARG(arg); 993 994 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE) 995 die(_("apply options and merge options cannot be used together")); 996 997 opts->type = REBASE_MERGE; 998 opts->flags |= REBASE_INTERACTIVE_EXPLICIT; 999 1000 return 0; 1001 } 1002 1003 static enum empty_type parse_empty_value(const char *value) 1004 { 1005 if (!strcasecmp(value, "drop")) 1006 return EMPTY_DROP; 1007 else if (!strcasecmp(value, "keep")) 1008 return EMPTY_KEEP; 1009 else if (!strcasecmp(value, "stop")) 1010 return EMPTY_STOP; 1011 else if (!strcasecmp(value, "ask")) { 1012 warning(_("--empty=ask is deprecated; use '--empty=stop' instead.")); 1013 return EMPTY_STOP; 1014 } 1015 1016 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value); 1017 } 1018 1019 static int parse_opt_keep_empty(const struct option *opt, const char *arg, 1020 int unset) 1021 { 1022 struct rebase_options *opts = opt->value; 1023 1024 BUG_ON_OPT_ARG(arg); 1025 1026 imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty"); 1027 opts->keep_empty = !unset; 1028 return 0; 1029 } 1030 1031 static int parse_opt_empty(const struct option *opt, const char *arg, int unset) 1032 { 1033 struct rebase_options *options = opt->value; 1034 enum empty_type value = parse_empty_value(arg); 1035 1036 BUG_ON_OPT_NEG(unset); 1037 1038 options->empty = value; 1039 return 0; 1040 } 1041 1042 static int parse_opt_rebase_merges(const struct option *opt, const char *arg, int unset) 1043 { 1044 struct rebase_options *options = opt->value; 1045 1046 options->rebase_merges = !unset; 1047 options->rebase_cousins = 0; 1048 1049 if (arg) { 1050 if (!*arg) { 1051 warning(_("--rebase-merges with an empty string " 1052 "argument is deprecated and will stop " 1053 "working in a future version of Git. Use " 1054 "--rebase-merges without an argument " 1055 "instead, which does the same thing.")); 1056 return 0; 1057 } 1058 parse_rebase_merges_value(options, arg); 1059 } 1060 1061 return 0; 1062 } 1063 1064 static void NORETURN error_on_missing_default_upstream(void) 1065 { 1066 struct branch *current_branch = branch_get(NULL); 1067 1068 printf(_("%s\n" 1069 "Please specify which branch you want to rebase against.\n" 1070 "See git-rebase(1) for details.\n" 1071 "\n" 1072 " git rebase ''\n" 1073 "\n"), 1074 current_branch ? _("There is no tracking information for " 1075 "the current branch.") : 1076 _("You are not currently on a branch.")); 1077 1078 if (current_branch) { 1079 const char *remote = current_branch->remote_name; 1080 1081 if (!remote) 1082 remote = _(""); 1083 1084 printf(_("If you wish to set tracking information for this " 1085 "branch you can do so with:\n" 1086 "\n" 1087 " git branch --set-upstream-to=%s/ %s\n" 1088 "\n"), 1089 remote, current_branch->name); 1090 } 1091 exit(1); 1092 } 1093 1094 static int check_exec_cmd(const char *cmd) 1095 { 1096 if (strchr(cmd, '\n')) 1097 return error(_("exec commands cannot contain newlines")); 1098 1099 /* Does the command consist purely of whitespace? */ 1100 if (!cmd[strspn(cmd, " \t\r\f\v")]) 1101 return error(_("empty exec command")); 1102 1103 return 0; 1104 } 1105 1106 int cmd_rebase(int argc, 1107 const char **argv, 1108 const char *prefix, 1109 struct repository *repo UNUSED) 1110 { 1111 struct rebase_options options = REBASE_OPTIONS_INIT; 1112 const char *branch_name; 1113 const char *strategy_opt = NULL; 1114 int ret, flags, total_argc, in_progress = 0; 1115 int keep_base = 0; 1116 int ok_to_skip_pre_rebase = 0; 1117 struct strbuf msg = STRBUF_INIT; 1118 struct strbuf revisions = STRBUF_INIT; 1119 struct strbuf buf = STRBUF_INIT; 1120 struct object_id branch_base; 1121 int ignore_whitespace = 0; 1122 const char *gpg_sign = NULL; 1123 struct object_id squash_onto; 1124 char *squash_onto_name = NULL; 1125 char *keep_base_onto_name = NULL; 1126 int reschedule_failed_exec = -1; 1127 int allow_preemptive_ff = 1; 1128 int preserve_merges_selected = 0; 1129 struct reset_working_tree_options ropts = { 0 }; 1130 struct option builtin_rebase_options[] = { 1131 OPT_STRING(0, "onto", &options.onto_name, 1132 N_("revision"), 1133 N_("rebase onto given branch instead of upstream")), 1134 OPT_BOOL(0, "keep-base", &keep_base, 1135 N_("use the merge-base of upstream and branch as the current base")), 1136 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, 1137 N_("allow pre-rebase hook to run")), 1138 OPT_NEGBIT('q', "quiet", &options.flags, 1139 N_("be quiet. implies --no-stat"), 1140 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), 1141 OPT_BIT('v', "verbose", &options.flags, 1142 N_("display a diffstat of what changed upstream"), 1143 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), 1144 { 1145 .type = OPTION_NEGBIT, 1146 .short_name = 'n', 1147 .long_name = "no-stat", 1148 .value = &options.flags, 1149 .precision = sizeof(options.flags), 1150 .help = N_("do not show diffstat of what changed upstream"), 1151 .flags = PARSE_OPT_NOARG, 1152 .defval = REBASE_DIFFSTAT, 1153 }, 1154 OPT_STRVEC(0, "trailer", &options.trailer_args, N_("trailer"), 1155 N_("add custom trailer(s)")), 1156 OPT_BOOL(0, "signoff", &options.signoff, 1157 N_("add a Signed-off-by trailer to each commit")), 1158 OPT_BOOL(0, "committer-date-is-author-date", 1159 &options.committer_date_is_author_date, 1160 N_("make committer date match author date")), 1161 OPT_BOOL(0, "reset-author-date", &options.ignore_date, 1162 N_("ignore author date and use current date")), 1163 OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date, 1164 N_("synonym of --reset-author-date")), 1165 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"), 1166 N_("passed to 'git apply'"), 0), 1167 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace, 1168 N_("ignore changes in whitespace")), 1169 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts, 1170 N_("action"), N_("passed to 'git apply'"), 0), 1171 OPT_BIT('f', "force-rebase", &options.flags, 1172 N_("cherry-pick all commits, even if unchanged"), 1173 REBASE_FORCE), 1174 OPT_BIT(0, "no-ff", &options.flags, 1175 N_("cherry-pick all commits, even if unchanged"), 1176 REBASE_FORCE), 1177 OPT_CMDMODE(0, "continue", &options.action, N_("continue"), 1178 ACTION_CONTINUE), 1179 OPT_CMDMODE(0, "skip", &options.action, 1180 N_("skip current patch and continue"), ACTION_SKIP), 1181 OPT_CMDMODE(0, "abort", &options.action, 1182 N_("abort and check out the original branch"), 1183 ACTION_ABORT), 1184 OPT_CMDMODE(0, "quit", &options.action, 1185 N_("abort but keep HEAD where it is"), ACTION_QUIT), 1186 OPT_CMDMODE(0, "edit-todo", &options.action, N_("edit the todo list " 1187 "during an interactive rebase"), ACTION_EDIT_TODO), 1188 OPT_CMDMODE(0, "show-current-patch", &options.action, 1189 N_("show the patch file being applied or merged"), 1190 ACTION_SHOW_CURRENT_PATCH), 1191 OPT_CALLBACK_F(0, "apply", &options, NULL, 1192 N_("use apply strategies to rebase"), 1193 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1194 parse_opt_am), 1195 OPT_CALLBACK_F('m', "merge", &options, NULL, 1196 N_("use merging strategies to rebase"), 1197 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1198 parse_opt_merge), 1199 OPT_CALLBACK_F('i', "interactive", &options, NULL, 1200 N_("let the user edit the list of commits to rebase"), 1201 PARSE_OPT_NOARG | PARSE_OPT_NONEG, 1202 parse_opt_interactive), 1203 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected, 1204 N_("(REMOVED) was: try to recreate merges " 1205 "instead of ignoring them"), 1206 1, PARSE_OPT_HIDDEN), 1207 OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate), 1208 OPT_CALLBACK_F(0, "empty", &options, "(drop|keep|stop)", 1209 N_("how to handle commits that become empty"), 1210 PARSE_OPT_NONEG, parse_opt_empty), 1211 OPT_CALLBACK_F('k', "keep-empty", &options, NULL, 1212 N_("keep commits which start empty"), 1213 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, 1214 parse_opt_keep_empty), 1215 OPT_BOOL(0, "autosquash", &options.autosquash, 1216 N_("move commits that begin with " 1217 "squash!/fixup! under -i")), 1218 OPT_BOOL(0, "update-refs", &options.update_refs, 1219 N_("update branches that point to commits " 1220 "that are being rebased")), 1221 { 1222 .type = OPTION_STRING, 1223 .short_name = 'S', 1224 .long_name = "gpg-sign", 1225 .value = &gpg_sign, 1226 .argh = N_("key-id"), 1227 .help = N_("GPG-sign commits"), 1228 .flags = PARSE_OPT_OPTARG, 1229 .defval = (intptr_t) "", 1230 }, 1231 OPT_AUTOSTASH(&options.autostash), 1232 OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"), 1233 N_("add exec lines after each commit of the " 1234 "editable list")), 1235 OPT_BOOL_F(0, "allow-empty-message", 1236 &options.allow_empty_message, 1237 N_("allow rebasing commits with empty messages"), 1238 PARSE_OPT_HIDDEN), 1239 OPT_CALLBACK_F('r', "rebase-merges", &options, N_("mode"), 1240 N_("try to rebase merges instead of skipping them"), 1241 PARSE_OPT_OPTARG, parse_opt_rebase_merges), 1242 OPT_BOOL(0, "fork-point", &options.fork_point, 1243 N_("use 'merge-base --fork-point' to refine upstream")), 1244 OPT_STRING('s', "strategy", &strategy_opt, 1245 N_("strategy"), N_("use the given merge strategy")), 1246 OPT_STRING_LIST('X', "strategy-option", &options.strategy_opts, 1247 N_("option"), 1248 N_("pass the argument through to the merge " 1249 "strategy")), 1250 OPT_BOOL(0, "root", &options.root, 1251 N_("rebase all reachable commits up to the root(s)")), 1252 OPT_BOOL(0, "reschedule-failed-exec", 1253 &reschedule_failed_exec, 1254 N_("automatically re-schedule any `exec` that fails")), 1255 OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks, 1256 N_("apply all changes, even those already present upstream")), 1257 OPT_END(), 1258 }; 1259 int i; 1260 1261 show_usage_with_options_if_asked(argc, argv, 1262 builtin_rebase_usage, 1263 builtin_rebase_options); 1264 1265 #ifndef WITH_BREAKING_CHANGES 1266 warn_on_auto_comment_char = true; 1267 #endif /* !WITH_BREAKING_CHANGES */ 1268 prepare_repo_settings(the_repository); 1269 the_repository->settings.command_requires_full_index = 0; 1270 1271 repo_config(the_repository, rebase_config, &options); 1272 /* options.gpg_sign_opt will be either "-S" or NULL */ 1273 gpg_sign = options.gpg_sign_opt ? "" : NULL; 1274 FREE_AND_NULL(options.gpg_sign_opt); 1275 1276 strbuf_reset(&buf); 1277 strbuf_addf(&buf, "%s/applying", apply_dir()); 1278 if(file_exists(buf.buf)) 1279 die(_("It looks like 'git am' is in progress. Cannot rebase.")); 1280 1281 if (is_directory(apply_dir())) { 1282 options.type = REBASE_APPLY; 1283 options.state_dir = apply_dir(); 1284 } else if (is_directory(merge_dir())) { 1285 strbuf_reset(&buf); 1286 strbuf_addf(&buf, "%s/rewritten", merge_dir()); 1287 if (!(options.action == ACTION_ABORT) && is_directory(buf.buf)) { 1288 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n" 1289 "Use `git rebase --abort` to terminate current rebase.\n" 1290 "Or downgrade to v2.33, or earlier, to complete the rebase.")); 1291 } else { 1292 strbuf_reset(&buf); 1293 strbuf_addf(&buf, "%s/interactive", merge_dir()); 1294 options.type = REBASE_MERGE; 1295 if (file_exists(buf.buf)) 1296 options.flags |= REBASE_INTERACTIVE_EXPLICIT; 1297 } 1298 options.state_dir = merge_dir(); 1299 } 1300 1301 if (options.type != REBASE_UNSPECIFIED) 1302 in_progress = 1; 1303 1304 total_argc = argc; 1305 argc = parse_options(argc, argv, prefix, 1306 builtin_rebase_options, 1307 builtin_rebase_usage, 0); 1308 1309 if (options.trailer_args.nr) { 1310 if (validate_trailer_args(&options.trailer_args)) 1311 die(NULL); 1312 options.flags |= REBASE_FORCE; 1313 } 1314 1315 if (preserve_merges_selected) 1316 die(_("--preserve-merges was replaced by --rebase-merges\n" 1317 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n" 1318 "which is no longer supported; use 'merges' instead")); 1319 1320 if (options.action != ACTION_NONE && total_argc != 2) { 1321 usage_with_options(builtin_rebase_usage, 1322 builtin_rebase_options); 1323 } 1324 1325 if (argc > 2) 1326 usage_with_options(builtin_rebase_usage, 1327 builtin_rebase_options); 1328 1329 if (keep_base) { 1330 if (options.onto_name) 1331 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto"); 1332 if (options.root) 1333 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root"); 1334 /* 1335 * --keep-base defaults to --no-fork-point to keep the 1336 * base the same. 1337 */ 1338 if (options.fork_point < 0) 1339 options.fork_point = 0; 1340 } 1341 if (options.root && options.fork_point > 0) 1342 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point"); 1343 1344 if (options.action != ACTION_NONE && !in_progress) 1345 die(_("no rebase in progress")); 1346 1347 if (options.action == ACTION_EDIT_TODO && !is_merge(&options)) 1348 die(_("The --edit-todo action can only be used during " 1349 "interactive rebase.")); 1350 1351 if (trace2_is_enabled()) { 1352 if (is_merge(&options)) 1353 trace2_cmd_mode("interactive"); 1354 else if (options.exec.nr) 1355 trace2_cmd_mode("interactive-exec"); 1356 else 1357 trace2_cmd_mode(action_names[options.action]); 1358 } 1359 1360 options.reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); 1361 options.reflog_action = 1362 xstrdup(options.reflog_action ? options.reflog_action : "rebase"); 1363 1364 switch (options.action) { 1365 case ACTION_CONTINUE: { 1366 struct object_id head; 1367 struct lock_file lock_file = LOCK_INIT; 1368 int fd; 1369 1370 /* Sanity check */ 1371 if (repo_get_oid(the_repository, "HEAD", &head)) 1372 die(_("Cannot read HEAD")); 1373 1374 fd = repo_hold_locked_index(the_repository, &lock_file, 0); 1375 if (repo_read_index(the_repository) < 0) 1376 die(_("could not read index")); 1377 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, 1378 NULL); 1379 if (0 <= fd) 1380 repo_update_index_if_able(the_repository, &lock_file); 1381 rollback_lock_file(&lock_file); 1382 1383 if (has_unstaged_changes(the_repository, 1)) { 1384 puts(_("You must edit all merge conflicts and then\n" 1385 "mark them as resolved using git add")); 1386 exit(1); 1387 } 1388 if (read_basic_state(&options)) 1389 exit(1); 1390 goto run_rebase; 1391 } 1392 case ACTION_SKIP: { 1393 struct string_list merge_rr = STRING_LIST_INIT_DUP; 1394 1395 rerere_clear(the_repository, &merge_rr); 1396 string_list_clear(&merge_rr, 1); 1397 ropts.flags = RESET_WORKING_TREE_HARD | 1398 RESET_WORKING_TREE_UPDATE_HEAD; 1399 if (reset_working_tree(the_repository, &ropts) < 0) 1400 die(_("could not discard worktree changes")); 1401 remove_branch_state(the_repository, 0); 1402 if (read_basic_state(&options)) 1403 exit(1); 1404 goto run_rebase; 1405 } 1406 case ACTION_ABORT: { 1407 struct string_list merge_rr = STRING_LIST_INIT_DUP; 1408 struct strbuf head_msg = STRBUF_INIT; 1409 1410 rerere_clear(the_repository, &merge_rr); 1411 string_list_clear(&merge_rr, 1); 1412 1413 if (read_basic_state(&options)) 1414 exit(1); 1415 1416 strbuf_addf(&head_msg, "%s (abort): returning to %s", 1417 options.reflog_action, 1418 options.head_name ? options.head_name 1419 : oid_to_hex(&options.orig_head->object.oid)); 1420 ropts.oid = &options.orig_head->object.oid; 1421 ropts.head_msg = head_msg.buf; 1422 ropts.branch = options.head_name; 1423 ropts.flags = RESET_WORKING_TREE_HARD | 1424 RESET_WORKING_TREE_UPDATE_HEAD; 1425 if (reset_working_tree(the_repository, &ropts) < 0) 1426 die(_("could not move back to %s"), 1427 oid_to_hex(&options.orig_head->object.oid)); 1428 strbuf_release(&head_msg); 1429 remove_branch_state(the_repository, 0); 1430 ret = finish_rebase(&options); 1431 goto cleanup; 1432 } 1433 case ACTION_QUIT: { 1434 save_autostash(state_dir_path("autostash", &options)); 1435 if (options.type == REBASE_MERGE) { 1436 struct replay_opts replay = REPLAY_OPTS_INIT; 1437 1438 replay.action = REPLAY_INTERACTIVE_REBASE; 1439 ret = sequencer_remove_state(&replay); 1440 replay_opts_release(&replay); 1441 } else { 1442 strbuf_reset(&buf); 1443 strbuf_addstr(&buf, options.state_dir); 1444 ret = remove_dir_recursively(&buf, 0); 1445 if (ret) 1446 error(_("could not remove '%s'"), 1447 options.state_dir); 1448 } 1449 goto cleanup; 1450 } 1451 case ACTION_EDIT_TODO: 1452 options.dont_finish_rebase = 1; 1453 goto run_rebase; 1454 case ACTION_SHOW_CURRENT_PATCH: 1455 options.dont_finish_rebase = 1; 1456 goto run_rebase; 1457 case ACTION_NONE: 1458 break; 1459 default: 1460 BUG("action: %d", options.action); 1461 } 1462 1463 /* Make sure no rebase is in progress */ 1464 if (in_progress) { 1465 const char *last_slash = strrchr(options.state_dir, '/'); 1466 const char *state_dir_base = 1467 last_slash ? last_slash + 1 : options.state_dir; 1468 const char *cmd_live_rebase = 1469 "git rebase (--continue | --abort | --skip)"; 1470 strbuf_reset(&buf); 1471 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); 1472 die(_("It seems that there is already a %s directory, and\n" 1473 "I wonder if you are in the middle of another rebase. " 1474 "If that is the\n" 1475 "case, please try\n\t%s\n" 1476 "If that is not the case, please\n\t%s\n" 1477 "and run me again. I am stopping in case you still " 1478 "have something\n" 1479 "valuable there.\n"), 1480 state_dir_base, cmd_live_rebase, buf.buf); 1481 } 1482 1483 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) || 1484 (options.action != ACTION_NONE) || 1485 (options.exec.nr > 0) || 1486 options.autosquash == 1) { 1487 allow_preemptive_ff = 0; 1488 } 1489 if (options.committer_date_is_author_date || options.ignore_date) 1490 options.flags |= REBASE_FORCE; 1491 1492 for (i = 0; i < options.git_am_opts.nr; i++) { 1493 const char *option = options.git_am_opts.v[i], *p; 1494 if (!strcmp(option, "--whitespace=fix") || 1495 !strcmp(option, "--whitespace=strip")) 1496 allow_preemptive_ff = 0; 1497 else if (skip_prefix(option, "-C", &p)) { 1498 while (*p) 1499 if (!isdigit(*(p++))) 1500 die(_("switch `C' expects a " 1501 "numerical value")); 1502 } else if (skip_prefix(option, "--whitespace=", &p)) { 1503 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") && 1504 strcmp(p, "error") && strcmp(p, "error-all")) 1505 die("Invalid whitespace option: '%s'", p); 1506 } 1507 } 1508 1509 for (i = 0; i < options.exec.nr; i++) 1510 if (check_exec_cmd(options.exec.items[i].string)) 1511 exit(1); 1512 1513 if (!(options.flags & REBASE_NO_QUIET)) 1514 strvec_push(&options.git_am_opts, "-q"); 1515 1516 if (options.empty != EMPTY_UNSPECIFIED) 1517 imply_merge(&options, "--empty"); 1518 1519 if (options.reapply_cherry_picks < 0) 1520 /* 1521 * We default to --no-reapply-cherry-picks unless 1522 * --keep-base is given; when --keep-base is given, we want 1523 * to default to --reapply-cherry-picks. 1524 */ 1525 options.reapply_cherry_picks = keep_base; 1526 else if (!keep_base) 1527 /* 1528 * The apply backend always searches for and drops cherry 1529 * picks. This is often not wanted with --keep-base, so 1530 * --keep-base allows --reapply-cherry-picks to be 1531 * simulated by altering the upstream such that 1532 * cherry-picks cannot be detected and thus all commits are 1533 * reapplied. Thus, --[no-]reapply-cherry-picks is 1534 * supported when --keep-base is specified, but not when 1535 * --keep-base is left out. 1536 */ 1537 imply_merge(&options, options.reapply_cherry_picks ? 1538 "--reapply-cherry-picks" : 1539 "--no-reapply-cherry-picks"); 1540 1541 if (gpg_sign) 1542 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); 1543 1544 if (options.exec.nr) 1545 imply_merge(&options, "--exec"); 1546 1547 if (options.type == REBASE_APPLY) { 1548 if (ignore_whitespace) 1549 strvec_push(&options.git_am_opts, 1550 "--ignore-whitespace"); 1551 if (options.committer_date_is_author_date) 1552 strvec_push(&options.git_am_opts, 1553 "--committer-date-is-author-date"); 1554 if (options.ignore_date) 1555 strvec_push(&options.git_am_opts, "--ignore-date"); 1556 } else { 1557 /* REBASE_MERGE */ 1558 if (ignore_whitespace) { 1559 string_list_append(&options.strategy_opts, 1560 "ignore-space-change"); 1561 } 1562 } 1563 1564 if (strategy_opt) 1565 options.strategy = xstrdup(strategy_opt); 1566 else if (options.strategy_opts.nr && !options.strategy) 1567 options.strategy = xstrdup("ort"); 1568 if (options.strategy) 1569 imply_merge(&options, "--strategy"); 1570 1571 if (options.root && !options.onto_name) 1572 imply_merge(&options, "--root without --onto"); 1573 1574 if (options.trailer_args.nr) 1575 imply_merge(&options, "--trailer"); 1576 1577 if (isatty(2) && options.flags & REBASE_NO_QUIET) 1578 strbuf_addstr(&options.git_format_patch_opt, " --progress"); 1579 1580 if (options.git_am_opts.nr || options.type == REBASE_APPLY) { 1581 /* all am options except -q are compatible only with --apply */ 1582 for (i = options.git_am_opts.nr - 1; i >= 0; i--) 1583 if (strcmp(options.git_am_opts.v[i], "-q")) 1584 break; 1585 1586 if (i >= 0 || options.type == REBASE_APPLY) { 1587 if (is_merge(&options)) 1588 die(_("apply options and merge options " 1589 "cannot be used together")); 1590 else if (options.rebase_merges == -1 && options.config_rebase_merges == 1) 1591 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges")); 1592 else if (options.update_refs == -1 && options.config_update_refs == 1) 1593 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs")); 1594 else 1595 options.type = REBASE_APPLY; 1596 } 1597 } 1598 1599 if (options.update_refs == 1) 1600 imply_merge(&options, "--update-refs"); 1601 options.update_refs = (options.update_refs >= 0) ? options.update_refs : 1602 ((options.config_update_refs >= 0) ? options.config_update_refs : 0); 1603 1604 if (options.rebase_merges == 1) 1605 imply_merge(&options, "--rebase-merges"); 1606 options.rebase_merges = (options.rebase_merges >= 0) ? options.rebase_merges : 1607 ((options.config_rebase_merges >= 0) ? options.config_rebase_merges : 0); 1608 1609 if (options.autosquash == 1) { 1610 imply_merge(&options, "--autosquash"); 1611 } else if (options.autosquash == -1) { 1612 options.autosquash = 1613 options.config_autosquash && 1614 (options.flags & REBASE_INTERACTIVE_EXPLICIT); 1615 } 1616 1617 if (options.type == REBASE_UNSPECIFIED) { 1618 if (!strcmp(options.default_backend, "merge")) 1619 options.type = REBASE_MERGE; 1620 else if (!strcmp(options.default_backend, "apply")) 1621 options.type = REBASE_APPLY; 1622 else 1623 die(_("Unknown rebase backend: %s"), 1624 options.default_backend); 1625 } 1626 1627 switch (options.type) { 1628 case REBASE_MERGE: 1629 options.state_dir = merge_dir(); 1630 break; 1631 case REBASE_APPLY: 1632 options.state_dir = apply_dir(); 1633 break; 1634 default: 1635 BUG("options.type was just set above; should be unreachable."); 1636 } 1637 1638 if (options.empty == EMPTY_UNSPECIFIED) { 1639 if (options.flags & REBASE_INTERACTIVE_EXPLICIT) 1640 options.empty = EMPTY_STOP; 1641 else if (options.exec.nr > 0) 1642 options.empty = EMPTY_KEEP; 1643 else 1644 options.empty = EMPTY_DROP; 1645 } 1646 if (reschedule_failed_exec > 0 && !is_merge(&options)) 1647 die(_("--reschedule-failed-exec requires " 1648 "--exec or --interactive")); 1649 if (reschedule_failed_exec >= 0) 1650 options.reschedule_failed_exec = reschedule_failed_exec; 1651 1652 if (options.signoff) { 1653 strvec_push(&options.git_am_opts, "--signoff"); 1654 options.flags |= REBASE_FORCE; 1655 } 1656 1657 if (!options.root) { 1658 if (argc < 1) { 1659 struct branch *branch; 1660 1661 branch = branch_get(NULL); 1662 options.upstream_name = branch_get_upstream(branch, 1663 NULL); 1664 if (!options.upstream_name) 1665 error_on_missing_default_upstream(); 1666 if (options.fork_point < 0) 1667 options.fork_point = 1; 1668 } else { 1669 options.upstream_name = argv[0]; 1670 argc--; 1671 argv++; 1672 if (!strcmp(options.upstream_name, "-")) 1673 options.upstream_name = "@{-1}"; 1674 } 1675 options.upstream = 1676 lookup_commit_reference_by_name(options.upstream_name); 1677 if (!options.upstream) 1678 die(_("invalid upstream '%s'"), options.upstream_name); 1679 options.upstream_arg = options.upstream_name; 1680 } else { 1681 if (!options.onto_name) { 1682 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, 1683 &squash_onto, NULL, NULL) < 0) 1684 die(_("Could not create new root commit")); 1685 options.squash_onto = &squash_onto; 1686 options.onto_name = squash_onto_name = 1687 xstrdup(oid_to_hex(&squash_onto)); 1688 } else 1689 options.root_with_onto = 1; 1690 1691 options.upstream_name = NULL; 1692 options.upstream = NULL; 1693 if (argc > 1) 1694 usage_with_options(builtin_rebase_usage, 1695 builtin_rebase_options); 1696 options.upstream_arg = "--root"; 1697 } 1698 1699 /* 1700 * If the branch to rebase is given, that is the branch we will rebase 1701 * branch_name -- branch/commit being rebased, or 1702 * HEAD (already detached) 1703 * orig_head -- commit object name of tip of the branch before rebasing 1704 * head_name -- refs/heads/ or NULL (detached HEAD) 1705 */ 1706 if (argc == 1) { 1707 /* Is it "rebase other branchname" or "rebase other commit"? */ 1708 struct object_id branch_oid; 1709 branch_name = argv[0]; 1710 options.switch_to = argv[0]; 1711 1712 /* Is it a local branch? */ 1713 strbuf_reset(&buf); 1714 strbuf_addf(&buf, "refs/heads/%s", branch_name); 1715 if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) { 1716 die_if_checked_out(buf.buf, 1); 1717 options.head_name = xstrdup(buf.buf); 1718 options.orig_head = 1719 lookup_commit_object(the_repository, 1720 &branch_oid); 1721 /* If not is it a valid ref (branch or commit)? */ 1722 } else { 1723 options.orig_head = 1724 lookup_commit_reference_by_name(branch_name); 1725 options.head_name = NULL; 1726 } 1727 if (!options.orig_head) 1728 die(_("no such branch/commit '%s'"), branch_name); 1729 } else if (argc == 0) { 1730 /* Do not need to switch branches, we are already on it. */ 1731 options.head_name = 1732 xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, 1733 &flags)); 1734 if (!options.head_name) 1735 die(_("No such ref: %s"), "HEAD"); 1736 if (flags & REF_ISSYMREF) { 1737 if (!skip_prefix(options.head_name, 1738 "refs/heads/", &branch_name)) 1739 branch_name = options.head_name; 1740 1741 } else { 1742 FREE_AND_NULL(options.head_name); 1743 branch_name = "HEAD"; 1744 } 1745 options.orig_head = lookup_commit_reference_by_name("HEAD"); 1746 if (!options.orig_head) 1747 die(_("Could not resolve HEAD to a commit")); 1748 } else 1749 BUG("unexpected number of arguments left to parse"); 1750 1751 /* Make sure the branch to rebase onto is valid. */ 1752 if (keep_base) { 1753 strbuf_reset(&buf); 1754 strbuf_addstr(&buf, options.upstream_name); 1755 strbuf_addstr(&buf, "..."); 1756 strbuf_addstr(&buf, branch_name); 1757 options.onto_name = keep_base_onto_name = xstrdup(buf.buf); 1758 } else if (!options.onto_name) 1759 options.onto_name = options.upstream_name; 1760 if (strstr(options.onto_name, "...")) { 1761 if (repo_get_oid_mb(the_repository, options.onto_name, &branch_base) < 0) { 1762 if (keep_base) 1763 die(_("'%s': need exactly one merge base with branch"), 1764 options.upstream_name); 1765 else 1766 die(_("'%s': need exactly one merge base"), 1767 options.onto_name); 1768 } 1769 options.onto = lookup_commit_or_die(&branch_base, 1770 options.onto_name); 1771 } else { 1772 options.onto = 1773 lookup_commit_reference_by_name(options.onto_name); 1774 if (!options.onto) 1775 die(_("Does not point to a valid commit '%s'"), 1776 options.onto_name); 1777 fill_branch_base(&options, &branch_base); 1778 } 1779 1780 if (keep_base && options.reapply_cherry_picks) 1781 options.upstream = options.onto; 1782 1783 if (options.fork_point > 0) 1784 options.restrict_revision = 1785 get_fork_point(options.upstream_name, options.orig_head); 1786 1787 if (repo_read_index(the_repository) < 0) 1788 die(_("could not read index")); 1789 1790 if (options.autostash) 1791 create_autostash(the_repository, 1792 state_dir_path("autostash", &options)); 1793 1794 1795 if (require_clean_work_tree(the_repository, "rebase", 1796 _("Please commit or stash them."), 1, 1)) { 1797 ret = -1; 1798 goto cleanup_autostash; 1799 } 1800 1801 /* 1802 * Now we are rebasing commits upstream..orig_head (or with --root, 1803 * everything leading up to orig_head) on top of onto. 1804 */ 1805 1806 /* 1807 * Check if we are already based on onto with linear history, 1808 * in which case we could fast-forward without replacing the commits 1809 * with new commits recreated by replaying their changes. 1810 */ 1811 if (allow_preemptive_ff && 1812 can_fast_forward(options.onto, options.upstream, options.restrict_revision, 1813 options.orig_head, &branch_base)) { 1814 int flag; 1815 1816 if (!(options.flags & REBASE_FORCE)) { 1817 /* Lazily switch to the target branch if needed... */ 1818 if (options.switch_to) { 1819 ret = checkout_up_to_date(&options); 1820 if (ret) 1821 goto cleanup_autostash; 1822 } 1823 1824 if (!(options.flags & REBASE_NO_QUIET)) 1825 ; /* be quiet */ 1826 else if (!strcmp(branch_name, "HEAD") && 1827 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) 1828 puts(_("HEAD is up to date.")); 1829 else 1830 printf(_("Current branch %s is up to date.\n"), 1831 branch_name); 1832 ret = finish_rebase(&options); 1833 goto cleanup; 1834 } else if (!(options.flags & REBASE_NO_QUIET)) 1835 ; /* be quiet */ 1836 else if (!strcmp(branch_name, "HEAD") && 1837 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag)) 1838 puts(_("HEAD is up to date, rebase forced.")); 1839 else 1840 printf(_("Current branch %s is up to date, rebase " 1841 "forced.\n"), branch_name); 1842 } 1843 1844 /* If a hook exists, give it a chance to interrupt*/ 1845 if (!ok_to_skip_pre_rebase && 1846 run_hooks_l(the_repository, "pre-rebase", options.upstream_arg, 1847 argc ? argv[0] : NULL, NULL)) { 1848 ret = error(_("The pre-rebase hook refused to rebase.")); 1849 goto cleanup_autostash; 1850 } 1851 1852 if (options.flags & REBASE_DIFFSTAT) { 1853 struct diff_options opts; 1854 1855 if (options.flags & REBASE_VERBOSE) { 1856 if (is_null_oid(&branch_base)) 1857 printf(_("Changes to %s:\n"), 1858 oid_to_hex(&options.onto->object.oid)); 1859 else 1860 printf(_("Changes from %s to %s:\n"), 1861 oid_to_hex(&branch_base), 1862 oid_to_hex(&options.onto->object.oid)); 1863 } 1864 1865 /* We want color (if set), but no pager */ 1866 repo_diff_setup(the_repository, &opts); 1867 init_diffstat_widths(&opts); 1868 opts.output_format |= 1869 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; 1870 opts.detect_rename = DIFF_DETECT_RENAME; 1871 diff_setup_done(&opts); 1872 diff_tree_oid(is_null_oid(&branch_base) ? 1873 the_hash_algo->empty_tree : &branch_base, 1874 &options.onto->object.oid, "", &opts); 1875 diffcore_std(&opts); 1876 diff_flush(&opts); 1877 } 1878 1879 if (is_merge(&options)) 1880 goto run_rebase; 1881 1882 /* Detach HEAD and reset the tree */ 1883 if (options.flags & REBASE_NO_QUIET) 1884 printf(_("First, rewinding head to replay your work on top of " 1885 "it...\n")); 1886 1887 strbuf_addf(&msg, "%s (start): checkout %s", 1888 options.reflog_action, options.onto_name); 1889 ropts.oid = &options.onto->object.oid; 1890 ropts.orig_head = &options.orig_head->object.oid; 1891 ropts.flags = RESET_WORKING_TREE_DETACH | 1892 RESET_WORKING_TREE_UPDATE_HEAD | 1893 RESET_WORKING_TREE_UPDATE_ORIG_HEAD | 1894 RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK; 1895 ropts.head_msg = msg.buf; 1896 ropts.default_reflog_action = options.reflog_action; 1897 if (reset_working_tree(the_repository, &ropts)) { 1898 ret = error(_("Could not detach HEAD")); 1899 goto cleanup_autostash; 1900 } 1901 1902 /* 1903 * If the onto is a proper descendant of the tip of the branch, then 1904 * we just fast-forwarded. 1905 */ 1906 if (oideq(&branch_base, &options.orig_head->object.oid)) { 1907 printf(_("Fast-forwarded %s to %s.\n"), 1908 branch_name, options.onto_name); 1909 move_to_original_branch(&options); 1910 ret = finish_rebase(&options); 1911 goto cleanup; 1912 } 1913 1914 strbuf_addf(&revisions, "%s..%s", 1915 options.root ? oid_to_hex(&options.onto->object.oid) : 1916 (options.restrict_revision ? 1917 oid_to_hex(&options.restrict_revision->object.oid) : 1918 oid_to_hex(&options.upstream->object.oid)), 1919 oid_to_hex(&options.orig_head->object.oid)); 1920 1921 options.revisions = revisions.buf; 1922 1923 run_rebase: 1924 ret = run_specific_rebase(&options); 1925 1926 cleanup: 1927 strbuf_release(&buf); 1928 strbuf_release(&msg); 1929 strbuf_release(&revisions); 1930 rebase_options_release(&options); 1931 free(squash_onto_name); 1932 free(keep_base_onto_name); 1933 return !!ret; 1934 1935 cleanup_autostash: 1936 ret |= !!cleanup_autostash(&options); 1937 goto cleanup; 1938 }