Merge branch 'dl/doc-stash-remove-mention-of-reflog'
[git.git] / builtin / rebase.c
blobca6aa0dc7a8685a7dbdd5e07f5b20be7e3a38756
1 /*
2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
5 */
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
8 #include "builtin.h"
9 #include "run-command.h"
10 #include "exec-cmd.h"
11 #include "argv-array.h"
12 #include "dir.h"
13 #include "packfile.h"
14 #include "refs.h"
15 #include "quote.h"
16 #include "config.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
19 #include "lockfile.h"
20 #include "parse-options.h"
21 #include "commit.h"
22 #include "diff.h"
23 #include "wt-status.h"
24 #include "revision.h"
25 #include "commit-reach.h"
26 #include "rerere.h"
27 #include "branch.h"
28 #include "sequencer.h"
29 #include "rebase-interactive.h"
30 #include "reset.h"
32 #define DEFAULT_REFLOG_ACTION "rebase"
34 static char const * const builtin_rebase_usage[] = {
35 N_("git rebase [-i] [options] [--exec <cmd>] "
36 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
37 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
38 "--root [<branch>]"),
39 N_("git rebase --continue | --abort | --skip | --edit-todo"),
40 NULL
43 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
44 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
45 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
46 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
48 enum rebase_type {
49 REBASE_UNSPECIFIED = -1,
50 REBASE_APPLY,
51 REBASE_MERGE,
52 REBASE_PRESERVE_MERGES
55 enum empty_type {
56 EMPTY_UNSPECIFIED = -1,
57 EMPTY_DROP,
58 EMPTY_KEEP,
59 EMPTY_ASK
62 struct rebase_options {
63 enum rebase_type type;
64 enum empty_type empty;
65 const char *default_backend;
66 const char *state_dir;
67 struct commit *upstream;
68 const char *upstream_name;
69 const char *upstream_arg;
70 char *head_name;
71 struct object_id orig_head;
72 struct commit *onto;
73 const char *onto_name;
74 const char *revisions;
75 const char *switch_to;
76 int root, root_with_onto;
77 struct object_id *squash_onto;
78 struct commit *restrict_revision;
79 int dont_finish_rebase;
80 enum {
81 REBASE_NO_QUIET = 1<<0,
82 REBASE_VERBOSE = 1<<1,
83 REBASE_DIFFSTAT = 1<<2,
84 REBASE_FORCE = 1<<3,
85 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
86 } flags;
87 struct argv_array git_am_opts;
88 const char *action;
89 int signoff;
90 int allow_rerere_autoupdate;
91 int keep_empty;
92 int autosquash;
93 char *gpg_sign_opt;
94 int autostash;
95 char *cmd;
96 int allow_empty_message;
97 int rebase_merges, rebase_cousins;
98 char *strategy, *strategy_opts;
99 struct strbuf git_format_patch_opt;
100 int reschedule_failed_exec;
101 int use_legacy_rebase;
102 int reapply_cherry_picks;
105 #define REBASE_OPTIONS_INIT { \
106 .type = REBASE_UNSPECIFIED, \
107 .empty = EMPTY_UNSPECIFIED, \
108 .keep_empty = 1, \
109 .default_backend = "merge", \
110 .flags = REBASE_NO_QUIET, \
111 .git_am_opts = ARGV_ARRAY_INIT, \
112 .git_format_patch_opt = STRBUF_INIT \
115 static struct replay_opts get_replay_opts(const struct rebase_options *opts)
117 struct replay_opts replay = REPLAY_OPTS_INIT;
119 replay.action = REPLAY_INTERACTIVE_REBASE;
120 sequencer_init_config(&replay);
122 replay.signoff = opts->signoff;
123 replay.allow_ff = !(opts->flags & REBASE_FORCE);
124 if (opts->allow_rerere_autoupdate)
125 replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
126 replay.allow_empty = 1;
127 replay.allow_empty_message = opts->allow_empty_message;
128 replay.drop_redundant_commits = (opts->empty == EMPTY_DROP);
129 replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP);
130 replay.quiet = !(opts->flags & REBASE_NO_QUIET);
131 replay.verbose = opts->flags & REBASE_VERBOSE;
132 replay.reschedule_failed_exec = opts->reschedule_failed_exec;
133 replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
134 replay.strategy = opts->strategy;
135 if (opts->strategy_opts)
136 parse_strategy_opts(&replay, opts->strategy_opts);
138 if (opts->squash_onto) {
139 oidcpy(&replay.squash_onto, opts->squash_onto);
140 replay.have_squash_onto = 1;
143 return replay;
146 enum action {
147 ACTION_NONE = 0,
148 ACTION_CONTINUE,
149 ACTION_SKIP,
150 ACTION_ABORT,
151 ACTION_QUIT,
152 ACTION_EDIT_TODO,
153 ACTION_SHOW_CURRENT_PATCH,
154 ACTION_SHORTEN_OIDS,
155 ACTION_EXPAND_OIDS,
156 ACTION_CHECK_TODO_LIST,
157 ACTION_REARRANGE_SQUASH,
158 ACTION_ADD_EXEC
161 static const char *action_names[] = { "undefined",
162 "continue",
163 "skip",
164 "abort",
165 "quit",
166 "edit_todo",
167 "show_current_patch" };
169 static int add_exec_commands(struct string_list *commands)
171 const char *todo_file = rebase_path_todo();
172 struct todo_list todo_list = TODO_LIST_INIT;
173 int res;
175 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
176 return error_errno(_("could not read '%s'."), todo_file);
178 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
179 &todo_list)) {
180 todo_list_release(&todo_list);
181 return error(_("unusable todo list: '%s'"), todo_file);
184 todo_list_add_exec_commands(&todo_list, commands);
185 res = todo_list_write_to_file(the_repository, &todo_list,
186 todo_file, NULL, NULL, -1, 0);
187 todo_list_release(&todo_list);
189 if (res)
190 return error_errno(_("could not write '%s'."), todo_file);
191 return 0;
194 static int rearrange_squash_in_todo_file(void)
196 const char *todo_file = rebase_path_todo();
197 struct todo_list todo_list = TODO_LIST_INIT;
198 int res = 0;
200 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
201 return error_errno(_("could not read '%s'."), todo_file);
202 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
203 &todo_list)) {
204 todo_list_release(&todo_list);
205 return error(_("unusable todo list: '%s'"), todo_file);
208 res = todo_list_rearrange_squash(&todo_list);
209 if (!res)
210 res = todo_list_write_to_file(the_repository, &todo_list,
211 todo_file, NULL, NULL, -1, 0);
213 todo_list_release(&todo_list);
215 if (res)
216 return error_errno(_("could not write '%s'."), todo_file);
217 return 0;
220 static int transform_todo_file(unsigned flags)
222 const char *todo_file = rebase_path_todo();
223 struct todo_list todo_list = TODO_LIST_INIT;
224 int res;
226 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
227 return error_errno(_("could not read '%s'."), todo_file);
229 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
230 &todo_list)) {
231 todo_list_release(&todo_list);
232 return error(_("unusable todo list: '%s'"), todo_file);
235 res = todo_list_write_to_file(the_repository, &todo_list, todo_file,
236 NULL, NULL, -1, flags);
237 todo_list_release(&todo_list);
239 if (res)
240 return error_errno(_("could not write '%s'."), todo_file);
241 return 0;
244 static int edit_todo_file(unsigned flags)
246 const char *todo_file = rebase_path_todo();
247 struct todo_list todo_list = TODO_LIST_INIT,
248 new_todo = TODO_LIST_INIT;
249 int res = 0;
251 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
252 return error_errno(_("could not read '%s'."), todo_file);
254 strbuf_stripspace(&todo_list.buf, 1);
255 res = edit_todo_list(the_repository, &todo_list, &new_todo, NULL, NULL, flags);
256 if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
257 NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
258 res = error_errno(_("could not write '%s'"), todo_file);
260 todo_list_release(&todo_list);
261 todo_list_release(&new_todo);
263 return res;
266 static int get_revision_ranges(struct commit *upstream, struct commit *onto,
267 struct object_id *orig_head, const char **head_hash,
268 char **revisions, char **shortrevisions)
270 struct commit *base_rev = upstream ? upstream : onto;
271 const char *shorthead;
273 *head_hash = find_unique_abbrev(orig_head, GIT_MAX_HEXSZ);
274 *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
275 *head_hash);
277 shorthead = find_unique_abbrev(orig_head, DEFAULT_ABBREV);
279 if (upstream) {
280 const char *shortrev;
282 shortrev = find_unique_abbrev(&base_rev->object.oid,
283 DEFAULT_ABBREV);
285 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
286 } else
287 *shortrevisions = xstrdup(shorthead);
289 return 0;
292 static int init_basic_state(struct replay_opts *opts, const char *head_name,
293 struct commit *onto, const char *orig_head)
295 FILE *interactive;
297 if (!is_directory(merge_dir()) && mkdir_in_gitdir(merge_dir()))
298 return error_errno(_("could not create temporary %s"), merge_dir());
300 delete_reflog("REBASE_HEAD");
302 interactive = fopen(path_interactive(), "w");
303 if (!interactive)
304 return error_errno(_("could not mark as interactive"));
305 fclose(interactive);
307 return write_basic_state(opts, head_name, onto, orig_head);
310 static void split_exec_commands(const char *cmd, struct string_list *commands)
312 if (cmd && *cmd) {
313 string_list_split(commands, cmd, '\n', -1);
315 /* rebase.c adds a new line to cmd after every command,
316 * so here the last command is always empty */
317 string_list_remove_empty_items(commands, 0);
321 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
323 int ret;
324 const char *head_hash = NULL;
325 char *revisions = NULL, *shortrevisions = NULL;
326 struct argv_array make_script_args = ARGV_ARRAY_INIT;
327 struct todo_list todo_list = TODO_LIST_INIT;
328 struct replay_opts replay = get_replay_opts(opts);
329 struct string_list commands = STRING_LIST_INIT_DUP;
331 if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head,
332 &head_hash, &revisions, &shortrevisions))
333 return -1;
335 if (init_basic_state(&replay,
336 opts->head_name ? opts->head_name : "detached HEAD",
337 opts->onto, head_hash)) {
338 free(revisions);
339 free(shortrevisions);
341 return -1;
344 if (!opts->upstream && opts->squash_onto)
345 write_file(path_squash_onto(), "%s\n",
346 oid_to_hex(opts->squash_onto));
348 argv_array_pushl(&make_script_args, "", revisions, NULL);
349 if (opts->restrict_revision)
350 argv_array_pushf(&make_script_args, "^%s",
351 oid_to_hex(&opts->restrict_revision->object.oid));
353 ret = sequencer_make_script(the_repository, &todo_list.buf,
354 make_script_args.argc, make_script_args.argv,
355 flags);
357 if (ret)
358 error(_("could not generate todo list"));
359 else {
360 discard_cache();
361 if (todo_list_parse_insn_buffer(the_repository, todo_list.buf.buf,
362 &todo_list))
363 BUG("unusable todo list");
365 split_exec_commands(opts->cmd, &commands);
366 ret = complete_action(the_repository, &replay, flags,
367 shortrevisions, opts->onto_name, opts->onto, head_hash,
368 &commands, opts->autosquash, &todo_list);
371 string_list_clear(&commands, 0);
372 free(revisions);
373 free(shortrevisions);
374 todo_list_release(&todo_list);
375 argv_array_clear(&make_script_args);
377 return ret;
380 static int run_sequencer_rebase(struct rebase_options *opts,
381 enum action command)
383 unsigned flags = 0;
384 int abbreviate_commands = 0, ret = 0;
386 git_config_get_bool("rebase.abbreviatecommands", &abbreviate_commands);
388 flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
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;
394 flags |= opts->reapply_cherry_picks ? TODO_LIST_REAPPLY_CHERRY_PICKS : 0;
396 switch (command) {
397 case ACTION_NONE: {
398 if (!opts->onto && !opts->upstream)
399 die(_("a base commit must be provided with --upstream or --onto"));
401 ret = do_interactive_rebase(opts, flags);
402 break;
404 case ACTION_SKIP: {
405 struct string_list merge_rr = STRING_LIST_INIT_DUP;
407 rerere_clear(the_repository, &merge_rr);
409 /* fallthrough */
410 case ACTION_CONTINUE: {
411 struct replay_opts replay_opts = get_replay_opts(opts);
413 ret = sequencer_continue(the_repository, &replay_opts);
414 break;
416 case ACTION_EDIT_TODO:
417 ret = edit_todo_file(flags);
418 break;
419 case ACTION_SHOW_CURRENT_PATCH: {
420 struct child_process cmd = CHILD_PROCESS_INIT;
422 cmd.git_cmd = 1;
423 argv_array_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
424 ret = run_command(&cmd);
426 break;
428 case ACTION_SHORTEN_OIDS:
429 case ACTION_EXPAND_OIDS:
430 ret = transform_todo_file(flags);
431 break;
432 case ACTION_CHECK_TODO_LIST:
433 ret = check_todo_list_from_file(the_repository);
434 break;
435 case ACTION_REARRANGE_SQUASH:
436 ret = rearrange_squash_in_todo_file();
437 break;
438 case ACTION_ADD_EXEC: {
439 struct string_list commands = STRING_LIST_INIT_DUP;
441 split_exec_commands(opts->cmd, &commands);
442 ret = add_exec_commands(&commands);
443 string_list_clear(&commands, 0);
444 break;
446 default:
447 BUG("invalid command '%d'", command);
450 return ret;
453 static void imply_merge(struct rebase_options *opts, const char *option);
454 static int parse_opt_keep_empty(const struct option *opt, const char *arg,
455 int unset)
457 struct rebase_options *opts = opt->value;
459 BUG_ON_OPT_ARG(arg);
461 imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty");
462 opts->keep_empty = !unset;
463 opts->type = REBASE_MERGE;
464 return 0;
467 static const char * const builtin_rebase_interactive_usage[] = {
468 N_("git rebase--interactive [<options>]"),
469 NULL
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"),
479 REBASE_FORCE),
480 OPT_CALLBACK_F('k', "keep-empty", &options, NULL,
481 N_("keep commits which start empty"),
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"),
486 PARSE_OPT_HIDDEN),
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"),
497 ACTION_CONTINUE),
498 OPT_CMDMODE(0, "skip", &command, N_("skip commit"), ACTION_SKIP),
499 OPT_CMDMODE(0, "edit-todo", &command, N_("edit the todo list"),
500 ACTION_EDIT_TODO),
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,
522 0 },
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")),
538 OPT_END()
541 opts.rebase_cousins = -1;
543 if (argc == 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 "
554 "--rebase-merges"));
556 return !!run_sequencer_rebase(&opts, command);
559 static int is_merge(struct rebase_options *opts)
561 return opts->type == REBASE_MERGE ||
562 opts->type == REBASE_PRESERVE_MERGES;
565 static void imply_merge(struct rebase_options *opts, const char *option)
567 switch (opts->type) {
568 case REBASE_APPLY:
569 die(_("%s requires the merge backend"), option);
570 break;
571 case REBASE_MERGE:
572 case REBASE_PRESERVE_MERGES:
573 break;
574 default:
575 opts->type = REBASE_MERGE; /* implied */
576 break;
580 /* Returns the filename prefixed by the state_dir */
581 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
583 static struct strbuf path = STRBUF_INIT;
584 static size_t prefix_len;
586 if (!prefix_len) {
587 strbuf_addf(&path, "%s/", opts->state_dir);
588 prefix_len = path.len;
591 strbuf_setlen(&path, prefix_len);
592 strbuf_addstr(&path, filename);
593 return path.buf;
596 /* Initialize the rebase options from the state directory. */
597 static int read_basic_state(struct rebase_options *opts)
599 struct strbuf head_name = STRBUF_INIT;
600 struct strbuf buf = STRBUF_INIT;
601 struct object_id oid;
603 if (!read_oneliner(&head_name, state_dir_path("head-name", opts),
604 READ_ONELINER_WARN_MISSING) ||
605 !read_oneliner(&buf, state_dir_path("onto", opts),
606 READ_ONELINER_WARN_MISSING))
607 return -1;
608 opts->head_name = starts_with(head_name.buf, "refs/") ?
609 xstrdup(head_name.buf) : NULL;
610 strbuf_release(&head_name);
611 if (get_oid(buf.buf, &oid))
612 return error(_("could not get 'onto': '%s'"), buf.buf);
613 opts->onto = lookup_commit_or_die(&oid, buf.buf);
616 * We always write to orig-head, but interactive rebase used to write to
617 * head. Fall back to reading from head to cover for the case that the
618 * user upgraded git with an ongoing interactive rebase.
620 strbuf_reset(&buf);
621 if (file_exists(state_dir_path("orig-head", opts))) {
622 if (!read_oneliner(&buf, state_dir_path("orig-head", opts),
623 READ_ONELINER_WARN_MISSING))
624 return -1;
625 } else if (!read_oneliner(&buf, state_dir_path("head", opts),
626 READ_ONELINER_WARN_MISSING))
627 return -1;
628 if (get_oid(buf.buf, &opts->orig_head))
629 return error(_("invalid orig-head: '%s'"), buf.buf);
631 if (file_exists(state_dir_path("quiet", opts)))
632 opts->flags &= ~REBASE_NO_QUIET;
633 else
634 opts->flags |= REBASE_NO_QUIET;
636 if (file_exists(state_dir_path("verbose", opts)))
637 opts->flags |= REBASE_VERBOSE;
639 if (file_exists(state_dir_path("signoff", opts))) {
640 opts->signoff = 1;
641 opts->flags |= REBASE_FORCE;
644 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
645 strbuf_reset(&buf);
646 if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts),
647 READ_ONELINER_WARN_MISSING))
648 return -1;
649 if (!strcmp(buf.buf, "--rerere-autoupdate"))
650 opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
651 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
652 opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE;
653 else
654 warning(_("ignoring invalid allow_rerere_autoupdate: "
655 "'%s'"), buf.buf);
658 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
659 strbuf_reset(&buf);
660 if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts),
661 READ_ONELINER_WARN_MISSING))
662 return -1;
663 free(opts->gpg_sign_opt);
664 opts->gpg_sign_opt = xstrdup(buf.buf);
667 if (file_exists(state_dir_path("strategy", opts))) {
668 strbuf_reset(&buf);
669 if (!read_oneliner(&buf, state_dir_path("strategy", opts),
670 READ_ONELINER_WARN_MISSING))
671 return -1;
672 free(opts->strategy);
673 opts->strategy = xstrdup(buf.buf);
676 if (file_exists(state_dir_path("strategy_opts", opts))) {
677 strbuf_reset(&buf);
678 if (!read_oneliner(&buf, state_dir_path("strategy_opts", opts),
679 READ_ONELINER_WARN_MISSING))
680 return -1;
681 free(opts->strategy_opts);
682 opts->strategy_opts = xstrdup(buf.buf);
685 strbuf_release(&buf);
687 return 0;
690 static int rebase_write_basic_state(struct rebase_options *opts)
692 write_file(state_dir_path("head-name", opts), "%s",
693 opts->head_name ? opts->head_name : "detached HEAD");
694 write_file(state_dir_path("onto", opts), "%s",
695 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
696 write_file(state_dir_path("orig-head", opts), "%s",
697 oid_to_hex(&opts->orig_head));
698 if (!(opts->flags & REBASE_NO_QUIET))
699 write_file(state_dir_path("quiet", opts), "%s", "");
700 if (opts->flags & REBASE_VERBOSE)
701 write_file(state_dir_path("verbose", opts), "%s", "");
702 if (opts->strategy)
703 write_file(state_dir_path("strategy", opts), "%s",
704 opts->strategy);
705 if (opts->strategy_opts)
706 write_file(state_dir_path("strategy_opts", opts), "%s",
707 opts->strategy_opts);
708 if (opts->allow_rerere_autoupdate > 0)
709 write_file(state_dir_path("allow_rerere_autoupdate", opts),
710 "-%s-rerere-autoupdate",
711 opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
712 "" : "-no");
713 if (opts->gpg_sign_opt)
714 write_file(state_dir_path("gpg_sign_opt", opts), "%s",
715 opts->gpg_sign_opt);
716 if (opts->signoff)
717 write_file(state_dir_path("signoff", opts), "--signoff");
719 return 0;
722 static int finish_rebase(struct rebase_options *opts)
724 struct strbuf dir = STRBUF_INIT;
725 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
726 int ret = 0;
728 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
729 apply_autostash(state_dir_path("autostash", opts));
730 close_object_store(the_repository->objects);
732 * We ignore errors in 'gc --auto', since the
733 * user should see them.
735 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
736 if (opts->type == REBASE_MERGE) {
737 struct replay_opts replay = REPLAY_OPTS_INIT;
739 replay.action = REPLAY_INTERACTIVE_REBASE;
740 ret = sequencer_remove_state(&replay);
741 } else {
742 strbuf_addstr(&dir, opts->state_dir);
743 if (remove_dir_recursively(&dir, 0))
744 ret = error(_("could not remove '%s'"),
745 opts->state_dir);
746 strbuf_release(&dir);
749 return ret;
752 static struct commit *peel_committish(const char *name)
754 struct object *obj;
755 struct object_id oid;
757 if (get_oid(name, &oid))
758 return NULL;
759 obj = parse_object(the_repository, &oid);
760 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
763 static void add_var(struct strbuf *buf, const char *name, const char *value)
765 if (!value)
766 strbuf_addf(buf, "unset %s; ", name);
767 else {
768 strbuf_addf(buf, "%s=", name);
769 sq_quote_buf(buf, value);
770 strbuf_addstr(buf, "; ");
774 static int move_to_original_branch(struct rebase_options *opts)
776 struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
777 int ret;
779 if (!opts->head_name)
780 return 0; /* nothing to move back to */
782 if (!opts->onto)
783 BUG("move_to_original_branch without onto");
785 strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
786 opts->head_name, oid_to_hex(&opts->onto->object.oid));
787 strbuf_addf(&head_reflog, "rebase finished: returning to %s",
788 opts->head_name);
789 ret = reset_head(the_repository, NULL, "", opts->head_name,
790 RESET_HEAD_REFS_ONLY,
791 orig_head_reflog.buf, head_reflog.buf,
792 DEFAULT_REFLOG_ACTION);
794 strbuf_release(&orig_head_reflog);
795 strbuf_release(&head_reflog);
796 return ret;
799 static const char *resolvemsg =
800 N_("Resolve all conflicts manually, mark them as resolved with\n"
801 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
802 "You can instead skip this commit: run \"git rebase --skip\".\n"
803 "To abort and get back to the state before \"git rebase\", run "
804 "\"git rebase --abort\".");
806 static int run_am(struct rebase_options *opts)
808 struct child_process am = CHILD_PROCESS_INIT;
809 struct child_process format_patch = CHILD_PROCESS_INIT;
810 struct strbuf revisions = STRBUF_INIT;
811 int status;
812 char *rebased_patches;
814 am.git_cmd = 1;
815 argv_array_push(&am.args, "am");
817 if (opts->action && !strcmp("continue", opts->action)) {
818 argv_array_push(&am.args, "--resolved");
819 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
820 if (opts->gpg_sign_opt)
821 argv_array_push(&am.args, opts->gpg_sign_opt);
822 status = run_command(&am);
823 if (status)
824 return status;
826 return move_to_original_branch(opts);
828 if (opts->action && !strcmp("skip", opts->action)) {
829 argv_array_push(&am.args, "--skip");
830 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
831 status = run_command(&am);
832 if (status)
833 return status;
835 return move_to_original_branch(opts);
837 if (opts->action && !strcmp("show-current-patch", opts->action)) {
838 argv_array_push(&am.args, "--show-current-patch");
839 return run_command(&am);
842 strbuf_addf(&revisions, "%s...%s",
843 oid_to_hex(opts->root ?
844 /* this is now equivalent to !opts->upstream */
845 &opts->onto->object.oid :
846 &opts->upstream->object.oid),
847 oid_to_hex(&opts->orig_head));
849 rebased_patches = xstrdup(git_path("rebased-patches"));
850 format_patch.out = open(rebased_patches,
851 O_WRONLY | O_CREAT | O_TRUNC, 0666);
852 if (format_patch.out < 0) {
853 status = error_errno(_("could not open '%s' for writing"),
854 rebased_patches);
855 free(rebased_patches);
856 argv_array_clear(&am.args);
857 return status;
860 format_patch.git_cmd = 1;
861 argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
862 "--full-index", "--cherry-pick", "--right-only",
863 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
864 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
865 "--no-base", NULL);
866 if (opts->git_format_patch_opt.len)
867 argv_array_split(&format_patch.args,
868 opts->git_format_patch_opt.buf);
869 argv_array_push(&format_patch.args, revisions.buf);
870 if (opts->restrict_revision)
871 argv_array_pushf(&format_patch.args, "^%s",
872 oid_to_hex(&opts->restrict_revision->object.oid));
874 status = run_command(&format_patch);
875 if (status) {
876 unlink(rebased_patches);
877 free(rebased_patches);
878 argv_array_clear(&am.args);
880 reset_head(the_repository, &opts->orig_head, "checkout",
881 opts->head_name, 0,
882 "HEAD", NULL, DEFAULT_REFLOG_ACTION);
883 error(_("\ngit encountered an error while preparing the "
884 "patches to replay\n"
885 "these revisions:\n"
886 "\n %s\n\n"
887 "As a result, git cannot rebase them."),
888 opts->revisions);
890 strbuf_release(&revisions);
891 return status;
893 strbuf_release(&revisions);
895 am.in = open(rebased_patches, O_RDONLY);
896 if (am.in < 0) {
897 status = error_errno(_("could not open '%s' for reading"),
898 rebased_patches);
899 free(rebased_patches);
900 argv_array_clear(&am.args);
901 return status;
904 argv_array_pushv(&am.args, opts->git_am_opts.argv);
905 argv_array_push(&am.args, "--rebasing");
906 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
907 argv_array_push(&am.args, "--patch-format=mboxrd");
908 if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
909 argv_array_push(&am.args, "--rerere-autoupdate");
910 else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
911 argv_array_push(&am.args, "--no-rerere-autoupdate");
912 if (opts->gpg_sign_opt)
913 argv_array_push(&am.args, opts->gpg_sign_opt);
914 status = run_command(&am);
915 unlink(rebased_patches);
916 free(rebased_patches);
918 if (!status) {
919 return move_to_original_branch(opts);
922 if (is_directory(opts->state_dir))
923 rebase_write_basic_state(opts);
925 return status;
928 static int run_specific_rebase(struct rebase_options *opts, enum action action)
930 const char *argv[] = { NULL, NULL };
931 struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
932 int status;
933 const char *backend, *backend_func;
935 if (opts->type == REBASE_MERGE) {
936 /* Run sequencer-based rebase */
937 setenv("GIT_CHERRY_PICK_HELP", resolvemsg, 1);
938 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
939 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
940 opts->autosquash = 0;
942 if (opts->gpg_sign_opt) {
943 /* remove the leading "-S" */
944 char *tmp = xstrdup(opts->gpg_sign_opt + 2);
945 free(opts->gpg_sign_opt);
946 opts->gpg_sign_opt = tmp;
949 status = run_sequencer_rebase(opts, action);
950 goto finished_rebase;
953 if (opts->type == REBASE_APPLY) {
954 status = run_am(opts);
955 goto finished_rebase;
958 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
959 add_var(&script_snippet, "state_dir", opts->state_dir);
961 add_var(&script_snippet, "upstream_name", opts->upstream_name);
962 add_var(&script_snippet, "upstream", opts->upstream ?
963 oid_to_hex(&opts->upstream->object.oid) : NULL);
964 add_var(&script_snippet, "head_name",
965 opts->head_name ? opts->head_name : "detached HEAD");
966 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
967 add_var(&script_snippet, "onto", opts->onto ?
968 oid_to_hex(&opts->onto->object.oid) : NULL);
969 add_var(&script_snippet, "onto_name", opts->onto_name);
970 add_var(&script_snippet, "revisions", opts->revisions);
971 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
972 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
973 sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
974 add_var(&script_snippet, "git_am_opt", buf.buf);
975 strbuf_release(&buf);
976 add_var(&script_snippet, "verbose",
977 opts->flags & REBASE_VERBOSE ? "t" : "");
978 add_var(&script_snippet, "diffstat",
979 opts->flags & REBASE_DIFFSTAT ? "t" : "");
980 add_var(&script_snippet, "force_rebase",
981 opts->flags & REBASE_FORCE ? "t" : "");
982 if (opts->switch_to)
983 add_var(&script_snippet, "switch_to", opts->switch_to);
984 add_var(&script_snippet, "action", opts->action ? opts->action : "");
985 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
986 add_var(&script_snippet, "allow_rerere_autoupdate",
987 opts->allow_rerere_autoupdate ?
988 opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
989 "--rerere-autoupdate" : "--no-rerere-autoupdate" : "");
990 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
991 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
992 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
993 add_var(&script_snippet, "cmd", opts->cmd);
994 add_var(&script_snippet, "allow_empty_message",
995 opts->allow_empty_message ? "--allow-empty-message" : "");
996 add_var(&script_snippet, "rebase_merges",
997 opts->rebase_merges ? "t" : "");
998 add_var(&script_snippet, "rebase_cousins",
999 opts->rebase_cousins ? "t" : "");
1000 add_var(&script_snippet, "strategy", opts->strategy);
1001 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
1002 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
1003 add_var(&script_snippet, "squash_onto",
1004 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
1005 add_var(&script_snippet, "git_format_patch_opt",
1006 opts->git_format_patch_opt.buf);
1008 if (is_merge(opts) &&
1009 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
1010 strbuf_addstr(&script_snippet,
1011 "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; ");
1012 opts->autosquash = 0;
1015 switch (opts->type) {
1016 case REBASE_PRESERVE_MERGES:
1017 backend = "git-rebase--preserve-merges";
1018 backend_func = "git_rebase__preserve_merges";
1019 break;
1020 default:
1021 BUG("Unhandled rebase type %d", opts->type);
1022 break;
1025 strbuf_addf(&script_snippet,
1026 ". git-sh-setup && . %s && %s", backend, backend_func);
1027 argv[0] = script_snippet.buf;
1029 status = run_command_v_opt(argv, RUN_USING_SHELL);
1030 finished_rebase:
1031 if (opts->dont_finish_rebase)
1032 ; /* do nothing */
1033 else if (opts->type == REBASE_MERGE)
1034 ; /* merge backend cleans up after itself */
1035 else if (status == 0) {
1036 if (!file_exists(state_dir_path("stopped-sha", opts)))
1037 finish_rebase(opts);
1038 } else if (status == 2) {
1039 struct strbuf dir = STRBUF_INIT;
1041 apply_autostash(state_dir_path("autostash", opts));
1042 strbuf_addstr(&dir, opts->state_dir);
1043 remove_dir_recursively(&dir, 0);
1044 strbuf_release(&dir);
1045 die("Nothing to do");
1048 strbuf_release(&script_snippet);
1050 return status ? -1 : 0;
1053 static int rebase_config(const char *var, const char *value, void *data)
1055 struct rebase_options *opts = data;
1057 if (!strcmp(var, "rebase.stat")) {
1058 if (git_config_bool(var, value))
1059 opts->flags |= REBASE_DIFFSTAT;
1060 else
1061 opts->flags &= ~REBASE_DIFFSTAT;
1062 return 0;
1065 if (!strcmp(var, "rebase.autosquash")) {
1066 opts->autosquash = git_config_bool(var, value);
1067 return 0;
1070 if (!strcmp(var, "commit.gpgsign")) {
1071 free(opts->gpg_sign_opt);
1072 opts->gpg_sign_opt = git_config_bool(var, value) ?
1073 xstrdup("-S") : NULL;
1074 return 0;
1077 if (!strcmp(var, "rebase.autostash")) {
1078 opts->autostash = git_config_bool(var, value);
1079 return 0;
1082 if (!strcmp(var, "rebase.reschedulefailedexec")) {
1083 opts->reschedule_failed_exec = git_config_bool(var, value);
1084 return 0;
1087 if (!strcmp(var, "rebase.usebuiltin")) {
1088 opts->use_legacy_rebase = !git_config_bool(var, value);
1089 return 0;
1092 if (!strcmp(var, "rebase.backend")) {
1093 return git_config_string(&opts->default_backend, var, value);
1096 return git_default_config(var, value, data);
1100 * Determines whether the commits in from..to are linear, i.e. contain
1101 * no merge commits. This function *expects* `from` to be an ancestor of
1102 * `to`.
1104 static int is_linear_history(struct commit *from, struct commit *to)
1106 while (to && to != from) {
1107 parse_commit(to);
1108 if (!to->parents)
1109 return 1;
1110 if (to->parents->next)
1111 return 0;
1112 to = to->parents->item;
1114 return 1;
1117 static int can_fast_forward(struct commit *onto, struct commit *upstream,
1118 struct commit *restrict_revision,
1119 struct object_id *head_oid, struct object_id *merge_base)
1121 struct commit *head = lookup_commit(the_repository, head_oid);
1122 struct commit_list *merge_bases = NULL;
1123 int res = 0;
1125 if (!head)
1126 goto done;
1128 merge_bases = get_merge_bases(onto, head);
1129 if (!merge_bases || merge_bases->next) {
1130 oidcpy(merge_base, &null_oid);
1131 goto done;
1134 oidcpy(merge_base, &merge_bases->item->object.oid);
1135 if (!oideq(merge_base, &onto->object.oid))
1136 goto done;
1138 if (restrict_revision && !oideq(&restrict_revision->object.oid, merge_base))
1139 goto done;
1141 if (!upstream)
1142 goto done;
1144 free_commit_list(merge_bases);
1145 merge_bases = get_merge_bases(upstream, head);
1146 if (!merge_bases || merge_bases->next)
1147 goto done;
1149 if (!oideq(&onto->object.oid, &merge_bases->item->object.oid))
1150 goto done;
1152 res = 1;
1154 done:
1155 free_commit_list(merge_bases);
1156 return res && is_linear_history(onto, head);
1159 static int parse_opt_am(const struct option *opt, const char *arg, int unset)
1161 struct rebase_options *opts = opt->value;
1163 BUG_ON_OPT_NEG(unset);
1164 BUG_ON_OPT_ARG(arg);
1166 opts->type = REBASE_APPLY;
1168 return 0;
1171 /* -i followed by -m is still -i */
1172 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
1174 struct rebase_options *opts = opt->value;
1176 BUG_ON_OPT_NEG(unset);
1177 BUG_ON_OPT_ARG(arg);
1179 if (!is_merge(opts))
1180 opts->type = REBASE_MERGE;
1182 return 0;
1185 /* -i followed by -p is still explicitly interactive, but -p alone is not */
1186 static int parse_opt_interactive(const struct option *opt, const char *arg,
1187 int unset)
1189 struct rebase_options *opts = opt->value;
1191 BUG_ON_OPT_NEG(unset);
1192 BUG_ON_OPT_ARG(arg);
1194 opts->type = REBASE_MERGE;
1195 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
1197 return 0;
1200 static enum empty_type parse_empty_value(const char *value)
1202 if (!strcasecmp(value, "drop"))
1203 return EMPTY_DROP;
1204 else if (!strcasecmp(value, "keep"))
1205 return EMPTY_KEEP;
1206 else if (!strcasecmp(value, "ask"))
1207 return EMPTY_ASK;
1209 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"ask\"."), value);
1212 static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
1214 struct rebase_options *options = opt->value;
1215 enum empty_type value = parse_empty_value(arg);
1217 BUG_ON_OPT_NEG(unset);
1219 options->empty = value;
1220 return 0;
1223 static void NORETURN error_on_missing_default_upstream(void)
1225 struct branch *current_branch = branch_get(NULL);
1227 printf(_("%s\n"
1228 "Please specify which branch you want to rebase against.\n"
1229 "See git-rebase(1) for details.\n"
1230 "\n"
1231 " git rebase '<branch>'\n"
1232 "\n"),
1233 current_branch ? _("There is no tracking information for "
1234 "the current branch.") :
1235 _("You are not currently on a branch."));
1237 if (current_branch) {
1238 const char *remote = current_branch->remote_name;
1240 if (!remote)
1241 remote = _("<remote>");
1243 printf(_("If you wish to set tracking information for this "
1244 "branch you can do so with:\n"
1245 "\n"
1246 " git branch --set-upstream-to=%s/<branch> %s\n"
1247 "\n"),
1248 remote, current_branch->name);
1250 exit(1);
1253 static void set_reflog_action(struct rebase_options *options)
1255 const char *env;
1256 struct strbuf buf = STRBUF_INIT;
1258 if (!is_merge(options))
1259 return;
1261 env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1262 if (env && strcmp("rebase", env))
1263 return; /* only override it if it is "rebase" */
1265 strbuf_addf(&buf, "rebase (%s)", options->action);
1266 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
1267 strbuf_release(&buf);
1270 static int check_exec_cmd(const char *cmd)
1272 if (strchr(cmd, '\n'))
1273 return error(_("exec commands cannot contain newlines"));
1275 /* Does the command consist purely of whitespace? */
1276 if (!cmd[strspn(cmd, " \t\r\f\v")])
1277 return error(_("empty exec command"));
1279 return 0;
1282 int cmd_rebase(int argc, const char **argv, const char *prefix)
1284 struct rebase_options options = REBASE_OPTIONS_INIT;
1285 const char *branch_name;
1286 int ret, flags, total_argc, in_progress = 0;
1287 int keep_base = 0;
1288 int ok_to_skip_pre_rebase = 0;
1289 struct strbuf msg = STRBUF_INIT;
1290 struct strbuf revisions = STRBUF_INIT;
1291 struct strbuf buf = STRBUF_INIT;
1292 struct object_id merge_base;
1293 enum action action = ACTION_NONE;
1294 const char *gpg_sign = NULL;
1295 struct string_list exec = STRING_LIST_INIT_NODUP;
1296 const char *rebase_merges = NULL;
1297 int fork_point = -1;
1298 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1299 struct object_id squash_onto;
1300 char *squash_onto_name = NULL;
1301 int reschedule_failed_exec = -1;
1302 int allow_preemptive_ff = 1;
1303 struct option builtin_rebase_options[] = {
1304 OPT_STRING(0, "onto", &options.onto_name,
1305 N_("revision"),
1306 N_("rebase onto given branch instead of upstream")),
1307 OPT_BOOL(0, "keep-base", &keep_base,
1308 N_("use the merge-base of upstream and branch as the current base")),
1309 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1310 N_("allow pre-rebase hook to run")),
1311 OPT_NEGBIT('q', "quiet", &options.flags,
1312 N_("be quiet. implies --no-stat"),
1313 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1314 OPT_BIT('v', "verbose", &options.flags,
1315 N_("display a diffstat of what changed upstream"),
1316 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1317 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1318 N_("do not show diffstat of what changed upstream"),
1319 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1320 OPT_BOOL(0, "signoff", &options.signoff,
1321 N_("add a Signed-off-by: line to each commit")),
1322 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
1323 NULL, N_("passed to 'git am'"),
1324 PARSE_OPT_NOARG),
1325 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1326 &options.git_am_opts, NULL,
1327 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1328 OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1329 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1330 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1331 N_("passed to 'git apply'"), 0),
1332 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1333 N_("action"), N_("passed to 'git apply'"), 0),
1334 OPT_BIT('f', "force-rebase", &options.flags,
1335 N_("cherry-pick all commits, even if unchanged"),
1336 REBASE_FORCE),
1337 OPT_BIT(0, "no-ff", &options.flags,
1338 N_("cherry-pick all commits, even if unchanged"),
1339 REBASE_FORCE),
1340 OPT_CMDMODE(0, "continue", &action, N_("continue"),
1341 ACTION_CONTINUE),
1342 OPT_CMDMODE(0, "skip", &action,
1343 N_("skip current patch and continue"), ACTION_SKIP),
1344 OPT_CMDMODE(0, "abort", &action,
1345 N_("abort and check out the original branch"),
1346 ACTION_ABORT),
1347 OPT_CMDMODE(0, "quit", &action,
1348 N_("abort but keep HEAD where it is"), ACTION_QUIT),
1349 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1350 "during an interactive rebase"), ACTION_EDIT_TODO),
1351 OPT_CMDMODE(0, "show-current-patch", &action,
1352 N_("show the patch file being applied or merged"),
1353 ACTION_SHOW_CURRENT_PATCH),
1354 OPT_CALLBACK_F(0, "apply", &options, NULL,
1355 N_("use apply strategies to rebase"),
1356 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1357 parse_opt_am),
1358 OPT_CALLBACK_F('m', "merge", &options, NULL,
1359 N_("use merging strategies to rebase"),
1360 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1361 parse_opt_merge),
1362 OPT_CALLBACK_F('i', "interactive", &options, NULL,
1363 N_("let the user edit the list of commits to rebase"),
1364 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1365 parse_opt_interactive),
1366 OPT_SET_INT_F('p', "preserve-merges", &options.type,
1367 N_("(DEPRECATED) try to recreate merges instead of "
1368 "ignoring them"),
1369 REBASE_PRESERVE_MERGES, PARSE_OPT_HIDDEN),
1370 OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
1371 OPT_CALLBACK_F(0, "empty", &options, "{drop,keep,ask}",
1372 N_("how to handle commits that become empty"),
1373 PARSE_OPT_NONEG, parse_opt_empty),
1374 OPT_CALLBACK_F('k', "keep-empty", &options, NULL,
1375 N_("keep commits which start empty"),
1376 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
1377 parse_opt_keep_empty),
1378 OPT_BOOL(0, "autosquash", &options.autosquash,
1379 N_("move commits that begin with "
1380 "squash!/fixup! under -i")),
1381 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1382 N_("GPG-sign commits"),
1383 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1384 OPT_AUTOSTASH(&options.autostash),
1385 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1386 N_("add exec lines after each commit of the "
1387 "editable list")),
1388 OPT_BOOL_F(0, "allow-empty-message",
1389 &options.allow_empty_message,
1390 N_("allow rebasing commits with empty messages"),
1391 PARSE_OPT_HIDDEN),
1392 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1393 N_("mode"),
1394 N_("try to rebase merges instead of skipping them"),
1395 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
1396 OPT_BOOL(0, "fork-point", &fork_point,
1397 N_("use 'merge-base --fork-point' to refine upstream")),
1398 OPT_STRING('s', "strategy", &options.strategy,
1399 N_("strategy"), N_("use the given merge strategy")),
1400 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1401 N_("option"),
1402 N_("pass the argument through to the merge "
1403 "strategy")),
1404 OPT_BOOL(0, "root", &options.root,
1405 N_("rebase all reachable commits up to the root(s)")),
1406 OPT_BOOL(0, "reschedule-failed-exec",
1407 &reschedule_failed_exec,
1408 N_("automatically re-schedule any `exec` that fails")),
1409 OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks,
1410 N_("apply all changes, even those already present upstream")),
1411 OPT_END(),
1413 int i;
1415 if (argc == 2 && !strcmp(argv[1], "-h"))
1416 usage_with_options(builtin_rebase_usage,
1417 builtin_rebase_options);
1419 options.allow_empty_message = 1;
1420 git_config(rebase_config, &options);
1421 /* options.gpg_sign_opt will be either "-S" or NULL */
1422 gpg_sign = options.gpg_sign_opt ? "" : NULL;
1423 FREE_AND_NULL(options.gpg_sign_opt);
1425 if (options.use_legacy_rebase ||
1426 !git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1))
1427 warning(_("the rebase.useBuiltin support has been removed!\n"
1428 "See its entry in 'git help config' for details."));
1430 strbuf_reset(&buf);
1431 strbuf_addf(&buf, "%s/applying", apply_dir());
1432 if(file_exists(buf.buf))
1433 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1435 if (is_directory(apply_dir())) {
1436 options.type = REBASE_APPLY;
1437 options.state_dir = apply_dir();
1438 } else if (is_directory(merge_dir())) {
1439 strbuf_reset(&buf);
1440 strbuf_addf(&buf, "%s/rewritten", merge_dir());
1441 if (is_directory(buf.buf)) {
1442 options.type = REBASE_PRESERVE_MERGES;
1443 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1444 } else {
1445 strbuf_reset(&buf);
1446 strbuf_addf(&buf, "%s/interactive", merge_dir());
1447 if(file_exists(buf.buf)) {
1448 options.type = REBASE_MERGE;
1449 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1450 } else
1451 options.type = REBASE_MERGE;
1453 options.state_dir = merge_dir();
1456 if (options.type != REBASE_UNSPECIFIED)
1457 in_progress = 1;
1459 total_argc = argc;
1460 argc = parse_options(argc, argv, prefix,
1461 builtin_rebase_options,
1462 builtin_rebase_usage, 0);
1464 if (action != ACTION_NONE && total_argc != 2) {
1465 usage_with_options(builtin_rebase_usage,
1466 builtin_rebase_options);
1469 if (argc > 2)
1470 usage_with_options(builtin_rebase_usage,
1471 builtin_rebase_options);
1473 if (options.type == REBASE_PRESERVE_MERGES)
1474 warning(_("git rebase --preserve-merges is deprecated. "
1475 "Use --rebase-merges instead."));
1477 if (keep_base) {
1478 if (options.onto_name)
1479 die(_("cannot combine '--keep-base' with '--onto'"));
1480 if (options.root)
1481 die(_("cannot combine '--keep-base' with '--root'"));
1484 if (options.root && fork_point > 0)
1485 die(_("cannot combine '--root' with '--fork-point'"));
1487 if (action != ACTION_NONE && !in_progress)
1488 die(_("No rebase in progress?"));
1489 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1491 if (action == ACTION_EDIT_TODO && !is_merge(&options))
1492 die(_("The --edit-todo action can only be used during "
1493 "interactive rebase."));
1495 if (trace2_is_enabled()) {
1496 if (is_merge(&options))
1497 trace2_cmd_mode("interactive");
1498 else if (exec.nr)
1499 trace2_cmd_mode("interactive-exec");
1500 else
1501 trace2_cmd_mode(action_names[action]);
1504 switch (action) {
1505 case ACTION_CONTINUE: {
1506 struct object_id head;
1507 struct lock_file lock_file = LOCK_INIT;
1508 int fd;
1510 options.action = "continue";
1511 set_reflog_action(&options);
1513 /* Sanity check */
1514 if (get_oid("HEAD", &head))
1515 die(_("Cannot read HEAD"));
1517 fd = hold_locked_index(&lock_file, 0);
1518 if (repo_read_index(the_repository) < 0)
1519 die(_("could not read index"));
1520 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1521 NULL);
1522 if (0 <= fd)
1523 repo_update_index_if_able(the_repository, &lock_file);
1524 rollback_lock_file(&lock_file);
1526 if (has_unstaged_changes(the_repository, 1)) {
1527 puts(_("You must edit all merge conflicts and then\n"
1528 "mark them as resolved using git add"));
1529 exit(1);
1531 if (read_basic_state(&options))
1532 exit(1);
1533 goto run_rebase;
1535 case ACTION_SKIP: {
1536 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1538 options.action = "skip";
1539 set_reflog_action(&options);
1541 rerere_clear(the_repository, &merge_rr);
1542 string_list_clear(&merge_rr, 1);
1544 if (reset_head(the_repository, NULL, "reset", NULL, RESET_HEAD_HARD,
1545 NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1546 die(_("could not discard worktree changes"));
1547 remove_branch_state(the_repository, 0);
1548 if (read_basic_state(&options))
1549 exit(1);
1550 goto run_rebase;
1552 case ACTION_ABORT: {
1553 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1554 options.action = "abort";
1555 set_reflog_action(&options);
1557 rerere_clear(the_repository, &merge_rr);
1558 string_list_clear(&merge_rr, 1);
1560 if (read_basic_state(&options))
1561 exit(1);
1562 if (reset_head(the_repository, &options.orig_head, "reset",
1563 options.head_name, RESET_HEAD_HARD,
1564 NULL, NULL, DEFAULT_REFLOG_ACTION) < 0)
1565 die(_("could not move back to %s"),
1566 oid_to_hex(&options.orig_head));
1567 remove_branch_state(the_repository, 0);
1568 ret = !!finish_rebase(&options);
1569 goto cleanup;
1571 case ACTION_QUIT: {
1572 save_autostash(state_dir_path("autostash", &options));
1573 if (options.type == REBASE_MERGE) {
1574 struct replay_opts replay = REPLAY_OPTS_INIT;
1576 replay.action = REPLAY_INTERACTIVE_REBASE;
1577 ret = !!sequencer_remove_state(&replay);
1578 } else {
1579 strbuf_reset(&buf);
1580 strbuf_addstr(&buf, options.state_dir);
1581 ret = !!remove_dir_recursively(&buf, 0);
1582 if (ret)
1583 error(_("could not remove '%s'"),
1584 options.state_dir);
1586 goto cleanup;
1588 case ACTION_EDIT_TODO:
1589 options.action = "edit-todo";
1590 options.dont_finish_rebase = 1;
1591 goto run_rebase;
1592 case ACTION_SHOW_CURRENT_PATCH:
1593 options.action = "show-current-patch";
1594 options.dont_finish_rebase = 1;
1595 goto run_rebase;
1596 case ACTION_NONE:
1597 break;
1598 default:
1599 BUG("action: %d", action);
1602 /* Make sure no rebase is in progress */
1603 if (in_progress) {
1604 const char *last_slash = strrchr(options.state_dir, '/');
1605 const char *state_dir_base =
1606 last_slash ? last_slash + 1 : options.state_dir;
1607 const char *cmd_live_rebase =
1608 "git rebase (--continue | --abort | --skip)";
1609 strbuf_reset(&buf);
1610 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1611 die(_("It seems that there is already a %s directory, and\n"
1612 "I wonder if you are in the middle of another rebase. "
1613 "If that is the\n"
1614 "case, please try\n\t%s\n"
1615 "If that is not the case, please\n\t%s\n"
1616 "and run me again. I am stopping in case you still "
1617 "have something\n"
1618 "valuable there.\n"),
1619 state_dir_base, cmd_live_rebase, buf.buf);
1622 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
1623 (action != ACTION_NONE) ||
1624 (exec.nr > 0) ||
1625 options.autosquash) {
1626 allow_preemptive_ff = 0;
1629 for (i = 0; i < options.git_am_opts.argc; i++) {
1630 const char *option = options.git_am_opts.argv[i], *p;
1631 if (!strcmp(option, "--committer-date-is-author-date") ||
1632 !strcmp(option, "--ignore-date") ||
1633 !strcmp(option, "--whitespace=fix") ||
1634 !strcmp(option, "--whitespace=strip"))
1635 allow_preemptive_ff = 0;
1636 else if (skip_prefix(option, "-C", &p)) {
1637 while (*p)
1638 if (!isdigit(*(p++)))
1639 die(_("switch `C' expects a "
1640 "numerical value"));
1641 } else if (skip_prefix(option, "--whitespace=", &p)) {
1642 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1643 strcmp(p, "error") && strcmp(p, "error-all"))
1644 die("Invalid whitespace option: '%s'", p);
1648 for (i = 0; i < exec.nr; i++)
1649 if (check_exec_cmd(exec.items[i].string))
1650 exit(1);
1652 if (!(options.flags & REBASE_NO_QUIET))
1653 argv_array_push(&options.git_am_opts, "-q");
1655 if (options.empty != EMPTY_UNSPECIFIED)
1656 imply_merge(&options, "--empty");
1658 if (options.reapply_cherry_picks)
1659 imply_merge(&options, "--reapply-cherry-picks");
1661 if (gpg_sign)
1662 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1664 if (exec.nr) {
1665 int i;
1667 imply_merge(&options, "--exec");
1669 strbuf_reset(&buf);
1670 for (i = 0; i < exec.nr; i++)
1671 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1672 options.cmd = xstrdup(buf.buf);
1675 if (rebase_merges) {
1676 if (!*rebase_merges)
1677 ; /* default mode; do nothing */
1678 else if (!strcmp("rebase-cousins", rebase_merges))
1679 options.rebase_cousins = 1;
1680 else if (strcmp("no-rebase-cousins", rebase_merges))
1681 die(_("Unknown mode: %s"), rebase_merges);
1682 options.rebase_merges = 1;
1683 imply_merge(&options, "--rebase-merges");
1686 if (strategy_options.nr) {
1687 int i;
1689 if (!options.strategy)
1690 options.strategy = "recursive";
1692 strbuf_reset(&buf);
1693 for (i = 0; i < strategy_options.nr; i++)
1694 strbuf_addf(&buf, " --%s",
1695 strategy_options.items[i].string);
1696 options.strategy_opts = xstrdup(buf.buf);
1699 if (options.strategy) {
1700 options.strategy = xstrdup(options.strategy);
1701 switch (options.type) {
1702 case REBASE_APPLY:
1703 die(_("--strategy requires --merge or --interactive"));
1704 case REBASE_MERGE:
1705 case REBASE_PRESERVE_MERGES:
1706 /* compatible */
1707 break;
1708 case REBASE_UNSPECIFIED:
1709 options.type = REBASE_MERGE;
1710 break;
1711 default:
1712 BUG("unhandled rebase type (%d)", options.type);
1716 if (options.type == REBASE_MERGE)
1717 imply_merge(&options, "--merge");
1719 if (options.root && !options.onto_name)
1720 imply_merge(&options, "--root without --onto");
1722 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1723 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1725 if (options.git_am_opts.argc || options.type == REBASE_APPLY) {
1726 /* all am options except -q are compatible only with --apply */
1727 for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1728 if (strcmp(options.git_am_opts.argv[i], "-q"))
1729 break;
1731 if (i >= 0) {
1732 if (is_merge(&options))
1733 die(_("cannot combine apply options with "
1734 "merge options"));
1735 else
1736 options.type = REBASE_APPLY;
1740 if (options.type == REBASE_UNSPECIFIED) {
1741 if (!strcmp(options.default_backend, "merge"))
1742 imply_merge(&options, "--merge");
1743 else if (!strcmp(options.default_backend, "apply"))
1744 options.type = REBASE_APPLY;
1745 else
1746 die(_("Unknown rebase backend: %s"),
1747 options.default_backend);
1750 switch (options.type) {
1751 case REBASE_MERGE:
1752 case REBASE_PRESERVE_MERGES:
1753 options.state_dir = merge_dir();
1754 break;
1755 case REBASE_APPLY:
1756 options.state_dir = apply_dir();
1757 break;
1758 default:
1759 BUG("options.type was just set above; should be unreachable.");
1762 if (options.empty == EMPTY_UNSPECIFIED) {
1763 if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
1764 options.empty = EMPTY_ASK;
1765 else if (exec.nr > 0)
1766 options.empty = EMPTY_KEEP;
1767 else
1768 options.empty = EMPTY_DROP;
1770 if (reschedule_failed_exec > 0 && !is_merge(&options))
1771 die(_("--reschedule-failed-exec requires "
1772 "--exec or --interactive"));
1773 if (reschedule_failed_exec >= 0)
1774 options.reschedule_failed_exec = reschedule_failed_exec;
1776 if (options.signoff) {
1777 if (options.type == REBASE_PRESERVE_MERGES)
1778 die("cannot combine '--signoff' with "
1779 "'--preserve-merges'");
1780 argv_array_push(&options.git_am_opts, "--signoff");
1781 options.flags |= REBASE_FORCE;
1784 if (options.type == REBASE_PRESERVE_MERGES) {
1786 * Note: incompatibility with --signoff handled in signoff block above
1787 * Note: incompatibility with --interactive is just a strong warning;
1788 * git-rebase.txt caveats with "unless you know what you are doing"
1790 if (options.rebase_merges)
1791 die(_("cannot combine '--preserve-merges' with "
1792 "'--rebase-merges'"));
1794 if (options.reschedule_failed_exec)
1795 die(_("error: cannot combine '--preserve-merges' with "
1796 "'--reschedule-failed-exec'"));
1799 if (!options.root) {
1800 if (argc < 1) {
1801 struct branch *branch;
1803 branch = branch_get(NULL);
1804 options.upstream_name = branch_get_upstream(branch,
1805 NULL);
1806 if (!options.upstream_name)
1807 error_on_missing_default_upstream();
1808 if (fork_point < 0)
1809 fork_point = 1;
1810 } else {
1811 options.upstream_name = argv[0];
1812 argc--;
1813 argv++;
1814 if (!strcmp(options.upstream_name, "-"))
1815 options.upstream_name = "@{-1}";
1817 options.upstream = peel_committish(options.upstream_name);
1818 if (!options.upstream)
1819 die(_("invalid upstream '%s'"), options.upstream_name);
1820 options.upstream_arg = options.upstream_name;
1821 } else {
1822 if (!options.onto_name) {
1823 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1824 &squash_onto, NULL, NULL) < 0)
1825 die(_("Could not create new root commit"));
1826 options.squash_onto = &squash_onto;
1827 options.onto_name = squash_onto_name =
1828 xstrdup(oid_to_hex(&squash_onto));
1829 } else
1830 options.root_with_onto = 1;
1832 options.upstream_name = NULL;
1833 options.upstream = NULL;
1834 if (argc > 1)
1835 usage_with_options(builtin_rebase_usage,
1836 builtin_rebase_options);
1837 options.upstream_arg = "--root";
1840 /* Make sure the branch to rebase onto is valid. */
1841 if (keep_base) {
1842 strbuf_reset(&buf);
1843 strbuf_addstr(&buf, options.upstream_name);
1844 strbuf_addstr(&buf, "...");
1845 options.onto_name = xstrdup(buf.buf);
1846 } else if (!options.onto_name)
1847 options.onto_name = options.upstream_name;
1848 if (strstr(options.onto_name, "...")) {
1849 if (get_oid_mb(options.onto_name, &merge_base) < 0) {
1850 if (keep_base)
1851 die(_("'%s': need exactly one merge base with branch"),
1852 options.upstream_name);
1853 else
1854 die(_("'%s': need exactly one merge base"),
1855 options.onto_name);
1857 options.onto = lookup_commit_or_die(&merge_base,
1858 options.onto_name);
1859 } else {
1860 options.onto = peel_committish(options.onto_name);
1861 if (!options.onto)
1862 die(_("Does not point to a valid commit '%s'"),
1863 options.onto_name);
1867 * If the branch to rebase is given, that is the branch we will rebase
1868 * branch_name -- branch/commit being rebased, or
1869 * HEAD (already detached)
1870 * orig_head -- commit object name of tip of the branch before rebasing
1871 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1873 if (argc == 1) {
1874 /* Is it "rebase other branchname" or "rebase other commit"? */
1875 branch_name = argv[0];
1876 options.switch_to = argv[0];
1878 /* Is it a local branch? */
1879 strbuf_reset(&buf);
1880 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1881 if (!read_ref(buf.buf, &options.orig_head)) {
1882 die_if_checked_out(buf.buf, 1);
1883 options.head_name = xstrdup(buf.buf);
1884 /* If not is it a valid ref (branch or commit)? */
1885 } else if (!get_oid(branch_name, &options.orig_head))
1886 options.head_name = NULL;
1887 else
1888 die(_("fatal: no such branch/commit '%s'"),
1889 branch_name);
1890 } else if (argc == 0) {
1891 /* Do not need to switch branches, we are already on it. */
1892 options.head_name =
1893 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1894 &flags));
1895 if (!options.head_name)
1896 die(_("No such ref: %s"), "HEAD");
1897 if (flags & REF_ISSYMREF) {
1898 if (!skip_prefix(options.head_name,
1899 "refs/heads/", &branch_name))
1900 branch_name = options.head_name;
1902 } else {
1903 FREE_AND_NULL(options.head_name);
1904 branch_name = "HEAD";
1906 if (get_oid("HEAD", &options.orig_head))
1907 die(_("Could not resolve HEAD to a revision"));
1908 } else
1909 BUG("unexpected number of arguments left to parse");
1911 if (fork_point > 0) {
1912 struct commit *head =
1913 lookup_commit_reference(the_repository,
1914 &options.orig_head);
1915 options.restrict_revision =
1916 get_fork_point(options.upstream_name, head);
1919 if (repo_read_index(the_repository) < 0)
1920 die(_("could not read index"));
1922 if (options.autostash) {
1923 create_autostash(the_repository, state_dir_path("autostash", &options),
1924 DEFAULT_REFLOG_ACTION);
1927 if (require_clean_work_tree(the_repository, "rebase",
1928 _("Please commit or stash them."), 1, 1)) {
1929 ret = 1;
1930 goto cleanup;
1934 * Now we are rebasing commits upstream..orig_head (or with --root,
1935 * everything leading up to orig_head) on top of onto.
1939 * Check if we are already based on onto with linear history,
1940 * in which case we could fast-forward without replacing the commits
1941 * with new commits recreated by replaying their changes.
1943 * Note that can_fast_forward() initializes merge_base, so we have to
1944 * call it before checking allow_preemptive_ff.
1946 if (can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1947 &options.orig_head, &merge_base) &&
1948 allow_preemptive_ff) {
1949 int flag;
1951 if (!(options.flags & REBASE_FORCE)) {
1952 /* Lazily switch to the target branch if needed... */
1953 if (options.switch_to) {
1954 strbuf_reset(&buf);
1955 strbuf_addf(&buf, "%s: checkout %s",
1956 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1957 options.switch_to);
1958 if (reset_head(the_repository,
1959 &options.orig_head, "checkout",
1960 options.head_name,
1961 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1962 NULL, buf.buf,
1963 DEFAULT_REFLOG_ACTION) < 0) {
1964 ret = !!error(_("could not switch to "
1965 "%s"),
1966 options.switch_to);
1967 goto cleanup;
1971 if (!(options.flags & REBASE_NO_QUIET))
1972 ; /* be quiet */
1973 else if (!strcmp(branch_name, "HEAD") &&
1974 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1975 puts(_("HEAD is up to date."));
1976 else
1977 printf(_("Current branch %s is up to date.\n"),
1978 branch_name);
1979 ret = !!finish_rebase(&options);
1980 goto cleanup;
1981 } else if (!(options.flags & REBASE_NO_QUIET))
1982 ; /* be quiet */
1983 else if (!strcmp(branch_name, "HEAD") &&
1984 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1985 puts(_("HEAD is up to date, rebase forced."));
1986 else
1987 printf(_("Current branch %s is up to date, rebase "
1988 "forced.\n"), branch_name);
1991 /* If a hook exists, give it a chance to interrupt*/
1992 if (!ok_to_skip_pre_rebase &&
1993 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1994 argc ? argv[0] : NULL, NULL))
1995 die(_("The pre-rebase hook refused to rebase."));
1997 if (options.flags & REBASE_DIFFSTAT) {
1998 struct diff_options opts;
2000 if (options.flags & REBASE_VERBOSE) {
2001 if (is_null_oid(&merge_base))
2002 printf(_("Changes to %s:\n"),
2003 oid_to_hex(&options.onto->object.oid));
2004 else
2005 printf(_("Changes from %s to %s:\n"),
2006 oid_to_hex(&merge_base),
2007 oid_to_hex(&options.onto->object.oid));
2010 /* We want color (if set), but no pager */
2011 diff_setup(&opts);
2012 opts.stat_width = -1; /* use full terminal width */
2013 opts.stat_graph_width = -1; /* respect statGraphWidth config */
2014 opts.output_format |=
2015 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
2016 opts.detect_rename = DIFF_DETECT_RENAME;
2017 diff_setup_done(&opts);
2018 diff_tree_oid(is_null_oid(&merge_base) ?
2019 the_hash_algo->empty_tree : &merge_base,
2020 &options.onto->object.oid, "", &opts);
2021 diffcore_std(&opts);
2022 diff_flush(&opts);
2025 if (is_merge(&options))
2026 goto run_rebase;
2028 /* Detach HEAD and reset the tree */
2029 if (options.flags & REBASE_NO_QUIET)
2030 printf(_("First, rewinding head to replay your work on top of "
2031 "it...\n"));
2033 strbuf_addf(&msg, "%s: checkout %s",
2034 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
2035 if (reset_head(the_repository, &options.onto->object.oid, "checkout", NULL,
2036 RESET_HEAD_DETACH | RESET_ORIG_HEAD |
2037 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
2038 NULL, msg.buf, DEFAULT_REFLOG_ACTION))
2039 die(_("Could not detach HEAD"));
2040 strbuf_release(&msg);
2043 * If the onto is a proper descendant of the tip of the branch, then
2044 * we just fast-forwarded.
2046 strbuf_reset(&msg);
2047 if (oideq(&merge_base, &options.orig_head)) {
2048 printf(_("Fast-forwarded %s to %s.\n"),
2049 branch_name, options.onto_name);
2050 strbuf_addf(&msg, "rebase finished: %s onto %s",
2051 options.head_name ? options.head_name : "detached HEAD",
2052 oid_to_hex(&options.onto->object.oid));
2053 reset_head(the_repository, NULL, "Fast-forwarded", options.head_name,
2054 RESET_HEAD_REFS_ONLY, "HEAD", msg.buf,
2055 DEFAULT_REFLOG_ACTION);
2056 strbuf_release(&msg);
2057 ret = !!finish_rebase(&options);
2058 goto cleanup;
2061 strbuf_addf(&revisions, "%s..%s",
2062 options.root ? oid_to_hex(&options.onto->object.oid) :
2063 (options.restrict_revision ?
2064 oid_to_hex(&options.restrict_revision->object.oid) :
2065 oid_to_hex(&options.upstream->object.oid)),
2066 oid_to_hex(&options.orig_head));
2068 options.revisions = revisions.buf;
2070 run_rebase:
2071 ret = !!run_specific_rebase(&options, action);
2073 cleanup:
2074 strbuf_release(&buf);
2075 strbuf_release(&revisions);
2076 free(options.head_name);
2077 free(options.gpg_sign_opt);
2078 free(options.cmd);
2079 free(squash_onto_name);
2080 return ret;