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