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