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