Merge branch 'pk/rebase-in-c-5-test'
[git/gitweb.git] / builtin / rebase.c
blob6fe17d198f07010aeb7eddda4af3e2bd312e1201
1 /*
2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
5 */
7 #include "builtin.h"
8 #include "run-command.h"
9 #include "exec-cmd.h"
10 #include "argv-array.h"
11 #include "dir.h"
12 #include "packfile.h"
13 #include "refs.h"
14 #include "quote.h"
15 #include "config.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
18 #include "lockfile.h"
19 #include "parse-options.h"
20 #include "commit.h"
21 #include "diff.h"
22 #include "wt-status.h"
23 #include "revision.h"
24 #include "commit-reach.h"
25 #include "rerere.h"
27 static char const * const builtin_rebase_usage[] = {
28 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
29 "[<upstream>] [<branch>]"),
30 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
31 "--root [<branch>]"),
32 N_("git rebase --continue | --abort | --skip | --edit-todo"),
33 NULL
36 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
37 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
39 enum rebase_type {
40 REBASE_UNSPECIFIED = -1,
41 REBASE_AM,
42 REBASE_MERGE,
43 REBASE_INTERACTIVE,
44 REBASE_PRESERVE_MERGES
47 static int use_builtin_rebase(void)
49 struct child_process cp = CHILD_PROCESS_INIT;
50 struct strbuf out = STRBUF_INIT;
51 int ret;
53 argv_array_pushl(&cp.args,
54 "config", "--bool", "rebase.usebuiltin", NULL);
55 cp.git_cmd = 1;
56 if (capture_command(&cp, &out, 6)) {
57 strbuf_release(&out);
58 return 0;
61 strbuf_trim(&out);
62 ret = !strcmp("true", out.buf);
63 strbuf_release(&out);
64 return ret;
67 struct rebase_options {
68 enum rebase_type type;
69 const char *state_dir;
70 struct commit *upstream;
71 const char *upstream_name;
72 const char *upstream_arg;
73 char *head_name;
74 struct object_id orig_head;
75 struct commit *onto;
76 const char *onto_name;
77 const char *revisions;
78 const char *switch_to;
79 int root;
80 struct object_id *squash_onto;
81 struct commit *restrict_revision;
82 int dont_finish_rebase;
83 enum {
84 REBASE_NO_QUIET = 1<<0,
85 REBASE_VERBOSE = 1<<1,
86 REBASE_DIFFSTAT = 1<<2,
87 REBASE_FORCE = 1<<3,
88 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
89 } flags;
90 struct strbuf git_am_opt;
91 const char *action;
92 int signoff;
93 int allow_rerere_autoupdate;
94 int keep_empty;
95 int autosquash;
96 char *gpg_sign_opt;
97 int autostash;
98 char *cmd;
99 int allow_empty_message;
100 int rebase_merges, rebase_cousins;
101 char *strategy, *strategy_opts;
102 struct strbuf git_format_patch_opt;
105 static int is_interactive(struct rebase_options *opts)
107 return opts->type == REBASE_INTERACTIVE ||
108 opts->type == REBASE_PRESERVE_MERGES;
111 static void imply_interactive(struct rebase_options *opts, const char *option)
113 switch (opts->type) {
114 case REBASE_AM:
115 die(_("%s requires an interactive rebase"), option);
116 break;
117 case REBASE_INTERACTIVE:
118 case REBASE_PRESERVE_MERGES:
119 break;
120 case REBASE_MERGE:
121 /* we silently *upgrade* --merge to --interactive if needed */
122 default:
123 opts->type = REBASE_INTERACTIVE; /* implied */
124 break;
128 /* Returns the filename prefixed by the state_dir */
129 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
131 static struct strbuf path = STRBUF_INIT;
132 static size_t prefix_len;
134 if (!prefix_len) {
135 strbuf_addf(&path, "%s/", opts->state_dir);
136 prefix_len = path.len;
139 strbuf_setlen(&path, prefix_len);
140 strbuf_addstr(&path, filename);
141 return path.buf;
144 /* Read one file, then strip line endings */
145 static int read_one(const char *path, struct strbuf *buf)
147 if (strbuf_read_file(buf, path, 0) < 0)
148 return error_errno(_("could not read '%s'"), path);
149 strbuf_trim_trailing_newline(buf);
150 return 0;
153 /* Initialize the rebase options from the state directory. */
154 static int read_basic_state(struct rebase_options *opts)
156 struct strbuf head_name = STRBUF_INIT;
157 struct strbuf buf = STRBUF_INIT;
158 struct object_id oid;
160 if (read_one(state_dir_path("head-name", opts), &head_name) ||
161 read_one(state_dir_path("onto", opts), &buf))
162 return -1;
163 opts->head_name = starts_with(head_name.buf, "refs/") ?
164 xstrdup(head_name.buf) : NULL;
165 strbuf_release(&head_name);
166 if (get_oid(buf.buf, &oid))
167 return error(_("could not get 'onto': '%s'"), buf.buf);
168 opts->onto = lookup_commit_or_die(&oid, buf.buf);
171 * We always write to orig-head, but interactive rebase used to write to
172 * head. Fall back to reading from head to cover for the case that the
173 * user upgraded git with an ongoing interactive rebase.
175 strbuf_reset(&buf);
176 if (file_exists(state_dir_path("orig-head", opts))) {
177 if (read_one(state_dir_path("orig-head", opts), &buf))
178 return -1;
179 } else if (read_one(state_dir_path("head", opts), &buf))
180 return -1;
181 if (get_oid(buf.buf, &opts->orig_head))
182 return error(_("invalid orig-head: '%s'"), buf.buf);
184 strbuf_reset(&buf);
185 if (read_one(state_dir_path("quiet", opts), &buf))
186 return -1;
187 if (buf.len)
188 opts->flags &= ~REBASE_NO_QUIET;
189 else
190 opts->flags |= REBASE_NO_QUIET;
192 if (file_exists(state_dir_path("verbose", opts)))
193 opts->flags |= REBASE_VERBOSE;
195 if (file_exists(state_dir_path("signoff", opts))) {
196 opts->signoff = 1;
197 opts->flags |= REBASE_FORCE;
200 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
201 strbuf_reset(&buf);
202 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
203 &buf))
204 return -1;
205 if (!strcmp(buf.buf, "--rerere-autoupdate"))
206 opts->allow_rerere_autoupdate = 1;
207 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
208 opts->allow_rerere_autoupdate = 0;
209 else
210 warning(_("ignoring invalid allow_rerere_autoupdate: "
211 "'%s'"), buf.buf);
212 } else
213 opts->allow_rerere_autoupdate = -1;
215 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
216 strbuf_reset(&buf);
217 if (read_one(state_dir_path("gpg_sign_opt", opts),
218 &buf))
219 return -1;
220 free(opts->gpg_sign_opt);
221 opts->gpg_sign_opt = xstrdup(buf.buf);
224 if (file_exists(state_dir_path("strategy", opts))) {
225 strbuf_reset(&buf);
226 if (read_one(state_dir_path("strategy", opts), &buf))
227 return -1;
228 free(opts->strategy);
229 opts->strategy = xstrdup(buf.buf);
232 if (file_exists(state_dir_path("strategy_opts", opts))) {
233 strbuf_reset(&buf);
234 if (read_one(state_dir_path("strategy_opts", opts), &buf))
235 return -1;
236 free(opts->strategy_opts);
237 opts->strategy_opts = xstrdup(buf.buf);
240 strbuf_release(&buf);
242 return 0;
245 static int apply_autostash(struct rebase_options *opts)
247 const char *path = state_dir_path("autostash", opts);
248 struct strbuf autostash = STRBUF_INIT;
249 struct child_process stash_apply = CHILD_PROCESS_INIT;
251 if (!file_exists(path))
252 return 0;
254 if (read_one(state_dir_path("autostash", opts), &autostash))
255 return error(_("Could not read '%s'"), path);
256 argv_array_pushl(&stash_apply.args,
257 "stash", "apply", autostash.buf, NULL);
258 stash_apply.git_cmd = 1;
259 stash_apply.no_stderr = stash_apply.no_stdout =
260 stash_apply.no_stdin = 1;
261 if (!run_command(&stash_apply))
262 printf(_("Applied autostash.\n"));
263 else {
264 struct argv_array args = ARGV_ARRAY_INIT;
265 int res = 0;
267 argv_array_pushl(&args,
268 "stash", "store", "-m", "autostash", "-q",
269 autostash.buf, NULL);
270 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
271 res = error(_("Cannot store %s"), autostash.buf);
272 argv_array_clear(&args);
273 strbuf_release(&autostash);
274 if (res)
275 return res;
277 fprintf(stderr,
278 _("Applying autostash resulted in conflicts.\n"
279 "Your changes are safe in the stash.\n"
280 "You can run \"git stash pop\" or \"git stash drop\" "
281 "at any time.\n"));
284 strbuf_release(&autostash);
285 return 0;
288 static int finish_rebase(struct rebase_options *opts)
290 struct strbuf dir = STRBUF_INIT;
291 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
293 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
294 apply_autostash(opts);
295 close_all_packs(the_repository->objects);
297 * We ignore errors in 'gc --auto', since the
298 * user should see them.
300 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
301 strbuf_addstr(&dir, opts->state_dir);
302 remove_dir_recursively(&dir, 0);
303 strbuf_release(&dir);
305 return 0;
308 static struct commit *peel_committish(const char *name)
310 struct object *obj;
311 struct object_id oid;
313 if (get_oid(name, &oid))
314 return NULL;
315 obj = parse_object(the_repository, &oid);
316 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
319 static void add_var(struct strbuf *buf, const char *name, const char *value)
321 if (!value)
322 strbuf_addf(buf, "unset %s; ", name);
323 else {
324 strbuf_addf(buf, "%s=", name);
325 sq_quote_buf(buf, value);
326 strbuf_addstr(buf, "; ");
330 static int run_specific_rebase(struct rebase_options *opts)
332 const char *argv[] = { NULL, NULL };
333 struct strbuf script_snippet = STRBUF_INIT;
334 int status;
335 const char *backend, *backend_func;
337 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
338 add_var(&script_snippet, "state_dir", opts->state_dir);
340 add_var(&script_snippet, "upstream_name", opts->upstream_name);
341 add_var(&script_snippet, "upstream", opts->upstream ?
342 oid_to_hex(&opts->upstream->object.oid) : NULL);
343 add_var(&script_snippet, "head_name",
344 opts->head_name ? opts->head_name : "detached HEAD");
345 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
346 add_var(&script_snippet, "onto", opts->onto ?
347 oid_to_hex(&opts->onto->object.oid) : NULL);
348 add_var(&script_snippet, "onto_name", opts->onto_name);
349 add_var(&script_snippet, "revisions", opts->revisions);
350 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
351 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
352 add_var(&script_snippet, "GIT_QUIET",
353 opts->flags & REBASE_NO_QUIET ? "" : "t");
354 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
355 add_var(&script_snippet, "verbose",
356 opts->flags & REBASE_VERBOSE ? "t" : "");
357 add_var(&script_snippet, "diffstat",
358 opts->flags & REBASE_DIFFSTAT ? "t" : "");
359 add_var(&script_snippet, "force_rebase",
360 opts->flags & REBASE_FORCE ? "t" : "");
361 if (opts->switch_to)
362 add_var(&script_snippet, "switch_to", opts->switch_to);
363 add_var(&script_snippet, "action", opts->action ? opts->action : "");
364 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
365 add_var(&script_snippet, "allow_rerere_autoupdate",
366 opts->allow_rerere_autoupdate < 0 ? "" :
367 opts->allow_rerere_autoupdate ?
368 "--rerere-autoupdate" : "--no-rerere-autoupdate");
369 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
370 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
371 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
372 add_var(&script_snippet, "cmd", opts->cmd);
373 add_var(&script_snippet, "allow_empty_message",
374 opts->allow_empty_message ? "--allow-empty-message" : "");
375 add_var(&script_snippet, "rebase_merges",
376 opts->rebase_merges ? "t" : "");
377 add_var(&script_snippet, "rebase_cousins",
378 opts->rebase_cousins ? "t" : "");
379 add_var(&script_snippet, "strategy", opts->strategy);
380 add_var(&script_snippet, "strategy_opts", opts->strategy_opts);
381 add_var(&script_snippet, "rebase_root", opts->root ? "t" : "");
382 add_var(&script_snippet, "squash_onto",
383 opts->squash_onto ? oid_to_hex(opts->squash_onto) : "");
384 add_var(&script_snippet, "git_format_patch_opt",
385 opts->git_format_patch_opt.buf);
387 if (is_interactive(opts) &&
388 !(opts->flags & REBASE_INTERACTIVE_EXPLICIT)) {
389 strbuf_addstr(&script_snippet,
390 "GIT_EDITOR=:; export GIT_EDITOR; ");
391 opts->autosquash = 0;
394 switch (opts->type) {
395 case REBASE_AM:
396 backend = "git-rebase--am";
397 backend_func = "git_rebase__am";
398 break;
399 case REBASE_INTERACTIVE:
400 backend = "git-rebase--interactive";
401 backend_func = "git_rebase__interactive";
402 break;
403 case REBASE_MERGE:
404 backend = "git-rebase--merge";
405 backend_func = "git_rebase__merge";
406 break;
407 case REBASE_PRESERVE_MERGES:
408 backend = "git-rebase--preserve-merges";
409 backend_func = "git_rebase__preserve_merges";
410 break;
411 default:
412 BUG("Unhandled rebase type %d", opts->type);
413 break;
416 strbuf_addf(&script_snippet,
417 ". git-sh-setup && . git-rebase--common &&"
418 " . %s && %s", backend, backend_func);
419 argv[0] = script_snippet.buf;
421 status = run_command_v_opt(argv, RUN_USING_SHELL);
422 if (opts->dont_finish_rebase)
423 ; /* do nothing */
424 else if (status == 0) {
425 if (!file_exists(state_dir_path("stopped-sha", opts)))
426 finish_rebase(opts);
427 } else if (status == 2) {
428 struct strbuf dir = STRBUF_INIT;
430 apply_autostash(opts);
431 strbuf_addstr(&dir, opts->state_dir);
432 remove_dir_recursively(&dir, 0);
433 strbuf_release(&dir);
434 die("Nothing to do");
437 strbuf_release(&script_snippet);
439 return status ? -1 : 0;
442 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
444 static int reset_head(struct object_id *oid, const char *action,
445 const char *switch_to_branch, int detach_head,
446 const char *reflog_orig_head, const char *reflog_head)
448 struct object_id head_oid;
449 struct tree_desc desc;
450 struct lock_file lock = LOCK_INIT;
451 struct unpack_trees_options unpack_tree_opts;
452 struct tree *tree;
453 const char *reflog_action;
454 struct strbuf msg = STRBUF_INIT;
455 size_t prefix_len;
456 struct object_id *orig = NULL, oid_orig,
457 *old_orig = NULL, oid_old_orig;
458 int ret = 0;
460 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
461 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
463 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
464 return -1;
466 if (!oid) {
467 if (get_oid("HEAD", &head_oid)) {
468 rollback_lock_file(&lock);
469 return error(_("could not determine HEAD revision"));
471 oid = &head_oid;
474 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
475 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
476 unpack_tree_opts.head_idx = 1;
477 unpack_tree_opts.src_index = the_repository->index;
478 unpack_tree_opts.dst_index = the_repository->index;
479 unpack_tree_opts.fn = oneway_merge;
480 unpack_tree_opts.update = 1;
481 unpack_tree_opts.merge = 1;
482 if (!detach_head)
483 unpack_tree_opts.reset = 1;
485 if (read_index_unmerged(the_repository->index) < 0) {
486 rollback_lock_file(&lock);
487 return error(_("could not read index"));
490 if (!fill_tree_descriptor(&desc, oid)) {
491 error(_("failed to find tree of %s"), oid_to_hex(oid));
492 rollback_lock_file(&lock);
493 free((void *)desc.buffer);
494 return -1;
497 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
498 rollback_lock_file(&lock);
499 free((void *)desc.buffer);
500 return -1;
503 tree = parse_tree_indirect(oid);
504 prime_cache_tree(the_repository->index, tree);
506 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
507 ret = error(_("could not write index"));
508 free((void *)desc.buffer);
510 if (ret)
511 return ret;
513 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
514 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
515 prefix_len = msg.len;
517 if (!get_oid("ORIG_HEAD", &oid_old_orig))
518 old_orig = &oid_old_orig;
519 if (!get_oid("HEAD", &oid_orig)) {
520 orig = &oid_orig;
521 if (!reflog_orig_head) {
522 strbuf_addstr(&msg, "updating ORIG_HEAD");
523 reflog_orig_head = msg.buf;
525 update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0,
526 UPDATE_REFS_MSG_ON_ERR);
527 } else if (old_orig)
528 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
529 if (!reflog_head) {
530 strbuf_setlen(&msg, prefix_len);
531 strbuf_addstr(&msg, "updating HEAD");
532 reflog_head = msg.buf;
534 if (!switch_to_branch)
535 ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF,
536 UPDATE_REFS_MSG_ON_ERR);
537 else {
538 ret = create_symref("HEAD", switch_to_branch, msg.buf);
539 if (!ret)
540 ret = update_ref(reflog_head, "HEAD", oid, NULL, 0,
541 UPDATE_REFS_MSG_ON_ERR);
544 strbuf_release(&msg);
545 return ret;
548 static int rebase_config(const char *var, const char *value, void *data)
550 struct rebase_options *opts = data;
552 if (!strcmp(var, "rebase.stat")) {
553 if (git_config_bool(var, value))
554 opts->flags |= REBASE_DIFFSTAT;
555 else
556 opts->flags &= !REBASE_DIFFSTAT;
557 return 0;
560 if (!strcmp(var, "rebase.autosquash")) {
561 opts->autosquash = git_config_bool(var, value);
562 return 0;
565 if (!strcmp(var, "commit.gpgsign")) {
566 free(opts->gpg_sign_opt);
567 opts->gpg_sign_opt = git_config_bool(var, value) ?
568 xstrdup("-S") : NULL;
569 return 0;
572 if (!strcmp(var, "rebase.autostash")) {
573 opts->autostash = git_config_bool(var, value);
574 return 0;
577 return git_default_config(var, value, data);
581 * Determines whether the commits in from..to are linear, i.e. contain
582 * no merge commits. This function *expects* `from` to be an ancestor of
583 * `to`.
585 static int is_linear_history(struct commit *from, struct commit *to)
587 while (to && to != from) {
588 parse_commit(to);
589 if (!to->parents)
590 return 1;
591 if (to->parents->next)
592 return 0;
593 to = to->parents->item;
595 return 1;
598 static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
599 struct object_id *merge_base)
601 struct commit *head = lookup_commit(the_repository, head_oid);
602 struct commit_list *merge_bases;
603 int res;
605 if (!head)
606 return 0;
608 merge_bases = get_merge_bases(onto, head);
609 if (merge_bases && !merge_bases->next) {
610 oidcpy(merge_base, &merge_bases->item->object.oid);
611 res = !oidcmp(merge_base, &onto->object.oid);
612 } else {
613 oidcpy(merge_base, &null_oid);
614 res = 0;
616 free_commit_list(merge_bases);
617 return res && is_linear_history(onto, head);
620 /* -i followed by -m is still -i */
621 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
623 struct rebase_options *opts = opt->value;
625 if (!is_interactive(opts))
626 opts->type = REBASE_MERGE;
628 return 0;
631 /* -i followed by -p is still explicitly interactive, but -p alone is not */
632 static int parse_opt_interactive(const struct option *opt, const char *arg,
633 int unset)
635 struct rebase_options *opts = opt->value;
637 opts->type = REBASE_INTERACTIVE;
638 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
640 return 0;
643 static void NORETURN error_on_missing_default_upstream(void)
645 struct branch *current_branch = branch_get(NULL);
647 printf(_("%s\n"
648 "Please specify which branch you want to rebase against.\n"
649 "See git-rebase(1) for details.\n"
650 "\n"
651 " git rebase '<branch>'\n"
652 "\n"),
653 current_branch ? _("There is no tracking information for "
654 "the current branch.") :
655 _("You are not currently on a branch."));
657 if (current_branch) {
658 const char *remote = current_branch->remote_name;
660 if (!remote)
661 remote = _("<remote>");
663 printf(_("If you wish to set tracking information for this "
664 "branch you can do so with:\n"
665 "\n"
666 " git branch --set-upstream-to=%s/<branch> %s\n"
667 "\n"),
668 remote, current_branch->name);
670 exit(1);
673 int cmd_rebase(int argc, const char **argv, const char *prefix)
675 struct rebase_options options = {
676 .type = REBASE_UNSPECIFIED,
677 .flags = REBASE_NO_QUIET,
678 .git_am_opt = STRBUF_INIT,
679 .allow_rerere_autoupdate = -1,
680 .allow_empty_message = 1,
681 .git_format_patch_opt = STRBUF_INIT,
683 const char *branch_name;
684 int ret, flags, total_argc, in_progress = 0;
685 int ok_to_skip_pre_rebase = 0;
686 struct strbuf msg = STRBUF_INIT;
687 struct strbuf revisions = STRBUF_INIT;
688 struct strbuf buf = STRBUF_INIT;
689 struct object_id merge_base;
690 enum {
691 NO_ACTION,
692 ACTION_CONTINUE,
693 ACTION_SKIP,
694 ACTION_ABORT,
695 ACTION_QUIT,
696 ACTION_EDIT_TODO,
697 ACTION_SHOW_CURRENT_PATCH,
698 } action = NO_ACTION;
699 int committer_date_is_author_date = 0;
700 int ignore_date = 0;
701 int ignore_whitespace = 0;
702 const char *gpg_sign = NULL;
703 int opt_c = -1;
704 struct string_list whitespace = STRING_LIST_INIT_NODUP;
705 struct string_list exec = STRING_LIST_INIT_NODUP;
706 const char *rebase_merges = NULL;
707 int fork_point = -1;
708 struct string_list strategy_options = STRING_LIST_INIT_NODUP;
709 struct object_id squash_onto;
710 char *squash_onto_name = NULL;
711 struct option builtin_rebase_options[] = {
712 OPT_STRING(0, "onto", &options.onto_name,
713 N_("revision"),
714 N_("rebase onto given branch instead of upstream")),
715 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
716 N_("allow pre-rebase hook to run")),
717 OPT_NEGBIT('q', "quiet", &options.flags,
718 N_("be quiet. implies --no-stat"),
719 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
720 OPT_BIT('v', "verbose", &options.flags,
721 N_("display a diffstat of what changed upstream"),
722 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
723 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
724 N_("do not show diffstat of what changed upstream"),
725 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
726 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
727 N_("passed to 'git apply'")),
728 OPT_BOOL(0, "signoff", &options.signoff,
729 N_("add a Signed-off-by: line to each commit")),
730 OPT_BOOL(0, "committer-date-is-author-date",
731 &committer_date_is_author_date,
732 N_("passed to 'git am'")),
733 OPT_BOOL(0, "ignore-date", &ignore_date,
734 N_("passed to 'git am'")),
735 OPT_BIT('f', "force-rebase", &options.flags,
736 N_("cherry-pick all commits, even if unchanged"),
737 REBASE_FORCE),
738 OPT_BIT(0, "no-ff", &options.flags,
739 N_("cherry-pick all commits, even if unchanged"),
740 REBASE_FORCE),
741 OPT_CMDMODE(0, "continue", &action, N_("continue"),
742 ACTION_CONTINUE),
743 OPT_CMDMODE(0, "skip", &action,
744 N_("skip current patch and continue"), ACTION_SKIP),
745 OPT_CMDMODE(0, "abort", &action,
746 N_("abort and check out the original branch"),
747 ACTION_ABORT),
748 OPT_CMDMODE(0, "quit", &action,
749 N_("abort but keep HEAD where it is"), ACTION_QUIT),
750 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
751 "during an interactive rebase"), ACTION_EDIT_TODO),
752 OPT_CMDMODE(0, "show-current-patch", &action,
753 N_("show the patch file being applied or merged"),
754 ACTION_SHOW_CURRENT_PATCH),
755 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
756 N_("use merging strategies to rebase"),
757 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
758 parse_opt_merge },
759 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
760 N_("let the user edit the list of commits to rebase"),
761 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
762 parse_opt_interactive },
763 OPT_SET_INT('p', "preserve-merges", &options.type,
764 N_("try to recreate merges instead of ignoring "
765 "them"), REBASE_PRESERVE_MERGES),
766 OPT_BOOL(0, "rerere-autoupdate",
767 &options.allow_rerere_autoupdate,
768 N_("allow rerere to update index with resolved "
769 "conflict")),
770 OPT_BOOL('k', "keep-empty", &options.keep_empty,
771 N_("preserve empty commits during rebase")),
772 OPT_BOOL(0, "autosquash", &options.autosquash,
773 N_("move commits that begin with "
774 "squash!/fixup! under -i")),
775 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
776 N_("GPG-sign commits"),
777 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
778 OPT_STRING_LIST(0, "whitespace", &whitespace,
779 N_("whitespace"), N_("passed to 'git apply'")),
780 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
781 REBASE_AM),
782 OPT_BOOL(0, "autostash", &options.autostash,
783 N_("automatically stash/stash pop before and after")),
784 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
785 N_("add exec lines after each commit of the "
786 "editable list")),
787 OPT_BOOL(0, "allow-empty-message",
788 &options.allow_empty_message,
789 N_("allow rebasing commits with empty messages")),
790 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
791 N_("mode"),
792 N_("try to rebase merges instead of skipping them"),
793 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
794 OPT_BOOL(0, "fork-point", &fork_point,
795 N_("use 'merge-base --fork-point' to refine upstream")),
796 OPT_STRING('s', "strategy", &options.strategy,
797 N_("strategy"), N_("use the given merge strategy")),
798 OPT_STRING_LIST('X', "strategy-option", &strategy_options,
799 N_("option"),
800 N_("pass the argument through to the merge "
801 "strategy")),
802 OPT_BOOL(0, "root", &options.root,
803 N_("rebase all reachable commits up to the root(s)")),
804 OPT_END(),
808 * NEEDSWORK: Once the builtin rebase has been tested enough
809 * and git-legacy-rebase.sh is retired to contrib/, this preamble
810 * can be removed.
813 if (!use_builtin_rebase()) {
814 const char *path = mkpath("%s/git-legacy-rebase",
815 git_exec_path());
817 if (sane_execvp(path, (char **)argv) < 0)
818 die_errno(_("could not exec %s"), path);
819 else
820 BUG("sane_execvp() returned???");
823 if (argc == 2 && !strcmp(argv[1], "-h"))
824 usage_with_options(builtin_rebase_usage,
825 builtin_rebase_options);
827 prefix = setup_git_directory();
828 trace_repo_setup(prefix);
829 setup_work_tree();
831 git_config(rebase_config, &options);
833 strbuf_reset(&buf);
834 strbuf_addf(&buf, "%s/applying", apply_dir());
835 if(file_exists(buf.buf))
836 die(_("It looks like 'git am' is in progress. Cannot rebase."));
838 if (is_directory(apply_dir())) {
839 options.type = REBASE_AM;
840 options.state_dir = apply_dir();
841 } else if (is_directory(merge_dir())) {
842 strbuf_reset(&buf);
843 strbuf_addf(&buf, "%s/rewritten", merge_dir());
844 if (is_directory(buf.buf)) {
845 options.type = REBASE_PRESERVE_MERGES;
846 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
847 } else {
848 strbuf_reset(&buf);
849 strbuf_addf(&buf, "%s/interactive", merge_dir());
850 if(file_exists(buf.buf)) {
851 options.type = REBASE_INTERACTIVE;
852 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
853 } else
854 options.type = REBASE_MERGE;
856 options.state_dir = merge_dir();
859 if (options.type != REBASE_UNSPECIFIED)
860 in_progress = 1;
862 total_argc = argc;
863 argc = parse_options(argc, argv, prefix,
864 builtin_rebase_options,
865 builtin_rebase_usage, 0);
867 if (action != NO_ACTION && total_argc != 2) {
868 usage_with_options(builtin_rebase_usage,
869 builtin_rebase_options);
872 if (argc > 2)
873 usage_with_options(builtin_rebase_usage,
874 builtin_rebase_options);
876 if (action != NO_ACTION && !in_progress)
877 die(_("No rebase in progress?"));
879 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
880 die(_("The --edit-todo action can only be used during "
881 "interactive rebase."));
883 switch (action) {
884 case ACTION_CONTINUE: {
885 struct object_id head;
886 struct lock_file lock_file = LOCK_INIT;
887 int fd;
889 options.action = "continue";
891 /* Sanity check */
892 if (get_oid("HEAD", &head))
893 die(_("Cannot read HEAD"));
895 fd = hold_locked_index(&lock_file, 0);
896 if (read_index(the_repository->index) < 0)
897 die(_("could not read index"));
898 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
899 NULL);
900 if (0 <= fd)
901 update_index_if_able(the_repository->index,
902 &lock_file);
903 rollback_lock_file(&lock_file);
905 if (has_unstaged_changes(1)) {
906 puts(_("You must edit all merge conflicts and then\n"
907 "mark them as resolved using git add"));
908 exit(1);
910 if (read_basic_state(&options))
911 exit(1);
912 goto run_rebase;
914 case ACTION_SKIP: {
915 struct string_list merge_rr = STRING_LIST_INIT_DUP;
917 options.action = "skip";
919 rerere_clear(&merge_rr);
920 string_list_clear(&merge_rr, 1);
922 if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0)
923 die(_("could not discard worktree changes"));
924 if (read_basic_state(&options))
925 exit(1);
926 goto run_rebase;
928 case ACTION_ABORT: {
929 struct string_list merge_rr = STRING_LIST_INIT_DUP;
930 options.action = "abort";
932 rerere_clear(&merge_rr);
933 string_list_clear(&merge_rr, 1);
935 if (read_basic_state(&options))
936 exit(1);
937 if (reset_head(&options.orig_head, "reset",
938 options.head_name, 0, NULL, NULL) < 0)
939 die(_("could not move back to %s"),
940 oid_to_hex(&options.orig_head));
941 ret = finish_rebase(&options);
942 goto cleanup;
944 case ACTION_QUIT: {
945 strbuf_reset(&buf);
946 strbuf_addstr(&buf, options.state_dir);
947 ret = !!remove_dir_recursively(&buf, 0);
948 if (ret)
949 die(_("could not remove '%s'"), options.state_dir);
950 goto cleanup;
952 case ACTION_EDIT_TODO:
953 options.action = "edit-todo";
954 options.dont_finish_rebase = 1;
955 goto run_rebase;
956 case ACTION_SHOW_CURRENT_PATCH:
957 options.action = "show-current-patch";
958 options.dont_finish_rebase = 1;
959 goto run_rebase;
960 case NO_ACTION:
961 break;
962 default:
963 BUG("action: %d", action);
966 /* Make sure no rebase is in progress */
967 if (in_progress) {
968 const char *last_slash = strrchr(options.state_dir, '/');
969 const char *state_dir_base =
970 last_slash ? last_slash + 1 : options.state_dir;
971 const char *cmd_live_rebase =
972 "git rebase (--continue | --abort | --skip)";
973 strbuf_reset(&buf);
974 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
975 die(_("It seems that there is already a %s directory, and\n"
976 "I wonder if you are in the middle of another rebase. "
977 "If that is the\n"
978 "case, please try\n\t%s\n"
979 "If that is not the case, please\n\t%s\n"
980 "and run me again. I am stopping in case you still "
981 "have something\n"
982 "valuable there.\n"),
983 state_dir_base, cmd_live_rebase, buf.buf);
986 if (!(options.flags & REBASE_NO_QUIET))
987 strbuf_addstr(&options.git_am_opt, " -q");
989 if (committer_date_is_author_date) {
990 strbuf_addstr(&options.git_am_opt,
991 " --committer-date-is-author-date");
992 options.flags |= REBASE_FORCE;
995 if (ignore_whitespace)
996 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
998 if (ignore_date) {
999 strbuf_addstr(&options.git_am_opt, " --ignore-date");
1000 options.flags |= REBASE_FORCE;
1003 if (options.keep_empty)
1004 imply_interactive(&options, "--keep-empty");
1006 if (gpg_sign) {
1007 free(options.gpg_sign_opt);
1008 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1011 if (opt_c >= 0)
1012 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
1014 if (whitespace.nr) {
1015 int i;
1017 for (i = 0; i < whitespace.nr; i++) {
1018 const char *item = whitespace.items[i].string;
1020 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
1021 item);
1023 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
1024 options.flags |= REBASE_FORCE;
1028 if (exec.nr) {
1029 int i;
1031 imply_interactive(&options, "--exec");
1033 strbuf_reset(&buf);
1034 for (i = 0; i < exec.nr; i++)
1035 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
1036 options.cmd = xstrdup(buf.buf);
1039 if (rebase_merges) {
1040 if (!*rebase_merges)
1041 ; /* default mode; do nothing */
1042 else if (!strcmp("rebase-cousins", rebase_merges))
1043 options.rebase_cousins = 1;
1044 else if (strcmp("no-rebase-cousins", rebase_merges))
1045 die(_("Unknown mode: %s"), rebase_merges);
1046 options.rebase_merges = 1;
1047 imply_interactive(&options, "--rebase-merges");
1050 if (strategy_options.nr) {
1051 int i;
1053 if (!options.strategy)
1054 options.strategy = "recursive";
1056 strbuf_reset(&buf);
1057 for (i = 0; i < strategy_options.nr; i++)
1058 strbuf_addf(&buf, " --%s",
1059 strategy_options.items[i].string);
1060 options.strategy_opts = xstrdup(buf.buf);
1063 if (options.strategy) {
1064 options.strategy = xstrdup(options.strategy);
1065 switch (options.type) {
1066 case REBASE_AM:
1067 die(_("--strategy requires --merge or --interactive"));
1068 case REBASE_MERGE:
1069 case REBASE_INTERACTIVE:
1070 case REBASE_PRESERVE_MERGES:
1071 /* compatible */
1072 break;
1073 case REBASE_UNSPECIFIED:
1074 options.type = REBASE_MERGE;
1075 break;
1076 default:
1077 BUG("unhandled rebase type (%d)", options.type);
1081 if (options.root && !options.onto_name)
1082 imply_interactive(&options, "--root without --onto");
1084 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1085 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1087 switch (options.type) {
1088 case REBASE_MERGE:
1089 case REBASE_INTERACTIVE:
1090 case REBASE_PRESERVE_MERGES:
1091 options.state_dir = merge_dir();
1092 break;
1093 case REBASE_AM:
1094 options.state_dir = apply_dir();
1095 break;
1096 default:
1097 /* the default rebase backend is `--am` */
1098 options.type = REBASE_AM;
1099 options.state_dir = apply_dir();
1100 break;
1103 if (options.git_am_opt.len) {
1104 const char *p;
1106 /* all am options except -q are compatible only with --am */
1107 strbuf_reset(&buf);
1108 strbuf_addbuf(&buf, &options.git_am_opt);
1109 strbuf_addch(&buf, ' ');
1110 while ((p = strstr(buf.buf, " -q ")))
1111 strbuf_splice(&buf, p - buf.buf, 4, " ", 1);
1112 strbuf_trim(&buf);
1114 if (is_interactive(&options) && buf.len)
1115 die(_("error: cannot combine interactive options "
1116 "(--interactive, --exec, --rebase-merges, "
1117 "--preserve-merges, --keep-empty, --root + "
1118 "--onto) with am options (%s)"), buf.buf);
1119 if (options.type == REBASE_MERGE && buf.len)
1120 die(_("error: cannot combine merge options (--merge, "
1121 "--strategy, --strategy-option) with am options "
1122 "(%s)"), buf.buf);
1125 if (options.signoff) {
1126 if (options.type == REBASE_PRESERVE_MERGES)
1127 die("cannot combine '--signoff' with "
1128 "'--preserve-merges'");
1129 strbuf_addstr(&options.git_am_opt, " --signoff");
1130 options.flags |= REBASE_FORCE;
1133 if (options.type == REBASE_PRESERVE_MERGES)
1135 * Note: incompatibility with --signoff handled in signoff block above
1136 * Note: incompatibility with --interactive is just a strong warning;
1137 * git-rebase.txt caveats with "unless you know what you are doing"
1139 if (options.rebase_merges)
1140 die(_("error: cannot combine '--preserve_merges' with "
1141 "'--rebase-merges'"));
1143 if (options.rebase_merges) {
1144 if (strategy_options.nr)
1145 die(_("error: cannot combine '--rebase_merges' with "
1146 "'--strategy-option'"));
1147 if (options.strategy)
1148 die(_("error: cannot combine '--rebase_merges' with "
1149 "'--strategy'"));
1152 if (!options.root) {
1153 if (argc < 1) {
1154 struct branch *branch;
1156 branch = branch_get(NULL);
1157 options.upstream_name = branch_get_upstream(branch,
1158 NULL);
1159 if (!options.upstream_name)
1160 error_on_missing_default_upstream();
1161 if (fork_point < 0)
1162 fork_point = 1;
1163 } else {
1164 options.upstream_name = argv[0];
1165 argc--;
1166 argv++;
1167 if (!strcmp(options.upstream_name, "-"))
1168 options.upstream_name = "@{-1}";
1170 options.upstream = peel_committish(options.upstream_name);
1171 if (!options.upstream)
1172 die(_("invalid upstream '%s'"), options.upstream_name);
1173 options.upstream_arg = options.upstream_name;
1174 } else {
1175 if (!options.onto_name) {
1176 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1177 &squash_onto, NULL, NULL) < 0)
1178 die(_("Could not create new root commit"));
1179 options.squash_onto = &squash_onto;
1180 options.onto_name = squash_onto_name =
1181 xstrdup(oid_to_hex(&squash_onto));
1183 options.upstream_name = NULL;
1184 options.upstream = NULL;
1185 if (argc > 1)
1186 usage_with_options(builtin_rebase_usage,
1187 builtin_rebase_options);
1188 options.upstream_arg = "--root";
1191 /* Make sure the branch to rebase onto is valid. */
1192 if (!options.onto_name)
1193 options.onto_name = options.upstream_name;
1194 if (strstr(options.onto_name, "...")) {
1195 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1196 die(_("'%s': need exactly one merge base"),
1197 options.onto_name);
1198 options.onto = lookup_commit_or_die(&merge_base,
1199 options.onto_name);
1200 } else {
1201 options.onto = peel_committish(options.onto_name);
1202 if (!options.onto)
1203 die(_("Does not point to a valid commit '%s'"),
1204 options.onto_name);
1208 * If the branch to rebase is given, that is the branch we will rebase
1209 * branch_name -- branch/commit being rebased, or
1210 * HEAD (already detached)
1211 * orig_head -- commit object name of tip of the branch before rebasing
1212 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1214 if (argc == 1) {
1215 /* Is it "rebase other branchname" or "rebase other commit"? */
1216 branch_name = argv[0];
1217 options.switch_to = argv[0];
1219 /* Is it a local branch? */
1220 strbuf_reset(&buf);
1221 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1222 if (!read_ref(buf.buf, &options.orig_head))
1223 options.head_name = xstrdup(buf.buf);
1224 /* If not is it a valid ref (branch or commit)? */
1225 else if (!get_oid(branch_name, &options.orig_head))
1226 options.head_name = NULL;
1227 else
1228 die(_("fatal: no such branch/commit '%s'"),
1229 branch_name);
1230 } else if (argc == 0) {
1231 /* Do not need to switch branches, we are already on it. */
1232 options.head_name =
1233 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1234 &flags));
1235 if (!options.head_name)
1236 die(_("No such ref: %s"), "HEAD");
1237 if (flags & REF_ISSYMREF) {
1238 if (!skip_prefix(options.head_name,
1239 "refs/heads/", &branch_name))
1240 branch_name = options.head_name;
1242 } else {
1243 free(options.head_name);
1244 options.head_name = NULL;
1245 branch_name = "HEAD";
1247 if (get_oid("HEAD", &options.orig_head))
1248 die(_("Could not resolve HEAD to a revision"));
1249 } else
1250 BUG("unexpected number of arguments left to parse");
1252 if (fork_point > 0) {
1253 struct commit *head =
1254 lookup_commit_reference(the_repository,
1255 &options.orig_head);
1256 options.restrict_revision =
1257 get_fork_point(options.upstream_name, head);
1260 if (read_index(the_repository->index) < 0)
1261 die(_("could not read index"));
1263 if (options.autostash) {
1264 struct lock_file lock_file = LOCK_INIT;
1265 int fd;
1267 fd = hold_locked_index(&lock_file, 0);
1268 refresh_cache(REFRESH_QUIET);
1269 if (0 <= fd)
1270 update_index_if_able(&the_index, &lock_file);
1271 rollback_lock_file(&lock_file);
1273 if (has_unstaged_changes(0) || has_uncommitted_changes(0)) {
1274 const char *autostash =
1275 state_dir_path("autostash", &options);
1276 struct child_process stash = CHILD_PROCESS_INIT;
1277 struct object_id oid;
1278 struct commit *head =
1279 lookup_commit_reference(the_repository,
1280 &options.orig_head);
1282 argv_array_pushl(&stash.args,
1283 "stash", "create", "autostash", NULL);
1284 stash.git_cmd = 1;
1285 stash.no_stdin = 1;
1286 strbuf_reset(&buf);
1287 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1288 die(_("Cannot autostash"));
1289 strbuf_trim_trailing_newline(&buf);
1290 if (get_oid(buf.buf, &oid))
1291 die(_("Unexpected stash response: '%s'"),
1292 buf.buf);
1293 strbuf_reset(&buf);
1294 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1296 if (safe_create_leading_directories_const(autostash))
1297 die(_("Could not create directory for '%s'"),
1298 options.state_dir);
1299 write_file(autostash, "%s", buf.buf);
1300 printf(_("Created autostash: %s\n"), buf.buf);
1301 if (reset_head(&head->object.oid, "reset --hard",
1302 NULL, 0, NULL, NULL) < 0)
1303 die(_("could not reset --hard"));
1304 printf(_("HEAD is now at %s"),
1305 find_unique_abbrev(&head->object.oid,
1306 DEFAULT_ABBREV));
1307 strbuf_reset(&buf);
1308 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1309 if (buf.len > 0)
1310 printf(" %s", buf.buf);
1311 putchar('\n');
1313 if (discard_index(the_repository->index) < 0 ||
1314 read_index(the_repository->index) < 0)
1315 die(_("could not read index"));
1319 if (require_clean_work_tree("rebase",
1320 _("Please commit or stash them."), 1, 1)) {
1321 ret = 1;
1322 goto cleanup;
1326 * Now we are rebasing commits upstream..orig_head (or with --root,
1327 * everything leading up to orig_head) on top of onto.
1331 * Check if we are already based on onto with linear history,
1332 * but this should be done only when upstream and onto are the same
1333 * and if this is not an interactive rebase.
1335 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1336 !is_interactive(&options) && !options.restrict_revision &&
1337 options.upstream &&
1338 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1339 int flag;
1341 if (!(options.flags & REBASE_FORCE)) {
1342 /* Lazily switch to the target branch if needed... */
1343 if (options.switch_to) {
1344 struct object_id oid;
1346 if (get_oid(options.switch_to, &oid) < 0) {
1347 ret = !!error(_("could not parse '%s'"),
1348 options.switch_to);
1349 goto cleanup;
1352 strbuf_reset(&buf);
1353 strbuf_addf(&buf, "rebase: checkout %s",
1354 options.switch_to);
1355 if (reset_head(&oid, "checkout",
1356 options.head_name, 0,
1357 NULL, NULL) < 0) {
1358 ret = !!error(_("could not switch to "
1359 "%s"),
1360 options.switch_to);
1361 goto cleanup;
1365 if (!(options.flags & REBASE_NO_QUIET))
1366 ; /* be quiet */
1367 else if (!strcmp(branch_name, "HEAD") &&
1368 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1369 puts(_("HEAD is up to date."));
1370 else
1371 printf(_("Current branch %s is up to date.\n"),
1372 branch_name);
1373 ret = !!finish_rebase(&options);
1374 goto cleanup;
1375 } else if (!(options.flags & REBASE_NO_QUIET))
1376 ; /* be quiet */
1377 else if (!strcmp(branch_name, "HEAD") &&
1378 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1379 puts(_("HEAD is up to date, rebase forced."));
1380 else
1381 printf(_("Current branch %s is up to date, rebase "
1382 "forced.\n"), branch_name);
1385 /* If a hook exists, give it a chance to interrupt*/
1386 if (!ok_to_skip_pre_rebase &&
1387 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1388 argc ? argv[0] : NULL, NULL))
1389 die(_("The pre-rebase hook refused to rebase."));
1391 if (options.flags & REBASE_DIFFSTAT) {
1392 struct diff_options opts;
1394 if (options.flags & REBASE_VERBOSE)
1395 printf(_("Changes from %s to %s:\n"),
1396 oid_to_hex(&merge_base),
1397 oid_to_hex(&options.onto->object.oid));
1399 /* We want color (if set), but no pager */
1400 diff_setup(&opts);
1401 opts.stat_width = -1; /* use full terminal width */
1402 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1403 opts.output_format |=
1404 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1405 opts.detect_rename = DIFF_DETECT_RENAME;
1406 diff_setup_done(&opts);
1407 diff_tree_oid(&merge_base, &options.onto->object.oid,
1408 "", &opts);
1409 diffcore_std(&opts);
1410 diff_flush(&opts);
1413 if (is_interactive(&options))
1414 goto run_rebase;
1416 /* Detach HEAD and reset the tree */
1417 if (options.flags & REBASE_NO_QUIET)
1418 printf(_("First, rewinding head to replay your work on top of "
1419 "it...\n"));
1421 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1422 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1,
1423 NULL, msg.buf))
1424 die(_("Could not detach HEAD"));
1425 strbuf_release(&msg);
1428 * If the onto is a proper descendant of the tip of the branch, then
1429 * we just fast-forwarded.
1431 strbuf_reset(&msg);
1432 if (!oidcmp(&merge_base, &options.orig_head)) {
1433 printf(_("Fast-forwarded %s to %s. \n"),
1434 branch_name, options.onto_name);
1435 strbuf_addf(&msg, "rebase finished: %s onto %s",
1436 options.head_name ? options.head_name : "detached HEAD",
1437 oid_to_hex(&options.onto->object.oid));
1438 reset_head(NULL, "Fast-forwarded", options.head_name, 0,
1439 "HEAD", msg.buf);
1440 strbuf_release(&msg);
1441 ret = !!finish_rebase(&options);
1442 goto cleanup;
1445 strbuf_addf(&revisions, "%s..%s",
1446 options.root ? oid_to_hex(&options.onto->object.oid) :
1447 (options.restrict_revision ?
1448 oid_to_hex(&options.restrict_revision->object.oid) :
1449 oid_to_hex(&options.upstream->object.oid)),
1450 oid_to_hex(&options.orig_head));
1452 options.revisions = revisions.buf;
1454 run_rebase:
1455 ret = !!run_specific_rebase(&options);
1457 cleanup:
1458 strbuf_release(&revisions);
1459 free(options.head_name);
1460 free(options.gpg_sign_opt);
1461 free(options.cmd);
1462 free(squash_onto_name);
1463 return ret;