read_index() shares the same problem as hold_locked_index(): it assumes $GIT_DIR/index. Move all call sites to repo_read_index() instead. read_index_preload() and read_index_unmerged() are also killed as a consequence. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@xxxxxxxxx> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> --- apply.c | 2 +- blame.c | 4 ++-- builtin/am.c | 2 +- builtin/commit.c | 2 +- builtin/diff-tree.c | 2 +- builtin/rebase.c | 8 ++++---- cache.h | 11 +++-------- merge-recursive.c | 2 +- merge.c | 2 +- preload-index.c | 11 ++++++----- read-cache.c | 11 ++++------- repository.h | 6 ++++++ rerere.c | 6 +++--- revision.c | 4 ++-- sequencer.c | 20 ++++++++++---------- sha1-name.c | 6 +++--- 16 files changed, 49 insertions(+), 50 deletions(-) diff --git a/apply.c b/apply.c index 08cde3c4bf..e040c0b957 100644 --- a/apply.c +++ b/apply.c @@ -4019,7 +4019,7 @@ static int read_apply_cache(struct apply_state *state) return read_index_from(state->repo->index, state->index_file, get_git_dir()); else - return read_index(state->repo->index); + return repo_read_index(state->repo); } /* This function tries to read the object name from the current index */ diff --git a/blame.c b/blame.c index 43861437f7..da57233cbb 100644 --- a/blame.c +++ b/blame.c @@ -188,7 +188,7 @@ static struct commit *fake_working_tree_commit(struct repository *r, unsigned mode; struct strbuf msg = STRBUF_INIT; - read_index(r->index); + repo_read_index(r); time(&now); commit = alloc_commit_node(r); commit->object.parsed = 1; @@ -270,7 +270,7 @@ static struct commit *fake_working_tree_commit(struct repository *r, * want to run "diff-index --cached". */ discard_index(r->index); - read_index(r->index); + repo_read_index(r); len = strlen(path); if (!mode) { diff --git a/builtin/am.c b/builtin/am.c index d32044545d..901dc55078 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -2278,7 +2278,7 @@ int cmd_am(int argc, const char **argv, const char *prefix) /* Ensure a valid committer ident can be constructed */ git_committer_info(IDENT_STRICT); - if (read_index_preload(&the_index, NULL, 0) < 0) + if (repo_read_index_preload(the_repository, NULL, 0) < 0) die(_("failed to read the index")); if (in_progress) { diff --git a/builtin/commit.c b/builtin/commit.c index e29fb5e3eb..19eb6cff86 100644 --- a/builtin/commit.c +++ b/builtin/commit.c @@ -1367,7 +1367,7 @@ int cmd_status(int argc, const char **argv, const char *prefix) if (status_format != STATUS_FORMAT_PORCELAIN && status_format != STATUS_FORMAT_PORCELAIN_V2) progress_flag = REFRESH_PROGRESS; - read_index(&the_index); + repo_read_index(the_repository); refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED|progress_flag, &s.pathspec, NULL, NULL); diff --git a/builtin/diff-tree.c b/builtin/diff-tree.c index ef996126d7..42bc1eb41d 100644 --- a/builtin/diff-tree.c +++ b/builtin/diff-tree.c @@ -165,7 +165,7 @@ int cmd_diff_tree(int argc, const char **argv, const char *prefix) if (opt->diffopt.detect_rename) { if (!the_index.cache) - read_index(&the_index); + repo_read_index(the_repository); opt->diffopt.setup |= DIFF_SETUP_USE_SIZE_CACHE; } while (fgets(line, sizeof(line), stdin)) { diff --git a/builtin/rebase.c b/builtin/rebase.c index 00de70365e..ce5f5b5a17 100644 --- a/builtin/rebase.c +++ b/builtin/rebase.c @@ -576,7 +576,7 @@ static int reset_head(struct object_id *oid, const char *action, 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; } @@ -1015,7 +1015,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) die(_("Cannot read HEAD")); fd = hold_locked_index(&lock_file, 0); - if (read_index(the_repository->index) < 0) + if (repo_read_index(the_repository) < 0) die(_("could not read index")); refresh_index(the_repository->index, REFRESH_QUIET, NULL, NULL, NULL); @@ -1368,7 +1368,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) get_fork_point(options.upstream_name, head); } - if (read_index(the_repository->index) < 0) + if (repo_read_index(the_repository) < 0) die(_("could not read index")); if (options.autostash) { @@ -1423,7 +1423,7 @@ int cmd_rebase(int argc, const char **argv, const char *prefix) putchar('\n'); if (discard_index(the_repository->index) < 0 || - read_index(the_repository->index) < 0) + repo_read_index(the_repository) < 0) die(_("could not read index")); } } diff --git a/cache.h b/cache.h index 634c9ce325..3715808f52 100644 --- a/cache.h +++ b/cache.h @@ -408,11 +408,11 @@ void validate_cache_entries(const struct index_state *istate); #define active_cache_changed (the_index.cache_changed) #define active_cache_tree (the_index.cache_tree) -#define read_cache() read_index(&the_index) +#define read_cache() repo_read_index(the_repository) #define read_cache_from(path) read_index_from(&the_index, (path), (get_git_dir())) -#define read_cache_preload(pathspec) read_index_preload(&the_index, (pathspec), 0) +#define read_cache_preload(pathspec) repo_read_index_preload(the_repository, (pathspec), 0) #define is_cache_unborn() is_index_unborn(&the_index) -#define read_cache_unmerged() read_index_unmerged(&the_index) +#define read_cache_unmerged() repo_read_index_unmerged(the_repository) #define discard_cache() discard_index(&the_index) #define unmerged_cache() unmerged_index(&the_index) #define cache_name_pos(name, namelen) index_name_pos(&the_index,(name),(namelen)) @@ -661,19 +661,14 @@ extern int daemonize(void); /* Initialize and use the cache information */ struct lock_file; -extern int read_index(struct index_state *); extern void preload_index(struct index_state *index, const struct pathspec *pathspec, unsigned int refresh_flags); -extern int read_index_preload(struct index_state *, - const struct pathspec *pathspec, - unsigned int refresh_flags); extern int do_read_index(struct index_state *istate, const char *path, int must_exist); /* for testting only! */ extern int read_index_from(struct index_state *, const char *path, const char *gitdir); extern int is_index_unborn(struct index_state *); -extern int read_index_unmerged(struct index_state *); /* For use with `write_locked_index()`. */ #define COMMIT_LOCK (1 << 0) diff --git a/merge-recursive.c b/merge-recursive.c index 8dba939d8f..5fbd4253c1 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -3576,7 +3576,7 @@ int merge_recursive(struct merge_options *o, discard_cache(); if (!o->call_depth) - read_cache(); + repo_read_index(the_repository); o->ancestor = "merged common ancestors"; clean = merge_trees(o, get_commit_tree(h1), get_commit_tree(h2), diff --git a/merge.c b/merge.c index dbbc9d9f80..7c1d756c3f 100644 --- a/merge.c +++ b/merge.c @@ -37,7 +37,7 @@ int try_merge_command(struct repository *r, argv_array_clear(&args); discard_index(r->index); - if (read_index(r->index) < 0) + if (repo_read_index(r) < 0) die(_("failed to read the cache")); resolve_undo_clear_index(r->index); diff --git a/preload-index.c b/preload-index.c index c7dc3f2b9f..e73600ee78 100644 --- a/preload-index.c +++ b/preload-index.c @@ -8,6 +8,7 @@ #include "config.h" #include "progress.h" #include "thread-utils.h" +#include "repository.h" /* * Mostly randomly chosen maximum thread counts: we @@ -146,12 +147,12 @@ void preload_index(struct index_state *index, trace_performance_leave("preload index"); } -int read_index_preload(struct index_state *index, - const struct pathspec *pathspec, - unsigned int refresh_flags) +int repo_read_index_preload(struct repository *repo, + const struct pathspec *pathspec, + unsigned int refresh_flags) { - int retval = read_index(index); + int retval = repo_read_index(repo); - preload_index(index, pathspec, refresh_flags); + preload_index(repo->index, pathspec, refresh_flags); return retval; } diff --git a/read-cache.c b/read-cache.c index 1030e11051..afbf976107 100644 --- a/read-cache.c +++ b/read-cache.c @@ -1733,11 +1733,6 @@ static int read_index_extension(struct index_state *istate, return 0; } -int read_index(struct index_state *istate) -{ - return read_index_from(istate, get_index_file(), get_git_dir()); -} - static struct cache_entry *create_from_disk(struct mem_pool *ce_mem_pool, unsigned int version, struct ondisk_cache_entry *ondisk, @@ -3218,12 +3213,14 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock, * state can call this and check its return value, instead of calling * read_cache(). */ -int read_index_unmerged(struct index_state *istate) +int repo_read_index_unmerged(struct repository *repo) { + struct index_state *istate; int i; int unmerged = 0; - read_index(istate); + repo_read_index(repo); + istate = repo->index; for (i = 0; i < istate->cache_nr; i++) { struct cache_entry *ce = istate->cache[i]; struct cache_entry *new_ce; diff --git a/repository.h b/repository.h index 968330218f..cc3879add4 100644 --- a/repository.h +++ b/repository.h @@ -7,6 +7,7 @@ struct config_set; struct git_hash_algo; struct index_state; struct lock_file; +struct pathspec; struct raw_object_store; struct submodule_cache; @@ -135,4 +136,9 @@ int repo_hold_locked_index(struct repository *repo, struct lock_file *lf, int flags); +int repo_read_index_preload(struct repository *, + const struct pathspec *pathspec, + unsigned refresh_flags); +int repo_read_index_unmerged(struct repository *); + #endif /* REPOSITORY_H */ diff --git a/rerere.c b/rerere.c index fb0fdb2392..17abb47321 100644 --- a/rerere.c +++ b/rerere.c @@ -561,7 +561,7 @@ static int find_conflict(struct repository *r, struct string_list *conflict) { int i; - if (read_index(r->index) < 0) + if (repo_read_index(r) < 0) return error(_("index file corrupt")); for (i = 0; i < r->index->cache_nr;) { @@ -595,7 +595,7 @@ int rerere_remaining(struct repository *r, struct string_list *merge_rr) if (setup_rerere(r, merge_rr, RERERE_READONLY)) return 0; - if (read_index(r->index) < 0) + if (repo_read_index(r) < 0) return error(_("index file corrupt")); for (i = 0; i < r->index->cache_nr;) { @@ -1107,7 +1107,7 @@ int rerere_forget(struct repository *r, struct pathspec *pathspec) struct string_list conflict = STRING_LIST_INIT_DUP; struct string_list merge_rr = STRING_LIST_INIT_DUP; - if (read_index(r->index) < 0) + if (repo_read_index(r) < 0) return error(_("index file corrupt")); fd = setup_rerere(r, &merge_rr, RERERE_NOAUTOUPDATE); diff --git a/revision.c b/revision.c index 13e0519c02..c51ea6a052 100644 --- a/revision.c +++ b/revision.c @@ -1384,7 +1384,7 @@ void add_index_objects_to_pending(struct rev_info *revs, unsigned int flags) { struct worktree **worktrees, **p; - read_index(revs->repo->index); + repo_read_index(revs->repo); do_add_index_objects_to_pending(revs, revs->repo->index, flags); if (revs->single_worktree) @@ -1530,7 +1530,7 @@ static void prepare_show_merge(struct rev_info *revs) head->object.flags |= SYMMETRIC_LEFT; if (!istate->cache_nr) - read_index(istate); + repo_read_index(revs->repo); for (i = 0; i < istate->cache_nr; i++) { const struct cache_entry *ce = istate->cache[i]; if (!ce_stage(ce)) diff --git a/sequencer.c b/sequencer.c index 668c232b05..c2c9cc7e2b 100644 --- a/sequencer.c +++ b/sequencer.c @@ -446,9 +446,9 @@ static struct tree *empty_tree(struct repository *r) return lookup_tree(r, the_hash_algo->empty_tree); } -static int error_dirty_index(struct index_state *istate, struct replay_opts *opts) +static int error_dirty_index(struct repository *repo, struct replay_opts *opts) { - if (read_index_unmerged(istate)) + if (repo_read_index_unmerged(repo)) return error_resolve_conflict(_(action_name(opts))); error(_("your local changes would be overwritten by %s."), @@ -483,7 +483,7 @@ static int fast_forward_to(struct repository *r, struct strbuf sb = STRBUF_INIT; struct strbuf err = STRBUF_INIT; - read_index(r->index); + repo_read_index(r); if (checkout_fast_forward(r, from, to, 1)) return -1; /* the callee should have complained already */ @@ -543,7 +543,7 @@ static int do_recursive_merge(struct repository *r, if (repo_hold_locked_index(r, &index_lock, LOCK_REPORT_ON_ERROR) < 0) return -1; - read_index(r->index); + repo_read_index(r); init_merge_options(&o); o.ancestor = base ? base_label : "(empty tree)"; @@ -1766,7 +1766,7 @@ static int do_pick_commit(struct repository *r, oidcpy(&head, the_hash_algo->empty_tree); if (index_differs_from(r, unborn ? empty_tree_oid_hex() : "HEAD", NULL, 0)) - return error_dirty_index(r->index, opts); + return error_dirty_index(r, opts); } discard_index(r->index); @@ -2854,7 +2854,7 @@ static int do_exec(struct repository *r, const char *command_line) child_env.argv); /* force re-reading of the cache */ - if (discard_index(r->index) < 0 || read_index(r->index) < 0) + if (discard_index(r->index) < 0 || repo_read_index(r) < 0) return error(_("could not read index")); dirty = require_clean_work_tree(r, "rebase", NULL, 1, 1); @@ -3023,7 +3023,7 @@ static int do_reset(struct repository *r, unpack_tree_opts.merge = 1; unpack_tree_opts.update = 1; - if (read_index_unmerged(r->index)) { + if (repo_read_index_unmerged(r)) { rollback_lock_file(&lock); strbuf_release(&ref_name); return error_resolve_conflict(_(action_name(opts))); @@ -3277,7 +3277,7 @@ static int do_merge(struct repository *r, /* force re-reading of the cache */ if (!ret && (discard_index(r->index) < 0 || - read_index(r->index) < 0)) + repo_read_index(r) < 0)) ret = error(_("could not read index")); goto leave_merge; } @@ -3299,7 +3299,7 @@ static int do_merge(struct repository *r, commit_list_insert(j->item, &reversed); free_commit_list(bases); - read_index(r->index); + repo_read_index(r); init_merge_options(&o); o.branch1 = "HEAD"; o.branch2 = ref_name.buf; @@ -3972,7 +3972,7 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts) goto release_todo_list; } if (index_differs_from(r, "HEAD", NULL, 0)) { - res = error_dirty_index(r->index, opts); + res = error_dirty_index(r, opts); goto release_todo_list; } todo_list.current++; diff --git a/sha1-name.c b/sha1-name.c index b24502811b..a0fc1008ec 100644 --- a/sha1-name.c +++ b/sha1-name.c @@ -1723,9 +1723,9 @@ static int get_oid_with_context_1(const char *name, if (flags & GET_OID_RECORD_PATH) oc->path = xstrdup(cp); - if (!active_cache) - read_cache(); - pos = cache_name_pos(cp, namelen); + if (!the_index.cache) + repo_read_index(the_repository); + pos = index_name_pos(&the_index, cp, namelen); if (pos < 0) pos = -pos - 1; while (pos < active_nr) { -- 2.20.1.560.g70ca8b83ee