builtin rebase: support `fork-point` option
[git/git.git] / builtin / rebase.c
CommitLineData
55071ea2
PK
1/*
2 * "git rebase" builtin command
3 *
4 * Copyright (c) 2018 Pratik Karki
5 */
6
7#include "builtin.h"
8#include "run-command.h"
9#include "exec-cmd.h"
10#include "argv-array.h"
11#include "dir.h"
ac7f467f
PK
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"
f28d40d3 19#include "parse-options.h"
075bc852 20#include "commit.h"
bff014da 21#include "diff.h"
e0333e5c 22#include "wt-status.h"
9a48a615 23#include "revision.h"
122420c2 24#include "rerere.h"
f28d40d3
PK
25
26static char const * const builtin_rebase_usage[] = {
27 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
28 "[<upstream>] [<branch>]"),
29 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
30 "--root [<branch>]"),
31 N_("git rebase --continue | --abort | --skip | --edit-todo"),
32 NULL
33};
ac7f467f
PK
34
35static GIT_PATH_FUNC(apply_dir, "rebase-apply")
36static GIT_PATH_FUNC(merge_dir, "rebase-merge")
37
38enum rebase_type {
39 REBASE_UNSPECIFIED = -1,
40 REBASE_AM,
41 REBASE_MERGE,
42 REBASE_INTERACTIVE,
43 REBASE_PRESERVE_MERGES
44};
55071ea2
PK
45
46static int use_builtin_rebase(void)
47{
48 struct child_process cp = CHILD_PROCESS_INIT;
49 struct strbuf out = STRBUF_INIT;
50 int ret;
51
52 argv_array_pushl(&cp.args,
53 "config", "--bool", "rebase.usebuiltin", NULL);
54 cp.git_cmd = 1;
55 if (capture_command(&cp, &out, 6)) {
56 strbuf_release(&out);
57 return 0;
58 }
59
60 strbuf_trim(&out);
61 ret = !strcmp("true", out.buf);
62 strbuf_release(&out);
63 return ret;
64}
65
ac7f467f
PK
66struct rebase_options {
67 enum rebase_type type;
68 const char *state_dir;
69 struct commit *upstream;
70 const char *upstream_name;
06e4775a 71 const char *upstream_arg;
ac7f467f
PK
72 char *head_name;
73 struct object_id orig_head;
74 struct commit *onto;
75 const char *onto_name;
76 const char *revisions;
e65123a7 77 const char *switch_to;
ac7f467f
PK
78 int root;
79 struct commit *restrict_revision;
80 int dont_finish_rebase;
b4c8eb02
PK
81 enum {
82 REBASE_NO_QUIET = 1<<0,
bff014da
PK
83 REBASE_VERBOSE = 1<<1,
84 REBASE_DIFFSTAT = 1<<2,
1ed9c14f 85 REBASE_FORCE = 1<<3,
c54dacb5 86 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
b4c8eb02
PK
87 } flags;
88 struct strbuf git_am_opt;
f9573628 89 const char *action;
73d51ed0 90 int signoff;
ead98c11 91 int allow_rerere_autoupdate;
002ee2fe 92 int keep_empty;
051910a9 93 int autosquash;
12026a41 94 char *gpg_sign_opt;
6defce2b 95 int autostash;
68e46d78 96 char *cmd;
9b3a448b 97 int allow_empty_message;
3c3588c7 98 int rebase_merges, rebase_cousins;
ac7f467f
PK
99};
100
9a48a615
PK
101static int is_interactive(struct rebase_options *opts)
102{
103 return opts->type == REBASE_INTERACTIVE ||
104 opts->type == REBASE_PRESERVE_MERGES;
105}
106
002ee2fe
PK
107static void imply_interactive(struct rebase_options *opts, const char *option)
108{
109 switch (opts->type) {
110 case REBASE_AM:
111 die(_("%s requires an interactive rebase"), option);
112 break;
113 case REBASE_INTERACTIVE:
114 case REBASE_PRESERVE_MERGES:
115 break;
116 case REBASE_MERGE:
117 /* we silently *upgrade* --merge to --interactive if needed */
118 default:
119 opts->type = REBASE_INTERACTIVE; /* implied */
120 break;
121 }
122}
123
ac7f467f
PK
124/* Returns the filename prefixed by the state_dir */
125static const char *state_dir_path(const char *filename, struct rebase_options *opts)
126{
127 static struct strbuf path = STRBUF_INIT;
128 static size_t prefix_len;
129
130 if (!prefix_len) {
131 strbuf_addf(&path, "%s/", opts->state_dir);
132 prefix_len = path.len;
133 }
134
135 strbuf_setlen(&path, prefix_len);
136 strbuf_addstr(&path, filename);
137 return path.buf;
138}
139
f9573628
PK
140/* Read one file, then strip line endings */
141static int read_one(const char *path, struct strbuf *buf)
142{
143 if (strbuf_read_file(buf, path, 0) < 0)
144 return error_errno(_("could not read '%s'"), path);
145 strbuf_trim_trailing_newline(buf);
146 return 0;
147}
148
149/* Initialize the rebase options from the state directory. */
150static int read_basic_state(struct rebase_options *opts)
151{
152 struct strbuf head_name = STRBUF_INIT;
153 struct strbuf buf = STRBUF_INIT;
154 struct object_id oid;
155
156 if (read_one(state_dir_path("head-name", opts), &head_name) ||
157 read_one(state_dir_path("onto", opts), &buf))
158 return -1;
159 opts->head_name = starts_with(head_name.buf, "refs/") ?
160 xstrdup(head_name.buf) : NULL;
161 strbuf_release(&head_name);
162 if (get_oid(buf.buf, &oid))
163 return error(_("could not get 'onto': '%s'"), buf.buf);
164 opts->onto = lookup_commit_or_die(&oid, buf.buf);
165
166 /*
167 * We always write to orig-head, but interactive rebase used to write to
168 * head. Fall back to reading from head to cover for the case that the
169 * user upgraded git with an ongoing interactive rebase.
170 */
171 strbuf_reset(&buf);
172 if (file_exists(state_dir_path("orig-head", opts))) {
173 if (read_one(state_dir_path("orig-head", opts), &buf))
174 return -1;
175 } else if (read_one(state_dir_path("head", opts), &buf))
176 return -1;
177 if (get_oid(buf.buf, &opts->orig_head))
178 return error(_("invalid orig-head: '%s'"), buf.buf);
179
180 strbuf_reset(&buf);
181 if (read_one(state_dir_path("quiet", opts), &buf))
182 return -1;
183 if (buf.len)
184 opts->flags &= ~REBASE_NO_QUIET;
185 else
186 opts->flags |= REBASE_NO_QUIET;
187
188 if (file_exists(state_dir_path("verbose", opts)))
189 opts->flags |= REBASE_VERBOSE;
190
73d51ed0
PK
191 if (file_exists(state_dir_path("signoff", opts))) {
192 opts->signoff = 1;
193 opts->flags |= REBASE_FORCE;
194 }
195
ead98c11
PK
196 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
197 strbuf_reset(&buf);
198 if (read_one(state_dir_path("allow_rerere_autoupdate", opts),
199 &buf))
200 return -1;
201 if (!strcmp(buf.buf, "--rerere-autoupdate"))
202 opts->allow_rerere_autoupdate = 1;
203 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
204 opts->allow_rerere_autoupdate = 0;
205 else
206 warning(_("ignoring invalid allow_rerere_autoupdate: "
207 "'%s'"), buf.buf);
208 } else
209 opts->allow_rerere_autoupdate = -1;
210
12026a41
PK
211 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
212 strbuf_reset(&buf);
213 if (read_one(state_dir_path("gpg_sign_opt", opts),
214 &buf))
215 return -1;
216 free(opts->gpg_sign_opt);
217 opts->gpg_sign_opt = xstrdup(buf.buf);
218 }
219
f9573628
PK
220 strbuf_release(&buf);
221
222 return 0;
223}
224
6defce2b
PK
225static int apply_autostash(struct rebase_options *opts)
226{
227 const char *path = state_dir_path("autostash", opts);
228 struct strbuf autostash = STRBUF_INIT;
229 struct child_process stash_apply = CHILD_PROCESS_INIT;
230
231 if (!file_exists(path))
232 return 0;
233
234 if (read_one(state_dir_path("autostash", opts), &autostash))
235 return error(_("Could not read '%s'"), path);
236 argv_array_pushl(&stash_apply.args,
237 "stash", "apply", autostash.buf, NULL);
238 stash_apply.git_cmd = 1;
239 stash_apply.no_stderr = stash_apply.no_stdout =
240 stash_apply.no_stdin = 1;
241 if (!run_command(&stash_apply))
242 printf(_("Applied autostash.\n"));
243 else {
244 struct argv_array args = ARGV_ARRAY_INIT;
245 int res = 0;
246
247 argv_array_pushl(&args,
248 "stash", "store", "-m", "autostash", "-q",
249 autostash.buf, NULL);
250 if (run_command_v_opt(args.argv, RUN_GIT_CMD))
251 res = error(_("Cannot store %s"), autostash.buf);
252 argv_array_clear(&args);
253 strbuf_release(&autostash);
254 if (res)
255 return res;
256
257 fprintf(stderr,
258 _("Applying autostash resulted in conflicts.\n"
259 "Your changes are safe in the stash.\n"
260 "You can run \"git stash pop\" or \"git stash drop\" "
261 "at any time.\n"));
262 }
263
264 strbuf_release(&autostash);
265 return 0;
266}
267
ac7f467f
PK
268static int finish_rebase(struct rebase_options *opts)
269{
270 struct strbuf dir = STRBUF_INIT;
271 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
272
273 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
6defce2b 274 apply_autostash(opts);
ac7f467f
PK
275 close_all_packs(the_repository->objects);
276 /*
277 * We ignore errors in 'gc --auto', since the
278 * user should see them.
279 */
280 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
281 strbuf_addstr(&dir, opts->state_dir);
282 remove_dir_recursively(&dir, 0);
283 strbuf_release(&dir);
284
285 return 0;
286}
287
288static struct commit *peel_committish(const char *name)
289{
290 struct object *obj;
291 struct object_id oid;
292
293 if (get_oid(name, &oid))
294 return NULL;
295 obj = parse_object(the_repository, &oid);
296 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
297}
298
299static void add_var(struct strbuf *buf, const char *name, const char *value)
300{
301 if (!value)
302 strbuf_addf(buf, "unset %s; ", name);
303 else {
304 strbuf_addf(buf, "%s=", name);
305 sq_quote_buf(buf, value);
306 strbuf_addstr(buf, "; ");
307 }
308}
309
310static int run_specific_rebase(struct rebase_options *opts)
311{
312 const char *argv[] = { NULL, NULL };
313 struct strbuf script_snippet = STRBUF_INIT;
314 int status;
315 const char *backend, *backend_func;
316
317 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
318 add_var(&script_snippet, "state_dir", opts->state_dir);
319
320 add_var(&script_snippet, "upstream_name", opts->upstream_name);
f9573628
PK
321 add_var(&script_snippet, "upstream", opts->upstream ?
322 oid_to_hex(&opts->upstream->object.oid) : NULL);
d4c569f8
PK
323 add_var(&script_snippet, "head_name",
324 opts->head_name ? opts->head_name : "detached HEAD");
ac7f467f 325 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
f9573628
PK
326 add_var(&script_snippet, "onto", opts->onto ?
327 oid_to_hex(&opts->onto->object.oid) : NULL);
ac7f467f
PK
328 add_var(&script_snippet, "onto_name", opts->onto_name);
329 add_var(&script_snippet, "revisions", opts->revisions);
330 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
331 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
b4c8eb02
PK
332 add_var(&script_snippet, "GIT_QUIET",
333 opts->flags & REBASE_NO_QUIET ? "" : "t");
334 add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf);
bff014da
PK
335 add_var(&script_snippet, "verbose",
336 opts->flags & REBASE_VERBOSE ? "t" : "");
337 add_var(&script_snippet, "diffstat",
338 opts->flags & REBASE_DIFFSTAT ? "t" : "");
1ed9c14f
PK
339 add_var(&script_snippet, "force_rebase",
340 opts->flags & REBASE_FORCE ? "t" : "");
e65123a7
PK
341 if (opts->switch_to)
342 add_var(&script_snippet, "switch_to", opts->switch_to);
f9573628 343 add_var(&script_snippet, "action", opts->action ? opts->action : "");
73d51ed0 344 add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : "");
ead98c11
PK
345 add_var(&script_snippet, "allow_rerere_autoupdate",
346 opts->allow_rerere_autoupdate < 0 ? "" :
347 opts->allow_rerere_autoupdate ?
348 "--rerere-autoupdate" : "--no-rerere-autoupdate");
002ee2fe 349 add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : "");
051910a9 350 add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : "");
12026a41 351 add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt);
68e46d78 352 add_var(&script_snippet, "cmd", opts->cmd);
9b3a448b
PK
353 add_var(&script_snippet, "allow_empty_message",
354 opts->allow_empty_message ? "--allow-empty-message" : "");
3c3588c7
PK
355 add_var(&script_snippet, "rebase_merges",
356 opts->rebase_merges ? "t" : "");
357 add_var(&script_snippet, "rebase_cousins",
358 opts->rebase_cousins ? "t" : "");
ac7f467f
PK
359
360 switch (opts->type) {
361 case REBASE_AM:
362 backend = "git-rebase--am";
363 backend_func = "git_rebase__am";
364 break;
365 case REBASE_INTERACTIVE:
366 backend = "git-rebase--interactive";
367 backend_func = "git_rebase__interactive";
368 break;
369 case REBASE_MERGE:
370 backend = "git-rebase--merge";
371 backend_func = "git_rebase__merge";
372 break;
373 case REBASE_PRESERVE_MERGES:
374 backend = "git-rebase--preserve-merges";
375 backend_func = "git_rebase__preserve_merges";
376 break;
377 default:
378 BUG("Unhandled rebase type %d", opts->type);
379 break;
380 }
381
382 strbuf_addf(&script_snippet,
383 ". git-sh-setup && . git-rebase--common &&"
384 " . %s && %s", backend, backend_func);
385 argv[0] = script_snippet.buf;
386
387 status = run_command_v_opt(argv, RUN_USING_SHELL);
388 if (opts->dont_finish_rebase)
389 ; /* do nothing */
390 else if (status == 0) {
391 if (!file_exists(state_dir_path("stopped-sha", opts)))
392 finish_rebase(opts);
393 } else if (status == 2) {
394 struct strbuf dir = STRBUF_INIT;
395
6defce2b 396 apply_autostash(opts);
ac7f467f
PK
397 strbuf_addstr(&dir, opts->state_dir);
398 remove_dir_recursively(&dir, 0);
399 strbuf_release(&dir);
400 die("Nothing to do");
401 }
402
403 strbuf_release(&script_snippet);
404
405 return status ? -1 : 0;
406}
407
408#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
409
410static int reset_head(struct object_id *oid, const char *action,
411 const char *switch_to_branch, int detach_head)
412{
413 struct object_id head_oid;
414 struct tree_desc desc;
415 struct lock_file lock = LOCK_INIT;
416 struct unpack_trees_options unpack_tree_opts;
417 struct tree *tree;
418 const char *reflog_action;
419 struct strbuf msg = STRBUF_INIT;
420 size_t prefix_len;
421 struct object_id *orig = NULL, oid_orig,
422 *old_orig = NULL, oid_old_orig;
423 int ret = 0;
424
d4c569f8
PK
425 if (switch_to_branch && !starts_with(switch_to_branch, "refs/"))
426 BUG("Not a fully qualified branch: '%s'", switch_to_branch);
427
ac7f467f
PK
428 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
429 return -1;
430
431 if (!oid) {
432 if (get_oid("HEAD", &head_oid)) {
433 rollback_lock_file(&lock);
434 return error(_("could not determine HEAD revision"));
435 }
436 oid = &head_oid;
437 }
438
439 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
440 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
441 unpack_tree_opts.head_idx = 1;
442 unpack_tree_opts.src_index = the_repository->index;
443 unpack_tree_opts.dst_index = the_repository->index;
444 unpack_tree_opts.fn = oneway_merge;
445 unpack_tree_opts.update = 1;
446 unpack_tree_opts.merge = 1;
447 if (!detach_head)
448 unpack_tree_opts.reset = 1;
449
450 if (read_index_unmerged(the_repository->index) < 0) {
451 rollback_lock_file(&lock);
452 return error(_("could not read index"));
453 }
454
455 if (!fill_tree_descriptor(&desc, oid)) {
456 error(_("failed to find tree of %s"), oid_to_hex(oid));
457 rollback_lock_file(&lock);
458 free((void *)desc.buffer);
459 return -1;
460 }
461
462 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
463 rollback_lock_file(&lock);
464 free((void *)desc.buffer);
465 return -1;
466 }
467
468 tree = parse_tree_indirect(oid);
469 prime_cache_tree(the_repository->index, tree);
470
471 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
472 ret = error(_("could not write index"));
473 free((void *)desc.buffer);
474
475 if (ret)
476 return ret;
477
478 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
479 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
480 prefix_len = msg.len;
481
482 if (!get_oid("ORIG_HEAD", &oid_old_orig))
483 old_orig = &oid_old_orig;
484 if (!get_oid("HEAD", &oid_orig)) {
485 orig = &oid_orig;
486 strbuf_addstr(&msg, "updating ORIG_HEAD");
487 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
488 UPDATE_REFS_MSG_ON_ERR);
489 } else if (old_orig)
490 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
491 strbuf_setlen(&msg, prefix_len);
492 strbuf_addstr(&msg, "updating HEAD");
493 if (!switch_to_branch)
494 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
495 UPDATE_REFS_MSG_ON_ERR);
496 else {
497 ret = create_symref("HEAD", switch_to_branch, msg.buf);
498 if (!ret)
499 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
500 UPDATE_REFS_MSG_ON_ERR);
501 }
502
503 strbuf_release(&msg);
504 return ret;
505}
506
bff014da
PK
507static int rebase_config(const char *var, const char *value, void *data)
508{
509 struct rebase_options *opts = data;
510
511 if (!strcmp(var, "rebase.stat")) {
512 if (git_config_bool(var, value))
513 opts->flags |= REBASE_DIFFSTAT;
514 else
515 opts->flags &= !REBASE_DIFFSTAT;
516 return 0;
517 }
518
051910a9
PK
519 if (!strcmp(var, "rebase.autosquash")) {
520 opts->autosquash = git_config_bool(var, value);
521 return 0;
522 }
523
12026a41
PK
524 if (!strcmp(var, "commit.gpgsign")) {
525 free(opts->gpg_sign_opt);
526 opts->gpg_sign_opt = git_config_bool(var, value) ?
527 xstrdup("-S") : NULL;
528 return 0;
529 }
530
6defce2b
PK
531 if (!strcmp(var, "rebase.autostash")) {
532 opts->autostash = git_config_bool(var, value);
533 return 0;
534 }
535
bff014da
PK
536 return git_default_config(var, value, data);
537}
538
9a48a615
PK
539/*
540 * Determines whether the commits in from..to are linear, i.e. contain
541 * no merge commits. This function *expects* `from` to be an ancestor of
542 * `to`.
543 */
544static int is_linear_history(struct commit *from, struct commit *to)
545{
546 while (to && to != from) {
547 parse_commit(to);
548 if (!to->parents)
549 return 1;
550 if (to->parents->next)
551 return 0;
552 to = to->parents->item;
553 }
554 return 1;
555}
556
557static int can_fast_forward(struct commit *onto, struct object_id *head_oid,
558 struct object_id *merge_base)
559{
560 struct commit *head = lookup_commit(the_repository, head_oid);
561 struct commit_list *merge_bases;
562 int res;
563
564 if (!head)
565 return 0;
566
567 merge_bases = get_merge_bases(onto, head);
568 if (merge_bases && !merge_bases->next) {
569 oidcpy(merge_base, &merge_bases->item->object.oid);
570 res = !oidcmp(merge_base, &onto->object.oid);
571 } else {
572 oidcpy(merge_base, &null_oid);
573 res = 0;
574 }
575 free_commit_list(merge_bases);
576 return res && is_linear_history(onto, head);
577}
578
361badd3
PK
579/* -i followed by -m is still -i */
580static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
581{
582 struct rebase_options *opts = opt->value;
583
584 if (!is_interactive(opts))
585 opts->type = REBASE_MERGE;
586
587 return 0;
588}
589
590/* -i followed by -p is still explicitly interactive, but -p alone is not */
591static int parse_opt_interactive(const struct option *opt, const char *arg,
592 int unset)
593{
594 struct rebase_options *opts = opt->value;
595
596 opts->type = REBASE_INTERACTIVE;
597 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
598
599 return 0;
600}
601
55071ea2
PK
602int cmd_rebase(int argc, const char **argv, const char *prefix)
603{
ac7f467f
PK
604 struct rebase_options options = {
605 .type = REBASE_UNSPECIFIED,
b4c8eb02
PK
606 .flags = REBASE_NO_QUIET,
607 .git_am_opt = STRBUF_INIT,
ead98c11 608 .allow_rerere_autoupdate = -1,
9b3a448b 609 .allow_empty_message = 1,
ac7f467f
PK
610 };
611 const char *branch_name;
f9573628 612 int ret, flags, total_argc, in_progress = 0;
06e4775a 613 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
614 struct strbuf msg = STRBUF_INIT;
615 struct strbuf revisions = STRBUF_INIT;
c54dacb5 616 struct strbuf buf = STRBUF_INIT;
075bc852 617 struct object_id merge_base;
f9573628
PK
618 enum {
619 NO_ACTION,
620 ACTION_CONTINUE,
122420c2 621 ACTION_SKIP,
5e5d9619 622 ACTION_ABORT,
5a614945 623 ACTION_QUIT,
51e9ea6d
PK
624 ACTION_EDIT_TODO,
625 ACTION_SHOW_CURRENT_PATCH,
f9573628 626 } action = NO_ACTION;
38dbcef2 627 int committer_date_is_author_date = 0;
53f9e5be 628 int ignore_date = 0;
99d8cc7c 629 int ignore_whitespace = 0;
12026a41 630 const char *gpg_sign = NULL;
7998dbe1
PK
631 int opt_c = -1;
632 struct string_list whitespace = STRING_LIST_INIT_NODUP;
68e46d78 633 struct string_list exec = STRING_LIST_INIT_NODUP;
3c3588c7 634 const char *rebase_merges = NULL;
92d0d74e 635 int fork_point = -1;
f28d40d3
PK
636 struct option builtin_rebase_options[] = {
637 OPT_STRING(0, "onto", &options.onto_name,
638 N_("revision"),
639 N_("rebase onto given branch instead of upstream")),
06e4775a
PK
640 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
641 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
642 OPT_NEGBIT('q', "quiet", &options.flags,
643 N_("be quiet. implies --no-stat"),
bff014da
PK
644 REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT),
645 OPT_BIT('v', "verbose", &options.flags,
646 N_("display a diffstat of what changed upstream"),
647 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
648 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
649 N_("do not show diffstat of what changed upstream"),
650 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
99d8cc7c
PK
651 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
652 N_("passed to 'git apply'")),
73d51ed0
PK
653 OPT_BOOL(0, "signoff", &options.signoff,
654 N_("add a Signed-off-by: line to each commit")),
38dbcef2
PK
655 OPT_BOOL(0, "committer-date-is-author-date",
656 &committer_date_is_author_date,
657 N_("passed to 'git am'")),
53f9e5be
PK
658 OPT_BOOL(0, "ignore-date", &ignore_date,
659 N_("passed to 'git am'")),
1ed9c14f
PK
660 OPT_BIT('f', "force-rebase", &options.flags,
661 N_("cherry-pick all commits, even if unchanged"),
662 REBASE_FORCE),
663 OPT_BIT(0, "no-ff", &options.flags,
664 N_("cherry-pick all commits, even if unchanged"),
665 REBASE_FORCE),
f9573628
PK
666 OPT_CMDMODE(0, "continue", &action, N_("continue"),
667 ACTION_CONTINUE),
122420c2
PK
668 OPT_CMDMODE(0, "skip", &action,
669 N_("skip current patch and continue"), ACTION_SKIP),
5e5d9619
PK
670 OPT_CMDMODE(0, "abort", &action,
671 N_("abort and check out the original branch"),
672 ACTION_ABORT),
5a614945
PK
673 OPT_CMDMODE(0, "quit", &action,
674 N_("abort but keep HEAD where it is"), ACTION_QUIT),
51e9ea6d
PK
675 OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list "
676 "during an interactive rebase"), ACTION_EDIT_TODO),
677 OPT_CMDMODE(0, "show-current-patch", &action,
678 N_("show the patch file being applied or merged"),
679 ACTION_SHOW_CURRENT_PATCH),
361badd3
PK
680 { OPTION_CALLBACK, 'm', "merge", &options, NULL,
681 N_("use merging strategies to rebase"),
682 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
683 parse_opt_merge },
684 { OPTION_CALLBACK, 'i', "interactive", &options, NULL,
685 N_("let the user edit the list of commits to rebase"),
686 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
687 parse_opt_interactive },
688 OPT_SET_INT('p', "preserve-merges", &options.type,
689 N_("try to recreate merges instead of ignoring "
690 "them"), REBASE_PRESERVE_MERGES),
ead98c11
PK
691 OPT_BOOL(0, "rerere-autoupdate",
692 &options.allow_rerere_autoupdate,
693 N_("allow rerere to update index with resolved "
694 "conflict")),
002ee2fe
PK
695 OPT_BOOL('k', "keep-empty", &options.keep_empty,
696 N_("preserve empty commits during rebase")),
051910a9
PK
697 OPT_BOOL(0, "autosquash", &options.autosquash,
698 N_("move commits that begin with "
699 "squash!/fixup! under -i")),
12026a41
PK
700 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
701 N_("GPG-sign commits"),
702 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
7998dbe1
PK
703 OPT_STRING_LIST(0, "whitespace", &whitespace,
704 N_("whitespace"), N_("passed to 'git apply'")),
705 OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"),
706 REBASE_AM),
6defce2b
PK
707 OPT_BOOL(0, "autostash", &options.autostash,
708 N_("automatically stash/stash pop before and after")),
68e46d78
PK
709 OPT_STRING_LIST('x', "exec", &exec, N_("exec"),
710 N_("add exec lines after each commit of the "
711 "editable list")),
9b3a448b
PK
712 OPT_BOOL(0, "allow-empty-message",
713 &options.allow_empty_message,
714 N_("allow rebasing commits with empty messages")),
3c3588c7
PK
715 {OPTION_STRING, 'r', "rebase-merges", &rebase_merges,
716 N_("mode"),
717 N_("try to rebase merges instead of skipping them"),
718 PARSE_OPT_OPTARG, NULL, (intptr_t)""},
92d0d74e
PK
719 OPT_BOOL(0, "fork-point", &fork_point,
720 N_("use 'merge-base --fork-point' to refine upstream")),
f28d40d3
PK
721 OPT_END(),
722 };
ac7f467f 723
55071ea2
PK
724 /*
725 * NEEDSWORK: Once the builtin rebase has been tested enough
726 * and git-legacy-rebase.sh is retired to contrib/, this preamble
727 * can be removed.
728 */
729
730 if (!use_builtin_rebase()) {
731 const char *path = mkpath("%s/git-legacy-rebase",
732 git_exec_path());
733
734 if (sane_execvp(path, (char **)argv) < 0)
735 die_errno(_("could not exec %s"), path);
736 else
737 BUG("sane_execvp() returned???");
738 }
739
f28d40d3
PK
740 if (argc == 2 && !strcmp(argv[1], "-h"))
741 usage_with_options(builtin_rebase_usage,
742 builtin_rebase_options);
743
55071ea2
PK
744 prefix = setup_git_directory();
745 trace_repo_setup(prefix);
746 setup_work_tree();
747
bff014da
PK
748 git_config(rebase_config, &options);
749
0eabf4b9
PK
750 strbuf_reset(&buf);
751 strbuf_addf(&buf, "%s/applying", apply_dir());
752 if(file_exists(buf.buf))
753 die(_("It looks like 'git am' is in progress. Cannot rebase."));
754
c54dacb5
PK
755 if (is_directory(apply_dir())) {
756 options.type = REBASE_AM;
757 options.state_dir = apply_dir();
758 } else if (is_directory(merge_dir())) {
759 strbuf_reset(&buf);
760 strbuf_addf(&buf, "%s/rewritten", merge_dir());
761 if (is_directory(buf.buf)) {
762 options.type = REBASE_PRESERVE_MERGES;
763 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
764 } else {
765 strbuf_reset(&buf);
766 strbuf_addf(&buf, "%s/interactive", merge_dir());
767 if(file_exists(buf.buf)) {
768 options.type = REBASE_INTERACTIVE;
769 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
770 } else
771 options.type = REBASE_MERGE;
772 }
773 options.state_dir = merge_dir();
774 }
775
776 if (options.type != REBASE_UNSPECIFIED)
777 in_progress = 1;
778
f9573628 779 total_argc = argc;
f28d40d3
PK
780 argc = parse_options(argc, argv, prefix,
781 builtin_rebase_options,
782 builtin_rebase_usage, 0);
783
f9573628
PK
784 if (action != NO_ACTION && total_argc != 2) {
785 usage_with_options(builtin_rebase_usage,
786 builtin_rebase_options);
787 }
788
f28d40d3
PK
789 if (argc > 2)
790 usage_with_options(builtin_rebase_usage,
791 builtin_rebase_options);
ac7f467f 792
d732a570
PK
793 if (action != NO_ACTION && !in_progress)
794 die(_("No rebase in progress?"));
795
51e9ea6d
PK
796 if (action == ACTION_EDIT_TODO && !is_interactive(&options))
797 die(_("The --edit-todo action can only be used during "
798 "interactive rebase."));
799
f9573628
PK
800 switch (action) {
801 case ACTION_CONTINUE: {
802 struct object_id head;
803 struct lock_file lock_file = LOCK_INIT;
804 int fd;
805
806 options.action = "continue";
807
808 /* Sanity check */
809 if (get_oid("HEAD", &head))
810 die(_("Cannot read HEAD"));
811
812 fd = hold_locked_index(&lock_file, 0);
813 if (read_index(the_repository->index) < 0)
814 die(_("could not read index"));
815 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
816 NULL);
817 if (0 <= fd)
818 update_index_if_able(the_repository->index,
819 &lock_file);
820 rollback_lock_file(&lock_file);
821
822 if (has_unstaged_changes(1)) {
823 puts(_("You must edit all merge conflicts and then\n"
824 "mark them as resolved using git add"));
825 exit(1);
826 }
827 if (read_basic_state(&options))
828 exit(1);
829 goto run_rebase;
830 }
122420c2
PK
831 case ACTION_SKIP: {
832 struct string_list merge_rr = STRING_LIST_INIT_DUP;
833
834 options.action = "skip";
835
836 rerere_clear(&merge_rr);
837 string_list_clear(&merge_rr, 1);
838
839 if (reset_head(NULL, "reset", NULL, 0) < 0)
840 die(_("could not discard worktree changes"));
841 if (read_basic_state(&options))
842 exit(1);
843 goto run_rebase;
844 }
5e5d9619
PK
845 case ACTION_ABORT: {
846 struct string_list merge_rr = STRING_LIST_INIT_DUP;
847 options.action = "abort";
848
849 rerere_clear(&merge_rr);
850 string_list_clear(&merge_rr, 1);
851
852 if (read_basic_state(&options))
853 exit(1);
854 if (reset_head(&options.orig_head, "reset",
855 options.head_name, 0) < 0)
856 die(_("could not move back to %s"),
857 oid_to_hex(&options.orig_head));
858 ret = finish_rebase(&options);
859 goto cleanup;
860 }
5a614945
PK
861 case ACTION_QUIT: {
862 strbuf_reset(&buf);
863 strbuf_addstr(&buf, options.state_dir);
864 ret = !!remove_dir_recursively(&buf, 0);
865 if (ret)
866 die(_("could not remove '%s'"), options.state_dir);
867 goto cleanup;
868 }
51e9ea6d
PK
869 case ACTION_EDIT_TODO:
870 options.action = "edit-todo";
871 options.dont_finish_rebase = 1;
872 goto run_rebase;
873 case ACTION_SHOW_CURRENT_PATCH:
874 options.action = "show-current-patch";
875 options.dont_finish_rebase = 1;
876 goto run_rebase;
877 case NO_ACTION:
878 break;
f9573628 879 default:
51e9ea6d 880 BUG("action: %d", action);
f9573628
PK
881 }
882
c54dacb5
PK
883 /* Make sure no rebase is in progress */
884 if (in_progress) {
885 const char *last_slash = strrchr(options.state_dir, '/');
886 const char *state_dir_base =
887 last_slash ? last_slash + 1 : options.state_dir;
888 const char *cmd_live_rebase =
889 "git rebase (--continue | --abort | --skip)";
890 strbuf_reset(&buf);
891 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
892 die(_("It seems that there is already a %s directory, and\n"
893 "I wonder if you are in the middle of another rebase. "
894 "If that is the\n"
895 "case, please try\n\t%s\n"
896 "If that is not the case, please\n\t%s\n"
897 "and run me again. I am stopping in case you still "
898 "have something\n"
899 "valuable there.\n"),
900 state_dir_base, cmd_live_rebase, buf.buf);
901 }
902
b4c8eb02
PK
903 if (!(options.flags & REBASE_NO_QUIET))
904 strbuf_addstr(&options.git_am_opt, " -q");
905
38dbcef2
PK
906 if (committer_date_is_author_date) {
907 strbuf_addstr(&options.git_am_opt,
908 " --committer-date-is-author-date");
909 options.flags |= REBASE_FORCE;
910 }
911
99d8cc7c
PK
912 if (ignore_whitespace)
913 strbuf_addstr(&options.git_am_opt, " --ignore-whitespace");
914
53f9e5be
PK
915 if (ignore_date) {
916 strbuf_addstr(&options.git_am_opt, " --ignore-date");
917 options.flags |= REBASE_FORCE;
918 }
919
002ee2fe
PK
920 if (options.keep_empty)
921 imply_interactive(&options, "--keep-empty");
922
12026a41
PK
923 if (gpg_sign) {
924 free(options.gpg_sign_opt);
925 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
926 }
927
7998dbe1
PK
928 if (opt_c >= 0)
929 strbuf_addf(&options.git_am_opt, " -C%d", opt_c);
930
931 if (whitespace.nr) {
932 int i;
933
934 for (i = 0; i < whitespace.nr; i++) {
935 const char *item = whitespace.items[i].string;
936
937 strbuf_addf(&options.git_am_opt, " --whitespace=%s",
938 item);
939
940 if ((!strcmp(item, "fix")) || (!strcmp(item, "strip")))
941 options.flags |= REBASE_FORCE;
942 }
943 }
944
68e46d78
PK
945 if (exec.nr) {
946 int i;
947
948 imply_interactive(&options, "--exec");
949
950 strbuf_reset(&buf);
951 for (i = 0; i < exec.nr; i++)
952 strbuf_addf(&buf, "exec %s\n", exec.items[i].string);
953 options.cmd = xstrdup(buf.buf);
954 }
955
3c3588c7
PK
956 if (rebase_merges) {
957 if (!*rebase_merges)
958 ; /* default mode; do nothing */
959 else if (!strcmp("rebase-cousins", rebase_merges))
960 options.rebase_cousins = 1;
961 else if (strcmp("no-rebase-cousins", rebase_merges))
962 die(_("Unknown mode: %s"), rebase_merges);
963 options.rebase_merges = 1;
964 imply_interactive(&options, "--rebase-merges");
965 }
966
ac7f467f
PK
967 switch (options.type) {
968 case REBASE_MERGE:
969 case REBASE_INTERACTIVE:
970 case REBASE_PRESERVE_MERGES:
971 options.state_dir = merge_dir();
972 break;
973 case REBASE_AM:
974 options.state_dir = apply_dir();
975 break;
976 default:
977 /* the default rebase backend is `--am` */
978 options.type = REBASE_AM;
979 options.state_dir = apply_dir();
980 break;
981 }
982
73d51ed0
PK
983 if (options.signoff) {
984 if (options.type == REBASE_PRESERVE_MERGES)
985 die("cannot combine '--signoff' with "
986 "'--preserve-merges'");
987 strbuf_addstr(&options.git_am_opt, " --signoff");
988 options.flags |= REBASE_FORCE;
989 }
990
ac7f467f 991 if (!options.root) {
f28d40d3 992 if (argc < 1)
ac7f467f
PK
993 die("TODO: handle @{upstream}");
994 else {
f28d40d3 995 options.upstream_name = argv[0];
ac7f467f
PK
996 argc--;
997 argv++;
998 if (!strcmp(options.upstream_name, "-"))
999 options.upstream_name = "@{-1}";
1000 }
1001 options.upstream = peel_committish(options.upstream_name);
1002 if (!options.upstream)
1003 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 1004 options.upstream_arg = options.upstream_name;
ac7f467f
PK
1005 } else
1006 die("TODO: upstream for --root");
1007
1008 /* Make sure the branch to rebase onto is valid. */
1009 if (!options.onto_name)
1010 options.onto_name = options.upstream_name;
1011 if (strstr(options.onto_name, "...")) {
075bc852
PK
1012 if (get_oid_mb(options.onto_name, &merge_base) < 0)
1013 die(_("'%s': need exactly one merge base"),
1014 options.onto_name);
1015 options.onto = lookup_commit_or_die(&merge_base,
1016 options.onto_name);
ac7f467f
PK
1017 } else {
1018 options.onto = peel_committish(options.onto_name);
1019 if (!options.onto)
1020 die(_("Does not point to a valid commit '%s'"),
1021 options.onto_name);
1022 }
1023
1024 /*
1025 * If the branch to rebase is given, that is the branch we will rebase
1026 * branch_name -- branch/commit being rebased, or
1027 * HEAD (already detached)
1028 * orig_head -- commit object name of tip of the branch before rebasing
d4c569f8 1029 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
ac7f467f 1030 */
e65123a7
PK
1031 if (argc == 1) {
1032 /* Is it "rebase other branchname" or "rebase other commit"? */
1033 branch_name = argv[0];
1034 options.switch_to = argv[0];
1035
1036 /* Is it a local branch? */
1037 strbuf_reset(&buf);
1038 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1039 if (!read_ref(buf.buf, &options.orig_head))
1040 options.head_name = xstrdup(buf.buf);
1041 /* If not is it a valid ref (branch or commit)? */
1042 else if (!get_oid(branch_name, &options.orig_head))
1043 options.head_name = NULL;
1044 else
1045 die(_("fatal: no such branch/commit '%s'"),
1046 branch_name);
1047 } else if (argc == 0) {
ac7f467f
PK
1048 /* Do not need to switch branches, we are already on it. */
1049 options.head_name =
1050 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
1051 &flags));
1052 if (!options.head_name)
1053 die(_("No such ref: %s"), "HEAD");
1054 if (flags & REF_ISSYMREF) {
1055 if (!skip_prefix(options.head_name,
1056 "refs/heads/", &branch_name))
1057 branch_name = options.head_name;
1058
1059 } else {
d4c569f8
PK
1060 free(options.head_name);
1061 options.head_name = NULL;
ac7f467f
PK
1062 branch_name = "HEAD";
1063 }
1064 if (get_oid("HEAD", &options.orig_head))
1065 die(_("Could not resolve HEAD to a revision"));
e65123a7
PK
1066 } else
1067 BUG("unexpected number of arguments left to parse");
ac7f467f 1068
92d0d74e
PK
1069 if (fork_point > 0) {
1070 struct commit *head =
1071 lookup_commit_reference(the_repository,
1072 &options.orig_head);
1073 options.restrict_revision =
1074 get_fork_point(options.upstream_name, head);
1075 }
1076
e0333e5c
PK
1077 if (read_index(the_repository->index) < 0)
1078 die(_("could not read index"));
1079
6defce2b
PK
1080 if (options.autostash) {
1081 struct lock_file lock_file = LOCK_INIT;
1082 int fd;
1083
1084 fd = hold_locked_index(&lock_file, 0);
1085 refresh_cache(REFRESH_QUIET);
1086 if (0 <= fd)
1087 update_index_if_able(&the_index, &lock_file);
1088 rollback_lock_file(&lock_file);
1089
1090 if (has_unstaged_changes(0) || has_uncommitted_changes(0)) {
1091 const char *autostash =
1092 state_dir_path("autostash", &options);
1093 struct child_process stash = CHILD_PROCESS_INIT;
1094 struct object_id oid;
1095 struct commit *head =
1096 lookup_commit_reference(the_repository,
1097 &options.orig_head);
1098
1099 argv_array_pushl(&stash.args,
1100 "stash", "create", "autostash", NULL);
1101 stash.git_cmd = 1;
1102 stash.no_stdin = 1;
1103 strbuf_reset(&buf);
1104 if (capture_command(&stash, &buf, GIT_MAX_HEXSZ))
1105 die(_("Cannot autostash"));
1106 strbuf_trim_trailing_newline(&buf);
1107 if (get_oid(buf.buf, &oid))
1108 die(_("Unexpected stash response: '%s'"),
1109 buf.buf);
1110 strbuf_reset(&buf);
1111 strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV);
1112
1113 if (safe_create_leading_directories_const(autostash))
1114 die(_("Could not create directory for '%s'"),
1115 options.state_dir);
1116 write_file(autostash, "%s", buf.buf);
1117 printf(_("Created autostash: %s\n"), buf.buf);
1118 if (reset_head(&head->object.oid, "reset --hard",
1119 NULL, 0) < 0)
1120 die(_("could not reset --hard"));
1121 printf(_("HEAD is now at %s"),
1122 find_unique_abbrev(&head->object.oid,
1123 DEFAULT_ABBREV));
1124 strbuf_reset(&buf);
1125 pp_commit_easy(CMIT_FMT_ONELINE, head, &buf);
1126 if (buf.len > 0)
1127 printf(" %s", buf.buf);
1128 putchar('\n');
1129
1130 if (discard_index(the_repository->index) < 0 ||
1131 read_index(the_repository->index) < 0)
1132 die(_("could not read index"));
1133 }
1134 }
1135
e0333e5c
PK
1136 if (require_clean_work_tree("rebase",
1137 _("Please commit or stash them."), 1, 1)) {
1138 ret = 1;
1139 goto cleanup;
1140 }
1141
9a48a615
PK
1142 /*
1143 * Now we are rebasing commits upstream..orig_head (or with --root,
1144 * everything leading up to orig_head) on top of onto.
1145 */
1146
1147 /*
1148 * Check if we are already based on onto with linear history,
1149 * but this should be done only when upstream and onto are the same
1150 * and if this is not an interactive rebase.
1151 */
1152 if (can_fast_forward(options.onto, &options.orig_head, &merge_base) &&
1153 !is_interactive(&options) && !options.restrict_revision &&
1154 !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) {
1155 int flag;
1156
1ed9c14f 1157 if (!(options.flags & REBASE_FORCE)) {
e65123a7
PK
1158 /* Lazily switch to the target branch if needed... */
1159 if (options.switch_to) {
1160 struct object_id oid;
1161
1162 if (get_oid(options.switch_to, &oid) < 0) {
1163 ret = !!error(_("could not parse '%s'"),
1164 options.switch_to);
1165 goto cleanup;
1166 }
1167
1168 strbuf_reset(&buf);
1169 strbuf_addf(&buf, "rebase: checkout %s",
1170 options.switch_to);
1171 if (reset_head(&oid, "checkout",
1172 options.head_name, 0) < 0) {
1173 ret = !!error(_("could not switch to "
1174 "%s"),
1175 options.switch_to);
1176 goto cleanup;
1177 }
1178 }
1179
1ed9c14f
PK
1180 if (!(options.flags & REBASE_NO_QUIET))
1181 ; /* be quiet */
1182 else if (!strcmp(branch_name, "HEAD") &&
1183 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1184 puts(_("HEAD is up to date."));
1185 else
1186 printf(_("Current branch %s is up to date.\n"),
1187 branch_name);
1188 ret = !!finish_rebase(&options);
1189 goto cleanup;
1190 } else if (!(options.flags & REBASE_NO_QUIET))
9a48a615
PK
1191 ; /* be quiet */
1192 else if (!strcmp(branch_name, "HEAD") &&
1193 resolve_ref_unsafe("HEAD", 0, NULL, &flag))
1194 puts(_("HEAD is up to date, rebase forced."));
1195 else
1196 printf(_("Current branch %s is up to date, rebase "
1197 "forced.\n"), branch_name);
1198 }
1199
06e4775a
PK
1200 /* If a hook exists, give it a chance to interrupt*/
1201 if (!ok_to_skip_pre_rebase &&
1202 run_hook_le(NULL, "pre-rebase", options.upstream_arg,
1203 argc ? argv[0] : NULL, NULL))
1204 die(_("The pre-rebase hook refused to rebase."));
1205
bff014da
PK
1206 if (options.flags & REBASE_DIFFSTAT) {
1207 struct diff_options opts;
1208
1209 if (options.flags & REBASE_VERBOSE)
1210 printf(_("Changes from %s to %s:\n"),
1211 oid_to_hex(&merge_base),
1212 oid_to_hex(&options.onto->object.oid));
1213
1214 /* We want color (if set), but no pager */
1215 diff_setup(&opts);
1216 opts.stat_width = -1; /* use full terminal width */
1217 opts.stat_graph_width = -1; /* respect statGraphWidth config */
1218 opts.output_format |=
1219 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1220 opts.detect_rename = DIFF_DETECT_RENAME;
1221 diff_setup_done(&opts);
1222 diff_tree_oid(&merge_base, &options.onto->object.oid,
1223 "", &opts);
1224 diffcore_std(&opts);
1225 diff_flush(&opts);
1226 }
1227
361badd3
PK
1228 if (is_interactive(&options))
1229 goto run_rebase;
1230
bff014da
PK
1231 /* Detach HEAD and reset the tree */
1232 if (options.flags & REBASE_NO_QUIET)
1233 printf(_("First, rewinding head to replay your work on top of "
1234 "it...\n"));
1235
ac7f467f
PK
1236 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
1237 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
1238 die(_("Could not detach HEAD"));
1239 strbuf_release(&msg);
1240
1241 strbuf_addf(&revisions, "%s..%s",
1242 options.root ? oid_to_hex(&options.onto->object.oid) :
1243 (options.restrict_revision ?
1244 oid_to_hex(&options.restrict_revision->object.oid) :
1245 oid_to_hex(&options.upstream->object.oid)),
1246 oid_to_hex(&options.orig_head));
1247
1248 options.revisions = revisions.buf;
1249
f9573628 1250run_rebase:
ac7f467f
PK
1251 ret = !!run_specific_rebase(&options);
1252
e0333e5c 1253cleanup:
ac7f467f
PK
1254 strbuf_release(&revisions);
1255 free(options.head_name);
12026a41 1256 free(options.gpg_sign_opt);
68e46d78 1257 free(options.cmd);
ac7f467f 1258 return ret;
55071ea2 1259}