]>
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" |
f28d40d3 PK |
21 | |
22 | static char const * const builtin_rebase_usage[] = { | |
23 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
24 | "[<upstream>] [<branch>]"), | |
25 | N_("git rebase [-i] [options] [--exec <cmd>] [--onto <newbase>] " | |
26 | "--root [<branch>]"), | |
27 | N_("git rebase --continue | --abort | --skip | --edit-todo"), | |
28 | NULL | |
29 | }; | |
ac7f467f PK |
30 | |
31 | static GIT_PATH_FUNC(apply_dir, "rebase-apply") | |
32 | static GIT_PATH_FUNC(merge_dir, "rebase-merge") | |
33 | ||
34 | enum rebase_type { | |
35 | REBASE_UNSPECIFIED = -1, | |
36 | REBASE_AM, | |
37 | REBASE_MERGE, | |
38 | REBASE_INTERACTIVE, | |
39 | REBASE_PRESERVE_MERGES | |
40 | }; | |
55071ea2 PK |
41 | |
42 | static int use_builtin_rebase(void) | |
43 | { | |
44 | struct child_process cp = CHILD_PROCESS_INIT; | |
45 | struct strbuf out = STRBUF_INIT; | |
46 | int ret; | |
47 | ||
48 | argv_array_pushl(&cp.args, | |
49 | "config", "--bool", "rebase.usebuiltin", NULL); | |
50 | cp.git_cmd = 1; | |
51 | if (capture_command(&cp, &out, 6)) { | |
52 | strbuf_release(&out); | |
53 | return 0; | |
54 | } | |
55 | ||
56 | strbuf_trim(&out); | |
57 | ret = !strcmp("true", out.buf); | |
58 | strbuf_release(&out); | |
59 | return ret; | |
60 | } | |
61 | ||
ac7f467f PK |
62 | static int apply_autostash(void) |
63 | { | |
64 | warning("TODO"); | |
65 | return 0; | |
66 | } | |
67 | ||
68 | struct rebase_options { | |
69 | enum rebase_type type; | |
70 | const char *state_dir; | |
71 | struct commit *upstream; | |
72 | const char *upstream_name; | |
06e4775a | 73 | const char *upstream_arg; |
ac7f467f PK |
74 | char *head_name; |
75 | struct object_id orig_head; | |
76 | struct commit *onto; | |
77 | const char *onto_name; | |
78 | const char *revisions; | |
79 | int root; | |
80 | struct commit *restrict_revision; | |
81 | int dont_finish_rebase; | |
b4c8eb02 PK |
82 | enum { |
83 | REBASE_NO_QUIET = 1<<0, | |
84 | } flags; | |
85 | struct strbuf git_am_opt; | |
ac7f467f PK |
86 | }; |
87 | ||
88 | /* Returns the filename prefixed by the state_dir */ | |
89 | static const char *state_dir_path(const char *filename, struct rebase_options *opts) | |
90 | { | |
91 | static struct strbuf path = STRBUF_INIT; | |
92 | static size_t prefix_len; | |
93 | ||
94 | if (!prefix_len) { | |
95 | strbuf_addf(&path, "%s/", opts->state_dir); | |
96 | prefix_len = path.len; | |
97 | } | |
98 | ||
99 | strbuf_setlen(&path, prefix_len); | |
100 | strbuf_addstr(&path, filename); | |
101 | return path.buf; | |
102 | } | |
103 | ||
104 | static int finish_rebase(struct rebase_options *opts) | |
105 | { | |
106 | struct strbuf dir = STRBUF_INIT; | |
107 | const char *argv_gc_auto[] = { "gc", "--auto", NULL }; | |
108 | ||
109 | delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF); | |
110 | apply_autostash(); | |
111 | close_all_packs(the_repository->objects); | |
112 | /* | |
113 | * We ignore errors in 'gc --auto', since the | |
114 | * user should see them. | |
115 | */ | |
116 | run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); | |
117 | strbuf_addstr(&dir, opts->state_dir); | |
118 | remove_dir_recursively(&dir, 0); | |
119 | strbuf_release(&dir); | |
120 | ||
121 | return 0; | |
122 | } | |
123 | ||
124 | static struct commit *peel_committish(const char *name) | |
125 | { | |
126 | struct object *obj; | |
127 | struct object_id oid; | |
128 | ||
129 | if (get_oid(name, &oid)) | |
130 | return NULL; | |
131 | obj = parse_object(the_repository, &oid); | |
132 | return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT); | |
133 | } | |
134 | ||
135 | static void add_var(struct strbuf *buf, const char *name, const char *value) | |
136 | { | |
137 | if (!value) | |
138 | strbuf_addf(buf, "unset %s; ", name); | |
139 | else { | |
140 | strbuf_addf(buf, "%s=", name); | |
141 | sq_quote_buf(buf, value); | |
142 | strbuf_addstr(buf, "; "); | |
143 | } | |
144 | } | |
145 | ||
146 | static int run_specific_rebase(struct rebase_options *opts) | |
147 | { | |
148 | const char *argv[] = { NULL, NULL }; | |
149 | struct strbuf script_snippet = STRBUF_INIT; | |
150 | int status; | |
151 | const char *backend, *backend_func; | |
152 | ||
153 | add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir())); | |
154 | add_var(&script_snippet, "state_dir", opts->state_dir); | |
155 | ||
156 | add_var(&script_snippet, "upstream_name", opts->upstream_name); | |
157 | add_var(&script_snippet, "upstream", | |
158 | oid_to_hex(&opts->upstream->object.oid)); | |
159 | add_var(&script_snippet, "head_name", opts->head_name); | |
160 | add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head)); | |
161 | add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid)); | |
162 | add_var(&script_snippet, "onto_name", opts->onto_name); | |
163 | add_var(&script_snippet, "revisions", opts->revisions); | |
164 | add_var(&script_snippet, "restrict_revision", opts->restrict_revision ? | |
165 | oid_to_hex(&opts->restrict_revision->object.oid) : NULL); | |
b4c8eb02 PK |
166 | add_var(&script_snippet, "GIT_QUIET", |
167 | opts->flags & REBASE_NO_QUIET ? "" : "t"); | |
168 | add_var(&script_snippet, "git_am_opt", opts->git_am_opt.buf); | |
ac7f467f PK |
169 | |
170 | switch (opts->type) { | |
171 | case REBASE_AM: | |
172 | backend = "git-rebase--am"; | |
173 | backend_func = "git_rebase__am"; | |
174 | break; | |
175 | case REBASE_INTERACTIVE: | |
176 | backend = "git-rebase--interactive"; | |
177 | backend_func = "git_rebase__interactive"; | |
178 | break; | |
179 | case REBASE_MERGE: | |
180 | backend = "git-rebase--merge"; | |
181 | backend_func = "git_rebase__merge"; | |
182 | break; | |
183 | case REBASE_PRESERVE_MERGES: | |
184 | backend = "git-rebase--preserve-merges"; | |
185 | backend_func = "git_rebase__preserve_merges"; | |
186 | break; | |
187 | default: | |
188 | BUG("Unhandled rebase type %d", opts->type); | |
189 | break; | |
190 | } | |
191 | ||
192 | strbuf_addf(&script_snippet, | |
193 | ". git-sh-setup && . git-rebase--common &&" | |
194 | " . %s && %s", backend, backend_func); | |
195 | argv[0] = script_snippet.buf; | |
196 | ||
197 | status = run_command_v_opt(argv, RUN_USING_SHELL); | |
198 | if (opts->dont_finish_rebase) | |
199 | ; /* do nothing */ | |
200 | else if (status == 0) { | |
201 | if (!file_exists(state_dir_path("stopped-sha", opts))) | |
202 | finish_rebase(opts); | |
203 | } else if (status == 2) { | |
204 | struct strbuf dir = STRBUF_INIT; | |
205 | ||
206 | apply_autostash(); | |
207 | strbuf_addstr(&dir, opts->state_dir); | |
208 | remove_dir_recursively(&dir, 0); | |
209 | strbuf_release(&dir); | |
210 | die("Nothing to do"); | |
211 | } | |
212 | ||
213 | strbuf_release(&script_snippet); | |
214 | ||
215 | return status ? -1 : 0; | |
216 | } | |
217 | ||
218 | #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" | |
219 | ||
220 | static int reset_head(struct object_id *oid, const char *action, | |
221 | const char *switch_to_branch, int detach_head) | |
222 | { | |
223 | struct object_id head_oid; | |
224 | struct tree_desc desc; | |
225 | struct lock_file lock = LOCK_INIT; | |
226 | struct unpack_trees_options unpack_tree_opts; | |
227 | struct tree *tree; | |
228 | const char *reflog_action; | |
229 | struct strbuf msg = STRBUF_INIT; | |
230 | size_t prefix_len; | |
231 | struct object_id *orig = NULL, oid_orig, | |
232 | *old_orig = NULL, oid_old_orig; | |
233 | int ret = 0; | |
234 | ||
235 | if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) | |
236 | return -1; | |
237 | ||
238 | if (!oid) { | |
239 | if (get_oid("HEAD", &head_oid)) { | |
240 | rollback_lock_file(&lock); | |
241 | return error(_("could not determine HEAD revision")); | |
242 | } | |
243 | oid = &head_oid; | |
244 | } | |
245 | ||
246 | memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); | |
247 | setup_unpack_trees_porcelain(&unpack_tree_opts, action); | |
248 | unpack_tree_opts.head_idx = 1; | |
249 | unpack_tree_opts.src_index = the_repository->index; | |
250 | unpack_tree_opts.dst_index = the_repository->index; | |
251 | unpack_tree_opts.fn = oneway_merge; | |
252 | unpack_tree_opts.update = 1; | |
253 | unpack_tree_opts.merge = 1; | |
254 | if (!detach_head) | |
255 | unpack_tree_opts.reset = 1; | |
256 | ||
257 | if (read_index_unmerged(the_repository->index) < 0) { | |
258 | rollback_lock_file(&lock); | |
259 | return error(_("could not read index")); | |
260 | } | |
261 | ||
262 | if (!fill_tree_descriptor(&desc, oid)) { | |
263 | error(_("failed to find tree of %s"), oid_to_hex(oid)); | |
264 | rollback_lock_file(&lock); | |
265 | free((void *)desc.buffer); | |
266 | return -1; | |
267 | } | |
268 | ||
269 | if (unpack_trees(1, &desc, &unpack_tree_opts)) { | |
270 | rollback_lock_file(&lock); | |
271 | free((void *)desc.buffer); | |
272 | return -1; | |
273 | } | |
274 | ||
275 | tree = parse_tree_indirect(oid); | |
276 | prime_cache_tree(the_repository->index, tree); | |
277 | ||
278 | if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) | |
279 | ret = error(_("could not write index")); | |
280 | free((void *)desc.buffer); | |
281 | ||
282 | if (ret) | |
283 | return ret; | |
284 | ||
285 | reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); | |
286 | strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); | |
287 | prefix_len = msg.len; | |
288 | ||
289 | if (!get_oid("ORIG_HEAD", &oid_old_orig)) | |
290 | old_orig = &oid_old_orig; | |
291 | if (!get_oid("HEAD", &oid_orig)) { | |
292 | orig = &oid_orig; | |
293 | strbuf_addstr(&msg, "updating ORIG_HEAD"); | |
294 | update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, | |
295 | UPDATE_REFS_MSG_ON_ERR); | |
296 | } else if (old_orig) | |
297 | delete_ref(NULL, "ORIG_HEAD", old_orig, 0); | |
298 | strbuf_setlen(&msg, prefix_len); | |
299 | strbuf_addstr(&msg, "updating HEAD"); | |
300 | if (!switch_to_branch) | |
301 | ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF, | |
302 | UPDATE_REFS_MSG_ON_ERR); | |
303 | else { | |
304 | ret = create_symref("HEAD", switch_to_branch, msg.buf); | |
305 | if (!ret) | |
306 | ret = update_ref(msg.buf, "HEAD", oid, NULL, 0, | |
307 | UPDATE_REFS_MSG_ON_ERR); | |
308 | } | |
309 | ||
310 | strbuf_release(&msg); | |
311 | return ret; | |
312 | } | |
313 | ||
55071ea2 PK |
314 | int cmd_rebase(int argc, const char **argv, const char *prefix) |
315 | { | |
ac7f467f PK |
316 | struct rebase_options options = { |
317 | .type = REBASE_UNSPECIFIED, | |
b4c8eb02 PK |
318 | .flags = REBASE_NO_QUIET, |
319 | .git_am_opt = STRBUF_INIT, | |
ac7f467f PK |
320 | }; |
321 | const char *branch_name; | |
322 | int ret, flags; | |
06e4775a | 323 | int ok_to_skip_pre_rebase = 0; |
ac7f467f PK |
324 | struct strbuf msg = STRBUF_INIT; |
325 | struct strbuf revisions = STRBUF_INIT; | |
075bc852 | 326 | struct object_id merge_base; |
f28d40d3 PK |
327 | struct option builtin_rebase_options[] = { |
328 | OPT_STRING(0, "onto", &options.onto_name, | |
329 | N_("revision"), | |
330 | N_("rebase onto given branch instead of upstream")), | |
06e4775a PK |
331 | OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase, |
332 | N_("allow pre-rebase hook to run")), | |
b4c8eb02 PK |
333 | OPT_NEGBIT('q', "quiet", &options.flags, |
334 | N_("be quiet. implies --no-stat"), | |
335 | REBASE_NO_QUIET), | |
f28d40d3 PK |
336 | OPT_END(), |
337 | }; | |
ac7f467f | 338 | |
55071ea2 PK |
339 | /* |
340 | * NEEDSWORK: Once the builtin rebase has been tested enough | |
341 | * and git-legacy-rebase.sh is retired to contrib/, this preamble | |
342 | * can be removed. | |
343 | */ | |
344 | ||
345 | if (!use_builtin_rebase()) { | |
346 | const char *path = mkpath("%s/git-legacy-rebase", | |
347 | git_exec_path()); | |
348 | ||
349 | if (sane_execvp(path, (char **)argv) < 0) | |
350 | die_errno(_("could not exec %s"), path); | |
351 | else | |
352 | BUG("sane_execvp() returned???"); | |
353 | } | |
354 | ||
f28d40d3 PK |
355 | if (argc == 2 && !strcmp(argv[1], "-h")) |
356 | usage_with_options(builtin_rebase_usage, | |
357 | builtin_rebase_options); | |
358 | ||
55071ea2 PK |
359 | prefix = setup_git_directory(); |
360 | trace_repo_setup(prefix); | |
361 | setup_work_tree(); | |
362 | ||
ac7f467f | 363 | git_config(git_default_config, NULL); |
f28d40d3 PK |
364 | argc = parse_options(argc, argv, prefix, |
365 | builtin_rebase_options, | |
366 | builtin_rebase_usage, 0); | |
367 | ||
368 | if (argc > 2) | |
369 | usage_with_options(builtin_rebase_usage, | |
370 | builtin_rebase_options); | |
ac7f467f | 371 | |
b4c8eb02 PK |
372 | if (!(options.flags & REBASE_NO_QUIET)) |
373 | strbuf_addstr(&options.git_am_opt, " -q"); | |
374 | ||
ac7f467f PK |
375 | switch (options.type) { |
376 | case REBASE_MERGE: | |
377 | case REBASE_INTERACTIVE: | |
378 | case REBASE_PRESERVE_MERGES: | |
379 | options.state_dir = merge_dir(); | |
380 | break; | |
381 | case REBASE_AM: | |
382 | options.state_dir = apply_dir(); | |
383 | break; | |
384 | default: | |
385 | /* the default rebase backend is `--am` */ | |
386 | options.type = REBASE_AM; | |
387 | options.state_dir = apply_dir(); | |
388 | break; | |
389 | } | |
390 | ||
391 | if (!options.root) { | |
f28d40d3 | 392 | if (argc < 1) |
ac7f467f PK |
393 | die("TODO: handle @{upstream}"); |
394 | else { | |
f28d40d3 | 395 | options.upstream_name = argv[0]; |
ac7f467f PK |
396 | argc--; |
397 | argv++; | |
398 | if (!strcmp(options.upstream_name, "-")) | |
399 | options.upstream_name = "@{-1}"; | |
400 | } | |
401 | options.upstream = peel_committish(options.upstream_name); | |
402 | if (!options.upstream) | |
403 | die(_("invalid upstream '%s'"), options.upstream_name); | |
06e4775a | 404 | options.upstream_arg = options.upstream_name; |
ac7f467f PK |
405 | } else |
406 | die("TODO: upstream for --root"); | |
407 | ||
408 | /* Make sure the branch to rebase onto is valid. */ | |
409 | if (!options.onto_name) | |
410 | options.onto_name = options.upstream_name; | |
411 | if (strstr(options.onto_name, "...")) { | |
075bc852 PK |
412 | if (get_oid_mb(options.onto_name, &merge_base) < 0) |
413 | die(_("'%s': need exactly one merge base"), | |
414 | options.onto_name); | |
415 | options.onto = lookup_commit_or_die(&merge_base, | |
416 | options.onto_name); | |
ac7f467f PK |
417 | } else { |
418 | options.onto = peel_committish(options.onto_name); | |
419 | if (!options.onto) | |
420 | die(_("Does not point to a valid commit '%s'"), | |
421 | options.onto_name); | |
422 | } | |
423 | ||
424 | /* | |
425 | * If the branch to rebase is given, that is the branch we will rebase | |
426 | * branch_name -- branch/commit being rebased, or | |
427 | * HEAD (already detached) | |
428 | * orig_head -- commit object name of tip of the branch before rebasing | |
429 | * head_name -- refs/heads/<that-branch> or "detached HEAD" | |
430 | */ | |
f28d40d3 | 431 | if (argc > 0) |
ac7f467f PK |
432 | die("TODO: handle switch_to"); |
433 | else { | |
434 | /* Do not need to switch branches, we are already on it. */ | |
435 | options.head_name = | |
436 | xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL, | |
437 | &flags)); | |
438 | if (!options.head_name) | |
439 | die(_("No such ref: %s"), "HEAD"); | |
440 | if (flags & REF_ISSYMREF) { | |
441 | if (!skip_prefix(options.head_name, | |
442 | "refs/heads/", &branch_name)) | |
443 | branch_name = options.head_name; | |
444 | ||
445 | } else { | |
446 | options.head_name = xstrdup("detached HEAD"); | |
447 | branch_name = "HEAD"; | |
448 | } | |
449 | if (get_oid("HEAD", &options.orig_head)) | |
450 | die(_("Could not resolve HEAD to a revision")); | |
451 | } | |
452 | ||
06e4775a PK |
453 | /* If a hook exists, give it a chance to interrupt*/ |
454 | if (!ok_to_skip_pre_rebase && | |
455 | run_hook_le(NULL, "pre-rebase", options.upstream_arg, | |
456 | argc ? argv[0] : NULL, NULL)) | |
457 | die(_("The pre-rebase hook refused to rebase.")); | |
458 | ||
ac7f467f PK |
459 | strbuf_addf(&msg, "rebase: checkout %s", options.onto_name); |
460 | if (reset_head(&options.onto->object.oid, "checkout", NULL, 1)) | |
461 | die(_("Could not detach HEAD")); | |
462 | strbuf_release(&msg); | |
463 | ||
464 | strbuf_addf(&revisions, "%s..%s", | |
465 | options.root ? oid_to_hex(&options.onto->object.oid) : | |
466 | (options.restrict_revision ? | |
467 | oid_to_hex(&options.restrict_revision->object.oid) : | |
468 | oid_to_hex(&options.upstream->object.oid)), | |
469 | oid_to_hex(&options.orig_head)); | |
470 | ||
471 | options.revisions = revisions.buf; | |
472 | ||
473 | ret = !!run_specific_rebase(&options); | |
474 | ||
475 | strbuf_release(&revisions); | |
476 | free(options.head_name); | |
477 | return ret; | |
55071ea2 | 478 | } |