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