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