]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/rebase.c
The seventh batch
[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 253
2e5c4758 254 refs_delete_reflog(get_main_ref_store(the_repository), "REBASE_HEAD");
0609b741
PW
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 515
2e5c4758
PS
516 refs_delete_ref(get_main_ref_store(the_repository), NULL,
517 "REBASE_HEAD", NULL, REF_NO_DEREF);
518 refs_delete_ref(get_main_ref_store(the_repository), NULL,
519 "AUTO_MERGE", NULL, REF_NO_DEREF);
86ed00af 520 apply_autostash(state_dir_path("autostash", opts));
ac7f467f 521 /*
a95ce124 522 * We ignore errors in 'git maintenance run --auto', since the
ac7f467f
PK
523 * user should see them.
524 */
a95ce124 525 run_auto_maintenance(!(opts->flags & (REBASE_NO_QUIET|REBASE_VERBOSE)));
10cdb9f3 526 if (opts->type == REBASE_MERGE) {
d559f502 527 struct replay_opts replay = REPLAY_OPTS_INIT;
ac7f467f 528
d559f502
PW
529 replay.action = REPLAY_INTERACTIVE_REBASE;
530 ret = sequencer_remove_state(&replay);
9ff2f060 531 replay_opts_release(&replay);
d559f502
PW
532 } else {
533 strbuf_addstr(&dir, opts->state_dir);
534 if (remove_dir_recursively(&dir, 0))
535 ret = error(_("could not remove '%s'"),
536 opts->state_dir);
537 strbuf_release(&dir);
538 }
ac7f467f 539
d3fce47d 540 return ret;
ac7f467f
PK
541}
542
21853626
JS
543static int move_to_original_branch(struct rebase_options *opts)
544{
7700ab08 545 struct strbuf branch_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT;
6ae80861 546 struct reset_head_opts ropts = { 0 };
21853626
JS
547 int ret;
548
549 if (!opts->head_name)
550 return 0; /* nothing to move back to */
551
552 if (!opts->onto)
553 BUG("move_to_original_branch without onto");
554
be0d29d3 555 strbuf_addf(&branch_reflog, "%s (finish): %s onto %s",
0e34efb3 556 opts->reflog_action,
21853626 557 opts->head_name, oid_to_hex(&opts->onto->object.oid));
be0d29d3 558 strbuf_addf(&head_reflog, "%s (finish): returning to %s",
0e34efb3 559 opts->reflog_action, opts->head_name);
6ae80861
PW
560 ropts.branch = opts->head_name;
561 ropts.flags = RESET_HEAD_REFS_ONLY;
7700ab08 562 ropts.branch_msg = branch_reflog.buf;
6ae80861
PW
563 ropts.head_msg = head_reflog.buf;
564 ret = reset_head(the_repository, &ropts);
21853626 565
7700ab08 566 strbuf_release(&branch_reflog);
21853626
JS
567 strbuf_release(&head_reflog);
568 return ret;
569}
570
21853626
JS
571static int run_am(struct rebase_options *opts)
572{
573 struct child_process am = CHILD_PROCESS_INIT;
574 struct child_process format_patch = CHILD_PROCESS_INIT;
21853626
JS
575 int status;
576 char *rebased_patches;
577
578 am.git_cmd = 1;
22f9b7f3 579 strvec_push(&am.args, "am");
be0d29d3 580 strvec_pushf(&am.env, GIT_REFLOG_ACTION_ENVIRONMENT "=%s (pick)",
0e34efb3 581 opts->reflog_action);
9a1925b0 582 if (opts->action == ACTION_CONTINUE) {
22f9b7f3 583 strvec_push(&am.args, "--resolved");
72a8d3f0 584 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
21853626 585 if (opts->gpg_sign_opt)
22f9b7f3 586 strvec_push(&am.args, opts->gpg_sign_opt);
21853626
JS
587 status = run_command(&am);
588 if (status)
589 return status;
590
591 return move_to_original_branch(opts);
592 }
9a1925b0 593 if (opts->action == ACTION_SKIP) {
22f9b7f3 594 strvec_push(&am.args, "--skip");
72a8d3f0 595 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
21853626
JS
596 status = run_command(&am);
597 if (status)
598 return status;
599
600 return move_to_original_branch(opts);
601 }
9a1925b0 602 if (opts->action == ACTION_SHOW_CURRENT_PATCH) {
22f9b7f3 603 strvec_push(&am.args, "--show-current-patch");
21853626
JS
604 return run_command(&am);
605 }
606
21853626
JS
607 rebased_patches = xstrdup(git_path("rebased-patches"));
608 format_patch.out = open(rebased_patches,
609 O_WRONLY | O_CREAT | O_TRUNC, 0666);
610 if (format_patch.out < 0) {
611 status = error_errno(_("could not open '%s' for writing"),
612 rebased_patches);
613 free(rebased_patches);
647e870a 614 child_process_clear(&am);
21853626
JS
615 return status;
616 }
617
618 format_patch.git_cmd = 1;
22f9b7f3 619 strvec_pushl(&format_patch.args, "format-patch", "-k", "--stdout",
f6d8942b 620 "--full-index", "--cherry-pick", "--right-only",
ab895753 621 "--default-prefix", "--no-renames",
f6d8942b
JK
622 "--no-cover-letter", "--pretty=mboxrd", "--topo-order",
623 "--no-base", NULL);
21853626 624 if (opts->git_format_patch_opt.len)
22f9b7f3 625 strvec_split(&format_patch.args,
f6d8942b 626 opts->git_format_patch_opt.buf);
45184afb
RS
627 strvec_pushf(&format_patch.args, "%s...%s",
628 oid_to_hex(opts->root ?
629 /* this is now equivalent to !opts->upstream */
630 &opts->onto->object.oid :
631 &opts->upstream->object.oid),
632 oid_to_hex(&opts->orig_head->object.oid));
21853626 633 if (opts->restrict_revision)
22f9b7f3 634 strvec_pushf(&format_patch.args, "^%s",
f6d8942b 635 oid_to_hex(&opts->restrict_revision->object.oid));
21853626
JS
636
637 status = run_command(&format_patch);
638 if (status) {
6ae80861 639 struct reset_head_opts ropts = { 0 };
21853626
JS
640 unlink(rebased_patches);
641 free(rebased_patches);
647e870a 642 child_process_clear(&am);
21853626 643
f21becdd 644 ropts.oid = &opts->orig_head->object.oid;
6ae80861 645 ropts.branch = opts->head_name;
0e34efb3 646 ropts.default_reflog_action = opts->reflog_action;
6ae80861 647 reset_head(the_repository, &ropts);
21853626
JS
648 error(_("\ngit encountered an error while preparing the "
649 "patches to replay\n"
650 "these revisions:\n"
651 "\n %s\n\n"
652 "As a result, git cannot rebase them."),
653 opts->revisions);
654
21853626
JS
655 return status;
656 }
21853626
JS
657
658 am.in = open(rebased_patches, O_RDONLY);
659 if (am.in < 0) {
660 status = error_errno(_("could not open '%s' for reading"),
661 rebased_patches);
662 free(rebased_patches);
647e870a 663 child_process_clear(&am);
21853626
JS
664 return status;
665 }
666
d70a9eb6 667 strvec_pushv(&am.args, opts->git_am_opts.v);
22f9b7f3 668 strvec_push(&am.args, "--rebasing");
72a8d3f0 669 strvec_pushf(&am.args, "--resolvemsg=%s", rebase_resolvemsg);
22f9b7f3 670 strvec_push(&am.args, "--patch-format=mboxrd");
6023c928 671 if (opts->allow_rerere_autoupdate == RERERE_AUTOUPDATE)
22f9b7f3 672 strvec_push(&am.args, "--rerere-autoupdate");
6023c928 673 else if (opts->allow_rerere_autoupdate == RERERE_NOAUTOUPDATE)
22f9b7f3 674 strvec_push(&am.args, "--no-rerere-autoupdate");
21853626 675 if (opts->gpg_sign_opt)
22f9b7f3 676 strvec_push(&am.args, opts->gpg_sign_opt);
21853626
JS
677 status = run_command(&am);
678 unlink(rebased_patches);
679 free(rebased_patches);
680
681 if (!status) {
682 return move_to_original_branch(opts);
683 }
684
685 if (is_directory(opts->state_dir))
28dc09de 686 rebase_write_basic_state(opts);
21853626
JS
687
688 return status;
689}
690
9a1925b0 691static int run_specific_rebase(struct rebase_options *opts)
ac7f467f 692{
ac7f467f 693 int status;
ac7f467f 694
10cdb9f3
EN
695 if (opts->type == REBASE_MERGE) {
696 /* Run sequencer-based rebase */
297be594 697 if (!(opts->flags & REBASE_INTERACTIVE_EXPLICIT))
460bc3ce 698 setenv("GIT_SEQUENCE_EDITOR", ":", 1);
460bc3ce
PW
699 if (opts->gpg_sign_opt) {
700 /* remove the leading "-S" */
701 char *tmp = xstrdup(opts->gpg_sign_opt + 2);
702 free(opts->gpg_sign_opt);
703 opts->gpg_sign_opt = tmp;
704 }
bc24382c 705
9a1925b0 706 status = run_sequencer_rebase(opts);
a74b3508 707 } else if (opts->type == REBASE_APPLY)
21853626 708 status = run_am(opts);
a74b3508 709 else
ac7f467f 710 BUG("Unhandled rebase type %d", opts->type);
ac7f467f 711
ac7f467f
PK
712 if (opts->dont_finish_rebase)
713 ; /* do nothing */
10cdb9f3
EN
714 else if (opts->type == REBASE_MERGE)
715 ; /* merge backend cleans up after itself */
ac7f467f
PK
716 else if (status == 0) {
717 if (!file_exists(state_dir_path("stopped-sha", opts)))
718 finish_rebase(opts);
719 } else if (status == 2) {
720 struct strbuf dir = STRBUF_INIT;
721
86ed00af 722 apply_autostash(state_dir_path("autostash", opts));
ac7f467f
PK
723 strbuf_addstr(&dir, opts->state_dir);
724 remove_dir_recursively(&dir, 0);
725 strbuf_release(&dir);
726 die("Nothing to do");
727 }
728
ac7f467f
PK
729 return status ? -1 : 0;
730}
731
6605fb70
AH
732static void parse_rebase_merges_value(struct rebase_options *options, const char *value)
733{
734 if (!strcmp("no-rebase-cousins", value))
735 options->rebase_cousins = 0;
736 else if (!strcmp("rebase-cousins", value))
737 options->rebase_cousins = 1;
738 else
739 die(_("Unknown rebase-merges mode: %s"), value);
740}
741
a4e7e317
GC
742static int rebase_config(const char *var, const char *value,
743 const struct config_context *ctx, void *data)
bff014da
PK
744{
745 struct rebase_options *opts = data;
746
747 if (!strcmp(var, "rebase.stat")) {
748 if (git_config_bool(var, value))
749 opts->flags |= REBASE_DIFFSTAT;
750 else
4c785c0e 751 opts->flags &= ~REBASE_DIFFSTAT;
bff014da
PK
752 return 0;
753 }
754
051910a9 755 if (!strcmp(var, "rebase.autosquash")) {
eddfcd8e 756 opts->config_autosquash = git_config_bool(var, value);
051910a9
PK
757 return 0;
758 }
759
12026a41
PK
760 if (!strcmp(var, "commit.gpgsign")) {
761 free(opts->gpg_sign_opt);
762 opts->gpg_sign_opt = git_config_bool(var, value) ?
763 xstrdup("-S") : NULL;
764 return 0;
765 }
766
6defce2b
PK
767 if (!strcmp(var, "rebase.autostash")) {
768 opts->autostash = git_config_bool(var, value);
769 return 0;
770 }
771
6605fb70
AH
772 if (!strcmp(var, "rebase.rebasemerges")) {
773 opts->config_rebase_merges = git_parse_maybe_bool(value);
774 if (opts->config_rebase_merges < 0) {
775 opts->config_rebase_merges = 1;
776 parse_rebase_merges_value(opts, value);
777 } else {
778 opts->rebase_cousins = 0;
779 }
780 return 0;
781 }
782
3113feda 783 if (!strcmp(var, "rebase.updaterefs")) {
eddfcd8e 784 opts->config_update_refs = git_config_bool(var, value);
3113feda
DS
785 return 0;
786 }
787
969de3ff
JS
788 if (!strcmp(var, "rebase.reschedulefailedexec")) {
789 opts->reschedule_failed_exec = git_config_bool(var, value);
790 return 0;
791 }
792
2803d800
AH
793 if (!strcmp(var, "rebase.forkpoint")) {
794 opts->fork_point = git_config_bool(var, value) ? -1 : 0;
795 return 0;
796 }
797
8295ed69
EN
798 if (!strcmp(var, "rebase.backend")) {
799 return git_config_string(&opts->default_backend, var, value);
800 }
801
a4e7e317 802 return git_default_config(var, value, ctx, data);
bff014da
PK
803}
804
ae42fa4c
PW
805static int checkout_up_to_date(struct rebase_options *options)
806{
807 struct strbuf buf = STRBUF_INIT;
6ae80861 808 struct reset_head_opts ropts = { 0 };
ae42fa4c
PW
809 int ret = 0;
810
811 strbuf_addf(&buf, "%s: checkout %s",
0e34efb3 812 options->reflog_action, options->switch_to);
f21becdd 813 ropts.oid = &options->orig_head->object.oid;
6ae80861
PW
814 ropts.branch = options->head_name;
815 ropts.flags = RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
bdff97a3
JC
816 if (!ropts.branch)
817 ropts.flags |= RESET_HEAD_DETACH;
6ae80861
PW
818 ropts.head_msg = buf.buf;
819 if (reset_head(the_repository, &ropts) < 0)
ae42fa4c
PW
820 ret = error(_("could not switch to %s"), options->switch_to);
821 strbuf_release(&buf);
822
823 return ret;
824}
825
9a48a615
PK
826/*
827 * Determines whether the commits in from..to are linear, i.e. contain
828 * no merge commits. This function *expects* `from` to be an ancestor of
829 * `to`.
830 */
831static int is_linear_history(struct commit *from, struct commit *to)
832{
833 while (to && to != from) {
ecb5091f 834 repo_parse_commit(the_repository, to);
9a48a615
PK
835 if (!to->parents)
836 return 1;
837 if (to->parents->next)
838 return 0;
839 to = to->parents->item;
840 }
841 return 1;
842}
843
c0efb4c1 844static int can_fast_forward(struct commit *onto, struct commit *upstream,
4effc5bc 845 struct commit *restrict_revision,
a7706021 846 struct commit *head, struct object_id *branch_base)
9a48a615 847{
2b318aa6
DL
848 struct commit_list *merge_bases = NULL;
849 int res = 0;
9a48a615 850
d42c9ffa
PW
851 if (is_null_oid(branch_base))
852 goto done; /* fill_branch_base() found multiple merge bases */
9a48a615 853
a7706021 854 if (!oideq(branch_base, &onto->object.oid))
2b318aa6 855 goto done;
2b318aa6 856
a7706021 857 if (restrict_revision && !oideq(&restrict_revision->object.oid, branch_base))
4effc5bc
DL
858 goto done;
859
c0efb4c1
DL
860 if (!upstream)
861 goto done;
862
76e2a099
JS
863 if (repo_get_merge_bases(the_repository, upstream, head, &merge_bases) < 0)
864 exit(128);
c0efb4c1
DL
865 if (!merge_bases || merge_bases->next)
866 goto done;
867
868 if (!oideq(&onto->object.oid, &merge_bases->item->object.oid))
869 goto done;
870
2b318aa6
DL
871 res = 1;
872
873done:
9a48a615
PK
874 free_commit_list(merge_bases);
875 return res && is_linear_history(onto, head);
876}
877
d42c9ffa
PW
878static void fill_branch_base(struct rebase_options *options,
879 struct object_id *branch_base)
880{
881 struct commit_list *merge_bases = NULL;
882
76e2a099
JS
883 if (repo_get_merge_bases(the_repository, options->onto,
884 options->orig_head, &merge_bases) < 0)
885 exit(128);
d42c9ffa
PW
886 if (!merge_bases || merge_bases->next)
887 oidcpy(branch_base, null_oid());
888 else
889 oidcpy(branch_base, &merge_bases->item->object.oid);
890
891 free_commit_list(merge_bases);
892}
893
52eb738d
EN
894static int parse_opt_am(const struct option *opt, const char *arg, int unset)
895{
896 struct rebase_options *opts = opt->value;
897
898 BUG_ON_OPT_NEG(unset);
899 BUG_ON_OPT_ARG(arg);
900
7d718c55
EN
901 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_APPLY)
902 die(_("apply options and merge options cannot be used together"));
903
10cdb9f3 904 opts->type = REBASE_APPLY;
52eb738d
EN
905
906 return 0;
907}
908
361badd3
PK
909/* -i followed by -m is still -i */
910static int parse_opt_merge(const struct option *opt, const char *arg, int unset)
911{
912 struct rebase_options *opts = opt->value;
913
517fe807
JK
914 BUG_ON_OPT_NEG(unset);
915 BUG_ON_OPT_ARG(arg);
916
7d718c55
EN
917 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE)
918 die(_("apply options and merge options cannot be used together"));
919
920 opts->type = REBASE_MERGE;
361badd3
PK
921
922 return 0;
923}
924
82db1f84 925/* -i followed by -r is still explicitly interactive, but -r alone is not */
361badd3
PK
926static int parse_opt_interactive(const struct option *opt, const char *arg,
927 int unset)
928{
929 struct rebase_options *opts = opt->value;
930
517fe807
JK
931 BUG_ON_OPT_NEG(unset);
932 BUG_ON_OPT_ARG(arg);
933
7d718c55
EN
934 if (opts->type != REBASE_UNSPECIFIED && opts->type != REBASE_MERGE)
935 die(_("apply options and merge options cannot be used together"));
936
10cdb9f3 937 opts->type = REBASE_MERGE;
361badd3
PK
938 opts->flags |= REBASE_INTERACTIVE_EXPLICIT;
939
940 return 0;
941}
942
e98c4269
EN
943static enum empty_type parse_empty_value(const char *value)
944{
945 if (!strcasecmp(value, "drop"))
946 return EMPTY_DROP;
947 else if (!strcasecmp(value, "keep"))
948 return EMPTY_KEEP;
c282eba2
BL
949 else if (!strcasecmp(value, "stop"))
950 return EMPTY_STOP;
951 else if (!strcasecmp(value, "ask")) {
952 warning(_("--empty=ask is deprecated; use '--empty=stop' instead."));
953 return EMPTY_STOP;
954 }
e98c4269 955
c282eba2 956 die(_("unrecognized empty type '%s'; valid values are \"drop\", \"keep\", and \"stop\"."), value);
e98c4269
EN
957}
958
96db1735
OB
959static int parse_opt_keep_empty(const struct option *opt, const char *arg,
960 int unset)
961{
962 struct rebase_options *opts = opt->value;
963
964 BUG_ON_OPT_ARG(arg);
965
966 imply_merge(opts, unset ? "--no-keep-empty" : "--keep-empty");
967 opts->keep_empty = !unset;
968 return 0;
969}
970
e98c4269
EN
971static int parse_opt_empty(const struct option *opt, const char *arg, int unset)
972{
973 struct rebase_options *options = opt->value;
974 enum empty_type value = parse_empty_value(arg);
975
976 BUG_ON_OPT_NEG(unset);
977
978 options->empty = value;
979 return 0;
980}
981
6605fb70
AH
982static int parse_opt_rebase_merges(const struct option *opt, const char *arg, int unset)
983{
984 struct rebase_options *options = opt->value;
985
986 options->rebase_merges = !unset;
987 options->rebase_cousins = 0;
988
989 if (arg) {
990 if (!*arg) {
991 warning(_("--rebase-merges with an empty string "
992 "argument is deprecated and will stop "
993 "working in a future version of Git. Use "
994 "--rebase-merges without an argument "
995 "instead, which does the same thing."));
996 return 0;
997 }
998 parse_rebase_merges_value(options, arg);
999 }
1000
1001 return 0;
1002}
1003
8f5986d9
PK
1004static void NORETURN error_on_missing_default_upstream(void)
1005{
1006 struct branch *current_branch = branch_get(NULL);
1007
1008 printf(_("%s\n"
1009 "Please specify which branch you want to rebase against.\n"
1010 "See git-rebase(1) for details.\n"
1011 "\n"
1012 " git rebase '<branch>'\n"
1013 "\n"),
1014 current_branch ? _("There is no tracking information for "
1015 "the current branch.") :
1016 _("You are not currently on a branch."));
1017
1018 if (current_branch) {
1019 const char *remote = current_branch->remote_name;
1020
1021 if (!remote)
1022 remote = _("<remote>");
1023
1024 printf(_("If you wish to set tracking information for this "
1025 "branch you can do so with:\n"
1026 "\n"
1027 " git branch --set-upstream-to=%s/<branch> %s\n"
1028 "\n"),
1029 remote, current_branch->name);
1030 }
1031 exit(1);
1032}
1033
c762aada
PW
1034static int check_exec_cmd(const char *cmd)
1035{
1036 if (strchr(cmd, '\n'))
1037 return error(_("exec commands cannot contain newlines"));
1038
1039 /* Does the command consist purely of whitespace? */
1040 if (!cmd[strspn(cmd, " \t\r\f\v")])
1041 return error(_("empty exec command"));
1042
1043 return 0;
1044}
1045
55071ea2
PK
1046int cmd_rebase(int argc, const char **argv, const char *prefix)
1047{
73fdc535 1048 struct rebase_options options = REBASE_OPTIONS_INIT;
ac7f467f 1049 const char *branch_name;
f9573628 1050 int ret, flags, total_argc, in_progress = 0;
414d924b 1051 int keep_base = 0;
06e4775a 1052 int ok_to_skip_pre_rebase = 0;
ac7f467f
PK
1053 struct strbuf msg = STRBUF_INIT;
1054 struct strbuf revisions = STRBUF_INIT;
c54dacb5 1055 struct strbuf buf = STRBUF_INIT;
a7706021 1056 struct object_id branch_base;
ef484add 1057 int ignore_whitespace = 0;
12026a41 1058 const char *gpg_sign = NULL;
9dba809a
PK
1059 struct object_id squash_onto;
1060 char *squash_onto_name = NULL;
94ad545d 1061 char *keep_base_onto_name = NULL;
906b6394 1062 int reschedule_failed_exec = -1;
befb89ce 1063 int allow_preemptive_ff = 1;
a74b3508 1064 int preserve_merges_selected = 0;
6ae80861 1065 struct reset_head_opts ropts = { 0 };
f28d40d3
PK
1066 struct option builtin_rebase_options[] = {
1067 OPT_STRING(0, "onto", &options.onto_name,
1068 N_("revision"),
1069 N_("rebase onto given branch instead of upstream")),
414d924b
DL
1070 OPT_BOOL(0, "keep-base", &keep_base,
1071 N_("use the merge-base of upstream and branch as the current base")),
06e4775a
PK
1072 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
1073 N_("allow pre-rebase hook to run")),
b4c8eb02
PK
1074 OPT_NEGBIT('q', "quiet", &options.flags,
1075 N_("be quiet. implies --no-stat"),
55d2b6d7 1076 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
bff014da
PK
1077 OPT_BIT('v', "verbose", &options.flags,
1078 N_("display a diffstat of what changed upstream"),
1079 REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT),
1080 {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL,
1081 N_("do not show diffstat of what changed upstream"),
1082 PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT },
73d51ed0 1083 OPT_BOOL(0, "signoff", &options.signoff,
3abd4a67 1084 N_("add a Signed-off-by trailer to each commit")),
7573cec5
PW
1085 OPT_BOOL(0, "committer-date-is-author-date",
1086 &options.committer_date_is_author_date,
1087 N_("make committer date match author date")),
27126692 1088 OPT_BOOL(0, "reset-author-date", &options.ignore_date,
a3894aad 1089 N_("ignore author date and use current date")),
27126692
RA
1090 OPT_HIDDEN_BOOL(0, "ignore-date", &options.ignore_date,
1091 N_("synonym of --reset-author-date")),
f5769680
JS
1092 OPT_PASSTHRU_ARGV('C', NULL, &options.git_am_opts, N_("n"),
1093 N_("passed to 'git apply'"), 0),
ef484add
RA
1094 OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace,
1095 N_("ignore changes in whitespace")),
f5769680
JS
1096 OPT_PASSTHRU_ARGV(0, "whitespace", &options.git_am_opts,
1097 N_("action"), N_("passed to 'git apply'"), 0),
1ed9c14f
PK
1098 OPT_BIT('f', "force-rebase", &options.flags,
1099 N_("cherry-pick all commits, even if unchanged"),
1100 REBASE_FORCE),
1101 OPT_BIT(0, "no-ff", &options.flags,
1102 N_("cherry-pick all commits, even if unchanged"),
1103 REBASE_FORCE),
9a1925b0 1104 OPT_CMDMODE(0, "continue", &options.action, N_("continue"),
f9573628 1105 ACTION_CONTINUE),
9a1925b0 1106 OPT_CMDMODE(0, "skip", &options.action,
122420c2 1107 N_("skip current patch and continue"), ACTION_SKIP),
9a1925b0 1108 OPT_CMDMODE(0, "abort", &options.action,
5e5d9619
PK
1109 N_("abort and check out the original branch"),
1110 ACTION_ABORT),
9a1925b0 1111 OPT_CMDMODE(0, "quit", &options.action,
5a614945 1112 N_("abort but keep HEAD where it is"), ACTION_QUIT),
9a1925b0 1113 OPT_CMDMODE(0, "edit-todo", &options.action, N_("edit the todo list "
51e9ea6d 1114 "during an interactive rebase"), ACTION_EDIT_TODO),
9a1925b0 1115 OPT_CMDMODE(0, "show-current-patch", &options.action,
51e9ea6d
PK
1116 N_("show the patch file being applied or merged"),
1117 ACTION_SHOW_CURRENT_PATCH),
203c8533 1118 OPT_CALLBACK_F(0, "apply", &options, NULL,
10cdb9f3 1119 N_("use apply strategies to rebase"),
52eb738d 1120 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533
DL
1121 parse_opt_am),
1122 OPT_CALLBACK_F('m', "merge", &options, NULL,
361badd3
PK
1123 N_("use merging strategies to rebase"),
1124 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533
DL
1125 parse_opt_merge),
1126 OPT_CALLBACK_F('i', "interactive", &options, NULL,
361badd3
PK
1127 N_("let the user edit the list of commits to rebase"),
1128 PARSE_OPT_NOARG | PARSE_OPT_NONEG,
203c8533 1129 parse_opt_interactive),
a74b3508 1130 OPT_SET_INT_F('p', "preserve-merges", &preserve_merges_selected,
2f7b9f9e
PO
1131 N_("(REMOVED) was: try to recreate merges "
1132 "instead of ignoring them"),
a74b3508 1133 1, PARSE_OPT_HIDDEN),
6023c928 1134 OPT_RERERE_AUTOUPDATE(&options.allow_rerere_autoupdate),
c282eba2 1135 OPT_CALLBACK_F(0, "empty", &options, "(drop|keep|stop)",
e98c4269
EN
1136 N_("how to handle commits that become empty"),
1137 PARSE_OPT_NONEG, parse_opt_empty),
203c8533 1138 OPT_CALLBACK_F('k', "keep-empty", &options, NULL,
b9cbd295 1139 N_("keep commits which start empty"),
d48e5e21 1140 PARSE_OPT_NOARG | PARSE_OPT_HIDDEN,
203c8533 1141 parse_opt_keep_empty),
051910a9
PK
1142 OPT_BOOL(0, "autosquash", &options.autosquash,
1143 N_("move commits that begin with "
1144 "squash!/fixup! under -i")),
900b50c2
DS
1145 OPT_BOOL(0, "update-refs", &options.update_refs,
1146 N_("update branches that point to commits "
1147 "that are being rebased")),
12026a41
PK
1148 { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"),
1149 N_("GPG-sign commits"),
1150 PARSE_OPT_OPTARG, NULL, (intptr_t) "" },
a03b5553 1151 OPT_AUTOSTASH(&options.autostash),
e57d2c59 1152 OPT_STRING_LIST('x', "exec", &options.exec, N_("exec"),
68e46d78
PK
1153 N_("add exec lines after each commit of the "
1154 "editable list")),
22a69fda
EN
1155 OPT_BOOL_F(0, "allow-empty-message",
1156 &options.allow_empty_message,
1157 N_("allow rebasing commits with empty messages"),
1158 PARSE_OPT_HIDDEN),
6605fb70 1159 OPT_CALLBACK_F('r', "rebase-merges", &options, N_("mode"),
3c3588c7 1160 N_("try to rebase merges instead of skipping them"),
6605fb70 1161 PARSE_OPT_OPTARG, parse_opt_rebase_merges),
2803d800 1162 OPT_BOOL(0, "fork-point", &options.fork_point,
92d0d74e 1163 N_("use 'merge-base --fork-point' to refine upstream")),
ba1905a5
PK
1164 OPT_STRING('s', "strategy", &options.strategy,
1165 N_("strategy"), N_("use the given merge strategy")),
4a8bc986 1166 OPT_STRING_LIST('X', "strategy-option", &options.strategy_opts,
ba1905a5
PK
1167 N_("option"),
1168 N_("pass the argument through to the merge "
1169 "strategy")),
9dba809a
PK
1170 OPT_BOOL(0, "root", &options.root,
1171 N_("rebase all reachable commits up to the root(s)")),
d421afa0 1172 OPT_BOOL(0, "reschedule-failed-exec",
906b6394 1173 &reschedule_failed_exec,
d421afa0 1174 N_("automatically re-schedule any `exec` that fails")),
0fcb4f6b
JT
1175 OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks,
1176 N_("apply all changes, even those already present upstream")),
f28d40d3
PK
1177 OPT_END(),
1178 };
f5769680 1179 int i;
ac7f467f 1180
f28d40d3
PK
1181 if (argc == 2 && !strcmp(argv[1], "-h"))
1182 usage_with_options(builtin_rebase_usage,
1183 builtin_rebase_options);
1184
516680ba
DS
1185 prepare_repo_settings(the_repository);
1186 the_repository->settings.command_requires_full_index = 0;
1187
bff014da 1188 git_config(rebase_config, &options);
c241371c
ĐTCD
1189 /* options.gpg_sign_opt will be either "-S" or NULL */
1190 gpg_sign = options.gpg_sign_opt ? "" : NULL;
1191 FREE_AND_NULL(options.gpg_sign_opt);
bff014da 1192
0eabf4b9
PK
1193 strbuf_reset(&buf);
1194 strbuf_addf(&buf, "%s/applying", apply_dir());
1195 if(file_exists(buf.buf))
1196 die(_("It looks like 'git am' is in progress. Cannot rebase."));
1197
c54dacb5 1198 if (is_directory(apply_dir())) {
10cdb9f3 1199 options.type = REBASE_APPLY;
c54dacb5
PK
1200 options.state_dir = apply_dir();
1201 } else if (is_directory(merge_dir())) {
1202 strbuf_reset(&buf);
1203 strbuf_addf(&buf, "%s/rewritten", merge_dir());
9a1925b0 1204 if (!(options.action == ACTION_ABORT) && is_directory(buf.buf)) {
f007713c 1205 die(_("`rebase --preserve-merges` (-p) is no longer supported.\n"
afd58a0d 1206 "Use `git rebase --abort` to terminate current rebase.\n"
f007713c 1207 "Or downgrade to v2.33, or earlier, to complete the rebase."));
c54dacb5
PK
1208 } else {
1209 strbuf_reset(&buf);
1210 strbuf_addf(&buf, "%s/interactive", merge_dir());
52e1ab8a
ECA
1211 options.type = REBASE_MERGE;
1212 if (file_exists(buf.buf))
c54dacb5 1213 options.flags |= REBASE_INTERACTIVE_EXPLICIT;
c54dacb5
PK
1214 }
1215 options.state_dir = merge_dir();
1216 }
1217
1218 if (options.type != REBASE_UNSPECIFIED)
1219 in_progress = 1;
1220
f9573628 1221 total_argc = argc;
f28d40d3
PK
1222 argc = parse_options(argc, argv, prefix,
1223 builtin_rebase_options,
1224 builtin_rebase_usage, 0);
1225
a74b3508 1226 if (preserve_merges_selected)
afea77a7 1227 die(_("--preserve-merges was replaced by --rebase-merges\n"
3b9a5a33 1228 "Note: Your `pull.rebase` configuration may also be set to 'preserve',\n"
afea77a7 1229 "which is no longer supported; use 'merges' instead"));
a74b3508 1230
9a1925b0 1231 if (options.action != ACTION_NONE && total_argc != 2) {
f9573628
PK
1232 usage_with_options(builtin_rebase_usage,
1233 builtin_rebase_options);
1234 }
1235
f28d40d3
PK
1236 if (argc > 2)
1237 usage_with_options(builtin_rebase_usage,
1238 builtin_rebase_options);
ac7f467f 1239
414d924b
DL
1240 if (keep_base) {
1241 if (options.onto_name)
12909b6b 1242 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--onto");
414d924b 1243 if (options.root)
12909b6b 1244 die(_("options '%s' and '%s' cannot be used together"), "--keep-base", "--root");
aa1df814
PW
1245 /*
1246 * --keep-base defaults to --no-fork-point to keep the
1247 * base the same.
1248 */
1249 if (options.fork_point < 0)
1250 options.fork_point = 0;
414d924b 1251 }
2803d800 1252 if (options.root && options.fork_point > 0)
12909b6b 1253 die(_("options '%s' and '%s' cannot be used together"), "--root", "--fork-point");
a35413c3 1254
9a1925b0 1255 if (options.action != ACTION_NONE && !in_progress)
244001aa 1256 die(_("no rebase in progress"));
d732a570 1257
9a1925b0 1258 if (options.action == ACTION_EDIT_TODO && !is_merge(&options))
51e9ea6d
PK
1259 die(_("The --edit-todo action can only be used during "
1260 "interactive rebase."));
1261
b3a5d5a8 1262 if (trace2_is_enabled()) {
10cdb9f3 1263 if (is_merge(&options))
b3a5d5a8 1264 trace2_cmd_mode("interactive");
e57d2c59 1265 else if (options.exec.nr)
b3a5d5a8
JH
1266 trace2_cmd_mode("interactive-exec");
1267 else
9a1925b0 1268 trace2_cmd_mode(action_names[options.action]);
b3a5d5a8
JH
1269 }
1270
0e34efb3
PW
1271 options.reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
1272 options.reflog_action =
1273 xstrdup(options.reflog_action ? options.reflog_action : "rebase");
1274
9a1925b0 1275 switch (options.action) {
f9573628
PK
1276 case ACTION_CONTINUE: {
1277 struct object_id head;
1278 struct lock_file lock_file = LOCK_INIT;
1279 int fd;
1280
f9573628 1281 /* Sanity check */
d850b7a5 1282 if (repo_get_oid(the_repository, "HEAD", &head))
f9573628
PK
1283 die(_("Cannot read HEAD"));
1284
07047d68 1285 fd = repo_hold_locked_index(the_repository, &lock_file, 0);
e1ff0a32 1286 if (repo_read_index(the_repository) < 0)
f9573628
PK
1287 die(_("could not read index"));
1288 refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL,
1289 NULL);
1290 if (0 <= fd)
1b0d968b 1291 repo_update_index_if_able(the_repository, &lock_file);
f9573628
PK
1292 rollback_lock_file(&lock_file);
1293
5b02ca38 1294 if (has_unstaged_changes(the_repository, 1)) {
f9573628
PK
1295 puts(_("You must edit all merge conflicts and then\n"
1296 "mark them as resolved using git add"));
1297 exit(1);
1298 }
1299 if (read_basic_state(&options))
1300 exit(1);
1301 goto run_rebase;
1302 }
122420c2
PK
1303 case ACTION_SKIP: {
1304 struct string_list merge_rr = STRING_LIST_INIT_DUP;
1305
55e6b354 1306 rerere_clear(the_repository, &merge_rr);
122420c2 1307 string_list_clear(&merge_rr, 1);
6ae80861
PW
1308 ropts.flags = RESET_HEAD_HARD;
1309 if (reset_head(the_repository, &ropts) < 0)
122420c2 1310 die(_("could not discard worktree changes"));
f4a4b9ac 1311 remove_branch_state(the_repository, 0);
122420c2
PK
1312 if (read_basic_state(&options))
1313 exit(1);
1314 goto run_rebase;
1315 }
5e5d9619
PK
1316 case ACTION_ABORT: {
1317 struct string_list merge_rr = STRING_LIST_INIT_DUP;
6159e7ad 1318 struct strbuf head_msg = STRBUF_INIT;
5e5d9619 1319
55e6b354 1320 rerere_clear(the_repository, &merge_rr);
5e5d9619
PK
1321 string_list_clear(&merge_rr, 1);
1322
1323 if (read_basic_state(&options))
1324 exit(1);
6159e7ad
PW
1325
1326 strbuf_addf(&head_msg, "%s (abort): returning to %s",
0e34efb3 1327 options.reflog_action,
6159e7ad
PW
1328 options.head_name ? options.head_name
1329 : oid_to_hex(&options.orig_head->object.oid));
f21becdd 1330 ropts.oid = &options.orig_head->object.oid;
6159e7ad 1331 ropts.head_msg = head_msg.buf;
6ae80861
PW
1332 ropts.branch = options.head_name;
1333 ropts.flags = RESET_HEAD_HARD;
6ae80861 1334 if (reset_head(the_repository, &ropts) < 0)
5e5d9619 1335 die(_("could not move back to %s"),
f21becdd 1336 oid_to_hex(&options.orig_head->object.oid));
5ff6e8af 1337 strbuf_release(&head_msg);
f4a4b9ac 1338 remove_branch_state(the_repository, 0);
35f070b4 1339 ret = finish_rebase(&options);
5e5d9619
PK
1340 goto cleanup;
1341 }
5a614945 1342 case ACTION_QUIT: {
9b2df3e8 1343 save_autostash(state_dir_path("autostash", &options));
10cdb9f3 1344 if (options.type == REBASE_MERGE) {
d559f502
PW
1345 struct replay_opts replay = REPLAY_OPTS_INIT;
1346
1347 replay.action = REPLAY_INTERACTIVE_REBASE;
35f070b4 1348 ret = sequencer_remove_state(&replay);
9ff2f060 1349 replay_opts_release(&replay);
d559f502
PW
1350 } else {
1351 strbuf_reset(&buf);
1352 strbuf_addstr(&buf, options.state_dir);
35f070b4 1353 ret = remove_dir_recursively(&buf, 0);
d559f502
PW
1354 if (ret)
1355 error(_("could not remove '%s'"),
1356 options.state_dir);
1357 }
5a614945
PK
1358 goto cleanup;
1359 }
51e9ea6d 1360 case ACTION_EDIT_TODO:
51e9ea6d
PK
1361 options.dont_finish_rebase = 1;
1362 goto run_rebase;
1363 case ACTION_SHOW_CURRENT_PATCH:
51e9ea6d
PK
1364 options.dont_finish_rebase = 1;
1365 goto run_rebase;
297b1e17 1366 case ACTION_NONE:
51e9ea6d 1367 break;
f9573628 1368 default:
9a1925b0 1369 BUG("action: %d", options.action);
f9573628
PK
1370 }
1371
c54dacb5
PK
1372 /* Make sure no rebase is in progress */
1373 if (in_progress) {
1374 const char *last_slash = strrchr(options.state_dir, '/');
1375 const char *state_dir_base =
1376 last_slash ? last_slash + 1 : options.state_dir;
1377 const char *cmd_live_rebase =
1378 "git rebase (--continue | --abort | --skip)";
1379 strbuf_reset(&buf);
1380 strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir);
1381 die(_("It seems that there is already a %s directory, and\n"
1382 "I wonder if you are in the middle of another rebase. "
1383 "If that is the\n"
1384 "case, please try\n\t%s\n"
1385 "If that is not the case, please\n\t%s\n"
1386 "and run me again. I am stopping in case you still "
1387 "have something\n"
1388 "valuable there.\n"),
1389 state_dir_base, cmd_live_rebase, buf.buf);
1390 }
1391
befb89ce 1392 if ((options.flags & REBASE_INTERACTIVE_EXPLICIT) ||
9a1925b0 1393 (options.action != ACTION_NONE) ||
e57d2c59 1394 (options.exec.nr > 0) ||
eddfcd8e 1395 options.autosquash == 1) {
befb89ce
EN
1396 allow_preemptive_ff = 0;
1397 }
a3894aad 1398 if (options.committer_date_is_author_date || options.ignore_date)
7573cec5 1399 options.flags |= REBASE_FORCE;
befb89ce 1400
d70a9eb6
JK
1401 for (i = 0; i < options.git_am_opts.nr; i++) {
1402 const char *option = options.git_am_opts.v[i], *p;
a3894aad 1403 if (!strcmp(option, "--whitespace=fix") ||
f5769680 1404 !strcmp(option, "--whitespace=strip"))
befb89ce 1405 allow_preemptive_ff = 0;
04519d72
JS
1406 else if (skip_prefix(option, "-C", &p)) {
1407 while (*p)
1408 if (!isdigit(*(p++)))
1409 die(_("switch `C' expects a "
1410 "numerical value"));
1411 } else if (skip_prefix(option, "--whitespace=", &p)) {
1412 if (*p && strcmp(p, "warn") && strcmp(p, "nowarn") &&
1413 strcmp(p, "error") && strcmp(p, "error-all"))
1414 die("Invalid whitespace option: '%s'", p);
1415 }
38dbcef2
PK
1416 }
1417
e57d2c59
PW
1418 for (i = 0; i < options.exec.nr; i++)
1419 if (check_exec_cmd(options.exec.items[i].string))
c762aada
PW
1420 exit(1);
1421
f5769680 1422 if (!(options.flags & REBASE_NO_QUIET))
22f9b7f3 1423 strvec_push(&options.git_am_opts, "-q");
53f9e5be 1424
e98c4269 1425 if (options.empty != EMPTY_UNSPECIFIED)
10cdb9f3 1426 imply_merge(&options, "--empty");
002ee2fe 1427
ffeaca17
EN
1428 if (options.reapply_cherry_picks < 0)
1429 /*
1430 * We default to --no-reapply-cherry-picks unless
1431 * --keep-base is given; when --keep-base is given, we want
1432 * to default to --reapply-cherry-picks.
1433 */
1434 options.reapply_cherry_picks = keep_base;
1435 else if (!keep_base)
1436 /*
1437 * The apply backend always searches for and drops cherry
1438 * picks. This is often not wanted with --keep-base, so
1439 * --keep-base allows --reapply-cherry-picks to be
1440 * simulated by altering the upstream such that
1441 * cherry-picks cannot be detected and thus all commits are
1442 * reapplied. Thus, --[no-]reapply-cherry-picks is
1443 * supported when --keep-base is specified, but not when
1444 * --keep-base is left out.
1445 */
1446 imply_merge(&options, options.reapply_cherry_picks ?
1447 "--reapply-cherry-picks" :
1448 "--no-reapply-cherry-picks");
0fcb4f6b 1449
c241371c 1450 if (gpg_sign)
12026a41 1451 options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign);
12026a41 1452
e57d2c59 1453 if (options.exec.nr)
10cdb9f3 1454 imply_merge(&options, "--exec");
68e46d78 1455
ef484add
RA
1456 if (options.type == REBASE_APPLY) {
1457 if (ignore_whitespace)
9c31b19d
JH
1458 strvec_push(&options.git_am_opts,
1459 "--ignore-whitespace");
7573cec5 1460 if (options.committer_date_is_author_date)
9c31b19d
JH
1461 strvec_push(&options.git_am_opts,
1462 "--committer-date-is-author-date");
a3894aad 1463 if (options.ignore_date)
9c31b19d 1464 strvec_push(&options.git_am_opts, "--ignore-date");
ef484add 1465 } else {
ff8d6e5a 1466 /* REBASE_MERGE */
ef484add 1467 if (ignore_whitespace) {
4a8bc986 1468 string_list_append(&options.strategy_opts,
ef484add
RA
1469 "ignore-space-change");
1470 }
1471 }
1472
4a8bc986
PW
1473 if (options.strategy_opts.nr && !options.strategy)
1474 options.strategy = "ort";
ba1905a5
PK
1475
1476 if (options.strategy) {
1477 options.strategy = xstrdup(options.strategy);
37e80a24 1478 imply_merge(&options, "--strategy");
ba1905a5
PK
1479 }
1480
9dba809a 1481 if (options.root && !options.onto_name)
10cdb9f3 1482 imply_merge(&options, "--root without --onto");
9dba809a 1483
cda614e4
PK
1484 if (isatty(2) && options.flags & REBASE_NO_QUIET)
1485 strbuf_addstr(&options.git_format_patch_opt, " --progress");
1486
d70a9eb6 1487 if (options.git_am_opts.nr || options.type == REBASE_APPLY) {
10cdb9f3 1488 /* all am options except -q are compatible only with --apply */
d70a9eb6
JK
1489 for (i = options.git_am_opts.nr - 1; i >= 0; i--)
1490 if (strcmp(options.git_am_opts.v[i], "-q"))
8af14f08
EN
1491 break;
1492
eddfcd8e 1493 if (i >= 0 || options.type == REBASE_APPLY) {
10cdb9f3 1494 if (is_merge(&options))
246cac85
JNA
1495 die(_("apply options and merge options "
1496 "cannot be used together"));
6605fb70
AH
1497 else if (options.rebase_merges == -1 && options.config_rebase_merges == 1)
1498 die(_("apply options are incompatible with rebase.rebaseMerges. Consider adding --no-rebase-merges"));
eddfcd8e
EN
1499 else if (options.update_refs == -1 && options.config_update_refs == 1)
1500 die(_("apply options are incompatible with rebase.updateRefs. Consider adding --no-update-refs"));
8295ed69 1501 else
10cdb9f3 1502 options.type = REBASE_APPLY;
8295ed69
EN
1503 }
1504 }
1505
eddfcd8e 1506 if (options.update_refs == 1)
1207599e 1507 imply_merge(&options, "--update-refs");
eddfcd8e
EN
1508 options.update_refs = (options.update_refs >= 0) ? options.update_refs :
1509 ((options.config_update_refs >= 0) ? options.config_update_refs : 0);
1207599e 1510
6605fb70
AH
1511 if (options.rebase_merges == 1)
1512 imply_merge(&options, "--rebase-merges");
1513 options.rebase_merges = (options.rebase_merges >= 0) ? options.rebase_merges :
1514 ((options.config_rebase_merges >= 0) ? options.config_rebase_merges : 0);
1515
75cf39b1 1516 if (options.autosquash == 1) {
796abac7 1517 imply_merge(&options, "--autosquash");
75cf39b1
AK
1518 } else if (options.autosquash == -1) {
1519 options.autosquash =
1520 options.config_autosquash &&
1521 (options.flags & REBASE_INTERACTIVE_EXPLICIT);
1522 }
796abac7 1523
8295ed69
EN
1524 if (options.type == REBASE_UNSPECIFIED) {
1525 if (!strcmp(options.default_backend, "merge"))
a5b5740b 1526 options.type = REBASE_MERGE;
10cdb9f3
EN
1527 else if (!strcmp(options.default_backend, "apply"))
1528 options.type = REBASE_APPLY;
8295ed69
EN
1529 else
1530 die(_("Unknown rebase backend: %s"),
1531 options.default_backend);
8af14f08
EN
1532 }
1533
14c4586c
EN
1534 if (options.type == REBASE_MERGE &&
1535 !options.strategy &&
1536 getenv("GIT_TEST_MERGE_ALGORITHM"))
1537 options.strategy = xstrdup(getenv("GIT_TEST_MERGE_ALGORITHM"));
1538
ac7f467f
PK
1539 switch (options.type) {
1540 case REBASE_MERGE:
ac7f467f
PK
1541 options.state_dir = merge_dir();
1542 break;
10cdb9f3 1543 case REBASE_APPLY:
ac7f467f
PK
1544 options.state_dir = apply_dir();
1545 break;
1546 default:
8295ed69 1547 BUG("options.type was just set above; should be unreachable.");
ac7f467f
PK
1548 }
1549
e98c4269
EN
1550 if (options.empty == EMPTY_UNSPECIFIED) {
1551 if (options.flags & REBASE_INTERACTIVE_EXPLICIT)
c282eba2 1552 options.empty = EMPTY_STOP;
e57d2c59 1553 else if (options.exec.nr > 0)
e98c4269
EN
1554 options.empty = EMPTY_KEEP;
1555 else
1556 options.empty = EMPTY_DROP;
1557 }
10cdb9f3 1558 if (reschedule_failed_exec > 0 && !is_merge(&options))
906b6394
JS
1559 die(_("--reschedule-failed-exec requires "
1560 "--exec or --interactive"));
1561 if (reschedule_failed_exec >= 0)
1562 options.reschedule_failed_exec = reschedule_failed_exec;
d421afa0 1563
73d51ed0 1564 if (options.signoff) {
22f9b7f3 1565 strvec_push(&options.git_am_opts, "--signoff");
73d51ed0
PK
1566 options.flags |= REBASE_FORCE;
1567 }
1568
ac7f467f 1569 if (!options.root) {
8f5986d9
PK
1570 if (argc < 1) {
1571 struct branch *branch;
1572
1573 branch = branch_get(NULL);
1574 options.upstream_name = branch_get_upstream(branch,
1575 NULL);
1576 if (!options.upstream_name)
1577 error_on_missing_default_upstream();
2803d800
AH
1578 if (options.fork_point < 0)
1579 options.fork_point = 1;
8f5986d9 1580 } else {
f28d40d3 1581 options.upstream_name = argv[0];
ac7f467f
PK
1582 argc--;
1583 argv++;
1584 if (!strcmp(options.upstream_name, "-"))
1585 options.upstream_name = "@{-1}";
1586 }
1d188263
PW
1587 options.upstream =
1588 lookup_commit_reference_by_name(options.upstream_name);
ac7f467f
PK
1589 if (!options.upstream)
1590 die(_("invalid upstream '%s'"), options.upstream_name);
06e4775a 1591 options.upstream_arg = options.upstream_name;
9dba809a
PK
1592 } else {
1593 if (!options.onto_name) {
1594 if (commit_tree("", 0, the_hash_algo->empty_tree, NULL,
1595 &squash_onto, NULL, NULL) < 0)
1596 die(_("Could not create new root commit"));
1597 options.squash_onto = &squash_onto;
1598 options.onto_name = squash_onto_name =
1599 xstrdup(oid_to_hex(&squash_onto));
e1fac531
JS
1600 } else
1601 options.root_with_onto = 1;
1602
9dba809a
PK
1603 options.upstream_name = NULL;
1604 options.upstream = NULL;
1605 if (argc > 1)
1606 usage_with_options(builtin_rebase_usage,
1607 builtin_rebase_options);
1608 options.upstream_arg = "--root";
1609 }
ac7f467f 1610
ac7f467f
PK
1611 /*
1612 * If the branch to rebase is given, that is the branch we will rebase
1613 * branch_name -- branch/commit being rebased, or
1614 * HEAD (already detached)
1615 * orig_head -- commit object name of tip of the branch before rebasing
d4c569f8 1616 * head_name -- refs/heads/<that-branch> or NULL (detached HEAD)
ac7f467f 1617 */
e65123a7
PK
1618 if (argc == 1) {
1619 /* Is it "rebase other branchname" or "rebase other commit"? */
f21becdd 1620 struct object_id branch_oid;
e65123a7
PK
1621 branch_name = argv[0];
1622 options.switch_to = argv[0];
1623
1624 /* Is it a local branch? */
1625 strbuf_reset(&buf);
1626 strbuf_addf(&buf, "refs/heads/%s", branch_name);
2e5c4758 1627 if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) {
b5cabb4a 1628 die_if_checked_out(buf.buf, 1);
e65123a7 1629 options.head_name = xstrdup(buf.buf);
f21becdd
PW
1630 options.orig_head =
1631 lookup_commit_object(the_repository,
1632 &branch_oid);
e65123a7 1633 /* If not is it a valid ref (branch or commit)? */
7740ac69 1634 } else {
f21becdd 1635 options.orig_head =
7740ac69 1636 lookup_commit_reference_by_name(branch_name);
e65123a7 1637 options.head_name = NULL;
7740ac69 1638 }
f21becdd
PW
1639 if (!options.orig_head)
1640 die(_("no such branch/commit '%s'"), branch_name);
e65123a7 1641 } else if (argc == 0) {
ac7f467f
PK
1642 /* Do not need to switch branches, we are already on it. */
1643 options.head_name =
2e5c4758
PS
1644 xstrdup_or_null(refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL,
1645 &flags));
ac7f467f
PK
1646 if (!options.head_name)
1647 die(_("No such ref: %s"), "HEAD");
1648 if (flags & REF_ISSYMREF) {
1649 if (!skip_prefix(options.head_name,
1650 "refs/heads/", &branch_name))
1651 branch_name = options.head_name;
1652
1653 } else {
7762f44e 1654 FREE_AND_NULL(options.head_name);
ac7f467f
PK
1655 branch_name = "HEAD";
1656 }
f21becdd
PW
1657 options.orig_head = lookup_commit_reference_by_name("HEAD");
1658 if (!options.orig_head)
1659 die(_("Could not resolve HEAD to a commit"));
e65123a7
PK
1660 } else
1661 BUG("unexpected number of arguments left to parse");
ac7f467f 1662
9e5ebe96
AH
1663 /* Make sure the branch to rebase onto is valid. */
1664 if (keep_base) {
1665 strbuf_reset(&buf);
1666 strbuf_addstr(&buf, options.upstream_name);
1667 strbuf_addstr(&buf, "...");
1668 strbuf_addstr(&buf, branch_name);
94ad545d 1669 options.onto_name = keep_base_onto_name = xstrdup(buf.buf);
9e5ebe96
AH
1670 } else if (!options.onto_name)
1671 options.onto_name = options.upstream_name;
1672 if (strstr(options.onto_name, "...")) {
d850b7a5 1673 if (repo_get_oid_mb(the_repository, options.onto_name, &branch_base) < 0) {
9e5ebe96
AH
1674 if (keep_base)
1675 die(_("'%s': need exactly one merge base with branch"),
1676 options.upstream_name);
1677 else
1678 die(_("'%s': need exactly one merge base"),
1679 options.onto_name);
1680 }
a7706021 1681 options.onto = lookup_commit_or_die(&branch_base,
9e5ebe96
AH
1682 options.onto_name);
1683 } else {
1684 options.onto =
1685 lookup_commit_reference_by_name(options.onto_name);
1686 if (!options.onto)
1687 die(_("Does not point to a valid commit '%s'"),
1688 options.onto_name);
d42c9ffa 1689 fill_branch_base(&options, &branch_base);
9e5ebe96
AH
1690 }
1691
ce5238a6
PW
1692 if (keep_base && options.reapply_cherry_picks)
1693 options.upstream = options.onto;
1694
f21becdd 1695 if (options.fork_point > 0)
92d0d74e 1696 options.restrict_revision =
f21becdd 1697 get_fork_point(options.upstream_name, options.orig_head);
92d0d74e 1698
e1ff0a32 1699 if (repo_read_index(the_repository) < 0)
e0333e5c
PK
1700 die(_("could not read index"));
1701
b7de153b
PW
1702 if (options.autostash)
1703 create_autostash(the_repository,
1704 state_dir_path("autostash", &options));
1705
6defce2b 1706
5b02ca38 1707 if (require_clean_work_tree(the_repository, "rebase",
e0333e5c 1708 _("Please commit or stash them."), 1, 1)) {
35f070b4 1709 ret = -1;
e0333e5c 1710 goto cleanup;
ac7f467f
PK
1711 }
1712
9a48a615
PK
1713 /*
1714 * Now we are rebasing commits upstream..orig_head (or with --root,
1715 * everything leading up to orig_head) on top of onto.
1716 */
1717
1718 /*
1719 * Check if we are already based on onto with linear history,
c0efb4c1 1720 * in which case we could fast-forward without replacing the commits
befb89ce 1721 * with new commits recreated by replaying their changes.
9a48a615 1722 */
d42c9ffa
PW
1723 if (allow_preemptive_ff &&
1724 can_fast_forward(options.onto, options.upstream, options.restrict_revision,
1725 options.orig_head, &branch_base)) {
9a48a615
PK
1726 int flag;
1727
1ed9c14f 1728 if (!(options.flags & REBASE_FORCE)) {
e65123a7
PK
1729 /* Lazily switch to the target branch if needed... */
1730 if (options.switch_to) {
ae42fa4c
PW
1731 ret = checkout_up_to_date(&options);
1732 if (ret)
e65123a7 1733 goto cleanup;
e65123a7
PK
1734 }
1735
1ed9c14f
PK
1736 if (!(options.flags & REBASE_NO_QUIET))
1737 ; /* be quiet */
1738 else if (!strcmp(branch_name, "HEAD") &&
2e5c4758 1739 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag))
1ed9c14f
PK
1740 puts(_("HEAD is up to date."));
1741 else
1742 printf(_("Current branch %s is up to date.\n"),
1743 branch_name);
35f070b4 1744 ret = finish_rebase(&options);
1ed9c14f
PK
1745 goto cleanup;
1746 } else if (!(options.flags & REBASE_NO_QUIET))
9a48a615
PK
1747 ; /* be quiet */
1748 else if (!strcmp(branch_name, "HEAD") &&
2e5c4758 1749 refs_resolve_ref_unsafe(get_main_ref_store(the_repository), "HEAD", 0, NULL, &flag))
9a48a615
PK
1750 puts(_("HEAD is up to date, rebase forced."));
1751 else
1752 printf(_("Current branch %s is up to date, rebase "
1753 "forced.\n"), branch_name);
1754 }
1755
06e4775a
PK
1756 /* If a hook exists, give it a chance to interrupt*/
1757 if (!ok_to_skip_pre_rebase &&
25d4e02c 1758 run_hooks_l("pre-rebase", options.upstream_arg,
06e4775a
PK
1759 argc ? argv[0] : NULL, NULL))
1760 die(_("The pre-rebase hook refused to rebase."));
1761
bff014da
PK
1762 if (options.flags & REBASE_DIFFSTAT) {
1763 struct diff_options opts;
1764
8797f0f0 1765 if (options.flags & REBASE_VERBOSE) {
a7706021 1766 if (is_null_oid(&branch_base))
8797f0f0
JS
1767 printf(_("Changes to %s:\n"),
1768 oid_to_hex(&options.onto->object.oid));
1769 else
1770 printf(_("Changes from %s to %s:\n"),
a7706021 1771 oid_to_hex(&branch_base),
8797f0f0
JS
1772 oid_to_hex(&options.onto->object.oid));
1773 }
bff014da
PK
1774
1775 /* We want color (if set), but no pager */
08539032 1776 repo_diff_setup(the_repository, &opts);
4ca7a3fd 1777 init_diffstat_widths(&opts);
bff014da
PK
1778 opts.output_format |=
1779 DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT;
1780 opts.detect_rename = DIFF_DETECT_RENAME;
1781 diff_setup_done(&opts);
a7706021
PW
1782 diff_tree_oid(is_null_oid(&branch_base) ?
1783 the_hash_algo->empty_tree : &branch_base,
8797f0f0 1784 &options.onto->object.oid, "", &opts);
bff014da
PK
1785 diffcore_std(&opts);
1786 diff_flush(&opts);
1787 }
1788
10cdb9f3 1789 if (is_merge(&options))
361badd3
PK
1790 goto run_rebase;
1791
bff014da
PK
1792 /* Detach HEAD and reset the tree */
1793 if (options.flags & REBASE_NO_QUIET)
1794 printf(_("First, rewinding head to replay your work on top of "
1795 "it...\n"));
1796
be0d29d3 1797 strbuf_addf(&msg, "%s (start): checkout %s",
0e34efb3 1798 options.reflog_action, options.onto_name);
6ae80861 1799 ropts.oid = &options.onto->object.oid;
f21becdd 1800 ropts.orig_head = &options.orig_head->object.oid,
6ae80861
PW
1801 ropts.flags = RESET_HEAD_DETACH | RESET_ORIG_HEAD |
1802 RESET_HEAD_RUN_POST_CHECKOUT_HOOK;
1803 ropts.head_msg = msg.buf;
0e34efb3 1804 ropts.default_reflog_action = options.reflog_action;
6ae80861 1805 if (reset_head(the_repository, &ropts))
ac7f467f
PK
1806 die(_("Could not detach HEAD"));
1807 strbuf_release(&msg);
1808
7eecfa56
PK
1809 /*
1810 * If the onto is a proper descendant of the tip of the branch, then
1811 * we just fast-forwarded.
1812 */
a7706021 1813 if (oideq(&branch_base, &options.orig_head->object.oid)) {
eff199a6 1814 printf(_("Fast-forwarded %s to %s.\n"),
7eecfa56 1815 branch_name, options.onto_name);
57a14985 1816 move_to_original_branch(&options);
35f070b4 1817 ret = finish_rebase(&options);
7eecfa56
PK
1818 goto cleanup;
1819 }
1820
ac7f467f
PK
1821 strbuf_addf(&revisions, "%s..%s",
1822 options.root ? oid_to_hex(&options.onto->object.oid) :
1823 (options.restrict_revision ?
1824 oid_to_hex(&options.restrict_revision->object.oid) :
1825 oid_to_hex(&options.upstream->object.oid)),
f21becdd 1826 oid_to_hex(&options.orig_head->object.oid));
ac7f467f
PK
1827
1828 options.revisions = revisions.buf;
1829
f9573628 1830run_rebase:
9a1925b0 1831 ret = run_specific_rebase(&options);
ac7f467f 1832
e0333e5c 1833cleanup:
7372eaeb 1834 strbuf_release(&buf);
ac7f467f 1835 strbuf_release(&revisions);
0e34efb3 1836 free(options.reflog_action);
ac7f467f 1837 free(options.head_name);
b6046abc 1838 strvec_clear(&options.git_am_opts);
12026a41 1839 free(options.gpg_sign_opt);
e57d2c59 1840 string_list_clear(&options.exec, 0);
b54cf3a7 1841 free(options.strategy);
4a8bc986 1842 string_list_clear(&options.strategy_opts, 0);
805b789a 1843 strbuf_release(&options.git_format_patch_opt);
9dba809a 1844 free(squash_onto_name);
94ad545d 1845 free(keep_base_onto_name);
35f070b4 1846 return !!ret;
55071ea2 1847}