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