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