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;
54 argv_array_pushl(&cp.args,
55 "config", "--bool", "rebase.usebuiltin", NULL);
57 if (capture_command(&cp, &out, 6)) {
63 ret = !strcmp("true", out.buf);
68 struct rebase_options {
69 enum rebase_type type;
70 const char *state_dir;
71 struct commit *upstream;
72 const char *upstream_name;
73 const char *upstream_arg;
75 struct object_id orig_head;
77 const char *onto_name;
78 const char *revisions;
79 const char *switch_to;
81 struct object_id *squash_onto;
82 struct commit *restrict_revision;
83 int dont_finish_rebase;
85 REBASE_NO_QUIET = 1<<0,
86 REBASE_VERBOSE = 1<<1,
87 REBASE_DIFFSTAT = 1<<2,
89 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
91 struct strbuf git_am_opt;
94 int allow_rerere_autoupdate;
100 int allow_empty_message;
101 int rebase_merges, rebase_cousins;
102 char *strategy, *strategy_opts;
103 struct strbuf git_format_patch_opt;
106 static int is_interactive(struct rebase_options *opts)
108 return opts->type == REBASE_INTERACTIVE ||
109 opts->type == REBASE_PRESERVE_MERGES;
112 static void imply_interactive(struct rebase_options *opts, const char *option)
114 switch (opts->type) {
116 die(_("%s requires an interactive rebase"), option);
118 case REBASE_INTERACTIVE:
119 case REBASE_PRESERVE_MERGES:
122 /* we silently *upgrade* --merge to --interactive if needed */
124 opts->type = REBASE_INTERACTIVE; /* implied */
129 /* Returns the filename prefixed by the state_dir */
130 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
132 static struct strbuf path = STRBUF_INIT;
133 static size_t prefix_len;
136 strbuf_addf(&path, "%s/", opts->state_dir);
137 prefix_len = path.len;
140 strbuf_setlen(&path, prefix_len);
141 strbuf_addstr(&path, filename);
145 /* Read one file, then strip line endings */
146 static int read_one(const char *path, struct strbuf *buf)
148 if (strbuf_read_file(buf, path, 0) < 0)
149 return error_errno(_("could not read '%s'"), path);
150 strbuf_trim_trailing_newline(buf);
154 /* Initialize the rebase options from the state directory. */
155 static int read_basic_state(struct rebase_options *opts)
157 struct strbuf head_name = STRBUF_INIT;
158 struct strbuf buf = STRBUF_INIT;
159 struct object_id oid;
161 if (read_one(state_dir_path("head-name", opts), &head_name) ||
162 read_one(state_dir_path("onto", opts), &buf))
164 opts->head_name = starts_with(head_name.buf, "refs/") ?
165 xstrdup(head_name.buf) : NULL;
166 strbuf_release(&head_name);
167 if (get_oid(buf.buf, &oid))
168 return error(_("could not get 'onto': '%s'"), buf.buf);
169 opts->onto = lookup_commit_or_die(&oid, buf.buf);
172 * We always write to orig-head, but interactive rebase used to write to
173 * head. Fall back to reading from head to cover for the case that the
174 * user upgraded git with an ongoing interactive rebase.
177 if (file_exists(state_dir_path("orig-head", opts))) {
178 if (read_one(state_dir_path("orig-head", opts), &buf))
180 } else if (read_one(state_dir_path("head", opts), &buf))
182 if (get_oid(buf.buf, &opts->orig_head))
183 return error(_("invalid orig-head: '%s'"), buf.buf);
186 if (read_one(state_dir_path("quiet", opts), &buf))
189 opts->flags &= ~REBASE_NO_QUIET;
191 opts->flags |= REBASE_NO_QUIET;
193 if (file_exists(state_dir_path("verbose", opts)))
194 opts->flags |= REBASE_VERBOSE;
196 if (file_exists(state_dir_path("signoff", opts))) {
198 opts->flags |= REBASE_FORCE;
201 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
203 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
206 if (!strcmp(buf.buf, "--rerere-autoupdate"))
207 opts->allow_rerere_autoupdate = 1;
208 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
209 opts->allow_rerere_autoupdate = 0;
211 warning(_("ignoring invalid allow_rerere_autoupdate: "
214 opts->allow_rerere_autoupdate = -1;
216 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
218 if (read_one(state_dir_path("gpg_sign_opt", opts),
221 free(opts->gpg_sign_opt);
222 opts->gpg_sign_opt = xstrdup(buf.buf);
225 if (file_exists(state_dir_path("strategy", opts))) {
227 if (read_one(state_dir_path("strategy", opts), &buf))
229 free(opts->strategy);
230 opts->strategy = xstrdup(buf.buf);
233 if (file_exists(state_dir_path("strategy_opts", opts))) {
235 if (read_one(state_dir_path("strategy_opts", opts), &buf))
237 free(opts->strategy_opts);
238 opts->strategy_opts = xstrdup(buf.buf);
241 strbuf_release(&buf);
246 static int apply_autostash(struct rebase_options *opts)
248 const char *path = state_dir_path("autostash", opts);
249 struct strbuf autostash = STRBUF_INIT;
250 struct child_process stash_apply = CHILD_PROCESS_INIT;
252 if (!file_exists(path))
255 if (read_one(path, &autostash))
256 return error(_("Could not read '%s'"), path);
257 /* Ensure that the hash is not mistaken for a number */
258 strbuf_addstr(&autostash, "^0");
259 argv_array_pushl(&stash_apply.args,
260 "stash", "apply", autostash.buf, NULL);
261 stash_apply.git_cmd = 1;
262 stash_apply.no_stderr = stash_apply.no_stdout =
263 stash_apply.no_stdin = 1;
264 if (!run_command(&stash_apply))
265 printf(_("Applied autostash.\n"));
267 struct argv_array args = ARGV_ARRAY_INIT;
270 argv_array_pushl(&args,
271 "stash", "store", "-m", "autostash", "-q",
272 autostash.buf, NULL);
273 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
274 res = error(_("Cannot store %s"), autostash.buf);
275 argv_array_clear(&args);
276 strbuf_release(&autostash);
281 _("Applying autostash resulted in conflicts.\n"
282 "Your changes are safe in the stash.\n"
283 "You can run \"git stash pop\" or \"git stash drop\" "
287 strbuf_release(&autostash);
291 static int finish_rebase(struct rebase_options *opts)
293 struct strbuf dir = STRBUF_INIT;
294 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
296 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
297 apply_autostash(opts);
298 close_all_packs(the_repository->objects);
300 * We ignore errors in 'gc --auto', since the
301 * user should see them.
303 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
304 strbuf_addstr(&dir, opts->state_dir);
305 remove_dir_recursively(&dir, 0);
306 strbuf_release(&dir);
311 static struct commit *peel_committish(const char *name)
314 struct object_id oid;
316 if (get_oid(name, &oid))
318 obj = parse_object(the_repository, &oid);
319 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
322 static void add_var(struct strbuf *buf, const char *name, const char *value)
325 strbuf_addf(buf, "unset %s; ", name);
327 strbuf_addf(buf, "%s=", name);
328 sq_quote_buf(buf, value);
329 strbuf_addstr(buf, "; ");
333 static const char *resolvemsg =
334 N_("Resolve all conflicts manually, mark them as resolved with\n"
335 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
336 "You can instead skip this commit: run \"git rebase --skip\".\n"
337 "To abort and get back to the state before \"git rebase\", run "
338 "\"git rebase --abort\".");
340 static int run_specific_rebase(struct rebase_options *opts)
342 const char *argv[] = { NULL, NULL };
343 struct strbuf script_snippet = STRBUF_INIT;
345 const char *backend, *backend_func;
347 if (opts->type == REBASE_INTERACTIVE) {
348 /* Run builtin interactive rebase */
349 struct child_process child = CHILD_PROCESS_INIT;
351 argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
353 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
354 argv_array_push(&child.env_array, "GIT_EDITOR=:");
355 opts->autosquash = 0;
359 argv_array_push(&child.args, "rebase--interactive");
362 argv_array_pushf(&child.args, "--%s", opts->action);
363 if (opts->keep_empty)
364 argv_array_push(&child.args, "--keep-empty");
365 if (opts->rebase_merges)
366 argv_array_push(&child.args, "--rebase-merges");
367 if (opts->rebase_cousins)
368 argv_array_push(&child.args, "--rebase-cousins");
369 if (opts->autosquash)
370 argv_array_push(&child.args, "--autosquash");
371 if (opts->flags & REBASE_VERBOSE)
372 argv_array_push(&child.args, "--verbose");
373 if (opts->flags & REBASE_FORCE)
374 argv_array_push(&child.args, "--no-ff");
375 if (opts->restrict_revision)
376 argv_array_pushf(&child.args,
377 "--restrict-revision=^%s",
378 oid_to_hex(&opts->restrict_revision->object.oid));
380 argv_array_pushf(&child.args, "--upstream=%s",
381 oid_to_hex(&opts->upstream->object.oid));
383 argv_array_pushf(&child.args, "--onto=%s",
384 oid_to_hex(&opts->onto->object.oid));
385 if (opts->squash_onto)
386 argv_array_pushf(&child.args, "--squash-onto=%s",
387 oid_to_hex(opts->squash_onto));
389 argv_array_pushf(&child.args, "--onto-name=%s",
391 argv_array_pushf(&child.args, "--head-name=%s",
393 opts->head_name : "detached HEAD");
395 argv_array_pushf(&child.args, "--strategy=%s",
397 if (opts->strategy_opts)
398 argv_array_pushf(&child.args, "--strategy-opts=%s",
399 opts->strategy_opts);
401 argv_array_pushf(&child.args, "--switch-to=%s",
404 argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
405 if (opts->allow_empty_message)
406 argv_array_push(&child.args, "--allow-empty-message");
407 if (opts->allow_rerere_autoupdate > 0)
408 argv_array_push(&child.args, "--rerere-autoupdate");
409 else if (opts->allow_rerere_autoupdate == 0)
410 argv_array_push(&child.args, "--no-rerere-autoupdate");
411 if (opts->gpg_sign_opt)
412 argv_array_push(&child.args, opts->gpg_sign_opt);
414 argv_array_push(&child.args, "--signoff");
416 status = run_command(&child);
417 goto finished_rebase;
420 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
421 add_var(&script_snippet, "state_dir", opts->state_dir);
423 add_var(&script_snippet, "upstream_name", opts->upstream_name);
424 add_var(&script_snippet, "upstream", opts->upstream ?
425 oid_to_hex(&opts->upstream->object.oid) : NULL);
426 add_var(&script_snippet, "head_name",
427 opts->head_name ? opts->head_name : "detached HEAD");
428 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
429 add_var(&script_snippet, "onto", opts->onto ?
430 oid_to_hex(&opts->onto->object.oid) : NULL);
431 add_var(&script_snippet, "onto_name", opts->onto_name);
432 add_var(&script_snippet, "revisions", opts->revisions);
433 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
434 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
435 add_var(&script_snippet, "GIT_QUIET",
436 opts->flags & REBASE_NO_QUIET ? "" : "t");
437 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
438 add_var(&script_snippet, "verbose",
439 opts->flags & REBASE_VERBOSE ? "t" : "");
440 add_var(&script_snippet, "diffstat",
441 opts->flags & REBASE_DIFFSTAT ? "t" : "");
442 add_var(&script_snippet, "force_rebase",
443 opts->flags & REBASE_FORCE ? "t" : "");
445 add_var(&script_snippet, "switch_to", opts->switch_to);
446 add_var(&script_snippet, "action", opts->action ? opts->action : "");
447 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
448 add_var(&script_snippet, "allow_rerere_autoupdate",
449 opts->allow_rerere_autoupdate < 0 ? "" :
450 opts->allow_rerere_autoupdate ?
451 "--rerere-autoupdate" : "--no-rerere-autoupdate");
452 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
453 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
454 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
455 add_var(&script_snippet, "cmd", opts->cmd);
456 add_var(&script_snippet, "allow_empty_message",
457 opts->allow_empty_message ? "--allow-empty-message" : "");
458 add_var(&script_snippet, "rebase_merges",
459 opts->rebase_merges ? "t" : "");
460 add_var(&script_snippet, "rebase_cousins",
461 opts->rebase_cousins ? "t" : "");
462 add_var(&script_snippet, "strategy", opts->strategy);
463 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
464 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
465 add_var(&script_snippet, "squash_onto",
466 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
467 add_var(&script_snippet, "git_format_patch_opt",
468 opts->git_format_patch_opt.buf);
470 if (is_interactive(opts) &&
471 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
472 strbuf_addstr(&script_snippet,
473 "GIT_EDITOR=:; export GIT_EDITOR; ");
474 opts->autosquash = 0;
477 switch (opts->type) {
479 backend = "git-rebase--am";
480 backend_func = "git_rebase__am";
483 backend = "git-rebase--merge";
484 backend_func = "git_rebase__merge";
486 case REBASE_PRESERVE_MERGES:
487 backend = "git-rebase--preserve-merges";
488 backend_func = "git_rebase__preserve_merges";
491 BUG("Unhandled rebase type %d", opts->type);
495 strbuf_addf(&script_snippet,
496 ". git-sh-setup && . git-rebase--common &&"
497 " . %s && %s", backend, backend_func);
498 argv[0] = script_snippet.buf;
500 status = run_command_v_opt(argv, RUN_USING_SHELL);
502 if (opts->dont_finish_rebase)
504 else if (opts->type == REBASE_INTERACTIVE)
505 ; /* interactive rebase cleans up after itself */
506 else if (status == 0) {
507 if (!file_exists(state_dir_path("stopped-sha", opts)))
509 } else if (status == 2) {
510 struct strbuf dir = STRBUF_INIT;
512 apply_autostash(opts);
513 strbuf_addstr(&dir, opts->state_dir);
514 remove_dir_recursively(&dir, 0);
515 strbuf_release(&dir);
516 die("Nothing to do");
519 strbuf_release(&script_snippet);
521 return status ? -1 : 0;
524 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
526 #define RESET_HEAD_DETACH (1<<0)
527 #define RESET_HEAD_HARD (1<<1)
529 static int reset_head(struct object_id *oid, const char *action,
530 const char *switch_to_branch, unsigned flags,
531 const char *reflog_orig_head, const char *reflog_head)
533 unsigned detach_head = flags & RESET_HEAD_DETACH;
534 unsigned reset_hard = flags & RESET_HEAD_HARD;
535 struct object_id head_oid;
536 struct tree_desc desc[2] = { { NULL }, { NULL } };
537 struct lock_file lock = LOCK_INIT;
538 struct unpack_trees_options unpack_tree_opts;
540 const char *reflog_action;
541 struct strbuf msg = STRBUF_INIT;
543 struct object_id *orig = NULL, oid_orig,
544 *old_orig = NULL, oid_old_orig;
547 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
548 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
550 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
552 goto leave_reset_head;
555 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
556 ret = error(_("could not determine HEAD revision"));
557 goto leave_reset_head;
563 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
564 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
565 unpack_tree_opts.head_idx = 1;
566 unpack_tree_opts.src_index = the_repository->index;
567 unpack_tree_opts.dst_index = the_repository->index;
568 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
569 unpack_tree_opts.update = 1;
570 unpack_tree_opts.merge = 1;
572 unpack_tree_opts.reset = 1;
574 if (read_index_unmerged(the_repository->index) < 0) {
575 ret = error(_("could not read index"));
576 goto leave_reset_head;
579 if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
580 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
581 goto leave_reset_head;
584 if (!fill_tree_descriptor(&desc[nr++], oid)) {
585 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
586 goto leave_reset_head;
589 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
591 goto leave_reset_head;
594 tree = parse_tree_indirect(oid);
595 prime_cache_tree(the_repository->index, tree);
597 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
598 ret = error(_("could not write index"));
599 goto leave_reset_head;
602 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
603 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
604 prefix_len = msg.len;
606 if (!get_oid("ORIG_HEAD", &oid_old_orig))
607 old_orig = &oid_old_orig;
608 if (!get_oid("HEAD", &oid_orig)) {
610 if (!reflog_orig_head) {
611 strbuf_addstr(&msg, "updating ORIG_HEAD");
612 reflog_orig_head = msg.buf;
614 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
615 UPDATE_REFS_MSG_ON_ERR);
617 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
619 strbuf_setlen(&msg, prefix_len);
620 strbuf_addstr(&msg, "updating HEAD");
621 reflog_head = msg.buf;
623 if (!switch_to_branch)
624 ret = update_ref(reflog_head, "HEAD", oid, orig,
625 detach_head ? REF_NO_DEREF : 0,
626 UPDATE_REFS_MSG_ON_ERR);
628 ret = create_symref("HEAD", switch_to_branch, msg.buf);
630 ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
631 UPDATE_REFS_MSG_ON_ERR);
635 strbuf_release(&msg);
636 rollback_lock_file(&lock);
638 free((void *)desc[--nr].buffer);
642 static int rebase_config(const char *var, const char *value, void *data)
644 struct rebase_options *opts = data;
646 if (!strcmp(var, "rebase.stat")) {
647 if (git_config_bool(var, value))
648 opts->flags |= REBASE_DIFFSTAT;
650 opts->flags &= !REBASE_DIFFSTAT;
654 if (!strcmp(var, "rebase.autosquash")) {
655 opts->autosquash = git_config_bool(var, value);
659 if (!strcmp(var, "commit.gpgsign")) {
660 free(opts->gpg_sign_opt);
661 opts->gpg_sign_opt = git_config_bool(var, value) ?
662 xstrdup("-S") : NULL;
666 if (!strcmp(var, "rebase.autostash")) {
667 opts->autostash = git_config_bool(var, value);
671 return git_default_config(var, value, data);
675 * Determines whether the commits in from..to are linear, i.e. contain
676 * no merge commits. This function *expects* `from` to be an ancestor of
679 static int is_linear_history(struct commit *from, struct commit *to)
681 while (to && to != from) {
685 if (to->parents->next)
687 to = to->parents->item;
692 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
693 struct object_id *merge_base)
695 struct commit *head = lookup_commit(the_repository, head_oid);
696 struct commit_list *merge_bases;
702 merge_bases = get_merge_bases(onto, head);
703 if (merge_bases && !merge_bases->next) {
704 oidcpy(merge_base, &merge_bases->item->object.oid);
705 res = oideq(merge_base, &onto->object.oid);
707 oidcpy(merge_base, &null_oid);
710 free_commit_list(merge_bases);
711 return res && is_linear_history(onto, head);
714 /* -i followed by -m is still -i */
715 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
717 struct rebase_options *opts = opt->value;
719 BUG_ON_OPT_NEG(unset);
722 if (!is_interactive(opts))
723 opts->type = REBASE_MERGE;
728 /* -i followed by -p is still explicitly interactive, but -p alone is not */
729 static int parse_opt_interactive(const struct option *opt, const char *arg,
732 struct rebase_options *opts = opt->value;
734 BUG_ON_OPT_NEG(unset);
737 opts->type = REBASE_INTERACTIVE;
738 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
743 static void NORETURN error_on_missing_default_upstream(void)
745 struct branch *current_branch = branch_get(NULL);
748 "Please specify which branch you want to rebase against.\n"
749 "See git-rebase(1) for details.\n"
751 " git rebase '<branch>'\n"
753 current_branch ? _("There is no tracking information for "
754 "the current branch.") :
755 _("You are not currently on a branch."));
757 if (current_branch) {
758 const char *remote = current_branch->remote_name;
761 remote = _("<remote>");
763 printf(_("If you wish to set tracking information for this "
764 "branch you can do so with:\n"
766 " git branch --set-upstream-to=%s/<branch> %s\n"
768 remote, current_branch->name);
773 int cmd_rebase(int argc, const char **argv, const char *prefix)
775 struct rebase_options options = {
776 .type = REBASE_UNSPECIFIED,
777 .flags = REBASE_NO_QUIET,
778 .git_am_opt = STRBUF_INIT,
779 .allow_rerere_autoupdate = -1,
780 .allow_empty_message = 1,
781 .git_format_patch_opt = STRBUF_INIT,
783 const char *branch_name;
784 int ret, flags, total_argc, in_progress = 0;
785 int ok_to_skip_pre_rebase = 0;
786 struct strbuf msg = STRBUF_INIT;
787 struct strbuf revisions = STRBUF_INIT;
788 struct strbuf buf = STRBUF_INIT;
789 struct object_id merge_base;
797 ACTION_SHOW_CURRENT_PATCH,
798 } action = NO_ACTION;
799 int committer_date_is_author_date = 0;
801 int ignore_whitespace = 0;
802 const char *gpg_sign = NULL;
804 struct string_list whitespace = STRING_LIST_INIT_NODUP;
805 struct string_list exec = STRING_LIST_INIT_NODUP;
806 const char *rebase_merges = NULL;
808 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
809 struct object_id squash_onto;
810 char *squash_onto_name = NULL;
811 struct option builtin_rebase_options[] = {
812 OPT_STRING(0, "onto", &options.onto_name,
814 N_("rebase onto given branch instead of upstream")),
815 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
816 N_("allow pre-rebase hook to run")),
817 OPT_NEGBIT('q', "quiet", &options.flags,
818 N_("be quiet. implies --no-stat"),
819 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
820 OPT_BIT('v', "verbose", &options.flags,
821 N_("display a diffstat of what changed upstream"),
822 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
823 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
824 N_("do not show diffstat of what changed upstream"),
825 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
826 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
827 N_("passed to 'git apply'")),
828 OPT_BOOL(0, "signoff", &options.signoff,
829 N_("add a Signed-off-by: line to each commit")),
830 OPT_BOOL(0, "committer-date-is-author-date",
831 &committer_date_is_author_date,
832 N_("passed to 'git am'")),
833 OPT_BOOL(0, "ignore-date", &ignore_date,
834 N_("passed to 'git am'")),
835 OPT_BIT('f', "force-rebase", &options.flags,
836 N_("cherry-pick all commits, even if unchanged"),
838 OPT_BIT(0, "no-ff", &options.flags,
839 N_("cherry-pick all commits, even if unchanged"),
841 OPT_CMDMODE(0, "continue", &action, N_("continue"),
843 OPT_CMDMODE(0, "skip", &action,
844 N_("skip current patch and continue"), ACTION_SKIP),
845 OPT_CMDMODE(0, "abort", &action,
846 N_("abort and check out the original branch"),
848 OPT_CMDMODE(0, "quit", &action,
849 N_("abort but keep HEAD where it is"), ACTION_QUIT),
850 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
851 "during an interactive rebase"), ACTION_EDIT_TODO),
852 OPT_CMDMODE(0, "show-current-patch", &action,
853 N_("show the patch file being applied or merged"),
854 ACTION_SHOW_CURRENT_PATCH),
855 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
856 N_("use merging strategies to rebase"),
857 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
859 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
860 N_("let the user edit the list of commits to rebase"),
861 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
862 parse_opt_interactive },
863 OPT_SET_INT('p', "preserve-merges", &options.type,
864 N_("try to recreate merges instead of ignoring "
865 "them"), REBASE_PRESERVE_MERGES),
866 OPT_BOOL(0, "rerere-autoupdate",
867 &options.allow_rerere_autoupdate,
868 N_("allow rerere to update index with resolved "
870 OPT_BOOL('k', "keep-empty", &options.keep_empty,
871 N_("preserve empty commits during rebase")),
872 OPT_BOOL(0, "autosquash", &options.autosquash,
873 N_("move commits that begin with "
874 "squash!/fixup! under -i")),
875 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
876 N_("GPG-sign commits"),
877 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
878 OPT_STRING_LIST(0, "whitespace", &whitespace,
879 N_("whitespace"), N_("passed to 'git apply'")),
880 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
882 OPT_BOOL(0, "autostash", &options.autostash,
883 N_("automatically stash/stash pop before and after")),
884 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
885 N_("add exec lines after each commit of the "
887 OPT_BOOL(0, "allow-empty-message",
888 &options.allow_empty_message,
889 N_("allow rebasing commits with empty messages")),
890 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
892 N_("try to rebase merges instead of skipping them"),
893 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
894 OPT_BOOL(0, "fork-point", &fork_point,
895 N_("use 'merge-base --fork-point' to refine upstream")),
896 OPT_STRING('s', "strategy", &options.strategy,
897 N_("strategy"), N_("use the given merge strategy")),
898 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
900 N_("pass the argument through to the merge "
902 OPT_BOOL(0, "root", &options.root,
903 N_("rebase all reachable commits up to the root(s)")),
908 * NEEDSWORK: Once the builtin rebase has been tested enough
909 * and git-legacy-rebase.sh is retired to contrib/, this preamble
913 if (!use_builtin_rebase()) {
914 const char *path = mkpath("%s/git-legacy-rebase",
917 if (sane_execvp(path, (char **)argv) < 0)
918 die_errno(_("could not exec %s"), path);
920 BUG("sane_execvp() returned???");
923 if (argc == 2 && !strcmp(argv[1], "-h"))
924 usage_with_options(builtin_rebase_usage,
925 builtin_rebase_options);
927 prefix = setup_git_directory();
928 trace_repo_setup(prefix);
931 git_config(rebase_config, &options);
934 strbuf_addf(&buf, "%s/applying", apply_dir());
935 if(file_exists(buf.buf))
936 die(_("It looks like 'git am' is in progress. Cannot rebase."));
938 if (is_directory(apply_dir())) {
939 options.type = REBASE_AM;
940 options.state_dir = apply_dir();
941 } else if (is_directory(merge_dir())) {
943 strbuf_addf(&buf, "%s/rewritten", merge_dir());
944 if (is_directory(buf.buf)) {
945 options.type = REBASE_PRESERVE_MERGES;
946 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
949 strbuf_addf(&buf, "%s/interactive", merge_dir());
950 if(file_exists(buf.buf)) {
951 options.type = REBASE_INTERACTIVE;
952 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
954 options.type = REBASE_MERGE;
956 options.state_dir = merge_dir();
959 if (options.type != REBASE_UNSPECIFIED)
963 argc = parse_options(argc, argv, prefix,
964 builtin_rebase_options,
965 builtin_rebase_usage, 0);
967 if (action != NO_ACTION && total_argc != 2) {
968 usage_with_options(builtin_rebase_usage,
969 builtin_rebase_options);
973 usage_with_options(builtin_rebase_usage,
974 builtin_rebase_options);
976 if (action != NO_ACTION && !in_progress)
977 die(_("No rebase in progress?"));
979 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
980 die(_("The --edit-todo action can only be used during "
981 "interactive rebase."));
984 case ACTION_CONTINUE: {
985 struct object_id head;
986 struct lock_file lock_file = LOCK_INIT;
989 options.action = "continue";
992 if (get_oid("HEAD", &head))
993 die(_("Cannot read HEAD"));
995 fd = hold_locked_index(&lock_file, 0);
996 if (read_index(the_repository->index) < 0)
997 die(_("could not read index"));
998 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1001 update_index_if_able(the_repository->index,
1003 rollback_lock_file(&lock_file);
1005 if (has_unstaged_changes(1)) {
1006 puts(_("You must edit all merge conflicts and then\n"
1007 "mark them as resolved using git add"));
1010 if (read_basic_state(&options))
1015 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1017 options.action = "skip";
1019 rerere_clear(&merge_rr);
1020 string_list_clear(&merge_rr, 1);
1022 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1024 die(_("could not discard worktree changes"));
1025 remove_branch_state();
1026 if (read_basic_state(&options))
1030 case ACTION_ABORT: {
1031 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1032 options.action = "abort";
1034 rerere_clear(&merge_rr);
1035 string_list_clear(&merge_rr, 1);
1037 if (read_basic_state(&options))
1039 if (reset_head(&options.orig_head, "reset",
1040 options.head_name, RESET_HEAD_HARD,
1042 die(_("could not move back to %s"),
1043 oid_to_hex(&options.orig_head));
1044 remove_branch_state();
1045 ret = finish_rebase(&options);
1050 strbuf_addstr(&buf, options.state_dir);
1051 ret = !!remove_dir_recursively(&buf, 0);
1053 die(_("could not remove '%s'"), options.state_dir);
1056 case ACTION_EDIT_TODO:
1057 options.action = "edit-todo";
1058 options.dont_finish_rebase = 1;
1060 case ACTION_SHOW_CURRENT_PATCH:
1061 options.action = "show-current-patch";
1062 options.dont_finish_rebase = 1;
1067 BUG("action: %d", action);
1070 /* Make sure no rebase is in progress */
1072 const char *last_slash = strrchr(options.state_dir, '/');
1073 const char *state_dir_base =
1074 last_slash ? last_slash + 1 : options.state_dir;
1075 const char *cmd_live_rebase =
1076 "git rebase (--continue | --abort | --skip)";
1078 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1079 die(_("It seems that there is already a %s directory, and\n"
1080 "I wonder if you are in the middle of another rebase. "
1082 "case, please try\n\t%s\n"
1083 "If that is not the case, please\n\t%s\n"
1084 "and run me again. I am stopping in case you still "
1086 "valuable there.\n"),
1087 state_dir_base, cmd_live_rebase, buf.buf);
1090 if (!(options.flags & REBASE_NO_QUIET))
1091 strbuf_addstr(&options.git_am_opt, " -q");
1093 if (committer_date_is_author_date) {
1094 strbuf_addstr(&options.git_am_opt,
1095 " --committer-date-is-author-date");
1096 options.flags |= REBASE_FORCE;
1099 if (ignore_whitespace)
1100 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
1103 strbuf_addstr(&options.git_am_opt, " --ignore-date");
1104 options.flags |= REBASE_FORCE;
1107 if (options.keep_empty)
1108 imply_interactive(&options, "--keep-empty");
1111 free(options.gpg_sign_opt);
1112 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1116 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
1118 if (whitespace.nr) {
1121 for (i = 0; i < whitespace.nr; i++) {
1122 const char *item = whitespace.items[i].string;
1124 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
1127 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
1128 options.flags |= REBASE_FORCE;
1135 imply_interactive(&options, "--exec");
1138 for (i = 0; i < exec.nr; i++)
1139 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1140 options.cmd = xstrdup(buf.buf);
1143 if (rebase_merges) {
1144 if (!*rebase_merges)
1145 ; /* default mode; do nothing */
1146 else if (!strcmp("rebase-cousins", rebase_merges))
1147 options.rebase_cousins = 1;
1148 else if (strcmp("no-rebase-cousins", rebase_merges))
1149 die(_("Unknown mode: %s"), rebase_merges);
1150 options.rebase_merges = 1;
1151 imply_interactive(&options, "--rebase-merges");
1154 if (strategy_options.nr) {
1157 if (!options.strategy)
1158 options.strategy = "recursive";
1161 for (i = 0; i < strategy_options.nr; i++)
1162 strbuf_addf(&buf, " --%s",
1163 strategy_options.items[i].string);
1164 options.strategy_opts = xstrdup(buf.buf);
1167 if (options.strategy) {
1168 options.strategy = xstrdup(options.strategy);
1169 switch (options.type) {
1171 die(_("--strategy requires --merge or --interactive"));
1173 case REBASE_INTERACTIVE:
1174 case REBASE_PRESERVE_MERGES:
1177 case REBASE_UNSPECIFIED:
1178 options.type = REBASE_MERGE;
1181 BUG("unhandled rebase type (%d)", options.type);
1185 if (options.root && !options.onto_name)
1186 imply_interactive(&options, "--root without --onto");
1188 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1189 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1191 switch (options.type) {
1193 case REBASE_INTERACTIVE:
1194 case REBASE_PRESERVE_MERGES:
1195 options.state_dir = merge_dir();
1198 options.state_dir = apply_dir();
1201 /* the default rebase backend is `--am` */
1202 options.type = REBASE_AM;
1203 options.state_dir = apply_dir();
1207 if (options.git_am_opt.len) {
1210 /* all am options except -q are compatible only with --am */
1212 strbuf_addbuf(&buf, &options.git_am_opt);
1213 strbuf_addch(&buf, ' ');
1214 while ((p = strstr(buf.buf, " -q ")))
1215 strbuf_splice(&buf, p - buf.buf, 4, " ", 1);
1218 if (is_interactive(&options) && buf.len)
1219 die(_("error: cannot combine interactive options "
1220 "(--interactive, --exec, --rebase-merges, "
1221 "--preserve-merges, --keep-empty, --root + "
1222 "--onto) with am options (%s)"), buf.buf);
1223 if (options.type == REBASE_MERGE && buf.len)
1224 die(_("error: cannot combine merge options (--merge, "
1225 "--strategy, --strategy-option) with am options "
1229 if (options.signoff) {
1230 if (options.type == REBASE_PRESERVE_MERGES)
1231 die("cannot combine '--signoff' with "
1232 "'--preserve-merges'");
1233 strbuf_addstr(&options.git_am_opt, " --signoff");
1234 options.flags |= REBASE_FORCE;
1237 if (options.type == REBASE_PRESERVE_MERGES)
1239 * Note: incompatibility with --signoff handled in signoff block above
1240 * Note: incompatibility with --interactive is just a strong warning;
1241 * git-rebase.txt caveats with "unless you know what you are doing"
1243 if (options.rebase_merges)
1244 die(_("error: cannot combine '--preserve-merges' with "
1245 "'--rebase-merges'"));
1247 if (options.rebase_merges) {
1248 if (strategy_options.nr)
1249 die(_("error: cannot combine '--rebase-merges' with "
1250 "'--strategy-option'"));
1251 if (options.strategy)
1252 die(_("error: cannot combine '--rebase-merges' with "
1256 if (!options.root) {
1258 struct branch *branch;
1260 branch = branch_get(NULL);
1261 options.upstream_name = branch_get_upstream(branch,
1263 if (!options.upstream_name)
1264 error_on_missing_default_upstream();
1268 options.upstream_name = argv[0];
1271 if (!strcmp(options.upstream_name, "-"))
1272 options.upstream_name = "@{-1}";
1274 options.upstream = peel_committish(options.upstream_name);
1275 if (!options.upstream)
1276 die(_("invalid upstream '%s'"), options.upstream_name);
1277 options.upstream_arg = options.upstream_name;
1279 if (!options.onto_name) {
1280 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1281 &squash_onto, NULL, NULL) < 0)
1282 die(_("Could not create new root commit"));
1283 options.squash_onto = &squash_onto;
1284 options.onto_name = squash_onto_name =
1285 xstrdup(oid_to_hex(&squash_onto));
1287 options.upstream_name = NULL;
1288 options.upstream = NULL;
1290 usage_with_options(builtin_rebase_usage,
1291 builtin_rebase_options);
1292 options.upstream_arg = "--root";
1295 /* Make sure the branch to rebase onto is valid. */
1296 if (!options.onto_name)
1297 options.onto_name = options.upstream_name;
1298 if (strstr(options.onto_name, "...")) {
1299 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1300 die(_("'%s': need exactly one merge base"),
1302 options.onto = lookup_commit_or_die(&merge_base,
1305 options.onto = peel_committish(options.onto_name);
1307 die(_("Does not point to a valid commit '%s'"),
1312 * If the branch to rebase is given, that is the branch we will rebase
1313 * branch_name -- branch/commit being rebased, or
1314 * HEAD (already detached)
1315 * orig_head -- commit object name of tip of the branch before rebasing
1316 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1319 /* Is it "rebase other branchname" or "rebase other commit"? */
1320 branch_name = argv[0];
1321 options.switch_to = argv[0];
1323 /* Is it a local branch? */
1325 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1326 if (!read_ref(buf.buf, &options.orig_head))
1327 options.head_name = xstrdup(buf.buf);
1328 /* If not is it a valid ref (branch or commit)? */
1329 else if (!get_oid(branch_name, &options.orig_head))
1330 options.head_name = NULL;
1332 die(_("fatal: no such branch/commit '%s'"),
1334 } else if (argc == 0) {
1335 /* Do not need to switch branches, we are already on it. */
1337 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1339 if (!options.head_name)
1340 die(_("No such ref: %s"), "HEAD");
1341 if (flags & REF_ISSYMREF) {
1342 if (!skip_prefix(options.head_name,
1343 "refs/heads/", &branch_name))
1344 branch_name = options.head_name;
1347 free(options.head_name);
1348 options.head_name = NULL;
1349 branch_name = "HEAD";
1351 if (get_oid("HEAD", &options.orig_head))
1352 die(_("Could not resolve HEAD to a revision"));
1354 BUG("unexpected number of arguments left to parse");
1356 if (fork_point > 0) {
1357 struct commit *head =
1358 lookup_commit_reference(the_repository,
1359 &options.orig_head);
1360 options.restrict_revision =
1361 get_fork_point(options.upstream_name, head);
1364 if (read_index(the_repository->index) < 0)
1365 die(_("could not read index"));
1367 if (options.autostash) {
1368 struct lock_file lock_file = LOCK_INIT;
1371 fd = hold_locked_index(&lock_file, 0);
1372 refresh_cache(REFRESH_QUIET);
1374 update_index_if_able(&the_index, &lock_file);
1375 rollback_lock_file(&lock_file);
1377 if (has_unstaged_changes(1) || has_uncommitted_changes(1)) {
1378 const char *autostash =
1379 state_dir_path("autostash", &options);
1380 struct child_process stash = CHILD_PROCESS_INIT;
1381 struct object_id oid;
1382 struct commit *head =
1383 lookup_commit_reference(the_repository,
1384 &options.orig_head);
1386 argv_array_pushl(&stash.args,
1387 "stash", "create", "autostash", NULL);
1391 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1392 die(_("Cannot autostash"));
1393 strbuf_trim_trailing_newline(&buf);
1394 if (get_oid(buf.buf, &oid))
1395 die(_("Unexpected stash response: '%s'"),
1398 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1400 if (safe_create_leading_directories_const(autostash))
1401 die(_("Could not create directory for '%s'"),
1403 write_file(autostash, "%s", oid_to_hex(&oid));
1404 printf(_("Created autostash: %s\n"), buf.buf);
1405 if (reset_head(&head->object.oid, "reset --hard",
1406 NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
1407 die(_("could not reset --hard"));
1408 printf(_("HEAD is now at %s"),
1409 find_unique_abbrev(&head->object.oid,
1412 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1414 printf(" %s", buf.buf);
1417 if (discard_index(the_repository->index) < 0 ||
1418 read_index(the_repository->index) < 0)
1419 die(_("could not read index"));
1423 if (require_clean_work_tree("rebase",
1424 _("Please commit or stash them."), 1, 1)) {
1430 * Now we are rebasing commits upstream..orig_head (or with --root,
1431 * everything leading up to orig_head) on top of onto.
1435 * Check if we are already based on onto with linear history,
1436 * but this should be done only when upstream and onto are the same
1437 * and if this is not an interactive rebase.
1439 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1440 !is_interactive(&options) && !options.restrict_revision &&
1442 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1445 if (!(options.flags & REBASE_FORCE)) {
1446 /* Lazily switch to the target branch if needed... */
1447 if (options.switch_to) {
1448 struct object_id oid;
1450 if (get_oid(options.switch_to, &oid) < 0) {
1451 ret = !!error(_("could not parse '%s'"),
1457 strbuf_addf(&buf, "rebase: checkout %s",
1459 if (reset_head(&oid, "checkout",
1460 options.head_name, 0,
1462 ret = !!error(_("could not switch to "
1469 if (!(options.flags & REBASE_NO_QUIET))
1471 else if (!strcmp(branch_name, "HEAD") &&
1472 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1473 puts(_("HEAD is up to date."));
1475 printf(_("Current branch %s is up to date.\n"),
1477 ret = !!finish_rebase(&options);
1479 } else if (!(options.flags & REBASE_NO_QUIET))
1481 else if (!strcmp(branch_name, "HEAD") &&
1482 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1483 puts(_("HEAD is up to date, rebase forced."));
1485 printf(_("Current branch %s is up to date, rebase "
1486 "forced.\n"), branch_name);
1489 /* If a hook exists, give it a chance to interrupt*/
1490 if (!ok_to_skip_pre_rebase &&
1491 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1492 argc ? argv[0] : NULL, NULL))
1493 die(_("The pre-rebase hook refused to rebase."));
1495 if (options.flags & REBASE_DIFFSTAT) {
1496 struct diff_options opts;
1498 if (options.flags & REBASE_VERBOSE)
1499 printf(_("Changes from %s to %s:\n"),
1500 oid_to_hex(&merge_base),
1501 oid_to_hex(&options.onto->object.oid));
1503 /* We want color (if set), but no pager */
1505 opts.stat_width = -1; /* use full terminal width */
1506 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1507 opts.output_format |=
1508 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1509 opts.detect_rename = DIFF_DETECT_RENAME;
1510 diff_setup_done(&opts);
1511 diff_tree_oid(&merge_base, &options.onto->object.oid,
1513 diffcore_std(&opts);
1517 if (is_interactive(&options))
1520 /* Detach HEAD and reset the tree */
1521 if (options.flags & REBASE_NO_QUIET)
1522 printf(_("First, rewinding head to replay your work on top of "
1525 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1526 if (reset_head(&options.onto->object.oid, "checkout", NULL,
1527 RESET_HEAD_DETACH, NULL, msg.buf))
1528 die(_("Could not detach HEAD"));
1529 strbuf_release(&msg);
1532 * If the onto is a proper descendant of the tip of the branch, then
1533 * we just fast-forwarded.
1536 if (!oidcmp(&merge_base, &options.orig_head)) {
1537 printf(_("Fast-forwarded %s to %s. \n"),
1538 branch_name, options.onto_name);
1539 strbuf_addf(&msg, "rebase finished: %s onto %s",
1540 options.head_name ? options.head_name : "detached HEAD",
1541 oid_to_hex(&options.onto->object.oid));
1542 reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1544 strbuf_release(&msg);
1545 ret = !!finish_rebase(&options);
1549 strbuf_addf(&revisions, "%s..%s",
1550 options.root ? oid_to_hex(&options.onto->object.oid) :
1551 (options.restrict_revision ?
1552 oid_to_hex(&options.restrict_revision->object.oid) :
1553 oid_to_hex(&options.upstream->object.oid)),
1554 oid_to_hex(&options.orig_head));
1556 options.revisions = revisions.buf;
1559 ret = !!run_specific_rebase(&options);
1562 strbuf_release(&revisions);
1563 free(options.head_name);
1564 free(options.gpg_sign_opt);
1566 free(squash_onto_name);