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