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