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