2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
9 #include "run-command.h"
11 #include "argv-array.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
20 #include "parse-options.h"
23 #include "wt-status.h"
25 #include "commit-reach.h"
28 #include "sequencer.h"
29 #include "rebase-interactive.h"
31 static char const * const builtin_rebase_usage[] = {
32 N_("git rebase [-i] [options] [--exec <cmd>] "
33 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
34 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
36 N_("git rebase --continue | --abort | --skip | --edit-todo"),
40 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
41 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
42 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
43 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
46 REBASE_UNSPECIFIED = -1,
50 REBASE_PRESERVE_MERGES
54 EMPTY_UNSPECIFIED = -1,
60 struct rebase_options {
61 enum rebase_type type;
62 enum empty_type empty;
63 const char *state_dir;
64 struct commit *upstream;
65 const char *upstream_name;
66 const char *upstream_arg;
68 struct object_id orig_head;
70 const char *onto_name;
71 const char *revisions;
72 const char *switch_to;
73 int root, root_with_onto;
74 struct object_id *squash_onto;
75 struct commit *restrict_revision;
76 int dont_finish_rebase;
78 REBASE_NO_QUIET = 1<<0,
79 REBASE_VERBOSE = 1<<1,
80 REBASE_DIFFSTAT = 1<<2,
82 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
84 struct argv_array git_am_opts;
87 int allow_rerere_autoupdate;
92 int allow_empty_message;
93 int rebase_merges, rebase_cousins;
94 char *strategy, *strategy_opts;
95 struct strbuf git_format_patch_opt;
96 int reschedule_failed_exec;
97 int use_legacy_rebase;
100 #define REBASE_OPTIONS_INIT { \
101 .type = REBASE_UNSPECIFIED, \
102 .empty = EMPTY_UNSPECIFIED, \
103 .flags = REBASE_NO_QUIET, \
104 .git_am_opts = ARGV_ARRAY_INIT, \
105 .git_format_patch_opt = STRBUF_INIT \
108 static struct replay_opts get_replay_opts(const struct rebase_options *opts)
110 struct replay_opts replay = REPLAY_OPTS_INIT;
112 replay.action = REPLAY_INTERACTIVE_REBASE;
113 sequencer_init_config(&replay);
115 replay.signoff = opts->signoff;
116 replay.allow_ff = !(opts->flags & REBASE_FORCE);
117 if (opts->allow_rerere_autoupdate)
118 replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
119 replay.allow_empty = 1;
120 replay.allow_empty_message = opts->allow_empty_message;
121 replay.drop_redundant_commits = (opts->empty == EMPTY_DROP);
122 replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP);
123 replay.quiet = !(opts->flags & REBASE_NO_QUIET);
124 replay.verbose = opts->flags & REBASE_VERBOSE;
125 replay.reschedule_failed_exec = opts->reschedule_failed_exec;
126 replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
127 replay.strategy = opts->strategy;
128 if (opts->strategy_opts)
129 parse_strategy_opts(&replay, opts->strategy_opts);
131 if (opts->squash_onto) {
132 oidcpy(&replay.squash_onto, opts->squash_onto);
133 replay.have_squash_onto = 1;
146 ACTION_SHOW_CURRENT_PATCH,
149 ACTION_CHECK_TODO_LIST,
150 ACTION_REARRANGE_SQUASH,
154 static const char *action_names[] = { "undefined",
160 "show_current_patch" };
162 static int add_exec_commands(struct string_list *commands)
164 const char *todo_file = rebase_path_todo();
165 struct todo_list todo_list = TODO_LIST_INIT;
168 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
169 return error_errno(_("could not read '%s'."), todo_file);
171 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
173 todo_list_release(&todo_list);
174 return error(_("unusable todo list: '%s'"), todo_file);
177 todo_list_add_exec_commands(&todo_list, commands);
178 res = todo_list_write_to_file(the_repository, &todo_list,
179 todo_file, NULL, NULL, -1, 0);
180 todo_list_release(&todo_list);
183 return error_errno(_("could not write '%s'."), todo_file);
187 static int rearrange_squash_in_todo_file(void)
189 const char *todo_file = rebase_path_todo();
190 struct todo_list todo_list = TODO_LIST_INIT;
193 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
194 return error_errno(_("could not read '%s'."), todo_file);
195 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
197 todo_list_release(&todo_list);
198 return error(_("unusable todo list: '%s'"), todo_file);
201 res = todo_list_rearrange_squash(&todo_list);
203 res = todo_list_write_to_file(the_repository, &todo_list,
204 todo_file, NULL, NULL, -1, 0);
206 todo_list_release(&todo_list);
209 return error_errno(_("could not write '%s'."), todo_file);
213 static int transform_todo_file(unsigned flags)
215 const char *todo_file = rebase_path_todo();
216 struct todo_list todo_list = TODO_LIST_INIT;
219 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
220 return error_errno(_("could not read '%s'."), todo_file);
222 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
224 todo_list_release(&todo_list);
225 return error(_("unusable todo list: '%s'"), todo_file);
228 res = todo_list_write_to_file(the_repository, &todo_list, todo_file,
229 NULL, NULL, -1, flags);
230 todo_list_release(&todo_list);
233 return error_errno(_("could not write '%s'."), todo_file);
237 static int edit_todo_file(unsigned flags)
239 const char *todo_file = rebase_path_todo();
240 struct todo_list todo_list = TODO_LIST_INIT,
241 new_todo = TODO_LIST_INIT;
244 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
245 return error_errno(_("could not read '%s'."), todo_file);
247 strbuf_stripspace(&todo_list.buf, 1);
248 res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
249 if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
250 NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
251 res = error_errno(_("could not write '%s'"), todo_file);
253 todo_list_release(&todo_list);
254 todo_list_release(&new_todo);
259 static int get_revision_ranges(struct commit *upstream, struct commit *onto,
260 const char **head_hash,
261 char **revisions, char **shortrevisions)
263 struct commit *base_rev = upstream ? upstream : onto;
264 const char *shorthead;
265 struct object_id orig_head;
267 if (get_oid("HEAD", &orig_head))
268 return error(_("no HEAD?"));
270 *head_hash = find_unique_abbrev(&orig_head, GIT_MAX_HEXSZ);
271 *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
274 shorthead = find_unique_abbrev(&orig_head, DEFAULT_ABBREV);
277 const char *shortrev;
279 shortrev = find_unique_abbrev(&base_rev->object.oid,
282 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
284 *shortrevisions = xstrdup(shorthead);
289 static int init_basic_state(struct replay_opts *opts, const char *head_name,
290 struct commit *onto, const char *orig_head)
294 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
295 return error_errno(_("could not create temporary %s"), merge_dir());
297 delete_reflog("REBASE_HEAD");
299 interactive = fopen(path_interactive(), "w");
301 return error_errno(_("could not mark as interactive"));
304 return write_basic_state(opts, head_name, onto, orig_head);
307 static void split_exec_commands(const char *cmd, struct string_list *commands)
310 string_list_split(commands, cmd, '\n', -1);
312 /* rebase.c adds a new line to cmd after every command,
313 * so here the last command is always empty */
314 string_list_remove_empty_items(commands, 0);
318 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
321 const char *head_hash = NULL;
322 char *revisions = NULL, *shortrevisions = NULL;
323 struct argv_array make_script_args = ARGV_ARRAY_INIT;
324 struct todo_list todo_list = TODO_LIST_INIT;
325 struct replay_opts replay = get_replay_opts(opts);
326 struct string_list commands = STRING_LIST_INIT_DUP;
328 if (prepare_branch_to_be_rebased(the_repository, &replay,
332 if (get_revision_ranges(opts->upstream, opts->onto, &head_hash,
333 &revisions, &shortrevisions))
336 if (init_basic_state(&replay,
337 opts->head_name ? opts->head_name : "detached HEAD",
338 opts->onto, head_hash)) {
340 free(shortrevisions);
345 if (!opts->upstream && opts->squash_onto)
346 write_file(path_squash_onto(), "%s\n",
347 oid_to_hex(opts->squash_onto));
349 argv_array_pushl(&make_script_args, "", revisions, NULL);
350 if (opts->restrict_revision)
351 argv_array_pushf(&make_script_args, "^%s",
352 oid_to_hex(&opts->restrict_revision->object.oid));
354 ret = sequencer_make_script(the_repository, &todo_list.buf,
355 make_script_args.argc, make_script_args.argv,
359 error(_("could not generate todo list"));
362 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
364 BUG("unusable todo list");
366 split_exec_commands(opts->cmd, &commands);
367 ret = complete_action(the_repository, &replay, flags,
368 shortrevisions, opts->onto_name, opts->onto, head_hash,
369 &commands, opts->autosquash, &todo_list);
372 string_list_clear(&commands, 0);
374 free(shortrevisions);
375 todo_list_release(&todo_list);
376 argv_array_clear(&make_script_args);
381 static int run_rebase_interactive(struct rebase_options *opts,
385 int abbreviate_commands = 0, ret = 0;
387 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
389 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
390 flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
391 flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
392 flags |= opts->root_with_onto ? TODO_LIST_ROOT_WITH_ONTO : 0;
393 flags |= command == ACTION_SHORTEN_OIDS ? TODO_LIST_SHORTEN_IDS : 0;
397 if (!opts->onto && !opts->upstream)
398 die(_("a base commit must be provided with --upstream or --onto"));
400 ret = do_interactive_rebase(opts, flags);
404 struct string_list merge_rr = STRING_LIST_INIT_DUP;
406 rerere_clear(the_repository, &merge_rr);
409 case ACTION_CONTINUE: {
410 struct replay_opts replay_opts = get_replay_opts(opts);
412 ret = sequencer_continue(the_repository, &replay_opts);
415 case ACTION_EDIT_TODO:
416 ret = edit_todo_file(flags);
418 case ACTION_SHOW_CURRENT_PATCH: {
419 struct child_process cmd = CHILD_PROCESS_INIT;
422 argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
423 ret = run_command(&cmd);
427 case ACTION_SHORTEN_OIDS:
428 case ACTION_EXPAND_OIDS:
429 ret = transform_todo_file(flags);
431 case ACTION_CHECK_TODO_LIST:
432 ret = check_todo_list_from_file(the_repository);
434 case ACTION_REARRANGE_SQUASH:
435 ret = rearrange_squash_in_todo_file();
437 case ACTION_ADD_EXEC: {
438 struct string_list commands = STRING_LIST_INIT_DUP;
440 split_exec_commands(opts->cmd, &commands);
441 ret = add_exec_commands(&commands);
442 string_list_clear(&commands, 0);
446 BUG("invalid command '%d'", command);
452 static int parse_opt_keep_empty(const struct option *opt, const char *arg,
455 struct rebase_options *opts = opt->value;
460 * If we ever want to remap --keep-empty to --empty=keep, insert:
461 * opts->empty = unset ? EMPTY_UNSPECIFIED : EMPTY_KEEP;
463 opts->type = REBASE_INTERACTIVE;
467 static const char * const builtin_rebase_interactive_usage[] = {
468 N_("git rebase--interactive [<options>]"),
472 int cmd_rebase__interactive(int argc, const char **argv, const char *prefix)
474 struct rebase_options opts = REBASE_OPTIONS_INIT;
475 struct object_id squash_onto = null_oid;
476 enum action command = ACTION_NONE;
477 struct option options[] = {
478 OPT_NEGBIT(0, "ff", &opts.flags, N_("allow fast-forward"),
480 { OPTION_CALLBACK, 'k', "keep-empty", &options, NULL,
481 N_("(DEPRECATED) keep empty commits"),
482 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
483 parse_opt_keep_empty },
484 OPT_BOOL_F(0, "allow-empty-message", &opts.allow_empty_message,
485 N_("allow commits with empty messages"),
487 OPT_BOOL(0, "rebase-merges", &opts.rebase_merges, N_("rebase merge commits")),
488 OPT_BOOL(0, "rebase-cousins", &opts.rebase_cousins,
489 N_("keep original branch points of cousins")),
490 OPT_BOOL(0, "autosquash", &opts.autosquash,
491 N_("move commits that begin with squash!/fixup!")),
492 OPT_BOOL(0, "signoff", &opts.signoff, N_("sign commits")),
493 OPT_BIT('v', "verbose", &opts.flags,
494 N_("display a diffstat of what changed upstream"),
495 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
496 OPT_CMDMODE(0, "continue", &command, N_("continue rebase"),
498 OPT_CMDMODE(0, "skip", &command, N_("skip commit"), ACTION_SKIP),
499 OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
501 OPT_CMDMODE(0, "show-current-patch", &command, N_("show the current patch"),
502 ACTION_SHOW_CURRENT_PATCH),
503 OPT_CMDMODE(0, "shorten-ids", &command,
504 N_("shorten commit ids in the todo list"), ACTION_SHORTEN_OIDS),
505 OPT_CMDMODE(0, "expand-ids", &command,
506 N_("expand commit ids in the todo list"), ACTION_EXPAND_OIDS),
507 OPT_CMDMODE(0, "check-todo-list", &command,
508 N_("check the todo list"), ACTION_CHECK_TODO_LIST),
509 OPT_CMDMODE(0, "rearrange-squash", &command,
510 N_("rearrange fixup/squash lines"), ACTION_REARRANGE_SQUASH),
511 OPT_CMDMODE(0, "add-exec-commands", &command,
512 N_("insert exec commands in todo list"), ACTION_ADD_EXEC),
513 { OPTION_CALLBACK, 0, "onto", &opts.onto, N_("onto"), N_("onto"),
514 PARSE_OPT_NONEG, parse_opt_commit, 0 },
515 { OPTION_CALLBACK, 0, "restrict-revision", &opts.restrict_revision,
516 N_("restrict-revision"), N_("restrict revision"),
517 PARSE_OPT_NONEG, parse_opt_commit, 0 },
518 { OPTION_CALLBACK, 0, "squash-onto", &squash_onto, N_("squash-onto"),
519 N_("squash onto"), PARSE_OPT_NONEG, parse_opt_object_id, 0 },
520 { OPTION_CALLBACK, 0, "upstream", &opts.upstream, N_("upstream"),
521 N_("the upstream commit"), PARSE_OPT_NONEG, parse_opt_commit,
523 OPT_STRING(0, "head-name", &opts.head_name, N_("head-name"), N_("head name")),
524 { OPTION_STRING, 'S', "gpg-sign", &opts.gpg_sign_opt, N_("key-id"),
525 N_("GPG-sign commits"),
526 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
527 OPT_STRING(0, "strategy", &opts.strategy, N_("strategy"),
528 N_("rebase strategy")),
529 OPT_STRING(0, "strategy-opts", &opts.strategy_opts, N_("strategy-opts"),
530 N_("strategy options")),
531 OPT_STRING(0, "switch-to", &opts.switch_to, N_("switch-to"),
532 N_("the branch or commit to checkout")),
533 OPT_STRING(0, "onto-name", &opts.onto_name, N_("onto-name"), N_("onto name")),
534 OPT_STRING(0, "cmd", &opts.cmd, N_("cmd"), N_("the command to run")),
535 OPT_RERERE_AUTOUPDATE(&opts.allow_rerere_autoupdate),
536 OPT_BOOL(0, "reschedule-failed-exec", &opts.reschedule_failed_exec,
537 N_("automatically re-schedule any `exec` that fails")),
541 opts.rebase_cousins = -1;
544 usage_with_options(builtin_rebase_interactive_usage, options);
546 argc = parse_options(argc, argv, prefix, options,
547 builtin_rebase_interactive_usage, PARSE_OPT_KEEP_ARGV0);
549 if (!is_null_oid(&squash_onto))
550 opts.squash_onto = &squash_onto;
552 if (opts.rebase_cousins >= 0 && !opts.rebase_merges)
553 warning(_("--[no-]rebase-cousins has no effect without "
556 return !!run_rebase_interactive(&opts, command);
559 static int is_interactive(struct rebase_options *opts)
561 return opts->type == REBASE_INTERACTIVE ||
562 opts->type == REBASE_PRESERVE_MERGES;
565 static void imply_interactive(struct rebase_options *opts, const char *option)
567 switch (opts->type) {
569 die(_("%s requires an interactive rebase"), option);
571 case REBASE_INTERACTIVE:
572 case REBASE_PRESERVE_MERGES:
575 /* we now implement --merge via --interactive */
577 opts->type = REBASE_INTERACTIVE; /* implied */
582 /* Returns the filename prefixed by the state_dir */
583 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
585 static struct strbuf path = STRBUF_INIT;
586 static size_t prefix_len;
589 strbuf_addf(&path, "%s/", opts->state_dir);
590 prefix_len = path.len;
593 strbuf_setlen(&path, prefix_len);
594 strbuf_addstr(&path, filename);
598 /* Read one file, then strip line endings */
599 static int read_one(const char *path, struct strbuf *buf)
601 if (strbuf_read_file(buf, path, 0) < 0)
602 return error_errno(_("could not read '%s'"), path);
603 strbuf_trim_trailing_newline(buf);
607 /* Initialize the rebase options from the state directory. */
608 static int read_basic_state(struct rebase_options *opts)
610 struct strbuf head_name = STRBUF_INIT;
611 struct strbuf buf = STRBUF_INIT;
612 struct object_id oid;
614 if (read_one(state_dir_path("head-name", opts), &head_name) ||
615 read_one(state_dir_path("onto", opts), &buf))
617 opts->head_name = starts_with(head_name.buf, "refs/") ?
618 xstrdup(head_name.buf) : NULL;
619 strbuf_release(&head_name);
620 if (get_oid(buf.buf, &oid))
621 return error(_("could not get 'onto': '%s'"), buf.buf);
622 opts->onto = lookup_commit_or_die(&oid, buf.buf);
625 * We always write to orig-head, but interactive rebase used to write to
626 * head. Fall back to reading from head to cover for the case that the
627 * user upgraded git with an ongoing interactive rebase.
630 if (file_exists(state_dir_path("orig-head", opts))) {
631 if (read_one(state_dir_path("orig-head", opts), &buf))
633 } else if (read_one(state_dir_path("head", opts), &buf))
635 if (get_oid(buf.buf, &opts->orig_head))
636 return error(_("invalid orig-head: '%s'"), buf.buf);
638 if (file_exists(state_dir_path("quiet", opts)))
639 opts->flags &= ~REBASE_NO_QUIET;
641 opts->flags |= REBASE_NO_QUIET;
643 if (file_exists(state_dir_path("verbose", opts)))
644 opts->flags |= REBASE_VERBOSE;
646 if (file_exists(state_dir_path("signoff", opts))) {
648 opts->flags |= REBASE_FORCE;
651 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
653 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
656 if (!strcmp(buf.buf, "--rerere-autoupdate"))
657 opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
658 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
659 opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE;
661 warning(_("ignoring invalid allow_rerere_autoupdate: "
665 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
667 if (read_one(state_dir_path("gpg_sign_opt", opts),
670 free(opts->gpg_sign_opt);
671 opts->gpg_sign_opt = xstrdup(buf.buf);
674 if (file_exists(state_dir_path("strategy", opts))) {
676 if (read_one(state_dir_path("strategy", opts), &buf))
678 free(opts->strategy);
679 opts->strategy = xstrdup(buf.buf);
682 if (file_exists(state_dir_path("strategy_opts", opts))) {
684 if (read_one(state_dir_path("strategy_opts", opts), &buf))
686 free(opts->strategy_opts);
687 opts->strategy_opts = xstrdup(buf.buf);
690 strbuf_release(&buf);
695 static int rebase_write_basic_state(struct rebase_options *opts)
697 write_file(state_dir_path("head-name", opts), "%s",
698 opts->head_name ? opts->head_name : "detached HEAD");
699 write_file(state_dir_path("onto", opts), "%s",
700 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
701 write_file(state_dir_path("orig-head", opts), "%s",
702 oid_to_hex(&opts->orig_head));
703 if (!(opts->flags & REBASE_NO_QUIET))
704 write_file(state_dir_path("quiet", opts), "%s", "");
705 if (opts->flags & REBASE_VERBOSE)
706 write_file(state_dir_path("verbose", opts), "%s", "");
708 write_file(state_dir_path("strategy", opts), "%s",
710 if (opts->strategy_opts)
711 write_file(state_dir_path("strategy_opts", opts), "%s",
712 opts->strategy_opts);
713 if (opts->allow_rerere_autoupdate > 0)
714 write_file(state_dir_path("allow_rerere_autoupdate", opts),
715 "-%s-rerere-autoupdate",
716 opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
718 if (opts->gpg_sign_opt)
719 write_file(state_dir_path("gpg_sign_opt", opts), "%s",
722 write_file(state_dir_path("signoff", opts), "--signoff");
727 static int apply_autostash(struct rebase_options *opts)
729 const char *path = state_dir_path("autostash", opts);
730 struct strbuf autostash = STRBUF_INIT;
731 struct child_process stash_apply = CHILD_PROCESS_INIT;
733 if (!file_exists(path))
736 if (read_one(path, &autostash))
737 return error(_("Could not read '%s'"), path);
738 /* Ensure that the hash is not mistaken for a number */
739 strbuf_addstr(&autostash, "^0");
740 argv_array_pushl(&stash_apply.args,
741 "stash", "apply", autostash.buf, NULL);
742 stash_apply.git_cmd = 1;
743 stash_apply.no_stderr = stash_apply.no_stdout =
744 stash_apply.no_stdin = 1;
745 if (!run_command(&stash_apply))
746 printf(_("Applied autostash.\n"));
748 struct argv_array args = ARGV_ARRAY_INIT;
751 argv_array_pushl(&args,
752 "stash", "store", "-m", "autostash", "-q",
753 autostash.buf, NULL);
754 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
755 res = error(_("Cannot store %s"), autostash.buf);
756 argv_array_clear(&args);
757 strbuf_release(&autostash);
762 _("Applying autostash resulted in conflicts.\n"
763 "Your changes are safe in the stash.\n"
764 "You can run \"git stash pop\" or \"git stash drop\" "
768 strbuf_release(&autostash);
772 static int finish_rebase(struct rebase_options *opts)
774 struct strbuf dir = STRBUF_INIT;
775 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
778 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
779 apply_autostash(opts);
780 close_object_store(the_repository->objects);
782 * We ignore errors in 'gc --auto', since the
783 * user should see them.
785 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
786 if (opts->type == REBASE_INTERACTIVE) {
787 struct replay_opts replay = REPLAY_OPTS_INIT;
789 replay.action = REPLAY_INTERACTIVE_REBASE;
790 ret = sequencer_remove_state(&replay);
792 strbuf_addstr(&dir, opts->state_dir);
793 if (remove_dir_recursively(&dir, 0))
794 ret = error(_("could not remove '%s'"),
796 strbuf_release(&dir);
802 static struct commit *peel_committish(const char *name)
805 struct object_id oid;
807 if (get_oid(name, &oid))
809 obj = parse_object(the_repository, &oid);
810 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
813 static void add_var(struct strbuf *buf, const char *name, const char *value)
816 strbuf_addf(buf, "unset %s; ", name);
818 strbuf_addf(buf, "%s=", name);
819 sq_quote_buf(buf, value);
820 strbuf_addstr(buf, "; ");
824 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
826 #define RESET_HEAD_DETACH (1<<0)
827 #define RESET_HEAD_HARD (1<<1)
828 #define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
829 #define RESET_HEAD_REFS_ONLY (1<<3)
830 #define RESET_ORIG_HEAD (1<<4)
832 static int reset_head(struct object_id *oid, const char *action,
833 const char *switch_to_branch, unsigned flags,
834 const char *reflog_orig_head, const char *reflog_head)
836 unsigned detach_head = flags & RESET_HEAD_DETACH;
837 unsigned reset_hard = flags & RESET_HEAD_HARD;
838 unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
839 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
840 unsigned update_orig_head = flags & RESET_ORIG_HEAD;
841 struct object_id head_oid;
842 struct tree_desc desc[2] = { { NULL }, { NULL } };
843 struct lock_file lock = LOCK_INIT;
844 struct unpack_trees_options unpack_tree_opts;
846 const char *reflog_action;
847 struct strbuf msg = STRBUF_INIT;
849 struct object_id *orig = NULL, oid_orig,
850 *old_orig = NULL, oid_old_orig;
853 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
854 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
856 if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
858 goto leave_reset_head;
861 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
862 ret = error(_("could not determine HEAD revision"));
863 goto leave_reset_head;
870 goto reset_head_refs;
872 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
873 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
874 unpack_tree_opts.head_idx = 1;
875 unpack_tree_opts.src_index = the_repository->index;
876 unpack_tree_opts.dst_index = the_repository->index;
877 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
878 unpack_tree_opts.update = 1;
879 unpack_tree_opts.merge = 1;
881 unpack_tree_opts.reset = 1;
883 if (repo_read_index_unmerged(the_repository) < 0) {
884 ret = error(_("could not read index"));
885 goto leave_reset_head;
888 if (!reset_hard && !fill_tree_descriptor(the_repository, &desc[nr++], &head_oid)) {
889 ret = error(_("failed to find tree of %s"),
890 oid_to_hex(&head_oid));
891 goto leave_reset_head;
894 if (!fill_tree_descriptor(the_repository, &desc[nr++], oid)) {
895 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
896 goto leave_reset_head;
899 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
901 goto leave_reset_head;
904 tree = parse_tree_indirect(oid);
905 prime_cache_tree(the_repository, the_repository->index, tree);
907 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
908 ret = error(_("could not write index"));
909 goto leave_reset_head;
913 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
914 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
915 prefix_len = msg.len;
917 if (update_orig_head) {
918 if (!get_oid("ORIG_HEAD", &oid_old_orig))
919 old_orig = &oid_old_orig;
920 if (!get_oid("HEAD", &oid_orig)) {
922 if (!reflog_orig_head) {
923 strbuf_addstr(&msg, "updating ORIG_HEAD");
924 reflog_orig_head = msg.buf;
926 update_ref(reflog_orig_head, "ORIG_HEAD", orig,
927 old_orig, 0, UPDATE_REFS_MSG_ON_ERR);
929 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
933 strbuf_setlen(&msg, prefix_len);
934 strbuf_addstr(&msg, "updating HEAD");
935 reflog_head = msg.buf;
937 if (!switch_to_branch)
938 ret = update_ref(reflog_head, "HEAD", oid, orig,
939 detach_head ? REF_NO_DEREF : 0,
940 UPDATE_REFS_MSG_ON_ERR);
942 ret = update_ref(reflog_head, switch_to_branch, oid,
943 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
945 ret = create_symref("HEAD", switch_to_branch,
949 run_hook_le(NULL, "post-checkout",
950 oid_to_hex(orig ? orig : &null_oid),
951 oid_to_hex(oid), "1", NULL);
954 strbuf_release(&msg);
955 rollback_lock_file(&lock);
957 free((void *)desc[--nr].buffer);
961 static int move_to_original_branch(struct rebase_options *opts)
963 struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
966 if (!opts->head_name)
967 return 0; /* nothing to move back to */
970 BUG("move_to_original_branch without onto");
972 strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
973 opts->head_name, oid_to_hex(&opts->onto->object.oid));
974 strbuf_addf(&head_reflog, "rebase finished: returning to %s",
976 ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
977 orig_head_reflog.buf, head_reflog.buf);
979 strbuf_release(&orig_head_reflog);
980 strbuf_release(&head_reflog);
984 static const char *resolvemsg =
985 N_("Resolve all conflicts manually, mark them as resolved with\n"
986 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
987 "You can instead skip this commit: run \"git rebase --skip\".\n"
988 "To abort and get back to the state before \"git rebase\", run "
989 "\"git rebase --abort\".");
991 static int run_am(struct rebase_options *opts)
993 struct child_process am = CHILD_PROCESS_INIT;
994 struct child_process format_patch = CHILD_PROCESS_INIT;
995 struct strbuf revisions = STRBUF_INIT;
997 char *rebased_patches;
1000 argv_array_push(&am.args, "am");
1002 if (opts->action && !strcmp("continue", opts->action)) {
1003 argv_array_push(&am.args, "--resolved");
1004 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
1005 if (opts->gpg_sign_opt)
1006 argv_array_push(&am.args, opts->gpg_sign_opt);
1007 status = run_command(&am);
1011 return move_to_original_branch(opts);
1013 if (opts->action && !strcmp("skip", opts->action)) {
1014 argv_array_push(&am.args, "--skip");
1015 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
1016 status = run_command(&am);
1020 return move_to_original_branch(opts);
1022 if (opts->action && !strcmp("show-current-patch", opts->action)) {
1023 argv_array_push(&am.args, "--show-current-patch");
1024 return run_command(&am);
1027 strbuf_addf(&revisions, "%s...%s",
1028 oid_to_hex(opts->root ?
1029 /* this is now equivalent to !opts->upstream */
1030 &opts->onto->object.oid :
1031 &opts->upstream->object.oid),
1032 oid_to_hex(&opts->orig_head));
1034 rebased_patches = xstrdup(git_path("rebased-patches"));
1035 format_patch.out = open(rebased_patches,
1036 O_WRONLY | O_CREAT | O_TRUNC, 0666);
1037 if (format_patch.out < 0) {
1038 status = error_errno(_("could not open '%s' for writing"),
1040 free(rebased_patches);
1041 argv_array_clear(&am.args);
1045 format_patch.git_cmd = 1;
1046 argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
1047 "--full-index", "--cherry-pick", "--right-only",
1048 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
1049 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
1051 if (opts->git_format_patch_opt.len)
1052 argv_array_split(&format_patch.args,
1053 opts->git_format_patch_opt.buf);
1054 argv_array_push(&format_patch.args, revisions.buf);
1055 if (opts->restrict_revision)
1056 argv_array_pushf(&format_patch.args, "^%s",
1057 oid_to_hex(&opts->restrict_revision->object.oid));
1059 status = run_command(&format_patch);
1061 unlink(rebased_patches);
1062 free(rebased_patches);
1063 argv_array_clear(&am.args);
1065 reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
1067 error(_("\ngit encountered an error while preparing the "
1068 "patches to replay\n"
1069 "these revisions:\n"
1071 "As a result, git cannot rebase them."),
1074 strbuf_release(&revisions);
1077 strbuf_release(&revisions);
1079 am.in = open(rebased_patches, O_RDONLY);
1081 status = error_errno(_("could not open '%s' for reading"),
1083 free(rebased_patches);
1084 argv_array_clear(&am.args);
1088 argv_array_pushv(&am.args, opts->git_am_opts.argv);
1089 argv_array_push(&am.args, "--rebasing");
1090 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
1091 argv_array_push(&am.args, "--patch-format=mboxrd");
1092 if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
1093 argv_array_push(&am.args, "--rerere-autoupdate");
1094 else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
1095 argv_array_push(&am.args, "--no-rerere-autoupdate");
1096 if (opts->gpg_sign_opt)
1097 argv_array_push(&am.args, opts->gpg_sign_opt);
1098 status = run_command(&am);
1099 unlink(rebased_patches);
1100 free(rebased_patches);
1103 return move_to_original_branch(opts);
1106 if (is_directory(opts->state_dir))
1107 rebase_write_basic_state(opts);
1112 static int run_specific_rebase(struct rebase_options *opts, enum action action)
1114 const char *argv[] = { NULL, NULL };
1115 struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
1117 const char *backend, *backend_func;
1119 if (opts->type == REBASE_INTERACTIVE) {
1120 /* Run builtin interactive rebase */
1121 setenv("GIT_CHERRY_PICK_HELP", resolvemsg, 1);
1122 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
1123 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
1124 opts->autosquash = 0;
1126 if (opts->gpg_sign_opt) {
1127 /* remove the leading "-S" */
1128 char *tmp = xstrdup(opts->gpg_sign_opt + 2);
1129 free(opts->gpg_sign_opt);
1130 opts->gpg_sign_opt = tmp;
1133 status = run_rebase_interactive(opts, action);
1134 goto finished_rebase;
1137 if (opts->type == REBASE_AM) {
1138 status = run_am(opts);
1139 goto finished_rebase;
1142 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
1143 add_var(&script_snippet, "state_dir", opts->state_dir);
1145 add_var(&script_snippet, "upstream_name", opts->upstream_name);
1146 add_var(&script_snippet, "upstream", opts->upstream ?
1147 oid_to_hex(&opts->upstream->object.oid) : NULL);
1148 add_var(&script_snippet, "head_name",
1149 opts->head_name ? opts->head_name : "detached HEAD");
1150 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
1151 add_var(&script_snippet, "onto", opts->onto ?
1152 oid_to_hex(&opts->onto->object.oid) : NULL);
1153 add_var(&script_snippet, "onto_name", opts->onto_name);
1154 add_var(&script_snippet, "revisions", opts->revisions);
1155 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
1156 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
1157 sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
1158 add_var(&script_snippet, "git_am_opt", buf.buf);
1159 strbuf_release(&buf);
1160 add_var(&script_snippet, "verbose",
1161 opts->flags & REBASE_VERBOSE ? "t" : "");
1162 add_var(&script_snippet, "diffstat",
1163 opts->flags & REBASE_DIFFSTAT ? "t" : "");
1164 add_var(&script_snippet, "force_rebase",
1165 opts->flags & REBASE_FORCE ? "t" : "");
1166 if (opts->switch_to)
1167 add_var(&script_snippet, "switch_to", opts->switch_to);
1168 add_var(&script_snippet, "action", opts->action ? opts->action : "");
1169 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
1170 add_var(&script_snippet, "allow_rerere_autoupdate",
1171 opts->allow_rerere_autoupdate ?
1172 opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
1173 "--rerere-autoupdate" : "--no-rerere-autoupdate" : "");
1174 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
1175 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
1176 add_var(&script_snippet, "cmd", opts->cmd);
1177 add_var(&script_snippet, "allow_empty_message",
1178 opts->allow_empty_message ? "--allow-empty-message" : "");
1179 add_var(&script_snippet, "rebase_merges",
1180 opts->rebase_merges ? "t" : "");
1181 add_var(&script_snippet, "rebase_cousins",
1182 opts->rebase_cousins ? "t" : "");
1183 add_var(&script_snippet, "strategy", opts->strategy);
1184 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
1185 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
1186 add_var(&script_snippet, "squash_onto",
1187 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
1188 add_var(&script_snippet, "git_format_patch_opt",
1189 opts->git_format_patch_opt.buf);
1191 if (is_interactive(opts) &&
1192 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
1193 strbuf_addstr(&script_snippet,
1194 "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; ");
1195 opts->autosquash = 0;
1198 switch (opts->type) {
1199 case REBASE_PRESERVE_MERGES:
1200 backend = "git-rebase--preserve-merges";
1201 backend_func = "git_rebase__preserve_merges";
1204 BUG("Unhandled rebase type %d", opts->type);
1208 strbuf_addf(&script_snippet,
1209 ". git-sh-setup && . %s && %s", backend, backend_func);
1210 argv[0] = script_snippet.buf;
1212 status = run_command_v_opt(argv, RUN_USING_SHELL);
1214 if (opts->dont_finish_rebase)
1216 else if (opts->type == REBASE_INTERACTIVE)
1217 ; /* interactive rebase cleans up after itself */
1218 else if (status == 0) {
1219 if (!file_exists(state_dir_path("stopped-sha", opts)))
1220 finish_rebase(opts);
1221 } else if (status == 2) {
1222 struct strbuf dir = STRBUF_INIT;
1224 apply_autostash(opts);
1225 strbuf_addstr(&dir, opts->state_dir);
1226 remove_dir_recursively(&dir, 0);
1227 strbuf_release(&dir);
1228 die("Nothing to do");
1231 strbuf_release(&script_snippet);
1233 return status ? -1 : 0;
1236 static int rebase_config(const char *var, const char *value, void *data)
1238 struct rebase_options *opts = data;
1240 if (!strcmp(var, "rebase.stat")) {
1241 if (git_config_bool(var, value))
1242 opts->flags |= REBASE_DIFFSTAT;
1244 opts->flags &= ~REBASE_DIFFSTAT;
1248 if (!strcmp(var, "rebase.autosquash")) {
1249 opts->autosquash = git_config_bool(var, value);
1253 if (!strcmp(var, "commit.gpgsign")) {
1254 free(opts->gpg_sign_opt);
1255 opts->gpg_sign_opt = git_config_bool(var, value) ?
1256 xstrdup("-S") : NULL;
1260 if (!strcmp(var, "rebase.autostash")) {
1261 opts->autostash = git_config_bool(var, value);
1265 if (!strcmp(var, "rebase.reschedulefailedexec")) {
1266 opts->reschedule_failed_exec = git_config_bool(var, value);
1270 if (!strcmp(var, "rebase.usebuiltin")) {
1271 opts->use_legacy_rebase = !git_config_bool(var, value);
1275 return git_default_config(var, value, data);
1279 * Determines whether the commits in from..to are linear, i.e. contain
1280 * no merge commits. This function *expects* `from` to be an ancestor of
1283 static int is_linear_history(struct commit *from, struct commit *to)
1285 while (to && to != from) {
1289 if (to->parents->next)
1291 to = to->parents->item;
1296 static int can_fast_forward(struct commit *onto, struct commit *upstream,
1297 struct commit *restrict_revision,
1298 struct object_id *head_oid, struct object_id *merge_base)
1300 struct commit *head = lookup_commit(the_repository, head_oid);
1301 struct commit_list *merge_bases = NULL;
1307 merge_bases = get_merge_bases(onto, head);
1308 if (!merge_bases || merge_bases->next) {
1309 oidcpy(merge_base, &null_oid);
1313 oidcpy(merge_base, &merge_bases->item->object.oid);
1314 if (!oideq(merge_base, &onto->object.oid))
1317 if (restrict_revision && !oideq(&restrict_revision->object.oid, merge_base))
1323 free_commit_list(merge_bases);
1324 merge_bases = get_merge_bases(upstream, head);
1325 if (!merge_bases || merge_bases->next)
1328 if (!oideq(&onto->object.oid, &merge_bases->item->object.oid))
1334 free_commit_list(merge_bases);
1335 return res && is_linear_history(onto, head);
1338 /* -i followed by -m is still -i */
1339 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
1341 struct rebase_options *opts = opt->value;
1343 BUG_ON_OPT_NEG(unset);
1344 BUG_ON_OPT_ARG(arg);
1346 if (!is_interactive(opts))
1347 opts->type = REBASE_MERGE;
1352 /* -i followed by -p is still explicitly interactive, but -p alone is not */
1353 static int parse_opt_interactive(const struct option *opt, const char *arg,
1356 struct rebase_options *opts = opt->value;
1358 BUG_ON_OPT_NEG(unset);
1359 BUG_ON_OPT_ARG(arg);
1361 opts->type = REBASE_INTERACTIVE;
1362 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
1367 static enum empty_type parse_empty_value(const char *value)
1369 if (!strcasecmp(value, "drop"))
1371 else if (!strcasecmp(value, "keep"))
1373 else if (!strcasecmp(value, "ask"))
1376 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value);
1379 static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
1381 struct rebase_options *options = opt->value;
1382 enum empty_type value = parse_empty_value(arg);
1384 BUG_ON_OPT_NEG(unset);
1386 options->empty = value;
1390 static void NORETURN error_on_missing_default_upstream(void)
1392 struct branch *current_branch = branch_get(NULL);
1395 "Please specify which branch you want to rebase against.\n"
1396 "See git-rebase(1) for details.\n"
1398 " git rebase '<branch>'\n"
1400 current_branch ? _("There is no tracking information for "
1401 "the current branch.") :
1402 _("You are not currently on a branch."));
1404 if (current_branch) {
1405 const char *remote = current_branch->remote_name;
1408 remote = _("<remote>");
1410 printf(_("If you wish to set tracking information for this "
1411 "branch you can do so with:\n"
1413 " git branch --set-upstream-to=%s/<branch> %s\n"
1415 remote, current_branch->name);
1420 static void set_reflog_action(struct rebase_options *options)
1423 struct strbuf buf = STRBUF_INIT;
1425 if (!is_interactive(options))
1428 env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1429 if (env && strcmp("rebase", env))
1430 return; /* only override it if it is "rebase" */
1432 strbuf_addf(&buf, "rebase -i (%s)", options->action);
1433 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
1434 strbuf_release(&buf);
1437 static int check_exec_cmd(const char *cmd)
1439 if (strchr(cmd, '\n'))
1440 return error(_("exec commands cannot contain newlines"));
1442 /* Does the command consist purely of whitespace? */
1443 if (!cmd[strspn(cmd, " \t\r\f\v")])
1444 return error(_("empty exec command"));
1450 int cmd_rebase(int argc, const char **argv, const char *prefix)
1452 struct rebase_options options = REBASE_OPTIONS_INIT;
1453 const char *branch_name;
1454 int ret, flags, total_argc, in_progress = 0;
1456 int ok_to_skip_pre_rebase = 0;
1457 struct strbuf msg = STRBUF_INIT;
1458 struct strbuf revisions = STRBUF_INIT;
1459 struct strbuf buf = STRBUF_INIT;
1460 struct object_id merge_base;
1461 enum action action = ACTION_NONE;
1462 const char *gpg_sign = NULL;
1463 struct string_list exec = STRING_LIST_INIT_NODUP;
1464 const char *rebase_merges = NULL;
1465 int fork_point = -1;
1466 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1467 struct object_id squash_onto;
1468 char *squash_onto_name = NULL;
1469 int reschedule_failed_exec = -1;
1470 int allow_preemptive_ff = 1;
1471 struct option builtin_rebase_options[] = {
1472 OPT_STRING(0, "onto", &options.onto_name,
1474 N_("rebase onto given branch instead of upstream")),
1475 OPT_BOOL(0, "keep-base", &keep_base,
1476 N_("use the merge-base of upstream and branch as the current base")),
1477 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1478 N_("allow pre-rebase hook to run")),
1479 OPT_NEGBIT('q', "quiet", &options.flags,
1480 N_("be quiet. implies --no-stat"),
1481 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1482 OPT_BIT('v', "verbose", &options.flags,
1483 N_("display a diffstat of what changed upstream"),
1484 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1485 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1486 N_("do not show diffstat of what changed upstream"),
1487 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1488 OPT_BOOL(0, "signoff", &options.signoff,
1489 N_("add a Signed-off-by: line to each commit")),
1490 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
1491 NULL, N_("passed to 'git am'"),
1493 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1494 &options.git_am_opts, NULL,
1495 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1496 OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1497 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1498 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1499 N_("passed to 'git apply'"), 0),
1500 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1501 N_("action"), N_("passed to 'git apply'"), 0),
1502 OPT_BIT('f', "force-rebase", &options.flags,
1503 N_("cherry-pick all commits, even if unchanged"),
1505 OPT_BIT(0, "no-ff", &options.flags,
1506 N_("cherry-pick all commits, even if unchanged"),
1508 OPT_CMDMODE(0, "continue", &action, N_("continue"),
1510 OPT_CMDMODE(0, "skip", &action,
1511 N_("skip current patch and continue"), ACTION_SKIP),
1512 OPT_CMDMODE(0, "abort", &action,
1513 N_("abort and check out the original branch"),
1515 OPT_CMDMODE(0, "quit", &action,
1516 N_("abort but keep HEAD where it is"), ACTION_QUIT),
1517 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1518 "during an interactive rebase"), ACTION_EDIT_TODO),
1519 OPT_CMDMODE(0, "show-current-patch", &action,
1520 N_("show the patch file being applied or merged"),
1521 ACTION_SHOW_CURRENT_PATCH),
1522 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
1523 N_("use merging strategies to rebase"),
1524 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1526 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
1527 N_("let the user edit the list of commits to rebase"),
1528 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1529 parse_opt_interactive },
1530 OPT_SET_INT_F('p', "preserve-merges", &options.type,
1531 N_("(DEPRECATED) try to recreate merges instead of "
1533 REBASE_PRESERVE_MERGES, PARSE_OPT_HIDDEN),
1534 OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
1535 OPT_CALLBACK_F(0, "empty", &options, N_("{drop,keep,ask}"),
1536 N_("how to handle commits that become empty"),
1537 PARSE_OPT_NONEG, parse_opt_empty),
1538 { OPTION_CALLBACK, 'k', "keep-empty", &options, NULL,
1539 N_("(DEPRECATED) keep empty commits"),
1540 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
1541 parse_opt_keep_empty },
1542 OPT_BOOL(0, "autosquash", &options.autosquash,
1543 N_("move commits that begin with "
1544 "squash!/fixup! under -i")),
1545 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1546 N_("GPG-sign commits"),
1547 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1548 OPT_BOOL(0, "autostash", &options.autostash,
1549 N_("automatically stash/stash pop before and after")),
1550 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1551 N_("add exec lines after each commit of the "
1553 OPT_BOOL_F(0, "allow-empty-message",
1554 &options.allow_empty_message,
1555 N_("allow rebasing commits with empty messages"),
1557 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1559 N_("try to rebase merges instead of skipping them"),
1560 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
1561 OPT_BOOL(0, "fork-point", &fork_point,
1562 N_("use 'merge-base --fork-point' to refine upstream")),
1563 OPT_STRING('s', "strategy", &options.strategy,
1564 N_("strategy"), N_("use the given merge strategy")),
1565 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1567 N_("pass the argument through to the merge "
1569 OPT_BOOL(0, "root", &options.root,
1570 N_("rebase all reachable commits up to the root(s)")),
1571 OPT_BOOL(0, "reschedule-failed-exec",
1572 &reschedule_failed_exec,
1573 N_("automatically re-schedule any `exec` that fails")),
1578 if (argc == 2 && !strcmp(argv[1], "-h"))
1579 usage_with_options(builtin_rebase_usage,
1580 builtin_rebase_options);
1582 options.allow_empty_message = 1;
1583 git_config(rebase_config, &options);
1585 if (options.use_legacy_rebase ||
1586 !git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1))
1587 warning(_("the rebase.useBuiltin support has been removed!\n"
1588 "See its entry in 'git help config' for details."));
1591 strbuf_addf(&buf, "%s/applying", apply_dir());
1592 if(file_exists(buf.buf))
1593 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1595 if (is_directory(apply_dir())) {
1596 options.type = REBASE_AM;
1597 options.state_dir = apply_dir();
1598 } else if (is_directory(merge_dir())) {
1600 strbuf_addf(&buf, "%s/rewritten", merge_dir());
1601 if (is_directory(buf.buf)) {
1602 options.type = REBASE_PRESERVE_MERGES;
1603 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1606 strbuf_addf(&buf, "%s/interactive", merge_dir());
1607 if(file_exists(buf.buf)) {
1608 options.type = REBASE_INTERACTIVE;
1609 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1611 options.type = REBASE_MERGE;
1613 options.state_dir = merge_dir();
1616 if (options.type != REBASE_UNSPECIFIED)
1620 argc = parse_options(argc, argv, prefix,
1621 builtin_rebase_options,
1622 builtin_rebase_usage, 0);
1624 if (action != ACTION_NONE && total_argc != 2) {
1625 usage_with_options(builtin_rebase_usage,
1626 builtin_rebase_options);
1630 usage_with_options(builtin_rebase_usage,
1631 builtin_rebase_options);
1633 if (options.type == REBASE_PRESERVE_MERGES)
1634 warning(_("git rebase --preserve-merges is deprecated. "
1635 "Use --rebase-merges instead."));
1638 if (options.onto_name)
1639 die(_("cannot combine '--keep-base' with '--onto'"));
1641 die(_("cannot combine '--keep-base' with '--root'"));
1644 if (action != ACTION_NONE && !in_progress)
1645 die(_("No rebase in progress?"));
1646 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1648 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
1649 die(_("The --edit-todo action can only be used during "
1650 "interactive rebase."));
1652 if (trace2_is_enabled()) {
1653 if (is_interactive(&options))
1654 trace2_cmd_mode("interactive");
1656 trace2_cmd_mode("interactive-exec");
1658 trace2_cmd_mode(action_names[action]);
1662 case ACTION_CONTINUE: {
1663 struct object_id head;
1664 struct lock_file lock_file = LOCK_INIT;
1667 options.action = "continue";
1668 set_reflog_action(&options);
1671 if (get_oid("HEAD", &head))
1672 die(_("Cannot read HEAD"));
1674 fd = hold_locked_index(&lock_file, 0);
1675 if (repo_read_index(the_repository) < 0)
1676 die(_("could not read index"));
1677 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1680 repo_update_index_if_able(the_repository, &lock_file);
1681 rollback_lock_file(&lock_file);
1683 if (has_unstaged_changes(the_repository, 1)) {
1684 puts(_("You must edit all merge conflicts and then\n"
1685 "mark them as resolved using git add"));
1688 if (read_basic_state(&options))
1693 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1695 options.action = "skip";
1696 set_reflog_action(&options);
1698 rerere_clear(the_repository, &merge_rr);
1699 string_list_clear(&merge_rr, 1);
1701 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1703 die(_("could not discard worktree changes"));
1704 remove_branch_state(the_repository, 0);
1705 if (read_basic_state(&options))
1709 case ACTION_ABORT: {
1710 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1711 options.action = "abort";
1712 set_reflog_action(&options);
1714 rerere_clear(the_repository, &merge_rr);
1715 string_list_clear(&merge_rr, 1);
1717 if (read_basic_state(&options))
1719 if (reset_head(&options.orig_head, "reset",
1720 options.head_name, RESET_HEAD_HARD,
1722 die(_("could not move back to %s"),
1723 oid_to_hex(&options.orig_head));
1724 remove_branch_state(the_repository, 0);
1725 ret = !!finish_rebase(&options);
1729 if (options.type == REBASE_INTERACTIVE) {
1730 struct replay_opts replay = REPLAY_OPTS_INIT;
1732 replay.action = REPLAY_INTERACTIVE_REBASE;
1733 ret = !!sequencer_remove_state(&replay);
1736 strbuf_addstr(&buf, options.state_dir);
1737 ret = !!remove_dir_recursively(&buf, 0);
1739 error(_("could not remove '%s'"),
1744 case ACTION_EDIT_TODO:
1745 options.action = "edit-todo";
1746 options.dont_finish_rebase = 1;
1748 case ACTION_SHOW_CURRENT_PATCH:
1749 options.action = "show-current-patch";
1750 options.dont_finish_rebase = 1;
1755 BUG("action: %d", action);
1758 /* Make sure no rebase is in progress */
1760 const char *last_slash = strrchr(options.state_dir, '/');
1761 const char *state_dir_base =
1762 last_slash ? last_slash + 1 : options.state_dir;
1763 const char *cmd_live_rebase =
1764 "git rebase (--continue | --abort | --skip)";
1766 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1767 die(_("It seems that there is already a %s directory, and\n"
1768 "I wonder if you are in the middle of another rebase. "
1770 "case, please try\n\t%s\n"
1771 "If that is not the case, please\n\t%s\n"
1772 "and run me again. I am stopping in case you still "
1774 "valuable there.\n"),
1775 state_dir_base, cmd_live_rebase, buf.buf);
1778 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
1779 (action != ACTION_NONE) ||
1781 options.autosquash) {
1782 allow_preemptive_ff = 0;
1785 for (i = 0; i < options.git_am_opts.argc; i++) {
1786 const char *option = options.git_am_opts.argv[i], *p;
1787 if (!strcmp(option, "--committer-date-is-author-date") ||
1788 !strcmp(option, "--ignore-date") ||
1789 !strcmp(option, "--whitespace=fix") ||
1790 !strcmp(option, "--whitespace=strip"))
1791 allow_preemptive_ff = 0;
1792 else if (skip_prefix(option, "-C", &p)) {
1794 if (!isdigit(*(p++)))
1795 die(_("switch `C' expects a "
1796 "numerical value"));
1797 } else if (skip_prefix(option, "--whitespace=", &p)) {
1798 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1799 strcmp(p, "error") && strcmp(p, "error-all"))
1800 die("Invalid whitespace option: '%s'", p);
1804 for (i = 0; i < exec.nr; i++)
1805 if (check_exec_cmd(exec.items[i].string))
1808 if (!(options.flags & REBASE_NO_QUIET))
1809 argv_array_push(&options.git_am_opts, "-q");
1811 if (options.empty != EMPTY_UNSPECIFIED)
1812 imply_interactive(&options, "--empty");
1815 free(options.gpg_sign_opt);
1816 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1822 imply_interactive(&options, "--exec");
1825 for (i = 0; i < exec.nr; i++)
1826 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1827 options.cmd = xstrdup(buf.buf);
1830 if (rebase_merges) {
1831 if (!*rebase_merges)
1832 ; /* default mode; do nothing */
1833 else if (!strcmp("rebase-cousins", rebase_merges))
1834 options.rebase_cousins = 1;
1835 else if (strcmp("no-rebase-cousins", rebase_merges))
1836 die(_("Unknown mode: %s"), rebase_merges);
1837 options.rebase_merges = 1;
1838 imply_interactive(&options, "--rebase-merges");
1841 if (strategy_options.nr) {
1844 if (!options.strategy)
1845 options.strategy = "recursive";
1848 for (i = 0; i < strategy_options.nr; i++)
1849 strbuf_addf(&buf, " --%s",
1850 strategy_options.items[i].string);
1851 options.strategy_opts = xstrdup(buf.buf);
1854 if (options.strategy) {
1855 options.strategy = xstrdup(options.strategy);
1856 switch (options.type) {
1858 die(_("--strategy requires --merge or --interactive"));
1860 case REBASE_INTERACTIVE:
1861 case REBASE_PRESERVE_MERGES:
1864 case REBASE_UNSPECIFIED:
1865 options.type = REBASE_MERGE;
1868 BUG("unhandled rebase type (%d)", options.type);
1872 if (options.type == REBASE_MERGE)
1873 imply_interactive(&options, "--merge");
1875 if (options.root && !options.onto_name)
1876 imply_interactive(&options, "--root without --onto");
1878 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1879 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1881 switch (options.type) {
1883 case REBASE_INTERACTIVE:
1884 case REBASE_PRESERVE_MERGES:
1885 options.state_dir = merge_dir();
1888 options.state_dir = apply_dir();
1891 /* the default rebase backend is `--am` */
1892 options.type = REBASE_AM;
1893 options.state_dir = apply_dir();
1897 if (options.empty == EMPTY_UNSPECIFIED) {
1898 if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
1899 options.empty = EMPTY_ASK;
1900 else if (exec.nr > 0)
1901 options.empty = EMPTY_KEEP;
1903 options.empty = EMPTY_DROP;
1905 if (reschedule_failed_exec > 0 && !is_interactive(&options))
1906 die(_("--reschedule-failed-exec requires "
1907 "--exec or --interactive"));
1908 if (reschedule_failed_exec >= 0)
1909 options.reschedule_failed_exec = reschedule_failed_exec;
1911 if (options.git_am_opts.argc) {
1912 /* all am options except -q are compatible only with --am */
1913 for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1914 if (strcmp(options.git_am_opts.argv[i], "-q"))
1917 if (is_interactive(&options) && i >= 0)
1918 die(_("cannot combine am options with either "
1919 "interactive or merge options"));
1922 if (options.signoff) {
1923 if (options.type == REBASE_PRESERVE_MERGES)
1924 die("cannot combine '--signoff' with "
1925 "'--preserve-merges'");
1926 argv_array_push(&options.git_am_opts, "--signoff");
1927 options.flags |= REBASE_FORCE;
1930 if (options.type == REBASE_PRESERVE_MERGES) {
1932 * Note: incompatibility with --signoff handled in signoff block above
1933 * Note: incompatibility with --interactive is just a strong warning;
1934 * git-rebase.txt caveats with "unless you know what you are doing"
1936 if (options.rebase_merges)
1937 die(_("cannot combine '--preserve-merges' with "
1938 "'--rebase-merges'"));
1940 if (options.reschedule_failed_exec)
1941 die(_("error: cannot combine '--preserve-merges' with "
1942 "'--reschedule-failed-exec'"));
1945 if (!options.root) {
1947 struct branch *branch;
1949 branch = branch_get(NULL);
1950 options.upstream_name = branch_get_upstream(branch,
1952 if (!options.upstream_name)
1953 error_on_missing_default_upstream();
1957 options.upstream_name = argv[0];
1960 if (!strcmp(options.upstream_name, "-"))
1961 options.upstream_name = "@{-1}";
1963 options.upstream = peel_committish(options.upstream_name);
1964 if (!options.upstream)
1965 die(_("invalid upstream '%s'"), options.upstream_name);
1966 options.upstream_arg = options.upstream_name;
1968 if (!options.onto_name) {
1969 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1970 &squash_onto, NULL, NULL) < 0)
1971 die(_("Could not create new root commit"));
1972 options.squash_onto = &squash_onto;
1973 options.onto_name = squash_onto_name =
1974 xstrdup(oid_to_hex(&squash_onto));
1976 options.root_with_onto = 1;
1978 options.upstream_name = NULL;
1979 options.upstream = NULL;
1981 usage_with_options(builtin_rebase_usage,
1982 builtin_rebase_options);
1983 options.upstream_arg = "--root";
1986 /* Make sure the branch to rebase onto is valid. */
1989 strbuf_addstr(&buf, options.upstream_name);
1990 strbuf_addstr(&buf, "...");
1991 options.onto_name = xstrdup(buf.buf);
1992 } else if (!options.onto_name)
1993 options.onto_name = options.upstream_name;
1994 if (strstr(options.onto_name, "...")) {
1995 if (get_oid_mb(options.onto_name, &merge_base) < 0) {
1997 die(_("'%s': need exactly one merge base with branch"),
1998 options.upstream_name);
2000 die(_("'%s': need exactly one merge base"),
2003 options.onto = lookup_commit_or_die(&merge_base,
2006 options.onto = peel_committish(options.onto_name);
2008 die(_("Does not point to a valid commit '%s'"),
2013 * If the branch to rebase is given, that is the branch we will rebase
2014 * branch_name -- branch/commit being rebased, or
2015 * HEAD (already detached)
2016 * orig_head -- commit object name of tip of the branch before rebasing
2017 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
2020 /* Is it "rebase other branchname" or "rebase other commit"? */
2021 branch_name = argv[0];
2022 options.switch_to = argv[0];
2024 /* Is it a local branch? */
2026 strbuf_addf(&buf, "refs/heads/%s", branch_name);
2027 if (!read_ref(buf.buf, &options.orig_head))
2028 options.head_name = xstrdup(buf.buf);
2029 /* If not is it a valid ref (branch or commit)? */
2030 else if (!get_oid(branch_name, &options.orig_head))
2031 options.head_name = NULL;
2033 die(_("fatal: no such branch/commit '%s'"),
2035 } else if (argc == 0) {
2036 /* Do not need to switch branches, we are already on it. */
2038 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
2040 if (!options.head_name)
2041 die(_("No such ref: %s"), "HEAD");
2042 if (flags & REF_ISSYMREF) {
2043 if (!skip_prefix(options.head_name,
2044 "refs/heads/", &branch_name))
2045 branch_name = options.head_name;
2048 FREE_AND_NULL(options.head_name);
2049 branch_name = "HEAD";
2051 if (get_oid("HEAD", &options.orig_head))
2052 die(_("Could not resolve HEAD to a revision"));
2054 BUG("unexpected number of arguments left to parse");
2056 if (fork_point > 0) {
2057 struct commit *head =
2058 lookup_commit_reference(the_repository,
2059 &options.orig_head);
2060 options.restrict_revision =
2061 get_fork_point(options.upstream_name, head);
2064 if (repo_read_index(the_repository) < 0)
2065 die(_("could not read index"));
2067 if (options.autostash) {
2068 struct lock_file lock_file = LOCK_INIT;
2071 fd = hold_locked_index(&lock_file, 0);
2072 refresh_cache(REFRESH_QUIET);
2074 repo_update_index_if_able(the_repository, &lock_file);
2075 rollback_lock_file(&lock_file);
2077 if (has_unstaged_changes(the_repository, 1) ||
2078 has_uncommitted_changes(the_repository, 1)) {
2079 const char *autostash =
2080 state_dir_path("autostash", &options);
2081 struct child_process stash = CHILD_PROCESS_INIT;
2082 struct object_id oid;
2084 argv_array_pushl(&stash.args,
2085 "stash", "create", "autostash", NULL);
2089 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
2090 die(_("Cannot autostash"));
2091 strbuf_trim_trailing_newline(&buf);
2092 if (get_oid(buf.buf, &oid))
2093 die(_("Unexpected stash response: '%s'"),
2096 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
2098 if (safe_create_leading_directories_const(autostash))
2099 die(_("Could not create directory for '%s'"),
2101 write_file(autostash, "%s", oid_to_hex(&oid));
2102 printf(_("Created autostash: %s\n"), buf.buf);
2103 if (reset_head(NULL, "reset --hard",
2104 NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
2105 die(_("could not reset --hard"));
2107 if (discard_index(the_repository->index) < 0 ||
2108 repo_read_index(the_repository) < 0)
2109 die(_("could not read index"));
2113 if (require_clean_work_tree(the_repository, "rebase",
2114 _("Please commit or stash them."), 1, 1)) {
2120 * Now we are rebasing commits upstream..orig_head (or with --root,
2121 * everything leading up to orig_head) on top of onto.
2125 * Check if we are already based on onto with linear history,
2126 * in which case we could fast-forward without replacing the commits
2127 * with new commits recreated by replaying their changes.
2129 * Note that can_fast_forward() initializes merge_base, so we have to
2130 * call it before checking allow_preemptive_ff.
2132 if (can_fast_forward(options.onto, options.upstream, options.restrict_revision,
2133 &options.orig_head, &merge_base) &&
2134 allow_preemptive_ff) {
2137 if (!(options.flags & REBASE_FORCE)) {
2138 /* Lazily switch to the target branch if needed... */
2139 if (options.switch_to) {
2140 struct object_id oid;
2142 if (get_oid(options.switch_to, &oid) < 0) {
2143 ret = !!error(_("could not parse '%s'"),
2149 strbuf_addf(&buf, "%s: checkout %s",
2150 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
2152 if (reset_head(&oid, "checkout",
2154 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2155 NULL, buf.buf) < 0) {
2156 ret = !!error(_("could not switch to "
2163 if (!(options.flags & REBASE_NO_QUIET))
2165 else if (!strcmp(branch_name, "HEAD") &&
2166 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
2167 puts(_("HEAD is up to date."));
2169 printf(_("Current branch %s is up to date.\n"),
2171 ret = !!finish_rebase(&options);
2173 } else if (!(options.flags & REBASE_NO_QUIET))
2175 else if (!strcmp(branch_name, "HEAD") &&
2176 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
2177 puts(_("HEAD is up to date, rebase forced."));
2179 printf(_("Current branch %s is up to date, rebase "
2180 "forced.\n"), branch_name);
2183 /* If a hook exists, give it a chance to interrupt*/
2184 if (!ok_to_skip_pre_rebase &&
2185 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
2186 argc ? argv[0] : NULL, NULL))
2187 die(_("The pre-rebase hook refused to rebase."));
2189 if (options.flags & REBASE_DIFFSTAT) {
2190 struct diff_options opts;
2192 if (options.flags & REBASE_VERBOSE) {
2193 if (is_null_oid(&merge_base))
2194 printf(_("Changes to %s:\n"),
2195 oid_to_hex(&options.onto->object.oid));
2197 printf(_("Changes from %s to %s:\n"),
2198 oid_to_hex(&merge_base),
2199 oid_to_hex(&options.onto->object.oid));
2202 /* We want color (if set), but no pager */
2204 opts.stat_width = -1; /* use full terminal width */
2205 opts.stat_graph_width = -1; /* respect statGraphWidth config */
2206 opts.output_format |=
2207 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
2208 opts.detect_rename = DIFF_DETECT_RENAME;
2209 diff_setup_done(&opts);
2210 diff_tree_oid(is_null_oid(&merge_base) ?
2211 the_hash_algo->empty_tree : &merge_base,
2212 &options.onto->object.oid, "", &opts);
2213 diffcore_std(&opts);
2217 if (is_interactive(&options))
2220 /* Detach HEAD and reset the tree */
2221 if (options.flags & REBASE_NO_QUIET)
2222 printf(_("First, rewinding head to replay your work on top of "
2225 strbuf_addf(&msg, "%s: checkout %s",
2226 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
2227 if (reset_head(&options.onto->object.oid, "checkout", NULL,
2228 RESET_HEAD_DETACH | RESET_ORIG_HEAD |
2229 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2231 die(_("Could not detach HEAD"));
2232 strbuf_release(&msg);
2235 * If the onto is a proper descendant of the tip of the branch, then
2236 * we just fast-forwarded.
2239 if (oideq(&merge_base, &options.orig_head)) {
2240 printf(_("Fast-forwarded %s to %s.\n"),
2241 branch_name, options.onto_name);
2242 strbuf_addf(&msg, "rebase finished: %s onto %s",
2243 options.head_name ? options.head_name : "detached HEAD",
2244 oid_to_hex(&options.onto->object.oid));
2245 reset_head(NULL, "Fast-forwarded", options.head_name,
2246 RESET_HEAD_REFS_ONLY, "HEAD", msg.buf);
2247 strbuf_release(&msg);
2248 ret = !!finish_rebase(&options);
2252 strbuf_addf(&revisions, "%s..%s",
2253 options.root ? oid_to_hex(&options.onto->object.oid) :
2254 (options.restrict_revision ?
2255 oid_to_hex(&options.restrict_revision->object.oid) :
2256 oid_to_hex(&options.upstream->object.oid)),
2257 oid_to_hex(&options.orig_head));
2259 options.revisions = revisions.buf;
2262 ret = !!run_specific_rebase(&options, action);
2265 strbuf_release(&buf);
2266 strbuf_release(&revisions);
2267 free(options.head_name);
2268 free(options.gpg_sign_opt);
2270 free(squash_onto_name);