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