Raw
1 /*
2 * "git rebase" builtin command
3 *
4 * Copyright (c) 2018 Pratik Karki
5 */
6
7 #define USE_THE_REPOSITORY_VARIABLE
8 #define DISABLE_SIGN_COMPARE_WARNINGS
9
10 #include "builtin.h"
11
12 #include "abspath.h"
13 #include "environment.h"
14 #include "gettext.h"
15 #include "hex.h"
16 #include "run-command.h"
17 #include "strvec.h"
18 #include "dir.h"
19 #include "refs.h"
20 #include "config.h"
21 #include "unpack-trees.h"
22 #include "lockfile.h"
23 #include "object-file.h"
24 #include "object-name.h"
25 #include "parse-options.h"
26 #include "path.h"
27 #include "commit.h"
28 #include "diff.h"
29 #include "wt-status.h"
30 #include "revision.h"
31 #include "commit-reach.h"
32 #include "rerere.h"
33 #include "branch.h"
34 #include "sequencer.h"
35 #include "rebase-interactive.h"
36 #include "reset.h"
37 #include "trace2.h"
38 #include "hook.h"
39 #include "trailer.h"
40
41 static char const * const builtin_rebase_usage[] = {
42 N_("git rebase [-i] [options] [--exec <cmd>] "
43 "[--onto <newbase> | --keep-base] [<upstream> [<branch>]]"),
44 N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] "
45 "--root [<branch>]"),
46 "git rebase --continue [--[no-]edit] | --abort | --skip | --edit-todo",
47 NULL
48 };
49
50 static GIT_PATH_FUNC(path_squash_onto, "rebase-merge/squash-onto")
51 static GIT_PATH_FUNC(path_interactive, "rebase-merge/interactive")
52 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
53 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
54
55 enum rebase_type {
56 REBASE_UNSPECIFIED = -1,
57 REBASE_APPLY,
58 REBASE_MERGE
59 };
60
61 enum empty_type {
62 EMPTY_UNSPECIFIED = -1,
63 EMPTY_DROP,
64 EMPTY_KEEP,
65 EMPTY_STOP
66 };
67
68 enum action {
69 ACTION_NONE = 0,
70 ACTION_CONTINUE,
71 ACTION_SKIP,
72 ACTION_ABORT,
73 ACTION_QUIT,
74 ACTION_EDIT_TODO,
75 ACTION_SHOW_CURRENT_PATCH
76 };
77
78 static const char *action_names[] = {
79 "undefined",
80 "continue",
81 "skip",
82 "abort",
83 "quit",
84 "edit_todo",
85 "show_current_patch"
86 };
87
88 struct rebase_options {
89 enum rebase_type type;
90 enum empty_type empty;
91 char *default_backend;
92 const char *state_dir;
93 struct commit *upstream;
94 const char *upstream_name;
95 const char *upstream_arg;
96 char *head_name;
97 struct commit *orig_head;
98 struct commit *onto;
99 const char *onto_name;
100 const char *revisions;
101 const char *switch_to;
102 int root, root_with_onto;
103 struct object_id *squash_onto;
104 struct commit *restrict_revision;
105 int dont_finish_rebase;
106 enum {
107 REBASE_NO_QUIET = 1<<0,
108 REBASE_VERBOSE = 1<<1,
109 REBASE_DIFFSTAT = 1<<2,
110 REBASE_FORCE = 1<<3,
111 REBASE_INTERACTIVE_EXPLICIT = 1<<4,
112 } flags;
113 struct strvec git_am_opts;
114 enum action action;
115 char *reflog_action;
116 int signoff;
117 struct strvec trailer_args;
118 int allow_rerere_autoupdate;
119 int keep_empty;
120 int autosquash;
121 char *gpg_sign_opt;
122 int autostash;
123 int committer_date_is_author_date;
124 int ignore_date;
125 struct string_list exec;
126 int allow_empty_message;
127 int rebase_merges, rebase_cousins;
128 char *strategy;
129 struct string_list strategy_opts;
130 struct strbuf git_format_patch_opt;
131 int reschedule_failed_exec;
132 int reapply_cherry_picks;
133 int fork_point;
134 int update_refs;
135 int config_autosquash;
136 int config_rebase_merges;
137 int config_update_refs;
138 int config_no_edit;
139 int edit;
140 };
141
142 #define REBASE_OPTIONS_INIT { \
143 .type = REBASE_UNSPECIFIED, \
144 .empty = EMPTY_UNSPECIFIED, \
145 .keep_empty = 1, \
146 .default_backend = xstrdup("merge"), \
147 .flags = REBASE_NO_QUIET, \
148 .git_am_opts = STRVEC_INIT, \
149 .exec = STRING_LIST_INIT_NODUP, \
150 .trailer_args = STRVEC_INIT, \
151 .git_format_patch_opt = STRBUF_INIT, \
152 .fork_point = -1, \
153 .reapply_cherry_picks = -1, \
154 .allow_empty_message = 1, \
155 .autosquash = -1, \
156 .rebase_merges = -1, \
157 .config_rebase_merges = -1, \
158 .update_refs = -1, \
159 .config_update_refs = -1, \
160 .strategy_opts = STRING_LIST_INIT_NODUP,\
161 .config_no_edit = -1, \
162 .edit = -1, \
163 }
164
165 static void rebase_options_release(struct rebase_options *opts)
166 {
167 free(opts->default_backend);
168 free(opts->reflog_action);
169 free(opts->head_name);
170 strvec_clear(&opts->git_am_opts);
171 free(opts->gpg_sign_opt);
172 string_list_clear(&opts->exec, 0);
173 free(opts->strategy);
174 string_list_clear(&opts->strategy_opts, 0);
175 strbuf_release(&opts->git_format_patch_opt);
176 strvec_clear(&opts->trailer_args);
177 }
178
179 static struct replay_opts get_replay_opts(const struct rebase_options *opts)
180 {
181 struct replay_opts replay = REPLAY_OPTS_INIT;
182
183 replay.action = REPLAY_INTERACTIVE_REBASE;
184 replay.strategy = NULL;
185 sequencer_init_config(&replay);
186
187 replay.signoff = opts->signoff;
188
189 strvec_pushv(&replay.trailer_args, opts->trailer_args.v);
190
191 replay.allow_ff = !(opts->flags & REBASE_FORCE);
192 if (opts->allow_rerere_autoupdate)
193 replay.allow_rerere_auto = opts->allow_rerere_autoupdate;
194 replay.allow_empty = 1;
195 replay.allow_empty_message = opts->allow_empty_message;
196 replay.drop_redundant_commits = (opts->empty == EMPTY_DROP);
197 replay.keep_redundant_commits = (opts->empty == EMPTY_KEEP);
198 replay.quiet = !(opts->flags & REBASE_NO_QUIET);
199 replay.verbose = opts->flags & REBASE_VERBOSE;
200 replay.reschedule_failed_exec = opts->reschedule_failed_exec;
201 replay.committer_date_is_author_date =
202 opts->committer_date_is_author_date;
203 replay.ignore_date = opts->ignore_date;
204 free(replay.gpg_sign);
205 replay.gpg_sign = xstrdup_or_null(opts->gpg_sign_opt);
206 replay.reflog_action = xstrdup(opts->reflog_action);
207 if (opts->strategy)
208 replay.strategy = xstrdup_or_null(opts->strategy);
209 else if (!replay.strategy && replay.default_strategy) {
210 replay.strategy = replay.default_strategy;
211 replay.default_strategy = NULL;
212 }
213
214 for (size_t i = 0; i < opts->strategy_opts.nr; i++)
215 strvec_push(&replay.xopts, opts->strategy_opts.items[i].string);
216
217 if (opts->squash_onto) {
218 oidcpy(&replay.squash_onto, opts->squash_onto);
219 replay.have_squash_onto = 1;
220 }
221
222 if (opts->action == ACTION_CONTINUE) {
223 if (opts->edit >= 0)
224 replay.edit = opts->edit;
225 else if (opts->config_no_edit > 0)
226 replay.edit = 0;
227 }
228
229 return replay;
230 }
231
232 static int edit_todo_file(unsigned flags, struct replay_opts *opts)
233 {
234 const char *todo_file = rebase_path_todo();
235 struct todo_list todo_list = TODO_LIST_INIT,
236 new_todo = TODO_LIST_INIT;
237 int res = 0;
238
239 if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
240 return error_errno(_("could not read '%s'."), todo_file);
241
242 strbuf_stripspace(&todo_list.buf, comment_line_str);
243 res = edit_todo_list(the_repository, opts, &todo_list, &new_todo,
244 NULL, NULL, flags);
245 if (!res && todo_list_write_to_file(the_repository, &new_todo, todo_file,
246 NULL, NULL, -1, flags & ~(TODO_LIST_SHORTEN_IDS)))
247 res = error_errno(_("could not write '%s'"), todo_file);
248
249 todo_list_release(&todo_list);
250 todo_list_release(&new_todo);
251
252 return res;
253 }
254
255 static int get_revision_ranges(struct commit *upstream, struct commit *onto,
256 struct object_id *orig_head, char **revisions,
257 char **shortrevisions)
258 {
259 struct commit *base_rev = upstream ? upstream : onto;
260 const char *shorthead;
261
262 *revisions = xstrfmt("%s...%s", oid_to_hex(&base_rev->object.oid),
263 oid_to_hex(orig_head));
264
265 shorthead = repo_find_unique_abbrev(the_repository, orig_head,
266 DEFAULT_ABBREV);
267
268 if (upstream) {
269 const char *shortrev;
270
271 shortrev = repo_find_unique_abbrev(the_repository,
272 &base_rev->object.oid,
273 DEFAULT_ABBREV);
274
275 *shortrevisions = xstrfmt("%s..%s", shortrev, shorthead);
276 } else
277 *shortrevisions = xstrdup(shorthead);
278
279 return 0;
280 }
281
282 static int init_basic_state(struct replay_opts *opts, const char *head_name,
283 struct commit *onto,
284 const struct object_id *orig_head)
285 {
286 FILE *interactive;
287
288 if (!is_directory(merge_dir()) &&
289 safe_create_dir_in_gitdir(the_repository, merge_dir()))
290 return error_errno(_("could not create temporary %s"), merge_dir());
291
292 refs_delete_reflog(get_main_ref_store(the_repository), "REBASE_HEAD");
293
294 interactive = fopen(path_interactive(), "w");
295 if (!interactive)
296 return error_errno(_("could not mark as interactive"));
297 fclose(interactive);
298
299 return write_basic_state(opts, head_name, onto, orig_head);
300 }
301
302 static int do_interactive_rebase(struct rebase_options *opts, unsigned flags)
303 {
304 int ret = -1;
305 char *revisions = NULL, *shortrevisions = NULL;
306 struct strvec make_script_args = STRVEC_INIT;
307 struct todo_list todo_list = TODO_LIST_INIT;
308 struct replay_opts replay = get_replay_opts(opts);
309
310 if (get_revision_ranges(opts->upstream, opts->onto, &opts->orig_head->object.oid,
311 &revisions, &shortrevisions))
312 goto cleanup;
313
314 strvec_pushl(&make_script_args, "", revisions, NULL);
315 if (opts->restrict_revision)
316 strvec_pushf(&make_script_args, "^%s",
317 oid_to_hex(&opts->restrict_revision->object.oid));
318
319 ret = sequencer_make_script(the_repository, &todo_list.buf,
320 &make_script_args, flags);
321 if (ret) {
322 error(_("could not generate todo list"));
323 goto cleanup;
324 }
325
326 if (init_basic_state(&replay,
327 opts->head_name ? opts->head_name : "detached HEAD",
328 opts->onto, &opts->orig_head->object.oid))
329 goto cleanup;
330
331 if (!opts->upstream && opts->squash_onto)
332 write_file(path_squash_onto(), "%s\n",
333 oid_to_hex(opts->squash_onto));
334
335 discard_index(the_repository->index);
336 if (todo_list_parse_insn_buffer(the_repository, &replay,
337 todo_list.buf.buf, &todo_list))
338 BUG("unusable todo list");
339
340 ret = complete_action(the_repository, &replay, flags,
341 shortrevisions, opts->onto_name, opts->onto,
342 &opts->orig_head->object.oid, &opts->exec,
343 opts->autosquash, opts->update_refs, &todo_list);
344
345 cleanup:
346 replay_opts_release(&replay);
347 free(revisions);
348 free(shortrevisions);
349 todo_list_release(&todo_list);
350 strvec_clear(&make_script_args);
351
352 return ret;
353 }
354
355 static int run_sequencer_rebase(struct rebase_options *opts)
356 {
357 unsigned flags = 0;
358 int abbreviate_commands = 0, ret = 0;
359
360 repo_config_get_bool(the_repository, "rebase.abbreviatecommands", &abbreviate_commands);
361
362 flags |= opts->keep_empty ? TODO_LIST_KEEP_EMPTY : 0;
363 flags |= abbreviate_commands ? TODO_LIST_ABBREVIATE_CMDS : 0;
364 flags |= opts->rebase_merges ? TODO_LIST_REBASE_MERGES : 0;
365 flags |= opts->rebase_cousins > 0 ? TODO_LIST_REBASE_COUSINS : 0;
366 flags |= opts->root_with_onto ? TODO_LIST_ROOT_WITH_ONTO : 0;
367 flags |= opts->reapply_cherry_picks ? TODO_LIST_REAPPLY_CHERRY_PICKS : 0;
368 flags |= opts->flags & REBASE_NO_QUIET ? TODO_LIST_WARN_SKIPPED_CHERRY_PICKS : 0;
369
370 switch (opts->action) {
371 case ACTION_NONE: {
372 if (!opts->onto && !opts->upstream)
373 die(_("a base commit must be provided with --upstream or --onto"));
374
375 ret = do_interactive_rebase(opts, flags);
376 break;
377 }
378 case ACTION_SKIP: {
379 struct string_list merge_rr = STRING_LIST_INIT_DUP;
380
381 rerere_clear(the_repository, &merge_rr);
382 }
383 /* fallthrough */
384 case ACTION_CONTINUE: {
385 struct replay_opts replay_opts = get_replay_opts(opts);
386
387 ret = sequencer_continue(the_repository, &replay_opts);
388 replay_opts_release(&replay_opts);
389 break;
390 }
391 case ACTION_EDIT_TODO: {
392 struct replay_opts replay_opts = get_replay_opts(opts);
393
394 ret = edit_todo_file(flags, &replay_opts);
395 replay_opts_release(&replay_opts);
396 break;
397 }
398 case ACTION_SHOW_CURRENT_PATCH: {
399 struct child_process cmd = CHILD_PROCESS_INIT;
400
401 cmd.git_cmd = 1;
402 strvec_pushl(&cmd.args, "show", "REBASE_HEAD", "--", NULL);
403 ret = run_command(&cmd);
404
405 break;
406 }
407 default:
408 BUG("invalid command '%d'", opts->action);
409 }
410
411 return ret;
412 }
413
414 static int is_merge(struct rebase_options *opts)
415 {
416 return opts->type == REBASE_MERGE;
417 }
418
419 static void imply_merge(struct rebase_options *opts, const char *option)
420 {
421 switch (opts->type) {
422 case REBASE_APPLY:
423 die(_("%s requires the merge backend"), option);
424 break;
425 case REBASE_MERGE:
426 break;
427 default:
428 opts->type = REBASE_MERGE; /* implied */
429 break;
430 }
431 }
432
433 /* Returns the filename prefixed by the state_dir */
434 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
435 {
436 static struct strbuf path = STRBUF_INIT;
437 static size_t prefix_len;
438
439 if (!prefix_len) {
440 strbuf_addf(&path, "%s/", opts->state_dir);
441 prefix_len = path.len;
442 }
443
444 strbuf_setlen(&path, prefix_len);
445 strbuf_addstr(&path, filename);
446 return path.buf;
447 }
448
449 /* Initialize the rebase options from the state directory. */
450 static int read_basic_state(struct rebase_options *opts)
451 {
452 struct strbuf head_name = STRBUF_INIT;
453 struct strbuf buf = STRBUF_INIT;
454 struct object_id oid;
455
456 if (!read_oneliner(&head_name, state_dir_path("head-name", opts),
457 READ_ONELINER_WARN_MISSING) ||
458 !read_oneliner(&buf, state_dir_path("onto", opts),
459 READ_ONELINER_WARN_MISSING))
460 return -1;
461 opts->head_name = starts_with(head_name.buf, "refs/") ?
462 xstrdup(head_name.buf) : NULL;
463 strbuf_release(&head_name);
464 if (get_oid_hex(buf.buf, &oid) ||
465 !(opts->onto = lookup_commit_object(the_repository, &oid)))
466 return error(_("invalid onto: '%s'"), buf.buf);
467
468 /*
469 * We always write to orig-head, but interactive rebase used to write to
470 * head. Fall back to reading from head to cover for the case that the
471 * user upgraded git with an ongoing interactive rebase.
472 */
473 strbuf_reset(&buf);
474 if (file_exists(state_dir_path("orig-head", opts))) {
475 if (!read_oneliner(&buf, state_dir_path("orig-head", opts),
476 READ_ONELINER_WARN_MISSING))
477 return -1;
478 } else if (!read_oneliner(&buf, state_dir_path("head", opts),
479 READ_ONELINER_WARN_MISSING))
480 return -1;
481 if (get_oid_hex(buf.buf, &oid) ||
482 !(opts->orig_head = lookup_commit_object(the_repository, &oid)))
483 return error(_("invalid orig-head: '%s'"), buf.buf);
484
485 if (file_exists(state_dir_path("quiet", opts)))
486 opts->flags &= ~REBASE_NO_QUIET;
487 else
488 opts->flags |= REBASE_NO_QUIET;
489
490 if (file_exists(state_dir_path("verbose", opts)))
491 opts->flags |= REBASE_VERBOSE;
492
493 if (file_exists(state_dir_path("signoff", opts))) {
494 opts->signoff = 1;
495 opts->flags |= REBASE_FORCE;
496 }
497
498 if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) {
499 strbuf_reset(&buf);
500 if (!read_oneliner(&buf, state_dir_path("allow_rerere_autoupdate", opts),
501 READ_ONELINER_WARN_MISSING))
502 return -1;
503 if (!strcmp(buf.buf, "--rerere-autoupdate"))
504 opts->allow_rerere_autoupdate = RERERE_AUTOUPDATE;
505 else if (!strcmp(buf.buf, "--no-rerere-autoupdate"))
506 opts->allow_rerere_autoupdate = RERERE_NOAUTOUPDATE;
507 else
508 warning(_("ignoring invalid allow_rerere_autoupdate: "
509 "'%s'"), buf.buf);
510 }
511
512 if (file_exists(state_dir_path("gpg_sign_opt", opts))) {
513 strbuf_reset(&buf);
514 if (!read_oneliner(&buf, state_dir_path("gpg_sign_opt", opts),
515 READ_ONELINER_WARN_MISSING))
516 return -1;
517 free(opts->gpg_sign_opt);
518 opts->gpg_sign_opt = xstrdup(buf.buf);
519 }
520
521 strbuf_release(&buf);
522
523 return 0;
524 }
525
526 static int rebase_write_basic_state(struct rebase_options *opts)
527 {
528 write_file(state_dir_path("head-name", opts), "%s",
529 opts->head_name ? opts->head_name : "detached HEAD");
530 write_file(state_dir_path("onto", opts), "%s",
531 opts->onto ? oid_to_hex(&opts->onto->object.oid) : "");
532 write_file(state_dir_path("orig-head", opts), "%s",
533 oid_to_hex(&opts->orig_head->object.oid));
534 if (!(opts->flags & REBASE_NO_QUIET))
535 write_file(state_dir_path("quiet", opts), "%s", "");
536 if (opts->flags & REBASE_VERBOSE)
537 write_file(state_dir_path("verbose", opts), "%s", "");
538 if (opts->allow_rerere_autoupdate > 0)
539 write_file(state_dir_path("allow_rerere_autoupdate", opts),
540 "-%s-rerere-autoupdate",
541 opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE ?
542 "" : "-no");
543 if (opts->gpg_sign_opt)
544 write_file(state_dir_path("gpg_sign_opt", opts), "%s",
545 opts->gpg_sign_opt);
546 if (opts->signoff)
547 write_file(state_dir_path("signoff", opts), "--signoff");
548
549 return 0;
550 }
551
552 static int cleanup_autostash(struct rebase_options *opts)
553 {
554 int ret;
555 struct strbuf dir = STRBUF_INIT;
556 const char *path = state_dir_path("autostash", opts);
557
558 if (!file_exists(path))
559 return 0;
560 ret = apply_autostash(path);
561 strbuf_addstr(&dir, opts->state_dir);
562 if (remove_dir_recursively(&dir, 0))
563 ret = error_errno(_("could not remove '%s'"), opts->state_dir);
564 strbuf_release(&dir);
565
566 return ret;
567 }
568
569 static int finish_rebase(struct rebase_options *opts)
570 {
571 struct strbuf dir = STRBUF_INIT;
572 int ret = 0;
573
574 refs_delete_ref(get_main_ref_store(the_repository), NULL,
575 "REBASE_HEAD", NULL, REF_NO_DEREF);
576 refs_delete_ref(get_main_ref_store(the_repository), NULL,
577 "AUTO_MERGE", NULL, REF_NO_DEREF);
578 apply_autostash(state_dir_path("autostash", opts));
579 /*
580 * We ignore errors in 'git maintenance run --auto', since the
581 * user should see them.
582 */
583 run_auto_maintenance(the_repository,
584 !(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE)));
585
586 if (opts->type == REBASE_MERGE) {
587 struct replay_opts replay = REPLAY_OPTS_INIT;
588
589 replay.action = REPLAY_INTERACTIVE_REBASE;
590 ret = sequencer_remove_state(&replay);
591 replay_opts_release(&replay);
592 } else {
593 strbuf_addstr(&dir, opts->state_dir);
594 if (remove_dir_recursively(&dir, 0))
595 ret = error(_("could not remove '%s'"),
596 opts->state_dir);
597 strbuf_release(&dir);
598 }
599
600 return ret;
601 }
602
603 static int move_to_original_branch(struct rebase_options *opts)
604 {
605 struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
606 struct reset_working_tree_options ropts = { 0 };
607 int ret;
608
609 if (!opts->head_name)
610 return 0; /* nothing to move back to */
611
612 if (!opts->onto)
613 BUG("move_to_original_branch without onto");
614
615 strbuf_addf(&branch_reflog, "%s (finish): %s onto %s",
616 opts->reflog_action,
617 opts->head_name, oid_to_hex(&opts->onto->object.oid));
618 strbuf_addf(&head_reflog, "%s (finish): returning to %s",
619 opts->reflog_action, opts->head_name);
620 ropts.branch = opts->head_name;
621 ropts.flags = RESET_WORKING_TREE_REFS_ONLY |
622 RESET_WORKING_TREE_UPDATE_HEAD;
623 ropts.branch_msg = branch_reflog.buf;
624 ropts.head_msg = head_reflog.buf;
625 ret = reset_working_tree(the_repository, &ropts);
626
627 strbuf_release(&branch_reflog);
628 strbuf_release(&head_reflog);
629 return ret;
630 }
631
632 static int run_am(struct rebase_options *opts)
633 {
634 struct child_process am = CHILD_PROCESS_INIT;
635 struct child_process format_patch = CHILD_PROCESS_INIT;
636 int status;
637 char *rebased_patches;
638
639 am.git_cmd = 1;
640 strvec_push(&am.args, "am");
641 strvec_pushf(&am.env, GIT_REFLOG_ACTION_ENVIRONMENT "=%s (pick)",
642 opts->reflog_action);
643 if (opts->action == ACTION_CONTINUE) {
644 strvec_push(&am.args, "--resolved");
645 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
646 if (opts->gpg_sign_opt)
647 strvec_push(&am.args, opts->gpg_sign_opt);
648 status = run_command(&am);
649 if (status)
650 return status;
651
652 return move_to_original_branch(opts);
653 }
654 if (opts->action == ACTION_SKIP) {
655 strvec_push(&am.args, "--skip");
656 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
657 status = run_command(&am);
658 if (status)
659 return status;
660
661 return move_to_original_branch(opts);
662 }
663 if (opts->action == ACTION_SHOW_CURRENT_PATCH) {
664 strvec_push(&am.args, "--show-current-patch");
665 return run_command(&am);
666 }
667
668 rebased_patches = repo_git_path(the_repository, "rebased-patches");
669 format_patch.out = open(rebased_patches,
670 O_WRONLY | O_CREAT | O_TRUNC, 0666);
671 if (format_patch.out < 0) {
672 status = error_errno(_("could not open '%s' for writing"),
673 rebased_patches);
674 free(rebased_patches);
675 child_process_clear(&am);
676 return status;
677 }
678
679 format_patch.git_cmd = 1;
680 strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
681 "--full-index", "--cherry-pick", "--right-only",
682 "--default-prefix", "--no-renames",
683 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
684 "--no-base", NULL);
685 if (opts->git_format_patch_opt.len)
686 strvec_split(&format_patch.args,
687 opts->git_format_patch_opt.buf);
688 strvec_pushf(&format_patch.args, "%s...%s",
689 oid_to_hex(opts->root ?
690 /* this is now equivalent to !opts->upstream */
691 &opts->onto->object.oid :
692 &opts->upstream->object.oid),
693 oid_to_hex(&opts->orig_head->object.oid));
694 if (opts->restrict_revision)
695 strvec_pushf(&format_patch.args, "^%s",
696 oid_to_hex(&opts->restrict_revision->object.oid));
697
698 status = run_command(&format_patch);
699 if (status) {
700 struct reset_working_tree_options ropts = { 0 };
701 unlink(rebased_patches);
702 free(rebased_patches);
703 child_process_clear(&am);
704
705 ropts.oid = &opts->orig_head->object.oid;
706 ropts.branch = opts->head_name;
707 ropts.default_reflog_action = opts->reflog_action;
708 ropts.flags = RESET_WORKING_TREE_UPDATE_HEAD;
709 reset_working_tree(the_repository, &ropts);
710 error(_("\ngit encountered an error while preparing the "
711 "patches to replay\n"
712 "these revisions:\n"
713 "\n %s\n\n"
714 "As a result, git cannot rebase them."),
715 opts->revisions);
716
717 return status;
718 }
719
720 am.in = open(rebased_patches, O_RDONLY);
721 if (am.in < 0) {
722 status = error_errno(_("could not open '%s' for reading"),
723 rebased_patches);
724 free(rebased_patches);
725 child_process_clear(&am);
726 return status;
727 }
728
729 strvec_pushv(&am.args, opts->git_am_opts.v);
730 strvec_push(&am.args, "--rebasing");
731 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
732 strvec_push(&am.args, "--patch-format=mboxrd");
733 if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
734 strvec_push(&am.args, "--rerere-autoupdate");
735 else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
736 strvec_push(&am.args, "--no-rerere-autoupdate");
737 if (opts->gpg_sign_opt)
738 strvec_push(&am.args, opts->gpg_sign_opt);
739 status = run_command(&am);
740 unlink(rebased_patches);
741 free(rebased_patches);
742
743 if (!status) {
744 return move_to_original_branch(opts);
745 }
746
747 if (is_directory(opts->state_dir))
748 rebase_write_basic_state(opts);
749
750 return status;
751 }
752
753 static int run_specific_rebase(struct rebase_options *opts)
754 {
755 int status;
756
757 if (opts->type == REBASE_MERGE) {
758 /* Run sequencer-based rebase */
759 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT))
760 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
761 if (opts->gpg_sign_opt) {
762 /* remove the leading "-S" */
763 char *tmp = xstrdup(opts->gpg_sign_opt + 2);
764 free(opts->gpg_sign_opt);
765 opts->gpg_sign_opt = tmp;
766 }
767
768 status = run_sequencer_rebase(opts);
769 } else if (opts->type == REBASE_APPLY)
770 status = run_am(opts);
771 else
772 BUG("Unhandled rebase type %d", opts->type);
773
774 if (opts->dont_finish_rebase)
775 ; /* do nothing */
776 else if (opts->type == REBASE_MERGE)
777 ; /* merge backend cleans up after itself */
778 else if (status == 0) {
779 if (!file_exists(state_dir_path("stopped-sha", opts)))
780 finish_rebase(opts);
781 } else if (status == 2) {
782 struct strbuf dir = STRBUF_INIT;
783
784 apply_autostash(state_dir_path("autostash", opts));
785 strbuf_addstr(&dir, opts->state_dir);
786 remove_dir_recursively(&dir, 0);
787 strbuf_release(&dir);
788 die("Nothing to do");
789 }
790
791 return status ? -1 : 0;
792 }
793
794 static void parse_rebase_merges_value(struct rebase_options *options, const char *value)
795 {
796 if (!strcmp("no-rebase-cousins", value))
797 options->rebase_cousins = 0;
798 else if (!strcmp("rebase-cousins", value))
799 options->rebase_cousins = 1;
800 else
801 die(_("Unknown rebase-merges mode: %s"), value);
802 }
803
804 static int rebase_config(const char *var, const char *value,
805 const struct config_context *ctx, void *data)
806 {
807 struct rebase_options *opts = data;
808
809 if (!strcmp(var, "rebase.stat")) {
810 if (git_config_bool(var, value))
811 opts->flags |= REBASE_DIFFSTAT;
812 else
813 opts->flags &= ~REBASE_DIFFSTAT;
814 return 0;
815 }
816
817 if (!strcmp(var, "rebase.autosquash")) {
818 opts->config_autosquash = git_config_bool(var, value);
819 return 0;
820 }
821
822 if (!strcmp(var, "commit.gpgsign")) {
823 free(opts->gpg_sign_opt);
824 opts->gpg_sign_opt = git_config_bool(var, value) ?
825 xstrdup("-S") : NULL;
826 return 0;
827 }
828
829 if (!strcmp(var, "rebase.autostash")) {
830 opts->autostash = git_config_bool(var, value);
831 return 0;
832 }
833
834 if (!strcmp(var, "rebase.rebasemerges")) {
835 opts->config_rebase_merges = git_parse_maybe_bool(value);
836 if (opts->config_rebase_merges < 0) {
837 opts->config_rebase_merges = 1;
838 parse_rebase_merges_value(opts, value);
839 } else {
840 opts->rebase_cousins = 0;
841 }
842 return 0;
843 }
844
845 if (!strcmp(var, "rebase.updaterefs")) {
846 opts->config_update_refs = git_config_bool(var, value);
847 return 0;
848 }
849
850 if (!strcmp(var, "rebase.reschedulefailedexec")) {
851 opts->reschedule_failed_exec = git_config_bool(var, value);
852 return 0;
853 }
854
855 if (!strcmp(var, "rebase.noedit")) {
856 opts->config_no_edit = git_config_bool(var, value);
857 return 0;
858 }
859
860 if (!strcmp(var, "rebase.forkpoint")) {
861 opts->fork_point = git_config_bool(var, value) ? -1 : 0;
862 return 0;
863 }
864
865 if (!strcmp(var, "rebase.backend")) {
866 FREE_AND_NULL(opts->default_backend);
867 return git_config_string(&opts->default_backend, var, value);
868 }
869
870 return git_default_config(var, value, ctx, data);
871 }
872
873 static int checkout_up_to_date(struct rebase_options *options)
874 {
875 struct strbuf buf = STRBUF_INIT;
876 struct reset_working_tree_options ropts = { 0 };
877 int ret = 0;
878
879 strbuf_addf(&buf, "%s: checkout %s",
880 options->reflog_action, options->switch_to);
881 ropts.oid = &options->orig_head->object.oid;
882 ropts.branch = options->head_name;
883 ropts.flags = RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK |
884 RESET_WORKING_TREE_UPDATE_HEAD;
885 if (!ropts.branch)
886 ropts.flags |= RESET_WORKING_TREE_DETACH;
887 ropts.head_msg = buf.buf;
888 if (reset_working_tree(the_repository, &ropts) < 0)
889 ret = error(_("could not switch to %s"), options->switch_to);
890 strbuf_release(&buf);
891
892 return ret;
893 }
894
895 /*
896 * Determines whether the commits in from..to are linear, i.e. contain
897 * no merge commits. This function *expects* `from` to be an ancestor of
898 * `to`.
899 */
900 static int is_linear_history(struct commit *from, struct commit *to)
901 {
902 while (to && to != from) {
903 repo_parse_commit(the_repository, to);
904 if (!to->parents)
905 return 1;
906 if (to->parents->next)
907 return 0;
908 to = to->parents->item;
909 }
910 return 1;
911 }
912
913 static int can_fast_forward(struct commit *onto, struct commit *upstream,
914 struct commit *restrict_revision,
915 struct commit *head, struct object_id *branch_base)
916 {
917 struct commit_list *merge_bases = NULL;
918 int res = 0;
919
920 if (is_null_oid(branch_base))
921 goto done; /* fill_branch_base() found multiple merge bases */
922
923 if (!oideq(branch_base, &onto->object.oid))
924 goto done;
925
926 if (restrict_revision && !oideq(&restrict_revision->object.oid, branch_base))
927 goto done;
928
929 if (!upstream)
930 goto done;
931
932 if (repo_get_merge_bases(the_repository, upstream, head, &merge_bases) < 0)
933 exit(128);
934 if (!merge_bases || merge_bases->next)
935 goto done;
936
937 if (!oideq(&onto->object.oid, &merge_bases->item->object.oid))
938 goto done;
939
940 res = 1;
941
942 done:
943 commit_list_free(merge_bases);
944 return res && is_linear_history(onto, head);
945 }
946
947 static void fill_branch_base(struct rebase_options *options,
948 struct object_id *branch_base)
949 {
950 struct commit_list *merge_bases = NULL;
951
952 if (repo_get_merge_bases(the_repository, options->onto,
953 options->orig_head, &merge_bases) < 0)
954 exit(128);
955 if (!merge_bases || merge_bases->next)
956 oidcpy(branch_base, null_oid(the_hash_algo));
957 else
958 oidcpy(branch_base, &merge_bases->item->object.oid);
959
960 commit_list_free(merge_bases);
961 }
962
963 static int parse_opt_am(const struct option *opt, const char *arg, int unset)
964 {
965 struct rebase_options *opts = opt->value;
966
967 BUG_ON_OPT_NEG(unset);
968 BUG_ON_OPT_ARG(arg);
969
970 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_APPLY)
971 die(_("apply options and merge options cannot be used together"));
972
973 opts->type = REBASE_APPLY;
974
975 return 0;
976 }
977
978 /* -i followed by -m is still -i */
979 static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
980 {
981 struct rebase_options *opts = opt->value;
982
983 BUG_ON_OPT_NEG(unset);
984 BUG_ON_OPT_ARG(arg);
985
986 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE)
987 die(_("apply options and merge options cannot be used together"));
988
989 opts->type = REBASE_MERGE;
990
991 return 0;
992 }
993
994 /* -i followed by -r is still explicitly interactive, but -r alone is not */
995 static int parse_opt_interactive(const struct option *opt, const char *arg,
996 int unset)
997 {
998 struct rebase_options *opts = opt->value;
999
1000 BUG_ON_OPT_NEG(unset);
1001 BUG_ON_OPT_ARG(arg);
1002
1003 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE)
1004 die(_("apply options and merge options cannot be used together"));
1005
1006 opts->type = REBASE_MERGE;
1007 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
1008
1009 return 0;
1010 }
1011
1012 static enum empty_type parse_empty_value(const char *value)
1013 {
1014 if (!strcasecmp(value, "drop"))
1015 return EMPTY_DROP;
1016 else if (!strcasecmp(value, "keep"))
1017 return EMPTY_KEEP;
1018 else if (!strcasecmp(value, "stop"))
1019 return EMPTY_STOP;
1020 else if (!strcasecmp(value, "ask")) {
1021 warning(_("--empty=ask is deprecated; use '--empty=stop' instead."));
1022 return EMPTY_STOP;
1023 }
1024
1025 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value);
1026 }
1027
1028 static int parse_opt_keep_empty(const struct option *opt, const char *arg,
1029 int unset)
1030 {
1031 struct rebase_options *opts = opt->value;
1032
1033 BUG_ON_OPT_ARG(arg);
1034
1035 imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty");
1036 opts->keep_empty = !unset;
1037 return 0;
1038 }
1039
1040 static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
1041 {
1042 struct rebase_options *options = opt->value;
1043 enum empty_type value = parse_empty_value(arg);
1044
1045 BUG_ON_OPT_NEG(unset);
1046
1047 options->empty = value;
1048 return 0;
1049 }
1050
1051 static int parse_opt_rebase_merges(const struct option *opt, const char *arg, int unset)
1052 {
1053 struct rebase_options *options = opt->value;
1054
1055 options->rebase_merges = !unset;
1056 options->rebase_cousins = 0;
1057
1058 if (arg) {
1059 if (!*arg) {
1060 warning(_("--rebase-merges with an empty string "
1061 "argument is deprecated and will stop "
1062 "working in a future version of Git. Use "
1063 "--rebase-merges without an argument "
1064 "instead, which does the same thing."));
1065 return 0;
1066 }
1067 parse_rebase_merges_value(options, arg);
1068 }
1069
1070 return 0;
1071 }
1072
1073 static void NORETURN error_on_missing_default_upstream(void)
1074 {
1075 struct branch *current_branch = branch_get(NULL);
1076
1077 printf(_("%s\n"
1078 "Please specify which branch you want to rebase against.\n"
1079 "See git-rebase(1) for details.\n"
1080 "\n"
1081 " git rebase '<branch>'\n"
1082 "\n"),
1083 current_branch ? _("There is no tracking information for "
1084 "the current branch.") :
1085 _("You are not currently on a branch."));
1086
1087 if (current_branch) {
1088 const char *remote = current_branch->remote_name;
1089
1090 if (!remote)
1091 remote = _("<remote>");
1092
1093 printf(_("If you wish to set tracking information for this "
1094 "branch you can do so with:\n"
1095 "\n"
1096 " git branch --set-upstream-to=%s/<branch> %s\n"
1097 "\n"),
1098 remote, current_branch->name);
1099 }
1100 exit(1);
1101 }
1102
1103 static int check_exec_cmd(const char *cmd)
1104 {
1105 if (strchr(cmd, '\n'))
1106 return error(_("exec commands cannot contain newlines"));
1107
1108 /* Does the command consist purely of whitespace? */
1109 if (!cmd[strspn(cmd, " \t\r\f\v")])
1110 return error(_("empty exec command"));
1111
1112 return 0;
1113 }
1114
1115 int cmd_rebase(int argc,
1116 const char **argv,
1117 const char *prefix,
1118 struct repository *repo UNUSED)
1119 {
1120 struct rebase_options options = REBASE_OPTIONS_INIT;
1121 const char *branch_name;
1122 const char *strategy_opt = NULL;
1123 int ret, flags, total_argc, in_progress = 0;
1124 int keep_base = 0;
1125 int ok_to_skip_pre_rebase = 0;
1126 struct strbuf msg = STRBUF_INIT;
1127 struct strbuf revisions = STRBUF_INIT;
1128 struct strbuf buf = STRBUF_INIT;
1129 struct object_id branch_base;
1130 int ignore_whitespace = 0;
1131 const char *gpg_sign = NULL;
1132 struct object_id squash_onto;
1133 char *squash_onto_name = NULL;
1134 char *keep_base_onto_name = NULL;
1135 int reschedule_failed_exec = -1;
1136 int allow_preemptive_ff = 1;
1137 int preserve_merges_selected = 0;
1138 struct reset_working_tree_options ropts = { 0 };
1139 struct option builtin_rebase_options[] = {
1140 OPT_STRING(0, "onto", &options.onto_name,
1141 N_("revision"),
1142 N_("rebase onto given branch instead of upstream")),
1143 OPT_BOOL(0, "keep-base", &keep_base,
1144 N_("use the merge-base of upstream and branch as the current base")),
1145 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1146 N_("allow pre-rebase hook to run")),
1147 OPT_NEGBIT('q', "quiet", &options.flags,
1148 N_("be quiet. implies --no-stat"),
1149 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1150 OPT_BIT('v', "verbose", &options.flags,
1151 N_("display a diffstat of what changed upstream"),
1152 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1153 {
1154 .type = OPTION_NEGBIT,
1155 .short_name = 'n',
1156 .long_name = "no-stat",
1157 .value = &options.flags,
1158 .precision = sizeof(options.flags),
1159 .help = N_("do not show diffstat of what changed upstream"),
1160 .flags = PARSE_OPT_NOARG,
1161 .defval = REBASE_DIFFSTAT,
1162 },
1163 OPT_STRVEC(0, "trailer", &options.trailer_args, N_("trailer"),
1164 N_("add custom trailer(s)")),
1165 OPT_BOOL(0, "signoff", &options.signoff,
1166 N_("add a Signed-off-by trailer to each commit")),
1167 OPT_BOOL(0, "committer-date-is-author-date",
1168 &options.committer_date_is_author_date,
1169 N_("make committer date match author date")),
1170 OPT_BOOL(0, "reset-author-date", &options.ignore_date,
1171 N_("ignore author date and use current date")),
1172 OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date,
1173 N_("synonym of --reset-author-date")),
1174 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1175 N_("passed to 'git apply'"), 0),
1176 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
1177 N_("ignore changes in whitespace")),
1178 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1179 N_("action"), N_("passed to 'git apply'"), 0),
1180 OPT_BIT('f', "force-rebase", &options.flags,
1181 N_("cherry-pick all commits, even if unchanged"),
1182 REBASE_FORCE),
1183 OPT_BIT(0, "no-ff", &options.flags,
1184 N_("cherry-pick all commits, even if unchanged"),
1185 REBASE_FORCE),
1186 OPT_CMDMODE(0, "continue", &options.action, N_("continue"),
1187 ACTION_CONTINUE),
1188 OPT_CMDMODE(0, "skip", &options.action,
1189 N_("skip current patch and continue"), ACTION_SKIP),
1190 OPT_BOOL('e', "edit", &options.edit,
1191 N_("edit the commit message")),
1192 OPT_CMDMODE(0, "abort", &options.action,
1193 N_("abort and check out the original branch"),
1194 ACTION_ABORT),
1195 OPT_CMDMODE(0, "quit", &options.action,
1196 N_("abort but keep HEAD where it is"), ACTION_QUIT),
1197 OPT_CMDMODE(0, "edit-todo", &options.action, N_("edit the todo list "
1198 "during an interactive rebase"), ACTION_EDIT_TODO),
1199 OPT_CMDMODE(0, "show-current-patch", &options.action,
1200 N_("show the patch file being applied or merged"),
1201 ACTION_SHOW_CURRENT_PATCH),
1202 OPT_CALLBACK_F(0, "apply", &options, NULL,
1203 N_("use apply strategies to rebase"),
1204 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1205 parse_opt_am),
1206 OPT_CALLBACK_F('m', "merge", &options, NULL,
1207 N_("use merging strategies to rebase"),
1208 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1209 parse_opt_merge),
1210 OPT_CALLBACK_F('i', "interactive", &options, NULL,
1211 N_("let the user edit the list of commits to rebase"),
1212 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
1213 parse_opt_interactive),
1214 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected,
1215 N_("(REMOVED) was: try to recreate merges "
1216 "instead of ignoring them"),
1217 1, PARSE_OPT_HIDDEN),
1218 OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
1219 OPT_CALLBACK_F(0, "empty", &options, "(drop|keep|stop)",
1220 N_("how to handle commits that become empty"),
1221 PARSE_OPT_NONEG, parse_opt_empty),
1222 OPT_CALLBACK_F('k', "keep-empty", &options, NULL,
1223 N_("keep commits which start empty"),
1224 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
1225 parse_opt_keep_empty),
1226 OPT_BOOL(0, "autosquash", &options.autosquash,
1227 N_("move commits that begin with "
1228 "squash!/fixup! under -i")),
1229 OPT_BOOL(0, "update-refs", &options.update_refs,
1230 N_("update branches that point to commits "
1231 "that are being rebased")),
1232 {
1233 .type = OPTION_STRING,
1234 .short_name = 'S',
1235 .long_name = "gpg-sign",
1236 .value = &gpg_sign,
1237 .argh = N_("key-id"),
1238 .help = N_("GPG-sign commits"),
1239 .flags = PARSE_OPT_OPTARG,
1240 .defval = (intptr_t) "",
1241 },
1242 OPT_AUTOSTASH(&options.autostash),
1243 OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"),
1244 N_("add exec lines after each commit of the "
1245 "editable list")),
1246 OPT_BOOL_F(0, "allow-empty-message",
1247 &options.allow_empty_message,
1248 N_("allow rebasing commits with empty messages"),
1249 PARSE_OPT_HIDDEN),
1250 OPT_CALLBACK_F('r', "rebase-merges", &options, N_("mode"),
1251 N_("try to rebase merges instead of skipping them"),
1252 PARSE_OPT_OPTARG, parse_opt_rebase_merges),
1253 OPT_BOOL(0, "fork-point", &options.fork_point,
1254 N_("use 'merge-base --fork-point' to refine upstream")),
1255 OPT_STRING('s', "strategy", &strategy_opt,
1256 N_("strategy"), N_("use the given merge strategy")),
1257 OPT_STRING_LIST('X', "strategy-option", &options.strategy_opts,
1258 N_("option"),
1259 N_("pass the argument through to the merge "
1260 "strategy")),
1261 OPT_BOOL(0, "root", &options.root,
1262 N_("rebase all reachable commits up to the root(s)")),
1263 OPT_BOOL(0, "reschedule-failed-exec",
1264 &reschedule_failed_exec,
1265 N_("automatically re-schedule any `exec` that fails")),
1266 OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks,
1267 N_("apply all changes, even those already present upstream")),
1268 OPT_END(),
1269 };
1270 int i;
1271
1272 show_usage_with_options_if_asked(argc, argv,
1273 builtin_rebase_usage,
1274 builtin_rebase_options);
1275
1276 #ifndef WITH_BREAKING_CHANGES
1277 warn_on_auto_comment_char = true;
1278 #endif /* !WITH_BREAKING_CHANGES */
1279 prepare_repo_settings(the_repository);
1280 the_repository->settings.command_requires_full_index = 0;
1281
1282 repo_config(the_repository, rebase_config, &options);
1283 /* options.gpg_sign_opt will be either "-S" or NULL */
1284 gpg_sign = options.gpg_sign_opt ? "" : NULL;
1285 FREE_AND_NULL(options.gpg_sign_opt);
1286
1287 strbuf_reset(&buf);
1288 strbuf_addf(&buf, "%s/applying", apply_dir());
1289 if(file_exists(buf.buf))
1290 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1291
1292 if (is_directory(apply_dir())) {
1293 options.type = REBASE_APPLY;
1294 options.state_dir = apply_dir();
1295 } else if (is_directory(merge_dir())) {
1296 strbuf_reset(&buf);
1297 strbuf_addf(&buf, "%s/rewritten", merge_dir());
1298 if (!(options.action == ACTION_ABORT) && is_directory(buf.buf)) {
1299 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
1300 "Use `git rebase --abort` to terminate current rebase.\n"
1301 "Or downgrade to v2.33, or earlier, to complete the rebase."));
1302 } else {
1303 strbuf_reset(&buf);
1304 strbuf_addf(&buf, "%s/interactive", merge_dir());
1305 options.type = REBASE_MERGE;
1306 if (file_exists(buf.buf))
1307 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
1308 }
1309 options.state_dir = merge_dir();
1310 }
1311
1312 if (options.type != REBASE_UNSPECIFIED)
1313 in_progress = 1;
1314
1315 total_argc = argc;
1316 argc = parse_options(argc, argv, prefix,
1317 builtin_rebase_options,
1318 builtin_rebase_usage, 0);
1319
1320 if (options.trailer_args.nr) {
1321 if (validate_trailer_args(&options.trailer_args))
1322 die(NULL);
1323 options.flags |= REBASE_FORCE;
1324 }
1325
1326 if (preserve_merges_selected)
1327 die(_("--preserve-merges was replaced by --rebase-merges\n"
1328 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
1329 "which is no longer supported; use 'merges' instead"));
1330
1331 if (options.action != ACTION_NONE && total_argc != 2) {
1332 if (options.action != ACTION_CONTINUE ||
1333 options.edit < 0 || total_argc != 3)
1334 usage_with_options(builtin_rebase_usage,
1335 builtin_rebase_options);
1336 }
1337
1338 if (options.edit >= 0 && options.action != ACTION_CONTINUE)
1339 die(_("--edit and --no-edit can only be used with --continue"));
1340
1341 if (argc > 2)
1342 usage_with_options(builtin_rebase_usage,
1343 builtin_rebase_options);
1344
1345 if (keep_base) {
1346 if (options.onto_name)
1347 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
1348 if (options.root)
1349 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
1350 /*
1351 * --keep-base defaults to --no-fork-point to keep the
1352 * base the same.
1353 */
1354 if (options.fork_point < 0)
1355 options.fork_point = 0;
1356 }
1357 if (options.root && options.fork_point > 0)
1358 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
1359
1360 if (options.action != ACTION_NONE && !in_progress)
1361 die(_("no rebase in progress"));
1362
1363 if (options.action == ACTION_EDIT_TODO && !is_merge(&options))
1364 die(_("The --edit-todo action can only be used during "
1365 "interactive rebase."));
1366
1367 if (trace2_is_enabled()) {
1368 if (is_merge(&options))
1369 trace2_cmd_mode("interactive");
1370 else if (options.exec.nr)
1371 trace2_cmd_mode("interactive-exec");
1372 else
1373 trace2_cmd_mode(action_names[options.action]);
1374 }
1375
1376 options.reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1377 options.reflog_action =
1378 xstrdup(options.reflog_action ? options.reflog_action : "rebase");
1379
1380 switch (options.action) {
1381 case ACTION_CONTINUE: {
1382 struct object_id head;
1383 struct lock_file lock_file = LOCK_INIT;
1384 int fd;
1385
1386 /* Sanity check */
1387 if (repo_get_oid(the_repository, "HEAD", &head))
1388 die(_("Cannot read HEAD"));
1389
1390 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
1391 if (repo_read_index(the_repository) < 0)
1392 die(_("could not read index"));
1393 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1394 NULL);
1395 if (0 <= fd)
1396 repo_update_index_if_able(the_repository, &lock_file);
1397 rollback_lock_file(&lock_file);
1398
1399 if (has_unstaged_changes(the_repository, 1)) {
1400 puts(_("You must edit all merge conflicts and then\n"
1401 "mark them as resolved using git add"));
1402 exit(1);
1403 }
1404 if (read_basic_state(&options))
1405 exit(1);
1406 goto run_rebase;
1407 }
1408 case ACTION_SKIP: {
1409 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1410
1411 rerere_clear(the_repository, &merge_rr);
1412 string_list_clear(&merge_rr, 1);
1413 ropts.flags = RESET_WORKING_TREE_HARD |
1414 RESET_WORKING_TREE_UPDATE_HEAD;
1415 if (reset_working_tree(the_repository, &ropts) < 0)
1416 die(_("could not discard worktree changes"));
1417 remove_branch_state(the_repository, 0);
1418 if (read_basic_state(&options))
1419 exit(1);
1420 goto run_rebase;
1421 }
1422 case ACTION_ABORT: {
1423 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1424 struct strbuf head_msg = STRBUF_INIT;
1425
1426 rerere_clear(the_repository, &merge_rr);
1427 string_list_clear(&merge_rr, 1);
1428
1429 if (read_basic_state(&options))
1430 exit(1);
1431
1432 strbuf_addf(&head_msg, "%s (abort): returning to %s",
1433 options.reflog_action,
1434 options.head_name ? options.head_name
1435 : oid_to_hex(&options.orig_head->object.oid));
1436 ropts.oid = &options.orig_head->object.oid;
1437 ropts.head_msg = head_msg.buf;
1438 ropts.branch = options.head_name;
1439 ropts.flags = RESET_WORKING_TREE_HARD |
1440 RESET_WORKING_TREE_UPDATE_HEAD;
1441 if (reset_working_tree(the_repository, &ropts) < 0)
1442 die(_("could not move back to %s"),
1443 oid_to_hex(&options.orig_head->object.oid));
1444 strbuf_release(&head_msg);
1445 remove_branch_state(the_repository, 0);
1446 ret = finish_rebase(&options);
1447 goto cleanup;
1448 }
1449 case ACTION_QUIT: {
1450 save_autostash(state_dir_path("autostash", &options));
1451 if (options.type == REBASE_MERGE) {
1452 struct replay_opts replay = REPLAY_OPTS_INIT;
1453
1454 replay.action = REPLAY_INTERACTIVE_REBASE;
1455 ret = sequencer_remove_state(&replay);
1456 replay_opts_release(&replay);
1457 } else {
1458 strbuf_reset(&buf);
1459 strbuf_addstr(&buf, options.state_dir);
1460 ret = remove_dir_recursively(&buf, 0);
1461 if (ret)
1462 error(_("could not remove '%s'"),
1463 options.state_dir);
1464 }
1465 goto cleanup;
1466 }
1467 case ACTION_EDIT_TODO:
1468 options.dont_finish_rebase = 1;
1469 goto run_rebase;
1470 case ACTION_SHOW_CURRENT_PATCH:
1471 options.dont_finish_rebase = 1;
1472 goto run_rebase;
1473 case ACTION_NONE:
1474 break;
1475 default:
1476 BUG("action: %d", options.action);
1477 }
1478
1479 /* Make sure no rebase is in progress */
1480 if (in_progress) {
1481 const char *last_slash = strrchr(options.state_dir, '/');
1482 const char *state_dir_base =
1483 last_slash ? last_slash + 1 : options.state_dir;
1484 const char *cmd_live_rebase =
1485 "git rebase (--continue | --abort | --skip)";
1486 strbuf_reset(&buf);
1487 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1488 die(_("It seems that there is already a %s directory, and\n"
1489 "I wonder if you are in the middle of another rebase. "
1490 "If that is the\n"
1491 "case, please try\n\t%s\n"
1492 "If that is not the case, please\n\t%s\n"
1493 "and run me again. I am stopping in case you still "
1494 "have something\n"
1495 "valuable there.\n"),
1496 state_dir_base, cmd_live_rebase, buf.buf);
1497 }
1498
1499 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
1500 (options.action != ACTION_NONE) ||
1501 (options.exec.nr > 0) ||
1502 options.autosquash == 1) {
1503 allow_preemptive_ff = 0;
1504 }
1505 if (options.committer_date_is_author_date || options.ignore_date)
1506 options.flags |= REBASE_FORCE;
1507
1508 for (i = 0; i < options.git_am_opts.nr; i++) {
1509 const char *option = options.git_am_opts.v[i], *p;
1510 if (!strcmp(option, "--whitespace=fix") ||
1511 !strcmp(option, "--whitespace=strip"))
1512 allow_preemptive_ff = 0;
1513 else if (skip_prefix(option, "-C", &p)) {
1514 while (*p)
1515 if (!isdigit(*(p++)))
1516 die(_("switch `C' expects a "
1517 "numerical value"));
1518 } else if (skip_prefix(option, "--whitespace=", &p)) {
1519 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1520 strcmp(p, "error") && strcmp(p, "error-all"))
1521 die("Invalid whitespace option: '%s'", p);
1522 }
1523 }
1524
1525 for (i = 0; i < options.exec.nr; i++)
1526 if (check_exec_cmd(options.exec.items[i].string))
1527 exit(1);
1528
1529 if (!(options.flags & REBASE_NO_QUIET))
1530 strvec_push(&options.git_am_opts, "-q");
1531
1532 if (options.empty != EMPTY_UNSPECIFIED)
1533 imply_merge(&options, "--empty");
1534
1535 if (options.reapply_cherry_picks < 0)
1536 /*
1537 * We default to --no-reapply-cherry-picks unless
1538 * --keep-base is given; when --keep-base is given, we want
1539 * to default to --reapply-cherry-picks.
1540 */
1541 options.reapply_cherry_picks = keep_base;
1542 else if (!keep_base)
1543 /*
1544 * The apply backend always searches for and drops cherry
1545 * picks. This is often not wanted with --keep-base, so
1546 * --keep-base allows --reapply-cherry-picks to be
1547 * simulated by altering the upstream such that
1548 * cherry-picks cannot be detected and thus all commits are
1549 * reapplied. Thus, --[no-]reapply-cherry-picks is
1550 * supported when --keep-base is specified, but not when
1551 * --keep-base is left out.
1552 */
1553 imply_merge(&options, options.reapply_cherry_picks ?
1554 "--reapply-cherry-picks" :
1555 "--no-reapply-cherry-picks");
1556
1557 if (gpg_sign)
1558 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
1559
1560 if (options.exec.nr)
1561 imply_merge(&options, "--exec");
1562
1563 if (options.type == REBASE_APPLY) {
1564 if (ignore_whitespace)
1565 strvec_push(&options.git_am_opts,
1566 "--ignore-whitespace");
1567 if (options.committer_date_is_author_date)
1568 strvec_push(&options.git_am_opts,
1569 "--committer-date-is-author-date");
1570 if (options.ignore_date)
1571 strvec_push(&options.git_am_opts, "--ignore-date");
1572 } else {
1573 /* REBASE_MERGE */
1574 if (ignore_whitespace) {
1575 string_list_append(&options.strategy_opts,
1576 "ignore-space-change");
1577 }
1578 }
1579
1580 if (strategy_opt)
1581 options.strategy = xstrdup(strategy_opt);
1582 else if (options.strategy_opts.nr && !options.strategy)
1583 options.strategy = xstrdup("ort");
1584 if (options.strategy)
1585 imply_merge(&options, "--strategy");
1586
1587 if (options.root && !options.onto_name)
1588 imply_merge(&options, "--root without --onto");
1589
1590 if (options.trailer_args.nr)
1591 imply_merge(&options, "--trailer");
1592
1593 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1594 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1595
1596 if (options.git_am_opts.nr || options.type == REBASE_APPLY) {
1597 /* all am options except -q are compatible only with --apply */
1598 for (i = options.git_am_opts.nr - 1; i >= 0; i--)
1599 if (strcmp(options.git_am_opts.v[i], "-q"))
1600 break;
1601
1602 if (i >= 0 || options.type == REBASE_APPLY) {
1603 if (is_merge(&options))
1604 die(_("apply options and merge options "
1605 "cannot be used together"));
1606 else if (options.rebase_merges == -1 && options.config_rebase_merges == 1)
1607 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges"));
1608 else if (options.update_refs == -1 && options.config_update_refs == 1)
1609 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs"));
1610 else
1611 options.type = REBASE_APPLY;
1612 }
1613 }
1614
1615 if (options.update_refs == 1)
1616 imply_merge(&options, "--update-refs");
1617 options.update_refs = (options.update_refs >= 0) ? options.update_refs :
1618 ((options.config_update_refs >= 0) ? options.config_update_refs : 0);
1619
1620 if (options.rebase_merges == 1)
1621 imply_merge(&options, "--rebase-merges");
1622 options.rebase_merges = (options.rebase_merges >= 0) ? options.rebase_merges :
1623 ((options.config_rebase_merges >= 0) ? options.config_rebase_merges : 0);
1624
1625 if (options.autosquash == 1) {
1626 imply_merge(&options, "--autosquash");
1627 } else if (options.autosquash == -1) {
1628 options.autosquash =
1629 options.config_autosquash &&
1630 (options.flags & REBASE_INTERACTIVE_EXPLICIT);
1631 }
1632
1633 if (options.type == REBASE_UNSPECIFIED) {
1634 if (!strcmp(options.default_backend, "merge"))
1635 options.type = REBASE_MERGE;
1636 else if (!strcmp(options.default_backend, "apply"))
1637 options.type = REBASE_APPLY;
1638 else
1639 die(_("Unknown rebase backend: %s"),
1640 options.default_backend);
1641 }
1642
1643 switch (options.type) {
1644 case REBASE_MERGE:
1645 options.state_dir = merge_dir();
1646 break;
1647 case REBASE_APPLY:
1648 options.state_dir = apply_dir();
1649 break;
1650 default:
1651 BUG("options.type was just set above; should be unreachable.");
1652 }
1653
1654 if (options.empty == EMPTY_UNSPECIFIED) {
1655 if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
1656 options.empty = EMPTY_STOP;
1657 else if (options.exec.nr > 0)
1658 options.empty = EMPTY_KEEP;
1659 else
1660 options.empty = EMPTY_DROP;
1661 }
1662 if (reschedule_failed_exec > 0 && !is_merge(&options))
1663 die(_("--reschedule-failed-exec requires "
1664 "--exec or --interactive"));
1665 if (reschedule_failed_exec >= 0)
1666 options.reschedule_failed_exec = reschedule_failed_exec;
1667
1668 if (options.signoff) {
1669 strvec_push(&options.git_am_opts, "--signoff");
1670 options.flags |= REBASE_FORCE;
1671 }
1672
1673 if (!options.root) {
1674 if (argc < 1) {
1675 struct branch *branch;
1676
1677 branch = branch_get(NULL);
1678 options.upstream_name = branch_get_upstream(branch,
1679 NULL);
1680 if (!options.upstream_name)
1681 error_on_missing_default_upstream();
1682 if (options.fork_point < 0)
1683 options.fork_point = 1;
1684 } else {
1685 options.upstream_name = argv[0];
1686 argc--;
1687 argv++;
1688 if (!strcmp(options.upstream_name, "-"))
1689 options.upstream_name = "@{-1}";
1690 }
1691 options.upstream =
1692 lookup_commit_reference_by_name(options.upstream_name);
1693 if (!options.upstream)
1694 die(_("invalid upstream '%s'"), options.upstream_name);
1695 options.upstream_arg = options.upstream_name;
1696 } else {
1697 if (!options.onto_name) {
1698 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1699 &squash_onto, NULL, NULL) < 0)
1700 die(_("Could not create new root commit"));
1701 options.squash_onto = &squash_onto;
1702 options.onto_name = squash_onto_name =
1703 xstrdup(oid_to_hex(&squash_onto));
1704 } else
1705 options.root_with_onto = 1;
1706
1707 options.upstream_name = NULL;
1708 options.upstream = NULL;
1709 if (argc > 1)
1710 usage_with_options(builtin_rebase_usage,
1711 builtin_rebase_options);
1712 options.upstream_arg = "--root";
1713 }
1714
1715 /*
1716 * If the branch to rebase is given, that is the branch we will rebase
1717 * branch_name -- branch/commit being rebased, or
1718 * HEAD (already detached)
1719 * orig_head -- commit object name of tip of the branch before rebasing
1720 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
1721 */
1722 if (argc == 1) {
1723 /* Is it "rebase other branchname" or "rebase other commit"? */
1724 struct object_id branch_oid;
1725 branch_name = argv[0];
1726 options.switch_to = argv[0];
1727
1728 /* Is it a local branch? */
1729 strbuf_reset(&buf);
1730 strbuf_addf(&buf, "refs/heads/%s", branch_name);
1731 if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) {
1732 die_if_checked_out(buf.buf, 1);
1733 options.head_name = xstrdup(buf.buf);
1734 options.orig_head =
1735 lookup_commit_object(the_repository,
1736 &branch_oid);
1737 /* If not is it a valid ref (branch or commit)? */
1738 } else {
1739 options.orig_head =
1740 lookup_commit_reference_by_name(branch_name);
1741 options.head_name = NULL;
1742 }
1743 if (!options.orig_head)
1744 die(_("no such branch/commit '%s'"), branch_name);
1745 } else if (argc == 0) {
1746 /* Do not need to switch branches, we are already on it. */
1747 options.head_name =
1748 xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL,
1749 &flags));
1750 if (!options.head_name)
1751 die(_("No such ref: %s"), "HEAD");
1752 if (flags & REF_ISSYMREF) {
1753 if (!skip_prefix(options.head_name,
1754 "refs/heads/", &branch_name))
1755 branch_name = options.head_name;
1756
1757 } else {
1758 FREE_AND_NULL(options.head_name);
1759 branch_name = "HEAD";
1760 }
1761 options.orig_head = lookup_commit_reference_by_name("HEAD");
1762 if (!options.orig_head)
1763 die(_("Could not resolve HEAD to a commit"));
1764 } else
1765 BUG("unexpected number of arguments left to parse");
1766
1767 /* Make sure the branch to rebase onto is valid. */
1768 if (keep_base) {
1769 strbuf_reset(&buf);
1770 strbuf_addstr(&buf, options.upstream_name);
1771 strbuf_addstr(&buf, "...");
1772 strbuf_addstr(&buf, branch_name);
1773 options.onto_name = keep_base_onto_name = xstrdup(buf.buf);
1774 } else if (!options.onto_name)
1775 options.onto_name = options.upstream_name;
1776 if (strstr(options.onto_name, "...")) {
1777 if (repo_get_oid_mb(the_repository, options.onto_name, &branch_base) < 0) {
1778 if (keep_base)
1779 die(_("'%s': need exactly one merge base with branch"),
1780 options.upstream_name);
1781 else
1782 die(_("'%s': need exactly one merge base"),
1783 options.onto_name);
1784 }
1785 options.onto = lookup_commit_or_die(&branch_base,
1786 options.onto_name);
1787 } else {
1788 options.onto =
1789 lookup_commit_reference_by_name(options.onto_name);
1790 if (!options.onto)
1791 die(_("Does not point to a valid commit '%s'"),
1792 options.onto_name);
1793 fill_branch_base(&options, &branch_base);
1794 }
1795
1796 if (keep_base && options.reapply_cherry_picks)
1797 options.upstream = options.onto;
1798
1799 if (options.fork_point > 0)
1800 options.restrict_revision =
1801 get_fork_point(options.upstream_name, options.orig_head);
1802
1803 if (repo_read_index(the_repository) < 0)
1804 die(_("could not read index"));
1805
1806 if (options.autostash)
1807 create_autostash(the_repository,
1808 state_dir_path("autostash", &options));
1809
1810
1811 if (require_clean_work_tree(the_repository, "rebase",
1812 _("Please commit or stash them."), 1, 1)) {
1813 ret = -1;
1814 goto cleanup_autostash;
1815 }
1816
1817 /*
1818 * Now we are rebasing commits upstream..orig_head (or with --root,
1819 * everything leading up to orig_head) on top of onto.
1820 */
1821
1822 /*
1823 * Check if we are already based on onto with linear history,
1824 * in which case we could fast-forward without replacing the commits
1825 * with new commits recreated by replaying their changes.
1826 */
1827 if (allow_preemptive_ff &&
1828 can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1829 options.orig_head, &branch_base)) {
1830 int flag;
1831
1832 if (!(options.flags & REBASE_FORCE)) {
1833 /* Lazily switch to the target branch if needed... */
1834 if (options.switch_to) {
1835 ret = checkout_up_to_date(&options);
1836 if (ret)
1837 goto cleanup_autostash;
1838 }
1839
1840 if (!(options.flags & REBASE_NO_QUIET))
1841 ; /* be quiet */
1842 else if (!strcmp(branch_name, "HEAD") &&
1843 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag))
1844 puts(_("HEAD is up to date."));
1845 else
1846 printf(_("Current branch %s is up to date.\n"),
1847 branch_name);
1848 ret = finish_rebase(&options);
1849 goto cleanup;
1850 } else if (!(options.flags & REBASE_NO_QUIET))
1851 ; /* be quiet */
1852 else if (!strcmp(branch_name, "HEAD") &&
1853 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag))
1854 puts(_("HEAD is up to date, rebase forced."));
1855 else
1856 printf(_("Current branch %s is up to date, rebase "
1857 "forced.\n"), branch_name);
1858 }
1859
1860 /* If a hook exists, give it a chance to interrupt*/
1861 if (!ok_to_skip_pre_rebase &&
1862 run_hooks_l(the_repository, "pre-rebase", options.upstream_arg,
1863 argc ? argv[0] : NULL, NULL)) {
1864 ret = error(_("The pre-rebase hook refused to rebase."));
1865 goto cleanup_autostash;
1866 }
1867
1868 if (options.flags & REBASE_DIFFSTAT) {
1869 struct diff_options opts;
1870
1871 if (options.flags & REBASE_VERBOSE) {
1872 if (is_null_oid(&branch_base))
1873 printf(_("Changes to %s:\n"),
1874 oid_to_hex(&options.onto->object.oid));
1875 else
1876 printf(_("Changes from %s to %s:\n"),
1877 oid_to_hex(&branch_base),
1878 oid_to_hex(&options.onto->object.oid));
1879 }
1880
1881 /* We want color (if set), but no pager */
1882 repo_diff_setup(the_repository, &opts);
1883 init_diffstat_widths(&opts);
1884 opts.output_format |=
1885 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1886 opts.detect_rename = DIFF_DETECT_RENAME;
1887 diff_setup_done(&opts);
1888 diff_tree_oid(is_null_oid(&branch_base) ?
1889 the_hash_algo->empty_tree : &branch_base,
1890 &options.onto->object.oid, "", &opts);
1891 diffcore_std(&opts);
1892 diff_flush(&opts);
1893 }
1894
1895 if (is_merge(&options))
1896 goto run_rebase;
1897
1898 /* Detach HEAD and reset the tree */
1899 if (options.flags & REBASE_NO_QUIET)
1900 printf(_("First, rewinding head to replay your work on top of "
1901 "it...\n"));
1902
1903 strbuf_addf(&msg, "%s (start): checkout %s",
1904 options.reflog_action, options.onto_name);
1905 ropts.oid = &options.onto->object.oid;
1906 ropts.orig_head = &options.orig_head->object.oid;
1907 ropts.flags = RESET_WORKING_TREE_DETACH |
1908 RESET_WORKING_TREE_UPDATE_HEAD |
1909 RESET_WORKING_TREE_UPDATE_ORIG_HEAD |
1910 RESET_WORKING_TREE_RUN_POST_CHECKOUT_HOOK;
1911 ropts.head_msg = msg.buf;
1912 ropts.default_reflog_action = options.reflog_action;
1913 if (reset_working_tree(the_repository, &ropts)) {
1914 ret = error(_("Could not detach HEAD"));
1915 goto cleanup_autostash;
1916 }
1917
1918 /*
1919 * If the onto is a proper descendant of the tip of the branch, then
1920 * we just fast-forwarded.
1921 */
1922 if (oideq(&branch_base, &options.orig_head->object.oid)) {
1923 printf(_("Fast-forwarded %s to %s.\n"),
1924 branch_name, options.onto_name);
1925 move_to_original_branch(&options);
1926 ret = finish_rebase(&options);
1927 goto cleanup;
1928 }
1929
1930 strbuf_addf(&revisions, "%s..%s",
1931 options.root ? oid_to_hex(&options.onto->object.oid) :
1932 (options.restrict_revision ?
1933 oid_to_hex(&options.restrict_revision->object.oid) :
1934 oid_to_hex(&options.upstream->object.oid)),
1935 oid_to_hex(&options.orig_head->object.oid));
1936
1937 options.revisions = revisions.buf;
1938
1939 run_rebase:
1940 ret = run_specific_rebase(&options);
1941
1942 cleanup:
1943 strbuf_release(&buf);
1944 strbuf_release(&msg);
1945 strbuf_release(&revisions);
1946 rebase_options_release(&options);
1947 free(squash_onto_name);
1948 free(keep_base_onto_name);
1949 return !!ret;
1950
1951 cleanup_autostash:
1952 ret |= !!cleanup_autostash(&options);
1953 goto cleanup;
1954 }