]>
Commit | Line | Data |
---|---|---|
55071ea2 PK |
1 | /* |
2 | * "git rebase" builtin command | |
3 | * | |
4 | * Copyright (c) 2018 Pratik Karki | |
5 | */ | |
6 | ||
7 | #include "builtin.h" | |
8 | #include "run-command.h" | |
9 | #include "exec-cmd.h" | |
10 | #include "argv-array.h" | |
11 | #include "dir.h" | |
ac7f467f PK |
12 | #include "packfile.h" |
13 | #include "refs.h" | |
14 | #include "quote.h" | |
15 | #include "config.h" | |
16 | #include "cache-tree.h" | |
17 | #include "unpack-trees.h" | |
18 | #include "lockfile.h" | |
f28d40d3 | 19 | #include "parse-options.h" |
075bc852 | 20 | #include "commit.h" |
bff014da | 21 | #include "diff.h" |
e0333e5c | 22 | #include "wt-status.h" |
9a48a615 | 23 | #include "revision.h" |
122420c2 | 24 | #include "rerere.h" |
f28d40d3 PK |
25 | |
26 | static char const * const builtin_rebase_usage[] = { | |
27 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
28 | "[<upstream>] [<branch>]"), | |
29 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
30 | "--root [<branch>]"), | |
31 | N_("git rebase --continue | --abort | --skip | --edit-todo"), | |
32 | NULL | |
33 | }; | |
ac7f467f PK |
34 | |
35 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") | |
36 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") | |
37 | ||
38 | enum rebase_type { | |
39 | REBASE_UNSPECIFIED = -1, | |
40 | REBASE_AM, | |
41 | REBASE_MERGE, | |
42 | REBASE_INTERACTIVE, | |
43 | REBASE_PRESERVE_MERGES | |
44 | }; | |
55071ea2 PK |
45 | |
46 | static int use_builtin_rebase(void) | |
47 | { | |
48 | struct child_process cp = CHILD_PROCESS_INIT; | |
49 | struct strbuf out = STRBUF_INIT; | |
50 | int ret; | |
51 | ||
52 | argv_array_pushl(&cp.args, | |
53 | "config", "--bool", "rebase.usebuiltin", NULL); | |
54 | cp.git_cmd = 1; | |
55 | if (capture_command(&cp, &out, 6)) { | |
56 | strbuf_release(&out); | |
57 | return 0; | |
58 | } | |
59 | ||
60 | strbuf_trim(&out); | |
61 | ret = !strcmp("true", out.buf); | |
62 | strbuf_release(&out); | |
63 | return ret; | |
64 | } | |
65 | ||
ac7f467f PK |
66 | struct rebase_options { |
67 | enum rebase_type type; | |
68 | const char *state_dir; | |
69 | struct commit *upstream; | |
70 | const char *upstream_name; | |
06e4775a | 71 | const char *upstream_arg; |
ac7f467f PK |
72 | char *head_name; |
73 | struct object_id orig_head; | |
74 | struct commit *onto; | |
75 | const char *onto_name; | |
76 | const char *revisions; | |
e65123a7 | 77 | const char *switch_to; |
ac7f467f | 78 | int root; |
9dba809a | 79 | struct object_id *squash_onto; |
ac7f467f PK |
80 | struct commit *restrict_revision; |
81 | int dont_finish_rebase; | |
b4c8eb02 PK |
82 | enum { |
83 | REBASE_NO_QUIET = 1<<0, | |
bff014da PK |
84 | REBASE_VERBOSE = 1<<1, |
85 | REBASE_DIFFSTAT = 1<<2, | |
1ed9c14f | 86 | REBASE_FORCE = 1<<3, |
c54dacb5 | 87 | REBASE_INTERACTIVE_EXPLICIT = 1<<4, |
b4c8eb02 PK |
88 | } flags; |
89 | struct strbuf git_am_opt; | |
f9573628 | 90 | const char *action; |
73d51ed0 | 91 | int signoff; |
ead98c11 | 92 | int allow_rerere_autoupdate; |
002ee2fe | 93 | int keep_empty; |
051910a9 | 94 | int autosquash; |
12026a41 | 95 | char *gpg_sign_opt; |
6defce2b | 96 | int autostash; |
68e46d78 | 97 | char *cmd; |
9b3a448b | 98 | int allow_empty_message; |
3c3588c7 | 99 | int rebase_merges, rebase_cousins; |
ba1905a5 | 100 | char *strategy, *strategy_opts; |
cda614e4 | 101 | struct strbuf git_format_patch_opt; |
ac7f467f PK |
102 | }; |
103 | ||
9a48a615 PK |
104 | static int is_interactive(struct rebase_options *opts) |
105 | { | |
106 | return opts->type == REBASE_INTERACTIVE || | |
107 | opts->type == REBASE_PRESERVE_MERGES; | |
108 | } | |
109 | ||
002ee2fe PK |
110 | static void imply_interactive(struct rebase_options *opts, const char *option) |
111 | { | |
112 | switch (opts->type) { | |
113 | case REBASE_AM: | |
114 | die(_("%s requires an interactive rebase"), option); | |
115 | break; | |
116 | case REBASE_INTERACTIVE: | |
117 | case REBASE_PRESERVE_MERGES: | |
118 | break; | |
119 | case REBASE_MERGE: | |
120 | /* we silently *upgrade* --merge to --interactive if needed */ | |
121 | default: | |
122 | opts->type = REBASE_INTERACTIVE; /* implied */ | |
123 | break; | |
124 | } | |
125 | } | |
126 | ||
ac7f467f PK |
127 | /* Returns the filename prefixed by the state_dir */ |
128 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) | |
129 | { | |
130 | static struct strbuf path = STRBUF_INIT; | |
131 | static size_t prefix_len; | |
132 | ||
133 | if (!prefix_len) { | |
134 | strbuf_addf(&path, "%s/", opts->state_dir); | |
135 | prefix_len = path.len; | |
136 | } | |
137 | ||
138 | strbuf_setlen(&path, prefix_len); | |
139 | strbuf_addstr(&path, filename); | |
140 | return path.buf; | |
141 | } | |
142 | ||
f9573628 PK |
143 | /* Read one file, then strip line endings */ |
144 | static int read_one(const char *path, struct strbuf *buf) | |
145 | { | |
146 | if (strbuf_read_file(buf, path, 0) < 0) | |
147 | return error_errno(_("could not read '%s'"), path); | |
148 | strbuf_trim_trailing_newline(buf); | |
149 | return 0; | |
150 | } | |
151 | ||
152 | /* Initialize the rebase options from the state directory. */ | |
153 | static int read_basic_state(struct rebase_options *opts) | |
154 | { | |
155 | struct strbuf head_name = STRBUF_INIT; | |
156 | struct strbuf buf = STRBUF_INIT; | |
157 | struct object_id oid; | |
158 | ||
159 | if (read_one(state_dir_path("head-name", opts), &head_name) || | |
160 | read_one(state_dir_path("onto", opts), &buf)) | |
161 | return -1; | |
162 | opts->head_name = starts_with(head_name.buf, "refs/") ? | |
163 | xstrdup(head_name.buf) : NULL; | |
164 | strbuf_release(&head_name); | |
165 | if (get_oid(buf.buf, &oid)) | |
166 | return error(_("could not get 'onto': '%s'"), buf.buf); | |
167 | opts->onto = lookup_commit_or_die(&oid, buf.buf); | |
168 | ||
169 | /* | |
170 | * We always write to orig-head, but interactive rebase used to write to | |
171 | * head. Fall back to reading from head to cover for the case that the | |
172 | * user upgraded git with an ongoing interactive rebase. | |
173 | */ | |
174 | strbuf_reset(&buf); | |
175 | if (file_exists(state_dir_path("orig-head", opts))) { | |
176 | if (read_one(state_dir_path("orig-head", opts), &buf)) | |
177 | return -1; | |
178 | } else if (read_one(state_dir_path("head", opts), &buf)) | |
179 | return -1; | |
180 | if (get_oid(buf.buf, &opts->orig_head)) | |
181 | return error(_("invalid orig-head: '%s'"), buf.buf); | |
182 | ||
183 | strbuf_reset(&buf); | |
184 | if (read_one(state_dir_path("quiet", opts), &buf)) | |
185 | return -1; | |
186 | if (buf.len) | |
187 | opts->flags &= ~REBASE_NO_QUIET; | |
188 | else | |
189 | opts->flags |= REBASE_NO_QUIET; | |
190 | ||
191 | if (file_exists(state_dir_path("verbose", opts))) | |
192 | opts->flags |= REBASE_VERBOSE; | |
193 | ||
73d51ed0 PK |
194 | if (file_exists(state_dir_path("signoff", opts))) { |
195 | opts->signoff = 1; | |
196 | opts->flags |= REBASE_FORCE; | |
197 | } | |
198 | ||
ead98c11 PK |
199 | if (file_exists(state_dir_path("allow_rerere_autoupdate", opts))) { |
200 | strbuf_reset(&buf); | |
201 | if (read_one(state_dir_path("allow_rerere_autoupdate", opts), | |
202 | &buf)) | |
203 | return -1; | |
204 | if (!strcmp(buf.buf, "--rerere-autoupdate")) | |
205 | opts->allow_rerere_autoupdate = 1; | |
206 | else if (!strcmp(buf.buf, "--no-rerere-autoupdate")) | |
207 | opts->allow_rerere_autoupdate = 0; | |
208 | else | |
209 | warning(_("ignoring invalid allow_rerere_autoupdate: " | |
210 | "'%s'"), buf.buf); | |
211 | } else | |
212 | opts->allow_rerere_autoupdate = -1; | |
213 | ||
12026a41 PK |
214 | if (file_exists(state_dir_path("gpg_sign_opt", opts))) { |
215 | strbuf_reset(&buf); | |
216 | if (read_one(state_dir_path("gpg_sign_opt", opts), | |
217 | &buf)) | |
218 | return -1; | |
219 | free(opts->gpg_sign_opt); | |
220 | opts->gpg_sign_opt = xstrdup(buf.buf); | |
221 | } | |
222 | ||
ba1905a5 PK |
223 | if (file_exists(state_dir_path("strategy", opts))) { |
224 | strbuf_reset(&buf); | |
225 | if (read_one(state_dir_path("strategy", opts), &buf)) | |
226 | return -1; | |
227 | free(opts->strategy); | |
228 | opts->strategy = xstrdup(buf.buf); | |
229 | } | |
230 | ||
231 | if (file_exists(state_dir_path("strategy_opts", opts))) { | |
232 | strbuf_reset(&buf); | |
233 | if (read_one(state_dir_path("strategy_opts", opts), &buf)) | |
234 | return -1; | |
235 | free(opts->strategy_opts); | |
236 | opts->strategy_opts = xstrdup(buf.buf); | |
237 | } | |
238 | ||
f9573628 PK |
239 | strbuf_release(&buf); |
240 | ||
241 | return 0; | |
242 | } | |
243 | ||
6defce2b PK |
244 | static int apply_autostash(struct rebase_options *opts) |
245 | { | |
246 | const char *path = state_dir_path("autostash", opts); | |
247 | struct strbuf autostash = STRBUF_INIT; | |
248 | struct child_process stash_apply = CHILD_PROCESS_INIT; | |
249 | ||
250 | if (!file_exists(path)) | |
251 | return 0; | |
252 | ||
253 | if (read_one(state_dir_path("autostash", opts), &autostash)) | |
254 | return error(_("Could not read '%s'"), path); | |
255 | argv_array_pushl(&stash_apply.args, | |
256 | "stash", "apply", autostash.buf, NULL); | |
257 | stash_apply.git_cmd = 1; | |
258 | stash_apply.no_stderr = stash_apply.no_stdout = | |
259 | stash_apply.no_stdin = 1; | |
260 | if (!run_command(&stash_apply)) | |
261 | printf(_("Applied autostash.\n")); | |
262 | else { | |
263 | struct argv_array args = ARGV_ARRAY_INIT; | |
264 | int res = 0; | |
265 | ||
266 | argv_array_pushl(&args, | |
267 | "stash", "store", "-m", "autostash", "-q", | |
268 | autostash.buf, NULL); | |
269 | if (run_command_v_opt(args.argv, RUN_GIT_CMD)) | |
270 | res = error(_("Cannot store %s"), autostash.buf); | |
271 | argv_array_clear(&args); | |
272 | strbuf_release(&autostash); | |
273 | if (res) | |
274 | return res; | |
275 | ||
276 | fprintf(stderr, | |
277 | _("Applying autostash resulted in conflicts.\n" | |
278 | "Your changes are safe in the stash.\n" | |
279 | "You can run \"git stash pop\" or \"git stash drop\" " | |
280 | "at any time.\n")); | |
281 | } | |
282 | ||
283 | strbuf_release(&autostash); | |
284 | return 0; | |
285 | } | |
286 | ||
ac7f467f PK |
287 | static int finish_rebase(struct rebase_options *opts) |
288 | { | |
289 | struct strbuf dir = STRBUF_INIT; | |
290 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
291 | ||
292 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); | |
6defce2b | 293 | apply_autostash(opts); |
ac7f467f PK |
294 | close_all_packs(the_repository->objects); |
295 | /* | |
296 | * We ignore errors in 'gc --auto', since the | |
297 | * user should see them. | |
298 | */ | |
299 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
300 | strbuf_addstr(&dir, opts->state_dir); | |
301 | remove_dir_recursively(&dir, 0); | |
302 | strbuf_release(&dir); | |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
307 | static struct commit *peel_committish(const char *name) | |
308 | { | |
309 | struct object *obj; | |
310 | struct object_id oid; | |
311 | ||
312 | if (get_oid(name, &oid)) | |
313 | return NULL; | |
314 | obj = parse_object(the_repository, &oid); | |
315 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); | |
316 | } | |
317 | ||
318 | static void add_var(struct strbuf *buf, const char *name, const char *value) | |
319 | { | |
320 | if (!value) | |
321 | strbuf_addf(buf, "unset %s; ", name); | |
322 | else { | |
323 | strbuf_addf(buf, "%s=", name); | |
324 | sq_quote_buf(buf, value); | |
325 | strbuf_addstr(buf, "; "); | |
326 | } | |
327 | } | |
328 | ||
329 | static int run_specific_rebase(struct rebase_options *opts) | |
330 | { | |
331 | const char *argv[] = { NULL, NULL }; | |
332 | struct strbuf script_snippet = STRBUF_INIT; | |
333 | int status; | |
334 | const char *backend, *backend_func; | |
335 | ||
336 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); | |
337 | add_var(&script_snippet, "state_dir", opts->state_dir); | |
338 | ||
339 | add_var(&script_snippet, "upstream_name", opts->upstream_name); | |
f9573628 PK |
340 | add_var(&script_snippet, "upstream", opts->upstream ? |
341 | oid_to_hex(&opts->upstream->object.oid) : NULL); | |
d4c569f8 PK |
342 | add_var(&script_snippet, "head_name", |
343 | opts->head_name ? opts->head_name : "detached HEAD"); | |
ac7f467f | 344 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); |
f9573628 PK |
345 | add_var(&script_snippet, "onto", opts->onto ? |
346 | oid_to_hex(&opts->onto->object.oid) : NULL); | |
ac7f467f PK |
347 | add_var(&script_snippet, "onto_name", opts->onto_name); |
348 | add_var(&script_snippet, "revisions", opts->revisions); | |
349 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? | |
350 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); | |
b4c8eb02 PK |
351 | add_var(&script_snippet, "GIT_QUIET", |
352 | opts->flags & REBASE_NO_QUIET ? "" : "t"); | |
353 | add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf); | |
bff014da PK |
354 | add_var(&script_snippet, "verbose", |
355 | opts->flags & REBASE_VERBOSE ? "t" : ""); | |
356 | add_var(&script_snippet, "diffstat", | |
357 | opts->flags & REBASE_DIFFSTAT ? "t" : ""); | |
1ed9c14f PK |
358 | add_var(&script_snippet, "force_rebase", |
359 | opts->flags & REBASE_FORCE ? "t" : ""); | |
e65123a7 PK |
360 | if (opts->switch_to) |
361 | add_var(&script_snippet, "switch_to", opts->switch_to); | |
f9573628 | 362 | add_var(&script_snippet, "action", opts->action ? opts->action : ""); |
73d51ed0 | 363 | add_var(&script_snippet, "signoff", opts->signoff ? "--signoff" : ""); |
ead98c11 PK |
364 | add_var(&script_snippet, "allow_rerere_autoupdate", |
365 | opts->allow_rerere_autoupdate < 0 ? "" : | |
366 | opts->allow_rerere_autoupdate ? | |
367 | "--rerere-autoupdate" : "--no-rerere-autoupdate"); | |
002ee2fe | 368 | add_var(&script_snippet, "keep_empty", opts->keep_empty ? "yes" : ""); |
051910a9 | 369 | add_var(&script_snippet, "autosquash", opts->autosquash ? "t" : ""); |
12026a41 | 370 | add_var(&script_snippet, "gpg_sign_opt", opts->gpg_sign_opt); |
68e46d78 | 371 | add_var(&script_snippet, "cmd", opts->cmd); |
9b3a448b PK |
372 | add_var(&script_snippet, "allow_empty_message", |
373 | opts->allow_empty_message ? "--allow-empty-message" : ""); | |
3c3588c7 PK |
374 | add_var(&script_snippet, "rebase_merges", |
375 | opts->rebase_merges ? "t" : ""); | |
376 | add_var(&script_snippet, "rebase_cousins", | |
377 | opts->rebase_cousins ? "t" : ""); | |
ba1905a5 PK |
378 | add_var(&script_snippet, "strategy", opts->strategy); |
379 | add_var(&script_snippet, "strategy_opts", opts->strategy_opts); | |
9dba809a PK |
380 | add_var(&script_snippet, "rebase_root", opts->root ? "t" : ""); |
381 | add_var(&script_snippet, "squash_onto", | |
382 | opts->squash_onto ? oid_to_hex(opts->squash_onto) : ""); | |
cda614e4 PK |
383 | add_var(&script_snippet, "git_format_patch_opt", |
384 | opts->git_format_patch_opt.buf); | |
ac7f467f PK |
385 | |
386 | switch (opts->type) { | |
387 | case REBASE_AM: | |
388 | backend = "git-rebase--am"; | |
389 | backend_func = "git_rebase__am"; | |
390 | break; | |
391 | case REBASE_INTERACTIVE: | |
392 | backend = "git-rebase--interactive"; | |
393 | backend_func = "git_rebase__interactive"; | |
394 | break; | |
395 | case REBASE_MERGE: | |
396 | backend = "git-rebase--merge"; | |
397 | backend_func = "git_rebase__merge"; | |
398 | break; | |
399 | case REBASE_PRESERVE_MERGES: | |
400 | backend = "git-rebase--preserve-merges"; | |
401 | backend_func = "git_rebase__preserve_merges"; | |
402 | break; | |
403 | default: | |
404 | BUG("Unhandled rebase type %d", opts->type); | |
405 | break; | |
406 | } | |
407 | ||
408 | strbuf_addf(&script_snippet, | |
409 | ". git-sh-setup && . git-rebase--common &&" | |
410 | " . %s && %s", backend, backend_func); | |
411 | argv[0] = script_snippet.buf; | |
412 | ||
413 | status = run_command_v_opt(argv, RUN_USING_SHELL); | |
414 | if (opts->dont_finish_rebase) | |
415 | ; /* do nothing */ | |
416 | else if (status == 0) { | |
417 | if (!file_exists(state_dir_path("stopped-sha", opts))) | |
418 | finish_rebase(opts); | |
419 | } else if (status == 2) { | |
420 | struct strbuf dir = STRBUF_INIT; | |
421 | ||
6defce2b | 422 | apply_autostash(opts); |
ac7f467f PK |
423 | strbuf_addstr(&dir, opts->state_dir); |
424 | remove_dir_recursively(&dir, 0); | |
425 | strbuf_release(&dir); | |
426 | die("Nothing to do"); | |
427 | } | |
428 | ||
429 | strbuf_release(&script_snippet); | |
430 | ||
431 | return status ? -1 : 0; | |
432 | } | |
433 | ||
434 | #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" | |
435 | ||
436 | static int reset_head(struct object_id *oid, const char *action, | |
fa443d40 PK |
437 | const char *switch_to_branch, int detach_head, |
438 | const char *reflog_orig_head, const char *reflog_head) | |
ac7f467f PK |
439 | { |
440 | struct object_id head_oid; | |
441 | struct tree_desc desc; | |
442 | struct lock_file lock = LOCK_INIT; | |
443 | struct unpack_trees_options unpack_tree_opts; | |
444 | struct tree *tree; | |
445 | const char *reflog_action; | |
446 | struct strbuf msg = STRBUF_INIT; | |
447 | size_t prefix_len; | |
448 | struct object_id *orig = NULL, oid_orig, | |
449 | *old_orig = NULL, oid_old_orig; | |
450 | int ret = 0; | |
451 | ||
d4c569f8 PK |
452 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
453 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); | |
454 | ||
ac7f467f PK |
455 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) |
456 | return -1; | |
457 | ||
458 | if (!oid) { | |
459 | if (get_oid("HEAD", &head_oid)) { | |
460 | rollback_lock_file(&lock); | |
461 | return error(_("could not determine HEAD revision")); | |
462 | } | |
463 | oid = &head_oid; | |
464 | } | |
465 | ||
466 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); | |
467 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); | |
468 | unpack_tree_opts.head_idx = 1; | |
469 | unpack_tree_opts.src_index = the_repository->index; | |
470 | unpack_tree_opts.dst_index = the_repository->index; | |
471 | unpack_tree_opts.fn = oneway_merge; | |
472 | unpack_tree_opts.update = 1; | |
473 | unpack_tree_opts.merge = 1; | |
474 | if (!detach_head) | |
475 | unpack_tree_opts.reset = 1; | |
476 | ||
477 | if (read_index_unmerged(the_repository->index) < 0) { | |
478 | rollback_lock_file(&lock); | |
479 | return error(_("could not read index")); | |
480 | } | |
481 | ||
482 | if (!fill_tree_descriptor(&desc, oid)) { | |
483 | error(_("failed to find tree of %s"), oid_to_hex(oid)); | |
484 | rollback_lock_file(&lock); | |
485 | free((void *)desc.buffer); | |
486 | return -1; | |
487 | } | |
488 | ||
489 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { | |
490 | rollback_lock_file(&lock); | |
491 | free((void *)desc.buffer); | |
492 | return -1; | |
493 | } | |
494 | ||
495 | tree = parse_tree_indirect(oid); | |
496 | prime_cache_tree(the_repository->index, tree); | |
497 | ||
498 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) | |
499 | ret = error(_("could not write index")); | |
500 | free((void *)desc.buffer); | |
501 | ||
502 | if (ret) | |
503 | return ret; | |
504 | ||
505 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
506 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); | |
507 | prefix_len = msg.len; | |
508 | ||
509 | if (!get_oid("ORIG_HEAD", &oid_old_orig)) | |
510 | old_orig = &oid_old_orig; | |
511 | if (!get_oid("HEAD", &oid_orig)) { | |
512 | orig = &oid_orig; | |
fa443d40 PK |
513 | if (!reflog_orig_head) { |
514 | strbuf_addstr(&msg, "updating ORIG_HEAD"); | |
515 | reflog_orig_head = msg.buf; | |
516 | } | |
517 | update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, | |
ac7f467f PK |
518 | UPDATE_REFS_MSG_ON_ERR); |
519 | } else if (old_orig) | |
520 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
fa443d40 PK |
521 | if (!reflog_head) { |
522 | strbuf_setlen(&msg, prefix_len); | |
523 | strbuf_addstr(&msg, "updating HEAD"); | |
524 | reflog_head = msg.buf; | |
525 | } | |
ac7f467f | 526 | if (!switch_to_branch) |
fa443d40 | 527 | ret = update_ref(reflog_head, "HEAD", oid, orig, REF_NO_DEREF, |
ac7f467f PK |
528 | UPDATE_REFS_MSG_ON_ERR); |
529 | else { | |
530 | ret = create_symref("HEAD", switch_to_branch, msg.buf); | |
531 | if (!ret) | |
fa443d40 | 532 | ret = update_ref(reflog_head, "HEAD", oid, NULL, 0, |
ac7f467f PK |
533 | UPDATE_REFS_MSG_ON_ERR); |
534 | } | |
535 | ||
536 | strbuf_release(&msg); | |
537 | return ret; | |
538 | } | |
539 | ||
bff014da PK |
540 | static int rebase_config(const char *var, const char *value, void *data) |
541 | { | |
542 | struct rebase_options *opts = data; | |
543 | ||
544 | if (!strcmp(var, "rebase.stat")) { | |
545 | if (git_config_bool(var, value)) | |
546 | opts->flags |= REBASE_DIFFSTAT; | |
547 | else | |
548 | opts->flags &= !REBASE_DIFFSTAT; | |
549 | return 0; | |
550 | } | |
551 | ||
051910a9 PK |
552 | if (!strcmp(var, "rebase.autosquash")) { |
553 | opts->autosquash = git_config_bool(var, value); | |
554 | return 0; | |
555 | } | |
556 | ||
12026a41 PK |
557 | if (!strcmp(var, "commit.gpgsign")) { |
558 | free(opts->gpg_sign_opt); | |
559 | opts->gpg_sign_opt = git_config_bool(var, value) ? | |
560 | xstrdup("-S") : NULL; | |
561 | return 0; | |
562 | } | |
563 | ||
6defce2b PK |
564 | if (!strcmp(var, "rebase.autostash")) { |
565 | opts->autostash = git_config_bool(var, value); | |
566 | return 0; | |
567 | } | |
568 | ||
bff014da PK |
569 | return git_default_config(var, value, data); |
570 | } | |
571 | ||
9a48a615 PK |
572 | /* |
573 | * Determines whether the commits in from..to are linear, i.e. contain | |
574 | * no merge commits. This function *expects* `from` to be an ancestor of | |
575 | * `to`. | |
576 | */ | |
577 | static int is_linear_history(struct commit *from, struct commit *to) | |
578 | { | |
579 | while (to && to != from) { | |
580 | parse_commit(to); | |
581 | if (!to->parents) | |
582 | return 1; | |
583 | if (to->parents->next) | |
584 | return 0; | |
585 | to = to->parents->item; | |
586 | } | |
587 | return 1; | |
588 | } | |
589 | ||
590 | static int can_fast_forward(struct commit *onto, struct object_id *head_oid, | |
591 | struct object_id *merge_base) | |
592 | { | |
593 | struct commit *head = lookup_commit(the_repository, head_oid); | |
594 | struct commit_list *merge_bases; | |
595 | int res; | |
596 | ||
597 | if (!head) | |
598 | return 0; | |
599 | ||
600 | merge_bases = get_merge_bases(onto, head); | |
601 | if (merge_bases && !merge_bases->next) { | |
602 | oidcpy(merge_base, &merge_bases->item->object.oid); | |
603 | res = !oidcmp(merge_base, &onto->object.oid); | |
604 | } else { | |
605 | oidcpy(merge_base, &null_oid); | |
606 | res = 0; | |
607 | } | |
608 | free_commit_list(merge_bases); | |
609 | return res && is_linear_history(onto, head); | |
610 | } | |
611 | ||
361badd3 PK |
612 | /* -i followed by -m is still -i */ |
613 | static int parse_opt_merge(const struct option *opt, const char *arg, int unset) | |
614 | { | |
615 | struct rebase_options *opts = opt->value; | |
616 | ||
617 | if (!is_interactive(opts)) | |
618 | opts->type = REBASE_MERGE; | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | /* -i followed by -p is still explicitly interactive, but -p alone is not */ | |
624 | static int parse_opt_interactive(const struct option *opt, const char *arg, | |
625 | int unset) | |
626 | { | |
627 | struct rebase_options *opts = opt->value; | |
628 | ||
629 | opts->type = REBASE_INTERACTIVE; | |
630 | opts->flags |= REBASE_INTERACTIVE_EXPLICIT; | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
8f5986d9 PK |
635 | static void NORETURN error_on_missing_default_upstream(void) |
636 | { | |
637 | struct branch *current_branch = branch_get(NULL); | |
638 | ||
639 | printf(_("%s\n" | |
640 | "Please specify which branch you want to rebase against.\n" | |
641 | "See git-rebase(1) for details.\n" | |
642 | "\n" | |
643 | " git rebase '<branch>'\n" | |
644 | "\n"), | |
645 | current_branch ? _("There is no tracking information for " | |
646 | "the current branch.") : | |
647 | _("You are not currently on a branch.")); | |
648 | ||
649 | if (current_branch) { | |
650 | const char *remote = current_branch->remote_name; | |
651 | ||
652 | if (!remote) | |
653 | remote = _("<remote>"); | |
654 | ||
655 | printf(_("If you wish to set tracking information for this " | |
656 | "branch you can do so with:\n" | |
657 | "\n" | |
658 | " git branch --set-upstream-to=%s/<branch> %s\n" | |
659 | "\n"), | |
660 | remote, current_branch->name); | |
661 | } | |
662 | exit(1); | |
663 | } | |
664 | ||
55071ea2 PK |
665 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
666 | { | |
ac7f467f PK |
667 | struct rebase_options options = { |
668 | .type = REBASE_UNSPECIFIED, | |
b4c8eb02 PK |
669 | .flags = REBASE_NO_QUIET, |
670 | .git_am_opt = STRBUF_INIT, | |
ead98c11 | 671 | .allow_rerere_autoupdate = -1, |
9b3a448b | 672 | .allow_empty_message = 1, |
cda614e4 | 673 | .git_format_patch_opt = STRBUF_INIT, |
ac7f467f PK |
674 | }; |
675 | const char *branch_name; | |
f9573628 | 676 | int ret, flags, total_argc, in_progress = 0; |
06e4775a | 677 | int ok_to_skip_pre_rebase = 0; |
ac7f467f PK |
678 | struct strbuf msg = STRBUF_INIT; |
679 | struct strbuf revisions = STRBUF_INIT; | |
c54dacb5 | 680 | struct strbuf buf = STRBUF_INIT; |
075bc852 | 681 | struct object_id merge_base; |
f9573628 PK |
682 | enum { |
683 | NO_ACTION, | |
684 | ACTION_CONTINUE, | |
122420c2 | 685 | ACTION_SKIP, |
5e5d9619 | 686 | ACTION_ABORT, |
5a614945 | 687 | ACTION_QUIT, |
51e9ea6d PK |
688 | ACTION_EDIT_TODO, |
689 | ACTION_SHOW_CURRENT_PATCH, | |
f9573628 | 690 | } action = NO_ACTION; |
38dbcef2 | 691 | int committer_date_is_author_date = 0; |
53f9e5be | 692 | int ignore_date = 0; |
99d8cc7c | 693 | int ignore_whitespace = 0; |
12026a41 | 694 | const char *gpg_sign = NULL; |
7998dbe1 PK |
695 | int opt_c = -1; |
696 | struct string_list whitespace = STRING_LIST_INIT_NODUP; | |
68e46d78 | 697 | struct string_list exec = STRING_LIST_INIT_NODUP; |
3c3588c7 | 698 | const char *rebase_merges = NULL; |
92d0d74e | 699 | int fork_point = -1; |
ba1905a5 | 700 | struct string_list strategy_options = STRING_LIST_INIT_NODUP; |
9dba809a PK |
701 | struct object_id squash_onto; |
702 | char *squash_onto_name = NULL; | |
f28d40d3 PK |
703 | struct option builtin_rebase_options[] = { |
704 | OPT_STRING(0, "onto", &options.onto_name, | |
705 | N_("revision"), | |
706 | N_("rebase onto given branch instead of upstream")), | |
06e4775a PK |
707 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, |
708 | N_("allow pre-rebase hook to run")), | |
b4c8eb02 PK |
709 | OPT_NEGBIT('q', "quiet", &options.flags, |
710 | N_("be quiet. implies --no-stat"), | |
bff014da PK |
711 | REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT), |
712 | OPT_BIT('v', "verbose", &options.flags, | |
713 | N_("display a diffstat of what changed upstream"), | |
714 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), | |
715 | {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, | |
716 | N_("do not show diffstat of what changed upstream"), | |
717 | PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, | |
99d8cc7c PK |
718 | OPT_BOOL(0, "ignore-whitespace", &ignore_whitespace, |
719 | N_("passed to 'git apply'")), | |
73d51ed0 PK |
720 | OPT_BOOL(0, "signoff", &options.signoff, |
721 | N_("add a Signed-off-by: line to each commit")), | |
38dbcef2 PK |
722 | OPT_BOOL(0, "committer-date-is-author-date", |
723 | &committer_date_is_author_date, | |
724 | N_("passed to 'git am'")), | |
53f9e5be PK |
725 | OPT_BOOL(0, "ignore-date", &ignore_date, |
726 | N_("passed to 'git am'")), | |
1ed9c14f PK |
727 | OPT_BIT('f', "force-rebase", &options.flags, |
728 | N_("cherry-pick all commits, even if unchanged"), | |
729 | REBASE_FORCE), | |
730 | OPT_BIT(0, "no-ff", &options.flags, | |
731 | N_("cherry-pick all commits, even if unchanged"), | |
732 | REBASE_FORCE), | |
f9573628 PK |
733 | OPT_CMDMODE(0, "continue", &action, N_("continue"), |
734 | ACTION_CONTINUE), | |
122420c2 PK |
735 | OPT_CMDMODE(0, "skip", &action, |
736 | N_("skip current patch and continue"), ACTION_SKIP), | |
5e5d9619 PK |
737 | OPT_CMDMODE(0, "abort", &action, |
738 | N_("abort and check out the original branch"), | |
739 | ACTION_ABORT), | |
5a614945 PK |
740 | OPT_CMDMODE(0, "quit", &action, |
741 | N_("abort but keep HEAD where it is"), ACTION_QUIT), | |
51e9ea6d PK |
742 | OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list " |
743 | "during an interactive rebase"), ACTION_EDIT_TODO), | |
744 | OPT_CMDMODE(0, "show-current-patch", &action, | |
745 | N_("show the patch file being applied or merged"), | |
746 | ACTION_SHOW_CURRENT_PATCH), | |
361badd3 PK |
747 | { OPTION_CALLBACK, 'm', "merge", &options, NULL, |
748 | N_("use merging strategies to rebase"), | |
749 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
750 | parse_opt_merge }, | |
751 | { OPTION_CALLBACK, 'i', "interactive", &options, NULL, | |
752 | N_("let the user edit the list of commits to rebase"), | |
753 | PARSE_OPT_NOARG | PARSE_OPT_NONEG, | |
754 | parse_opt_interactive }, | |
755 | OPT_SET_INT('p', "preserve-merges", &options.type, | |
756 | N_("try to recreate merges instead of ignoring " | |
757 | "them"), REBASE_PRESERVE_MERGES), | |
ead98c11 PK |
758 | OPT_BOOL(0, "rerere-autoupdate", |
759 | &options.allow_rerere_autoupdate, | |
760 | N_("allow rerere to update index with resolved " | |
761 | "conflict")), | |
002ee2fe PK |
762 | OPT_BOOL('k', "keep-empty", &options.keep_empty, |
763 | N_("preserve empty commits during rebase")), | |
051910a9 PK |
764 | OPT_BOOL(0, "autosquash", &options.autosquash, |
765 | N_("move commits that begin with " | |
766 | "squash!/fixup! under -i")), | |
12026a41 PK |
767 | { OPTION_STRING, 'S', "gpg-sign", &gpg_sign, N_("key-id"), |
768 | N_("GPG-sign commits"), | |
769 | PARSE_OPT_OPTARG, NULL, (intptr_t) "" }, | |
7998dbe1 PK |
770 | OPT_STRING_LIST(0, "whitespace", &whitespace, |
771 | N_("whitespace"), N_("passed to 'git apply'")), | |
772 | OPT_SET_INT('C', NULL, &opt_c, N_("passed to 'git apply'"), | |
773 | REBASE_AM), | |
6defce2b PK |
774 | OPT_BOOL(0, "autostash", &options.autostash, |
775 | N_("automatically stash/stash pop before and after")), | |
68e46d78 PK |
776 | OPT_STRING_LIST('x', "exec", &exec, N_("exec"), |
777 | N_("add exec lines after each commit of the " | |
778 | "editable list")), | |
9b3a448b PK |
779 | OPT_BOOL(0, "allow-empty-message", |
780 | &options.allow_empty_message, | |
781 | N_("allow rebasing commits with empty messages")), | |
3c3588c7 PK |
782 | {OPTION_STRING, 'r', "rebase-merges", &rebase_merges, |
783 | N_("mode"), | |
784 | N_("try to rebase merges instead of skipping them"), | |
785 | PARSE_OPT_OPTARG, NULL, (intptr_t)""}, | |
92d0d74e PK |
786 | OPT_BOOL(0, "fork-point", &fork_point, |
787 | N_("use 'merge-base --fork-point' to refine upstream")), | |
ba1905a5 PK |
788 | OPT_STRING('s', "strategy", &options.strategy, |
789 | N_("strategy"), N_("use the given merge strategy")), | |
790 | OPT_STRING_LIST('X', "strategy-option", &strategy_options, | |
791 | N_("option"), | |
792 | N_("pass the argument through to the merge " | |
793 | "strategy")), | |
9dba809a PK |
794 | OPT_BOOL(0, "root", &options.root, |
795 | N_("rebase all reachable commits up to the root(s)")), | |
f28d40d3 PK |
796 | OPT_END(), |
797 | }; | |
ac7f467f | 798 | |
55071ea2 PK |
799 | /* |
800 | * NEEDSWORK: Once the builtin rebase has been tested enough | |
801 | * and git-legacy-rebase.sh is retired to contrib/, this preamble | |
802 | * can be removed. | |
803 | */ | |
804 | ||
805 | if (!use_builtin_rebase()) { | |
806 | const char *path = mkpath("%s/git-legacy-rebase", | |
807 | git_exec_path()); | |
808 | ||
809 | if (sane_execvp(path, (char **)argv) < 0) | |
810 | die_errno(_("could not exec %s"), path); | |
811 | else | |
812 | BUG("sane_execvp() returned???"); | |
813 | } | |
814 | ||
f28d40d3 PK |
815 | if (argc == 2 && !strcmp(argv[1], "-h")) |
816 | usage_with_options(builtin_rebase_usage, | |
817 | builtin_rebase_options); | |
818 | ||
55071ea2 PK |
819 | prefix = setup_git_directory(); |
820 | trace_repo_setup(prefix); | |
821 | setup_work_tree(); | |
822 | ||
bff014da PK |
823 | git_config(rebase_config, &options); |
824 | ||
0eabf4b9 PK |
825 | strbuf_reset(&buf); |
826 | strbuf_addf(&buf, "%s/applying", apply_dir()); | |
827 | if(file_exists(buf.buf)) | |
828 | die(_("It looks like 'git am' is in progress. Cannot rebase.")); | |
829 | ||
c54dacb5 PK |
830 | if (is_directory(apply_dir())) { |
831 | options.type = REBASE_AM; | |
832 | options.state_dir = apply_dir(); | |
833 | } else if (is_directory(merge_dir())) { | |
834 | strbuf_reset(&buf); | |
835 | strbuf_addf(&buf, "%s/rewritten", merge_dir()); | |
836 | if (is_directory(buf.buf)) { | |
837 | options.type = REBASE_PRESERVE_MERGES; | |
838 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
839 | } else { | |
840 | strbuf_reset(&buf); | |
841 | strbuf_addf(&buf, "%s/interactive", merge_dir()); | |
842 | if(file_exists(buf.buf)) { | |
843 | options.type = REBASE_INTERACTIVE; | |
844 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
845 | } else | |
846 | options.type = REBASE_MERGE; | |
847 | } | |
848 | options.state_dir = merge_dir(); | |
849 | } | |
850 | ||
851 | if (options.type != REBASE_UNSPECIFIED) | |
852 | in_progress = 1; | |
853 | ||
f9573628 | 854 | total_argc = argc; |
f28d40d3 PK |
855 | argc = parse_options(argc, argv, prefix, |
856 | builtin_rebase_options, | |
857 | builtin_rebase_usage, 0); | |
858 | ||
f9573628 PK |
859 | if (action != NO_ACTION && total_argc != 2) { |
860 | usage_with_options(builtin_rebase_usage, | |
861 | builtin_rebase_options); | |
862 | } | |
863 | ||
f28d40d3 PK |
864 | if (argc > 2) |
865 | usage_with_options(builtin_rebase_usage, | |
866 | builtin_rebase_options); | |
ac7f467f | 867 | |
d732a570 PK |
868 | if (action != NO_ACTION && !in_progress) |
869 | die(_("No rebase in progress?")); | |
870 | ||
51e9ea6d PK |
871 | if (action == ACTION_EDIT_TODO && !is_interactive(&options)) |
872 | die(_("The --edit-todo action can only be used during " | |
873 | "interactive rebase.")); | |
874 | ||
f9573628 PK |
875 | switch (action) { |
876 | case ACTION_CONTINUE: { | |
877 | struct object_id head; | |
878 | struct lock_file lock_file = LOCK_INIT; | |
879 | int fd; | |
880 | ||
881 | options.action = "continue"; | |
882 | ||
883 | /* Sanity check */ | |
884 | if (get_oid("HEAD", &head)) | |
885 | die(_("Cannot read HEAD")); | |
886 | ||
887 | fd = hold_locked_index(&lock_file, 0); | |
888 | if (read_index(the_repository->index) < 0) | |
889 | die(_("could not read index")); | |
890 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, | |
891 | NULL); | |
892 | if (0 <= fd) | |
893 | update_index_if_able(the_repository->index, | |
894 | &lock_file); | |
895 | rollback_lock_file(&lock_file); | |
896 | ||
897 | if (has_unstaged_changes(1)) { | |
898 | puts(_("You must edit all merge conflicts and then\n" | |
899 | "mark them as resolved using git add")); | |
900 | exit(1); | |
901 | } | |
902 | if (read_basic_state(&options)) | |
903 | exit(1); | |
904 | goto run_rebase; | |
905 | } | |
122420c2 PK |
906 | case ACTION_SKIP: { |
907 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
908 | ||
909 | options.action = "skip"; | |
910 | ||
911 | rerere_clear(&merge_rr); | |
912 | string_list_clear(&merge_rr, 1); | |
913 | ||
fa443d40 | 914 | if (reset_head(NULL, "reset", NULL, 0, NULL, NULL) < 0) |
122420c2 PK |
915 | die(_("could not discard worktree changes")); |
916 | if (read_basic_state(&options)) | |
917 | exit(1); | |
918 | goto run_rebase; | |
919 | } | |
5e5d9619 PK |
920 | case ACTION_ABORT: { |
921 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
922 | options.action = "abort"; | |
923 | ||
924 | rerere_clear(&merge_rr); | |
925 | string_list_clear(&merge_rr, 1); | |
926 | ||
927 | if (read_basic_state(&options)) | |
928 | exit(1); | |
929 | if (reset_head(&options.orig_head, "reset", | |
fa443d40 | 930 | options.head_name, 0, NULL, NULL) < 0) |
5e5d9619 PK |
931 | die(_("could not move back to %s"), |
932 | oid_to_hex(&options.orig_head)); | |
933 | ret = finish_rebase(&options); | |
934 | goto cleanup; | |
935 | } | |
5a614945 PK |
936 | case ACTION_QUIT: { |
937 | strbuf_reset(&buf); | |
938 | strbuf_addstr(&buf, options.state_dir); | |
939 | ret = !!remove_dir_recursively(&buf, 0); | |
940 | if (ret) | |
941 | die(_("could not remove '%s'"), options.state_dir); | |
942 | goto cleanup; | |
943 | } | |
51e9ea6d PK |
944 | case ACTION_EDIT_TODO: |
945 | options.action = "edit-todo"; | |
946 | options.dont_finish_rebase = 1; | |
947 | goto run_rebase; | |
948 | case ACTION_SHOW_CURRENT_PATCH: | |
949 | options.action = "show-current-patch"; | |
950 | options.dont_finish_rebase = 1; | |
951 | goto run_rebase; | |
952 | case NO_ACTION: | |
953 | break; | |
f9573628 | 954 | default: |
51e9ea6d | 955 | BUG("action: %d", action); |
f9573628 PK |
956 | } |
957 | ||
c54dacb5 PK |
958 | /* Make sure no rebase is in progress */ |
959 | if (in_progress) { | |
960 | const char *last_slash = strrchr(options.state_dir, '/'); | |
961 | const char *state_dir_base = | |
962 | last_slash ? last_slash + 1 : options.state_dir; | |
963 | const char *cmd_live_rebase = | |
964 | "git rebase (--continue | --abort | --skip)"; | |
965 | strbuf_reset(&buf); | |
966 | strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); | |
967 | die(_("It seems that there is already a %s directory, and\n" | |
968 | "I wonder if you are in the middle of another rebase. " | |
969 | "If that is the\n" | |
970 | "case, please try\n\t%s\n" | |
971 | "If that is not the case, please\n\t%s\n" | |
972 | "and run me again. I am stopping in case you still " | |
973 | "have something\n" | |
974 | "valuable there.\n"), | |
975 | state_dir_base, cmd_live_rebase, buf.buf); | |
976 | } | |
977 | ||
b4c8eb02 PK |
978 | if (!(options.flags & REBASE_NO_QUIET)) |
979 | strbuf_addstr(&options.git_am_opt, " -q"); | |
980 | ||
38dbcef2 PK |
981 | if (committer_date_is_author_date) { |
982 | strbuf_addstr(&options.git_am_opt, | |
983 | " --committer-date-is-author-date"); | |
984 | options.flags |= REBASE_FORCE; | |
985 | } | |
986 | ||
99d8cc7c PK |
987 | if (ignore_whitespace) |
988 | strbuf_addstr(&options.git_am_opt, " --ignore-whitespace"); | |
989 | ||
53f9e5be PK |
990 | if (ignore_date) { |
991 | strbuf_addstr(&options.git_am_opt, " --ignore-date"); | |
992 | options.flags |= REBASE_FORCE; | |
993 | } | |
994 | ||
002ee2fe PK |
995 | if (options.keep_empty) |
996 | imply_interactive(&options, "--keep-empty"); | |
997 | ||
12026a41 PK |
998 | if (gpg_sign) { |
999 | free(options.gpg_sign_opt); | |
1000 | options.gpg_sign_opt = xstrfmt("-S%s", gpg_sign); | |
1001 | } | |
1002 | ||
7998dbe1 PK |
1003 | if (opt_c >= 0) |
1004 | strbuf_addf(&options.git_am_opt, " -C%d", opt_c); | |
1005 | ||
1006 | if (whitespace.nr) { | |
1007 | int i; | |
1008 | ||
1009 | for (i = 0; i < whitespace.nr; i++) { | |
1010 | const char *item = whitespace.items[i].string; | |
1011 | ||
1012 | strbuf_addf(&options.git_am_opt, " --whitespace=%s", | |
1013 | item); | |
1014 | ||
1015 | if ((!strcmp(item, "fix")) || (!strcmp(item, "strip"))) | |
1016 | options.flags |= REBASE_FORCE; | |
1017 | } | |
1018 | } | |
1019 | ||
68e46d78 PK |
1020 | if (exec.nr) { |
1021 | int i; | |
1022 | ||
1023 | imply_interactive(&options, "--exec"); | |
1024 | ||
1025 | strbuf_reset(&buf); | |
1026 | for (i = 0; i < exec.nr; i++) | |
1027 | strbuf_addf(&buf, "exec %s\n", exec.items[i].string); | |
1028 | options.cmd = xstrdup(buf.buf); | |
1029 | } | |
1030 | ||
3c3588c7 PK |
1031 | if (rebase_merges) { |
1032 | if (!*rebase_merges) | |
1033 | ; /* default mode; do nothing */ | |
1034 | else if (!strcmp("rebase-cousins", rebase_merges)) | |
1035 | options.rebase_cousins = 1; | |
1036 | else if (strcmp("no-rebase-cousins", rebase_merges)) | |
1037 | die(_("Unknown mode: %s"), rebase_merges); | |
1038 | options.rebase_merges = 1; | |
1039 | imply_interactive(&options, "--rebase-merges"); | |
1040 | } | |
1041 | ||
ba1905a5 PK |
1042 | if (strategy_options.nr) { |
1043 | int i; | |
1044 | ||
1045 | if (!options.strategy) | |
1046 | options.strategy = "recursive"; | |
1047 | ||
1048 | strbuf_reset(&buf); | |
1049 | for (i = 0; i < strategy_options.nr; i++) | |
1050 | strbuf_addf(&buf, " --%s", | |
1051 | strategy_options.items[i].string); | |
1052 | options.strategy_opts = xstrdup(buf.buf); | |
1053 | } | |
1054 | ||
1055 | if (options.strategy) { | |
1056 | options.strategy = xstrdup(options.strategy); | |
1057 | switch (options.type) { | |
1058 | case REBASE_AM: | |
1059 | die(_("--strategy requires --merge or --interactive")); | |
1060 | case REBASE_MERGE: | |
1061 | case REBASE_INTERACTIVE: | |
1062 | case REBASE_PRESERVE_MERGES: | |
1063 | /* compatible */ | |
1064 | break; | |
1065 | case REBASE_UNSPECIFIED: | |
1066 | options.type = REBASE_MERGE; | |
1067 | break; | |
1068 | default: | |
1069 | BUG("unhandled rebase type (%d)", options.type); | |
1070 | } | |
1071 | } | |
1072 | ||
9dba809a PK |
1073 | if (options.root && !options.onto_name) |
1074 | imply_interactive(&options, "--root without --onto"); | |
1075 | ||
cda614e4 PK |
1076 | if (isatty(2) && options.flags & REBASE_NO_QUIET) |
1077 | strbuf_addstr(&options.git_format_patch_opt, " --progress"); | |
1078 | ||
ac7f467f PK |
1079 | switch (options.type) { |
1080 | case REBASE_MERGE: | |
1081 | case REBASE_INTERACTIVE: | |
1082 | case REBASE_PRESERVE_MERGES: | |
1083 | options.state_dir = merge_dir(); | |
1084 | break; | |
1085 | case REBASE_AM: | |
1086 | options.state_dir = apply_dir(); | |
1087 | break; | |
1088 | default: | |
1089 | /* the default rebase backend is `--am` */ | |
1090 | options.type = REBASE_AM; | |
1091 | options.state_dir = apply_dir(); | |
1092 | break; | |
1093 | } | |
1094 | ||
73d51ed0 PK |
1095 | if (options.signoff) { |
1096 | if (options.type == REBASE_PRESERVE_MERGES) | |
1097 | die("cannot combine '--signoff' with " | |
1098 | "'--preserve-merges'"); | |
1099 | strbuf_addstr(&options.git_am_opt, " --signoff"); | |
1100 | options.flags |= REBASE_FORCE; | |
1101 | } | |
1102 | ||
ac7f467f | 1103 | if (!options.root) { |
8f5986d9 PK |
1104 | if (argc < 1) { |
1105 | struct branch *branch; | |
1106 | ||
1107 | branch = branch_get(NULL); | |
1108 | options.upstream_name = branch_get_upstream(branch, | |
1109 | NULL); | |
1110 | if (!options.upstream_name) | |
1111 | error_on_missing_default_upstream(); | |
1112 | if (fork_point < 0) | |
1113 | fork_point = 1; | |
1114 | } else { | |
f28d40d3 | 1115 | options.upstream_name = argv[0]; |
ac7f467f PK |
1116 | argc--; |
1117 | argv++; | |
1118 | if (!strcmp(options.upstream_name, "-")) | |
1119 | options.upstream_name = "@{-1}"; | |
1120 | } | |
1121 | options.upstream = peel_committish(options.upstream_name); | |
1122 | if (!options.upstream) | |
1123 | die(_("invalid upstream '%s'"), options.upstream_name); | |
06e4775a | 1124 | options.upstream_arg = options.upstream_name; |
9dba809a PK |
1125 | } else { |
1126 | if (!options.onto_name) { | |
1127 | if (commit_tree("", 0, the_hash_algo->empty_tree, NULL, | |
1128 | &squash_onto, NULL, NULL) < 0) | |
1129 | die(_("Could not create new root commit")); | |
1130 | options.squash_onto = &squash_onto; | |
1131 | options.onto_name = squash_onto_name = | |
1132 | xstrdup(oid_to_hex(&squash_onto)); | |
1133 | } | |
1134 | options.upstream_name = NULL; | |
1135 | options.upstream = NULL; | |
1136 | if (argc > 1) | |
1137 | usage_with_options(builtin_rebase_usage, | |
1138 | builtin_rebase_options); | |
1139 | options.upstream_arg = "--root"; | |
1140 | } | |
ac7f467f PK |
1141 | |
1142 | /* Make sure the branch to rebase onto is valid. */ | |
1143 | if (!options.onto_name) | |
1144 | options.onto_name = options.upstream_name; | |
1145 | if (strstr(options.onto_name, "...")) { | |
075bc852 PK |
1146 | if (get_oid_mb(options.onto_name, &merge_base) < 0) |
1147 | die(_("'%s': need exactly one merge base"), | |
1148 | options.onto_name); | |
1149 | options.onto = lookup_commit_or_die(&merge_base, | |
1150 | options.onto_name); | |
ac7f467f PK |
1151 | } else { |
1152 | options.onto = peel_committish(options.onto_name); | |
1153 | if (!options.onto) | |
1154 | die(_("Does not point to a valid commit '%s'"), | |
1155 | options.onto_name); | |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * If the branch to rebase is given, that is the branch we will rebase | |
1160 | * branch_name -- branch/commit being rebased, or | |
1161 | * HEAD (already detached) | |
1162 | * orig_head -- commit object name of tip of the branch before rebasing | |
d4c569f8 | 1163 | * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) |
ac7f467f | 1164 | */ |
e65123a7 PK |
1165 | if (argc == 1) { |
1166 | /* Is it "rebase other branchname" or "rebase other commit"? */ | |
1167 | branch_name = argv[0]; | |
1168 | options.switch_to = argv[0]; | |
1169 | ||
1170 | /* Is it a local branch? */ | |
1171 | strbuf_reset(&buf); | |
1172 | strbuf_addf(&buf, "refs/heads/%s", branch_name); | |
1173 | if (!read_ref(buf.buf, &options.orig_head)) | |
1174 | options.head_name = xstrdup(buf.buf); | |
1175 | /* If not is it a valid ref (branch or commit)? */ | |
1176 | else if (!get_oid(branch_name, &options.orig_head)) | |
1177 | options.head_name = NULL; | |
1178 | else | |
1179 | die(_("fatal: no such branch/commit '%s'"), | |
1180 | branch_name); | |
1181 | } else if (argc == 0) { | |
ac7f467f PK |
1182 | /* Do not need to switch branches, we are already on it. */ |
1183 | options.head_name = | |
1184 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, | |
1185 | &flags)); | |
1186 | if (!options.head_name) | |
1187 | die(_("No such ref: %s"), "HEAD"); | |
1188 | if (flags & REF_ISSYMREF) { | |
1189 | if (!skip_prefix(options.head_name, | |
1190 | "refs/heads/", &branch_name)) | |
1191 | branch_name = options.head_name; | |
1192 | ||
1193 | } else { | |
d4c569f8 PK |
1194 | free(options.head_name); |
1195 | options.head_name = NULL; | |
ac7f467f PK |
1196 | branch_name = "HEAD"; |
1197 | } | |
1198 | if (get_oid("HEAD", &options.orig_head)) | |
1199 | die(_("Could not resolve HEAD to a revision")); | |
e65123a7 PK |
1200 | } else |
1201 | BUG("unexpected number of arguments left to parse"); | |
ac7f467f | 1202 | |
92d0d74e PK |
1203 | if (fork_point > 0) { |
1204 | struct commit *head = | |
1205 | lookup_commit_reference(the_repository, | |
1206 | &options.orig_head); | |
1207 | options.restrict_revision = | |
1208 | get_fork_point(options.upstream_name, head); | |
1209 | } | |
1210 | ||
e0333e5c PK |
1211 | if (read_index(the_repository->index) < 0) |
1212 | die(_("could not read index")); | |
1213 | ||
6defce2b PK |
1214 | if (options.autostash) { |
1215 | struct lock_file lock_file = LOCK_INIT; | |
1216 | int fd; | |
1217 | ||
1218 | fd = hold_locked_index(&lock_file, 0); | |
1219 | refresh_cache(REFRESH_QUIET); | |
1220 | if (0 <= fd) | |
1221 | update_index_if_able(&the_index, &lock_file); | |
1222 | rollback_lock_file(&lock_file); | |
1223 | ||
1224 | if (has_unstaged_changes(0) || has_uncommitted_changes(0)) { | |
1225 | const char *autostash = | |
1226 | state_dir_path("autostash", &options); | |
1227 | struct child_process stash = CHILD_PROCESS_INIT; | |
1228 | struct object_id oid; | |
1229 | struct commit *head = | |
1230 | lookup_commit_reference(the_repository, | |
1231 | &options.orig_head); | |
1232 | ||
1233 | argv_array_pushl(&stash.args, | |
1234 | "stash", "create", "autostash", NULL); | |
1235 | stash.git_cmd = 1; | |
1236 | stash.no_stdin = 1; | |
1237 | strbuf_reset(&buf); | |
1238 | if (capture_command(&stash, &buf, GIT_MAX_HEXSZ)) | |
1239 | die(_("Cannot autostash")); | |
1240 | strbuf_trim_trailing_newline(&buf); | |
1241 | if (get_oid(buf.buf, &oid)) | |
1242 | die(_("Unexpected stash response: '%s'"), | |
1243 | buf.buf); | |
1244 | strbuf_reset(&buf); | |
1245 | strbuf_add_unique_abbrev(&buf, &oid, DEFAULT_ABBREV); | |
1246 | ||
1247 | if (safe_create_leading_directories_const(autostash)) | |
1248 | die(_("Could not create directory for '%s'"), | |
1249 | options.state_dir); | |
1250 | write_file(autostash, "%s", buf.buf); | |
1251 | printf(_("Created autostash: %s\n"), buf.buf); | |
1252 | if (reset_head(&head->object.oid, "reset --hard", | |
fa443d40 | 1253 | NULL, 0, NULL, NULL) < 0) |
6defce2b PK |
1254 | die(_("could not reset --hard")); |
1255 | printf(_("HEAD is now at %s"), | |
1256 | find_unique_abbrev(&head->object.oid, | |
1257 | DEFAULT_ABBREV)); | |
1258 | strbuf_reset(&buf); | |
1259 | pp_commit_easy(CMIT_FMT_ONELINE, head, &buf); | |
1260 | if (buf.len > 0) | |
1261 | printf(" %s", buf.buf); | |
1262 | putchar('\n'); | |
1263 | ||
1264 | if (discard_index(the_repository->index) < 0 || | |
1265 | read_index(the_repository->index) < 0) | |
1266 | die(_("could not read index")); | |
1267 | } | |
1268 | } | |
1269 | ||
e0333e5c PK |
1270 | if (require_clean_work_tree("rebase", |
1271 | _("Please commit or stash them."), 1, 1)) { | |
1272 | ret = 1; | |
1273 | goto cleanup; | |
1274 | } | |
1275 | ||
9a48a615 PK |
1276 | /* |
1277 | * Now we are rebasing commits upstream..orig_head (or with --root, | |
1278 | * everything leading up to orig_head) on top of onto. | |
1279 | */ | |
1280 | ||
1281 | /* | |
1282 | * Check if we are already based on onto with linear history, | |
1283 | * but this should be done only when upstream and onto are the same | |
1284 | * and if this is not an interactive rebase. | |
1285 | */ | |
1286 | if (can_fast_forward(options.onto, &options.orig_head, &merge_base) && | |
1287 | !is_interactive(&options) && !options.restrict_revision && | |
9dba809a | 1288 | options.upstream && |
9a48a615 PK |
1289 | !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { |
1290 | int flag; | |
1291 | ||
1ed9c14f | 1292 | if (!(options.flags & REBASE_FORCE)) { |
e65123a7 PK |
1293 | /* Lazily switch to the target branch if needed... */ |
1294 | if (options.switch_to) { | |
1295 | struct object_id oid; | |
1296 | ||
1297 | if (get_oid(options.switch_to, &oid) < 0) { | |
1298 | ret = !!error(_("could not parse '%s'"), | |
1299 | options.switch_to); | |
1300 | goto cleanup; | |
1301 | } | |
1302 | ||
1303 | strbuf_reset(&buf); | |
1304 | strbuf_addf(&buf, "rebase: checkout %s", | |
1305 | options.switch_to); | |
1306 | if (reset_head(&oid, "checkout", | |
fa443d40 PK |
1307 | options.head_name, 0, |
1308 | NULL, NULL) < 0) { | |
e65123a7 PK |
1309 | ret = !!error(_("could not switch to " |
1310 | "%s"), | |
1311 | options.switch_to); | |
1312 | goto cleanup; | |
1313 | } | |
1314 | } | |
1315 | ||
1ed9c14f PK |
1316 | if (!(options.flags & REBASE_NO_QUIET)) |
1317 | ; /* be quiet */ | |
1318 | else if (!strcmp(branch_name, "HEAD") && | |
1319 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
1320 | puts(_("HEAD is up to date.")); | |
1321 | else | |
1322 | printf(_("Current branch %s is up to date.\n"), | |
1323 | branch_name); | |
1324 | ret = !!finish_rebase(&options); | |
1325 | goto cleanup; | |
1326 | } else if (!(options.flags & REBASE_NO_QUIET)) | |
9a48a615 PK |
1327 | ; /* be quiet */ |
1328 | else if (!strcmp(branch_name, "HEAD") && | |
1329 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
1330 | puts(_("HEAD is up to date, rebase forced.")); | |
1331 | else | |
1332 | printf(_("Current branch %s is up to date, rebase " | |
1333 | "forced.\n"), branch_name); | |
1334 | } | |
1335 | ||
06e4775a PK |
1336 | /* If a hook exists, give it a chance to interrupt*/ |
1337 | if (!ok_to_skip_pre_rebase && | |
1338 | run_hook_le(NULL, "pre-rebase", options.upstream_arg, | |
1339 | argc ? argv[0] : NULL, NULL)) | |
1340 | die(_("The pre-rebase hook refused to rebase.")); | |
1341 | ||
bff014da PK |
1342 | if (options.flags & REBASE_DIFFSTAT) { |
1343 | struct diff_options opts; | |
1344 | ||
1345 | if (options.flags & REBASE_VERBOSE) | |
1346 | printf(_("Changes from %s to %s:\n"), | |
1347 | oid_to_hex(&merge_base), | |
1348 | oid_to_hex(&options.onto->object.oid)); | |
1349 | ||
1350 | /* We want color (if set), but no pager */ | |
1351 | diff_setup(&opts); | |
1352 | opts.stat_width = -1; /* use full terminal width */ | |
1353 | opts.stat_graph_width = -1; /* respect statGraphWidth config */ | |
1354 | opts.output_format |= | |
1355 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
1356 | opts.detect_rename = DIFF_DETECT_RENAME; | |
1357 | diff_setup_done(&opts); | |
1358 | diff_tree_oid(&merge_base, &options.onto->object.oid, | |
1359 | "", &opts); | |
1360 | diffcore_std(&opts); | |
1361 | diff_flush(&opts); | |
1362 | } | |
1363 | ||
361badd3 PK |
1364 | if (is_interactive(&options)) |
1365 | goto run_rebase; | |
1366 | ||
bff014da PK |
1367 | /* Detach HEAD and reset the tree */ |
1368 | if (options.flags & REBASE_NO_QUIET) | |
1369 | printf(_("First, rewinding head to replay your work on top of " | |
1370 | "it...\n")); | |
1371 | ||
ac7f467f | 1372 | strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); |
fa443d40 PK |
1373 | if (reset_head(&options.onto->object.oid, "checkout", NULL, 1, |
1374 | NULL, msg.buf)) | |
ac7f467f PK |
1375 | die(_("Could not detach HEAD")); |
1376 | strbuf_release(&msg); | |
1377 | ||
7eecfa56 PK |
1378 | /* |
1379 | * If the onto is a proper descendant of the tip of the branch, then | |
1380 | * we just fast-forwarded. | |
1381 | */ | |
1382 | strbuf_reset(&msg); | |
1383 | if (!oidcmp(&merge_base, &options.orig_head)) { | |
1384 | printf(_("Fast-forwarded %s to %s. \n"), | |
1385 | branch_name, options.onto_name); | |
1386 | strbuf_addf(&msg, "rebase finished: %s onto %s", | |
1387 | options.head_name ? options.head_name : "detached HEAD", | |
1388 | oid_to_hex(&options.onto->object.oid)); | |
1389 | reset_head(NULL, "Fast-forwarded", options.head_name, 0, | |
1390 | "HEAD", msg.buf); | |
1391 | strbuf_release(&msg); | |
1392 | ret = !!finish_rebase(&options); | |
1393 | goto cleanup; | |
1394 | } | |
1395 | ||
ac7f467f PK |
1396 | strbuf_addf(&revisions, "%s..%s", |
1397 | options.root ? oid_to_hex(&options.onto->object.oid) : | |
1398 | (options.restrict_revision ? | |
1399 | oid_to_hex(&options.restrict_revision->object.oid) : | |
1400 | oid_to_hex(&options.upstream->object.oid)), | |
1401 | oid_to_hex(&options.orig_head)); | |
1402 | ||
1403 | options.revisions = revisions.buf; | |
1404 | ||
f9573628 | 1405 | run_rebase: |
ac7f467f PK |
1406 | ret = !!run_specific_rebase(&options); |
1407 | ||
e0333e5c | 1408 | cleanup: |
ac7f467f PK |
1409 | strbuf_release(&revisions); |
1410 | free(options.head_name); | |
12026a41 | 1411 | free(options.gpg_sign_opt); |
68e46d78 | 1412 | free(options.cmd); |
9dba809a | 1413 | free(squash_onto_name); |
ac7f467f | 1414 | return ret; |
55071ea2 | 1415 | } |