builtin rebase: support --continue
[git/debian.git] / builtin / rebase.c
blobf112d91d678b0556ea97cd1ac946ff4116dccdbe
1 /*
2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
5 */
7 #include "builtin.h"
8 #include "run-command.h"
9 #include "exec-cmd.h"
10 #include "argv-array.h"
11 #include "dir.h"
12 #include "packfile.h"
13 #include "refs.h"
14 #include "quote.h"
15 #include "config.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
18 #include "lockfile.h"
19 #include "parse-options.h"
20 #include "commit.h"
21 #include "diff.h"
22 #include "wt-status.h"
23 #include "revision.h"
25 static char const * const builtin_rebase_usage[] = {
26 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
27 "[<upstream>] [<branch>]"),
28 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
29 "--root [<branch>]"),
30 N_("git rebase --continue | --abort | --skip | --edit-todo"),
31 NULL
34 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
35 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
37 enum rebase_type {
38 REBASE_UNSPECIFIED = -1,
39 REBASE_AM,
40 REBASE_MERGE,
41 REBASE_INTERACTIVE,
42 REBASE_PRESERVE_MERGES
45 static int use_builtin_rebase(void)
47 struct child_process cp = CHILD_PROCESS_INIT;
48 struct strbuf out = STRBUF_INIT;
49 int ret;
51 argv_array_pushl(&cp.args,
52 "config", "--bool", "rebase.usebuiltin", NULL);
53 cp.git_cmd = 1;
54 if (capture_command(&cp, &out, 6)) {
55 strbuf_release(&out);
56 return 0;
59 strbuf_trim(&out);
60 ret = !strcmp("true", out.buf);
61 strbuf_release(&out);
62 return ret;
65 static int apply_autostash(void)
67 warning("TODO");
68 return 0;
71 struct rebase_options {
72 enum rebase_type type;
73 const char *state_dir;
74 struct commit *upstream;
75 const char *upstream_name;
76 const char *upstream_arg;
77 char *head_name;
78 struct object_id orig_head;
79 struct commit *onto;
80 const char *onto_name;
81 const char *revisions;
82 const char *switch_to;
83 int root;
84 struct commit *restrict_revision;
85 int dont_finish_rebase;
86 enum {
87 REBASE_NO_QUIET = 1<<0,
88 REBASE_VERBOSE = 1<<1,
89 REBASE_DIFFSTAT = 1<<2,
90 REBASE_FORCE = 1<<3,
91 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
92 } flags;
93 struct strbuf git_am_opt;
94 const char *action;
97 static int is_interactive(struct rebase_options *opts)
99 return opts->type == REBASE_INTERACTIVE ||
100 opts->type == REBASE_PRESERVE_MERGES;
103 /* Returns the filename prefixed by the state_dir */
104 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
106 static struct strbuf path = STRBUF_INIT;
107 static size_t prefix_len;
109 if (!prefix_len) {
110 strbuf_addf(&path, "%s/", opts->state_dir);
111 prefix_len = path.len;
114 strbuf_setlen(&path, prefix_len);
115 strbuf_addstr(&path, filename);
116 return path.buf;
119 /* Read one file, then strip line endings */
120 static int read_one(const char *path, struct strbuf *buf)
122 if (strbuf_read_file(buf, path, 0) < 0)
123 return error_errno(_("could not read '%s'"), path);
124 strbuf_trim_trailing_newline(buf);
125 return 0;
128 /* Initialize the rebase options from the state directory. */
129 static int read_basic_state(struct rebase_options *opts)
131 struct strbuf head_name = STRBUF_INIT;
132 struct strbuf buf = STRBUF_INIT;
133 struct object_id oid;
135 if (read_one(state_dir_path("head-name", opts), &head_name) ||
136 read_one(state_dir_path("onto", opts), &buf))
137 return -1;
138 opts->head_name = starts_with(head_name.buf, "refs/") ?
139 xstrdup(head_name.buf) : NULL;
140 strbuf_release(&head_name);
141 if (get_oid(buf.buf, &oid))
142 return error(_("could not get 'onto': '%s'"), buf.buf);
143 opts->onto = lookup_commit_or_die(&oid, buf.buf);
146 * We always write to orig-head, but interactive rebase used to write to
147 * head. Fall back to reading from head to cover for the case that the
148 * user upgraded git with an ongoing interactive rebase.
150 strbuf_reset(&buf);
151 if (file_exists(state_dir_path("orig-head", opts))) {
152 if (read_one(state_dir_path("orig-head", opts), &buf))
153 return -1;
154 } else if (read_one(state_dir_path("head", opts), &buf))
155 return -1;
156 if (get_oid(buf.buf, &opts->orig_head))
157 return error(_("invalid orig-head: '%s'"), buf.buf);
159 strbuf_reset(&buf);
160 if (read_one(state_dir_path("quiet", opts), &buf))
161 return -1;
162 if (buf.len)
163 opts->flags &= ~REBASE_NO_QUIET;
164 else
165 opts->flags |= REBASE_NO_QUIET;
167 if (file_exists(state_dir_path("verbose", opts)))
168 opts->flags |= REBASE_VERBOSE;
170 strbuf_release(&buf);
172 return 0;
175 static int finish_rebase(struct rebase_options *opts)
177 struct strbuf dir = STRBUF_INIT;
178 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
180 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
181 apply_autostash();
182 close_all_packs(the_repository->objects);
184 * We ignore errors in 'gc --auto', since the
185 * user should see them.
187 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
188 strbuf_addstr(&dir, opts->state_dir);
189 remove_dir_recursively(&dir, 0);
190 strbuf_release(&dir);
192 return 0;
195 static struct commit *peel_committish(const char *name)
197 struct object *obj;
198 struct object_id oid;
200 if (get_oid(name, &oid))
201 return NULL;
202 obj = parse_object(the_repository, &oid);
203 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
206 static void add_var(struct strbuf *buf, const char *name, const char *value)
208 if (!value)
209 strbuf_addf(buf, "unset %s; ", name);
210 else {
211 strbuf_addf(buf, "%s=", name);
212 sq_quote_buf(buf, value);
213 strbuf_addstr(buf, "; ");
217 static int run_specific_rebase(struct rebase_options *opts)
219 const char *argv[] = { NULL, NULL };
220 struct strbuf script_snippet = STRBUF_INIT;
221 int status;
222 const char *backend, *backend_func;
224 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
225 add_var(&script_snippet, "state_dir", opts->state_dir);
227 add_var(&script_snippet, "upstream_name", opts->upstream_name);
228 add_var(&script_snippet, "upstream", opts->upstream ?
229 oid_to_hex(&opts->upstream->object.oid) : NULL);
230 add_var(&script_snippet, "head_name",
231 opts->head_name ? opts->head_name : "detached HEAD");
232 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
233 add_var(&script_snippet, "onto", opts->onto ?
234 oid_to_hex(&opts->onto->object.oid) : NULL);
235 add_var(&script_snippet, "onto_name", opts->onto_name);
236 add_var(&script_snippet, "revisions", opts->revisions);
237 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
238 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
239 add_var(&script_snippet, "GIT_QUIET",
240 opts->flags & REBASE_NO_QUIET ? "" : "t");
241 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
242 add_var(&script_snippet, "verbose",
243 opts->flags & REBASE_VERBOSE ? "t" : "");
244 add_var(&script_snippet, "diffstat",
245 opts->flags & REBASE_DIFFSTAT ? "t" : "");
246 add_var(&script_snippet, "force_rebase",
247 opts->flags & REBASE_FORCE ? "t" : "");
248 if (opts->switch_to)
249 add_var(&script_snippet, "switch_to", opts->switch_to);
250 add_var(&script_snippet, "action", opts->action ? opts->action : "");
252 switch (opts->type) {
253 case REBASE_AM:
254 backend = "git-rebase--am";
255 backend_func = "git_rebase__am";
256 break;
257 case REBASE_INTERACTIVE:
258 backend = "git-rebase--interactive";
259 backend_func = "git_rebase__interactive";
260 break;
261 case REBASE_MERGE:
262 backend = "git-rebase--merge";
263 backend_func = "git_rebase__merge";
264 break;
265 case REBASE_PRESERVE_MERGES:
266 backend = "git-rebase--preserve-merges";
267 backend_func = "git_rebase__preserve_merges";
268 break;
269 default:
270 BUG("Unhandled rebase type %d", opts->type);
271 break;
274 strbuf_addf(&script_snippet,
275 ". git-sh-setup && . git-rebase--common &&"
276 " . %s && %s", backend, backend_func);
277 argv[0] = script_snippet.buf;
279 status = run_command_v_opt(argv, RUN_USING_SHELL);
280 if (opts->dont_finish_rebase)
281 ; /* do nothing */
282 else if (status == 0) {
283 if (!file_exists(state_dir_path("stopped-sha", opts)))
284 finish_rebase(opts);
285 } else if (status == 2) {
286 struct strbuf dir = STRBUF_INIT;
288 apply_autostash();
289 strbuf_addstr(&dir, opts->state_dir);
290 remove_dir_recursively(&dir, 0);
291 strbuf_release(&dir);
292 die("Nothing to do");
295 strbuf_release(&script_snippet);
297 return status ? -1 : 0;
300 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
302 static int reset_head(struct object_id *oid, const char *action,
303 const char *switch_to_branch, int detach_head)
305 struct object_id head_oid;
306 struct tree_desc desc;
307 struct lock_file lock = LOCK_INIT;
308 struct unpack_trees_options unpack_tree_opts;
309 struct tree *tree;
310 const char *reflog_action;
311 struct strbuf msg = STRBUF_INIT;
312 size_t prefix_len;
313 struct object_id *orig = NULL, oid_orig,
314 *old_orig = NULL, oid_old_orig;
315 int ret = 0;
317 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
318 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
320 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
321 return -1;
323 if (!oid) {
324 if (get_oid("HEAD", &head_oid)) {
325 rollback_lock_file(&lock);
326 return error(_("could not determine HEAD revision"));
328 oid = &head_oid;
331 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
332 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
333 unpack_tree_opts.head_idx = 1;
334 unpack_tree_opts.src_index = the_repository->index;
335 unpack_tree_opts.dst_index = the_repository->index;
336 unpack_tree_opts.fn = oneway_merge;
337 unpack_tree_opts.update = 1;
338 unpack_tree_opts.merge = 1;
339 if (!detach_head)
340 unpack_tree_opts.reset = 1;
342 if (read_index_unmerged(the_repository->index) < 0) {
343 rollback_lock_file(&lock);
344 return error(_("could not read index"));
347 if (!fill_tree_descriptor(&desc, oid)) {
348 error(_("failed to find tree of %s"), oid_to_hex(oid));
349 rollback_lock_file(&lock);
350 free((void *)desc.buffer);
351 return -1;
354 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
355 rollback_lock_file(&lock);
356 free((void *)desc.buffer);
357 return -1;
360 tree = parse_tree_indirect(oid);
361 prime_cache_tree(the_repository->index, tree);
363 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
364 ret = error(_("could not write index"));
365 free((void *)desc.buffer);
367 if (ret)
368 return ret;
370 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
371 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
372 prefix_len = msg.len;
374 if (!get_oid("ORIG_HEAD", &oid_old_orig))
375 old_orig = &oid_old_orig;
376 if (!get_oid("HEAD", &oid_orig)) {
377 orig = &oid_orig;
378 strbuf_addstr(&msg, "updating ORIG_HEAD");
379 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
380 UPDATE_REFS_MSG_ON_ERR);
381 } else if (old_orig)
382 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
383 strbuf_setlen(&msg, prefix_len);
384 strbuf_addstr(&msg, "updating HEAD");
385 if (!switch_to_branch)
386 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
387 UPDATE_REFS_MSG_ON_ERR);
388 else {
389 ret = create_symref("HEAD", switch_to_branch, msg.buf);
390 if (!ret)
391 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
392 UPDATE_REFS_MSG_ON_ERR);
395 strbuf_release(&msg);
396 return ret;
399 static int rebase_config(const char *var, const char *value, void *data)
401 struct rebase_options *opts = data;
403 if (!strcmp(var, "rebase.stat")) {
404 if (git_config_bool(var, value))
405 opts->flags |= REBASE_DIFFSTAT;
406 else
407 opts->flags &= !REBASE_DIFFSTAT;
408 return 0;
411 return git_default_config(var, value, data);
415 * Determines whether the commits in from..to are linear, i.e. contain
416 * no merge commits. This function *expects* `from` to be an ancestor of
417 * `to`.
419 static int is_linear_history(struct commit *from, struct commit *to)
421 while (to && to != from) {
422 parse_commit(to);
423 if (!to->parents)
424 return 1;
425 if (to->parents->next)
426 return 0;
427 to = to->parents->item;
429 return 1;
432 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
433 struct object_id *merge_base)
435 struct commit *head = lookup_commit(the_repository, head_oid);
436 struct commit_list *merge_bases;
437 int res;
439 if (!head)
440 return 0;
442 merge_bases = get_merge_bases(onto, head);
443 if (merge_bases && !merge_bases->next) {
444 oidcpy(merge_base, &merge_bases->item->object.oid);
445 res = !oidcmp(merge_base, &onto->object.oid);
446 } else {
447 oidcpy(merge_base, &null_oid);
448 res = 0;
450 free_commit_list(merge_bases);
451 return res && is_linear_history(onto, head);
454 int cmd_rebase(int argc, const char **argv, const char *prefix)
456 struct rebase_options options = {
457 .type = REBASE_UNSPECIFIED,
458 .flags = REBASE_NO_QUIET,
459 .git_am_opt = STRBUF_INIT,
461 const char *branch_name;
462 int ret, flags, total_argc, in_progress = 0;
463 int ok_to_skip_pre_rebase = 0;
464 struct strbuf msg = STRBUF_INIT;
465 struct strbuf revisions = STRBUF_INIT;
466 struct strbuf buf = STRBUF_INIT;
467 struct object_id merge_base;
468 enum {
469 NO_ACTION,
470 ACTION_CONTINUE,
471 } action = NO_ACTION;
472 struct option builtin_rebase_options[] = {
473 OPT_STRING(0, "onto", &options.onto_name,
474 N_("revision"),
475 N_("rebase onto given branch instead of upstream")),
476 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
477 N_("allow pre-rebase hook to run")),
478 OPT_NEGBIT('q', "quiet", &options.flags,
479 N_("be quiet. implies --no-stat"),
480 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
481 OPT_BIT('v', "verbose", &options.flags,
482 N_("display a diffstat of what changed upstream"),
483 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
484 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
485 N_("do not show diffstat of what changed upstream"),
486 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
487 OPT_BIT('f', "force-rebase", &options.flags,
488 N_("cherry-pick all commits, even if unchanged"),
489 REBASE_FORCE),
490 OPT_BIT(0, "no-ff", &options.flags,
491 N_("cherry-pick all commits, even if unchanged"),
492 REBASE_FORCE),
493 OPT_CMDMODE(0, "continue", &action, N_("continue"),
494 ACTION_CONTINUE),
495 OPT_END(),
499 * NEEDSWORK: Once the builtin rebase has been tested enough
500 * and git-legacy-rebase.sh is retired to contrib/, this preamble
501 * can be removed.
504 if (!use_builtin_rebase()) {
505 const char *path = mkpath("%s/git-legacy-rebase",
506 git_exec_path());
508 if (sane_execvp(path, (char **)argv) < 0)
509 die_errno(_("could not exec %s"), path);
510 else
511 BUG("sane_execvp() returned???");
514 if (argc == 2 && !strcmp(argv[1], "-h"))
515 usage_with_options(builtin_rebase_usage,
516 builtin_rebase_options);
518 prefix = setup_git_directory();
519 trace_repo_setup(prefix);
520 setup_work_tree();
522 git_config(rebase_config, &options);
524 if (is_directory(apply_dir())) {
525 options.type = REBASE_AM;
526 options.state_dir = apply_dir();
527 } else if (is_directory(merge_dir())) {
528 strbuf_reset(&buf);
529 strbuf_addf(&buf, "%s/rewritten", merge_dir());
530 if (is_directory(buf.buf)) {
531 options.type = REBASE_PRESERVE_MERGES;
532 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
533 } else {
534 strbuf_reset(&buf);
535 strbuf_addf(&buf, "%s/interactive", merge_dir());
536 if(file_exists(buf.buf)) {
537 options.type = REBASE_INTERACTIVE;
538 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
539 } else
540 options.type = REBASE_MERGE;
542 options.state_dir = merge_dir();
545 if (options.type != REBASE_UNSPECIFIED)
546 in_progress = 1;
548 total_argc = argc;
549 argc = parse_options(argc, argv, prefix,
550 builtin_rebase_options,
551 builtin_rebase_usage, 0);
553 if (action != NO_ACTION && total_argc != 2) {
554 usage_with_options(builtin_rebase_usage,
555 builtin_rebase_options);
558 if (argc > 2)
559 usage_with_options(builtin_rebase_usage,
560 builtin_rebase_options);
562 switch (action) {
563 case ACTION_CONTINUE: {
564 struct object_id head;
565 struct lock_file lock_file = LOCK_INIT;
566 int fd;
568 options.action = "continue";
570 /* Sanity check */
571 if (get_oid("HEAD", &head))
572 die(_("Cannot read HEAD"));
574 fd = hold_locked_index(&lock_file, 0);
575 if (read_index(the_repository->index) < 0)
576 die(_("could not read index"));
577 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
578 NULL);
579 if (0 <= fd)
580 update_index_if_able(the_repository->index,
581 &lock_file);
582 rollback_lock_file(&lock_file);
584 if (has_unstaged_changes(1)) {
585 puts(_("You must edit all merge conflicts and then\n"
586 "mark them as resolved using git add"));
587 exit(1);
589 if (read_basic_state(&options))
590 exit(1);
591 goto run_rebase;
593 default:
594 die("TODO");
597 /* Make sure no rebase is in progress */
598 if (in_progress) {
599 const char *last_slash = strrchr(options.state_dir, '/');
600 const char *state_dir_base =
601 last_slash ? last_slash + 1 : options.state_dir;
602 const char *cmd_live_rebase =
603 "git rebase (--continue | --abort | --skip)";
604 strbuf_reset(&buf);
605 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
606 die(_("It seems that there is already a %s directory, and\n"
607 "I wonder if you are in the middle of another rebase. "
608 "If that is the\n"
609 "case, please try\n\t%s\n"
610 "If that is not the case, please\n\t%s\n"
611 "and run me again. I am stopping in case you still "
612 "have something\n"
613 "valuable there.\n"),
614 state_dir_base, cmd_live_rebase, buf.buf);
617 if (!(options.flags & REBASE_NO_QUIET))
618 strbuf_addstr(&options.git_am_opt, " -q");
620 switch (options.type) {
621 case REBASE_MERGE:
622 case REBASE_INTERACTIVE:
623 case REBASE_PRESERVE_MERGES:
624 options.state_dir = merge_dir();
625 break;
626 case REBASE_AM:
627 options.state_dir = apply_dir();
628 break;
629 default:
630 /* the default rebase backend is `--am` */
631 options.type = REBASE_AM;
632 options.state_dir = apply_dir();
633 break;
636 if (!options.root) {
637 if (argc < 1)
638 die("TODO: handle @{upstream}");
639 else {
640 options.upstream_name = argv[0];
641 argc--;
642 argv++;
643 if (!strcmp(options.upstream_name, "-"))
644 options.upstream_name = "@{-1}";
646 options.upstream = peel_committish(options.upstream_name);
647 if (!options.upstream)
648 die(_("invalid upstream '%s'"), options.upstream_name);
649 options.upstream_arg = options.upstream_name;
650 } else
651 die("TODO: upstream for --root");
653 /* Make sure the branch to rebase onto is valid. */
654 if (!options.onto_name)
655 options.onto_name = options.upstream_name;
656 if (strstr(options.onto_name, "...")) {
657 if (get_oid_mb(options.onto_name, &merge_base) < 0)
658 die(_("'%s': need exactly one merge base"),
659 options.onto_name);
660 options.onto = lookup_commit_or_die(&merge_base,
661 options.onto_name);
662 } else {
663 options.onto = peel_committish(options.onto_name);
664 if (!options.onto)
665 die(_("Does not point to a valid commit '%s'"),
666 options.onto_name);
670 * If the branch to rebase is given, that is the branch we will rebase
671 * branch_name -- branch/commit being rebased, or
672 * HEAD (already detached)
673 * orig_head -- commit object name of tip of the branch before rebasing
674 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
676 if (argc == 1) {
677 /* Is it "rebase other branchname" or "rebase other commit"? */
678 branch_name = argv[0];
679 options.switch_to = argv[0];
681 /* Is it a local branch? */
682 strbuf_reset(&buf);
683 strbuf_addf(&buf, "refs/heads/%s", branch_name);
684 if (!read_ref(buf.buf, &options.orig_head))
685 options.head_name = xstrdup(buf.buf);
686 /* If not is it a valid ref (branch or commit)? */
687 else if (!get_oid(branch_name, &options.orig_head))
688 options.head_name = NULL;
689 else
690 die(_("fatal: no such branch/commit '%s'"),
691 branch_name);
692 } else if (argc == 0) {
693 /* Do not need to switch branches, we are already on it. */
694 options.head_name =
695 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
696 &flags));
697 if (!options.head_name)
698 die(_("No such ref: %s"), "HEAD");
699 if (flags & REF_ISSYMREF) {
700 if (!skip_prefix(options.head_name,
701 "refs/heads/", &branch_name))
702 branch_name = options.head_name;
704 } else {
705 free(options.head_name);
706 options.head_name = NULL;
707 branch_name = "HEAD";
709 if (get_oid("HEAD", &options.orig_head))
710 die(_("Could not resolve HEAD to a revision"));
711 } else
712 BUG("unexpected number of arguments left to parse");
714 if (read_index(the_repository->index) < 0)
715 die(_("could not read index"));
717 if (require_clean_work_tree("rebase",
718 _("Please commit or stash them."), 1, 1)) {
719 ret = 1;
720 goto cleanup;
724 * Now we are rebasing commits upstream..orig_head (or with --root,
725 * everything leading up to orig_head) on top of onto.
729 * Check if we are already based on onto with linear history,
730 * but this should be done only when upstream and onto are the same
731 * and if this is not an interactive rebase.
733 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
734 !is_interactive(&options) && !options.restrict_revision &&
735 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
736 int flag;
738 if (!(options.flags & REBASE_FORCE)) {
739 /* Lazily switch to the target branch if needed... */
740 if (options.switch_to) {
741 struct object_id oid;
743 if (get_oid(options.switch_to, &oid) < 0) {
744 ret = !!error(_("could not parse '%s'"),
745 options.switch_to);
746 goto cleanup;
749 strbuf_reset(&buf);
750 strbuf_addf(&buf, "rebase: checkout %s",
751 options.switch_to);
752 if (reset_head(&oid, "checkout",
753 options.head_name, 0) < 0) {
754 ret = !!error(_("could not switch to "
755 "%s"),
756 options.switch_to);
757 goto cleanup;
761 if (!(options.flags & REBASE_NO_QUIET))
762 ; /* be quiet */
763 else if (!strcmp(branch_name, "HEAD") &&
764 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
765 puts(_("HEAD is up to date."));
766 else
767 printf(_("Current branch %s is up to date.\n"),
768 branch_name);
769 ret = !!finish_rebase(&options);
770 goto cleanup;
771 } else if (!(options.flags & REBASE_NO_QUIET))
772 ; /* be quiet */
773 else if (!strcmp(branch_name, "HEAD") &&
774 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
775 puts(_("HEAD is up to date, rebase forced."));
776 else
777 printf(_("Current branch %s is up to date, rebase "
778 "forced.\n"), branch_name);
781 /* If a hook exists, give it a chance to interrupt*/
782 if (!ok_to_skip_pre_rebase &&
783 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
784 argc ? argv[0] : NULL, NULL))
785 die(_("The pre-rebase hook refused to rebase."));
787 if (options.flags & REBASE_DIFFSTAT) {
788 struct diff_options opts;
790 if (options.flags & REBASE_VERBOSE)
791 printf(_("Changes from %s to %s:\n"),
792 oid_to_hex(&merge_base),
793 oid_to_hex(&options.onto->object.oid));
795 /* We want color (if set), but no pager */
796 diff_setup(&opts);
797 opts.stat_width = -1; /* use full terminal width */
798 opts.stat_graph_width = -1; /* respect statGraphWidth config */
799 opts.output_format |=
800 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
801 opts.detect_rename = DIFF_DETECT_RENAME;
802 diff_setup_done(&opts);
803 diff_tree_oid(&merge_base, &options.onto->object.oid,
804 "", &opts);
805 diffcore_std(&opts);
806 diff_flush(&opts);
809 /* Detach HEAD and reset the tree */
810 if (options.flags & REBASE_NO_QUIET)
811 printf(_("First, rewinding head to replay your work on top of "
812 "it...\n"));
814 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
815 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
816 die(_("Could not detach HEAD"));
817 strbuf_release(&msg);
819 strbuf_addf(&revisions, "%s..%s",
820 options.root ? oid_to_hex(&options.onto->object.oid) :
821 (options.restrict_revision ?
822 oid_to_hex(&options.restrict_revision->object.oid) :
823 oid_to_hex(&options.upstream->object.oid)),
824 oid_to_hex(&options.orig_head));
826 options.revisions = revisions.buf;
828 run_rebase:
829 ret = !!run_specific_rebase(&options);
831 cleanup:
832 strbuf_release(&revisions);
833 free(options.head_name);
834 return ret;