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