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