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