read-cache.c: kill read_index()
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sat, 12 Jan 2019 02:13:26 +0000 (09:13 +0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 14 Jan 2019 20:13:04 +0000 (12:13 -0800)
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@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
16 files changed:
apply.c
blame.c
builtin/am.c
builtin/commit.c
builtin/diff-tree.c
builtin/rebase.c
cache.h
merge-recursive.c
merge.c
preload-index.c
read-cache.c
repository.h
rerere.c
revision.c
sequencer.c
sha1-name.c
diff --git a/apply.c b/apply.c
index 08cde3c4bf1ea6e6a5d855ea37de34a23130d638..e040c0b957be27c3f805ae3280d160161358a3ce 100644 (file)
--- 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 43861437f749849e85d448eba692b42488ec459d..da57233cbbd98d41bdfeba46e861b1f5f61dfbe2 100644 (file)
--- 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) {
index d32044545d7b10157042045161b839344cbbc8d8..901dc55078c904cd1cfd6d0a3e95bc398bc16d76 100644 (file)
@@ -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) {
index e29fb5e3eb003e21ec8b1f70bc09641f79ba2126..19eb6cff86545689ba7b1cfe5a4c1867b35ec822 100644 (file)
@@ -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);
index ef996126d7b521ea1de2c837e4ed97fdcb6f1e71..42bc1eb41db8287ab9f64445ec2a151f216d8840 100644 (file)
@@ -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)) {
index 00de70365ed41167c18e3233106e89d082370b6a..ce5f5b5a17d4887b1d34e3476f3edf06a330268e 100644 (file)
@@ -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 634c9ce3254565d9f3f05f803604b4c6adddc501..3715808f52c3daad6221edf49d84227d431fab4f 100644 (file)
--- 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)
index 8dba939d8f8393151a4ba2c1865c59f5c7a101fd..5fbd4253c1baa6a290c501112b4a4fdc55814eb1 100644 (file)
@@ -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 dbbc9d9f802fef2781b9b60b798700f49229f84f..7c1d756c3fad6f9570b31c2c36f96e9f90df39f4 100644 (file)
--- 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);
 
index c7dc3f2b9f62a762efd7f590d6e8959fc1707479..e73600ee7841a299e060369ca9eecf46d5a7fb0f 100644 (file)
@@ -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;
 }
index 1030e1105159f09f8a548176ed585963fb95eb47..afbf976107552dd15f9a4c5a946da6c1b012298a 100644 (file)
@@ -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;
index 968330218fd81a2f424f12142dfa40af790bdaa4..cc3879add46573382b44d12f2da3226eecfec665 100644 (file)
@@ -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 */
index fb0fdb23927b544c7221cd5c1b78e5314310066d..17abb47321f14ada67f8ffc6cbfb2ce0d527fa82 100644 (file)
--- 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);
index 13e0519c0241635c0c1fd14a7ff12e9478c70bd4..c51ea6a052cbd9fae77bb75038636764ee721ccf 100644 (file)
@@ -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))
index 668c232b05d5bfd9ee2369dbbf68eb444eb5824c..c2c9cc7e2b7fb3949f71b4edf5f8f70c77618ef4 100644 (file)
@@ -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++;
index b24502811bbad1bb89908f0914366e4f1cb9178b..a0fc1008ec4dfb8bdfb1ea0c447e12a1ecf703e2 100644 (file)
@@ -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) {