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