2 * "git rebase" builtin command
4 * Copyright (c) 2018 Pratik Karki
8 #include "run-command.h"
10 #include "argv-array.h"
16 #include "cache-tree.h"
17 #include "unpack-trees.h"
20 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
21 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
24 REBASE_UNSPECIFIED = -1,
28 REBASE_PRESERVE_MERGES
31 static int use_builtin_rebase(void)
33 struct child_process cp = CHILD_PROCESS_INIT;
34 struct strbuf out = STRBUF_INIT;
37 argv_array_pushl(&cp.args,
38 "config", "--bool", "rebase.usebuiltin", NULL);
40 if (capture_command(&cp, &out, 6)) {
46 ret = !strcmp("true", out.buf);
51 static int apply_autostash(void)
57 struct rebase_options {
58 enum rebase_type type;
59 const char *state_dir;
60 struct commit *upstream;
61 const char *upstream_name;
63 struct object_id orig_head;
65 const char *onto_name;
66 const char *revisions;
68 struct commit *restrict_revision;
69 int dont_finish_rebase;
72 /* Returns the filename prefixed by the state_dir */
73 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
75 static struct strbuf path = STRBUF_INIT;
76 static size_t prefix_len;
79 strbuf_addf(&path, "%s/", opts->state_dir);
80 prefix_len = path.len;
83 strbuf_setlen(&path, prefix_len);
84 strbuf_addstr(&path, filename);
88 static int finish_rebase(struct rebase_options *opts)
90 struct strbuf dir = STRBUF_INIT;
91 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
93 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
95 close_all_packs(the_repository->objects);
97 * We ignore errors in 'gc --auto', since the
98 * user should see them.
100 run_command_v_opt(argv_gc_auto, RUN_GIT_CMD);
101 strbuf_addstr(&dir, opts->state_dir);
102 remove_dir_recursively(&dir, 0);
103 strbuf_release(&dir);
108 static struct commit *peel_committish(const char *name)
111 struct object_id oid;
113 if (get_oid(name, &oid))
115 obj = parse_object(the_repository, &oid);
116 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
119 static void add_var(struct strbuf *buf, const char *name, const char *value)
122 strbuf_addf(buf, "unset %s; ", name);
124 strbuf_addf(buf, "%s=", name);
125 sq_quote_buf(buf, value);
126 strbuf_addstr(buf, "; ");
130 static int run_specific_rebase(struct rebase_options *opts)
132 const char *argv[] = { NULL, NULL };
133 struct strbuf script_snippet = STRBUF_INIT;
135 const char *backend, *backend_func;
137 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
138 add_var(&script_snippet, "state_dir", opts->state_dir);
140 add_var(&script_snippet, "upstream_name", opts->upstream_name);
141 add_var(&script_snippet, "upstream",
142 oid_to_hex(&opts->upstream->object.oid));
143 add_var(&script_snippet, "head_name", opts->head_name);
144 add_var(&script_snippet, "orig_head", oid_to_hex(&opts->orig_head));
145 add_var(&script_snippet, "onto", oid_to_hex(&opts->onto->object.oid));
146 add_var(&script_snippet, "onto_name", opts->onto_name);
147 add_var(&script_snippet, "revisions", opts->revisions);
148 add_var(&script_snippet, "restrict_revision", opts->restrict_revision ?
149 oid_to_hex(&opts->restrict_revision->object.oid) : NULL);
151 switch (opts->type) {
153 backend = "git-rebase--am";
154 backend_func = "git_rebase__am";
156 case REBASE_INTERACTIVE:
157 backend = "git-rebase--interactive";
158 backend_func = "git_rebase__interactive";
161 backend = "git-rebase--merge";
162 backend_func = "git_rebase__merge";
164 case REBASE_PRESERVE_MERGES:
165 backend = "git-rebase--preserve-merges";
166 backend_func = "git_rebase__preserve_merges";
169 BUG("Unhandled rebase type %d", opts->type);
173 strbuf_addf(&script_snippet,
174 ". git-sh-setup && . git-rebase--common &&"
175 " . %s && %s", backend, backend_func);
176 argv[0] = script_snippet.buf;
178 status = run_command_v_opt(argv, RUN_USING_SHELL);
179 if (opts->dont_finish_rebase)
181 else if (status == 0) {
182 if (!file_exists(state_dir_path("stopped-sha", opts)))
184 } else if (status == 2) {
185 struct strbuf dir = STRBUF_INIT;
188 strbuf_addstr(&dir, opts->state_dir);
189 remove_dir_recursively(&dir, 0);
190 strbuf_release(&dir);
191 die("Nothing to do");
194 strbuf_release(&script_snippet);
196 return status ? -1 : 0;
199 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
201 static int reset_head(struct object_id *oid, const char *action,
202 const char *switch_to_branch, int detach_head)
204 struct object_id head_oid;
205 struct tree_desc desc;
206 struct lock_file lock = LOCK_INIT;
207 struct unpack_trees_options unpack_tree_opts;
209 const char *reflog_action;
210 struct strbuf msg = STRBUF_INIT;
212 struct object_id *orig = NULL, oid_orig,
213 *old_orig = NULL, oid_old_orig;
216 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
220 if (get_oid("HEAD", &head_oid)) {
221 rollback_lock_file(&lock);
222 return error(_("could not determine HEAD revision"));
227 memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts));
228 setup_unpack_trees_porcelain(&unpack_tree_opts, action);
229 unpack_tree_opts.head_idx = 1;
230 unpack_tree_opts.src_index = the_repository->index;
231 unpack_tree_opts.dst_index = the_repository->index;
232 unpack_tree_opts.fn = oneway_merge;
233 unpack_tree_opts.update = 1;
234 unpack_tree_opts.merge = 1;
236 unpack_tree_opts.reset = 1;
238 if (read_index_unmerged(the_repository->index) < 0) {
239 rollback_lock_file(&lock);
240 return error(_("could not read index"));
243 if (!fill_tree_descriptor(&desc, oid)) {
244 error(_("failed to find tree of %s"), oid_to_hex(oid));
245 rollback_lock_file(&lock);
246 free((void *)desc.buffer);
250 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
251 rollback_lock_file(&lock);
252 free((void *)desc.buffer);
256 tree = parse_tree_indirect(oid);
257 prime_cache_tree(the_repository->index, tree);
259 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
260 ret = error(_("could not write index"));
261 free((void *)desc.buffer);
266 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
267 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
268 prefix_len = msg.len;
270 if (!get_oid("ORIG_HEAD", &oid_old_orig))
271 old_orig = &oid_old_orig;
272 if (!get_oid("HEAD", &oid_orig)) {
274 strbuf_addstr(&msg, "updating ORIG_HEAD");
275 update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0,
276 UPDATE_REFS_MSG_ON_ERR);
278 delete_ref(NULL, "ORIG_HEAD", old_orig, 0);
279 strbuf_setlen(&msg, prefix_len);
280 strbuf_addstr(&msg, "updating HEAD");
281 if (!switch_to_branch)
282 ret = update_ref(msg.buf, "HEAD", oid, orig, REF_NO_DEREF,
283 UPDATE_REFS_MSG_ON_ERR);
285 ret = create_symref("HEAD", switch_to_branch, msg.buf);
287 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
288 UPDATE_REFS_MSG_ON_ERR);
291 strbuf_release(&msg);
295 int cmd_rebase(int argc, const char **argv, const char *prefix)
297 struct rebase_options options = {
298 .type = REBASE_UNSPECIFIED,
300 const char *branch_name;
302 struct strbuf msg = STRBUF_INIT;
303 struct strbuf revisions = STRBUF_INIT;
306 * NEEDSWORK: Once the builtin rebase has been tested enough
307 * and git-legacy-rebase.sh is retired to contrib/, this preamble
311 if (!use_builtin_rebase()) {
312 const char *path = mkpath("%s/git-legacy-rebase",
315 if (sane_execvp(path, (char **)argv) < 0)
316 die_errno(_("could not exec %s"), path);
318 BUG("sane_execvp() returned???");
322 die(_("Usage: %s <base>"), argv[0]);
323 prefix = setup_git_directory();
324 trace_repo_setup(prefix);
327 git_config(git_default_config, NULL);
329 switch (options.type) {
331 case REBASE_INTERACTIVE:
332 case REBASE_PRESERVE_MERGES:
333 options.state_dir = merge_dir();
336 options.state_dir = apply_dir();
339 /* the default rebase backend is `--am` */
340 options.type = REBASE_AM;
341 options.state_dir = apply_dir();
347 die("TODO: handle @{upstream}");
349 options.upstream_name = argv[1];
352 if (!strcmp(options.upstream_name, "-"))
353 options.upstream_name = "@{-1}";
355 options.upstream = peel_committish(options.upstream_name);
356 if (!options.upstream)
357 die(_("invalid upstream '%s'"), options.upstream_name);
359 die("TODO: upstream for --root");
361 /* Make sure the branch to rebase onto is valid. */
362 if (!options.onto_name)
363 options.onto_name = options.upstream_name;
364 if (strstr(options.onto_name, "...")) {
367 options.onto = peel_committish(options.onto_name);
369 die(_("Does not point to a valid commit '%s'"),
374 * If the branch to rebase is given, that is the branch we will rebase
375 * branch_name -- branch/commit being rebased, or
376 * HEAD (already detached)
377 * orig_head -- commit object name of tip of the branch before rebasing
378 * head_name -- refs/heads/<that-branch> or "detached HEAD"
381 die("TODO: handle switch_to");
383 /* Do not need to switch branches, we are already on it. */
385 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
387 if (!options.head_name)
388 die(_("No such ref: %s"), "HEAD");
389 if (flags & REF_ISSYMREF) {
390 if (!skip_prefix(options.head_name,
391 "refs/heads/", &branch_name))
392 branch_name = options.head_name;
395 options.head_name = xstrdup("detached HEAD");
396 branch_name = "HEAD";
398 if (get_oid("HEAD", &options.orig_head))
399 die(_("Could not resolve HEAD to a revision"));
402 strbuf_addf(&msg, "rebase: checkout %s", options.onto_name);
403 if (reset_head(&options.onto->object.oid, "checkout", NULL, 1))
404 die(_("Could not detach HEAD"));
405 strbuf_release(&msg);
407 strbuf_addf(&revisions, "%s..%s",
408 options.root ? oid_to_hex(&options.onto->object.oid) :
409 (options.restrict_revision ?
410 oid_to_hex(&options.restrict_revision->object.oid) :
411 oid_to_hex(&options.upstream->object.oid)),
412 oid_to_hex(&options.orig_head));
414 options.revisions = revisions.buf;
416 ret = !!run_specific_rebase(&options);
418 strbuf_release(&revisions);
419 free(options.head_name);