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