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"
19 #include "parse-options.h"
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>] "
27 N_("git rebase --continue | --abort | --skip | --edit-todo"),
31 static GIT_PATH_FUNC(apply_dir, "rebase-apply")
32 static GIT_PATH_FUNC(merge_dir, "rebase-merge")
35 REBASE_UNSPECIFIED = -1,
39 REBASE_PRESERVE_MERGES
42 static int use_builtin_rebase(void)
44 struct child_process cp = CHILD_PROCESS_INIT;
45 struct strbuf out = STRBUF_INIT;
48 argv_array_pushl(&cp.args,
49 "config", "--bool", "rebase.usebuiltin", NULL);
51 if (capture_command(&cp, &out, 6)) {
57 ret = !strcmp("true", out.buf);
62 static int apply_autostash(void)
68 struct rebase_options {
69 enum rebase_type type;
70 const char *state_dir;
71 struct commit *upstream;
72 const char *upstream_name;
73 const char *upstream_arg;
75 struct object_id orig_head;
77 const char *onto_name;
78 const char *revisions;
80 struct commit *restrict_revision;
81 int dont_finish_rebase;
83 REBASE_NO_QUIET = 1<<0,
85 struct strbuf git_am_opt;
88 /* Returns the filename prefixed by the state_dir */
89 static const char *state_dir_path(const char *filename, struct rebase_options *opts)
91 static struct strbuf path = STRBUF_INIT;
92 static size_t prefix_len;
95 strbuf_addf(&path, "%s/", opts->state_dir);
96 prefix_len = path.len;
99 strbuf_setlen(&path, prefix_len);
100 strbuf_addstr(&path, filename);
104 static int finish_rebase(struct rebase_options *opts)
106 struct strbuf dir = STRBUF_INIT;
107 const char *argv_gc_auto[] = { "gc", "--auto", NULL };
109 delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
111 close_all_packs(the_repository->objects);
113 * We ignore errors in 'gc --auto', since the
114 * user should see them.
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);
124 static struct commit *peel_committish(const char *name)
127 struct object_id oid;
129 if (get_oid(name, &oid))
131 obj = parse_object(the_repository, &oid);
132 return (struct commit *)peel_to_type(name, 0, obj, OBJ_COMMIT);
135 static void add_var(struct strbuf *buf, const char *name, const char *value)
138 strbuf_addf(buf, "unset %s; ", name);
140 strbuf_addf(buf, "%s=", name);
141 sq_quote_buf(buf, value);
142 strbuf_addstr(buf, "; ");
146 static int run_specific_rebase(struct rebase_options *opts)
148 const char *argv[] = { NULL, NULL };
149 struct strbuf script_snippet = STRBUF_INIT;
151 const char *backend, *backend_func;
153 add_var(&script_snippet, "GIT_DIR", absolute_path(get_git_dir()));
154 add_var(&script_snippet, "state_dir", opts->state_dir);
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);
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);
170 switch (opts->type) {
172 backend = "git-rebase--am";
173 backend_func = "git_rebase__am";
175 case REBASE_INTERACTIVE:
176 backend = "git-rebase--interactive";
177 backend_func = "git_rebase__interactive";
180 backend = "git-rebase--merge";
181 backend_func = "git_rebase__merge";
183 case REBASE_PRESERVE_MERGES:
184 backend = "git-rebase--preserve-merges";
185 backend_func = "git_rebase__preserve_merges";
188 BUG("Unhandled rebase type %d", opts->type);
192 strbuf_addf(&script_snippet,
193 ". git-sh-setup && . git-rebase--common &&"
194 " . %s && %s", backend, backend_func);
195 argv[0] = script_snippet.buf;
197 status = run_command_v_opt(argv, RUN_USING_SHELL);
198 if (opts->dont_finish_rebase)
200 else if (status == 0) {
201 if (!file_exists(state_dir_path("stopped-sha", opts)))
203 } else if (status == 2) {
204 struct strbuf dir = STRBUF_INIT;
207 strbuf_addstr(&dir, opts->state_dir);
208 remove_dir_recursively(&dir, 0);
209 strbuf_release(&dir);
210 die("Nothing to do");
213 strbuf_release(&script_snippet);
215 return status ? -1 : 0;
218 #define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION"
220 static int reset_head(struct object_id *oid, const char *action,
221 const char *switch_to_branch, int detach_head)
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;
228 const char *reflog_action;
229 struct strbuf msg = STRBUF_INIT;
231 struct object_id *orig = NULL, oid_orig,
232 *old_orig = NULL, oid_old_orig;
235 if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
239 if (get_oid("HEAD", &head_oid)) {
240 rollback_lock_file(&lock);
241 return error(_("could not determine HEAD revision"));
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;
255 unpack_tree_opts.reset = 1;
257 if (read_index_unmerged(the_repository->index) < 0) {
258 rollback_lock_file(&lock);
259 return error(_("could not read index"));
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);
269 if (unpack_trees(1, &desc, &unpack_tree_opts)) {
270 rollback_lock_file(&lock);
271 free((void *)desc.buffer);
275 tree = parse_tree_indirect(oid);
276 prime_cache_tree(the_repository->index, tree);
278 if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0)
279 ret = error(_("could not write index"));
280 free((void *)desc.buffer);
285 reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT);
286 strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase");
287 prefix_len = msg.len;
289 if (!get_oid("ORIG_HEAD", &oid_old_orig))
290 old_orig = &oid_old_orig;
291 if (!get_oid("HEAD", &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);
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);
304 ret = create_symref("HEAD", switch_to_branch, msg.buf);
306 ret = update_ref(msg.buf, "HEAD", oid, NULL, 0,
307 UPDATE_REFS_MSG_ON_ERR);
310 strbuf_release(&msg);
314 int cmd_rebase(int argc, const char **argv, const char *prefix)
316 struct rebase_options options = {
317 .type = REBASE_UNSPECIFIED,
318 .flags = REBASE_NO_QUIET,
319 .git_am_opt = STRBUF_INIT,
321 const char *branch_name;
323 int ok_to_skip_pre_rebase = 0;
324 struct strbuf msg = STRBUF_INIT;
325 struct strbuf revisions = STRBUF_INIT;
326 struct object_id merge_base;
327 struct option builtin_rebase_options[] = {
328 OPT_STRING(0, "onto", &options.onto_name,
330 N_("rebase onto given branch instead of upstream")),
331 OPT_BOOL(0, "no-verify", &ok_to_skip_pre_rebase,
332 N_("allow pre-rebase hook to run")),
333 OPT_NEGBIT('q', "quiet", &options.flags,
334 N_("be quiet. implies --no-stat"),
340 * NEEDSWORK: Once the builtin rebase has been tested enough
341 * and git-legacy-rebase.sh is retired to contrib/, this preamble
345 if (!use_builtin_rebase()) {
346 const char *path = mkpath("%s/git-legacy-rebase",
349 if (sane_execvp(path, (char **)argv) < 0)
350 die_errno(_("could not exec %s"), path);
352 BUG("sane_execvp() returned???");
355 if (argc == 2 && !strcmp(argv[1], "-h"))
356 usage_with_options(builtin_rebase_usage,
357 builtin_rebase_options);
359 prefix = setup_git_directory();
360 trace_repo_setup(prefix);
363 git_config(git_default_config, NULL);
364 argc = parse_options(argc, argv, prefix,
365 builtin_rebase_options,
366 builtin_rebase_usage, 0);
369 usage_with_options(builtin_rebase_usage,
370 builtin_rebase_options);
372 if (!(options.flags & REBASE_NO_QUIET))
373 strbuf_addstr(&options.git_am_opt, " -q");
375 switch (options.type) {
377 case REBASE_INTERACTIVE:
378 case REBASE_PRESERVE_MERGES:
379 options.state_dir = merge_dir();
382 options.state_dir = apply_dir();
385 /* the default rebase backend is `--am` */
386 options.type = REBASE_AM;
387 options.state_dir = apply_dir();
393 die("TODO: handle @{upstream}");
395 options.upstream_name = argv[0];
398 if (!strcmp(options.upstream_name, "-"))
399 options.upstream_name = "@{-1}";
401 options.upstream = peel_committish(options.upstream_name);
402 if (!options.upstream)
403 die(_("invalid upstream '%s'"), options.upstream_name);
404 options.upstream_arg = options.upstream_name;
406 die("TODO: upstream for --root");
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, "...")) {
412 if (get_oid_mb(options.onto_name, &merge_base) < 0)
413 die(_("'%s': need exactly one merge base"),
415 options.onto = lookup_commit_or_die(&merge_base,
418 options.onto = peel_committish(options.onto_name);
420 die(_("Does not point to a valid commit '%s'"),
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"
432 die("TODO: handle switch_to");
434 /* Do not need to switch branches, we are already on it. */
436 xstrdup_or_null(resolve_ref_unsafe("HEAD", 0, NULL,
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;
446 options.head_name = xstrdup("detached HEAD");
447 branch_name = "HEAD";
449 if (get_oid("HEAD", &options.orig_head))
450 die(_("Could not resolve HEAD to a revision"));
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."));
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);
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));
471 options.revisions = revisions.buf;
473 ret = !!run_specific_rebase(&options);
475 strbuf_release(&revisions);
476 free(options.head_name);