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