]>
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 | static int apply_autostash(void) |
67 | { | |
68 | warning("TODO"); | |
69 | return 0; | |
70 | } | |
71 | ||
72 | struct rebase_options { | |
73 | enum rebase_type type; | |
74 | const char *state_dir; | |
75 | struct commit *upstream; | |
76 | const char *upstream_name; | |
06e4775a | 77 | const char *upstream_arg; |
ac7f467f PK |
78 | char *head_name; |
79 | struct object_id orig_head; | |
80 | struct commit *onto; | |
81 | const char *onto_name; | |
82 | const char *revisions; | |
e65123a7 | 83 | const char *switch_to; |
ac7f467f PK |
84 | int root; |
85 | struct commit *restrict_revision; | |
86 | int dont_finish_rebase; | |
b4c8eb02 PK |
87 | enum { |
88 | REBASE_NO_QUIET = 1<<0, | |
bff014da PK |
89 | REBASE_VERBOSE = 1<<1, |
90 | REBASE_DIFFSTAT = 1<<2, | |
1ed9c14f | 91 | REBASE_FORCE = 1<<3, |
c54dacb5 | 92 | REBASE_INTERACTIVE_EXPLICIT = 1<<4, |
b4c8eb02 PK |
93 | } flags; |
94 | struct strbuf git_am_opt; | |
f9573628 | 95 | const char *action; |
ac7f467f PK |
96 | }; |
97 | ||
9a48a615 PK |
98 | static int is_interactive(struct rebase_options *opts) |
99 | { | |
100 | return opts->type == REBASE_INTERACTIVE || | |
101 | opts->type == REBASE_PRESERVE_MERGES; | |
102 | } | |
103 | ||
ac7f467f PK |
104 | /* Returns the filename prefixed by the state_dir */ |
105 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) | |
106 | { | |
107 | static struct strbuf path = STRBUF_INIT; | |
108 | static size_t prefix_len; | |
109 | ||
110 | if (!prefix_len) { | |
111 | strbuf_addf(&path, "%s/", opts->state_dir); | |
112 | prefix_len = path.len; | |
113 | } | |
114 | ||
115 | strbuf_setlen(&path, prefix_len); | |
116 | strbuf_addstr(&path, filename); | |
117 | return path.buf; | |
118 | } | |
119 | ||
f9573628 PK |
120 | /* Read one file, then strip line endings */ |
121 | static int read_one(const char *path, struct strbuf *buf) | |
122 | { | |
123 | if (strbuf_read_file(buf, path, 0) < 0) | |
124 | return error_errno(_("could not read '%s'"), path); | |
125 | strbuf_trim_trailing_newline(buf); | |
126 | return 0; | |
127 | } | |
128 | ||
129 | /* Initialize the rebase options from the state directory. */ | |
130 | static int read_basic_state(struct rebase_options *opts) | |
131 | { | |
132 | struct strbuf head_name = STRBUF_INIT; | |
133 | struct strbuf buf = STRBUF_INIT; | |
134 | struct object_id oid; | |
135 | ||
136 | if (read_one(state_dir_path("head-name", opts), &head_name) || | |
137 | read_one(state_dir_path("onto", opts), &buf)) | |
138 | return -1; | |
139 | opts->head_name = starts_with(head_name.buf, "refs/") ? | |
140 | xstrdup(head_name.buf) : NULL; | |
141 | strbuf_release(&head_name); | |
142 | if (get_oid(buf.buf, &oid)) | |
143 | return error(_("could not get 'onto': '%s'"), buf.buf); | |
144 | opts->onto = lookup_commit_or_die(&oid, buf.buf); | |
145 | ||
146 | /* | |
147 | * We always write to orig-head, but interactive rebase used to write to | |
148 | * head. Fall back to reading from head to cover for the case that the | |
149 | * user upgraded git with an ongoing interactive rebase. | |
150 | */ | |
151 | strbuf_reset(&buf); | |
152 | if (file_exists(state_dir_path("orig-head", opts))) { | |
153 | if (read_one(state_dir_path("orig-head", opts), &buf)) | |
154 | return -1; | |
155 | } else if (read_one(state_dir_path("head", opts), &buf)) | |
156 | return -1; | |
157 | if (get_oid(buf.buf, &opts->orig_head)) | |
158 | return error(_("invalid orig-head: '%s'"), buf.buf); | |
159 | ||
160 | strbuf_reset(&buf); | |
161 | if (read_one(state_dir_path("quiet", opts), &buf)) | |
162 | return -1; | |
163 | if (buf.len) | |
164 | opts->flags &= ~REBASE_NO_QUIET; | |
165 | else | |
166 | opts->flags |= REBASE_NO_QUIET; | |
167 | ||
168 | if (file_exists(state_dir_path("verbose", opts))) | |
169 | opts->flags |= REBASE_VERBOSE; | |
170 | ||
171 | strbuf_release(&buf); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
ac7f467f PK |
176 | static int finish_rebase(struct rebase_options *opts) |
177 | { | |
178 | struct strbuf dir = STRBUF_INIT; | |
179 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
180 | ||
181 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); | |
182 | apply_autostash(); | |
183 | close_all_packs(the_repository->objects); | |
184 | /* | |
185 | * We ignore errors in 'gc --auto', since the | |
186 | * user should see them. | |
187 | */ | |
188 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
189 | strbuf_addstr(&dir, opts->state_dir); | |
190 | remove_dir_recursively(&dir, 0); | |
191 | strbuf_release(&dir); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static struct commit *peel_committish(const char *name) | |
197 | { | |
198 | struct object *obj; | |
199 | struct object_id oid; | |
200 | ||
201 | if (get_oid(name, &oid)) | |
202 | return NULL; | |
203 | obj = parse_object(the_repository, &oid); | |
204 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); | |
205 | } | |
206 | ||
207 | static void add_var(struct strbuf *buf, const char *name, const char *value) | |
208 | { | |
209 | if (!value) | |
210 | strbuf_addf(buf, "unset %s; ", name); | |
211 | else { | |
212 | strbuf_addf(buf, "%s=", name); | |
213 | sq_quote_buf(buf, value); | |
214 | strbuf_addstr(buf, "; "); | |
215 | } | |
216 | } | |
217 | ||
218 | static int run_specific_rebase(struct rebase_options *opts) | |
219 | { | |
220 | const char *argv[] = { NULL, NULL }; | |
221 | struct strbuf script_snippet = STRBUF_INIT; | |
222 | int status; | |
223 | const char *backend, *backend_func; | |
224 | ||
225 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); | |
226 | add_var(&script_snippet, "state_dir", opts->state_dir); | |
227 | ||
228 | add_var(&script_snippet, "upstream_name", opts->upstream_name); | |
f9573628 PK |
229 | add_var(&script_snippet, "upstream", opts->upstream ? |
230 | oid_to_hex(&opts->upstream->object.oid) : NULL); | |
d4c569f8 PK |
231 | add_var(&script_snippet, "head_name", |
232 | opts->head_name ? opts->head_name : "detached HEAD"); | |
ac7f467f | 233 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); |
f9573628 PK |
234 | add_var(&script_snippet, "onto", opts->onto ? |
235 | oid_to_hex(&opts->onto->object.oid) : NULL); | |
ac7f467f PK |
236 | add_var(&script_snippet, "onto_name", opts->onto_name); |
237 | add_var(&script_snippet, "revisions", opts->revisions); | |
238 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? | |
239 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); | |
b4c8eb02 PK |
240 | add_var(&script_snippet, "GIT_QUIET", |
241 | opts->flags & REBASE_NO_QUIET ? "" : "t"); | |
242 | add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf); | |
bff014da PK |
243 | add_var(&script_snippet, "verbose", |
244 | opts->flags & REBASE_VERBOSE ? "t" : ""); | |
245 | add_var(&script_snippet, "diffstat", | |
246 | opts->flags & REBASE_DIFFSTAT ? "t" : ""); | |
1ed9c14f PK |
247 | add_var(&script_snippet, "force_rebase", |
248 | opts->flags & REBASE_FORCE ? "t" : ""); | |
e65123a7 PK |
249 | if (opts->switch_to) |
250 | add_var(&script_snippet, "switch_to", opts->switch_to); | |
f9573628 | 251 | add_var(&script_snippet, "action", opts->action ? opts->action : ""); |
ac7f467f PK |
252 | |
253 | switch (opts->type) { | |
254 | case REBASE_AM: | |
255 | backend = "git-rebase--am"; | |
256 | backend_func = "git_rebase__am"; | |
257 | break; | |
258 | case REBASE_INTERACTIVE: | |
259 | backend = "git-rebase--interactive"; | |
260 | backend_func = "git_rebase__interactive"; | |
261 | break; | |
262 | case REBASE_MERGE: | |
263 | backend = "git-rebase--merge"; | |
264 | backend_func = "git_rebase__merge"; | |
265 | break; | |
266 | case REBASE_PRESERVE_MERGES: | |
267 | backend = "git-rebase--preserve-merges"; | |
268 | backend_func = "git_rebase__preserve_merges"; | |
269 | break; | |
270 | default: | |
271 | BUG("Unhandled rebase type %d", opts->type); | |
272 | break; | |
273 | } | |
274 | ||
275 | strbuf_addf(&script_snippet, | |
276 | ". git-sh-setup && . git-rebase--common &&" | |
277 | " . %s && %s", backend, backend_func); | |
278 | argv[0] = script_snippet.buf; | |
279 | ||
280 | status = run_command_v_opt(argv, RUN_USING_SHELL); | |
281 | if (opts->dont_finish_rebase) | |
282 | ; /* do nothing */ | |
283 | else if (status == 0) { | |
284 | if (!file_exists(state_dir_path("stopped-sha", opts))) | |
285 | finish_rebase(opts); | |
286 | } else if (status == 2) { | |
287 | struct strbuf dir = STRBUF_INIT; | |
288 | ||
289 | apply_autostash(); | |
290 | strbuf_addstr(&dir, opts->state_dir); | |
291 | remove_dir_recursively(&dir, 0); | |
292 | strbuf_release(&dir); | |
293 | die("Nothing to do"); | |
294 | } | |
295 | ||
296 | strbuf_release(&script_snippet); | |
297 | ||
298 | return status ? -1 : 0; | |
299 | } | |
300 | ||
301 | #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" | |
302 | ||
303 | static int reset_head(struct object_id *oid, const char *action, | |
304 | const char *switch_to_branch, int detach_head) | |
305 | { | |
306 | struct object_id head_oid; | |
307 | struct tree_desc desc; | |
308 | struct lock_file lock = LOCK_INIT; | |
309 | struct unpack_trees_options unpack_tree_opts; | |
310 | struct tree *tree; | |
311 | const char *reflog_action; | |
312 | struct strbuf msg = STRBUF_INIT; | |
313 | size_t prefix_len; | |
314 | struct object_id *orig = NULL, oid_orig, | |
315 | *old_orig = NULL, oid_old_orig; | |
316 | int ret = 0; | |
317 | ||
d4c569f8 PK |
318 | if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) |
319 | BUG("Not a fully qualified branch: '%s'", switch_to_branch); | |
320 | ||
ac7f467f PK |
321 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) |
322 | return -1; | |
323 | ||
324 | if (!oid) { | |
325 | if (get_oid("HEAD", &head_oid)) { | |
326 | rollback_lock_file(&lock); | |
327 | return error(_("could not determine HEAD revision")); | |
328 | } | |
329 | oid = &head_oid; | |
330 | } | |
331 | ||
332 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); | |
333 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); | |
334 | unpack_tree_opts.head_idx = 1; | |
335 | unpack_tree_opts.src_index = the_repository->index; | |
336 | unpack_tree_opts.dst_index = the_repository->index; | |
337 | unpack_tree_opts.fn = oneway_merge; | |
338 | unpack_tree_opts.update = 1; | |
339 | unpack_tree_opts.merge = 1; | |
340 | if (!detach_head) | |
341 | unpack_tree_opts.reset = 1; | |
342 | ||
343 | if (read_index_unmerged(the_repository->index) < 0) { | |
344 | rollback_lock_file(&lock); | |
345 | return error(_("could not read index")); | |
346 | } | |
347 | ||
348 | if (!fill_tree_descriptor(&desc, oid)) { | |
349 | error(_("failed to find tree of %s"), oid_to_hex(oid)); | |
350 | rollback_lock_file(&lock); | |
351 | free((void *)desc.buffer); | |
352 | return -1; | |
353 | } | |
354 | ||
355 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { | |
356 | rollback_lock_file(&lock); | |
357 | free((void *)desc.buffer); | |
358 | return -1; | |
359 | } | |
360 | ||
361 | tree = parse_tree_indirect(oid); | |
362 | prime_cache_tree(the_repository->index, tree); | |
363 | ||
364 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) | |
365 | ret = error(_("could not write index")); | |
366 | free((void *)desc.buffer); | |
367 | ||
368 | if (ret) | |
369 | return ret; | |
370 | ||
371 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
372 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); | |
373 | prefix_len = msg.len; | |
374 | ||
375 | if (!get_oid("ORIG_HEAD", &oid_old_orig)) | |
376 | old_orig = &oid_old_orig; | |
377 | if (!get_oid("HEAD", &oid_orig)) { | |
378 | orig = &oid_orig; | |
379 | strbuf_addstr(&msg, "updating ORIG_HEAD"); | |
380 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, | |
381 | UPDATE_REFS_MSG_ON_ERR); | |
382 | } else if (old_orig) | |
383 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
384 | strbuf_setlen(&msg, prefix_len); | |
385 | strbuf_addstr(&msg, "updating HEAD"); | |
386 | if (!switch_to_branch) | |
387 | ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF, | |
388 | UPDATE_REFS_MSG_ON_ERR); | |
389 | else { | |
390 | ret = create_symref("HEAD", switch_to_branch, msg.buf); | |
391 | if (!ret) | |
392 | ret = update_ref(msg.buf, "HEAD", oid, NULL, 0, | |
393 | UPDATE_REFS_MSG_ON_ERR); | |
394 | } | |
395 | ||
396 | strbuf_release(&msg); | |
397 | return ret; | |
398 | } | |
399 | ||
bff014da PK |
400 | static int rebase_config(const char *var, const char *value, void *data) |
401 | { | |
402 | struct rebase_options *opts = data; | |
403 | ||
404 | if (!strcmp(var, "rebase.stat")) { | |
405 | if (git_config_bool(var, value)) | |
406 | opts->flags |= REBASE_DIFFSTAT; | |
407 | else | |
408 | opts->flags &= !REBASE_DIFFSTAT; | |
409 | return 0; | |
410 | } | |
411 | ||
412 | return git_default_config(var, value, data); | |
413 | } | |
414 | ||
9a48a615 PK |
415 | /* |
416 | * Determines whether the commits in from..to are linear, i.e. contain | |
417 | * no merge commits. This function *expects* `from` to be an ancestor of | |
418 | * `to`. | |
419 | */ | |
420 | static int is_linear_history(struct commit *from, struct commit *to) | |
421 | { | |
422 | while (to && to != from) { | |
423 | parse_commit(to); | |
424 | if (!to->parents) | |
425 | return 1; | |
426 | if (to->parents->next) | |
427 | return 0; | |
428 | to = to->parents->item; | |
429 | } | |
430 | return 1; | |
431 | } | |
432 | ||
433 | static int can_fast_forward(struct commit *onto, struct object_id *head_oid, | |
434 | struct object_id *merge_base) | |
435 | { | |
436 | struct commit *head = lookup_commit(the_repository, head_oid); | |
437 | struct commit_list *merge_bases; | |
438 | int res; | |
439 | ||
440 | if (!head) | |
441 | return 0; | |
442 | ||
443 | merge_bases = get_merge_bases(onto, head); | |
444 | if (merge_bases && !merge_bases->next) { | |
445 | oidcpy(merge_base, &merge_bases->item->object.oid); | |
446 | res = !oidcmp(merge_base, &onto->object.oid); | |
447 | } else { | |
448 | oidcpy(merge_base, &null_oid); | |
449 | res = 0; | |
450 | } | |
451 | free_commit_list(merge_bases); | |
452 | return res && is_linear_history(onto, head); | |
453 | } | |
454 | ||
55071ea2 PK |
455 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
456 | { | |
ac7f467f PK |
457 | struct rebase_options options = { |
458 | .type = REBASE_UNSPECIFIED, | |
b4c8eb02 PK |
459 | .flags = REBASE_NO_QUIET, |
460 | .git_am_opt = STRBUF_INIT, | |
ac7f467f PK |
461 | }; |
462 | const char *branch_name; | |
f9573628 | 463 | int ret, flags, total_argc, in_progress = 0; |
06e4775a | 464 | int ok_to_skip_pre_rebase = 0; |
ac7f467f PK |
465 | struct strbuf msg = STRBUF_INIT; |
466 | struct strbuf revisions = STRBUF_INIT; | |
c54dacb5 | 467 | struct strbuf buf = STRBUF_INIT; |
075bc852 | 468 | struct object_id merge_base; |
f9573628 PK |
469 | enum { |
470 | NO_ACTION, | |
471 | ACTION_CONTINUE, | |
122420c2 | 472 | ACTION_SKIP, |
5e5d9619 | 473 | ACTION_ABORT, |
5a614945 | 474 | ACTION_QUIT, |
51e9ea6d PK |
475 | ACTION_EDIT_TODO, |
476 | ACTION_SHOW_CURRENT_PATCH, | |
f9573628 | 477 | } action = NO_ACTION; |
f28d40d3 PK |
478 | struct option builtin_rebase_options[] = { |
479 | OPT_STRING(0, "onto", &options.onto_name, | |
480 | N_("revision"), | |
481 | N_("rebase onto given branch instead of upstream")), | |
06e4775a PK |
482 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, |
483 | N_("allow pre-rebase hook to run")), | |
b4c8eb02 PK |
484 | OPT_NEGBIT('q', "quiet", &options.flags, |
485 | N_("be quiet. implies --no-stat"), | |
bff014da PK |
486 | REBASE_NO_QUIET| REBASE_VERBOSE | REBASE_DIFFSTAT), |
487 | OPT_BIT('v', "verbose", &options.flags, | |
488 | N_("display a diffstat of what changed upstream"), | |
489 | REBASE_NO_QUIET | REBASE_VERBOSE | REBASE_DIFFSTAT), | |
490 | {OPTION_NEGBIT, 'n', "no-stat", &options.flags, NULL, | |
491 | N_("do not show diffstat of what changed upstream"), | |
492 | PARSE_OPT_NOARG, NULL, REBASE_DIFFSTAT }, | |
1ed9c14f PK |
493 | OPT_BIT('f', "force-rebase", &options.flags, |
494 | N_("cherry-pick all commits, even if unchanged"), | |
495 | REBASE_FORCE), | |
496 | OPT_BIT(0, "no-ff", &options.flags, | |
497 | N_("cherry-pick all commits, even if unchanged"), | |
498 | REBASE_FORCE), | |
f9573628 PK |
499 | OPT_CMDMODE(0, "continue", &action, N_("continue"), |
500 | ACTION_CONTINUE), | |
122420c2 PK |
501 | OPT_CMDMODE(0, "skip", &action, |
502 | N_("skip current patch and continue"), ACTION_SKIP), | |
5e5d9619 PK |
503 | OPT_CMDMODE(0, "abort", &action, |
504 | N_("abort and check out the original branch"), | |
505 | ACTION_ABORT), | |
5a614945 PK |
506 | OPT_CMDMODE(0, "quit", &action, |
507 | N_("abort but keep HEAD where it is"), ACTION_QUIT), | |
51e9ea6d PK |
508 | OPT_CMDMODE(0, "edit-todo", &action, N_("edit the todo list " |
509 | "during an interactive rebase"), ACTION_EDIT_TODO), | |
510 | OPT_CMDMODE(0, "show-current-patch", &action, | |
511 | N_("show the patch file being applied or merged"), | |
512 | ACTION_SHOW_CURRENT_PATCH), | |
f28d40d3 PK |
513 | OPT_END(), |
514 | }; | |
ac7f467f | 515 | |
55071ea2 PK |
516 | /* |
517 | * NEEDSWORK: Once the builtin rebase has been tested enough | |
518 | * and git-legacy-rebase.sh is retired to contrib/, this preamble | |
519 | * can be removed. | |
520 | */ | |
521 | ||
522 | if (!use_builtin_rebase()) { | |
523 | const char *path = mkpath("%s/git-legacy-rebase", | |
524 | git_exec_path()); | |
525 | ||
526 | if (sane_execvp(path, (char **)argv) < 0) | |
527 | die_errno(_("could not exec %s"), path); | |
528 | else | |
529 | BUG("sane_execvp() returned???"); | |
530 | } | |
531 | ||
f28d40d3 PK |
532 | if (argc == 2 && !strcmp(argv[1], "-h")) |
533 | usage_with_options(builtin_rebase_usage, | |
534 | builtin_rebase_options); | |
535 | ||
55071ea2 PK |
536 | prefix = setup_git_directory(); |
537 | trace_repo_setup(prefix); | |
538 | setup_work_tree(); | |
539 | ||
bff014da PK |
540 | git_config(rebase_config, &options); |
541 | ||
c54dacb5 PK |
542 | if (is_directory(apply_dir())) { |
543 | options.type = REBASE_AM; | |
544 | options.state_dir = apply_dir(); | |
545 | } else if (is_directory(merge_dir())) { | |
546 | strbuf_reset(&buf); | |
547 | strbuf_addf(&buf, "%s/rewritten", merge_dir()); | |
548 | if (is_directory(buf.buf)) { | |
549 | options.type = REBASE_PRESERVE_MERGES; | |
550 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
551 | } else { | |
552 | strbuf_reset(&buf); | |
553 | strbuf_addf(&buf, "%s/interactive", merge_dir()); | |
554 | if(file_exists(buf.buf)) { | |
555 | options.type = REBASE_INTERACTIVE; | |
556 | options.flags |= REBASE_INTERACTIVE_EXPLICIT; | |
557 | } else | |
558 | options.type = REBASE_MERGE; | |
559 | } | |
560 | options.state_dir = merge_dir(); | |
561 | } | |
562 | ||
563 | if (options.type != REBASE_UNSPECIFIED) | |
564 | in_progress = 1; | |
565 | ||
f9573628 | 566 | total_argc = argc; |
f28d40d3 PK |
567 | argc = parse_options(argc, argv, prefix, |
568 | builtin_rebase_options, | |
569 | builtin_rebase_usage, 0); | |
570 | ||
f9573628 PK |
571 | if (action != NO_ACTION && total_argc != 2) { |
572 | usage_with_options(builtin_rebase_usage, | |
573 | builtin_rebase_options); | |
574 | } | |
575 | ||
f28d40d3 PK |
576 | if (argc > 2) |
577 | usage_with_options(builtin_rebase_usage, | |
578 | builtin_rebase_options); | |
ac7f467f | 579 | |
d732a570 PK |
580 | if (action != NO_ACTION && !in_progress) |
581 | die(_("No rebase in progress?")); | |
582 | ||
51e9ea6d PK |
583 | if (action == ACTION_EDIT_TODO && !is_interactive(&options)) |
584 | die(_("The --edit-todo action can only be used during " | |
585 | "interactive rebase.")); | |
586 | ||
f9573628 PK |
587 | switch (action) { |
588 | case ACTION_CONTINUE: { | |
589 | struct object_id head; | |
590 | struct lock_file lock_file = LOCK_INIT; | |
591 | int fd; | |
592 | ||
593 | options.action = "continue"; | |
594 | ||
595 | /* Sanity check */ | |
596 | if (get_oid("HEAD", &head)) | |
597 | die(_("Cannot read HEAD")); | |
598 | ||
599 | fd = hold_locked_index(&lock_file, 0); | |
600 | if (read_index(the_repository->index) < 0) | |
601 | die(_("could not read index")); | |
602 | refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, | |
603 | NULL); | |
604 | if (0 <= fd) | |
605 | update_index_if_able(the_repository->index, | |
606 | &lock_file); | |
607 | rollback_lock_file(&lock_file); | |
608 | ||
609 | if (has_unstaged_changes(1)) { | |
610 | puts(_("You must edit all merge conflicts and then\n" | |
611 | "mark them as resolved using git add")); | |
612 | exit(1); | |
613 | } | |
614 | if (read_basic_state(&options)) | |
615 | exit(1); | |
616 | goto run_rebase; | |
617 | } | |
122420c2 PK |
618 | case ACTION_SKIP: { |
619 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
620 | ||
621 | options.action = "skip"; | |
622 | ||
623 | rerere_clear(&merge_rr); | |
624 | string_list_clear(&merge_rr, 1); | |
625 | ||
626 | if (reset_head(NULL, "reset", NULL, 0) < 0) | |
627 | die(_("could not discard worktree changes")); | |
628 | if (read_basic_state(&options)) | |
629 | exit(1); | |
630 | goto run_rebase; | |
631 | } | |
5e5d9619 PK |
632 | case ACTION_ABORT: { |
633 | struct string_list merge_rr = STRING_LIST_INIT_DUP; | |
634 | options.action = "abort"; | |
635 | ||
636 | rerere_clear(&merge_rr); | |
637 | string_list_clear(&merge_rr, 1); | |
638 | ||
639 | if (read_basic_state(&options)) | |
640 | exit(1); | |
641 | if (reset_head(&options.orig_head, "reset", | |
642 | options.head_name, 0) < 0) | |
643 | die(_("could not move back to %s"), | |
644 | oid_to_hex(&options.orig_head)); | |
645 | ret = finish_rebase(&options); | |
646 | goto cleanup; | |
647 | } | |
5a614945 PK |
648 | case ACTION_QUIT: { |
649 | strbuf_reset(&buf); | |
650 | strbuf_addstr(&buf, options.state_dir); | |
651 | ret = !!remove_dir_recursively(&buf, 0); | |
652 | if (ret) | |
653 | die(_("could not remove '%s'"), options.state_dir); | |
654 | goto cleanup; | |
655 | } | |
51e9ea6d PK |
656 | case ACTION_EDIT_TODO: |
657 | options.action = "edit-todo"; | |
658 | options.dont_finish_rebase = 1; | |
659 | goto run_rebase; | |
660 | case ACTION_SHOW_CURRENT_PATCH: | |
661 | options.action = "show-current-patch"; | |
662 | options.dont_finish_rebase = 1; | |
663 | goto run_rebase; | |
664 | case NO_ACTION: | |
665 | break; | |
f9573628 | 666 | default: |
51e9ea6d | 667 | BUG("action: %d", action); |
f9573628 PK |
668 | } |
669 | ||
c54dacb5 PK |
670 | /* Make sure no rebase is in progress */ |
671 | if (in_progress) { | |
672 | const char *last_slash = strrchr(options.state_dir, '/'); | |
673 | const char *state_dir_base = | |
674 | last_slash ? last_slash + 1 : options.state_dir; | |
675 | const char *cmd_live_rebase = | |
676 | "git rebase (--continue | --abort | --skip)"; | |
677 | strbuf_reset(&buf); | |
678 | strbuf_addf(&buf, "rm -fr \"%s\"", options.state_dir); | |
679 | die(_("It seems that there is already a %s directory, and\n" | |
680 | "I wonder if you are in the middle of another rebase. " | |
681 | "If that is the\n" | |
682 | "case, please try\n\t%s\n" | |
683 | "If that is not the case, please\n\t%s\n" | |
684 | "and run me again. I am stopping in case you still " | |
685 | "have something\n" | |
686 | "valuable there.\n"), | |
687 | state_dir_base, cmd_live_rebase, buf.buf); | |
688 | } | |
689 | ||
b4c8eb02 PK |
690 | if (!(options.flags & REBASE_NO_QUIET)) |
691 | strbuf_addstr(&options.git_am_opt, " -q"); | |
692 | ||
ac7f467f PK |
693 | switch (options.type) { |
694 | case REBASE_MERGE: | |
695 | case REBASE_INTERACTIVE: | |
696 | case REBASE_PRESERVE_MERGES: | |
697 | options.state_dir = merge_dir(); | |
698 | break; | |
699 | case REBASE_AM: | |
700 | options.state_dir = apply_dir(); | |
701 | break; | |
702 | default: | |
703 | /* the default rebase backend is `--am` */ | |
704 | options.type = REBASE_AM; | |
705 | options.state_dir = apply_dir(); | |
706 | break; | |
707 | } | |
708 | ||
709 | if (!options.root) { | |
f28d40d3 | 710 | if (argc < 1) |
ac7f467f PK |
711 | die("TODO: handle @{upstream}"); |
712 | else { | |
f28d40d3 | 713 | options.upstream_name = argv[0]; |
ac7f467f PK |
714 | argc--; |
715 | argv++; | |
716 | if (!strcmp(options.upstream_name, "-")) | |
717 | options.upstream_name = "@{-1}"; | |
718 | } | |
719 | options.upstream = peel_committish(options.upstream_name); | |
720 | if (!options.upstream) | |
721 | die(_("invalid upstream '%s'"), options.upstream_name); | |
06e4775a | 722 | options.upstream_arg = options.upstream_name; |
ac7f467f PK |
723 | } else |
724 | die("TODO: upstream for --root"); | |
725 | ||
726 | /* Make sure the branch to rebase onto is valid. */ | |
727 | if (!options.onto_name) | |
728 | options.onto_name = options.upstream_name; | |
729 | if (strstr(options.onto_name, "...")) { | |
075bc852 PK |
730 | if (get_oid_mb(options.onto_name, &merge_base) < 0) |
731 | die(_("'%s': need exactly one merge base"), | |
732 | options.onto_name); | |
733 | options.onto = lookup_commit_or_die(&merge_base, | |
734 | options.onto_name); | |
ac7f467f PK |
735 | } else { |
736 | options.onto = peel_committish(options.onto_name); | |
737 | if (!options.onto) | |
738 | die(_("Does not point to a valid commit '%s'"), | |
739 | options.onto_name); | |
740 | } | |
741 | ||
742 | /* | |
743 | * If the branch to rebase is given, that is the branch we will rebase | |
744 | * branch_name -- branch/commit being rebased, or | |
745 | * HEAD (already detached) | |
746 | * orig_head -- commit object name of tip of the branch before rebasing | |
d4c569f8 | 747 | * head_name -- refs/heads/<that-branch> or NULL (detached HEAD) |
ac7f467f | 748 | */ |
e65123a7 PK |
749 | if (argc == 1) { |
750 | /* Is it "rebase other branchname" or "rebase other commit"? */ | |
751 | branch_name = argv[0]; | |
752 | options.switch_to = argv[0]; | |
753 | ||
754 | /* Is it a local branch? */ | |
755 | strbuf_reset(&buf); | |
756 | strbuf_addf(&buf, "refs/heads/%s", branch_name); | |
757 | if (!read_ref(buf.buf, &options.orig_head)) | |
758 | options.head_name = xstrdup(buf.buf); | |
759 | /* If not is it a valid ref (branch or commit)? */ | |
760 | else if (!get_oid(branch_name, &options.orig_head)) | |
761 | options.head_name = NULL; | |
762 | else | |
763 | die(_("fatal: no such branch/commit '%s'"), | |
764 | branch_name); | |
765 | } else if (argc == 0) { | |
ac7f467f PK |
766 | /* Do not need to switch branches, we are already on it. */ |
767 | options.head_name = | |
768 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, | |
769 | &flags)); | |
770 | if (!options.head_name) | |
771 | die(_("No such ref: %s"), "HEAD"); | |
772 | if (flags & REF_ISSYMREF) { | |
773 | if (!skip_prefix(options.head_name, | |
774 | "refs/heads/", &branch_name)) | |
775 | branch_name = options.head_name; | |
776 | ||
777 | } else { | |
d4c569f8 PK |
778 | free(options.head_name); |
779 | options.head_name = NULL; | |
ac7f467f PK |
780 | branch_name = "HEAD"; |
781 | } | |
782 | if (get_oid("HEAD", &options.orig_head)) | |
783 | die(_("Could not resolve HEAD to a revision")); | |
e65123a7 PK |
784 | } else |
785 | BUG("unexpected number of arguments left to parse"); | |
ac7f467f | 786 | |
e0333e5c PK |
787 | if (read_index(the_repository->index) < 0) |
788 | die(_("could not read index")); | |
789 | ||
790 | if (require_clean_work_tree("rebase", | |
791 | _("Please commit or stash them."), 1, 1)) { | |
792 | ret = 1; | |
793 | goto cleanup; | |
794 | } | |
795 | ||
9a48a615 PK |
796 | /* |
797 | * Now we are rebasing commits upstream..orig_head (or with --root, | |
798 | * everything leading up to orig_head) on top of onto. | |
799 | */ | |
800 | ||
801 | /* | |
802 | * Check if we are already based on onto with linear history, | |
803 | * but this should be done only when upstream and onto are the same | |
804 | * and if this is not an interactive rebase. | |
805 | */ | |
806 | if (can_fast_forward(options.onto, &options.orig_head, &merge_base) && | |
807 | !is_interactive(&options) && !options.restrict_revision && | |
808 | !oidcmp(&options.upstream->object.oid, &options.onto->object.oid)) { | |
809 | int flag; | |
810 | ||
1ed9c14f | 811 | if (!(options.flags & REBASE_FORCE)) { |
e65123a7 PK |
812 | /* Lazily switch to the target branch if needed... */ |
813 | if (options.switch_to) { | |
814 | struct object_id oid; | |
815 | ||
816 | if (get_oid(options.switch_to, &oid) < 0) { | |
817 | ret = !!error(_("could not parse '%s'"), | |
818 | options.switch_to); | |
819 | goto cleanup; | |
820 | } | |
821 | ||
822 | strbuf_reset(&buf); | |
823 | strbuf_addf(&buf, "rebase: checkout %s", | |
824 | options.switch_to); | |
825 | if (reset_head(&oid, "checkout", | |
826 | options.head_name, 0) < 0) { | |
827 | ret = !!error(_("could not switch to " | |
828 | "%s"), | |
829 | options.switch_to); | |
830 | goto cleanup; | |
831 | } | |
832 | } | |
833 | ||
1ed9c14f PK |
834 | if (!(options.flags & REBASE_NO_QUIET)) |
835 | ; /* be quiet */ | |
836 | else if (!strcmp(branch_name, "HEAD") && | |
837 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
838 | puts(_("HEAD is up to date.")); | |
839 | else | |
840 | printf(_("Current branch %s is up to date.\n"), | |
841 | branch_name); | |
842 | ret = !!finish_rebase(&options); | |
843 | goto cleanup; | |
844 | } else if (!(options.flags & REBASE_NO_QUIET)) | |
9a48a615 PK |
845 | ; /* be quiet */ |
846 | else if (!strcmp(branch_name, "HEAD") && | |
847 | resolve_ref_unsafe("HEAD", 0, NULL, &flag)) | |
848 | puts(_("HEAD is up to date, rebase forced.")); | |
849 | else | |
850 | printf(_("Current branch %s is up to date, rebase " | |
851 | "forced.\n"), branch_name); | |
852 | } | |
853 | ||
06e4775a PK |
854 | /* If a hook exists, give it a chance to interrupt*/ |
855 | if (!ok_to_skip_pre_rebase && | |
856 | run_hook_le(NULL, "pre-rebase", options.upstream_arg, | |
857 | argc ? argv[0] : NULL, NULL)) | |
858 | die(_("The pre-rebase hook refused to rebase.")); | |
859 | ||
bff014da PK |
860 | if (options.flags & REBASE_DIFFSTAT) { |
861 | struct diff_options opts; | |
862 | ||
863 | if (options.flags & REBASE_VERBOSE) | |
864 | printf(_("Changes from %s to %s:\n"), | |
865 | oid_to_hex(&merge_base), | |
866 | oid_to_hex(&options.onto->object.oid)); | |
867 | ||
868 | /* We want color (if set), but no pager */ | |
869 | diff_setup(&opts); | |
870 | opts.stat_width = -1; /* use full terminal width */ | |
871 | opts.stat_graph_width = -1; /* respect statGraphWidth config */ | |
872 | opts.output_format |= | |
873 | DIFF_FORMAT_SUMMARY | DIFF_FORMAT_DIFFSTAT; | |
874 | opts.detect_rename = DIFF_DETECT_RENAME; | |
875 | diff_setup_done(&opts); | |
876 | diff_tree_oid(&merge_base, &options.onto->object.oid, | |
877 | "", &opts); | |
878 | diffcore_std(&opts); | |
879 | diff_flush(&opts); | |
880 | } | |
881 | ||
882 | /* Detach HEAD and reset the tree */ | |
883 | if (options.flags & REBASE_NO_QUIET) | |
884 | printf(_("First, rewinding head to replay your work on top of " | |
885 | "it...\n")); | |
886 | ||
ac7f467f PK |
887 | strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); |
888 | if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) | |
889 | die(_("Could not detach HEAD")); | |
890 | strbuf_release(&msg); | |
891 | ||
892 | strbuf_addf(&revisions, "%s..%s", | |
893 | options.root ? oid_to_hex(&options.onto->object.oid) : | |
894 | (options.restrict_revision ? | |
895 | oid_to_hex(&options.restrict_revision->object.oid) : | |
896 | oid_to_hex(&options.upstream->object.oid)), | |
897 | oid_to_hex(&options.orig_head)); | |
898 | ||
899 | options.revisions = revisions.buf; | |
900 | ||
f9573628 | 901 | run_rebase: |
ac7f467f PK |
902 | ret = !!run_specific_rebase(&options); |
903 | ||
e0333e5c | 904 | cleanup: |
ac7f467f PK |
905 | strbuf_release(&revisions); |
906 | free(options.head_name); | |
907 | return ret; | |
55071ea2 | 908 | } |