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 struct rebase_options {
67 enum rebase_type type;
68 const char *state_dir;
69 struct commit *upstream;
70 const char *upstream_name;
71 const char *upstream_arg;
73 struct object_id orig_head;
75 const char *onto_name;
76 const char *revisions;
77 const char *switch_to;
79 struct commit *restrict_revision;
80 int dont_finish_rebase;
82 REBASE_NO_QUIET = 1<<0,
83 REBASE_VERBOSE = 1<<1,
84 REBASE_DIFFSTAT = 1<<2,
86 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
88 struct strbuf git_am_opt;
91 int allow_rerere_autoupdate;
97 int allow_empty_message;
98 int rebase_merges, rebase_cousins;
101 static int is_interactive(struct rebase_options *opts)
103 return opts->type == REBASE_INTERACTIVE ||
104 opts->type == REBASE_PRESERVE_MERGES;
107 static void imply_interactive(struct rebase_options *opts, const char *option)
109 switch (opts->type) {
111 die(_("%s requires an interactive rebase"), option);
113 case REBASE_INTERACTIVE:
114 case REBASE_PRESERVE_MERGES:
117 /* we silently *upgrade* --merge to --interactive if needed */
119 opts->type = REBASE_INTERACTIVE; /* implied */
124 /* Returns the filename prefixed by the state_dir */
125 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
127 static struct strbuf path = STRBUF_INIT;
128 static size_t prefix_len;
131 strbuf_addf(&path, "%s/", opts->state_dir);
132 prefix_len = path.len;
135 strbuf_setlen(&path, prefix_len);
136 strbuf_addstr(&path, filename);
140 /* Read one file, then strip line endings */
141 static int read_one(const char *path, struct strbuf *buf)
143 if (strbuf_read_file(buf, path, 0) < 0)
144 return error_errno(_("could not read '%s'"), path);
145 strbuf_trim_trailing_newline(buf);
149 /* Initialize the rebase options from the state directory. */
150 static int read_basic_state(struct rebase_options *opts)
152 struct strbuf head_name = STRBUF_INIT;
153 struct strbuf buf = STRBUF_INIT;
154 struct object_id oid;
156 if (read_one(state_dir_path("head-name", opts), &head_name) ||
157 read_one(state_dir_path("onto", opts), &buf))
159 opts->head_name = starts_with(head_name.buf, "refs/") ?
160 xstrdup(head_name.buf) : NULL;
161 strbuf_release(&head_name);
162 if (get_oid(buf.buf, &oid))
163 return error(_("could not get 'onto': '%s'"), buf.buf);
164 opts->onto = lookup_commit_or_die(&oid, buf.buf);
167 * We always write to orig-head, but interactive rebase used to write to
168 * head. Fall back to reading from head to cover for the case that the
169 * user upgraded git with an ongoing interactive rebase.
172 if (file_exists(state_dir_path("orig-head", opts))) {
173 if (read_one(state_dir_path("orig-head", opts), &buf))
175 } else if (read_one(state_dir_path("head", opts), &buf))
177 if (get_oid(buf.buf, &opts->orig_head))
178 return error(_("invalid orig-head: '%s'"), buf.buf);
181 if (read_one(state_dir_path("quiet", opts), &buf))
184 opts->flags &= ~REBASE_NO_QUIET;
186 opts->flags |= REBASE_NO_QUIET;
188 if (file_exists(state_dir_path("verbose", opts)))
189 opts->flags |= REBASE_VERBOSE;
191 if (file_exists(state_dir_path("signoff", opts))) {
193 opts->flags |= REBASE_FORCE;
196 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
198 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
201 if (!strcmp(buf.buf, "--rerere-autoupdate"))
202 opts->allow_rerere_autoupdate = 1;
203 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
204 opts->allow_rerere_autoupdate = 0;
206 warning(_("ignoring invalid allow_rerere_autoupdate: "
209 opts->allow_rerere_autoupdate = -1;
211 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
213 if (read_one(state_dir_path("gpg_sign_opt", opts),
216 free(opts->gpg_sign_opt);
217 opts->gpg_sign_opt = xstrdup(buf.buf);
220 strbuf_release(&buf);
225 static int apply_autostash(struct rebase_options *opts)
227 const char *path = state_dir_path("autostash", opts);
228 struct strbuf autostash = STRBUF_INIT;
229 struct child_process stash_apply = CHILD_PROCESS_INIT;
231 if (!file_exists(path))
234 if (read_one(state_dir_path("autostash", opts), &autostash))
235 return error(_("Could not read '%s'"), path);
236 argv_array_pushl(&stash_apply.args,
237 "stash", "apply", autostash.buf, NULL);
238 stash_apply.git_cmd = 1;
239 stash_apply.no_stderr = stash_apply.no_stdout =
240 stash_apply.no_stdin = 1;
241 if (!run_command(&stash_apply))
242 printf(_("Applied autostash.\n"));
244 struct argv_array args = ARGV_ARRAY_INIT;
247 argv_array_pushl(&args,
248 "stash", "store", "-m", "autostash", "-q",
249 autostash.buf, NULL);
250 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
251 res = error(_("Cannot store %s"), autostash.buf);
252 argv_array_clear(&args);
253 strbuf_release(&autostash);
258 _("Applying autostash resulted in conflicts.\n"
259 "Your changes are safe in the stash.\n"
260 "You can run \"git stash pop\" or \"git stash drop\" "
264 strbuf_release(&autostash);
268 static int finish_rebase(struct rebase_options *opts)
270 struct strbuf dir = STRBUF_INIT;
271 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
273 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
274 apply_autostash(opts);
275 close_all_packs(the_repository->objects);
277 * We ignore errors in 'gc --auto', since the
278 * user should see them.
280 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
281 strbuf_addstr(&dir, opts->state_dir);
282 remove_dir_recursively(&dir, 0);
283 strbuf_release(&dir);
288 static struct commit *peel_committish(const char *name)
291 struct object_id oid;
293 if (get_oid(name, &oid))
295 obj = parse_object(the_repository, &oid);
296 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
299 static void add_var(struct strbuf *buf, const char *name, const char *value)
302 strbuf_addf(buf, "unset %s; ", name);
304 strbuf_addf(buf, "%s=", name);
305 sq_quote_buf(buf, value);
306 strbuf_addstr(buf, "; ");
310 static int run_specific_rebase(struct rebase_options *opts)
312 const char *argv[] = { NULL, NULL };
313 struct strbuf script_snippet = STRBUF_INIT;
315 const char *backend, *backend_func;
317 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
318 add_var(&script_snippet, "state_dir", opts->state_dir);
320 add_var(&script_snippet, "upstream_name", opts->upstream_name);
321 add_var(&script_snippet, "upstream", opts->upstream ?
322 oid_to_hex(&opts->upstream->object.oid) : NULL);
323 add_var(&script_snippet, "head_name",
324 opts->head_name ? opts->head_name : "detached HEAD");
325 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
326 add_var(&script_snippet, "onto", opts->onto ?
327 oid_to_hex(&opts->onto->object.oid) : NULL);
328 add_var(&script_snippet, "onto_name", opts->onto_name);
329 add_var(&script_snippet, "revisions", opts->revisions);
330 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
331 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
332 add_var(&script_snippet, "GIT_QUIET",
333 opts->flags & REBASE_NO_QUIET ? "" : "t");
334 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
335 add_var(&script_snippet, "verbose",
336 opts->flags & REBASE_VERBOSE ? "t" : "");
337 add_var(&script_snippet, "diffstat",
338 opts->flags & REBASE_DIFFSTAT ? "t" : "");
339 add_var(&script_snippet, "force_rebase",
340 opts->flags & REBASE_FORCE ? "t" : "");
342 add_var(&script_snippet, "switch_to", opts->switch_to);
343 add_var(&script_snippet, "action", opts->action ? opts->action : "");
344 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
345 add_var(&script_snippet, "allow_rerere_autoupdate",
346 opts->allow_rerere_autoupdate < 0 ? "" :
347 opts->allow_rerere_autoupdate ?
348 "--rerere-autoupdate" : "--no-rerere-autoupdate");
349 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
350 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
351 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
352 add_var(&script_snippet, "cmd", opts->cmd);
353 add_var(&script_snippet, "allow_empty_message",
354 opts->allow_empty_message ? "--allow-empty-message" : "");
355 add_var(&script_snippet, "rebase_merges",
356 opts->rebase_merges ? "t" : "");
357 add_var(&script_snippet, "rebase_cousins",
358 opts->rebase_cousins ? "t" : "");
360 switch (opts->type) {
362 backend = "git-rebase--am";
363 backend_func = "git_rebase__am";
365 case REBASE_INTERACTIVE:
366 backend = "git-rebase--interactive";
367 backend_func = "git_rebase__interactive";
370 backend = "git-rebase--merge";
371 backend_func = "git_rebase__merge";
373 case REBASE_PRESERVE_MERGES:
374 backend = "git-rebase--preserve-merges";
375 backend_func = "git_rebase__preserve_merges";
378 BUG("Unhandled rebase type %d", opts->type);
382 strbuf_addf(&script_snippet,
383 ". git-sh-setup && . git-rebase--common &&"
384 " . %s && %s", backend, backend_func);
385 argv[0] = script_snippet.buf;
387 status = run_command_v_opt(argv, RUN_USING_SHELL);
388 if (opts->dont_finish_rebase)
390 else if (status == 0) {
391 if (!file_exists(state_dir_path("stopped-sha", opts)))
393 } else if (status == 2) {
394 struct strbuf dir = STRBUF_INIT;
396 apply_autostash(opts);
397 strbuf_addstr(&dir, opts->state_dir);
398 remove_dir_recursively(&dir, 0);
399 strbuf_release(&dir);
400 die("Nothing to do");
403 strbuf_release(&script_snippet);
405 return status ? -1 : 0;
408 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
410 static int reset_head(struct object_id *oid, const char *action,
411 const char *switch_to_branch, int detach_head)
413 struct object_id head_oid;
414 struct tree_desc desc;
415 struct lock_file lock = LOCK_INIT;
416 struct unpack_trees_options unpack_tree_opts;
418 const char *reflog_action;
419 struct strbuf msg = STRBUF_INIT;
421 struct object_id *orig = NULL, oid_orig,
422 *old_orig = NULL, oid_old_orig;
425 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
426 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
428 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
432 if (get_oid("HEAD", &head_oid)) {
433 rollback_lock_file(&lock);
434 return error(_("could not determine HEAD revision"));
439 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
440 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
441 unpack_tree_opts.head_idx = 1;
442 unpack_tree_opts.src_index = the_repository->index;
443 unpack_tree_opts.dst_index = the_repository->index;
444 unpack_tree_opts.fn = oneway_merge;
445 unpack_tree_opts.update = 1;
446 unpack_tree_opts.merge = 1;
448 unpack_tree_opts.reset = 1;
450 if (read_index_unmerged(the_repository->index) < 0) {
451 rollback_lock_file(&lock);
452 return error(_("could not read index"));
455 if (!fill_tree_descriptor(&desc, oid)) {
456 error(_("failed to find tree of %s"), oid_to_hex(oid));
457 rollback_lock_file(&lock);
458 free((void *)desc.buffer);
462 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
463 rollback_lock_file(&lock);
464 free((void *)desc.buffer);
468 tree = parse_tree_indirect(oid);
469 prime_cache_tree(the_repository->index, tree);
471 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
472 ret = error(_("could not write index"));
473 free((void *)desc.buffer);
478 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
479 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
480 prefix_len = msg.len;
482 if (!get_oid("ORIG_HEAD", &oid_old_orig))
483 old_orig = &oid_old_orig;
484 if (!get_oid("HEAD", &oid_orig)) {
486 strbuf_addstr(&msg, "updating ORIG_HEAD");
487 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
488 UPDATE_REFS_MSG_ON_ERR);
490 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
491 strbuf_setlen(&msg, prefix_len);
492 strbuf_addstr(&msg, "updating HEAD");
493 if (!switch_to_branch)
494 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
495 UPDATE_REFS_MSG_ON_ERR);
497 ret = create_symref("HEAD", switch_to_branch, msg.buf);
499 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
500 UPDATE_REFS_MSG_ON_ERR);
503 strbuf_release(&msg);
507 static int rebase_config(const char *var, const char *value, void *data)
509 struct rebase_options *opts = data;
511 if (!strcmp(var, "rebase.stat")) {
512 if (git_config_bool(var, value))
513 opts->flags |= REBASE_DIFFSTAT;
515 opts->flags &= !REBASE_DIFFSTAT;
519 if (!strcmp(var, "rebase.autosquash")) {
520 opts->autosquash = git_config_bool(var, value);
524 if (!strcmp(var, "commit.gpgsign")) {
525 free(opts->gpg_sign_opt);
526 opts->gpg_sign_opt = git_config_bool(var, value) ?
527 xstrdup("-S") : NULL;
531 if (!strcmp(var, "rebase.autostash")) {
532 opts->autostash = git_config_bool(var, value);
536 return git_default_config(var, value, data);
540 * Determines whether the commits in from..to are linear, i.e. contain
541 * no merge commits. This function *expects* `from` to be an ancestor of
544 static int is_linear_history(struct commit *from, struct commit *to)
546 while (to && to != from) {
550 if (to->parents->next)
552 to = to->parents->item;
557 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
558 struct object_id *merge_base)
560 struct commit *head = lookup_commit(the_repository, head_oid);
561 struct commit_list *merge_bases;
567 merge_bases = get_merge_bases(onto, head);
568 if (merge_bases && !merge_bases->next) {
569 oidcpy(merge_base, &merge_bases->item->object.oid);
570 res = !oidcmp(merge_base, &onto->object.oid);
572 oidcpy(merge_base, &null_oid);
575 free_commit_list(merge_bases);
576 return res && is_linear_history(onto, head);
579 /* -i followed by -m is still -i */
580 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
582 struct rebase_options *opts = opt->value;
584 if (!is_interactive(opts))
585 opts->type = REBASE_MERGE;
590 /* -i followed by -p is still explicitly interactive, but -p alone is not */
591 static int parse_opt_interactive(const struct option *opt, const char *arg,
594 struct rebase_options *opts = opt->value;
596 opts->type = REBASE_INTERACTIVE;
597 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
602 int cmd_rebase(int argc, const char **argv, const char *prefix)
604 struct rebase_options options = {
605 .type = REBASE_UNSPECIFIED,
606 .flags = REBASE_NO_QUIET,
607 .git_am_opt = STRBUF_INIT,
608 .allow_rerere_autoupdate = -1,
609 .allow_empty_message = 1,
611 const char *branch_name;
612 int ret, flags, total_argc, in_progress = 0;
613 int ok_to_skip_pre_rebase = 0;
614 struct strbuf msg = STRBUF_INIT;
615 struct strbuf revisions = STRBUF_INIT;
616 struct strbuf buf = STRBUF_INIT;
617 struct object_id merge_base;
625 ACTION_SHOW_CURRENT_PATCH,
626 } action = NO_ACTION;
627 int committer_date_is_author_date = 0;
629 int ignore_whitespace = 0;
630 const char *gpg_sign = NULL;
632 struct string_list whitespace = STRING_LIST_INIT_NODUP;
633 struct string_list exec = STRING_LIST_INIT_NODUP;
634 const char *rebase_merges = NULL;
635 struct option builtin_rebase_options[] = {
636 OPT_STRING(0, "onto", &options.onto_name,
638 N_("rebase onto given branch instead of upstream")),
639 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
640 N_("allow pre-rebase hook to run")),
641 OPT_NEGBIT('q', "quiet", &options.flags,
642 N_("be quiet. implies --no-stat"),
643 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
644 OPT_BIT('v', "verbose", &options.flags,
645 N_("display a diffstat of what changed upstream"),
646 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
647 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
648 N_("do not show diffstat of what changed upstream"),
649 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
650 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
651 N_("passed to 'git apply'")),
652 OPT_BOOL(0, "signoff", &options.signoff,
653 N_("add a Signed-off-by: line to each commit")),
654 OPT_BOOL(0, "committer-date-is-author-date",
655 &committer_date_is_author_date,
656 N_("passed to 'git am'")),
657 OPT_BOOL(0, "ignore-date", &ignore_date,
658 N_("passed to 'git am'")),
659 OPT_BIT('f', "force-rebase", &options.flags,
660 N_("cherry-pick all commits, even if unchanged"),
662 OPT_BIT(0, "no-ff", &options.flags,
663 N_("cherry-pick all commits, even if unchanged"),
665 OPT_CMDMODE(0, "continue", &action, N_("continue"),
667 OPT_CMDMODE(0, "skip", &action,
668 N_("skip current patch and continue"), ACTION_SKIP),
669 OPT_CMDMODE(0, "abort", &action,
670 N_("abort and check out the original branch"),
672 OPT_CMDMODE(0, "quit", &action,
673 N_("abort but keep HEAD where it is"), ACTION_QUIT),
674 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
675 "during an interactive rebase"), ACTION_EDIT_TODO),
676 OPT_CMDMODE(0, "show-current-patch", &action,
677 N_("show the patch file being applied or merged"),
678 ACTION_SHOW_CURRENT_PATCH),
679 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
680 N_("use merging strategies to rebase"),
681 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
683 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
684 N_("let the user edit the list of commits to rebase"),
685 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
686 parse_opt_interactive },
687 OPT_SET_INT('p', "preserve-merges", &options.type,
688 N_("try to recreate merges instead of ignoring "
689 "them"), REBASE_PRESERVE_MERGES),
690 OPT_BOOL(0, "rerere-autoupdate",
691 &options.allow_rerere_autoupdate,
692 N_("allow rerere to update index with resolved "
694 OPT_BOOL('k', "keep-empty", &options.keep_empty,
695 N_("preserve empty commits during rebase")),
696 OPT_BOOL(0, "autosquash", &options.autosquash,
697 N_("move commits that begin with "
698 "squash!/fixup! under -i")),
699 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
700 N_("GPG-sign commits"),
701 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
702 OPT_STRING_LIST(0, "whitespace", &whitespace,
703 N_("whitespace"), N_("passed to 'git apply'")),
704 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
706 OPT_BOOL(0, "autostash", &options.autostash,
707 N_("automatically stash/stash pop before and after")),
708 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
709 N_("add exec lines after each commit of the "
711 OPT_BOOL(0, "allow-empty-message",
712 &options.allow_empty_message,
713 N_("allow rebasing commits with empty messages")),
714 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
716 N_("try to rebase merges instead of skipping them"),
717 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
722 * NEEDSWORK: Once the builtin rebase has been tested enough
723 * and git-legacy-rebase.sh is retired to contrib/, this preamble
727 if (!use_builtin_rebase()) {
728 const char *path = mkpath("%s/git-legacy-rebase",
731 if (sane_execvp(path, (char **)argv) < 0)
732 die_errno(_("could not exec %s"), path);
734 BUG("sane_execvp() returned???");
737 if (argc == 2 && !strcmp(argv[1], "-h"))
738 usage_with_options(builtin_rebase_usage,
739 builtin_rebase_options);
741 prefix = setup_git_directory();
742 trace_repo_setup(prefix);
745 git_config(rebase_config, &options);
748 strbuf_addf(&buf, "%s/applying", apply_dir());
749 if(file_exists(buf.buf))
750 die(_("It looks like 'git am' is in progress. Cannot rebase."));
752 if (is_directory(apply_dir())) {
753 options.type = REBASE_AM;
754 options.state_dir = apply_dir();
755 } else if (is_directory(merge_dir())) {
757 strbuf_addf(&buf, "%s/rewritten", merge_dir());
758 if (is_directory(buf.buf)) {
759 options.type = REBASE_PRESERVE_MERGES;
760 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
763 strbuf_addf(&buf, "%s/interactive", merge_dir());
764 if(file_exists(buf.buf)) {
765 options.type = REBASE_INTERACTIVE;
766 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
768 options.type = REBASE_MERGE;
770 options.state_dir = merge_dir();
773 if (options.type != REBASE_UNSPECIFIED)
777 argc = parse_options(argc, argv, prefix,
778 builtin_rebase_options,
779 builtin_rebase_usage, 0);
781 if (action != NO_ACTION && total_argc != 2) {
782 usage_with_options(builtin_rebase_usage,
783 builtin_rebase_options);
787 usage_with_options(builtin_rebase_usage,
788 builtin_rebase_options);
790 if (action != NO_ACTION && !in_progress)
791 die(_("No rebase in progress?"));
793 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
794 die(_("The --edit-todo action can only be used during "
795 "interactive rebase."));
798 case ACTION_CONTINUE: {
799 struct object_id head;
800 struct lock_file lock_file = LOCK_INIT;
803 options.action = "continue";
806 if (get_oid("HEAD", &head))
807 die(_("Cannot read HEAD"));
809 fd = hold_locked_index(&lock_file, 0);
810 if (read_index(the_repository->index) < 0)
811 die(_("could not read index"));
812 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
815 update_index_if_able(the_repository->index,
817 rollback_lock_file(&lock_file);
819 if (has_unstaged_changes(1)) {
820 puts(_("You must edit all merge conflicts and then\n"
821 "mark them as resolved using git add"));
824 if (read_basic_state(&options))
829 struct string_list merge_rr = STRING_LIST_INIT_DUP;
831 options.action = "skip";
833 rerere_clear(&merge_rr);
834 string_list_clear(&merge_rr, 1);
836 if (reset_head(NULL, "reset", NULL, 0) < 0)
837 die(_("could not discard worktree changes"));
838 if (read_basic_state(&options))
843 struct string_list merge_rr = STRING_LIST_INIT_DUP;
844 options.action = "abort";
846 rerere_clear(&merge_rr);
847 string_list_clear(&merge_rr, 1);
849 if (read_basic_state(&options))
851 if (reset_head(&options.orig_head, "reset",
852 options.head_name, 0) < 0)
853 die(_("could not move back to %s"),
854 oid_to_hex(&options.orig_head));
855 ret = finish_rebase(&options);
860 strbuf_addstr(&buf, options.state_dir);
861 ret = !!remove_dir_recursively(&buf, 0);
863 die(_("could not remove '%s'"), options.state_dir);
866 case ACTION_EDIT_TODO:
867 options.action = "edit-todo";
868 options.dont_finish_rebase = 1;
870 case ACTION_SHOW_CURRENT_PATCH:
871 options.action = "show-current-patch";
872 options.dont_finish_rebase = 1;
877 BUG("action: %d", action);
880 /* Make sure no rebase is in progress */
882 const char *last_slash = strrchr(options.state_dir, '/');
883 const char *state_dir_base =
884 last_slash ? last_slash + 1 : options.state_dir;
885 const char *cmd_live_rebase =
886 "git rebase (--continue | --abort | --skip)";
888 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
889 die(_("It seems that there is already a %s directory, and\n"
890 "I wonder if you are in the middle of another rebase. "
892 "case, please try\n\t%s\n"
893 "If that is not the case, please\n\t%s\n"
894 "and run me again. I am stopping in case you still "
896 "valuable there.\n"),
897 state_dir_base, cmd_live_rebase, buf.buf);
900 if (!(options.flags & REBASE_NO_QUIET))
901 strbuf_addstr(&options.git_am_opt, " -q");
903 if (committer_date_is_author_date) {
904 strbuf_addstr(&options.git_am_opt,
905 " --committer-date-is-author-date");
906 options.flags |= REBASE_FORCE;
909 if (ignore_whitespace)
910 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
913 strbuf_addstr(&options.git_am_opt, " --ignore-date");
914 options.flags |= REBASE_FORCE;
917 if (options.keep_empty)
918 imply_interactive(&options, "--keep-empty");
921 free(options.gpg_sign_opt);
922 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
926 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
931 for (i = 0; i < whitespace.nr; i++) {
932 const char *item = whitespace.items[i].string;
934 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
937 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
938 options.flags |= REBASE_FORCE;
945 imply_interactive(&options, "--exec");
948 for (i = 0; i < exec.nr; i++)
949 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
950 options.cmd = xstrdup(buf.buf);
955 ; /* default mode; do nothing */
956 else if (!strcmp("rebase-cousins", rebase_merges))
957 options.rebase_cousins = 1;
958 else if (strcmp("no-rebase-cousins", rebase_merges))
959 die(_("Unknown mode: %s"), rebase_merges);
960 options.rebase_merges = 1;
961 imply_interactive(&options, "--rebase-merges");
964 switch (options.type) {
966 case REBASE_INTERACTIVE:
967 case REBASE_PRESERVE_MERGES:
968 options.state_dir = merge_dir();
971 options.state_dir = apply_dir();
974 /* the default rebase backend is `--am` */
975 options.type = REBASE_AM;
976 options.state_dir = apply_dir();
980 if (options.signoff) {
981 if (options.type == REBASE_PRESERVE_MERGES)
982 die("cannot combine '--signoff' with "
983 "'--preserve-merges'");
984 strbuf_addstr(&options.git_am_opt, " --signoff");
985 options.flags |= REBASE_FORCE;
990 die("TODO: handle @{upstream}");
992 options.upstream_name = argv[0];
995 if (!strcmp(options.upstream_name, "-"))
996 options.upstream_name = "@{-1}";
998 options.upstream = peel_committish(options.upstream_name);
999 if (!options.upstream)
1000 die(_("invalid upstream '%s'"), options.upstream_name);
1001 options.upstream_arg = options.upstream_name;
1003 die("TODO: upstream for --root");
1005 /* Make sure the branch to rebase onto is valid. */
1006 if (!options.onto_name)
1007 options.onto_name = options.upstream_name;
1008 if (strstr(options.onto_name, "...")) {
1009 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1010 die(_("'%s': need exactly one merge base"),
1012 options.onto = lookup_commit_or_die(&merge_base,
1015 options.onto = peel_committish(options.onto_name);
1017 die(_("Does not point to a valid commit '%s'"),
1022 * If the branch to rebase is given, that is the branch we will rebase
1023 * branch_name -- branch/commit being rebased, or
1024 * HEAD (already detached)
1025 * orig_head -- commit object name of tip of the branch before rebasing
1026 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1029 /* Is it "rebase other branchname" or "rebase other commit"? */
1030 branch_name = argv[0];
1031 options.switch_to = argv[0];
1033 /* Is it a local branch? */
1035 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1036 if (!read_ref(buf.buf, &options.orig_head))
1037 options.head_name = xstrdup(buf.buf);
1038 /* If not is it a valid ref (branch or commit)? */
1039 else if (!get_oid(branch_name, &options.orig_head))
1040 options.head_name = NULL;
1042 die(_("fatal: no such branch/commit '%s'"),
1044 } else if (argc == 0) {
1045 /* Do not need to switch branches, we are already on it. */
1047 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1049 if (!options.head_name)
1050 die(_("No such ref: %s"), "HEAD");
1051 if (flags & REF_ISSYMREF) {
1052 if (!skip_prefix(options.head_name,
1053 "refs/heads/", &branch_name))
1054 branch_name = options.head_name;
1057 free(options.head_name);
1058 options.head_name = NULL;
1059 branch_name = "HEAD";
1061 if (get_oid("HEAD", &options.orig_head))
1062 die(_("Could not resolve HEAD to a revision"));
1064 BUG("unexpected number of arguments left to parse");
1066 if (read_index(the_repository->index) < 0)
1067 die(_("could not read index"));
1069 if (options.autostash) {
1070 struct lock_file lock_file = LOCK_INIT;
1073 fd = hold_locked_index(&lock_file, 0);
1074 refresh_cache(REFRESH_QUIET);
1076 update_index_if_able(&the_index, &lock_file);
1077 rollback_lock_file(&lock_file);
1079 if (has_unstaged_changes(0) || has_uncommitted_changes(0)) {
1080 const char *autostash =
1081 state_dir_path("autostash", &options);
1082 struct child_process stash = CHILD_PROCESS_INIT;
1083 struct object_id oid;
1084 struct commit *head =
1085 lookup_commit_reference(the_repository,
1086 &options.orig_head);
1088 argv_array_pushl(&stash.args,
1089 "stash", "create", "autostash", NULL);
1093 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1094 die(_("Cannot autostash"));
1095 strbuf_trim_trailing_newline(&buf);
1096 if (get_oid(buf.buf, &oid))
1097 die(_("Unexpected stash response: '%s'"),
1100 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1102 if (safe_create_leading_directories_const(autostash))
1103 die(_("Could not create directory for '%s'"),
1105 write_file(autostash, "%s", buf.buf);
1106 printf(_("Created autostash: %s\n"), buf.buf);
1107 if (reset_head(&head->object.oid, "reset --hard",
1109 die(_("could not reset --hard"));
1110 printf(_("HEAD is now at %s"),
1111 find_unique_abbrev(&head->object.oid,
1114 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1116 printf(" %s", buf.buf);
1119 if (discard_index(the_repository->index) < 0 ||
1120 read_index(the_repository->index) < 0)
1121 die(_("could not read index"));
1125 if (require_clean_work_tree("rebase",
1126 _("Please commit or stash them."), 1, 1)) {
1132 * Now we are rebasing commits upstream..orig_head (or with --root,
1133 * everything leading up to orig_head) on top of onto.
1137 * Check if we are already based on onto with linear history,
1138 * but this should be done only when upstream and onto are the same
1139 * and if this is not an interactive rebase.
1141 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1142 !is_interactive(&options) && !options.restrict_revision &&
1143 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1146 if (!(options.flags & REBASE_FORCE)) {
1147 /* Lazily switch to the target branch if needed... */
1148 if (options.switch_to) {
1149 struct object_id oid;
1151 if (get_oid(options.switch_to, &oid) < 0) {
1152 ret = !!error(_("could not parse '%s'"),
1158 strbuf_addf(&buf, "rebase: checkout %s",
1160 if (reset_head(&oid, "checkout",
1161 options.head_name, 0) < 0) {
1162 ret = !!error(_("could not switch to "
1169 if (!(options.flags & REBASE_NO_QUIET))
1171 else if (!strcmp(branch_name, "HEAD") &&
1172 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1173 puts(_("HEAD is up to date."));
1175 printf(_("Current branch %s is up to date.\n"),
1177 ret = !!finish_rebase(&options);
1179 } else if (!(options.flags & REBASE_NO_QUIET))
1181 else if (!strcmp(branch_name, "HEAD") &&
1182 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1183 puts(_("HEAD is up to date, rebase forced."));
1185 printf(_("Current branch %s is up to date, rebase "
1186 "forced.\n"), branch_name);
1189 /* If a hook exists, give it a chance to interrupt*/
1190 if (!ok_to_skip_pre_rebase &&
1191 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1192 argc ? argv[0] : NULL, NULL))
1193 die(_("The pre-rebase hook refused to rebase."));
1195 if (options.flags & REBASE_DIFFSTAT) {
1196 struct diff_options opts;
1198 if (options.flags & REBASE_VERBOSE)
1199 printf(_("Changes from %s to %s:\n"),
1200 oid_to_hex(&merge_base),
1201 oid_to_hex(&options.onto->object.oid));
1203 /* We want color (if set), but no pager */
1205 opts.stat_width = -1; /* use full terminal width */
1206 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1207 opts.output_format |=
1208 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1209 opts.detect_rename = DIFF_DETECT_RENAME;
1210 diff_setup_done(&opts);
1211 diff_tree_oid(&merge_base, &options.onto->object.oid,
1213 diffcore_std(&opts);
1217 if (is_interactive(&options))
1220 /* Detach HEAD and reset the tree */
1221 if (options.flags & REBASE_NO_QUIET)
1222 printf(_("First, rewinding head to replay your work on top of "
1225 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1226 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1227 die(_("Could not detach HEAD"));
1228 strbuf_release(&msg);
1230 strbuf_addf(&revisions, "%s..%s",
1231 options.root ? oid_to_hex(&options.onto->object.oid) :
1232 (options.restrict_revision ?
1233 oid_to_hex(&options.restrict_revision->object.oid) :
1234 oid_to_hex(&options.upstream->object.oid)),
1235 oid_to_hex(&options.orig_head));
1237 options.revisions = revisions.buf;
1240 ret = !!run_specific_rebase(&options);
1243 strbuf_release(&revisions);
1244 free(options.head_name);
1245 free(options.gpg_sign_opt);