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