2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
8 #include "run-command.h"
10 #include "argv-array.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
19 #include "parse-options.h"
22 #include "wt-status.h"
26 static char const * const builtin_rebase_usage[] = {
27 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
28 "[<upstream>] [<branch>]"),
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
31 N_("git rebase --continue | --abort | --skip | --edit-todo"),
35 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
36 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
39 REBASE_UNSPECIFIED = -1,
43 REBASE_PRESERVE_MERGES
46 static int use_builtin_rebase(void)
48 struct child_process cp = CHILD_PROCESS_INIT;
49 struct strbuf out = STRBUF_INIT;
52 argv_array_pushl(&cp.args,
53 "config", "--bool", "rebase.usebuiltin", NULL);
55 if (capture_command(&cp, &out, 6)) {
61 ret = !strcmp("true", out.buf);
66 static int apply_autostash(void)
72 struct rebase_options {
73 enum rebase_type type;
74 const char *state_dir;
75 struct commit *upstream;
76 const char *upstream_name;
77 const char *upstream_arg;
79 struct object_id orig_head;
81 const char *onto_name;
82 const char *revisions;
83 const char *switch_to;
85 struct commit *restrict_revision;
86 int dont_finish_rebase;
88 REBASE_NO_QUIET = 1<<0,
89 REBASE_VERBOSE = 1<<1,
90 REBASE_DIFFSTAT = 1<<2,
92 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
94 struct strbuf git_am_opt;
97 int allow_rerere_autoupdate;
103 static int is_interactive(struct rebase_options *opts)
105 return opts->type == REBASE_INTERACTIVE ||
106 opts->type == REBASE_PRESERVE_MERGES;
109 static void imply_interactive(struct rebase_options *opts, const char *option)
111 switch (opts->type) {
113 die(_("%s requires an interactive rebase"), option);
115 case REBASE_INTERACTIVE:
116 case REBASE_PRESERVE_MERGES:
119 /* we silently *upgrade* --merge to --interactive if needed */
121 opts->type = REBASE_INTERACTIVE; /* implied */
126 /* Returns the filename prefixed by the state_dir */
127 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
129 static struct strbuf path = STRBUF_INIT;
130 static size_t prefix_len;
133 strbuf_addf(&path, "%s/", opts->state_dir);
134 prefix_len = path.len;
137 strbuf_setlen(&path, prefix_len);
138 strbuf_addstr(&path, filename);
142 /* Read one file, then strip line endings */
143 static int read_one(const char *path, struct strbuf *buf)
145 if (strbuf_read_file(buf, path, 0) < 0)
146 return error_errno(_("could not read '%s'"), path);
147 strbuf_trim_trailing_newline(buf);
151 /* Initialize the rebase options from the state directory. */
152 static int read_basic_state(struct rebase_options *opts)
154 struct strbuf head_name = STRBUF_INIT;
155 struct strbuf buf = STRBUF_INIT;
156 struct object_id oid;
158 if (read_one(state_dir_path("head-name", opts), &head_name) ||
159 read_one(state_dir_path("onto", opts), &buf))
161 opts->head_name = starts_with(head_name.buf, "refs/") ?
162 xstrdup(head_name.buf) : NULL;
163 strbuf_release(&head_name);
164 if (get_oid(buf.buf, &oid))
165 return error(_("could not get 'onto': '%s'"), buf.buf);
166 opts->onto = lookup_commit_or_die(&oid, buf.buf);
169 * We always write to orig-head, but interactive rebase used to write to
170 * head. Fall back to reading from head to cover for the case that the
171 * user upgraded git with an ongoing interactive rebase.
174 if (file_exists(state_dir_path("orig-head", opts))) {
175 if (read_one(state_dir_path("orig-head", opts), &buf))
177 } else if (read_one(state_dir_path("head", opts), &buf))
179 if (get_oid(buf.buf, &opts->orig_head))
180 return error(_("invalid orig-head: '%s'"), buf.buf);
183 if (read_one(state_dir_path("quiet", opts), &buf))
186 opts->flags &= ~REBASE_NO_QUIET;
188 opts->flags |= REBASE_NO_QUIET;
190 if (file_exists(state_dir_path("verbose", opts)))
191 opts->flags |= REBASE_VERBOSE;
193 if (file_exists(state_dir_path("signoff", opts))) {
195 opts->flags |= REBASE_FORCE;
198 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
200 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
203 if (!strcmp(buf.buf, "--rerere-autoupdate"))
204 opts->allow_rerere_autoupdate = 1;
205 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
206 opts->allow_rerere_autoupdate = 0;
208 warning(_("ignoring invalid allow_rerere_autoupdate: "
211 opts->allow_rerere_autoupdate = -1;
213 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
215 if (read_one(state_dir_path("gpg_sign_opt", opts),
218 free(opts->gpg_sign_opt);
219 opts->gpg_sign_opt = xstrdup(buf.buf);
222 strbuf_release(&buf);
227 static int finish_rebase(struct rebase_options *opts)
229 struct strbuf dir = STRBUF_INIT;
230 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
232 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
234 close_all_packs(the_repository->objects);
236 * We ignore errors in 'gc --auto', since the
237 * user should see them.
239 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
240 strbuf_addstr(&dir, opts->state_dir);
241 remove_dir_recursively(&dir, 0);
242 strbuf_release(&dir);
247 static struct commit *peel_committish(const char *name)
250 struct object_id oid;
252 if (get_oid(name, &oid))
254 obj = parse_object(the_repository, &oid);
255 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
258 static void add_var(struct strbuf *buf, const char *name, const char *value)
261 strbuf_addf(buf, "unset %s; ", name);
263 strbuf_addf(buf, "%s=", name);
264 sq_quote_buf(buf, value);
265 strbuf_addstr(buf, "; ");
269 static int run_specific_rebase(struct rebase_options *opts)
271 const char *argv[] = { NULL, NULL };
272 struct strbuf script_snippet = STRBUF_INIT;
274 const char *backend, *backend_func;
276 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
277 add_var(&script_snippet, "state_dir", opts->state_dir);
279 add_var(&script_snippet, "upstream_name", opts->upstream_name);
280 add_var(&script_snippet, "upstream", opts->upstream ?
281 oid_to_hex(&opts->upstream->object.oid) : NULL);
282 add_var(&script_snippet, "head_name",
283 opts->head_name ? opts->head_name : "detached HEAD");
284 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
285 add_var(&script_snippet, "onto", opts->onto ?
286 oid_to_hex(&opts->onto->object.oid) : NULL);
287 add_var(&script_snippet, "onto_name", opts->onto_name);
288 add_var(&script_snippet, "revisions", opts->revisions);
289 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
290 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
291 add_var(&script_snippet, "GIT_QUIET",
292 opts->flags & REBASE_NO_QUIET ? "" : "t");
293 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
294 add_var(&script_snippet, "verbose",
295 opts->flags & REBASE_VERBOSE ? "t" : "");
296 add_var(&script_snippet, "diffstat",
297 opts->flags & REBASE_DIFFSTAT ? "t" : "");
298 add_var(&script_snippet, "force_rebase",
299 opts->flags & REBASE_FORCE ? "t" : "");
301 add_var(&script_snippet, "switch_to", opts->switch_to);
302 add_var(&script_snippet, "action", opts->action ? opts->action : "");
303 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
304 add_var(&script_snippet, "allow_rerere_autoupdate",
305 opts->allow_rerere_autoupdate < 0 ? "" :
306 opts->allow_rerere_autoupdate ?
307 "--rerere-autoupdate" : "--no-rerere-autoupdate");
308 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
309 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
310 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
312 switch (opts->type) {
314 backend = "git-rebase--am";
315 backend_func = "git_rebase__am";
317 case REBASE_INTERACTIVE:
318 backend = "git-rebase--interactive";
319 backend_func = "git_rebase__interactive";
322 backend = "git-rebase--merge";
323 backend_func = "git_rebase__merge";
325 case REBASE_PRESERVE_MERGES:
326 backend = "git-rebase--preserve-merges";
327 backend_func = "git_rebase__preserve_merges";
330 BUG("Unhandled rebase type %d", opts->type);
334 strbuf_addf(&script_snippet,
335 ". git-sh-setup && . git-rebase--common &&"
336 " . %s && %s", backend, backend_func);
337 argv[0] = script_snippet.buf;
339 status = run_command_v_opt(argv, RUN_USING_SHELL);
340 if (opts->dont_finish_rebase)
342 else if (status == 0) {
343 if (!file_exists(state_dir_path("stopped-sha", opts)))
345 } else if (status == 2) {
346 struct strbuf dir = STRBUF_INIT;
349 strbuf_addstr(&dir, opts->state_dir);
350 remove_dir_recursively(&dir, 0);
351 strbuf_release(&dir);
352 die("Nothing to do");
355 strbuf_release(&script_snippet);
357 return status ? -1 : 0;
360 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
362 static int reset_head(struct object_id *oid, const char *action,
363 const char *switch_to_branch, int detach_head)
365 struct object_id head_oid;
366 struct tree_desc desc;
367 struct lock_file lock = LOCK_INIT;
368 struct unpack_trees_options unpack_tree_opts;
370 const char *reflog_action;
371 struct strbuf msg = STRBUF_INIT;
373 struct object_id *orig = NULL, oid_orig,
374 *old_orig = NULL, oid_old_orig;
377 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
378 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
380 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
384 if (get_oid("HEAD", &head_oid)) {
385 rollback_lock_file(&lock);
386 return error(_("could not determine HEAD revision"));
391 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
392 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
393 unpack_tree_opts.head_idx = 1;
394 unpack_tree_opts.src_index = the_repository->index;
395 unpack_tree_opts.dst_index = the_repository->index;
396 unpack_tree_opts.fn = oneway_merge;
397 unpack_tree_opts.update = 1;
398 unpack_tree_opts.merge = 1;
400 unpack_tree_opts.reset = 1;
402 if (read_index_unmerged(the_repository->index) < 0) {
403 rollback_lock_file(&lock);
404 return error(_("could not read index"));
407 if (!fill_tree_descriptor(&desc, oid)) {
408 error(_("failed to find tree of %s"), oid_to_hex(oid));
409 rollback_lock_file(&lock);
410 free((void *)desc.buffer);
414 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
415 rollback_lock_file(&lock);
416 free((void *)desc.buffer);
420 tree = parse_tree_indirect(oid);
421 prime_cache_tree(the_repository->index, tree);
423 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
424 ret = error(_("could not write index"));
425 free((void *)desc.buffer);
430 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
431 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
432 prefix_len = msg.len;
434 if (!get_oid("ORIG_HEAD", &oid_old_orig))
435 old_orig = &oid_old_orig;
436 if (!get_oid("HEAD", &oid_orig)) {
438 strbuf_addstr(&msg, "updating ORIG_HEAD");
439 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
440 UPDATE_REFS_MSG_ON_ERR);
442 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
443 strbuf_setlen(&msg, prefix_len);
444 strbuf_addstr(&msg, "updating HEAD");
445 if (!switch_to_branch)
446 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
447 UPDATE_REFS_MSG_ON_ERR);
449 ret = create_symref("HEAD", switch_to_branch, msg.buf);
451 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
452 UPDATE_REFS_MSG_ON_ERR);
455 strbuf_release(&msg);
459 static int rebase_config(const char *var, const char *value, void *data)
461 struct rebase_options *opts = data;
463 if (!strcmp(var, "rebase.stat")) {
464 if (git_config_bool(var, value))
465 opts->flags |= REBASE_DIFFSTAT;
467 opts->flags &= !REBASE_DIFFSTAT;
471 if (!strcmp(var, "rebase.autosquash")) {
472 opts->autosquash = git_config_bool(var, value);
476 if (!strcmp(var, "commit.gpgsign")) {
477 free(opts->gpg_sign_opt);
478 opts->gpg_sign_opt = git_config_bool(var, value) ?
479 xstrdup("-S") : NULL;
483 return git_default_config(var, value, data);
487 * Determines whether the commits in from..to are linear, i.e. contain
488 * no merge commits. This function *expects* `from` to be an ancestor of
491 static int is_linear_history(struct commit *from, struct commit *to)
493 while (to && to != from) {
497 if (to->parents->next)
499 to = to->parents->item;
504 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
505 struct object_id *merge_base)
507 struct commit *head = lookup_commit(the_repository, head_oid);
508 struct commit_list *merge_bases;
514 merge_bases = get_merge_bases(onto, head);
515 if (merge_bases && !merge_bases->next) {
516 oidcpy(merge_base, &merge_bases->item->object.oid);
517 res = !oidcmp(merge_base, &onto->object.oid);
519 oidcpy(merge_base, &null_oid);
522 free_commit_list(merge_bases);
523 return res && is_linear_history(onto, head);
526 /* -i followed by -m is still -i */
527 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
529 struct rebase_options *opts = opt->value;
531 if (!is_interactive(opts))
532 opts->type = REBASE_MERGE;
537 /* -i followed by -p is still explicitly interactive, but -p alone is not */
538 static int parse_opt_interactive(const struct option *opt, const char *arg,
541 struct rebase_options *opts = opt->value;
543 opts->type = REBASE_INTERACTIVE;
544 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
549 int cmd_rebase(int argc, const char **argv, const char *prefix)
551 struct rebase_options options = {
552 .type = REBASE_UNSPECIFIED,
553 .flags = REBASE_NO_QUIET,
554 .git_am_opt = STRBUF_INIT,
555 .allow_rerere_autoupdate = -1,
557 const char *branch_name;
558 int ret, flags, total_argc, in_progress = 0;
559 int ok_to_skip_pre_rebase = 0;
560 struct strbuf msg = STRBUF_INIT;
561 struct strbuf revisions = STRBUF_INIT;
562 struct strbuf buf = STRBUF_INIT;
563 struct object_id merge_base;
571 ACTION_SHOW_CURRENT_PATCH,
572 } action = NO_ACTION;
573 int committer_date_is_author_date = 0;
575 int ignore_whitespace = 0;
576 const char *gpg_sign = NULL;
578 struct string_list whitespace = STRING_LIST_INIT_NODUP;
579 struct option builtin_rebase_options[] = {
580 OPT_STRING(0, "onto", &options.onto_name,
582 N_("rebase onto given branch instead of upstream")),
583 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
584 N_("allow pre-rebase hook to run")),
585 OPT_NEGBIT('q', "quiet", &options.flags,
586 N_("be quiet. implies --no-stat"),
587 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
588 OPT_BIT('v', "verbose", &options.flags,
589 N_("display a diffstat of what changed upstream"),
590 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
591 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
592 N_("do not show diffstat of what changed upstream"),
593 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
594 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
595 N_("passed to 'git apply'")),
596 OPT_BOOL(0, "signoff", &options.signoff,
597 N_("add a Signed-off-by: line to each commit")),
598 OPT_BOOL(0, "committer-date-is-author-date",
599 &committer_date_is_author_date,
600 N_("passed to 'git am'")),
601 OPT_BOOL(0, "ignore-date", &ignore_date,
602 N_("passed to 'git am'")),
603 OPT_BIT('f', "force-rebase", &options.flags,
604 N_("cherry-pick all commits, even if unchanged"),
606 OPT_BIT(0, "no-ff", &options.flags,
607 N_("cherry-pick all commits, even if unchanged"),
609 OPT_CMDMODE(0, "continue", &action, N_("continue"),
611 OPT_CMDMODE(0, "skip", &action,
612 N_("skip current patch and continue"), ACTION_SKIP),
613 OPT_CMDMODE(0, "abort", &action,
614 N_("abort and check out the original branch"),
616 OPT_CMDMODE(0, "quit", &action,
617 N_("abort but keep HEAD where it is"), ACTION_QUIT),
618 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
619 "during an interactive rebase"), ACTION_EDIT_TODO),
620 OPT_CMDMODE(0, "show-current-patch", &action,
621 N_("show the patch file being applied or merged"),
622 ACTION_SHOW_CURRENT_PATCH),
623 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
624 N_("use merging strategies to rebase"),
625 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
627 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
628 N_("let the user edit the list of commits to rebase"),
629 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
630 parse_opt_interactive },
631 OPT_SET_INT('p', "preserve-merges", &options.type,
632 N_("try to recreate merges instead of ignoring "
633 "them"), REBASE_PRESERVE_MERGES),
634 OPT_BOOL(0, "rerere-autoupdate",
635 &options.allow_rerere_autoupdate,
636 N_("allow rerere to update index with resolved "
638 OPT_BOOL('k', "keep-empty", &options.keep_empty,
639 N_("preserve empty commits during rebase")),
640 OPT_BOOL(0, "autosquash", &options.autosquash,
641 N_("move commits that begin with "
642 "squash!/fixup! under -i")),
643 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
644 N_("GPG-sign commits"),
645 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
646 OPT_STRING_LIST(0, "whitespace", &whitespace,
647 N_("whitespace"), N_("passed to 'git apply'")),
648 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
654 * NEEDSWORK: Once the builtin rebase has been tested enough
655 * and git-legacy-rebase.sh is retired to contrib/, this preamble
659 if (!use_builtin_rebase()) {
660 const char *path = mkpath("%s/git-legacy-rebase",
663 if (sane_execvp(path, (char **)argv) < 0)
664 die_errno(_("could not exec %s"), path);
666 BUG("sane_execvp() returned???");
669 if (argc == 2 && !strcmp(argv[1], "-h"))
670 usage_with_options(builtin_rebase_usage,
671 builtin_rebase_options);
673 prefix = setup_git_directory();
674 trace_repo_setup(prefix);
677 git_config(rebase_config, &options);
680 strbuf_addf(&buf, "%s/applying", apply_dir());
681 if(file_exists(buf.buf))
682 die(_("It looks like 'git am' is in progress. Cannot rebase."));
684 if (is_directory(apply_dir())) {
685 options.type = REBASE_AM;
686 options.state_dir = apply_dir();
687 } else if (is_directory(merge_dir())) {
689 strbuf_addf(&buf, "%s/rewritten", merge_dir());
690 if (is_directory(buf.buf)) {
691 options.type = REBASE_PRESERVE_MERGES;
692 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
695 strbuf_addf(&buf, "%s/interactive", merge_dir());
696 if(file_exists(buf.buf)) {
697 options.type = REBASE_INTERACTIVE;
698 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
700 options.type = REBASE_MERGE;
702 options.state_dir = merge_dir();
705 if (options.type != REBASE_UNSPECIFIED)
709 argc = parse_options(argc, argv, prefix,
710 builtin_rebase_options,
711 builtin_rebase_usage, 0);
713 if (action != NO_ACTION && total_argc != 2) {
714 usage_with_options(builtin_rebase_usage,
715 builtin_rebase_options);
719 usage_with_options(builtin_rebase_usage,
720 builtin_rebase_options);
722 if (action != NO_ACTION && !in_progress)
723 die(_("No rebase in progress?"));
725 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
726 die(_("The --edit-todo action can only be used during "
727 "interactive rebase."));
730 case ACTION_CONTINUE: {
731 struct object_id head;
732 struct lock_file lock_file = LOCK_INIT;
735 options.action = "continue";
738 if (get_oid("HEAD", &head))
739 die(_("Cannot read HEAD"));
741 fd = hold_locked_index(&lock_file, 0);
742 if (read_index(the_repository->index) < 0)
743 die(_("could not read index"));
744 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
747 update_index_if_able(the_repository->index,
749 rollback_lock_file(&lock_file);
751 if (has_unstaged_changes(1)) {
752 puts(_("You must edit all merge conflicts and then\n"
753 "mark them as resolved using git add"));
756 if (read_basic_state(&options))
761 struct string_list merge_rr = STRING_LIST_INIT_DUP;
763 options.action = "skip";
765 rerere_clear(&merge_rr);
766 string_list_clear(&merge_rr, 1);
768 if (reset_head(NULL, "reset", NULL, 0) < 0)
769 die(_("could not discard worktree changes"));
770 if (read_basic_state(&options))
775 struct string_list merge_rr = STRING_LIST_INIT_DUP;
776 options.action = "abort";
778 rerere_clear(&merge_rr);
779 string_list_clear(&merge_rr, 1);
781 if (read_basic_state(&options))
783 if (reset_head(&options.orig_head, "reset",
784 options.head_name, 0) < 0)
785 die(_("could not move back to %s"),
786 oid_to_hex(&options.orig_head));
787 ret = finish_rebase(&options);
792 strbuf_addstr(&buf, options.state_dir);
793 ret = !!remove_dir_recursively(&buf, 0);
795 die(_("could not remove '%s'"), options.state_dir);
798 case ACTION_EDIT_TODO:
799 options.action = "edit-todo";
800 options.dont_finish_rebase = 1;
802 case ACTION_SHOW_CURRENT_PATCH:
803 options.action = "show-current-patch";
804 options.dont_finish_rebase = 1;
809 BUG("action: %d", action);
812 /* Make sure no rebase is in progress */
814 const char *last_slash = strrchr(options.state_dir, '/');
815 const char *state_dir_base =
816 last_slash ? last_slash + 1 : options.state_dir;
817 const char *cmd_live_rebase =
818 "git rebase (--continue | --abort | --skip)";
820 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
821 die(_("It seems that there is already a %s directory, and\n"
822 "I wonder if you are in the middle of another rebase. "
824 "case, please try\n\t%s\n"
825 "If that is not the case, please\n\t%s\n"
826 "and run me again. I am stopping in case you still "
828 "valuable there.\n"),
829 state_dir_base, cmd_live_rebase, buf.buf);
832 if (!(options.flags & REBASE_NO_QUIET))
833 strbuf_addstr(&options.git_am_opt, " -q");
835 if (committer_date_is_author_date) {
836 strbuf_addstr(&options.git_am_opt,
837 " --committer-date-is-author-date");
838 options.flags |= REBASE_FORCE;
841 if (ignore_whitespace)
842 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
845 strbuf_addstr(&options.git_am_opt, " --ignore-date");
846 options.flags |= REBASE_FORCE;
849 if (options.keep_empty)
850 imply_interactive(&options, "--keep-empty");
853 free(options.gpg_sign_opt);
854 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
858 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
863 for (i = 0; i < whitespace.nr; i++) {
864 const char *item = whitespace.items[i].string;
866 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
869 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
870 options.flags |= REBASE_FORCE;
874 switch (options.type) {
876 case REBASE_INTERACTIVE:
877 case REBASE_PRESERVE_MERGES:
878 options.state_dir = merge_dir();
881 options.state_dir = apply_dir();
884 /* the default rebase backend is `--am` */
885 options.type = REBASE_AM;
886 options.state_dir = apply_dir();
890 if (options.signoff) {
891 if (options.type == REBASE_PRESERVE_MERGES)
892 die("cannot combine '--signoff' with "
893 "'--preserve-merges'");
894 strbuf_addstr(&options.git_am_opt, " --signoff");
895 options.flags |= REBASE_FORCE;
900 die("TODO: handle @{upstream}");
902 options.upstream_name = argv[0];
905 if (!strcmp(options.upstream_name, "-"))
906 options.upstream_name = "@{-1}";
908 options.upstream = peel_committish(options.upstream_name);
909 if (!options.upstream)
910 die(_("invalid upstream '%s'"), options.upstream_name);
911 options.upstream_arg = options.upstream_name;
913 die("TODO: upstream for --root");
915 /* Make sure the branch to rebase onto is valid. */
916 if (!options.onto_name)
917 options.onto_name = options.upstream_name;
918 if (strstr(options.onto_name, "...")) {
919 if (get_oid_mb(options.onto_name, &merge_base) < 0)
920 die(_("'%s': need exactly one merge base"),
922 options.onto = lookup_commit_or_die(&merge_base,
925 options.onto = peel_committish(options.onto_name);
927 die(_("Does not point to a valid commit '%s'"),
932 * If the branch to rebase is given, that is the branch we will rebase
933 * branch_name -- branch/commit being rebased, or
934 * HEAD (already detached)
935 * orig_head -- commit object name of tip of the branch before rebasing
936 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
939 /* Is it "rebase other branchname" or "rebase other commit"? */
940 branch_name = argv[0];
941 options.switch_to = argv[0];
943 /* Is it a local branch? */
945 strbuf_addf(&buf, "refs/heads/%s", branch_name);
946 if (!read_ref(buf.buf, &options.orig_head))
947 options.head_name = xstrdup(buf.buf);
948 /* If not is it a valid ref (branch or commit)? */
949 else if (!get_oid(branch_name, &options.orig_head))
950 options.head_name = NULL;
952 die(_("fatal: no such branch/commit '%s'"),
954 } else if (argc == 0) {
955 /* Do not need to switch branches, we are already on it. */
957 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
959 if (!options.head_name)
960 die(_("No such ref: %s"), "HEAD");
961 if (flags & REF_ISSYMREF) {
962 if (!skip_prefix(options.head_name,
963 "refs/heads/", &branch_name))
964 branch_name = options.head_name;
967 free(options.head_name);
968 options.head_name = NULL;
969 branch_name = "HEAD";
971 if (get_oid("HEAD", &options.orig_head))
972 die(_("Could not resolve HEAD to a revision"));
974 BUG("unexpected number of arguments left to parse");
976 if (read_index(the_repository->index) < 0)
977 die(_("could not read index"));
979 if (require_clean_work_tree("rebase",
980 _("Please commit or stash them."), 1, 1)) {
986 * Now we are rebasing commits upstream..orig_head (or with --root,
987 * everything leading up to orig_head) on top of onto.
991 * Check if we are already based on onto with linear history,
992 * but this should be done only when upstream and onto are the same
993 * and if this is not an interactive rebase.
995 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
996 !is_interactive(&options) && !options.restrict_revision &&
997 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1000 if (!(options.flags & REBASE_FORCE)) {
1001 /* Lazily switch to the target branch if needed... */
1002 if (options.switch_to) {
1003 struct object_id oid;
1005 if (get_oid(options.switch_to, &oid) < 0) {
1006 ret = !!error(_("could not parse '%s'"),
1012 strbuf_addf(&buf, "rebase: checkout %s",
1014 if (reset_head(&oid, "checkout",
1015 options.head_name, 0) < 0) {
1016 ret = !!error(_("could not switch to "
1023 if (!(options.flags & REBASE_NO_QUIET))
1025 else if (!strcmp(branch_name, "HEAD") &&
1026 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1027 puts(_("HEAD is up to date."));
1029 printf(_("Current branch %s is up to date.\n"),
1031 ret = !!finish_rebase(&options);
1033 } else if (!(options.flags & REBASE_NO_QUIET))
1035 else if (!strcmp(branch_name, "HEAD") &&
1036 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1037 puts(_("HEAD is up to date, rebase forced."));
1039 printf(_("Current branch %s is up to date, rebase "
1040 "forced.\n"), branch_name);
1043 /* If a hook exists, give it a chance to interrupt*/
1044 if (!ok_to_skip_pre_rebase &&
1045 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1046 argc ? argv[0] : NULL, NULL))
1047 die(_("The pre-rebase hook refused to rebase."));
1049 if (options.flags & REBASE_DIFFSTAT) {
1050 struct diff_options opts;
1052 if (options.flags & REBASE_VERBOSE)
1053 printf(_("Changes from %s to %s:\n"),
1054 oid_to_hex(&merge_base),
1055 oid_to_hex(&options.onto->object.oid));
1057 /* We want color (if set), but no pager */
1059 opts.stat_width = -1; /* use full terminal width */
1060 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1061 opts.output_format |=
1062 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1063 opts.detect_rename = DIFF_DETECT_RENAME;
1064 diff_setup_done(&opts);
1065 diff_tree_oid(&merge_base, &options.onto->object.oid,
1067 diffcore_std(&opts);
1071 if (is_interactive(&options))
1074 /* Detach HEAD and reset the tree */
1075 if (options.flags & REBASE_NO_QUIET)
1076 printf(_("First, rewinding head to replay your work on top of "
1079 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1080 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1081 die(_("Could not detach HEAD"));
1082 strbuf_release(&msg);
1084 strbuf_addf(&revisions, "%s..%s",
1085 options.root ? oid_to_hex(&options.onto->object.oid) :
1086 (options.restrict_revision ?
1087 oid_to_hex(&options.restrict_revision->object.oid) :
1088 oid_to_hex(&options.upstream->object.oid)),
1089 oid_to_hex(&options.orig_head));
1091 options.revisions = revisions.buf;
1094 ret = !!run_specific_rebase(&options);
1097 strbuf_release(&revisions);
1098 free(options.head_name);
1099 free(options.gpg_sign_opt);