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