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