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