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;
102 static int is_interactive(struct rebase_options *opts)
104 return opts->type == REBASE_INTERACTIVE ||
105 opts->type == REBASE_PRESERVE_MERGES;
108 static void imply_interactive(struct rebase_options *opts, const char *option)
110 switch (opts->type) {
112 die(_("%s requires an interactive rebase"), option);
114 case REBASE_INTERACTIVE:
115 case REBASE_PRESERVE_MERGES:
118 /* we silently *upgrade* --merge to --interactive if needed */
120 opts->type = REBASE_INTERACTIVE; /* implied */
125 /* Returns the filename prefixed by the state_dir */
126 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
128 static struct strbuf path = STRBUF_INIT;
129 static size_t prefix_len;
132 strbuf_addf(&path, "%s/", opts->state_dir);
133 prefix_len = path.len;
136 strbuf_setlen(&path, prefix_len);
137 strbuf_addstr(&path, filename);
141 /* Read one file, then strip line endings */
142 static int read_one(const char *path, struct strbuf *buf)
144 if (strbuf_read_file(buf, path, 0) < 0)
145 return error_errno(_("could not read '%s'"), path);
146 strbuf_trim_trailing_newline(buf);
150 /* Initialize the rebase options from the state directory. */
151 static int read_basic_state(struct rebase_options *opts)
153 struct strbuf head_name = STRBUF_INIT;
154 struct strbuf buf = STRBUF_INIT;
155 struct object_id oid;
157 if (read_one(state_dir_path("head-name", opts), &head_name) ||
158 read_one(state_dir_path("onto", opts), &buf))
160 opts->head_name = starts_with(head_name.buf, "refs/") ?
161 xstrdup(head_name.buf) : NULL;
162 strbuf_release(&head_name);
163 if (get_oid(buf.buf, &oid))
164 return error(_("could not get 'onto': '%s'"), buf.buf);
165 opts->onto = lookup_commit_or_die(&oid, buf.buf);
168 * We always write to orig-head, but interactive rebase used to write to
169 * head. Fall back to reading from head to cover for the case that the
170 * user upgraded git with an ongoing interactive rebase.
173 if (file_exists(state_dir_path("orig-head", opts))) {
174 if (read_one(state_dir_path("orig-head", opts), &buf))
176 } else if (read_one(state_dir_path("head", opts), &buf))
178 if (get_oid(buf.buf, &opts->orig_head))
179 return error(_("invalid orig-head: '%s'"), buf.buf);
182 if (read_one(state_dir_path("quiet", opts), &buf))
185 opts->flags &= ~REBASE_NO_QUIET;
187 opts->flags |= REBASE_NO_QUIET;
189 if (file_exists(state_dir_path("verbose", opts)))
190 opts->flags |= REBASE_VERBOSE;
192 if (file_exists(state_dir_path("signoff", opts))) {
194 opts->flags |= REBASE_FORCE;
197 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
199 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
202 if (!strcmp(buf.buf, "--rerere-autoupdate"))
203 opts->allow_rerere_autoupdate = 1;
204 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
205 opts->allow_rerere_autoupdate = 0;
207 warning(_("ignoring invalid allow_rerere_autoupdate: "
210 opts->allow_rerere_autoupdate = -1;
212 strbuf_release(&buf);
217 static int finish_rebase(struct rebase_options *opts)
219 struct strbuf dir = STRBUF_INIT;
220 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
222 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
224 close_all_packs(the_repository->objects);
226 * We ignore errors in 'gc --auto', since the
227 * user should see them.
229 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
230 strbuf_addstr(&dir, opts->state_dir);
231 remove_dir_recursively(&dir, 0);
232 strbuf_release(&dir);
237 static struct commit *peel_committish(const char *name)
240 struct object_id oid;
242 if (get_oid(name, &oid))
244 obj = parse_object(the_repository, &oid);
245 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
248 static void add_var(struct strbuf *buf, const char *name, const char *value)
251 strbuf_addf(buf, "unset %s; ", name);
253 strbuf_addf(buf, "%s=", name);
254 sq_quote_buf(buf, value);
255 strbuf_addstr(buf, "; ");
259 static int run_specific_rebase(struct rebase_options *opts)
261 const char *argv[] = { NULL, NULL };
262 struct strbuf script_snippet = STRBUF_INIT;
264 const char *backend, *backend_func;
266 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
267 add_var(&script_snippet, "state_dir", opts->state_dir);
269 add_var(&script_snippet, "upstream_name", opts->upstream_name);
270 add_var(&script_snippet, "upstream", opts->upstream ?
271 oid_to_hex(&opts->upstream->object.oid) : NULL);
272 add_var(&script_snippet, "head_name",
273 opts->head_name ? opts->head_name : "detached HEAD");
274 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
275 add_var(&script_snippet, "onto", opts->onto ?
276 oid_to_hex(&opts->onto->object.oid) : NULL);
277 add_var(&script_snippet, "onto_name", opts->onto_name);
278 add_var(&script_snippet, "revisions", opts->revisions);
279 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
280 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
281 add_var(&script_snippet, "GIT_QUIET",
282 opts->flags & REBASE_NO_QUIET ? "" : "t");
283 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
284 add_var(&script_snippet, "verbose",
285 opts->flags & REBASE_VERBOSE ? "t" : "");
286 add_var(&script_snippet, "diffstat",
287 opts->flags & REBASE_DIFFSTAT ? "t" : "");
288 add_var(&script_snippet, "force_rebase",
289 opts->flags & REBASE_FORCE ? "t" : "");
291 add_var(&script_snippet, "switch_to", opts->switch_to);
292 add_var(&script_snippet, "action", opts->action ? opts->action : "");
293 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
294 add_var(&script_snippet, "allow_rerere_autoupdate",
295 opts->allow_rerere_autoupdate < 0 ? "" :
296 opts->allow_rerere_autoupdate ?
297 "--rerere-autoupdate" : "--no-rerere-autoupdate");
298 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
299 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
301 switch (opts->type) {
303 backend = "git-rebase--am";
304 backend_func = "git_rebase__am";
306 case REBASE_INTERACTIVE:
307 backend = "git-rebase--interactive";
308 backend_func = "git_rebase__interactive";
311 backend = "git-rebase--merge";
312 backend_func = "git_rebase__merge";
314 case REBASE_PRESERVE_MERGES:
315 backend = "git-rebase--preserve-merges";
316 backend_func = "git_rebase__preserve_merges";
319 BUG("Unhandled rebase type %d", opts->type);
323 strbuf_addf(&script_snippet,
324 ". git-sh-setup && . git-rebase--common &&"
325 " . %s && %s", backend, backend_func);
326 argv[0] = script_snippet.buf;
328 status = run_command_v_opt(argv, RUN_USING_SHELL);
329 if (opts->dont_finish_rebase)
331 else if (status == 0) {
332 if (!file_exists(state_dir_path("stopped-sha", opts)))
334 } else if (status == 2) {
335 struct strbuf dir = STRBUF_INIT;
338 strbuf_addstr(&dir, opts->state_dir);
339 remove_dir_recursively(&dir, 0);
340 strbuf_release(&dir);
341 die("Nothing to do");
344 strbuf_release(&script_snippet);
346 return status ? -1 : 0;
349 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
351 static int reset_head(struct object_id *oid, const char *action,
352 const char *switch_to_branch, int detach_head)
354 struct object_id head_oid;
355 struct tree_desc desc;
356 struct lock_file lock = LOCK_INIT;
357 struct unpack_trees_options unpack_tree_opts;
359 const char *reflog_action;
360 struct strbuf msg = STRBUF_INIT;
362 struct object_id *orig = NULL, oid_orig,
363 *old_orig = NULL, oid_old_orig;
366 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
367 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
369 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
373 if (get_oid("HEAD", &head_oid)) {
374 rollback_lock_file(&lock);
375 return error(_("could not determine HEAD revision"));
380 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
381 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
382 unpack_tree_opts.head_idx = 1;
383 unpack_tree_opts.src_index = the_repository->index;
384 unpack_tree_opts.dst_index = the_repository->index;
385 unpack_tree_opts.fn = oneway_merge;
386 unpack_tree_opts.update = 1;
387 unpack_tree_opts.merge = 1;
389 unpack_tree_opts.reset = 1;
391 if (read_index_unmerged(the_repository->index) < 0) {
392 rollback_lock_file(&lock);
393 return error(_("could not read index"));
396 if (!fill_tree_descriptor(&desc, oid)) {
397 error(_("failed to find tree of %s"), oid_to_hex(oid));
398 rollback_lock_file(&lock);
399 free((void *)desc.buffer);
403 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
404 rollback_lock_file(&lock);
405 free((void *)desc.buffer);
409 tree = parse_tree_indirect(oid);
410 prime_cache_tree(the_repository->index, tree);
412 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
413 ret = error(_("could not write index"));
414 free((void *)desc.buffer);
419 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
420 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
421 prefix_len = msg.len;
423 if (!get_oid("ORIG_HEAD", &oid_old_orig))
424 old_orig = &oid_old_orig;
425 if (!get_oid("HEAD", &oid_orig)) {
427 strbuf_addstr(&msg, "updating ORIG_HEAD");
428 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
429 UPDATE_REFS_MSG_ON_ERR);
431 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
432 strbuf_setlen(&msg, prefix_len);
433 strbuf_addstr(&msg, "updating HEAD");
434 if (!switch_to_branch)
435 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
436 UPDATE_REFS_MSG_ON_ERR);
438 ret = create_symref("HEAD", switch_to_branch, msg.buf);
440 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
441 UPDATE_REFS_MSG_ON_ERR);
444 strbuf_release(&msg);
448 static int rebase_config(const char *var, const char *value, void *data)
450 struct rebase_options *opts = data;
452 if (!strcmp(var, "rebase.stat")) {
453 if (git_config_bool(var, value))
454 opts->flags |= REBASE_DIFFSTAT;
456 opts->flags &= !REBASE_DIFFSTAT;
460 if (!strcmp(var, "rebase.autosquash")) {
461 opts->autosquash = git_config_bool(var, value);
465 return git_default_config(var, value, data);
469 * Determines whether the commits in from..to are linear, i.e. contain
470 * no merge commits. This function *expects* `from` to be an ancestor of
473 static int is_linear_history(struct commit *from, struct commit *to)
475 while (to && to != from) {
479 if (to->parents->next)
481 to = to->parents->item;
486 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
487 struct object_id *merge_base)
489 struct commit *head = lookup_commit(the_repository, head_oid);
490 struct commit_list *merge_bases;
496 merge_bases = get_merge_bases(onto, head);
497 if (merge_bases && !merge_bases->next) {
498 oidcpy(merge_base, &merge_bases->item->object.oid);
499 res = !oidcmp(merge_base, &onto->object.oid);
501 oidcpy(merge_base, &null_oid);
504 free_commit_list(merge_bases);
505 return res && is_linear_history(onto, head);
508 /* -i followed by -m is still -i */
509 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
511 struct rebase_options *opts = opt->value;
513 if (!is_interactive(opts))
514 opts->type = REBASE_MERGE;
519 /* -i followed by -p is still explicitly interactive, but -p alone is not */
520 static int parse_opt_interactive(const struct option *opt, const char *arg,
523 struct rebase_options *opts = opt->value;
525 opts->type = REBASE_INTERACTIVE;
526 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
531 int cmd_rebase(int argc, const char **argv, const char *prefix)
533 struct rebase_options options = {
534 .type = REBASE_UNSPECIFIED,
535 .flags = REBASE_NO_QUIET,
536 .git_am_opt = STRBUF_INIT,
537 .allow_rerere_autoupdate = -1,
539 const char *branch_name;
540 int ret, flags, total_argc, in_progress = 0;
541 int ok_to_skip_pre_rebase = 0;
542 struct strbuf msg = STRBUF_INIT;
543 struct strbuf revisions = STRBUF_INIT;
544 struct strbuf buf = STRBUF_INIT;
545 struct object_id merge_base;
553 ACTION_SHOW_CURRENT_PATCH,
554 } action = NO_ACTION;
555 int committer_date_is_author_date = 0;
557 int ignore_whitespace = 0;
558 struct option builtin_rebase_options[] = {
559 OPT_STRING(0, "onto", &options.onto_name,
561 N_("rebase onto given branch instead of upstream")),
562 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
563 N_("allow pre-rebase hook to run")),
564 OPT_NEGBIT('q', "quiet", &options.flags,
565 N_("be quiet. implies --no-stat"),
566 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
567 OPT_BIT('v', "verbose", &options.flags,
568 N_("display a diffstat of what changed upstream"),
569 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
570 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
571 N_("do not show diffstat of what changed upstream"),
572 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
573 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
574 N_("passed to 'git apply'")),
575 OPT_BOOL(0, "signoff", &options.signoff,
576 N_("add a Signed-off-by: line to each commit")),
577 OPT_BOOL(0, "committer-date-is-author-date",
578 &committer_date_is_author_date,
579 N_("passed to 'git am'")),
580 OPT_BOOL(0, "ignore-date", &ignore_date,
581 N_("passed to 'git am'")),
582 OPT_BIT('f', "force-rebase", &options.flags,
583 N_("cherry-pick all commits, even if unchanged"),
585 OPT_BIT(0, "no-ff", &options.flags,
586 N_("cherry-pick all commits, even if unchanged"),
588 OPT_CMDMODE(0, "continue", &action, N_("continue"),
590 OPT_CMDMODE(0, "skip", &action,
591 N_("skip current patch and continue"), ACTION_SKIP),
592 OPT_CMDMODE(0, "abort", &action,
593 N_("abort and check out the original branch"),
595 OPT_CMDMODE(0, "quit", &action,
596 N_("abort but keep HEAD where it is"), ACTION_QUIT),
597 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
598 "during an interactive rebase"), ACTION_EDIT_TODO),
599 OPT_CMDMODE(0, "show-current-patch", &action,
600 N_("show the patch file being applied or merged"),
601 ACTION_SHOW_CURRENT_PATCH),
602 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
603 N_("use merging strategies to rebase"),
604 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
606 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
607 N_("let the user edit the list of commits to rebase"),
608 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
609 parse_opt_interactive },
610 OPT_SET_INT('p', "preserve-merges", &options.type,
611 N_("try to recreate merges instead of ignoring "
612 "them"), REBASE_PRESERVE_MERGES),
613 OPT_BOOL(0, "rerere-autoupdate",
614 &options.allow_rerere_autoupdate,
615 N_("allow rerere to update index with resolved "
617 OPT_BOOL('k', "keep-empty", &options.keep_empty,
618 N_("preserve empty commits during rebase")),
619 OPT_BOOL(0, "autosquash", &options.autosquash,
620 N_("move commits that begin with "
621 "squash!/fixup! under -i")),
626 * NEEDSWORK: Once the builtin rebase has been tested enough
627 * and git-legacy-rebase.sh is retired to contrib/, this preamble
631 if (!use_builtin_rebase()) {
632 const char *path = mkpath("%s/git-legacy-rebase",
635 if (sane_execvp(path, (char **)argv) < 0)
636 die_errno(_("could not exec %s"), path);
638 BUG("sane_execvp() returned???");
641 if (argc == 2 && !strcmp(argv[1], "-h"))
642 usage_with_options(builtin_rebase_usage,
643 builtin_rebase_options);
645 prefix = setup_git_directory();
646 trace_repo_setup(prefix);
649 git_config(rebase_config, &options);
652 strbuf_addf(&buf, "%s/applying", apply_dir());
653 if(file_exists(buf.buf))
654 die(_("It looks like 'git am' is in progress. Cannot rebase."));
656 if (is_directory(apply_dir())) {
657 options.type = REBASE_AM;
658 options.state_dir = apply_dir();
659 } else if (is_directory(merge_dir())) {
661 strbuf_addf(&buf, "%s/rewritten", merge_dir());
662 if (is_directory(buf.buf)) {
663 options.type = REBASE_PRESERVE_MERGES;
664 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
667 strbuf_addf(&buf, "%s/interactive", merge_dir());
668 if(file_exists(buf.buf)) {
669 options.type = REBASE_INTERACTIVE;
670 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
672 options.type = REBASE_MERGE;
674 options.state_dir = merge_dir();
677 if (options.type != REBASE_UNSPECIFIED)
681 argc = parse_options(argc, argv, prefix,
682 builtin_rebase_options,
683 builtin_rebase_usage, 0);
685 if (action != NO_ACTION && total_argc != 2) {
686 usage_with_options(builtin_rebase_usage,
687 builtin_rebase_options);
691 usage_with_options(builtin_rebase_usage,
692 builtin_rebase_options);
694 if (action != NO_ACTION && !in_progress)
695 die(_("No rebase in progress?"));
697 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
698 die(_("The --edit-todo action can only be used during "
699 "interactive rebase."));
702 case ACTION_CONTINUE: {
703 struct object_id head;
704 struct lock_file lock_file = LOCK_INIT;
707 options.action = "continue";
710 if (get_oid("HEAD", &head))
711 die(_("Cannot read HEAD"));
713 fd = hold_locked_index(&lock_file, 0);
714 if (read_index(the_repository->index) < 0)
715 die(_("could not read index"));
716 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
719 update_index_if_able(the_repository->index,
721 rollback_lock_file(&lock_file);
723 if (has_unstaged_changes(1)) {
724 puts(_("You must edit all merge conflicts and then\n"
725 "mark them as resolved using git add"));
728 if (read_basic_state(&options))
733 struct string_list merge_rr = STRING_LIST_INIT_DUP;
735 options.action = "skip";
737 rerere_clear(&merge_rr);
738 string_list_clear(&merge_rr, 1);
740 if (reset_head(NULL, "reset", NULL, 0) < 0)
741 die(_("could not discard worktree changes"));
742 if (read_basic_state(&options))
747 struct string_list merge_rr = STRING_LIST_INIT_DUP;
748 options.action = "abort";
750 rerere_clear(&merge_rr);
751 string_list_clear(&merge_rr, 1);
753 if (read_basic_state(&options))
755 if (reset_head(&options.orig_head, "reset",
756 options.head_name, 0) < 0)
757 die(_("could not move back to %s"),
758 oid_to_hex(&options.orig_head));
759 ret = finish_rebase(&options);
764 strbuf_addstr(&buf, options.state_dir);
765 ret = !!remove_dir_recursively(&buf, 0);
767 die(_("could not remove '%s'"), options.state_dir);
770 case ACTION_EDIT_TODO:
771 options.action = "edit-todo";
772 options.dont_finish_rebase = 1;
774 case ACTION_SHOW_CURRENT_PATCH:
775 options.action = "show-current-patch";
776 options.dont_finish_rebase = 1;
781 BUG("action: %d", action);
784 /* Make sure no rebase is in progress */
786 const char *last_slash = strrchr(options.state_dir, '/');
787 const char *state_dir_base =
788 last_slash ? last_slash + 1 : options.state_dir;
789 const char *cmd_live_rebase =
790 "git rebase (--continue | --abort | --skip)";
792 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
793 die(_("It seems that there is already a %s directory, and\n"
794 "I wonder if you are in the middle of another rebase. "
796 "case, please try\n\t%s\n"
797 "If that is not the case, please\n\t%s\n"
798 "and run me again. I am stopping in case you still "
800 "valuable there.\n"),
801 state_dir_base, cmd_live_rebase, buf.buf);
804 if (!(options.flags & REBASE_NO_QUIET))
805 strbuf_addstr(&options.git_am_opt, " -q");
807 if (committer_date_is_author_date) {
808 strbuf_addstr(&options.git_am_opt,
809 " --committer-date-is-author-date");
810 options.flags |= REBASE_FORCE;
813 if (ignore_whitespace)
814 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
817 strbuf_addstr(&options.git_am_opt, " --ignore-date");
818 options.flags |= REBASE_FORCE;
821 if (options.keep_empty)
822 imply_interactive(&options, "--keep-empty");
824 switch (options.type) {
826 case REBASE_INTERACTIVE:
827 case REBASE_PRESERVE_MERGES:
828 options.state_dir = merge_dir();
831 options.state_dir = apply_dir();
834 /* the default rebase backend is `--am` */
835 options.type = REBASE_AM;
836 options.state_dir = apply_dir();
840 if (options.signoff) {
841 if (options.type == REBASE_PRESERVE_MERGES)
842 die("cannot combine '--signoff' with "
843 "'--preserve-merges'");
844 strbuf_addstr(&options.git_am_opt, " --signoff");
845 options.flags |= REBASE_FORCE;
850 die("TODO: handle @{upstream}");
852 options.upstream_name = argv[0];
855 if (!strcmp(options.upstream_name, "-"))
856 options.upstream_name = "@{-1}";
858 options.upstream = peel_committish(options.upstream_name);
859 if (!options.upstream)
860 die(_("invalid upstream '%s'"), options.upstream_name);
861 options.upstream_arg = options.upstream_name;
863 die("TODO: upstream for --root");
865 /* Make sure the branch to rebase onto is valid. */
866 if (!options.onto_name)
867 options.onto_name = options.upstream_name;
868 if (strstr(options.onto_name, "...")) {
869 if (get_oid_mb(options.onto_name, &merge_base) < 0)
870 die(_("'%s': need exactly one merge base"),
872 options.onto = lookup_commit_or_die(&merge_base,
875 options.onto = peel_committish(options.onto_name);
877 die(_("Does not point to a valid commit '%s'"),
882 * If the branch to rebase is given, that is the branch we will rebase
883 * branch_name -- branch/commit being rebased, or
884 * HEAD (already detached)
885 * orig_head -- commit object name of tip of the branch before rebasing
886 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
889 /* Is it "rebase other branchname" or "rebase other commit"? */
890 branch_name = argv[0];
891 options.switch_to = argv[0];
893 /* Is it a local branch? */
895 strbuf_addf(&buf, "refs/heads/%s", branch_name);
896 if (!read_ref(buf.buf, &options.orig_head))
897 options.head_name = xstrdup(buf.buf);
898 /* If not is it a valid ref (branch or commit)? */
899 else if (!get_oid(branch_name, &options.orig_head))
900 options.head_name = NULL;
902 die(_("fatal: no such branch/commit '%s'"),
904 } else if (argc == 0) {
905 /* Do not need to switch branches, we are already on it. */
907 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
909 if (!options.head_name)
910 die(_("No such ref: %s"), "HEAD");
911 if (flags & REF_ISSYMREF) {
912 if (!skip_prefix(options.head_name,
913 "refs/heads/", &branch_name))
914 branch_name = options.head_name;
917 free(options.head_name);
918 options.head_name = NULL;
919 branch_name = "HEAD";
921 if (get_oid("HEAD", &options.orig_head))
922 die(_("Could not resolve HEAD to a revision"));
924 BUG("unexpected number of arguments left to parse");
926 if (read_index(the_repository->index) < 0)
927 die(_("could not read index"));
929 if (require_clean_work_tree("rebase",
930 _("Please commit or stash them."), 1, 1)) {
936 * Now we are rebasing commits upstream..orig_head (or with --root,
937 * everything leading up to orig_head) on top of onto.
941 * Check if we are already based on onto with linear history,
942 * but this should be done only when upstream and onto are the same
943 * and if this is not an interactive rebase.
945 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
946 !is_interactive(&options) && !options.restrict_revision &&
947 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
950 if (!(options.flags & REBASE_FORCE)) {
951 /* Lazily switch to the target branch if needed... */
952 if (options.switch_to) {
953 struct object_id oid;
955 if (get_oid(options.switch_to, &oid) < 0) {
956 ret = !!error(_("could not parse '%s'"),
962 strbuf_addf(&buf, "rebase: checkout %s",
964 if (reset_head(&oid, "checkout",
965 options.head_name, 0) < 0) {
966 ret = !!error(_("could not switch to "
973 if (!(options.flags & REBASE_NO_QUIET))
975 else if (!strcmp(branch_name, "HEAD") &&
976 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
977 puts(_("HEAD is up to date."));
979 printf(_("Current branch %s is up to date.\n"),
981 ret = !!finish_rebase(&options);
983 } else if (!(options.flags & REBASE_NO_QUIET))
985 else if (!strcmp(branch_name, "HEAD") &&
986 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
987 puts(_("HEAD is up to date, rebase forced."));
989 printf(_("Current branch %s is up to date, rebase "
990 "forced.\n"), branch_name);
993 /* If a hook exists, give it a chance to interrupt*/
994 if (!ok_to_skip_pre_rebase &&
995 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
996 argc ? argv[0] : NULL, NULL))
997 die(_("The pre-rebase hook refused to rebase."));
999 if (options.flags & REBASE_DIFFSTAT) {
1000 struct diff_options opts;
1002 if (options.flags & REBASE_VERBOSE)
1003 printf(_("Changes from %s to %s:\n"),
1004 oid_to_hex(&merge_base),
1005 oid_to_hex(&options.onto->object.oid));
1007 /* We want color (if set), but no pager */
1009 opts.stat_width = -1; /* use full terminal width */
1010 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1011 opts.output_format |=
1012 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1013 opts.detect_rename = DIFF_DETECT_RENAME;
1014 diff_setup_done(&opts);
1015 diff_tree_oid(&merge_base, &options.onto->object.oid,
1017 diffcore_std(&opts);
1021 if (is_interactive(&options))
1024 /* Detach HEAD and reset the tree */
1025 if (options.flags & REBASE_NO_QUIET)
1026 printf(_("First, rewinding head to replay your work on top of "
1029 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1030 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1031 die(_("Could not detach HEAD"));
1032 strbuf_release(&msg);
1034 strbuf_addf(&revisions, "%s..%s",
1035 options.root ? oid_to_hex(&options.onto->object.oid) :
1036 (options.restrict_revision ?
1037 oid_to_hex(&options.restrict_revision->object.oid) :
1038 oid_to_hex(&options.upstream->object.oid)),
1039 oid_to_hex(&options.orig_head));
1041 options.revisions = revisions.buf;
1044 ret = !!run_specific_rebase(&options);
1047 strbuf_release(&revisions);
1048 free(options.head_name);