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