2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
7 #define USE_THE_INDEX_COMPATIBILITY_MACROS
9 #include "run-command.h"
11 #include "argv-array.h"
17 #include "cache-tree.h"
18 #include "unpack-trees.h"
20 #include "parse-options.h"
23 #include "wt-status.h"
25 #include "commit-reach.h"
29 static char const * const builtin_rebase_usage[] = {
30 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
31 "[<upstream>] [<branch>]"),
32 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
34 N_("git rebase --continue | --abort | --skip | --edit-todo"),
38 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
39 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
42 REBASE_UNSPECIFIED = -1,
46 REBASE_PRESERVE_MERGES
49 static int use_builtin_rebase(void)
51 struct child_process cp = CHILD_PROCESS_INIT;
52 struct strbuf out = STRBUF_INIT;
53 int ret, env = git_env_bool("GIT_TEST_REBASE_USE_BUILTIN", -1);
58 argv_array_pushl(&cp.args,
59 "config", "--bool", "rebase.usebuiltin", NULL);
61 if (capture_command(&cp, &out, 6)) {
67 ret = !strcmp("true", out.buf);
72 struct rebase_options {
73 enum rebase_type type;
74 const char *state_dir;
75 struct commit *upstream;
76 const char *upstream_name;
77 const char *upstream_arg;
79 struct object_id orig_head;
81 const char *onto_name;
82 const char *revisions;
83 const char *switch_to;
85 struct object_id *squash_onto;
86 struct commit *restrict_revision;
87 int dont_finish_rebase;
89 REBASE_NO_QUIET = 1<<0,
90 REBASE_VERBOSE = 1<<1,
91 REBASE_DIFFSTAT = 1<<2,
93 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
95 struct argv_array git_am_opts;
98 int allow_rerere_autoupdate;
104 int allow_empty_message;
105 int rebase_merges, rebase_cousins;
106 char *strategy, *strategy_opts;
107 struct strbuf git_format_patch_opt;
108 int reschedule_failed_exec;
111 static int is_interactive(struct rebase_options *opts)
113 return opts->type == REBASE_INTERACTIVE ||
114 opts->type == REBASE_PRESERVE_MERGES;
117 static void imply_interactive(struct rebase_options *opts, const char *option)
119 switch (opts->type) {
121 die(_("%s requires an interactive rebase"), option);
123 case REBASE_INTERACTIVE:
124 case REBASE_PRESERVE_MERGES:
127 /* we now implement --merge via --interactive */
129 opts->type = REBASE_INTERACTIVE; /* implied */
134 /* Returns the filename prefixed by the state_dir */
135 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
137 static struct strbuf path = STRBUF_INIT;
138 static size_t prefix_len;
141 strbuf_addf(&path, "%s/", opts->state_dir);
142 prefix_len = path.len;
145 strbuf_setlen(&path, prefix_len);
146 strbuf_addstr(&path, filename);
150 /* Read one file, then strip line endings */
151 static int read_one(const char *path, struct strbuf *buf)
153 if (strbuf_read_file(buf, path, 0) < 0)
154 return error_errno(_("could not read '%s'"), path);
155 strbuf_trim_trailing_newline(buf);
159 /* Initialize the rebase options from the state directory. */
160 static int read_basic_state(struct rebase_options *opts)
162 struct strbuf head_name = STRBUF_INIT;
163 struct strbuf buf = STRBUF_INIT;
164 struct object_id oid;
166 if (read_one(state_dir_path("head-name", opts), &head_name) ||
167 read_one(state_dir_path("onto", opts), &buf))
169 opts->head_name = starts_with(head_name.buf, "refs/") ?
170 xstrdup(head_name.buf) : NULL;
171 strbuf_release(&head_name);
172 if (get_oid(buf.buf, &oid))
173 return error(_("could not get 'onto': '%s'"), buf.buf);
174 opts->onto = lookup_commit_or_die(&oid, buf.buf);
177 * We always write to orig-head, but interactive rebase used to write to
178 * head. Fall back to reading from head to cover for the case that the
179 * user upgraded git with an ongoing interactive rebase.
182 if (file_exists(state_dir_path("orig-head", opts))) {
183 if (read_one(state_dir_path("orig-head", opts), &buf))
185 } else if (read_one(state_dir_path("head", opts), &buf))
187 if (get_oid(buf.buf, &opts->orig_head))
188 return error(_("invalid orig-head: '%s'"), buf.buf);
190 if (file_exists(state_dir_path("quiet", opts)))
191 opts->flags &= ~REBASE_NO_QUIET;
193 opts->flags |= REBASE_NO_QUIET;
195 if (file_exists(state_dir_path("verbose", opts)))
196 opts->flags |= REBASE_VERBOSE;
198 if (file_exists(state_dir_path("signoff", opts))) {
200 opts->flags |= REBASE_FORCE;
203 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
205 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
208 if (!strcmp(buf.buf, "--rerere-autoupdate"))
209 opts->allow_rerere_autoupdate = 1;
210 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
211 opts->allow_rerere_autoupdate = 0;
213 warning(_("ignoring invalid allow_rerere_autoupdate: "
216 opts->allow_rerere_autoupdate = -1;
218 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
220 if (read_one(state_dir_path("gpg_sign_opt", opts),
223 free(opts->gpg_sign_opt);
224 opts->gpg_sign_opt = xstrdup(buf.buf);
227 if (file_exists(state_dir_path("strategy", opts))) {
229 if (read_one(state_dir_path("strategy", opts), &buf))
231 free(opts->strategy);
232 opts->strategy = xstrdup(buf.buf);
235 if (file_exists(state_dir_path("strategy_opts", opts))) {
237 if (read_one(state_dir_path("strategy_opts", opts), &buf))
239 free(opts->strategy_opts);
240 opts->strategy_opts = xstrdup(buf.buf);
243 strbuf_release(&buf);
248 static int write_basic_state(struct rebase_options *opts)
250 write_file(state_dir_path("head-name", opts), "%s",
251 opts->head_name ? opts->head_name : "detached HEAD");
252 write_file(state_dir_path("onto", opts), "%s",
253 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
254 write_file(state_dir_path("orig-head", opts), "%s",
255 oid_to_hex(&opts->orig_head));
256 write_file(state_dir_path("quiet", opts), "%s",
257 opts->flags & REBASE_NO_QUIET ? "" : "t");
258 if (opts->flags & REBASE_VERBOSE)
259 write_file(state_dir_path("verbose", opts), "%s", "");
261 write_file(state_dir_path("strategy", opts), "%s",
263 if (opts->strategy_opts)
264 write_file(state_dir_path("strategy_opts", opts), "%s",
265 opts->strategy_opts);
266 if (opts->allow_rerere_autoupdate >= 0)
267 write_file(state_dir_path("allow_rerere_autoupdate", opts),
268 "-%s-rerere-autoupdate",
269 opts->allow_rerere_autoupdate ? "" : "-no");
270 if (opts->gpg_sign_opt)
271 write_file(state_dir_path("gpg_sign_opt", opts), "%s",
274 write_file(state_dir_path("strategy", opts), "--signoff");
279 static int apply_autostash(struct rebase_options *opts)
281 const char *path = state_dir_path("autostash", opts);
282 struct strbuf autostash = STRBUF_INIT;
283 struct child_process stash_apply = CHILD_PROCESS_INIT;
285 if (!file_exists(path))
288 if (read_one(path, &autostash))
289 return error(_("Could not read '%s'"), path);
290 /* Ensure that the hash is not mistaken for a number */
291 strbuf_addstr(&autostash, "^0");
292 argv_array_pushl(&stash_apply.args,
293 "stash", "apply", autostash.buf, NULL);
294 stash_apply.git_cmd = 1;
295 stash_apply.no_stderr = stash_apply.no_stdout =
296 stash_apply.no_stdin = 1;
297 if (!run_command(&stash_apply))
298 printf(_("Applied autostash.\n"));
300 struct argv_array args = ARGV_ARRAY_INIT;
303 argv_array_pushl(&args,
304 "stash", "store", "-m", "autostash", "-q",
305 autostash.buf, NULL);
306 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
307 res = error(_("Cannot store %s"), autostash.buf);
308 argv_array_clear(&args);
309 strbuf_release(&autostash);
314 _("Applying autostash resulted in conflicts.\n"
315 "Your changes are safe in the stash.\n"
316 "You can run \"git stash pop\" or \"git stash drop\" "
320 strbuf_release(&autostash);
324 static int finish_rebase(struct rebase_options *opts)
326 struct strbuf dir = STRBUF_INIT;
327 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
329 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
330 apply_autostash(opts);
331 close_all_packs(the_repository->objects);
333 * We ignore errors in 'gc --auto', since the
334 * user should see them.
336 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
337 strbuf_addstr(&dir, opts->state_dir);
338 remove_dir_recursively(&dir, 0);
339 strbuf_release(&dir);
344 static struct commit *peel_committish(const char *name)
347 struct object_id oid;
349 if (get_oid(name, &oid))
351 obj = parse_object(the_repository, &oid);
352 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
355 static void add_var(struct strbuf *buf, const char *name, const char *value)
358 strbuf_addf(buf, "unset %s; ", name);
360 strbuf_addf(buf, "%s=", name);
361 sq_quote_buf(buf, value);
362 strbuf_addstr(buf, "; ");
366 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
368 #define RESET_HEAD_DETACH (1<<0)
369 #define RESET_HEAD_HARD (1<<1)
370 #define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2)
371 #define RESET_HEAD_REFS_ONLY (1<<3)
373 static int reset_head(struct object_id *oid, const char *action,
374 const char *switch_to_branch, unsigned flags,
375 const char *reflog_orig_head, const char *reflog_head)
377 unsigned detach_head = flags & RESET_HEAD_DETACH;
378 unsigned reset_hard = flags & RESET_HEAD_HARD;
379 unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
380 unsigned refs_only = flags & RESET_HEAD_REFS_ONLY;
381 struct object_id head_oid;
382 struct tree_desc desc[2] = { { NULL }, { NULL } };
383 struct lock_file lock = LOCK_INIT;
384 struct unpack_trees_options unpack_tree_opts;
386 const char *reflog_action;
387 struct strbuf msg = STRBUF_INIT;
389 struct object_id *orig = NULL, oid_orig,
390 *old_orig = NULL, oid_old_orig;
393 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
394 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
396 if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) {
398 goto leave_reset_head;
401 if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) {
402 ret = error(_("could not determine HEAD revision"));
403 goto leave_reset_head;
410 goto reset_head_refs;
412 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
413 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
414 unpack_tree_opts.head_idx = 1;
415 unpack_tree_opts.src_index = the_repository->index;
416 unpack_tree_opts.dst_index = the_repository->index;
417 unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge;
418 unpack_tree_opts.update = 1;
419 unpack_tree_opts.merge = 1;
421 unpack_tree_opts.reset = 1;
423 if (repo_read_index_unmerged(the_repository) < 0) {
424 ret = error(_("could not read index"));
425 goto leave_reset_head;
428 if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) {
429 ret = error(_("failed to find tree of %s"),
430 oid_to_hex(&head_oid));
431 goto leave_reset_head;
434 if (!fill_tree_descriptor(&desc[nr++], oid)) {
435 ret = error(_("failed to find tree of %s"), oid_to_hex(oid));
436 goto leave_reset_head;
439 if (unpack_trees(nr, desc, &unpack_tree_opts)) {
441 goto leave_reset_head;
444 tree = parse_tree_indirect(oid);
445 prime_cache_tree(the_repository, the_repository->index, tree);
447 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) {
448 ret = error(_("could not write index"));
449 goto leave_reset_head;
453 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
454 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
455 prefix_len = msg.len;
457 if (!get_oid("ORIG_HEAD", &oid_old_orig))
458 old_orig = &oid_old_orig;
459 if (!get_oid("HEAD", &oid_orig)) {
461 if (!reflog_orig_head) {
462 strbuf_addstr(&msg, "updating ORIG_HEAD");
463 reflog_orig_head = msg.buf;
465 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
466 UPDATE_REFS_MSG_ON_ERR);
468 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
470 strbuf_setlen(&msg, prefix_len);
471 strbuf_addstr(&msg, "updating HEAD");
472 reflog_head = msg.buf;
474 if (!switch_to_branch)
475 ret = update_ref(reflog_head, "HEAD", oid, orig,
476 detach_head ? REF_NO_DEREF : 0,
477 UPDATE_REFS_MSG_ON_ERR);
479 ret = update_ref(reflog_orig_head, switch_to_branch, oid,
480 NULL, 0, UPDATE_REFS_MSG_ON_ERR);
482 ret = create_symref("HEAD", switch_to_branch,
486 run_hook_le(NULL, "post-checkout",
487 oid_to_hex(orig ? orig : &null_oid),
488 oid_to_hex(oid), "1", NULL);
491 strbuf_release(&msg);
492 rollback_lock_file(&lock);
494 free((void *)desc[--nr].buffer);
498 static int move_to_original_branch(struct rebase_options *opts)
500 struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
503 if (!opts->head_name)
504 return 0; /* nothing to move back to */
507 BUG("move_to_original_branch without onto");
509 strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s",
510 opts->head_name, oid_to_hex(&opts->onto->object.oid));
511 strbuf_addf(&head_reflog, "rebase finished: returning to %s",
513 ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY,
514 orig_head_reflog.buf, head_reflog.buf);
516 strbuf_release(&orig_head_reflog);
517 strbuf_release(&head_reflog);
521 static const char *resolvemsg =
522 N_("Resolve all conflicts manually, mark them as resolved with\n"
523 "\"git add/rm <conflicted_files>\", then run \"git rebase --continue\".\n"
524 "You can instead skip this commit: run \"git rebase --skip\".\n"
525 "To abort and get back to the state before \"git rebase\", run "
526 "\"git rebase --abort\".");
528 static int run_am(struct rebase_options *opts)
530 struct child_process am = CHILD_PROCESS_INIT;
531 struct child_process format_patch = CHILD_PROCESS_INIT;
532 struct strbuf revisions = STRBUF_INIT;
534 char *rebased_patches;
537 argv_array_push(&am.args, "am");
539 if (opts->action && !strcmp("continue", opts->action)) {
540 argv_array_push(&am.args, "--resolved");
541 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
542 if (opts->gpg_sign_opt)
543 argv_array_push(&am.args, opts->gpg_sign_opt);
544 status = run_command(&am);
548 return move_to_original_branch(opts);
550 if (opts->action && !strcmp("skip", opts->action)) {
551 argv_array_push(&am.args, "--skip");
552 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
553 status = run_command(&am);
557 return move_to_original_branch(opts);
559 if (opts->action && !strcmp("show-current-patch", opts->action)) {
560 argv_array_push(&am.args, "--show-current-patch");
561 return run_command(&am);
564 strbuf_addf(&revisions, "%s...%s",
565 oid_to_hex(opts->root ?
566 /* this is now equivalent to !opts->upstream */
567 &opts->onto->object.oid :
568 &opts->upstream->object.oid),
569 oid_to_hex(&opts->orig_head));
571 rebased_patches = xstrdup(git_path("rebased-patches"));
572 format_patch.out = open(rebased_patches,
573 O_WRONLY | O_CREAT | O_TRUNC, 0666);
574 if (format_patch.out < 0) {
575 status = error_errno(_("could not open '%s' for writing"),
577 free(rebased_patches);
578 argv_array_clear(&am.args);
582 format_patch.git_cmd = 1;
583 argv_array_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
584 "--full-index", "--cherry-pick", "--right-only",
585 "--src-prefix=a/", "--dst-prefix=b/", "--no-renames",
586 "--no-cover-letter", "--pretty=mboxrd", "--topo-order", NULL);
587 if (opts->git_format_patch_opt.len)
588 argv_array_split(&format_patch.args,
589 opts->git_format_patch_opt.buf);
590 argv_array_push(&format_patch.args, revisions.buf);
591 if (opts->restrict_revision)
592 argv_array_pushf(&format_patch.args, "^%s",
593 oid_to_hex(&opts->restrict_revision->object.oid));
595 status = run_command(&format_patch);
597 unlink(rebased_patches);
598 free(rebased_patches);
599 argv_array_clear(&am.args);
601 reset_head(&opts->orig_head, "checkout", opts->head_name, 0,
603 error(_("\ngit encountered an error while preparing the "
604 "patches to replay\n"
607 "As a result, git cannot rebase them."),
610 strbuf_release(&revisions);
613 strbuf_release(&revisions);
615 am.in = open(rebased_patches, O_RDONLY);
617 status = error_errno(_("could not open '%s' for reading"),
619 free(rebased_patches);
620 argv_array_clear(&am.args);
624 argv_array_pushv(&am.args, opts->git_am_opts.argv);
625 argv_array_push(&am.args, "--rebasing");
626 argv_array_pushf(&am.args, "--resolvemsg=%s", resolvemsg);
627 argv_array_push(&am.args, "--patch-format=mboxrd");
628 if (opts->allow_rerere_autoupdate > 0)
629 argv_array_push(&am.args, "--rerere-autoupdate");
630 else if (opts->allow_rerere_autoupdate == 0)
631 argv_array_push(&am.args, "--no-rerere-autoupdate");
632 if (opts->gpg_sign_opt)
633 argv_array_push(&am.args, opts->gpg_sign_opt);
634 status = run_command(&am);
635 unlink(rebased_patches);
636 free(rebased_patches);
639 return move_to_original_branch(opts);
642 if (is_directory(opts->state_dir))
643 write_basic_state(opts);
648 static int run_specific_rebase(struct rebase_options *opts)
650 const char *argv[] = { NULL, NULL };
651 struct strbuf script_snippet = STRBUF_INIT, buf = STRBUF_INIT;
653 const char *backend, *backend_func;
655 if (opts->type == REBASE_INTERACTIVE) {
656 /* Run builtin interactive rebase */
657 struct child_process child = CHILD_PROCESS_INIT;
659 argv_array_pushf(&child.env_array, "GIT_CHERRY_PICK_HELP=%s",
661 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
662 argv_array_push(&child.env_array,
663 "GIT_SEQUENCE_EDITOR=:");
664 opts->autosquash = 0;
668 argv_array_push(&child.args, "rebase--interactive");
671 argv_array_pushf(&child.args, "--%s", opts->action);
672 if (opts->keep_empty)
673 argv_array_push(&child.args, "--keep-empty");
674 if (opts->rebase_merges)
675 argv_array_push(&child.args, "--rebase-merges");
676 if (opts->rebase_cousins)
677 argv_array_push(&child.args, "--rebase-cousins");
678 if (opts->autosquash)
679 argv_array_push(&child.args, "--autosquash");
680 if (opts->flags & REBASE_VERBOSE)
681 argv_array_push(&child.args, "--verbose");
682 if (opts->flags & REBASE_FORCE)
683 argv_array_push(&child.args, "--no-ff");
684 if (opts->restrict_revision)
685 argv_array_pushf(&child.args,
686 "--restrict-revision=^%s",
687 oid_to_hex(&opts->restrict_revision->object.oid));
689 argv_array_pushf(&child.args, "--upstream=%s",
690 oid_to_hex(&opts->upstream->object.oid));
692 argv_array_pushf(&child.args, "--onto=%s",
693 oid_to_hex(&opts->onto->object.oid));
694 if (opts->squash_onto)
695 argv_array_pushf(&child.args, "--squash-onto=%s",
696 oid_to_hex(opts->squash_onto));
698 argv_array_pushf(&child.args, "--onto-name=%s",
700 argv_array_pushf(&child.args, "--head-name=%s",
702 opts->head_name : "detached HEAD");
704 argv_array_pushf(&child.args, "--strategy=%s",
706 if (opts->strategy_opts)
707 argv_array_pushf(&child.args, "--strategy-opts=%s",
708 opts->strategy_opts);
710 argv_array_pushf(&child.args, "--switch-to=%s",
713 argv_array_pushf(&child.args, "--cmd=%s", opts->cmd);
714 if (opts->allow_empty_message)
715 argv_array_push(&child.args, "--allow-empty-message");
716 if (opts->allow_rerere_autoupdate > 0)
717 argv_array_push(&child.args, "--rerere-autoupdate");
718 else if (opts->allow_rerere_autoupdate == 0)
719 argv_array_push(&child.args, "--no-rerere-autoupdate");
720 if (opts->gpg_sign_opt)
721 argv_array_push(&child.args, opts->gpg_sign_opt);
723 argv_array_push(&child.args, "--signoff");
724 if (opts->reschedule_failed_exec)
725 argv_array_push(&child.args, "--reschedule-failed-exec");
727 status = run_command(&child);
728 goto finished_rebase;
731 if (opts->type == REBASE_AM) {
732 status = run_am(opts);
733 goto finished_rebase;
736 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
737 add_var(&script_snippet, "state_dir", opts->state_dir);
739 add_var(&script_snippet, "upstream_name", opts->upstream_name);
740 add_var(&script_snippet, "upstream", opts->upstream ?
741 oid_to_hex(&opts->upstream->object.oid) : NULL);
742 add_var(&script_snippet, "head_name",
743 opts->head_name ? opts->head_name : "detached HEAD");
744 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
745 add_var(&script_snippet, "onto", opts->onto ?
746 oid_to_hex(&opts->onto->object.oid) : NULL);
747 add_var(&script_snippet, "onto_name", opts->onto_name);
748 add_var(&script_snippet, "revisions", opts->revisions);
749 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
750 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
751 add_var(&script_snippet, "GIT_QUIET",
752 opts->flags & REBASE_NO_QUIET ? "" : "t");
753 sq_quote_argv_pretty(&buf, opts->git_am_opts.argv);
754 add_var(&script_snippet, "git_am_opt", buf.buf);
755 strbuf_release(&buf);
756 add_var(&script_snippet, "verbose",
757 opts->flags & REBASE_VERBOSE ? "t" : "");
758 add_var(&script_snippet, "diffstat",
759 opts->flags & REBASE_DIFFSTAT ? "t" : "");
760 add_var(&script_snippet, "force_rebase",
761 opts->flags & REBASE_FORCE ? "t" : "");
763 add_var(&script_snippet, "switch_to", opts->switch_to);
764 add_var(&script_snippet, "action", opts->action ? opts->action : "");
765 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
766 add_var(&script_snippet, "allow_rerere_autoupdate",
767 opts->allow_rerere_autoupdate < 0 ? "" :
768 opts->allow_rerere_autoupdate ?
769 "--rerere-autoupdate" : "--no-rerere-autoupdate");
770 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
771 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
772 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
773 add_var(&script_snippet, "cmd", opts->cmd);
774 add_var(&script_snippet, "allow_empty_message",
775 opts->allow_empty_message ? "--allow-empty-message" : "");
776 add_var(&script_snippet, "rebase_merges",
777 opts->rebase_merges ? "t" : "");
778 add_var(&script_snippet, "rebase_cousins",
779 opts->rebase_cousins ? "t" : "");
780 add_var(&script_snippet, "strategy", opts->strategy);
781 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
782 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
783 add_var(&script_snippet, "squash_onto",
784 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
785 add_var(&script_snippet, "git_format_patch_opt",
786 opts->git_format_patch_opt.buf);
788 if (is_interactive(opts) &&
789 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
790 strbuf_addstr(&script_snippet,
791 "GIT_SEQUENCE_EDITOR=:; export GIT_SEQUENCE_EDITOR; ");
792 opts->autosquash = 0;
795 switch (opts->type) {
797 backend = "git-rebase--am";
798 backend_func = "git_rebase__am";
800 case REBASE_PRESERVE_MERGES:
801 backend = "git-rebase--preserve-merges";
802 backend_func = "git_rebase__preserve_merges";
805 BUG("Unhandled rebase type %d", opts->type);
809 strbuf_addf(&script_snippet,
810 ". git-sh-setup && . git-rebase--common &&"
811 " . %s && %s", backend, backend_func);
812 argv[0] = script_snippet.buf;
814 status = run_command_v_opt(argv, RUN_USING_SHELL);
816 if (opts->dont_finish_rebase)
818 else if (opts->type == REBASE_INTERACTIVE)
819 ; /* interactive rebase cleans up after itself */
820 else if (status == 0) {
821 if (!file_exists(state_dir_path("stopped-sha", opts)))
823 } else if (status == 2) {
824 struct strbuf dir = STRBUF_INIT;
826 apply_autostash(opts);
827 strbuf_addstr(&dir, opts->state_dir);
828 remove_dir_recursively(&dir, 0);
829 strbuf_release(&dir);
830 die("Nothing to do");
833 strbuf_release(&script_snippet);
835 return status ? -1 : 0;
838 static int rebase_config(const char *var, const char *value, void *data)
840 struct rebase_options *opts = data;
842 if (!strcmp(var, "rebase.stat")) {
843 if (git_config_bool(var, value))
844 opts->flags |= REBASE_DIFFSTAT;
846 opts->flags &= !REBASE_DIFFSTAT;
850 if (!strcmp(var, "rebase.autosquash")) {
851 opts->autosquash = git_config_bool(var, value);
855 if (!strcmp(var, "commit.gpgsign")) {
856 free(opts->gpg_sign_opt);
857 opts->gpg_sign_opt = git_config_bool(var, value) ?
858 xstrdup("-S") : NULL;
862 if (!strcmp(var, "rebase.autostash")) {
863 opts->autostash = git_config_bool(var, value);
867 if (!strcmp(var, "rebase.reschedulefailedexec")) {
868 opts->reschedule_failed_exec = git_config_bool(var, value);
872 return git_default_config(var, value, data);
876 * Determines whether the commits in from..to are linear, i.e. contain
877 * no merge commits. This function *expects* `from` to be an ancestor of
880 static int is_linear_history(struct commit *from, struct commit *to)
882 while (to && to != from) {
886 if (to->parents->next)
888 to = to->parents->item;
893 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
894 struct object_id *merge_base)
896 struct commit *head = lookup_commit(the_repository, head_oid);
897 struct commit_list *merge_bases;
903 merge_bases = get_merge_bases(onto, head);
904 if (merge_bases && !merge_bases->next) {
905 oidcpy(merge_base, &merge_bases->item->object.oid);
906 res = oideq(merge_base, &onto->object.oid);
908 oidcpy(merge_base, &null_oid);
911 free_commit_list(merge_bases);
912 return res && is_linear_history(onto, head);
915 /* -i followed by -m is still -i */
916 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
918 struct rebase_options *opts = opt->value;
920 BUG_ON_OPT_NEG(unset);
923 if (!is_interactive(opts))
924 opts->type = REBASE_MERGE;
929 /* -i followed by -p is still explicitly interactive, but -p alone is not */
930 static int parse_opt_interactive(const struct option *opt, const char *arg,
933 struct rebase_options *opts = opt->value;
935 BUG_ON_OPT_NEG(unset);
938 opts->type = REBASE_INTERACTIVE;
939 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
945 struct string_list *list;
946 struct rebase_options *options;
949 static int parse_opt_y(const struct option *opt, const char *arg, int unset)
951 struct opt_y *o = opt->value;
956 o->options->reschedule_failed_exec = 1;
957 string_list_append(o->list, arg);
961 static void NORETURN error_on_missing_default_upstream(void)
963 struct branch *current_branch = branch_get(NULL);
966 "Please specify which branch you want to rebase against.\n"
967 "See git-rebase(1) for details.\n"
969 " git rebase '<branch>'\n"
971 current_branch ? _("There is no tracking information for "
972 "the current branch.") :
973 _("You are not currently on a branch."));
975 if (current_branch) {
976 const char *remote = current_branch->remote_name;
979 remote = _("<remote>");
981 printf(_("If you wish to set tracking information for this "
982 "branch you can do so with:\n"
984 " git branch --set-upstream-to=%s/<branch> %s\n"
986 remote, current_branch->name);
991 static void set_reflog_action(struct rebase_options *options)
994 struct strbuf buf = STRBUF_INIT;
996 if (!is_interactive(options))
999 env = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1000 if (env && strcmp("rebase", env))
1001 return; /* only override it if it is "rebase" */
1003 strbuf_addf(&buf, "rebase -i (%s)", options->action);
1004 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, buf.buf, 1);
1005 strbuf_release(&buf);
1008 static int check_exec_cmd(const char *cmd)
1010 if (strchr(cmd, '\n'))
1011 return error(_("exec commands cannot contain newlines"));
1013 /* Does the command consist purely of whitespace? */
1014 if (!cmd[strspn(cmd, " \t\r\f\v")])
1015 return error(_("empty exec command"));
1021 int cmd_rebase(int argc, const char **argv, const char *prefix)
1023 struct rebase_options options = {
1024 .type = REBASE_UNSPECIFIED,
1025 .flags = REBASE_NO_QUIET,
1026 .git_am_opts = ARGV_ARRAY_INIT,
1027 .allow_rerere_autoupdate = -1,
1028 .allow_empty_message = 1,
1029 .git_format_patch_opt = STRBUF_INIT,
1031 const char *branch_name;
1032 int ret, flags, total_argc, in_progress = 0;
1033 int ok_to_skip_pre_rebase = 0;
1034 struct strbuf msg = STRBUF_INIT;
1035 struct strbuf revisions = STRBUF_INIT;
1036 struct strbuf buf = STRBUF_INIT;
1037 struct object_id merge_base;
1045 ACTION_SHOW_CURRENT_PATCH,
1046 } action = NO_ACTION;
1047 const char *gpg_sign = NULL;
1048 struct string_list exec = STRING_LIST_INIT_NODUP;
1049 const char *rebase_merges = NULL;
1050 int fork_point = -1;
1051 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
1052 struct object_id squash_onto;
1053 char *squash_onto_name = NULL;
1054 struct opt_y opt_y = { .list = &exec, .options = &options };
1055 struct option builtin_rebase_options[] = {
1056 OPT_STRING(0, "onto", &options.onto_name,
1058 N_("rebase onto given branch instead of upstream")),
1059 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1060 N_("allow pre-rebase hook to run")),
1061 OPT_NEGBIT('q', "quiet", &options.flags,
1062 N_("be quiet. implies --no-stat"),
1063 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
1064 OPT_BIT('v', "verbose", &options.flags,
1065 N_("display a diffstat of what changed upstream"),
1066 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1067 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1068 N_("do not show diffstat of what changed upstream"),
1069 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
1070 OPT_BOOL(0, "signoff", &options.signoff,
1071 N_("add a Signed-off-by: line to each commit")),
1072 OPT_PASSTHRU_ARGV(0, "ignore-whitespace", &options.git_am_opts,
1073 NULL, N_("passed to 'git am'"),
1075 OPT_PASSTHRU_ARGV(0, "committer-date-is-author-date",
1076 &options.git_am_opts, NULL,
1077 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1078 OPT_PASSTHRU_ARGV(0, "ignore-date", &options.git_am_opts, NULL,
1079 N_("passed to 'git am'"), PARSE_OPT_NOARG),
1080 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1081 N_("passed to 'git apply'"), 0),
1082 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1083 N_("action"), N_("passed to 'git apply'"), 0),
1084 OPT_BIT('f', "force-rebase", &options.flags,
1085 N_("cherry-pick all commits, even if unchanged"),
1087 OPT_BIT(0, "no-ff", &options.flags,
1088 N_("cherry-pick all commits, even if unchanged"),
1090 OPT_CMDMODE(0, "continue", &action, N_("continue"),
1092 OPT_CMDMODE(0, "skip", &action,
1093 N_("skip current patch and continue"), ACTION_SKIP),
1094 OPT_CMDMODE(0, "abort", &action,
1095 N_("abort and check out the original branch"),
1097 OPT_CMDMODE(0, "quit", &action,
1098 N_("abort but keep HEAD where it is"), ACTION_QUIT),
1099 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
1100 "during an interactive rebase"), ACTION_EDIT_TODO),
1101 OPT_CMDMODE(0, "show-current-patch", &action,
1102 N_("show the patch file being applied or merged"),
1103 ACTION_SHOW_CURRENT_PATCH),
1104 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
1105 N_("use merging strategies to rebase"),
1106 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1108 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
1109 N_("let the user edit the list of commits to rebase"),
1110 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1111 parse_opt_interactive },
1112 OPT_SET_INT('p', "preserve-merges", &options.type,
1113 N_("try to recreate merges instead of ignoring "
1114 "them"), REBASE_PRESERVE_MERGES),
1115 OPT_BOOL(0, "rerere-autoupdate",
1116 &options.allow_rerere_autoupdate,
1117 N_("allow rerere to update index with resolved "
1119 OPT_BOOL('k', "keep-empty", &options.keep_empty,
1120 N_("preserve empty commits during rebase")),
1121 OPT_BOOL(0, "autosquash", &options.autosquash,
1122 N_("move commits that begin with "
1123 "squash!/fixup! under -i")),
1124 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1125 N_("GPG-sign commits"),
1126 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
1127 OPT_BOOL(0, "autostash", &options.autostash,
1128 N_("automatically stash/stash pop before and after")),
1129 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
1130 N_("add exec lines after each commit of the "
1132 { OPTION_CALLBACK, 'y', NULL, &opt_y, N_("<cmd>"),
1133 N_("same as --reschedule-failed-exec -x <cmd>"),
1134 PARSE_OPT_NONEG, parse_opt_y },
1135 OPT_BOOL(0, "allow-empty-message",
1136 &options.allow_empty_message,
1137 N_("allow rebasing commits with empty messages")),
1138 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
1140 N_("try to rebase merges instead of skipping them"),
1141 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
1142 OPT_BOOL(0, "fork-point", &fork_point,
1143 N_("use 'merge-base --fork-point' to refine upstream")),
1144 OPT_STRING('s', "strategy", &options.strategy,
1145 N_("strategy"), N_("use the given merge strategy")),
1146 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
1148 N_("pass the argument through to the merge "
1150 OPT_BOOL(0, "root", &options.root,
1151 N_("rebase all reachable commits up to the root(s)")),
1152 OPT_BOOL(0, "reschedule-failed-exec",
1153 &options.reschedule_failed_exec,
1154 N_("automatically re-schedule any `exec` that fails")),
1160 * NEEDSWORK: Once the builtin rebase has been tested enough
1161 * and git-legacy-rebase.sh is retired to contrib/, this preamble
1165 if (!use_builtin_rebase()) {
1166 const char *path = mkpath("%s/git-legacy-rebase",
1169 if (sane_execvp(path, (char **)argv) < 0)
1170 die_errno(_("could not exec %s"), path);
1172 BUG("sane_execvp() returned???");
1175 if (argc == 2 && !strcmp(argv[1], "-h"))
1176 usage_with_options(builtin_rebase_usage,
1177 builtin_rebase_options);
1179 prefix = setup_git_directory();
1180 trace_repo_setup(prefix);
1183 git_config(rebase_config, &options);
1186 strbuf_addf(&buf, "%s/applying", apply_dir());
1187 if(file_exists(buf.buf))
1188 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1190 if (is_directory(apply_dir())) {
1191 options.type = REBASE_AM;
1192 options.state_dir = apply_dir();
1193 } else if (is_directory(merge_dir())) {
1195 strbuf_addf(&buf, "%s/rewritten", merge_dir());
1196 if (is_directory(buf.buf)) {
1197 options.type = REBASE_PRESERVE_MERGES;
1198 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1201 strbuf_addf(&buf, "%s/interactive", merge_dir());
1202 if(file_exists(buf.buf)) {
1203 options.type = REBASE_INTERACTIVE;
1204 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1206 options.type = REBASE_MERGE;
1208 options.state_dir = merge_dir();
1211 if (options.type != REBASE_UNSPECIFIED)
1215 argc = parse_options(argc, argv, prefix,
1216 builtin_rebase_options,
1217 builtin_rebase_usage, 0);
1219 if (action != NO_ACTION && total_argc != 2) {
1220 usage_with_options(builtin_rebase_usage,
1221 builtin_rebase_options);
1225 usage_with_options(builtin_rebase_usage,
1226 builtin_rebase_options);
1228 if (action != NO_ACTION && !in_progress)
1229 die(_("No rebase in progress?"));
1230 setenv(GIT_REFLOG_ACTION_ENVIRONMENT, "rebase", 0);
1232 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
1233 die(_("The --edit-todo action can only be used during "
1234 "interactive rebase."));
1237 case ACTION_CONTINUE: {
1238 struct object_id head;
1239 struct lock_file lock_file = LOCK_INIT;
1242 options.action = "continue";
1243 set_reflog_action(&options);
1246 if (get_oid("HEAD", &head))
1247 die(_("Cannot read HEAD"));
1249 fd = hold_locked_index(&lock_file, 0);
1250 if (repo_read_index(the_repository) < 0)
1251 die(_("could not read index"));
1252 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1255 repo_update_index_if_able(the_repository, &lock_file);
1256 rollback_lock_file(&lock_file);
1258 if (has_unstaged_changes(the_repository, 1)) {
1259 puts(_("You must edit all merge conflicts and then\n"
1260 "mark them as resolved using git add"));
1263 if (read_basic_state(&options))
1268 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1270 options.action = "skip";
1271 set_reflog_action(&options);
1273 rerere_clear(the_repository, &merge_rr);
1274 string_list_clear(&merge_rr, 1);
1276 if (reset_head(NULL, "reset", NULL, RESET_HEAD_HARD,
1278 die(_("could not discard worktree changes"));
1279 remove_branch_state(the_repository);
1280 if (read_basic_state(&options))
1284 case ACTION_ABORT: {
1285 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1286 options.action = "abort";
1287 set_reflog_action(&options);
1289 rerere_clear(the_repository, &merge_rr);
1290 string_list_clear(&merge_rr, 1);
1292 if (read_basic_state(&options))
1294 if (reset_head(&options.orig_head, "reset",
1295 options.head_name, RESET_HEAD_HARD,
1297 die(_("could not move back to %s"),
1298 oid_to_hex(&options.orig_head));
1299 remove_branch_state(the_repository);
1300 ret = finish_rebase(&options);
1305 strbuf_addstr(&buf, options.state_dir);
1306 ret = !!remove_dir_recursively(&buf, 0);
1308 die(_("could not remove '%s'"), options.state_dir);
1311 case ACTION_EDIT_TODO:
1312 options.action = "edit-todo";
1313 options.dont_finish_rebase = 1;
1315 case ACTION_SHOW_CURRENT_PATCH:
1316 options.action = "show-current-patch";
1317 options.dont_finish_rebase = 1;
1322 BUG("action: %d", action);
1325 /* Make sure no rebase is in progress */
1327 const char *last_slash = strrchr(options.state_dir, '/');
1328 const char *state_dir_base =
1329 last_slash ? last_slash + 1 : options.state_dir;
1330 const char *cmd_live_rebase =
1331 "git rebase (--continue | --abort | --skip)";
1333 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1334 die(_("It seems that there is already a %s directory, and\n"
1335 "I wonder if you are in the middle of another rebase. "
1337 "case, please try\n\t%s\n"
1338 "If that is not the case, please\n\t%s\n"
1339 "and run me again. I am stopping in case you still "
1341 "valuable there.\n"),
1342 state_dir_base, cmd_live_rebase, buf.buf);
1345 for (i = 0; i < options.git_am_opts.argc; i++) {
1346 const char *option = options.git_am_opts.argv[i], *p;
1347 if (!strcmp(option, "--committer-date-is-author-date") ||
1348 !strcmp(option, "--ignore-date") ||
1349 !strcmp(option, "--whitespace=fix") ||
1350 !strcmp(option, "--whitespace=strip"))
1351 options.flags |= REBASE_FORCE;
1352 else if (skip_prefix(option, "-C", &p)) {
1354 if (!isdigit(*(p++)))
1355 die(_("switch `C' expects a "
1356 "numerical value"));
1357 } else if (skip_prefix(option, "--whitespace=", &p)) {
1358 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1359 strcmp(p, "error") && strcmp(p, "error-all"))
1360 die("Invalid whitespace option: '%s'", p);
1364 for (i = 0; i < exec.nr; i++)
1365 if (check_exec_cmd(exec.items[i].string))
1368 if (!(options.flags & REBASE_NO_QUIET))
1369 argv_array_push(&options.git_am_opts, "-q");
1371 if (options.keep_empty)
1372 imply_interactive(&options, "--keep-empty");
1375 free(options.gpg_sign_opt);
1376 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1382 imply_interactive(&options, "--exec");
1385 for (i = 0; i < exec.nr; i++)
1386 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1387 options.cmd = xstrdup(buf.buf);
1390 if (rebase_merges) {
1391 if (!*rebase_merges)
1392 ; /* default mode; do nothing */
1393 else if (!strcmp("rebase-cousins", rebase_merges))
1394 options.rebase_cousins = 1;
1395 else if (strcmp("no-rebase-cousins", rebase_merges))
1396 die(_("Unknown mode: %s"), rebase_merges);
1397 options.rebase_merges = 1;
1398 imply_interactive(&options, "--rebase-merges");
1401 if (strategy_options.nr) {
1404 if (!options.strategy)
1405 options.strategy = "recursive";
1408 for (i = 0; i < strategy_options.nr; i++)
1409 strbuf_addf(&buf, " --%s",
1410 strategy_options.items[i].string);
1411 options.strategy_opts = xstrdup(buf.buf);
1414 if (options.strategy) {
1415 options.strategy = xstrdup(options.strategy);
1416 switch (options.type) {
1418 die(_("--strategy requires --merge or --interactive"));
1420 case REBASE_INTERACTIVE:
1421 case REBASE_PRESERVE_MERGES:
1424 case REBASE_UNSPECIFIED:
1425 options.type = REBASE_MERGE;
1428 BUG("unhandled rebase type (%d)", options.type);
1432 if (options.type == REBASE_MERGE)
1433 imply_interactive(&options, "--merge");
1435 if (options.root && !options.onto_name)
1436 imply_interactive(&options, "--root without --onto");
1438 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1439 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1441 switch (options.type) {
1443 case REBASE_INTERACTIVE:
1444 case REBASE_PRESERVE_MERGES:
1445 options.state_dir = merge_dir();
1448 options.state_dir = apply_dir();
1451 /* the default rebase backend is `--am` */
1452 options.type = REBASE_AM;
1453 options.state_dir = apply_dir();
1457 if (options.reschedule_failed_exec && !is_interactive(&options))
1458 die(_("--reschedule-failed-exec requires an interactive rebase"));
1460 if (options.git_am_opts.argc) {
1461 /* all am options except -q are compatible only with --am */
1462 for (i = options.git_am_opts.argc - 1; i >= 0; i--)
1463 if (strcmp(options.git_am_opts.argv[i], "-q"))
1466 if (is_interactive(&options) && i >= 0)
1467 die(_("cannot combine am options with either "
1468 "interactive or merge options"));
1471 if (options.signoff) {
1472 if (options.type == REBASE_PRESERVE_MERGES)
1473 die("cannot combine '--signoff' with "
1474 "'--preserve-merges'");
1475 argv_array_push(&options.git_am_opts, "--signoff");
1476 options.flags |= REBASE_FORCE;
1479 if (options.type == REBASE_PRESERVE_MERGES) {
1481 * Note: incompatibility with --signoff handled in signoff block above
1482 * Note: incompatibility with --interactive is just a strong warning;
1483 * git-rebase.txt caveats with "unless you know what you are doing"
1485 if (options.rebase_merges)
1486 die(_("cannot combine '--preserve-merges' with "
1487 "'--rebase-merges'"));
1489 if (options.reschedule_failed_exec)
1490 die(_("error: cannot combine '--preserve-merges' with "
1491 "'--reschedule-failed-exec'"));
1494 if (options.rebase_merges) {
1495 if (strategy_options.nr)
1496 die(_("cannot combine '--rebase-merges' with "
1497 "'--strategy-option'"));
1498 if (options.strategy)
1499 die(_("cannot combine '--rebase-merges' with "
1503 if (!options.root) {
1505 struct branch *branch;
1507 branch = branch_get(NULL);
1508 options.upstream_name = branch_get_upstream(branch,
1510 if (!options.upstream_name)
1511 error_on_missing_default_upstream();
1515 options.upstream_name = argv[0];
1518 if (!strcmp(options.upstream_name, "-"))
1519 options.upstream_name = "@{-1}";
1521 options.upstream = peel_committish(options.upstream_name);
1522 if (!options.upstream)
1523 die(_("invalid upstream '%s'"), options.upstream_name);
1524 options.upstream_arg = options.upstream_name;
1526 if (!options.onto_name) {
1527 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1528 &squash_onto, NULL, NULL) < 0)
1529 die(_("Could not create new root commit"));
1530 options.squash_onto = &squash_onto;
1531 options.onto_name = squash_onto_name =
1532 xstrdup(oid_to_hex(&squash_onto));
1534 options.upstream_name = NULL;
1535 options.upstream = NULL;
1537 usage_with_options(builtin_rebase_usage,
1538 builtin_rebase_options);
1539 options.upstream_arg = "--root";
1542 /* Make sure the branch to rebase onto is valid. */
1543 if (!options.onto_name)
1544 options.onto_name = options.upstream_name;
1545 if (strstr(options.onto_name, "...")) {
1546 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1547 die(_("'%s': need exactly one merge base"),
1549 options.onto = lookup_commit_or_die(&merge_base,
1552 options.onto = peel_committish(options.onto_name);
1554 die(_("Does not point to a valid commit '%s'"),
1559 * If the branch to rebase is given, that is the branch we will rebase
1560 * branch_name -- branch/commit being rebased, or
1561 * HEAD (already detached)
1562 * orig_head -- commit object name of tip of the branch before rebasing
1563 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1566 /* Is it "rebase other branchname" or "rebase other commit"? */
1567 branch_name = argv[0];
1568 options.switch_to = argv[0];
1570 /* Is it a local branch? */
1572 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1573 if (!read_ref(buf.buf, &options.orig_head))
1574 options.head_name = xstrdup(buf.buf);
1575 /* If not is it a valid ref (branch or commit)? */
1576 else if (!get_oid(branch_name, &options.orig_head))
1577 options.head_name = NULL;
1579 die(_("fatal: no such branch/commit '%s'"),
1581 } else if (argc == 0) {
1582 /* Do not need to switch branches, we are already on it. */
1584 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1586 if (!options.head_name)
1587 die(_("No such ref: %s"), "HEAD");
1588 if (flags & REF_ISSYMREF) {
1589 if (!skip_prefix(options.head_name,
1590 "refs/heads/", &branch_name))
1591 branch_name = options.head_name;
1594 free(options.head_name);
1595 options.head_name = NULL;
1596 branch_name = "HEAD";
1598 if (get_oid("HEAD", &options.orig_head))
1599 die(_("Could not resolve HEAD to a revision"));
1601 BUG("unexpected number of arguments left to parse");
1603 if (fork_point > 0) {
1604 struct commit *head =
1605 lookup_commit_reference(the_repository,
1606 &options.orig_head);
1607 options.restrict_revision =
1608 get_fork_point(options.upstream_name, head);
1611 if (repo_read_index(the_repository) < 0)
1612 die(_("could not read index"));
1614 if (options.autostash) {
1615 struct lock_file lock_file = LOCK_INIT;
1618 fd = hold_locked_index(&lock_file, 0);
1619 refresh_cache(REFRESH_QUIET);
1621 repo_update_index_if_able(the_repository, &lock_file);
1622 rollback_lock_file(&lock_file);
1624 if (has_unstaged_changes(the_repository, 1) ||
1625 has_uncommitted_changes(the_repository, 1)) {
1626 const char *autostash =
1627 state_dir_path("autostash", &options);
1628 struct child_process stash = CHILD_PROCESS_INIT;
1629 struct object_id oid;
1630 struct commit *head =
1631 lookup_commit_reference(the_repository,
1632 &options.orig_head);
1634 argv_array_pushl(&stash.args,
1635 "stash", "create", "autostash", NULL);
1639 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1640 die(_("Cannot autostash"));
1641 strbuf_trim_trailing_newline(&buf);
1642 if (get_oid(buf.buf, &oid))
1643 die(_("Unexpected stash response: '%s'"),
1646 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1648 if (safe_create_leading_directories_const(autostash))
1649 die(_("Could not create directory for '%s'"),
1651 write_file(autostash, "%s", oid_to_hex(&oid));
1652 printf(_("Created autostash: %s\n"), buf.buf);
1653 if (reset_head(&head->object.oid, "reset --hard",
1654 NULL, RESET_HEAD_HARD, NULL, NULL) < 0)
1655 die(_("could not reset --hard"));
1656 printf(_("HEAD is now at %s"),
1657 find_unique_abbrev(&head->object.oid,
1660 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1662 printf(" %s", buf.buf);
1665 if (discard_index(the_repository->index) < 0 ||
1666 repo_read_index(the_repository) < 0)
1667 die(_("could not read index"));
1671 if (require_clean_work_tree(the_repository, "rebase",
1672 _("Please commit or stash them."), 1, 1)) {
1678 * Now we are rebasing commits upstream..orig_head (or with --root,
1679 * everything leading up to orig_head) on top of onto.
1683 * Check if we are already based on onto with linear history,
1684 * but this should be done only when upstream and onto are the same
1685 * and if this is not an interactive rebase.
1687 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1688 !is_interactive(&options) && !options.restrict_revision &&
1690 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1693 if (!(options.flags & REBASE_FORCE)) {
1694 /* Lazily switch to the target branch if needed... */
1695 if (options.switch_to) {
1696 struct object_id oid;
1698 if (get_oid(options.switch_to, &oid) < 0) {
1699 ret = !!error(_("could not parse '%s'"),
1705 strbuf_addf(&buf, "%s: checkout %s",
1706 getenv(GIT_REFLOG_ACTION_ENVIRONMENT),
1708 if (reset_head(&oid, "checkout",
1710 RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1711 NULL, buf.buf) < 0) {
1712 ret = !!error(_("could not switch to "
1719 if (!(options.flags & REBASE_NO_QUIET))
1721 else if (!strcmp(branch_name, "HEAD") &&
1722 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1723 puts(_("HEAD is up to date."));
1725 printf(_("Current branch %s is up to date.\n"),
1727 ret = !!finish_rebase(&options);
1729 } else if (!(options.flags & REBASE_NO_QUIET))
1731 else if (!strcmp(branch_name, "HEAD") &&
1732 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1733 puts(_("HEAD is up to date, rebase forced."));
1735 printf(_("Current branch %s is up to date, rebase "
1736 "forced.\n"), branch_name);
1739 /* If a hook exists, give it a chance to interrupt*/
1740 if (!ok_to_skip_pre_rebase &&
1741 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1742 argc ? argv[0] : NULL, NULL))
1743 die(_("The pre-rebase hook refused to rebase."));
1745 if (options.flags & REBASE_DIFFSTAT) {
1746 struct diff_options opts;
1748 if (options.flags & REBASE_VERBOSE) {
1749 if (is_null_oid(&merge_base))
1750 printf(_("Changes to %s:\n"),
1751 oid_to_hex(&options.onto->object.oid));
1753 printf(_("Changes from %s to %s:\n"),
1754 oid_to_hex(&merge_base),
1755 oid_to_hex(&options.onto->object.oid));
1758 /* We want color (if set), but no pager */
1760 opts.stat_width = -1; /* use full terminal width */
1761 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1762 opts.output_format |=
1763 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1764 opts.detect_rename = DIFF_DETECT_RENAME;
1765 diff_setup_done(&opts);
1766 diff_tree_oid(is_null_oid(&merge_base) ?
1767 the_hash_algo->empty_tree : &merge_base,
1768 &options.onto->object.oid, "", &opts);
1769 diffcore_std(&opts);
1773 if (is_interactive(&options))
1776 /* Detach HEAD and reset the tree */
1777 if (options.flags & REBASE_NO_QUIET)
1778 printf(_("First, rewinding head to replay your work on top of "
1781 strbuf_addf(&msg, "%s: checkout %s",
1782 getenv(GIT_REFLOG_ACTION_ENVIRONMENT), options.onto_name);
1783 if (reset_head(&options.onto->object.oid, "checkout", NULL,
1784 RESET_HEAD_DETACH | RESET_HEAD_RUN_POST_CHECKOUT_HOOK,
1786 die(_("Could not detach HEAD"));
1787 strbuf_release(&msg);
1790 * If the onto is a proper descendant of the tip of the branch, then
1791 * we just fast-forwarded.
1794 if (!oidcmp(&merge_base, &options.orig_head)) {
1795 printf(_("Fast-forwarded %s to %s.\n"),
1796 branch_name, options.onto_name);
1797 strbuf_addf(&msg, "rebase finished: %s onto %s",
1798 options.head_name ? options.head_name : "detached HEAD",
1799 oid_to_hex(&options.onto->object.oid));
1800 reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1802 strbuf_release(&msg);
1803 ret = !!finish_rebase(&options);
1807 strbuf_addf(&revisions, "%s..%s",
1808 options.root ? oid_to_hex(&options.onto->object.oid) :
1809 (options.restrict_revision ?
1810 oid_to_hex(&options.restrict_revision->object.oid) :
1811 oid_to_hex(&options.upstream->object.oid)),
1812 oid_to_hex(&options.orig_head));
1814 options.revisions = revisions.buf;
1817 ret = !!run_specific_rebase(&options);
1820 strbuf_release(&revisions);
1821 free(options.head_name);
1822 free(options.gpg_sign_opt);
1824 free(squash_onto_name);