From: Junio C Hamano Date: Thu, 7 Feb 2019 06:05:22 +0000 (-0800) Subject: Merge branch 'nd/the-index-final' X-Git-Tag: v2.21.0-rc0~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7589e63648bf5224e186990931b1491f36e10a4b;p=git Merge branch 'nd/the-index-final' The assumption to work on the single "in-core index" instance has been reduced from the library-ish part of the codebase. * nd/the-index-final: cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch read-cache.c: remove the_* from index_has_changes() merge-recursive.c: remove implicit dependency on the_repository merge-recursive.c: remove implicit dependency on the_index sha1-name.c: remove implicit dependency on the_index read-cache.c: replace update_index_if_able with repo_& read-cache.c: kill read_index() checkout: avoid the_index when possible repository.c: replace hold_locked_index() with repo_hold_locked_index() notes-utils.c: remove the_repository references grep: use grep_opt->repo instead of explict repo argument --- 7589e63648bf5224e186990931b1491f36e10a4b diff --cc builtin/cat-file.c index cebc6d7f8a,a5ca47c697..8487cd7dba --- a/builtin/cat-file.c +++ b/builtin/cat-file.c @@@ -380,9 -382,10 +382,10 @@@ static void batch_one_object(const cha { struct object_context ctx; int flags = opt->follow_symlinks ? GET_OID_FOLLOW_SYMLINKS : 0; - enum follow_symlinks_result result; + enum get_oid_result result; - result = get_oid_with_context(obj_name, flags, &data->oid, &ctx); + result = get_oid_with_context(the_repository, obj_name, + flags, &data->oid, &ctx); if (result != FOUND) { switch (result) { case MISSING_OBJECT: diff --cc builtin/grep.c index dd52ea968b,39a8e9d4a3..580fd38f41 --- a/builtin/grep.c +++ b/builtin/grep.c @@@ -404,10 -405,9 +405,11 @@@ static int grep_submodule(struct grep_o const struct object_id *oid, const char *filename, const char *path) { + struct repository subrepo; + struct repository *superproject = opt->repo; - struct repository submodule; + const struct submodule *sub = submodule_from_path(superproject, + &null_oid, path); - + struct grep_opt subopt; int hit; /* @@@ -440,9 -440,12 +442,12 @@@ * store is no longer global and instead is a member of the repository * object. */ - add_to_alternates_memory(submodule.objects->odb->path); + add_to_alternates_memory(subrepo.objects->odb->path); grep_read_unlock(); + memcpy(&subopt, opt, sizeof(subopt)); - subopt.repo = &submodule; ++ subopt.repo = &subrepo; + if (oid) { struct object *object; struct tree_desc tree; @@@ -469,10 -472,10 +474,10 @@@ strbuf_release(&base); free(data); } else { - hit = grep_cache(opt, &subrepo, pathspec, 1); + hit = grep_cache(&subopt, pathspec, 1); } - repo_clear(&submodule); + repo_clear(&subrepo); return hit; } @@@ -586,10 -590,10 +593,10 @@@ static int grep_tree(struct grep_opt *o strbuf_addch(base, '/'); init_tree_desc(&sub, data, size); hit |= grep_tree(opt, pathspec, &sub, base, tn_len, - check_attr, repo); + check_attr); free(data); } else if (recurse_submodules && S_ISGITLINK(entry.mode)) { - hit |= grep_submodule(opt, repo, pathspec, &entry.oid, - hit |= grep_submodule(opt, pathspec, entry.oid, ++ hit |= grep_submodule(opt, pathspec, &entry.oid, base->buf, base->buf + tn_len); } diff --cc builtin/merge-recursive.c index 7545136c2a,4864f7b22f..5b910e351e --- a/builtin/merge-recursive.c +++ b/builtin/merge-recursive.c @@@ -26,10 -26,9 +26,10 @@@ int cmd_merge_recursive(int argc, cons int i, failed; struct object_id h1, h2; struct merge_options o; + char *better1, *better2; struct commit *result; - init_merge_options(&o); + init_merge_options(&o, the_repository); if (argv[0] && ends_with(argv[0], "-subtree")) o.subtree_shift = ""; diff --cc builtin/rebase.c index 8d49e53997,b667837276..0b039319e1 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@@ -362,161 -334,6 +363,161 @@@ static void add_var(struct strbuf *buf } } +#define GIT_REFLOG_ACTION_ENVIRONMENT "GIT_REFLOG_ACTION" + +#define RESET_HEAD_DETACH (1<<0) +#define RESET_HEAD_HARD (1<<1) +#define RESET_HEAD_RUN_POST_CHECKOUT_HOOK (1<<2) +#define RESET_HEAD_REFS_ONLY (1<<3) + +static int reset_head(struct object_id *oid, const char *action, + const char *switch_to_branch, unsigned flags, + const char *reflog_orig_head, const char *reflog_head) +{ + unsigned detach_head = flags & RESET_HEAD_DETACH; + unsigned reset_hard = flags & RESET_HEAD_HARD; + unsigned run_hook = flags & RESET_HEAD_RUN_POST_CHECKOUT_HOOK; + unsigned refs_only = flags & RESET_HEAD_REFS_ONLY; + struct object_id head_oid; + struct tree_desc desc[2] = { { NULL }, { NULL } }; + struct lock_file lock = LOCK_INIT; + struct unpack_trees_options unpack_tree_opts; + struct tree *tree; + const char *reflog_action; + struct strbuf msg = STRBUF_INIT; + size_t prefix_len; + struct object_id *orig = NULL, oid_orig, + *old_orig = NULL, oid_old_orig; + int ret = 0, nr = 0; + + if (switch_to_branch && !starts_with(switch_to_branch, "refs/")) + BUG("Not a fully qualified branch: '%s'", switch_to_branch); + + if (!refs_only && hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0) { + ret = -1; + goto leave_reset_head; + } + + if ((!oid || !reset_hard) && get_oid("HEAD", &head_oid)) { + ret = error(_("could not determine HEAD revision")); + goto leave_reset_head; + } + + if (!oid) + oid = &head_oid; + + if (refs_only) + goto reset_head_refs; + + memset(&unpack_tree_opts, 0, sizeof(unpack_tree_opts)); + setup_unpack_trees_porcelain(&unpack_tree_opts, action); + unpack_tree_opts.head_idx = 1; + unpack_tree_opts.src_index = the_repository->index; + unpack_tree_opts.dst_index = the_repository->index; + unpack_tree_opts.fn = reset_hard ? oneway_merge : twoway_merge; + unpack_tree_opts.update = 1; + unpack_tree_opts.merge = 1; + if (!detach_head) + unpack_tree_opts.reset = 1; + - if (read_index_unmerged(the_repository->index) < 0) { ++ if (repo_read_index_unmerged(the_repository) < 0) { + ret = error(_("could not read index")); + goto leave_reset_head; + } + + if (!reset_hard && !fill_tree_descriptor(&desc[nr++], &head_oid)) { + ret = error(_("failed to find tree of %s"), + oid_to_hex(&head_oid)); + goto leave_reset_head; + } + + if (!fill_tree_descriptor(&desc[nr++], oid)) { + ret = error(_("failed to find tree of %s"), oid_to_hex(oid)); + goto leave_reset_head; + } + + if (unpack_trees(nr, desc, &unpack_tree_opts)) { + ret = -1; + goto leave_reset_head; + } + + tree = parse_tree_indirect(oid); + prime_cache_tree(the_repository, the_repository->index, tree); + + if (write_locked_index(the_repository->index, &lock, COMMIT_LOCK) < 0) { + ret = error(_("could not write index")); + goto leave_reset_head; + } + +reset_head_refs: + reflog_action = getenv(GIT_REFLOG_ACTION_ENVIRONMENT); + strbuf_addf(&msg, "%s: ", reflog_action ? reflog_action : "rebase"); + prefix_len = msg.len; + + if (!get_oid("ORIG_HEAD", &oid_old_orig)) + old_orig = &oid_old_orig; + if (!get_oid("HEAD", &oid_orig)) { + orig = &oid_orig; + if (!reflog_orig_head) { + strbuf_addstr(&msg, "updating ORIG_HEAD"); + reflog_orig_head = msg.buf; + } + update_ref(reflog_orig_head, "ORIG_HEAD", orig, old_orig, 0, + UPDATE_REFS_MSG_ON_ERR); + } else if (old_orig) + delete_ref(NULL, "ORIG_HEAD", old_orig, 0); + if (!reflog_head) { + strbuf_setlen(&msg, prefix_len); + strbuf_addstr(&msg, "updating HEAD"); + reflog_head = msg.buf; + } + if (!switch_to_branch) + ret = update_ref(reflog_head, "HEAD", oid, orig, + detach_head ? REF_NO_DEREF : 0, + UPDATE_REFS_MSG_ON_ERR); + else { + ret = update_ref(reflog_orig_head, switch_to_branch, oid, + NULL, 0, UPDATE_REFS_MSG_ON_ERR); + if (!ret) + ret = create_symref("HEAD", switch_to_branch, + reflog_head); + } + if (run_hook) + run_hook_le(NULL, "post-checkout", + oid_to_hex(orig ? orig : &null_oid), + oid_to_hex(oid), "1", NULL); + +leave_reset_head: + strbuf_release(&msg); + rollback_lock_file(&lock); + while (nr) + free((void *)desc[--nr].buffer); + return ret; +} + +static int move_to_original_branch(struct rebase_options *opts) +{ + struct strbuf orig_head_reflog = STRBUF_INIT, head_reflog = STRBUF_INIT; + int ret; + + if (!opts->head_name) + return 0; /* nothing to move back to */ + + if (!opts->onto) + BUG("move_to_original_branch without onto"); + + strbuf_addf(&orig_head_reflog, "rebase finished: %s onto %s", + opts->head_name, oid_to_hex(&opts->onto->object.oid)); + strbuf_addf(&head_reflog, "rebase finished: returning to %s", + opts->head_name); + ret = reset_head(NULL, "", opts->head_name, RESET_HEAD_REFS_ONLY, + orig_head_reflog.buf, head_reflog.buf); + + strbuf_release(&orig_head_reflog); + strbuf_release(&head_reflog); + return ret; +} + static const char *resolvemsg = N_("Resolve all conflicts manually, mark them as resolved with\n" "\"git add/rm \", then run \"git rebase --continue\".\n" diff --cc cache.h index bcf9fd445f,962eb127d3..c653668340 --- a/cache.h +++ b/cache.h @@@ -1370,9 -1328,11 +1359,10 @@@ extern int get_oid_tree(const char *str extern int get_oid_treeish(const char *str, struct object_id *oid); extern int get_oid_blob(const char *str, struct object_id *oid); extern void maybe_die_on_misspelt_object_name(const char *name, const char *prefix); - extern enum get_oid_result get_oid_with_context(const char *str, unsigned flags, struct object_id *oid, struct object_context *oc); - -extern int get_oid_with_context(struct repository *repo, const char *str, ++extern enum get_oid_result get_oid_with_context(struct repository *repo, const char *str, + unsigned flags, struct object_id *oid, + struct object_context *oc); - typedef int each_abbrev_fn(const struct object_id *oid, void *); extern int for_each_abbrev(const char *prefix, each_abbrev_fn, void *); diff --cc sha1-name.c index 4acd406b0a,d8cab7b5a6..d1cc77c124 --- a/sha1-name.c +++ b/sha1-name.c @@@ -1666,11 -1670,12 +1672,12 @@@ static char *resolve_relative_path(cons rel); } - static enum get_oid_result get_oid_with_context_1(const char *name, - unsigned flags, - const char *prefix, - struct object_id *oid, - struct object_context *oc) -static int get_oid_with_context_1(struct repository *repo, ++static enum get_oid_result get_oid_with_context_1(struct repository *repo, + const char *name, + unsigned flags, + const char *prefix, + struct object_id *oid, + struct object_context *oc) { int ret, bracket_depth; int namelen = strlen(name);