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