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