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"
24 #include "commit-reach.h"
28 static char const * const builtin_rebase_usage[] = {
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
30 "[<upstream>] [<branch>]"),
31 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
33 N_("git rebase --continue | --abort | --skip | --edit-todo"),
37 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
38 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
41 REBASE_UNSPECIFIED = -1,
45 REBASE_PRESERVE_MERGES
48 static int use_builtin_rebase(void)
50 struct child_process cp = CHILD_PROCESS_INIT;
51 struct strbuf out = STRBUF_INIT;
52 int ret, env = git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
57 argv_array_pushl(&cp.args,
58 "config", "--bool", "rebase.usebuiltin", NULL);
60 if (capture_command(&cp, &out, 6)) {
66 ret = !strcmp("true", out.buf);
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;
78 struct object_id orig_head;
80 const char *onto_name;
81 const char *revisions;
82 const char *switch_to;
84 struct object_id *squash_onto;
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 argv_array git_am_opts;
97 int allow_rerere_autoupdate;
103 int allow_empty_message;
104 int rebase_merges, rebase_cousins;
105 char *strategy, *strategy_opts;
106 struct strbuf git_format_patch_opt;
109 static int is_interactive(struct rebase_options *opts)
111 return opts->type == REBASE_INTERACTIVE ||
112 opts->type == REBASE_PRESERVE_MERGES;
115 static void imply_interactive(struct rebase_options *opts, const char *option)
117 switch (opts->type) {
119 die(_("%s requires an interactive rebase"), option);
121 case REBASE_INTERACTIVE:
122 case REBASE_PRESERVE_MERGES:
125 /* we silently *upgrade* --merge to --interactive if needed */
127 opts->type = REBASE_INTERACTIVE; /* implied */
132 /* Returns the filename prefixed by the state_dir */
133 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
135 static struct strbuf path = STRBUF_INIT;
136 static size_t prefix_len;
139 strbuf_addf(&path, "%s/", opts->state_dir);
140 prefix_len = path.len;
143 strbuf_setlen(&path, prefix_len);
144 strbuf_addstr(&path, filename);
148 /* Read one file, then strip line endings */
149 static int read_one(const char *path, struct strbuf *buf)
151 if (strbuf_read_file(buf, path, 0) < 0)
152 return error_errno(_("could not read '%s'"), path);
153 strbuf_trim_trailing_newline(buf);
157 /* Initialize the rebase options from the state directory. */
158 static int read_basic_state(struct rebase_options *opts)
160 struct strbuf head_name = STRBUF_INIT;
161 struct strbuf buf = STRBUF_INIT;
162 struct object_id oid;
164 if (read_one(state_dir_path("head-name", opts), &head_name) ||
165 read_one(state_dir_path("onto", opts), &buf))
167 opts->head_name = starts_with(head_name.buf, "refs/") ?
168 xstrdup(head_name.buf) : NULL;
169 strbuf_release(&head_name);
170 if (get_oid(buf.buf, &oid))
171 return error(_("could not get 'onto': '%s'"), buf.buf);
172 opts->onto = lookup_commit_or_die(&oid, buf.buf);
175 * We always write to orig-head, but interactive rebase used to write to
176 * head. Fall back to reading from head to cover for the case that the
177 * user upgraded git with an ongoing interactive rebase.
180 if (file_exists(state_dir_path("orig-head", opts))) {
181 if (read_one(state_dir_path("orig-head", opts), &buf))
183 } else if (read_one(state_dir_path("head", opts), &buf))
185 if (get_oid(buf.buf, &opts->orig_head))
186 return error(_("invalid orig-head: '%s'"), buf.buf);
189 if (read_one(state_dir_path("quiet", opts), &buf))
192 opts->flags &= ~REBASE_NO_QUIET;
194 opts->flags |= REBASE_NO_QUIET;
196 if (file_exists(state_dir_path("verbose", opts)))
197 opts->flags |= REBASE_VERBOSE;
199 if (file_exists(state_dir_path("signoff", opts))) {
201 opts->flags |= REBASE_FORCE;
204 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
206 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
209 if (!strcmp(buf.buf, "--rerere-autoupdate"))
210 opts->allow_rerere_autoupdate = 1;
211 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
212 opts->allow_rerere_autoupdate = 0;
214 warning(_("ignoring invalid allow_rerere_autoupdate: "
217 opts->allow_rerere_autoupdate = -1;
219 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
221 if (read_one(state_dir_path("gpg_sign_opt", opts),
224 free(opts->gpg_sign_opt);
225 opts->gpg_sign_opt = xstrdup(buf.buf);
228 if (file_exists(state_dir_path("strategy", opts))) {
230 if (read_one(state_dir_path("strategy", opts), &buf))
232 free(opts->strategy);
233 opts->strategy = xstrdup(buf.buf);
236 if (file_exists(state_dir_path("strategy_opts", opts))) {
238 if (read_one(state_dir_path("strategy_opts", opts), &buf))
240 free(opts->strategy_opts);
241 opts->strategy_opts = xstrdup(buf.buf);
244 strbuf_release(&buf);
249 static int apply_autostash(struct rebase_options *opts)
251 const char *path = state_dir_path("autostash", opts);
252 struct strbuf autostash = STRBUF_INIT;
253 struct child_process stash_apply = CHILD_PROCESS_INIT;
255 if (!file_exists(path))
258 if (read_one(path, &autostash))
259 return error(_("Could not read '%s'"), path);
260 /* Ensure that the hash is not mistaken for a number */
261 strbuf_addstr(&autostash, "^0");
262 argv_array_pushl(&stash_apply.args,
263 "stash", "apply", autostash.buf, NULL);
264 stash_apply.git_cmd = 1;
265 stash_apply.no_stderr = stash_apply.no_stdout =
266 stash_apply.no_stdin = 1;
267 if (!run_command(&stash_apply))
268 printf(_("Applied autostash.\n"));
270 struct argv_array args = ARGV_ARRAY_INIT;
273 argv_array_pushl(&args,
274 "stash", "store", "-m", "autostash", "-q",
275 autostash.buf, NULL);
276 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
277 res = error(_("Cannot store %s"), autostash.buf);
278 argv_array_clear(&args);
279 strbuf_release(&autostash);
284 _("Applying autostash resulted in conflicts.\n"
285 "Your changes are safe in the stash.\n"
286 "You can run \"git stash pop\" or \"git stash drop\" "
290 strbuf_release(&autostash);
294 static int finish_rebase(struct rebase_options *opts)
296 struct strbuf dir = STRBUF_INIT;
297 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
299 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
300 apply_autostash(opts);
301 close_all_packs(the_repository->objects);
303 * We ignore errors in 'gc --auto', since the
304 * user should see them.
306 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
307 strbuf_addstr(&dir, opts->state_dir);
308 remove_dir_recursively(&dir, 0);
309 strbuf_release(&dir);
314 static struct commit *peel_committish(const char *name)
317 struct object_id oid;
319 if (get_oid(name, &oid))
321 obj = parse_object(the_repository, &oid);
322 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
325 static void add_var(struct strbuf *buf, const char *name, const char *value)
328 strbuf_addf(buf, "unset %s; ", name);
330 strbuf_addf(buf, "%s=", name);
331 sq_quote_buf(buf, value);
332 strbuf_addstr(buf, "; ");
336 static const char *resolvemsg =
337 N_("Resolve all conflicts manually, mark them as resolved with\n"
338 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
339 "You can instead skip this commit: run \"git rebase --skip\".\n"
340 "To abort and get back to the state before \"git rebase\", run "
341 "\"git rebase --abort\".");
343 static int run_specific_rebase(struct rebase_options *opts)
345 const char *argv[] = { NULL, NULL };
346 struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
348 const char *backend, *backend_func;
350 if (opts->type == REBASE_INTERACTIVE) {
351 /* Run builtin interactive rebase */
352 struct child_process child = CHILD_PROCESS_INIT;
354 argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
356 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
357 argv_array_push(&child.env_array, "GIT_EDITOR=:");
358 opts->autosquash = 0;
362 argv_array_push(&child.args, "rebase--interactive");
365 argv_array_pushf(&child.args, "--%s", opts->action);
366 if (opts->keep_empty)
367 argv_array_push(&child.args, "--keep-empty");
368 if (opts->rebase_merges)
369 argv_array_push(&child.args, "--rebase-merges");
370 if (opts->rebase_cousins)
371 argv_array_push(&child.args, "--rebase-cousins");
372 if (opts->autosquash)
373 argv_array_push(&child.args, "--autosquash");
374 if (opts->flags & REBASE_VERBOSE)
375 argv_array_push(&child.args, "--verbose");
376 if (opts->flags & REBASE_FORCE)
377 argv_array_push(&child.args, "--no-ff");
378 if (opts->restrict_revision)
379 argv_array_pushf(&child.args,
380 "--restrict-revision=^%s",
381 oid_to_hex(&opts->restrict_revision->object.oid));
383 argv_array_pushf(&child.args, "--upstream=%s",
384 oid_to_hex(&opts->upstream->object.oid));
386 argv_array_pushf(&child.args, "--onto=%s",
387 oid_to_hex(&opts->onto->object.oid));
388 if (opts->squash_onto)
389 argv_array_pushf(&child.args, "--squash-onto=%s",
390 oid_to_hex(opts->squash_onto));
392 argv_array_pushf(&child.args, "--onto-name=%s",
394 argv_array_pushf(&child.args, "--head-name=%s",
396 opts->head_name : "detached HEAD");
398 argv_array_pushf(&child.args, "--strategy=%s",
400 if (opts->strategy_opts)
401 argv_array_pushf(&child.args, "--strategy-opts=%s",
402 opts->strategy_opts);
404 argv_array_pushf(&child.args, "--switch-to=%s",
407 argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
408 if (opts->allow_empty_message)
409 argv_array_push(&child.args, "--allow-empty-message");
410 if (opts->allow_rerere_autoupdate > 0)
411 argv_array_push(&child.args, "--rerere-autoupdate");
412 else if (opts->allow_rerere_autoupdate == 0)
413 argv_array_push(&child.args, "--no-rerere-autoupdate");
414 if (opts->gpg_sign_opt)
415 argv_array_push(&child.args, opts->gpg_sign_opt);
417 argv_array_push(&child.args, "--signoff");
419 status = run_command(&child);
420 goto finished_rebase;
423 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
424 add_var(&script_snippet, "state_dir", opts->state_dir);
426 add_var(&script_snippet, "upstream_name", opts->upstream_name);
427 add_var(&script_snippet, "upstream", opts->upstream ?
428 oid_to_hex(&opts->upstream->object.oid) : NULL);
429 add_var(&script_snippet, "head_name",
430 opts->head_name ? opts->head_name : "detached HEAD");
431 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
432 add_var(&script_snippet, "onto", opts->onto ?
433 oid_to_hex(&opts->onto->object.oid) : NULL);
434 add_var(&script_snippet, "onto_name", opts->onto_name);
435 add_var(&script_snippet, "revisions", opts->revisions);
436 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
437 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
438 add_var(&script_snippet, "GIT_QUIET",
439 opts->flags & REBASE_NO_QUIET ? "" : "t");
440 sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
441 add_var(&script_snippet, "git_am_opt", buf.buf);
442 strbuf_release(&buf);
443 add_var(&script_snippet, "verbose",
444 opts->flags & REBASE_VERBOSE ? "t" : "");
445 add_var(&script_snippet, "diffstat",
446 opts->flags & REBASE_DIFFSTAT ? "t" : "");
447 add_var(&script_snippet, "force_rebase",
448 opts->flags & REBASE_FORCE ? "t" : "");
450 add_var(&script_snippet, "switch_to", opts->switch_to);
451 add_var(&script_snippet, "action", opts->action ? opts->action : "");
452 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
453 add_var(&script_snippet, "allow_rerere_autoupdate",
454 opts->allow_rerere_autoupdate < 0 ? "" :
455 opts->allow_rerere_autoupdate ?
456 "--rerere-autoupdate" : "--no-rerere-autoupdate");
457 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
458 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
459 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
460 add_var(&script_snippet, "cmd", opts->cmd);
461 add_var(&script_snippet, "allow_empty_message",
462 opts->allow_empty_message ? "--allow-empty-message" : "");
463 add_var(&script_snippet, "rebase_merges",
464 opts->rebase_merges ? "t" : "");
465 add_var(&script_snippet, "rebase_cousins",
466 opts->rebase_cousins ? "t" : "");
467 add_var(&script_snippet, "strategy", opts->strategy);
468 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
469 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
470 add_var(&script_snippet, "squash_onto",
471 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
472 add_var(&script_snippet, "git_format_patch_opt",
473 opts->git_format_patch_opt.buf);
475 if (is_interactive(opts) &&
476 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
477 strbuf_addstr(&script_snippet,
478 "GIT_EDITOR=:; export GIT_EDITOR; ");
479 opts->autosquash = 0;
482 switch (opts->type) {
484 backend = "git-rebase--am";
485 backend_func = "git_rebase__am";
488 backend = "git-rebase--merge";
489 backend_func = "git_rebase__merge";
491 case REBASE_PRESERVE_MERGES:
492 backend = "git-rebase--preserve-merges";
493 backend_func = "git_rebase__preserve_merges";
496 BUG("Unhandled rebase type %d", opts->type);
500 strbuf_addf(&script_snippet,
501 ". git-sh-setup && . git-rebase--common &&"
502 " . %s && %s", backend, backend_func);
503 argv[0] = script_snippet.buf;
505 status = run_command_v_opt(argv, RUN_USING_SHELL);
507 if (opts->dont_finish_rebase)
509 else if (opts->type == REBASE_INTERACTIVE)
510 ; /* interactive rebase cleans up after itself */
511 else if (status == 0) {
512 if (!file_exists(state_dir_path("stopped-sha", opts)))
514 } else if (status == 2) {
515 struct strbuf dir = STRBUF_INIT;
517 apply_autostash(opts);
518 strbuf_addstr(&dir, opts->state_dir);
519 remove_dir_recursively(&dir, 0);
520 strbuf_release(&dir);
521 die("Nothing to do");
524 strbuf_release(&script_snippet);
526 return status ? -1 : 0;
529 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
531 #define RESET_HEAD_DETACH (1<<0)
532 #define RESET_HEAD_HARD (1<<1)
534 static int reset_head(struct object_id *oid, const char *action,
535 const char *switch_to_branch, unsigned flags,
536 const char *reflog_orig_head, const char *reflog_head)
538 unsigned detach_head = flags & RESET_HEAD_DETACH;
539 unsigned reset_hard = flags & RESET_HEAD_HARD;
540 struct object_id head_oid;
541 struct tree_desc desc[2] = { { NULL }, { NULL } };
542 struct lock_file lock = LOCK_INIT;
543 struct unpack_trees_options unpack_tree_opts;
545 const char *reflog_action;
546 struct strbuf msg = STRBUF_INIT;
548 struct object_id *orig = NULL, oid_orig,
549 *old_orig = NULL, oid_old_orig;
552 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
553 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
555 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
557 goto leave_reset_head;
560 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
561 ret = error(_("could not determine HEAD revision"));
562 goto leave_reset_head;
568 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
569 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
570 unpack_tree_opts.head_idx = 1;
571 unpack_tree_opts.src_index = the_repository->index;
572 unpack_tree_opts.dst_index = the_repository->index;
573 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
574 unpack_tree_opts.update = 1;
575 unpack_tree_opts.merge = 1;
577 unpack_tree_opts.reset = 1;
579 if (read_index_unmerged(the_repository->index) < 0) {
580 ret = error(_("could not read index"));
581 goto leave_reset_head;
584 if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
585 ret = error(_("failed to find tree of %s"),
586 oid_to_hex(&head_oid));
587 goto leave_reset_head;
590 if (!fill_tree_descriptor(&desc[nr++], oid)) {
591 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
592 goto leave_reset_head;
595 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
597 goto leave_reset_head;
600 tree = parse_tree_indirect(oid);
601 prime_cache_tree(the_repository->index, tree);
603 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
604 ret = error(_("could not write index"));
605 goto leave_reset_head;
608 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
609 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
610 prefix_len = msg.len;
612 if (!get_oid("ORIG_HEAD", &oid_old_orig))
613 old_orig = &oid_old_orig;
614 if (!get_oid("HEAD", &oid_orig)) {
616 if (!reflog_orig_head) {
617 strbuf_addstr(&msg, "updating ORIG_HEAD");
618 reflog_orig_head = msg.buf;
620 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
621 UPDATE_REFS_MSG_ON_ERR);
623 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
625 strbuf_setlen(&msg, prefix_len);
626 strbuf_addstr(&msg, "updating HEAD");
627 reflog_head = msg.buf;
629 if (!switch_to_branch)
630 ret = update_ref(reflog_head, "HEAD", oid, orig,
631 detach_head ? REF_NO_DEREF : 0,
632 UPDATE_REFS_MSG_ON_ERR);
634 ret = create_symref("HEAD", switch_to_branch, msg.buf);
636 ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
637 UPDATE_REFS_MSG_ON_ERR);
641 strbuf_release(&msg);
642 rollback_lock_file(&lock);
644 free((void *)desc[--nr].buffer);
648 static int rebase_config(const char *var, const char *value, void *data)
650 struct rebase_options *opts = data;
652 if (!strcmp(var, "rebase.stat")) {
653 if (git_config_bool(var, value))
654 opts->flags |= REBASE_DIFFSTAT;
656 opts->flags &= !REBASE_DIFFSTAT;
660 if (!strcmp(var, "rebase.autosquash")) {
661 opts->autosquash = git_config_bool(var, value);
665 if (!strcmp(var, "commit.gpgsign")) {
666 free(opts->gpg_sign_opt);
667 opts->gpg_sign_opt = git_config_bool(var, value) ?
668 xstrdup("-S") : NULL;
672 if (!strcmp(var, "rebase.autostash")) {
673 opts->autostash = git_config_bool(var, value);
677 return git_default_config(var, value, data);
681 * Determines whether the commits in from..to are linear, i.e. contain
682 * no merge commits. This function *expects* `from` to be an ancestor of
685 static int is_linear_history(struct commit *from, struct commit *to)
687 while (to && to != from) {
691 if (to->parents->next)
693 to = to->parents->item;
698 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
699 struct object_id *merge_base)
701 struct commit *head = lookup_commit(the_repository, head_oid);
702 struct commit_list *merge_bases;
708 merge_bases = get_merge_bases(onto, head);
709 if (merge_bases && !merge_bases->next) {
710 oidcpy(merge_base, &merge_bases->item->object.oid);
711 res = oideq(merge_base, &onto->object.oid);
713 oidcpy(merge_base, &null_oid);
716 free_commit_list(merge_bases);
717 return res && is_linear_history(onto, head);
720 /* -i followed by -m is still -i */
721 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
723 struct rebase_options *opts = opt->value;
725 BUG_ON_OPT_NEG(unset);
728 if (!is_interactive(opts))
729 opts->type = REBASE_MERGE;
734 /* -i followed by -p is still explicitly interactive, but -p alone is not */
735 static int parse_opt_interactive(const struct option *opt, const char *arg,
738 struct rebase_options *opts = opt->value;
740 BUG_ON_OPT_NEG(unset);
743 opts->type = REBASE_INTERACTIVE;
744 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
749 static void NORETURN error_on_missing_default_upstream(void)
751 struct branch *current_branch = branch_get(NULL);
754 "Please specify which branch you want to rebase against.\n"
755 "See git-rebase(1) for details.\n"
757 " git rebase '<branch>'\n"
759 current_branch ? _("There is no tracking information for "
760 "the current branch.") :
761 _("You are not currently on a branch."));
763 if (current_branch) {
764 const char *remote = current_branch->remote_name;
767 remote = _("<remote>");
769 printf(_("If you wish to set tracking information for this "
770 "branch you can do so with:\n"
772 " git branch --set-upstream-to=%s/<branch> %s\n"
774 remote, current_branch->name);
779 static void set_reflog_action(struct rebase_options *options)
782 struct strbuf buf = STRBUF_INIT;
784 if (!is_interactive(options))
787 env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
788 if (env && strcmp("rebase", env))
789 return; /* only override it if it is "rebase" */
791 strbuf_addf(&buf, "rebase -i (%s)", options->action);
792 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
793 strbuf_release(&buf);
796 static int check_exec_cmd(const char *cmd)
798 if (strchr(cmd, '\n'))
799 return error(_("exec commands cannot contain newlines"));
801 /* Does the command consist purely of whitespace? */
802 if (!cmd[strspn(cmd, " \t\r\f\v")])
803 return error(_("empty exec command"));
809 int cmd_rebase(int argc, const char **argv, const char *prefix)
811 struct rebase_options options = {
812 .type = REBASE_UNSPECIFIED,
813 .flags = REBASE_NO_QUIET,
814 .git_am_opts = ARGV_ARRAY_INIT,
815 .allow_rerere_autoupdate = -1,
816 .allow_empty_message = 1,
817 .git_format_patch_opt = STRBUF_INIT,
819 const char *branch_name;
820 int ret, flags, total_argc, in_progress = 0;
821 int ok_to_skip_pre_rebase = 0;
822 struct strbuf msg = STRBUF_INIT;
823 struct strbuf revisions = STRBUF_INIT;
824 struct strbuf buf = STRBUF_INIT;
825 struct object_id merge_base;
833 ACTION_SHOW_CURRENT_PATCH,
834 } action = NO_ACTION;
835 const char *gpg_sign = NULL;
836 struct string_list exec = STRING_LIST_INIT_NODUP;
837 const char *rebase_merges = NULL;
839 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
840 struct object_id squash_onto;
841 char *squash_onto_name = NULL;
842 struct option builtin_rebase_options[] = {
843 OPT_STRING(0, "onto", &options.onto_name,
845 N_("rebase onto given branch instead of upstream")),
846 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
847 N_("allow pre-rebase hook to run")),
848 OPT_NEGBIT('q', "quiet", &options.flags,
849 N_("be quiet. implies --no-stat"),
850 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
851 OPT_BIT('v', "verbose", &options.flags,
852 N_("display a diffstat of what changed upstream"),
853 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
854 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
855 N_("do not show diffstat of what changed upstream"),
856 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
857 OPT_BOOL(0, "signoff", &options.signoff,
858 N_("add a Signed-off-by: line to each commit")),
859 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
860 NULL, N_("passed to 'git am'"),
862 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
863 &options.git_am_opts, NULL,
864 N_("passed to 'git am'"), PARSE_OPT_NOARG),
865 OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
866 N_("passed to 'git am'"), PARSE_OPT_NOARG),
867 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
868 N_("passed to 'git apply'"), 0),
869 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
870 N_("action"), N_("passed to 'git apply'"), 0),
871 OPT_BIT('f', "force-rebase", &options.flags,
872 N_("cherry-pick all commits, even if unchanged"),
874 OPT_BIT(0, "no-ff", &options.flags,
875 N_("cherry-pick all commits, even if unchanged"),
877 OPT_CMDMODE(0, "continue", &action, N_("continue"),
879 OPT_CMDMODE(0, "skip", &action,
880 N_("skip current patch and continue"), ACTION_SKIP),
881 OPT_CMDMODE(0, "abort", &action,
882 N_("abort and check out the original branch"),
884 OPT_CMDMODE(0, "quit", &action,
885 N_("abort but keep HEAD where it is"), ACTION_QUIT),
886 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
887 "during an interactive rebase"), ACTION_EDIT_TODO),
888 OPT_CMDMODE(0, "show-current-patch", &action,
889 N_("show the patch file being applied or merged"),
890 ACTION_SHOW_CURRENT_PATCH),
891 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
892 N_("use merging strategies to rebase"),
893 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
895 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
896 N_("let the user edit the list of commits to rebase"),
897 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
898 parse_opt_interactive },
899 OPT_SET_INT('p', "preserve-merges", &options.type,
900 N_("try to recreate merges instead of ignoring "
901 "them"), REBASE_PRESERVE_MERGES),
902 OPT_BOOL(0, "rerere-autoupdate",
903 &options.allow_rerere_autoupdate,
904 N_("allow rerere to update index with resolved "
906 OPT_BOOL('k', "keep-empty", &options.keep_empty,
907 N_("preserve empty commits during rebase")),
908 OPT_BOOL(0, "autosquash", &options.autosquash,
909 N_("move commits that begin with "
910 "squash!/fixup! under -i")),
911 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
912 N_("GPG-sign commits"),
913 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
914 OPT_BOOL(0, "autostash", &options.autostash,
915 N_("automatically stash/stash pop before and after")),
916 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
917 N_("add exec lines after each commit of the "
919 OPT_BOOL(0, "allow-empty-message",
920 &options.allow_empty_message,
921 N_("allow rebasing commits with empty messages")),
922 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
924 N_("try to rebase merges instead of skipping them"),
925 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
926 OPT_BOOL(0, "fork-point", &fork_point,
927 N_("use 'merge-base --fork-point' to refine upstream")),
928 OPT_STRING('s', "strategy", &options.strategy,
929 N_("strategy"), N_("use the given merge strategy")),
930 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
932 N_("pass the argument through to the merge "
934 OPT_BOOL(0, "root", &options.root,
935 N_("rebase all reachable commits up to the root(s)")),
941 * NEEDSWORK: Once the builtin rebase has been tested enough
942 * and git-legacy-rebase.sh is retired to contrib/, this preamble
946 if (!use_builtin_rebase()) {
947 const char *path = mkpath("%s/git-legacy-rebase",
950 if (sane_execvp(path, (char **)argv) < 0)
951 die_errno(_("could not exec %s"), path);
953 BUG("sane_execvp() returned???");
956 if (argc == 2 && !strcmp(argv[1], "-h"))
957 usage_with_options(builtin_rebase_usage,
958 builtin_rebase_options);
960 prefix = setup_git_directory();
961 trace_repo_setup(prefix);
964 git_config(rebase_config, &options);
967 strbuf_addf(&buf, "%s/applying", apply_dir());
968 if(file_exists(buf.buf))
969 die(_("It looks like 'git am' is in progress. Cannot rebase."));
971 if (is_directory(apply_dir())) {
972 options.type = REBASE_AM;
973 options.state_dir = apply_dir();
974 } else if (is_directory(merge_dir())) {
976 strbuf_addf(&buf, "%s/rewritten", merge_dir());
977 if (is_directory(buf.buf)) {
978 options.type = REBASE_PRESERVE_MERGES;
979 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
982 strbuf_addf(&buf, "%s/interactive", merge_dir());
983 if(file_exists(buf.buf)) {
984 options.type = REBASE_INTERACTIVE;
985 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
987 options.type = REBASE_MERGE;
989 options.state_dir = merge_dir();
992 if (options.type != REBASE_UNSPECIFIED)
996 argc = parse_options(argc, argv, prefix,
997 builtin_rebase_options,
998 builtin_rebase_usage, 0);
1000 if (action != NO_ACTION && total_argc != 2) {
1001 usage_with_options(builtin_rebase_usage,
1002 builtin_rebase_options);
1006 usage_with_options(builtin_rebase_usage,
1007 builtin_rebase_options);
1009 if (action != NO_ACTION && !in_progress)
1010 die(_("No rebase in progress?"));
1011 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1013 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
1014 die(_("The --edit-todo action can only be used during "
1015 "interactive rebase."));
1018 case ACTION_CONTINUE: {
1019 struct object_id head;
1020 struct lock_file lock_file = LOCK_INIT;
1023 options.action = "continue";
1024 set_reflog_action(&options);
1027 if (get_oid("HEAD", &head))
1028 die(_("Cannot read HEAD"));
1030 fd = hold_locked_index(&lock_file, 0);
1031 if (read_index(the_repository->index) < 0)
1032 die(_("could not read index"));
1033 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1036 update_index_if_able(the_repository->index,
1038 rollback_lock_file(&lock_file);
1040 if (has_unstaged_changes(1)) {
1041 puts(_("You must edit all merge conflicts and then\n"
1042 "mark them as resolved using git add"));
1045 if (read_basic_state(&options))
1050 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1052 options.action = "skip";
1053 set_reflog_action(&options);
1055 rerere_clear(&merge_rr);
1056 string_list_clear(&merge_rr, 1);
1058 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1060 die(_("could not discard worktree changes"));
1061 remove_branch_state();
1062 if (read_basic_state(&options))
1066 case ACTION_ABORT: {
1067 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1068 options.action = "abort";
1069 set_reflog_action(&options);
1071 rerere_clear(&merge_rr);
1072 string_list_clear(&merge_rr, 1);
1074 if (read_basic_state(&options))
1076 if (reset_head(&options.orig_head, "reset",
1077 options.head_name, RESET_HEAD_HARD,
1079 die(_("could not move back to %s"),
1080 oid_to_hex(&options.orig_head));
1081 remove_branch_state();
1082 ret = finish_rebase(&options);
1087 strbuf_addstr(&buf, options.state_dir);
1088 ret = !!remove_dir_recursively(&buf, 0);
1090 die(_("could not remove '%s'"), options.state_dir);
1093 case ACTION_EDIT_TODO:
1094 options.action = "edit-todo";
1095 options.dont_finish_rebase = 1;
1097 case ACTION_SHOW_CURRENT_PATCH:
1098 options.action = "show-current-patch";
1099 options.dont_finish_rebase = 1;
1104 BUG("action: %d", action);
1107 /* Make sure no rebase is in progress */
1109 const char *last_slash = strrchr(options.state_dir, '/');
1110 const char *state_dir_base =
1111 last_slash ? last_slash + 1 : options.state_dir;
1112 const char *cmd_live_rebase =
1113 "git rebase (--continue | --abort | --skip)";
1115 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1116 die(_("It seems that there is already a %s directory, and\n"
1117 "I wonder if you are in the middle of another rebase. "
1119 "case, please try\n\t%s\n"
1120 "If that is not the case, please\n\t%s\n"
1121 "and run me again. I am stopping in case you still "
1123 "valuable there.\n"),
1124 state_dir_base, cmd_live_rebase, buf.buf);
1127 for (i = 0; i < options.git_am_opts.argc; i++) {
1128 const char *option = options.git_am_opts.argv[i], *p;
1129 if (!strcmp(option, "--committer-date-is-author-date") ||
1130 !strcmp(option, "--ignore-date") ||
1131 !strcmp(option, "--whitespace=fix") ||
1132 !strcmp(option, "--whitespace=strip"))
1133 options.flags |= REBASE_FORCE;
1134 else if (skip_prefix(option, "-C", &p)) {
1136 if (!isdigit(*(p++)))
1137 die(_("switch `C' expects a "
1138 "numerical value"));
1139 } else if (skip_prefix(option, "--whitespace=", &p)) {
1140 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1141 strcmp(p, "error") && strcmp(p, "error-all"))
1142 die("Invalid whitespace option: '%s'", p);
1146 for (i = 0; i < exec.nr; i++)
1147 if (check_exec_cmd(exec.items[i].string))
1150 if (!(options.flags & REBASE_NO_QUIET))
1151 argv_array_push(&options.git_am_opts, "-q");
1153 if (options.keep_empty)
1154 imply_interactive(&options, "--keep-empty");
1157 free(options.gpg_sign_opt);
1158 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1164 imply_interactive(&options, "--exec");
1167 for (i = 0; i < exec.nr; i++)
1168 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1169 options.cmd = xstrdup(buf.buf);
1172 if (rebase_merges) {
1173 if (!*rebase_merges)
1174 ; /* default mode; do nothing */
1175 else if (!strcmp("rebase-cousins", rebase_merges))
1176 options.rebase_cousins = 1;
1177 else if (strcmp("no-rebase-cousins", rebase_merges))
1178 die(_("Unknown mode: %s"), rebase_merges);
1179 options.rebase_merges = 1;
1180 imply_interactive(&options, "--rebase-merges");
1183 if (strategy_options.nr) {
1186 if (!options.strategy)
1187 options.strategy = "recursive";
1190 for (i = 0; i < strategy_options.nr; i++)
1191 strbuf_addf(&buf, " --%s",
1192 strategy_options.items[i].string);
1193 options.strategy_opts = xstrdup(buf.buf);
1196 if (options.strategy) {
1197 options.strategy = xstrdup(options.strategy);
1198 switch (options.type) {
1200 die(_("--strategy requires --merge or --interactive"));
1202 case REBASE_INTERACTIVE:
1203 case REBASE_PRESERVE_MERGES:
1206 case REBASE_UNSPECIFIED:
1207 options.type = REBASE_MERGE;
1210 BUG("unhandled rebase type (%d)", options.type);
1214 if (options.root && !options.onto_name)
1215 imply_interactive(&options, "--root without --onto");
1217 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1218 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1220 switch (options.type) {
1222 case REBASE_INTERACTIVE:
1223 case REBASE_PRESERVE_MERGES:
1224 options.state_dir = merge_dir();
1227 options.state_dir = apply_dir();
1230 /* the default rebase backend is `--am` */
1231 options.type = REBASE_AM;
1232 options.state_dir = apply_dir();
1236 if (options.git_am_opts.argc) {
1237 /* all am options except -q are compatible only with --am */
1238 for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1239 if (strcmp(options.git_am_opts.argv[i], "-q"))
1242 if (is_interactive(&options) && i >= 0)
1243 die(_("error: cannot combine interactive options "
1244 "(--interactive, --exec, --rebase-merges, "
1245 "--preserve-merges, --keep-empty, --root + "
1246 "--onto) with am options (%s)"), buf.buf);
1247 if (options.type == REBASE_MERGE && i >= 0)
1248 die(_("error: cannot combine merge options (--merge, "
1249 "--strategy, --strategy-option) with am options "
1253 if (options.signoff) {
1254 if (options.type == REBASE_PRESERVE_MERGES)
1255 die("cannot combine '--signoff' with "
1256 "'--preserve-merges'");
1257 argv_array_push(&options.git_am_opts, "--signoff");
1258 options.flags |= REBASE_FORCE;
1261 if (options.type == REBASE_PRESERVE_MERGES)
1263 * Note: incompatibility with --signoff handled in signoff block above
1264 * Note: incompatibility with --interactive is just a strong warning;
1265 * git-rebase.txt caveats with "unless you know what you are doing"
1267 if (options.rebase_merges)
1268 die(_("error: cannot combine '--preserve-merges' with "
1269 "'--rebase-merges'"));
1271 if (options.rebase_merges) {
1272 if (strategy_options.nr)
1273 die(_("error: cannot combine '--rebase-merges' with "
1274 "'--strategy-option'"));
1275 if (options.strategy)
1276 die(_("error: cannot combine '--rebase-merges' with "
1280 if (!options.root) {
1282 struct branch *branch;
1284 branch = branch_get(NULL);
1285 options.upstream_name = branch_get_upstream(branch,
1287 if (!options.upstream_name)
1288 error_on_missing_default_upstream();
1292 options.upstream_name = argv[0];
1295 if (!strcmp(options.upstream_name, "-"))
1296 options.upstream_name = "@{-1}";
1298 options.upstream = peel_committish(options.upstream_name);
1299 if (!options.upstream)
1300 die(_("invalid upstream '%s'"), options.upstream_name);
1301 options.upstream_arg = options.upstream_name;
1303 if (!options.onto_name) {
1304 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1305 &squash_onto, NULL, NULL) < 0)
1306 die(_("Could not create new root commit"));
1307 options.squash_onto = &squash_onto;
1308 options.onto_name = squash_onto_name =
1309 xstrdup(oid_to_hex(&squash_onto));
1311 options.upstream_name = NULL;
1312 options.upstream = NULL;
1314 usage_with_options(builtin_rebase_usage,
1315 builtin_rebase_options);
1316 options.upstream_arg = "--root";
1319 /* Make sure the branch to rebase onto is valid. */
1320 if (!options.onto_name)
1321 options.onto_name = options.upstream_name;
1322 if (strstr(options.onto_name, "...")) {
1323 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1324 die(_("'%s': need exactly one merge base"),
1326 options.onto = lookup_commit_or_die(&merge_base,
1329 options.onto = peel_committish(options.onto_name);
1331 die(_("Does not point to a valid commit '%s'"),
1336 * If the branch to rebase is given, that is the branch we will rebase
1337 * branch_name -- branch/commit being rebased, or
1338 * HEAD (already detached)
1339 * orig_head -- commit object name of tip of the branch before rebasing
1340 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1343 /* Is it "rebase other branchname" or "rebase other commit"? */
1344 branch_name = argv[0];
1345 options.switch_to = argv[0];
1347 /* Is it a local branch? */
1349 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1350 if (!read_ref(buf.buf, &options.orig_head))
1351 options.head_name = xstrdup(buf.buf);
1352 /* If not is it a valid ref (branch or commit)? */
1353 else if (!get_oid(branch_name, &options.orig_head))
1354 options.head_name = NULL;
1356 die(_("fatal: no such branch/commit '%s'"),
1358 } else if (argc == 0) {
1359 /* Do not need to switch branches, we are already on it. */
1361 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1363 if (!options.head_name)
1364 die(_("No such ref: %s"), "HEAD");
1365 if (flags & REF_ISSYMREF) {
1366 if (!skip_prefix(options.head_name,
1367 "refs/heads/", &branch_name))
1368 branch_name = options.head_name;
1371 free(options.head_name);
1372 options.head_name = NULL;
1373 branch_name = "HEAD";
1375 if (get_oid("HEAD", &options.orig_head))
1376 die(_("Could not resolve HEAD to a revision"));
1378 BUG("unexpected number of arguments left to parse");
1380 if (fork_point > 0) {
1381 struct commit *head =
1382 lookup_commit_reference(the_repository,
1383 &options.orig_head);
1384 options.restrict_revision =
1385 get_fork_point(options.upstream_name, head);
1388 if (read_index(the_repository->index) < 0)
1389 die(_("could not read index"));
1391 if (options.autostash) {
1392 struct lock_file lock_file = LOCK_INIT;
1395 fd = hold_locked_index(&lock_file, 0);
1396 refresh_cache(REFRESH_QUIET);
1398 update_index_if_able(&the_index, &lock_file);
1399 rollback_lock_file(&lock_file);
1401 if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1402 const char *autostash =
1403 state_dir_path("autostash", &options);
1404 struct child_process stash = CHILD_PROCESS_INIT;
1405 struct object_id oid;
1406 struct commit *head =
1407 lookup_commit_reference(the_repository,
1408 &options.orig_head);
1410 argv_array_pushl(&stash.args,
1411 "stash", "create", "autostash", NULL);
1415 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1416 die(_("Cannot autostash"));
1417 strbuf_trim_trailing_newline(&buf);
1418 if (get_oid(buf.buf, &oid))
1419 die(_("Unexpected stash response: '%s'"),
1422 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1424 if (safe_create_leading_directories_const(autostash))
1425 die(_("Could not create directory for '%s'"),
1427 write_file(autostash, "%s", oid_to_hex(&oid));
1428 printf(_("Created autostash: %s\n"), buf.buf);
1429 if (reset_head(&head->object.oid, "reset --hard",
1430 NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
1431 die(_("could not reset --hard"));
1432 printf(_("HEAD is now at %s"),
1433 find_unique_abbrev(&head->object.oid,
1436 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1438 printf(" %s", buf.buf);
1441 if (discard_index(the_repository->index) < 0 ||
1442 read_index(the_repository->index) < 0)
1443 die(_("could not read index"));
1447 if (require_clean_work_tree("rebase",
1448 _("Please commit or stash them."), 1, 1)) {
1454 * Now we are rebasing commits upstream..orig_head (or with --root,
1455 * everything leading up to orig_head) on top of onto.
1459 * Check if we are already based on onto with linear history,
1460 * but this should be done only when upstream and onto are the same
1461 * and if this is not an interactive rebase.
1463 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1464 !is_interactive(&options) && !options.restrict_revision &&
1466 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1469 if (!(options.flags & REBASE_FORCE)) {
1470 /* Lazily switch to the target branch if needed... */
1471 if (options.switch_to) {
1472 struct object_id oid;
1474 if (get_oid(options.switch_to, &oid) < 0) {
1475 ret = !!error(_("could not parse '%s'"),
1481 strbuf_addf(&buf, "%s: checkout %s",
1482 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1484 if (reset_head(&oid, "checkout",
1485 options.head_name, 0,
1486 NULL, buf.buf) < 0) {
1487 ret = !!error(_("could not switch to "
1494 if (!(options.flags & REBASE_NO_QUIET))
1496 else if (!strcmp(branch_name, "HEAD") &&
1497 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1498 puts(_("HEAD is up to date."));
1500 printf(_("Current branch %s is up to date.\n"),
1502 ret = !!finish_rebase(&options);
1504 } else if (!(options.flags & REBASE_NO_QUIET))
1506 else if (!strcmp(branch_name, "HEAD") &&
1507 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1508 puts(_("HEAD is up to date, rebase forced."));
1510 printf(_("Current branch %s is up to date, rebase "
1511 "forced.\n"), branch_name);
1514 /* If a hook exists, give it a chance to interrupt*/
1515 if (!ok_to_skip_pre_rebase &&
1516 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1517 argc ? argv[0] : NULL, NULL))
1518 die(_("The pre-rebase hook refused to rebase."));
1520 if (options.flags & REBASE_DIFFSTAT) {
1521 struct diff_options opts;
1523 if (options.flags & REBASE_VERBOSE) {
1524 if (is_null_oid(&merge_base))
1525 printf(_("Changes to %s:\n"),
1526 oid_to_hex(&options.onto->object.oid));
1528 printf(_("Changes from %s to %s:\n"),
1529 oid_to_hex(&merge_base),
1530 oid_to_hex(&options.onto->object.oid));
1533 /* We want color (if set), but no pager */
1535 opts.stat_width = -1; /* use full terminal width */
1536 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1537 opts.output_format |=
1538 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1539 opts.detect_rename = DIFF_DETECT_RENAME;
1540 diff_setup_done(&opts);
1541 diff_tree_oid(is_null_oid(&merge_base) ?
1542 the_hash_algo->empty_tree : &merge_base,
1543 &options.onto->object.oid, "", &opts);
1544 diffcore_std(&opts);
1548 if (is_interactive(&options))
1551 /* Detach HEAD and reset the tree */
1552 if (options.flags & REBASE_NO_QUIET)
1553 printf(_("First, rewinding head to replay your work on top of "
1556 strbuf_addf(&msg, "%s: checkout %s",
1557 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1558 if (reset_head(&options.onto->object.oid, "checkout", NULL,
1559 RESET_HEAD_DETACH, NULL, msg.buf))
1560 die(_("Could not detach HEAD"));
1561 strbuf_release(&msg);
1564 * If the onto is a proper descendant of the tip of the branch, then
1565 * we just fast-forwarded.
1568 if (!oidcmp(&merge_base, &options.orig_head)) {
1569 printf(_("Fast-forwarded %s to %s.\n"),
1570 branch_name, options.onto_name);
1571 strbuf_addf(&msg, "rebase finished: %s onto %s",
1572 options.head_name ? options.head_name : "detached HEAD",
1573 oid_to_hex(&options.onto->object.oid));
1574 reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1576 strbuf_release(&msg);
1577 ret = !!finish_rebase(&options);
1581 strbuf_addf(&revisions, "%s..%s",
1582 options.root ? oid_to_hex(&options.onto->object.oid) :
1583 (options.restrict_revision ?
1584 oid_to_hex(&options.restrict_revision->object.oid) :
1585 oid_to_hex(&options.upstream->object.oid)),
1586 oid_to_hex(&options.orig_head));
1588 options.revisions = revisions.buf;
1591 ret = !!run_specific_rebase(&options);
1594 strbuf_release(&revisions);
1595 free(options.head_name);
1596 free(options.gpg_sign_opt);
1598 free(squash_onto_name);