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