resolve_missing_loose_ref(): add a files_ref_store argument
[gitweb.git] / refs / files-backend.c
index 395056b67598f84120a7fa9443fdf4328b1c0e20..9c81af900c1805e26d623e2a560c5ab0f67212d2 100644 (file)
@@ -2,6 +2,7 @@
 #include "../refs.h"
 #include "refs-internal.h"
 #include "../iterator.h"
+#include "../dir-iterator.h"
 #include "../lockfile.h"
 #include "../object.h"
 #include "../dir.h"
@@ -38,7 +39,7 @@ struct ref_value {
        struct object_id peeled;
 };
 
-struct ref_cache;
+struct files_ref_store;
 
 /*
  * Information used (along with the information in ref_entry) to
@@ -77,8 +78,8 @@ struct ref_dir {
         */
        int sorted;
 
-       /* A pointer to the ref_cache that contains this ref_dir. */
-       struct ref_cache *ref_cache;
+       /* A pointer to the files_ref_store that contains this ref_dir. */
+       struct files_ref_store *ref_store;
 
        struct ref_entry **entries;
 };
@@ -160,7 +161,7 @@ struct ref_entry {
 
 static void read_loose_refs(const char *dirname, struct ref_dir *dir);
 static int search_ref_dir(struct ref_dir *dir, const char *refname, size_t len);
-static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
+static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
                                          const char *dirname, size_t len,
                                          int incomplete);
 static void add_entry_to_dir(struct ref_dir *dir, struct ref_entry *entry);
@@ -182,7 +183,7 @@ static struct ref_dir *get_ref_dir(struct ref_entry *entry)
                        int pos = search_ref_dir(dir, "refs/bisect/", 12);
                        if (pos < 0) {
                                struct ref_entry *child_entry;
-                               child_entry = create_dir_entry(dir->ref_cache,
+                               child_entry = create_dir_entry(dir->ref_store,
                                                               "refs/bisect/",
                                                               12, 1);
                                add_entry_to_dir(dir, child_entry);
@@ -260,13 +261,13 @@ static void clear_ref_dir(struct ref_dir *dir)
  * dirname is the name of the directory with a trailing slash (e.g.,
  * "refs/heads/") or "" for the top-level directory.
  */
-static struct ref_entry *create_dir_entry(struct ref_cache *ref_cache,
+static struct ref_entry *create_dir_entry(struct files_ref_store *ref_store,
                                          const char *dirname, size_t len,
                                          int incomplete)
 {
        struct ref_entry *direntry;
        FLEX_ALLOC_MEM(direntry, name, dirname, len);
-       direntry->u.subdir.ref_cache = ref_cache;
+       direntry->u.subdir.ref_store = ref_store;
        direntry->flag = REF_DIR | (incomplete ? REF_INCOMPLETE : 0);
        return direntry;
 }
@@ -342,7 +343,7 @@ static struct ref_dir *search_for_subdir(struct ref_dir *dir,
                 * therefore, create an empty record for it but mark
                 * the record complete.
                 */
-               entry = create_dir_entry(dir->ref_cache, subdirname, len, 0);
+               entry = create_dir_entry(dir->ref_store, subdirname, len, 0);
                add_entry_to_dir(dir, entry);
        } else {
                entry = dir->entries[entry_index];
@@ -542,53 +543,8 @@ static int entry_resolves_to_object(struct ref_entry *entry)
                                      &entry->u.value.oid, entry->flag);
 }
 
-/*
- * current_ref is a performance hack: when iterating over references
- * using the for_each_ref*() functions, current_ref is set to the
- * current reference's entry before calling the callback function.  If
- * the callback function calls peel_ref(), then peel_ref() first
- * checks whether the reference to be peeled is the current reference
- * (it usually is) and if so, returns that reference's peeled version
- * if it is available.  This avoids a refname lookup in a common case.
- */
-static struct ref_entry *current_ref;
-
 typedef int each_ref_entry_fn(struct ref_entry *entry, void *cb_data);
 
-struct ref_entry_cb {
-       const char *prefix;
-       int trim;
-       int flags;
-       each_ref_fn *fn;
-       void *cb_data;
-};
-
-/*
- * Handle one reference in a do_for_each_ref*()-style iteration,
- * calling an each_ref_fn for each entry.
- */
-static int do_one_ref(struct ref_entry *entry, void *cb_data)
-{
-       struct ref_entry_cb *data = cb_data;
-       struct ref_entry *old_current_ref;
-       int retval;
-
-       if (!starts_with(entry->name, data->prefix))
-               return 0;
-
-       if (!(data->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
-             !entry_resolves_to_object(entry))
-               return 0;
-
-       /* Store the old value, in case this is a recursive call: */
-       old_current_ref = current_ref;
-       current_ref = entry;
-       retval = data->fn(entry->name + data->trim, &entry->u.value.oid,
-                         entry->flag, data->cb_data);
-       current_ref = old_current_ref;
-       return retval;
-}
-
 /*
  * Call fn for each reference in dir that has index in the range
  * offset <= index < dir->nr.  Recurse into subdirectories that are in
@@ -617,78 +573,6 @@ static int do_for_each_entry_in_dir(struct ref_dir *dir, int offset,
        return 0;
 }
 
-/*
- * Call fn for each reference in the union of dir1 and dir2, in order
- * by refname.  Recurse into subdirectories.  If a value entry appears
- * in both dir1 and dir2, then only process the version that is in
- * dir2.  The input dirs must already be sorted, but subdirs will be
- * sorted as needed.  fn is called for all references, including
- * broken ones.
- */
-static int do_for_each_entry_in_dirs(struct ref_dir *dir1,
-                                    struct ref_dir *dir2,
-                                    each_ref_entry_fn fn, void *cb_data)
-{
-       int retval;
-       int i1 = 0, i2 = 0;
-
-       assert(dir1->sorted == dir1->nr);
-       assert(dir2->sorted == dir2->nr);
-       while (1) {
-               struct ref_entry *e1, *e2;
-               int cmp;
-               if (i1 == dir1->nr) {
-                       return do_for_each_entry_in_dir(dir2, i2, fn, cb_data);
-               }
-               if (i2 == dir2->nr) {
-                       return do_for_each_entry_in_dir(dir1, i1, fn, cb_data);
-               }
-               e1 = dir1->entries[i1];
-               e2 = dir2->entries[i2];
-               cmp = strcmp(e1->name, e2->name);
-               if (cmp == 0) {
-                       if ((e1->flag & REF_DIR) && (e2->flag & REF_DIR)) {
-                               /* Both are directories; descend them in parallel. */
-                               struct ref_dir *subdir1 = get_ref_dir(e1);
-                               struct ref_dir *subdir2 = get_ref_dir(e2);
-                               sort_ref_dir(subdir1);
-                               sort_ref_dir(subdir2);
-                               retval = do_for_each_entry_in_dirs(
-                                               subdir1, subdir2, fn, cb_data);
-                               i1++;
-                               i2++;
-                       } else if (!(e1->flag & REF_DIR) && !(e2->flag & REF_DIR)) {
-                               /* Both are references; ignore the one from dir1. */
-                               retval = fn(e2, cb_data);
-                               i1++;
-                               i2++;
-                       } else {
-                               die("conflict between reference and directory: %s",
-                                   e1->name);
-                       }
-               } else {
-                       struct ref_entry *e;
-                       if (cmp < 0) {
-                               e = e1;
-                               i1++;
-                       } else {
-                               e = e2;
-                               i2++;
-                       }
-                       if (e->flag & REF_DIR) {
-                               struct ref_dir *subdir = get_ref_dir(e);
-                               sort_ref_dir(subdir);
-                               retval = do_for_each_entry_in_dir(
-                                               subdir, 0, fn, cb_data);
-                       } else {
-                               retval = fn(e, cb_data);
-                       }
-               }
-               if (retval)
-                       return retval;
-       }
-}
-
 /*
  * Load all of the refs from the dir into our in-memory cache. The hard work
  * of loading loose refs is done by get_ref_dir(), so we just need to recurse
@@ -1003,9 +887,9 @@ struct packed_ref_cache {
 
        /*
         * Count of references to the data structure in this instance,
-        * including the pointer from ref_cache::packed if any.  The
-        * data will not be freed as long as the reference count is
-        * nonzero.
+        * including the pointer from files_ref_store::packed if any.
+        * The data will not be freed as long as the reference count
+        * is nonzero.
         */
        unsigned int referrers;
 
@@ -1026,17 +910,11 @@ struct packed_ref_cache {
  * Future: need to be in "struct repository"
  * when doing a full libification.
  */
-static struct ref_cache {
-       struct ref_cache *next;
+struct files_ref_store {
+       struct ref_store base;
        struct ref_entry *loose;
        struct packed_ref_cache *packed;
-       /*
-        * The submodule name, or "" for the main repo.  We allocate
-        * length 1 rather than FLEX_ARRAY so that the main ref_cache
-        * is initialized correctly.
-        */
-       char name[1];
-} ref_cache, *submodule_ref_caches;
+};
 
 /* Lock used for the main packed-refs file: */
 static struct lock_file packlock;
@@ -1065,7 +943,7 @@ static int release_packed_ref_cache(struct packed_ref_cache *packed_refs)
        }
 }
 
-static void clear_packed_ref_cache(struct ref_cache *refs)
+static void clear_packed_ref_cache(struct files_ref_store *refs)
 {
        if (refs->packed) {
                struct packed_ref_cache *packed_refs = refs->packed;
@@ -1077,7 +955,7 @@ static void clear_packed_ref_cache(struct ref_cache *refs)
        }
 }
 
-static void clear_loose_ref_cache(struct ref_cache *refs)
+static void clear_loose_ref_cache(struct files_ref_store *refs)
 {
        if (refs->loose) {
                free_ref_entry(refs->loose);
@@ -1089,53 +967,50 @@ static void clear_loose_ref_cache(struct ref_cache *refs)
  * Create a new submodule ref cache and add it to the internal
  * set of caches.
  */
-static struct ref_cache *create_ref_cache(const char *submodule)
+static struct ref_store *files_ref_store_create(const char *submodule)
 {
-       struct ref_cache *refs;
-       if (!submodule)
-               submodule = "";
-       FLEX_ALLOC_STR(refs, name, submodule);
-       refs->next = submodule_ref_caches;
-       submodule_ref_caches = refs;
-       return refs;
+       struct files_ref_store *refs = xcalloc(1, sizeof(*refs));
+       struct ref_store *ref_store = (struct ref_store *)refs;
+
+       base_ref_store_init(ref_store, &refs_be_files, submodule);
+
+       return ref_store;
 }
 
-static struct ref_cache *lookup_ref_cache(const char *submodule)
+/*
+ * Downcast ref_store to files_ref_store. Die if ref_store is not a
+ * files_ref_store. If submodule_allowed is not true, then also die if
+ * files_ref_store is for a submodule (i.e., not for the main
+ * repository). caller is used in any necessary error messages.
+ */
+static struct files_ref_store *files_downcast(
+               struct ref_store *ref_store, int submodule_allowed,
+               const char *caller)
 {
-       struct ref_cache *refs;
+       if (ref_store->be != &refs_be_files)
+               die("BUG: ref_store is type \"%s\" not \"files\" in %s",
+                   ref_store->be->name, caller);
 
-       if (!submodule || !*submodule)
-               return &ref_cache;
+       if (!submodule_allowed)
+               assert_main_repository(ref_store, caller);
 
-       for (refs = submodule_ref_caches; refs; refs = refs->next)
-               if (!strcmp(submodule, refs->name))
-                       return refs;
-       return NULL;
+       return (struct files_ref_store *)ref_store;
 }
 
 /*
- * Return a pointer to a ref_cache for the specified submodule. For
- * the main repository, use submodule==NULL; such a call cannot fail.
- * For a submodule, the submodule must exist and be a nonbare
- * repository, otherwise return NULL.
- *
- * The returned structure will be allocated and initialized but not
- * necessarily populated; it should not be freed.
+ * Return a pointer to the reference store for the specified
+ * submodule. For the main repository, use submodule==NULL; such a
+ * call cannot fail. For a submodule, the submodule must exist and be
+ * a nonbare repository, otherwise return NULL. Verify that the
+ * reference store is a files_ref_store, and cast it to that type
+ * before returning it.
  */
-static struct ref_cache *get_ref_cache(const char *submodule)
+static struct files_ref_store *get_files_ref_store(const char *submodule,
+                                                  const char *caller)
 {
-       struct ref_cache *refs = lookup_ref_cache(submodule);
+       struct ref_store *refs = get_ref_store(submodule);
 
-       if (!refs) {
-               struct strbuf submodule_sb = STRBUF_INIT;
-
-               strbuf_addstr(&submodule_sb, submodule);
-               if (is_nonbare_repository_dir(&submodule_sb))
-                       refs = create_ref_cache(submodule);
-               strbuf_release(&submodule_sb);
-       }
-
-       return refs;
+       return refs ? files_downcast(refs, 1, caller) : NULL;
 }
 
 /* The length of a peeled reference line in packed-refs, including EOL: */
@@ -1267,15 +1142,16 @@ static void read_packed_refs(FILE *f, struct ref_dir *dir)
 }
 
 /*
- * Get the packed_ref_cache for the specified ref_cache, creating it
- * if necessary.
+ * Get the packed_ref_cache for the specified files_ref_store,
+ * creating it if necessary.
  */
-static struct packed_ref_cache *get_packed_ref_cache(struct ref_cache *refs)
+static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *refs)
 {
        char *packed_refs_file;
 
-       if (*refs->name)
-               packed_refs_file = git_pathdup_submodule(refs->name, "packed-refs");
+       if (*refs->base.submodule)
+               packed_refs_file = git_pathdup_submodule(refs->base.submodule,
+                                                        "packed-refs");
        else
                packed_refs_file = git_pathdup("packed-refs");
 
@@ -1305,7 +1181,7 @@ static struct ref_dir *get_packed_ref_dir(struct packed_ref_cache *packed_ref_ca
        return get_ref_dir(packed_ref_cache->root);
 }
 
-static struct ref_dir *get_packed_refs(struct ref_cache *refs)
+static struct ref_dir *get_packed_refs(struct files_ref_store *refs)
 {
        return get_packed_ref_dir(get_packed_ref_cache(refs));
 }
@@ -1316,10 +1192,10 @@ static struct ref_dir *get_packed_refs(struct ref_cache *refs)
  * lock_packed_refs()).  To actually write the packed-refs file, call
  * commit_packed_refs().
  */
-static void add_packed_ref(const char *refname, const unsigned char *sha1)
+static void add_packed_ref(struct files_ref_store *refs,
+                          const char *refname, const unsigned char *sha1)
 {
-       struct packed_ref_cache *packed_ref_cache =
-               get_packed_ref_cache(&ref_cache);
+       struct packed_ref_cache *packed_ref_cache = get_packed_ref_cache(refs);
 
        if (!packed_ref_cache->lock)
                die("internal error: packed refs not locked");
@@ -1334,7 +1210,7 @@ static void add_packed_ref(const char *refname, const unsigned char *sha1)
  */
 static void read_loose_refs(const char *dirname, struct ref_dir *dir)
 {
-       struct ref_cache *refs = dir->ref_cache;
+       struct files_ref_store *refs = dir->ref_store;
        DIR *d;
        struct dirent *de;
        int dirnamelen = strlen(dirname);
@@ -1342,8 +1218,8 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
        struct strbuf path = STRBUF_INIT;
        size_t path_baselen;
 
-       if (*refs->name)
-               strbuf_git_path_submodule(&path, refs->name, "%s", dirname);
+       if (*refs->base.submodule)
+               strbuf_git_path_submodule(&path, refs->base.submodule, "%s", dirname);
        else
                strbuf_git_path(&path, "%s", dirname);
        path_baselen = path.len;
@@ -1378,10 +1254,10 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
                } else {
                        int read_ok;
 
-                       if (*refs->name) {
+                       if (*refs->base.submodule) {
                                hashclr(sha1);
                                flag = 0;
-                               read_ok = !resolve_gitlink_ref(refs->name,
+                               read_ok = !resolve_gitlink_ref(refs->base.submodule,
                                                               refname.buf, sha1);
                        } else {
                                read_ok = !read_ref_full(refname.buf,
@@ -1422,7 +1298,7 @@ static void read_loose_refs(const char *dirname, struct ref_dir *dir)
        closedir(d);
 }
 
-static struct ref_dir *get_loose_refs(struct ref_cache *refs)
+static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
 {
        if (!refs->loose) {
                /*
@@ -1444,10 +1320,10 @@ static struct ref_dir *get_loose_refs(struct ref_cache *refs)
 
 /*
  * Called by resolve_gitlink_ref_recursive() after it failed to read
- * from the loose refs in ref_cache refs. Find <refname> in the
- * packed-refs file for the submodule.
+ * from the loose refs in refs. Find <refname> in the packed-refs file
+ * for the submodule.
  */
-static int resolve_gitlink_packed_ref(struct ref_cache *refs,
+static int resolve_gitlink_packed_ref(struct files_ref_store *refs,
                                      const char *refname, unsigned char *sha1)
 {
        struct ref_entry *ref;
@@ -1461,7 +1337,7 @@ static int resolve_gitlink_packed_ref(struct ref_cache *refs,
        return 0;
 }
 
-static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
+static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
                                         const char *refname, unsigned char *sha1,
                                         int recursion)
 {
@@ -1471,8 +1347,8 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
 
        if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
                return -1;
-       path = *refs->name
-               ? git_pathdup_submodule(refs->name, "%s", refname)
+       path = *refs->base.submodule
+               ? git_pathdup_submodule(refs->base.submodule, "%s", refname)
                : git_pathdup("%s", refname);
        fd = open(path, O_RDONLY);
        free(path);
@@ -1503,9 +1379,9 @@ static int resolve_gitlink_ref_recursive(struct ref_cache *refs,
 
 int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
 {
-       int len = strlen(path), retval;
+       int len = strlen(path);
        struct strbuf submodule = STRBUF_INIT;
-       struct ref_cache *refs;
+       struct files_ref_store *refs;
 
        while (len && path[len-1] == '/')
                len--;
@@ -1513,30 +1389,31 @@ int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sh
                return -1;
 
        strbuf_add(&submodule, path, len);
-       refs = get_ref_cache(submodule.buf);
+       refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
        if (!refs) {
                strbuf_release(&submodule);
                return -1;
        }
        strbuf_release(&submodule);
 
-       retval = resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
-       return retval;
+       return resolve_gitlink_ref_recursive(refs, refname, sha1, 0);
 }
 
 /*
  * Return the ref_entry for the given refname from the packed
  * references.  If it does not exist, return NULL.
  */
-static struct ref_entry *get_packed_ref(const char *refname)
+static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
+                                       const char *refname)
 {
-       return find_ref(get_packed_refs(&ref_cache), refname);
+       return find_ref(get_packed_refs(refs), refname);
 }
 
 /*
  * A loose ref file doesn't exist; check for a packed ref.
  */
-static int resolve_missing_loose_ref(const char *refname,
+static int resolve_missing_loose_ref(struct files_ref_store *refs,
+                                    const char *refname,
                                     unsigned char *sha1,
                                     unsigned int *flags)
 {
@@ -1546,7 +1423,7 @@ static int resolve_missing_loose_ref(const char *refname,
         * The loose reference file does not exist; check for a packed
         * reference.
         */
-       entry = get_packed_ref(refname);
+       entry = get_packed_ref(refs, refname);
        if (entry) {
                hashcpy(sha1, entry->u.value.oid.hash);
                *flags |= REF_ISPACKED;
@@ -1559,6 +1436,8 @@ static int resolve_missing_loose_ref(const char *refname,
 int read_raw_ref(const char *refname, unsigned char *sha1,
                 struct strbuf *referent, unsigned int *type)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "read_raw_ref");
        struct strbuf sb_contents = STRBUF_INIT;
        struct strbuf sb_path = STRBUF_INIT;
        const char *path;
@@ -1587,7 +1466,7 @@ int read_raw_ref(const char *refname, unsigned char *sha1,
        if (lstat(path, &st) < 0) {
                if (errno != ENOENT)
                        goto out;
-               if (resolve_missing_loose_ref(refname, sha1, type)) {
+               if (resolve_missing_loose_ref(refs, refname, sha1, type)) {
                        errno = ENOENT;
                        goto out;
                }
@@ -1621,7 +1500,7 @@ int read_raw_ref(const char *refname, unsigned char *sha1,
                 * ref is supposed to be, there could still be a
                 * packed ref:
                 */
-               if (resolve_missing_loose_ref(refname, sha1, type)) {
+               if (resolve_missing_loose_ref(refs, refname, sha1, type)) {
                        errno = EISDIR;
                        goto out;
                }
@@ -1730,6 +1609,8 @@ static int lock_raw_ref(const char *refname, int mustexist,
                        unsigned int *type,
                        struct strbuf *err)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "lock_raw_ref");
        struct ref_lock *lock;
        struct strbuf ref_file = STRBUF_INIT;
        int attempts_remaining = 3;
@@ -1862,7 +1743,7 @@ static int lock_raw_ref(const char *refname, int mustexist,
                                                          REMOVE_DIR_EMPTY_ONLY)) {
                                if (verify_refname_available_dir(
                                                    refname, extras, skip,
-                                                   get_loose_refs(&ref_cache),
+                                                   get_loose_refs(refs),
                                                    err)) {
                                        /*
                                         * The error message set by
@@ -1901,7 +1782,7 @@ static int lock_raw_ref(const char *refname, int mustexist,
                 */
                if (verify_refname_available_dir(
                                    refname, extras, skip,
-                                   get_packed_refs(&ref_cache),
+                                   get_packed_refs(refs),
                                    err)) {
                        goto error_return;
                }
@@ -1956,14 +1837,16 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
 
 int peel_ref(const char *refname, unsigned char *sha1)
 {
+       struct files_ref_store *refs = get_files_ref_store(NULL, "peel_ref");
        int flag;
        unsigned char base[20];
 
-       if (current_ref && (current_ref->name == refname
-                           || !strcmp(current_ref->name, refname))) {
-               if (peel_entry(current_ref, 0))
+       if (current_ref_iter && current_ref_iter->refname == refname) {
+               struct object_id peeled;
+
+               if (ref_iterator_peel(current_ref_iter, &peeled))
                        return -1;
-               hashcpy(sha1, current_ref->u.value.peeled.hash);
+               hashcpy(sha1, peeled.hash);
                return 0;
        }
 
@@ -1979,7 +1862,7 @@ int peel_ref(const char *refname, unsigned char *sha1)
         * have REF_KNOWS_PEELED.
         */
        if (flag & REF_ISPACKED) {
-               struct ref_entry *r = get_packed_ref(refname);
+               struct ref_entry *r = get_packed_ref(refs, refname);
                if (r) {
                        if (peel_entry(r, 0))
                                return -1;
@@ -2058,7 +1941,8 @@ struct ref_iterator *files_ref_iterator_begin(
                const char *submodule,
                const char *prefix, unsigned int flags)
 {
-       struct ref_cache *refs = get_ref_cache(submodule);
+       struct files_ref_store *refs =
+               get_files_ref_store(submodule, "ref_iterator_begin");
        struct ref_dir *loose_dir, *packed_dir;
        struct ref_iterator *loose_iter, *packed_iter;
        struct files_ref_iterator *iter;
@@ -2124,86 +2008,6 @@ struct ref_iterator *files_ref_iterator_begin(
        return ref_iterator;
 }
 
-/*
- * Call fn for each reference in the specified ref_cache, omitting
- * references not in the containing_dir of prefix. Call fn for all
- * references, including broken ones. If fn ever returns a non-zero
- * value, stop the iteration and return that value; otherwise, return
- * 0.
- */
-static int do_for_each_entry(struct ref_cache *refs, const char *prefix,
-                            each_ref_entry_fn fn, void *cb_data)
-{
-       struct packed_ref_cache *packed_ref_cache;
-       struct ref_dir *loose_dir;
-       struct ref_dir *packed_dir;
-       int retval = 0;
-
-       /*
-        * We must make sure that all loose refs are read before accessing the
-        * packed-refs file; this avoids a race condition in which loose refs
-        * are migrated to the packed-refs file by a simultaneous process, but
-        * our in-memory view is from before the migration. get_packed_ref_cache()
-        * takes care of making sure our view is up to date with what is on
-        * disk.
-        */
-       loose_dir = get_loose_refs(refs);
-       if (prefix && *prefix) {
-               loose_dir = find_containing_dir(loose_dir, prefix, 0);
-       }
-       if (loose_dir)
-               prime_ref_dir(loose_dir);
-
-       packed_ref_cache = get_packed_ref_cache(refs);
-       acquire_packed_ref_cache(packed_ref_cache);
-       packed_dir = get_packed_ref_dir(packed_ref_cache);
-       if (prefix && *prefix) {
-               packed_dir = find_containing_dir(packed_dir, prefix, 0);
-       }
-
-       if (packed_dir && loose_dir) {
-               sort_ref_dir(packed_dir);
-               sort_ref_dir(loose_dir);
-               retval = do_for_each_entry_in_dirs(
-                               packed_dir, loose_dir, fn, cb_data);
-       } else if (packed_dir) {
-               sort_ref_dir(packed_dir);
-               retval = do_for_each_entry_in_dir(
-                               packed_dir, 0, fn, cb_data);
-       } else if (loose_dir) {
-               sort_ref_dir(loose_dir);
-               retval = do_for_each_entry_in_dir(
-                               loose_dir, 0, fn, cb_data);
-       }
-
-       release_packed_ref_cache(packed_ref_cache);
-       return retval;
-}
-
-int do_for_each_ref(const char *submodule, const char *prefix,
-                   each_ref_fn fn, int trim, int flags, void *cb_data)
-{
-       struct ref_entry_cb data;
-       struct ref_cache *refs;
-
-       refs = get_ref_cache(submodule);
-       if (!refs)
-               return 0;
-
-       data.prefix = prefix;
-       data.trim = trim;
-       data.flags = flags;
-       data.fn = fn;
-       data.cb_data = cb_data;
-
-       if (ref_paranoia < 0)
-               ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
-       if (ref_paranoia)
-               data.flags |= DO_FOR_EACH_INCLUDE_BROKEN;
-
-       return do_for_each_entry(refs, prefix, do_one_ref, &data);
-}
-
 /*
  * Verify that the reference locked by lock has the value old_sha1.
  * Fail if the reference doesn't exist and mustexist is set. Return 0
@@ -2261,6 +2065,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                                            unsigned int flags, int *type,
                                            struct strbuf *err)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "lock_ref_sha1_basic");
        struct strbuf ref_file = STRBUF_INIT;
        struct ref_lock *lock;
        int last_errno = 0;
@@ -2291,8 +2097,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
                 */
                if (remove_empty_directories(&ref_file)) {
                        last_errno = errno;
-                       if (!verify_refname_available_dir(refname, extras, skip,
-                                                         get_loose_refs(&ref_cache), err))
+                       if (!verify_refname_available_dir(
+                                           refname, extras, skip,
+                                           get_loose_refs(refs), err))
                                strbuf_addf(err, "there are still refs under '%s'",
                                            refname);
                        goto error_return;
@@ -2303,8 +2110,9 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
        if (!resolved) {
                last_errno = errno;
                if (last_errno != ENOTDIR ||
-                   !verify_refname_available_dir(refname, extras, skip,
-                                                 get_loose_refs(&ref_cache), err))
+                   !verify_refname_available_dir(
+                                   refname, extras, skip,
+                                   get_loose_refs(refs), err))
                        strbuf_addf(err, "unable to resolve reference '%s': %s",
                                    refname, strerror(last_errno));
 
@@ -2319,7 +2127,8 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
         */
        if (is_null_oid(&lock->old_oid) &&
            verify_refname_available_dir(refname, extras, skip,
-                                        get_packed_refs(&ref_cache), err)) {
+                                        get_packed_refs(refs),
+                                        err)) {
                last_errno = ENOTDIR;
                goto error_return;
        }
@@ -2408,9 +2217,10 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
  */
 static int lock_packed_refs(int flags)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "lock_packed_refs");
        static int timeout_configured = 0;
        static int timeout_value = 1000;
-
        struct packed_ref_cache *packed_ref_cache;
 
        if (!timeout_configured) {
@@ -2428,7 +2238,7 @@ static int lock_packed_refs(int flags)
         * this will automatically invalidate the cache and re-read
         * the packed-refs file.
         */
-       packed_ref_cache = get_packed_ref_cache(&ref_cache);
+       packed_ref_cache = get_packed_ref_cache(refs);
        packed_ref_cache->lock = &packlock;
        /* Increment the reference count to prevent it from being freed: */
        acquire_packed_ref_cache(packed_ref_cache);
@@ -2443,8 +2253,10 @@ static int lock_packed_refs(int flags)
  */
 static int commit_packed_refs(void)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "commit_packed_refs");
        struct packed_ref_cache *packed_ref_cache =
-               get_packed_ref_cache(&ref_cache);
+               get_packed_ref_cache(refs);
        int error = 0;
        int save_errno = 0;
        FILE *out;
@@ -2477,15 +2289,17 @@ static int commit_packed_refs(void)
  */
 static void rollback_packed_refs(void)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "rollback_packed_refs");
        struct packed_ref_cache *packed_ref_cache =
-               get_packed_ref_cache(&ref_cache);
+               get_packed_ref_cache(refs);
 
        if (!packed_ref_cache->lock)
                die("internal error: packed-refs not locked");
        rollback_lock_file(packed_ref_cache->lock);
        packed_ref_cache->lock = NULL;
        release_packed_ref_cache(packed_ref_cache);
-       clear_packed_ref_cache(&ref_cache);
+       clear_packed_ref_cache(refs);
 }
 
 struct ref_to_prune {
@@ -2618,15 +2432,17 @@ static void prune_refs(struct ref_to_prune *r)
 
 int pack_refs(unsigned int flags)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "pack_refs");
        struct pack_refs_cb_data cbdata;
 
        memset(&cbdata, 0, sizeof(cbdata));
        cbdata.flags = flags;
 
        lock_packed_refs(LOCK_DIE_ON_ERROR);
-       cbdata.packed_refs = get_packed_refs(&ref_cache);
+       cbdata.packed_refs = get_packed_refs(refs);
 
-       do_for_each_entry_in_dir(get_loose_refs(&ref_cache), 0,
+       do_for_each_entry_in_dir(get_loose_refs(refs), 0,
                                 pack_if_possible_fn, &cbdata);
 
        if (commit_packed_refs())
@@ -2645,6 +2461,8 @@ int pack_refs(unsigned int flags)
  */
 static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "repack_without_refs");
        struct ref_dir *packed;
        struct string_list_item *refname;
        int ret, needs_repacking = 0, removed = 0;
@@ -2653,7 +2471,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
 
        /* Look for a packed ref */
        for_each_string_list_item(refname, refnames) {
-               if (get_packed_ref(refname->string)) {
+               if (get_packed_ref(refs, refname->string)) {
                        needs_repacking = 1;
                        break;
                }
@@ -2667,7 +2485,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
                unable_to_lock_message(git_path("packed-refs"), errno, err);
                return -1;
        }
-       packed = get_packed_refs(&ref_cache);
+       packed = get_packed_refs(refs);
 
        /* Remove refnames from the cache */
        for_each_string_list_item(refname, refnames)
@@ -2812,8 +2630,10 @@ int verify_refname_available(const char *newname,
                             const struct string_list *skip,
                             struct strbuf *err)
 {
-       struct ref_dir *packed_refs = get_packed_refs(&ref_cache);
-       struct ref_dir *loose_refs = get_loose_refs(&ref_cache);
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "verify_refname_available");
+       struct ref_dir *packed_refs = get_packed_refs(refs);
+       struct ref_dir *loose_refs = get_loose_refs(refs);
 
        if (verify_refname_available_dir(newname, extras, skip,
                                         packed_refs, err) ||
@@ -3164,7 +2984,10 @@ static int commit_ref_update(struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
                             struct strbuf *err)
 {
-       clear_loose_ref_cache(&ref_cache);
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "commit_ref_update");
+
+       clear_loose_ref_cache(refs);
        if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
                char *old_msg = strbuf_detach(err, NULL);
                strbuf_addf(err, "cannot update the ref '%s': %s",
@@ -3487,60 +3310,88 @@ int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_dat
        strbuf_release(&sb);
        return ret;
 }
-/*
- * Call fn for each reflog in the namespace indicated by name.  name
- * must be empty or end with '/'.  Name will be used as a scratch
- * space, but its contents will be restored before return.
- */
-static int do_for_each_reflog(struct strbuf *name, each_ref_fn fn, void *cb_data)
-{
-       DIR *d = opendir(git_path("logs/%s", name->buf));
-       int retval = 0;
-       struct dirent *de;
-       int oldlen = name->len;
 
-       if (!d)
-               return name->len ? errno : 0;
+struct files_reflog_iterator {
+       struct ref_iterator base;
 
-       while ((de = readdir(d)) != NULL) {
-               struct stat st;
+       struct dir_iterator *dir_iterator;
+       struct object_id oid;
+};
 
-               if (de->d_name[0] == '.')
+static int files_reflog_iterator_advance(struct ref_iterator *ref_iterator)
+{
+       struct files_reflog_iterator *iter =
+               (struct files_reflog_iterator *)ref_iterator;
+       struct dir_iterator *diter = iter->dir_iterator;
+       int ok;
+
+       while ((ok = dir_iterator_advance(diter)) == ITER_OK) {
+               int flags;
+
+               if (!S_ISREG(diter->st.st_mode))
                        continue;
-               if (ends_with(de->d_name, ".lock"))
+               if (diter->basename[0] == '.')
+                       continue;
+               if (ends_with(diter->basename, ".lock"))
                        continue;
-               strbuf_addstr(name, de->d_name);
-               if (stat(git_path("logs/%s", name->buf), &st) < 0) {
-                       ; /* silently ignore */
-               } else {
-                       if (S_ISDIR(st.st_mode)) {
-                               strbuf_addch(name, '/');
-                               retval = do_for_each_reflog(name, fn, cb_data);
-                       } else {
-                               struct object_id oid;
 
-                               if (read_ref_full(name->buf, 0, oid.hash, NULL))
-                                       retval = error("bad ref for %s", name->buf);
-                               else
-                                       retval = fn(name->buf, &oid, 0, cb_data);
-                       }
-                       if (retval)
-                               break;
+               if (read_ref_full(diter->relative_path, 0,
+                                 iter->oid.hash, &flags)) {
+                       error("bad ref for %s", diter->path.buf);
+                       continue;
                }
-               strbuf_setlen(name, oldlen);
+
+               iter->base.refname = diter->relative_path;
+               iter->base.oid = &iter->oid;
+               iter->base.flags = flags;
+               return ITER_OK;
        }
-       closedir(d);
-       return retval;
+
+       iter->dir_iterator = NULL;
+       if (ref_iterator_abort(ref_iterator) == ITER_ERROR)
+               ok = ITER_ERROR;
+       return ok;
+}
+
+static int files_reflog_iterator_peel(struct ref_iterator *ref_iterator,
+                                  struct object_id *peeled)
+{
+       die("BUG: ref_iterator_peel() called for reflog_iterator");
+}
+
+static int files_reflog_iterator_abort(struct ref_iterator *ref_iterator)
+{
+       struct files_reflog_iterator *iter =
+               (struct files_reflog_iterator *)ref_iterator;
+       int ok = ITER_DONE;
+
+       if (iter->dir_iterator)
+               ok = dir_iterator_abort(iter->dir_iterator);
+
+       base_ref_iterator_free(ref_iterator);
+       return ok;
+}
+
+static struct ref_iterator_vtable files_reflog_iterator_vtable = {
+       files_reflog_iterator_advance,
+       files_reflog_iterator_peel,
+       files_reflog_iterator_abort
+};
+
+struct ref_iterator *files_reflog_iterator_begin(void)
+{
+       struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
+       struct ref_iterator *ref_iterator = &iter->base;
+
+       base_ref_iterator_init(ref_iterator, &files_reflog_iterator_vtable);
+       iter->dir_iterator = dir_iterator_begin(git_path("logs"));
+       return ref_iterator;
 }
 
 int for_each_reflog(each_ref_fn fn, void *cb_data)
 {
-       int retval;
-       struct strbuf name;
-       strbuf_init(&name, PATH_MAX);
-       retval = do_for_each_reflog(&name, fn, cb_data);
-       strbuf_release(&name);
-       return retval;
+       return do_for_each_ref_iterator(files_reflog_iterator_begin(),
+                                       fn, cb_data);
 }
 
 static int ref_update_reject_duplicates(struct string_list *refnames,
@@ -3852,6 +3703,8 @@ static int lock_ref_for_update(struct ref_update *update,
 int ref_transaction_commit(struct ref_transaction *transaction,
                           struct strbuf *err)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "ref_transaction_commit");
        int ret = 0, i;
        struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
        struct string_list_item *ref_to_delete;
@@ -3958,7 +3811,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
                        }
                }
                if (update->flags & REF_NEEDS_COMMIT) {
-                       clear_loose_ref_cache(&ref_cache);
+                       clear_loose_ref_cache(refs);
                        if (commit_ref(lock)) {
                                strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
                                unlock_ref(lock);
@@ -3991,7 +3844,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        }
        for_each_string_list_item(ref_to_delete, &refs_to_delete)
                unlink_or_warn(git_path("logs/%s", ref_to_delete->string));
-       clear_loose_ref_cache(&ref_cache);
+       clear_loose_ref_cache(refs);
 
 cleanup:
        transaction->state = REF_TRANSACTION_CLOSED;
@@ -4017,6 +3870,8 @@ static int ref_present(const char *refname,
 int initial_ref_transaction_commit(struct ref_transaction *transaction,
                                   struct strbuf *err)
 {
+       struct files_ref_store *refs =
+               get_files_ref_store(NULL, "initial_ref_transaction_commit");
        int ret = 0, i;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
 
@@ -4076,7 +3931,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
 
                if ((update->flags & REF_HAVE_NEW) &&
                    !is_null_sha1(update->new_sha1))
-                       add_packed_ref(update->refname, update->new_sha1);
+                       add_packed_ref(refs, update->refname, update->new_sha1);
        }
 
        if (commit_packed_refs()) {
@@ -4234,3 +4089,9 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
        unlock_ref(lock);
        return -1;
 }
+
+struct ref_storage_be refs_be_files = {
+       NULL,
+       "files",
+       files_ref_store_create
+};