refs: implement iteration over only per-worktree refs
[gitweb.git] / refs / files-backend.c
index 16d5b9f14baec66937218ab2c4fc69c8af1c245a..47710fcf286283871c0949e888e4b319a7e83616 100644 (file)
@@ -997,22 +997,6 @@ static struct files_ref_store *files_downcast(
        return (struct files_ref_store *)ref_store;
 }
 
-/*
- * 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 files_ref_store *get_files_ref_store(const char *submodule,
-                                                  const char *caller)
-{
-       struct ref_store *refs = get_ref_store(submodule);
-
-       return refs ? files_downcast(refs, 1, caller) : NULL;
-}
-
 /* The length of a peeled reference line in packed-refs, including EOL: */
 #define PEELED_LINE_LENGTH 42
 
@@ -1316,89 +1300,6 @@ static struct ref_dir *get_loose_refs(struct files_ref_store *refs)
        return get_ref_dir(refs->loose);
 }
 
-#define MAXREFLEN (1024)
-
-/*
- * Called by resolve_gitlink_ref_recursive() after it failed to read
- * from the loose refs in refs. Find <refname> in the packed-refs file
- * for the submodule.
- */
-static int resolve_gitlink_packed_ref(struct files_ref_store *refs,
-                                     const char *refname, unsigned char *sha1)
-{
-       struct ref_entry *ref;
-       struct ref_dir *dir = get_packed_refs(refs);
-
-       ref = find_ref(dir, refname);
-       if (ref == NULL)
-               return -1;
-
-       hashcpy(sha1, ref->u.value.oid.hash);
-       return 0;
-}
-
-static int resolve_gitlink_ref_recursive(struct files_ref_store *refs,
-                                        const char *refname, unsigned char *sha1,
-                                        int recursion)
-{
-       int fd, len;
-       char buffer[128], *p;
-       char *path;
-
-       if (recursion > SYMREF_MAXDEPTH || strlen(refname) > MAXREFLEN)
-               return -1;
-       path = *refs->base.submodule
-               ? git_pathdup_submodule(refs->base.submodule, "%s", refname)
-               : git_pathdup("%s", refname);
-       fd = open(path, O_RDONLY);
-       free(path);
-       if (fd < 0)
-               return resolve_gitlink_packed_ref(refs, refname, sha1);
-
-       len = read(fd, buffer, sizeof(buffer)-1);
-       close(fd);
-       if (len < 0)
-               return -1;
-       while (len && isspace(buffer[len-1]))
-               len--;
-       buffer[len] = 0;
-
-       /* Was it a detached head or an old-fashioned symlink? */
-       if (!get_sha1_hex(buffer, sha1))
-               return 0;
-
-       /* Symref? */
-       if (strncmp(buffer, "ref:", 4))
-               return -1;
-       p = buffer + 4;
-       while (isspace(*p))
-               p++;
-
-       return resolve_gitlink_ref_recursive(refs, p, sha1, recursion+1);
-}
-
-int resolve_gitlink_ref(const char *path, const char *refname, unsigned char *sha1)
-{
-       int len = strlen(path);
-       struct strbuf submodule = STRBUF_INIT;
-       struct files_ref_store *refs;
-
-       while (len && path[len-1] == '/')
-               len--;
-       if (!len)
-               return -1;
-
-       strbuf_add(&submodule, path, len);
-       refs = get_files_ref_store(submodule.buf, "resolve_gitlink_ref");
-       if (!refs) {
-               strbuf_release(&submodule);
-               return -1;
-       }
-       strbuf_release(&submodule);
-
-       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.
@@ -1412,13 +1313,10 @@ static struct ref_entry *get_packed_ref(struct files_ref_store *refs,
 /*
  * A loose ref file doesn't exist; check for a packed ref.
  */
-static int resolve_missing_loose_ref(const char *refname,
-                                    unsigned char *sha1,
-                                    unsigned int *flags)
+static int resolve_packed_ref(struct files_ref_store *refs,
+                             const char *refname,
+                             unsigned char *sha1, unsigned int *flags)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "resolve_missing_loose_ref");
-
        struct ref_entry *entry;
 
        /*
@@ -1435,9 +1333,12 @@ static int resolve_missing_loose_ref(const char *refname,
        return -1;
 }
 
-int read_raw_ref(const char *refname, unsigned char *sha1,
-                struct strbuf *referent, unsigned int *type)
+static int files_read_raw_ref(struct ref_store *ref_store,
+                             const char *refname, unsigned char *sha1,
+                             struct strbuf *referent, unsigned int *type)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 1, "read_raw_ref");
        struct strbuf sb_contents = STRBUF_INIT;
        struct strbuf sb_path = STRBUF_INIT;
        const char *path;
@@ -1449,7 +1350,12 @@ int read_raw_ref(const char *refname, unsigned char *sha1,
 
        *type = 0;
        strbuf_reset(&sb_path);
-       strbuf_git_path(&sb_path, "%s", refname);
+
+       if (*refs->base.submodule)
+               strbuf_git_path_submodule(&sb_path, refs->base.submodule, "%s", refname);
+       else
+               strbuf_git_path(&sb_path, "%s", refname);
+
        path = sb_path.buf;
 
 stat_ref:
@@ -1466,7 +1372,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_packed_ref(refs, refname, sha1, type)) {
                        errno = ENOENT;
                        goto out;
                }
@@ -1500,7 +1406,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_packed_ref(refs, refname, sha1, type)) {
                        errno = EISDIR;
                        goto out;
                }
@@ -1601,7 +1507,8 @@ static void unlock_ref(struct ref_lock *lock)
  *   avoided, namely if we were successfully able to read the ref
  * - Generate informative error messages in the case of failure
  */
-static int lock_raw_ref(const char *refname, int mustexist,
+static int lock_raw_ref(struct files_ref_store *refs,
+                       const char *refname, int mustexist,
                        const struct string_list *extras,
                        const struct string_list *skip,
                        struct ref_lock **lock_p,
@@ -1609,14 +1516,14 @@ 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;
        int ret = TRANSACTION_GENERIC_ERROR;
 
        assert(err);
+       assert_main_repository(&refs->base, "lock_raw_ref");
+
        *type = 0;
 
        /* First lock the file so it can't change out from under us. */
@@ -1700,7 +1607,8 @@ static int lock_raw_ref(const char *refname, int mustexist,
         * fear that its value will change.
         */
 
-       if (read_raw_ref(refname, lock->old_oid.hash, referent, type)) {
+       if (files_read_raw_ref(&refs->base, refname,
+                              lock->old_oid.hash, referent, type)) {
                if (errno == ENOENT) {
                        if (mustexist) {
                                /* Garden variety missing reference. */
@@ -1835,9 +1743,10 @@ static enum peel_status peel_entry(struct ref_entry *entry, int repeel)
        return status;
 }
 
-int peel_ref(const char *refname, unsigned char *sha1)
+static int files_peel_ref(struct ref_store *ref_store,
+                         const char *refname, unsigned char *sha1)
 {
-       struct files_ref_store *refs = get_files_ref_store(NULL, "peel_ref");
+       struct files_ref_store *refs = files_downcast(ref_store, 0, "peel_ref");
        int flag;
        unsigned char base[20];
 
@@ -1889,6 +1798,10 @@ static int files_ref_iterator_advance(struct ref_iterator *ref_iterator)
        int ok;
 
        while ((ok = ref_iterator_advance(iter->iter0)) == ITER_OK) {
+               if (iter->flags & DO_FOR_EACH_PER_WORKTREE_ONLY &&
+                   ref_type(iter->iter0->refname) != REF_TYPE_PER_WORKTREE)
+                       continue;
+
                if (!(iter->flags & DO_FOR_EACH_INCLUDE_BROKEN) &&
                    !ref_resolves_to_object(iter->iter0->refname,
                                            iter->iter0->oid,
@@ -1937,12 +1850,12 @@ static struct ref_iterator_vtable files_ref_iterator_vtable = {
        files_ref_iterator_abort
 };
 
-struct ref_iterator *files_ref_iterator_begin(
-               const char *submodule,
+static struct ref_iterator *files_ref_iterator_begin(
+               struct ref_store *ref_store,
                const char *prefix, unsigned int flags)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(submodule, "ref_iterator_begin");
+               files_downcast(ref_store, 1, "ref_iterator_begin");
        struct ref_dir *loose_dir, *packed_dir;
        struct ref_iterator *loose_iter, *packed_iter;
        struct files_ref_iterator *iter;
@@ -2058,15 +1971,14 @@ static int remove_empty_directories(struct strbuf *path)
  * Locks a ref returning the lock on success and NULL on failure.
  * On failure errno is set to something meaningful.
  */
-static struct ref_lock *lock_ref_sha1_basic(const char *refname,
+static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
+                                           const char *refname,
                                            const unsigned char *old_sha1,
                                            const struct string_list *extras,
                                            const struct string_list *skip,
                                            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;
@@ -2076,6 +1988,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *refname,
        int attempts_remaining = 3;
        int resolved;
 
+       assert_main_repository(&refs->base, "lock_ref_sha1_basic");
        assert(err);
 
        lock = xcalloc(1, sizeof(struct ref_lock));
@@ -2215,14 +2128,14 @@ static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
  * hold_lock_file_for_update(). Return 0 on success. On errors, set
  * errno appropriately and return a nonzero value.
  */
-static int lock_packed_refs(int flags)
+static int lock_packed_refs(struct files_ref_store *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;
 
+       assert_main_repository(&refs->base, "lock_packed_refs");
+
        if (!timeout_configured) {
                git_config_get_int("core.packedrefstimeout", &timeout_value);
                timeout_configured = 1;
@@ -2251,16 +2164,16 @@ static int lock_packed_refs(int flags)
  * lock_packed_refs()). Return zero on success. On errors, set errno
  * and return a nonzero value
  */
-static int commit_packed_refs(void)
+static int commit_packed_refs(struct files_ref_store *refs)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "commit_packed_refs");
        struct packed_ref_cache *packed_ref_cache =
                get_packed_ref_cache(refs);
        int error = 0;
        int save_errno = 0;
        FILE *out;
 
+       assert_main_repository(&refs->base, "commit_packed_refs");
+
        if (!packed_ref_cache->lock)
                die("internal error: packed-refs not locked");
 
@@ -2287,13 +2200,13 @@ static int commit_packed_refs(void)
  * in-memory packed reference cache.  (The packed-refs file will be
  * read anew if it is needed again after this function is called.)
  */
-static void rollback_packed_refs(void)
+static void rollback_packed_refs(struct files_ref_store *refs)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "rollback_packed_refs");
        struct packed_ref_cache *packed_ref_cache =
                get_packed_ref_cache(refs);
 
+       assert_main_repository(&refs->base, "rollback_packed_refs");
+
        if (!packed_ref_cache->lock)
                die("internal error: packed-refs not locked");
        rollback_lock_file(packed_ref_cache->lock);
@@ -2430,22 +2343,22 @@ static void prune_refs(struct ref_to_prune *r)
        }
 }
 
-int pack_refs(unsigned int flags)
+static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(NULL, "pack_refs");
+               files_downcast(ref_store, 0, "pack_refs");
        struct pack_refs_cb_data cbdata;
 
        memset(&cbdata, 0, sizeof(cbdata));
        cbdata.flags = flags;
 
-       lock_packed_refs(LOCK_DIE_ON_ERROR);
+       lock_packed_refs(refs, LOCK_DIE_ON_ERROR);
        cbdata.packed_refs = get_packed_refs(refs);
 
        do_for_each_entry_in_dir(get_loose_refs(refs), 0,
                                 pack_if_possible_fn, &cbdata);
 
-       if (commit_packed_refs())
+       if (commit_packed_refs(refs))
                die_errno("unable to overwrite old ref-pack file");
 
        prune_refs(cbdata.ref_to_prune);
@@ -2459,14 +2372,14 @@ int pack_refs(unsigned int flags)
  *
  * The refs in 'refnames' needn't be sorted. `err` must not be NULL.
  */
-static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
+static int repack_without_refs(struct files_ref_store *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;
 
+       assert_main_repository(&refs->base, "repack_without_refs");
        assert(err);
 
        /* Look for a packed ref */
@@ -2481,7 +2394,7 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
        if (!needs_repacking)
                return 0; /* no refname exists in packed refs */
 
-       if (lock_packed_refs(0)) {
+       if (lock_packed_refs(refs, 0)) {
                unable_to_lock_message(git_path("packed-refs"), errno, err);
                return -1;
        }
@@ -2496,12 +2409,12 @@ static int repack_without_refs(struct string_list *refnames, struct strbuf *err)
                 * All packed entries disappeared while we were
                 * acquiring the lock.
                 */
-               rollback_packed_refs();
+               rollback_packed_refs(refs);
                return 0;
        }
 
        /* Write what remains */
-       ret = commit_packed_refs();
+       ret = commit_packed_refs(refs);
        if (ret)
                strbuf_addf(err, "unable to overwrite old ref-pack file: %s",
                            strerror(errno));
@@ -2526,15 +2439,18 @@ static int delete_ref_loose(struct ref_lock *lock, int flag, struct strbuf *err)
        return 0;
 }
 
-int delete_refs(struct string_list *refnames, unsigned int flags)
+static int files_delete_refs(struct ref_store *ref_store,
+                            struct string_list *refnames, unsigned int flags)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "delete_refs");
        struct strbuf err = STRBUF_INIT;
        int i, result = 0;
 
        if (!refnames->nr)
                return 0;
 
-       result = repack_without_refs(refnames, &err);
+       result = repack_without_refs(refs, refnames, &err);
        if (result) {
                /*
                 * If we failed to rewrite the packed-refs file, then
@@ -2625,13 +2541,14 @@ static int rename_tmp_log(const char *newrefname)
        return ret;
 }
 
-int verify_refname_available(const char *newname,
-                            const struct string_list *extras,
-                            const struct string_list *skip,
-                            struct strbuf *err)
+static int files_verify_refname_available(struct ref_store *ref_store,
+                                         const char *newname,
+                                         const struct string_list *extras,
+                                         const struct string_list *skip,
+                                         struct strbuf *err)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(NULL, "verify_refname_available");
+               files_downcast(ref_store, 1, "verify_refname_available");
        struct ref_dir *packed_refs = get_packed_refs(refs);
        struct ref_dir *loose_refs = get_loose_refs(refs);
 
@@ -2646,12 +2563,17 @@ int verify_refname_available(const char *newname,
 
 static int write_ref_to_lockfile(struct ref_lock *lock,
                                 const unsigned char *sha1, struct strbuf *err);
-static int commit_ref_update(struct ref_lock *lock,
+static int commit_ref_update(struct files_ref_store *refs,
+                            struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
                             struct strbuf *err);
 
-int rename_ref(const char *oldrefname, const char *newrefname, const char *logmsg)
+static int files_rename_ref(struct ref_store *ref_store,
+                           const char *oldrefname, const char *newrefname,
+                           const char *logmsg)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "rename_ref");
        unsigned char sha1[20], orig_sha1[20];
        int flag = 0, logmoved = 0;
        struct ref_lock *lock;
@@ -2714,8 +2636,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
 
        logmoved = log;
 
-       lock = lock_ref_sha1_basic(newrefname, NULL, NULL, NULL, REF_NODEREF,
-                                  NULL, &err);
+       lock = lock_ref_sha1_basic(refs, newrefname, NULL, NULL, NULL,
+                                  REF_NODEREF, NULL, &err);
        if (!lock) {
                error("unable to rename '%s' to '%s': %s", oldrefname, newrefname, err.buf);
                strbuf_release(&err);
@@ -2724,7 +2646,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        hashcpy(lock->old_oid.hash, orig_sha1);
 
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, logmsg, &err)) {
+           commit_ref_update(refs, lock, orig_sha1, logmsg, &err)) {
                error("unable to write current sha1 into %s: %s", newrefname, err.buf);
                strbuf_release(&err);
                goto rollback;
@@ -2733,8 +2655,8 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        return 0;
 
  rollback:
-       lock = lock_ref_sha1_basic(oldrefname, NULL, NULL, NULL, REF_NODEREF,
-                                  NULL, &err);
+       lock = lock_ref_sha1_basic(refs, oldrefname, NULL, NULL, NULL,
+                                  REF_NODEREF, NULL, &err);
        if (!lock) {
                error("unable to lock %s for rollback: %s", oldrefname, err.buf);
                strbuf_release(&err);
@@ -2744,7 +2666,7 @@ int rename_ref(const char *oldrefname, const char *newrefname, const char *logms
        flag = log_all_ref_updates;
        log_all_ref_updates = 0;
        if (write_ref_to_lockfile(lock, orig_sha1, &err) ||
-           commit_ref_update(lock, orig_sha1, NULL, &err)) {
+           commit_ref_update(refs, lock, orig_sha1, NULL, &err)) {
                error("unable to write current sha1 into %s: %s", oldrefname, err.buf);
                strbuf_release(&err);
        }
@@ -2847,11 +2769,16 @@ static int log_ref_setup(const char *refname, struct strbuf *logfile, struct str
 }
 
 
-int safe_create_reflog(const char *refname, int force_create, struct strbuf *err)
+static int files_create_reflog(struct ref_store *ref_store,
+                              const char *refname, int force_create,
+                              struct strbuf *err)
 {
        int ret;
        struct strbuf sb = STRBUF_INIT;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "create_reflog");
+
        ret = log_ref_setup(refname, &sb, err, force_create);
        strbuf_release(&sb);
        return ret;
@@ -2980,12 +2907,12 @@ static int write_ref_to_lockfile(struct ref_lock *lock,
  * to the loose reference lockfile. Also update the reflogs if
  * necessary, using the specified lockmsg (which can be NULL).
  */
-static int commit_ref_update(struct ref_lock *lock,
+static int commit_ref_update(struct files_ref_store *refs,
+                            struct ref_lock *lock,
                             const unsigned char *sha1, const char *logmsg,
                             struct strbuf *err)
 {
-       struct files_ref_store *refs =
-               get_files_ref_store(NULL, "commit_ref_update");
+       assert_main_repository(&refs->base, "commit_ref_update");
 
        clear_loose_ref_cache(refs);
        if (log_ref_write(lock->ref_name, lock->old_oid.hash, sha1, logmsg, 0, err)) {
@@ -3086,13 +3013,18 @@ static int create_symref_locked(struct ref_lock *lock, const char *refname,
        return 0;
 }
 
-int create_symref(const char *refname, const char *target, const char *logmsg)
+static int files_create_symref(struct ref_store *ref_store,
+                              const char *refname, const char *target,
+                              const char *logmsg)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "create_symref");
        struct strbuf err = STRBUF_INIT;
        struct ref_lock *lock;
        int ret;
 
-       lock = lock_ref_sha1_basic(refname, NULL, NULL, NULL, REF_NODEREF, NULL,
+       lock = lock_ref_sha1_basic(refs, refname, NULL,
+                                  NULL, NULL, REF_NODEREF, NULL,
                                   &err);
        if (!lock) {
                error("%s", err.buf);
@@ -3140,16 +3072,24 @@ int set_worktree_head_symref(const char *gitdir, const char *target)
        return ret;
 }
 
-int reflog_exists(const char *refname)
+static int files_reflog_exists(struct ref_store *ref_store,
+                              const char *refname)
 {
        struct stat st;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "reflog_exists");
+
        return !lstat(git_path("logs/%s", refname), &st) &&
                S_ISREG(st.st_mode);
 }
 
-int delete_reflog(const char *refname)
+static int files_delete_reflog(struct ref_store *ref_store,
+                              const char *refname)
 {
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "delete_reflog");
+
        return remove_path(git_path("logs/%s", refname));
 }
 
@@ -3192,13 +3132,19 @@ static char *find_beginning_of_line(char *bob, char *scan)
        return scan;
 }
 
-int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent_reverse(struct ref_store *ref_store,
+                                            const char *refname,
+                                            each_reflog_ent_fn fn,
+                                            void *cb_data)
 {
        struct strbuf sb = STRBUF_INIT;
        FILE *logfp;
        long pos;
        int ret = 0, at_tail = 1;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "for_each_reflog_ent_reverse");
+
        logfp = fopen(git_path("logs/%s", refname), "r");
        if (!logfp)
                return -1;
@@ -3294,12 +3240,17 @@ int for_each_reflog_ent_reverse(const char *refname, each_reflog_ent_fn fn, void
        return ret;
 }
 
-int for_each_reflog_ent(const char *refname, each_reflog_ent_fn fn, void *cb_data)
+static int files_for_each_reflog_ent(struct ref_store *ref_store,
+                                    const char *refname,
+                                    each_reflog_ent_fn fn, void *cb_data)
 {
        FILE *logfp;
        struct strbuf sb = STRBUF_INIT;
        int ret = 0;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "for_each_reflog_ent");
+
        logfp = fopen(git_path("logs/%s", refname), "r");
        if (!logfp)
                return -1;
@@ -3378,22 +3329,19 @@ static struct ref_iterator_vtable files_reflog_iterator_vtable = {
        files_reflog_iterator_abort
 };
 
-struct ref_iterator *files_reflog_iterator_begin(void)
+static struct ref_iterator *files_reflog_iterator_begin(struct ref_store *ref_store)
 {
        struct files_reflog_iterator *iter = xcalloc(1, sizeof(*iter));
        struct ref_iterator *ref_iterator = &iter->base;
 
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "reflog_iterator_begin");
+
        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)
-{
-       return do_for_each_ref_iterator(files_reflog_iterator_begin(),
-                                       fn, cb_data);
-}
-
 static int ref_update_reject_duplicates(struct string_list *refnames,
                                        struct strbuf *err)
 {
@@ -3466,7 +3414,8 @@ static int split_head_update(struct ref_update *update,
  * Note that the new update will itself be subject to splitting when
  * the iteration gets to it.
  */
-static int split_symref_update(struct ref_update *update,
+static int split_symref_update(struct files_ref_store *refs,
+                              struct ref_update *update,
                               const char *referent,
                               struct ref_transaction *transaction,
                               struct string_list *affected_refnames,
@@ -3548,7 +3497,8 @@ static const char *original_update_refname(struct ref_update *update)
  * - If it is an update of head_ref, add a corresponding REF_LOG_ONLY
  *   update of HEAD.
  */
-static int lock_ref_for_update(struct ref_update *update,
+static int lock_ref_for_update(struct files_ref_store *refs,
+                              struct ref_update *update,
                               struct ref_transaction *transaction,
                               const char *head_ref,
                               struct string_list *affected_refnames,
@@ -3560,6 +3510,8 @@ static int lock_ref_for_update(struct ref_update *update,
        int ret;
        struct ref_lock *lock;
 
+       assert_main_repository(&refs->base, "lock_ref_for_update");
+
        if ((update->flags & REF_HAVE_NEW) && is_null_sha1(update->new_sha1))
                update->flags |= REF_DELETING;
 
@@ -3570,11 +3522,10 @@ static int lock_ref_for_update(struct ref_update *update,
                        return ret;
        }
 
-       ret = lock_raw_ref(update->refname, mustexist,
+       ret = lock_raw_ref(refs, update->refname, mustexist,
                           affected_refnames, NULL,
-                          &update->lock, &referent,
+                          &lock, &referent,
                           &update->type, err);
-
        if (ret) {
                char *reason;
 
@@ -3585,7 +3536,7 @@ static int lock_ref_for_update(struct ref_update *update,
                return ret;
        }
 
-       lock = update->lock;
+       update->backend_data = lock;
 
        if (update->type & REF_ISSYMREF) {
                if (update->flags & REF_NODEREF) {
@@ -3624,7 +3575,8 @@ static int lock_ref_for_update(struct ref_update *update,
                         * of processing the split-off update, so we
                         * don't have to do it here.
                         */
-                       ret = split_symref_update(update, referent.buf, transaction,
+                       ret = split_symref_update(refs, update,
+                                                 referent.buf, transaction,
                                                  affected_refnames, err);
                        if (ret)
                                return ret;
@@ -3640,7 +3592,8 @@ static int lock_ref_for_update(struct ref_update *update,
                for (parent_update = update->parent_update;
                     parent_update;
                     parent_update = parent_update->parent_update) {
-                       oidcpy(&parent_update->lock->old_oid, &lock->old_oid);
+                       struct ref_lock *parent_lock = parent_update->backend_data;
+                       oidcpy(&parent_lock->old_oid, &lock->old_oid);
                }
 
                if ((update->flags & REF_HAVE_OLD) &&
@@ -3675,7 +3628,7 @@ static int lock_ref_for_update(struct ref_update *update,
                         * The lock was freed upon failure of
                         * write_ref_to_lockfile():
                         */
-                       update->lock = NULL;
+                       update->backend_data = NULL;
                        strbuf_addf(err,
                                    "cannot update the ref '%s': %s",
                                    update->refname, write_err);
@@ -3700,11 +3653,12 @@ static int lock_ref_for_update(struct ref_update *update,
        return 0;
 }
 
-int ref_transaction_commit(struct ref_transaction *transaction,
-                          struct strbuf *err)
+static int files_transaction_commit(struct ref_store *ref_store,
+                                   struct ref_transaction *transaction,
+                                   struct strbuf *err)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(NULL, "ref_transaction_commit");
+               files_downcast(ref_store, 0, "ref_transaction_commit");
        int ret = 0, i;
        struct string_list refs_to_delete = STRING_LIST_INIT_NODUP;
        struct string_list_item *ref_to_delete;
@@ -3783,8 +3737,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
 
-               ret = lock_ref_for_update(update, transaction, head_ref,
-                                         &affected_refnames, err);
+               ret = lock_ref_for_update(refs, update, transaction,
+                                         head_ref, &affected_refnames, err);
                if (ret)
                        goto cleanup;
        }
@@ -3792,7 +3746,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        /* Perform updates first so live commits remain referenced */
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
-               struct ref_lock *lock = update->lock;
+               struct ref_lock *lock = update->backend_data;
 
                if (update->flags & REF_NEEDS_COMMIT ||
                    update->flags & REF_LOG_ONLY) {
@@ -3805,7 +3759,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
                                            lock->ref_name, old_msg);
                                free(old_msg);
                                unlock_ref(lock);
-                               update->lock = NULL;
+                               update->backend_data = NULL;
                                ret = TRANSACTION_GENERIC_ERROR;
                                goto cleanup;
                        }
@@ -3815,7 +3769,7 @@ int ref_transaction_commit(struct ref_transaction *transaction,
                        if (commit_ref(lock)) {
                                strbuf_addf(err, "couldn't set '%s'", lock->ref_name);
                                unlock_ref(lock);
-                               update->lock = NULL;
+                               update->backend_data = NULL;
                                ret = TRANSACTION_GENERIC_ERROR;
                                goto cleanup;
                        }
@@ -3824,21 +3778,22 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        /* Perform deletes now that updates are safely completed */
        for (i = 0; i < transaction->nr; i++) {
                struct ref_update *update = transaction->updates[i];
+               struct ref_lock *lock = update->backend_data;
 
                if (update->flags & REF_DELETING &&
                    !(update->flags & REF_LOG_ONLY)) {
-                       if (delete_ref_loose(update->lock, update->type, err)) {
+                       if (delete_ref_loose(lock, update->type, err)) {
                                ret = TRANSACTION_GENERIC_ERROR;
                                goto cleanup;
                        }
 
                        if (!(update->flags & REF_ISPRUNING))
                                string_list_append(&refs_to_delete,
-                                                  update->lock->ref_name);
+                                                  lock->ref_name);
                }
        }
 
-       if (repack_without_refs(&refs_to_delete, err)) {
+       if (repack_without_refs(refs, &refs_to_delete, err)) {
                ret = TRANSACTION_GENERIC_ERROR;
                goto cleanup;
        }
@@ -3850,8 +3805,8 @@ int ref_transaction_commit(struct ref_transaction *transaction,
        transaction->state = REF_TRANSACTION_CLOSED;
 
        for (i = 0; i < transaction->nr; i++)
-               if (transaction->updates[i]->lock)
-                       unlock_ref(transaction->updates[i]->lock);
+               if (transaction->updates[i]->backend_data)
+                       unlock_ref(transaction->updates[i]->backend_data);
        string_list_clear(&refs_to_delete, 0);
        free(head_ref);
        string_list_clear(&affected_refnames, 0);
@@ -3867,11 +3822,12 @@ static int ref_present(const char *refname,
        return string_list_has_string(affected_refnames, refname);
 }
 
-int initial_ref_transaction_commit(struct ref_transaction *transaction,
-                                  struct strbuf *err)
+static int files_initial_transaction_commit(struct ref_store *ref_store,
+                                           struct ref_transaction *transaction,
+                                           struct strbuf *err)
 {
        struct files_ref_store *refs =
-               get_files_ref_store(NULL, "initial_ref_transaction_commit");
+               files_downcast(ref_store, 0, "initial_ref_transaction_commit");
        int ret = 0, i;
        struct string_list affected_refnames = STRING_LIST_INIT_NODUP;
 
@@ -3919,7 +3875,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
                }
        }
 
-       if (lock_packed_refs(0)) {
+       if (lock_packed_refs(refs, 0)) {
                strbuf_addf(err, "unable to lock packed-refs file: %s",
                            strerror(errno));
                ret = TRANSACTION_GENERIC_ERROR;
@@ -3934,7 +3890,7 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
                        add_packed_ref(refs, update->refname, update->new_sha1);
        }
 
-       if (commit_packed_refs()) {
+       if (commit_packed_refs(refs)) {
                strbuf_addf(err, "unable to commit packed-refs file: %s",
                            strerror(errno));
                ret = TRANSACTION_GENERIC_ERROR;
@@ -3984,13 +3940,16 @@ static int expire_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
        return 0;
 }
 
-int reflog_expire(const char *refname, const unsigned char *sha1,
-                unsigned int flags,
-                reflog_expiry_prepare_fn prepare_fn,
-                reflog_expiry_should_prune_fn should_prune_fn,
-                reflog_expiry_cleanup_fn cleanup_fn,
-                void *policy_cb_data)
+static int files_reflog_expire(struct ref_store *ref_store,
+                              const char *refname, const unsigned char *sha1,
+                              unsigned int flags,
+                              reflog_expiry_prepare_fn prepare_fn,
+                              reflog_expiry_should_prune_fn should_prune_fn,
+                              reflog_expiry_cleanup_fn cleanup_fn,
+                              void *policy_cb_data)
 {
+       struct files_ref_store *refs =
+               files_downcast(ref_store, 0, "reflog_expire");
        static struct lock_file reflog_lock;
        struct expire_reflog_cb cb;
        struct ref_lock *lock;
@@ -4009,7 +3968,8 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
         * reference itself, plus we might need to update the
         * reference if --updateref was specified:
         */
-       lock = lock_ref_sha1_basic(refname, sha1, NULL, NULL, REF_NODEREF,
+       lock = lock_ref_sha1_basic(refs, refname, sha1,
+                                  NULL, NULL, REF_NODEREF,
                                   &type, &err);
        if (!lock) {
                error("cannot lock ref '%s': %s", refname, err.buf);
@@ -4090,8 +4050,46 @@ int reflog_expire(const char *refname, const unsigned char *sha1,
        return -1;
 }
 
+static int files_init_db(struct ref_store *ref_store, struct strbuf *err)
+{
+       /* Check validity (but we don't need the result): */
+       files_downcast(ref_store, 0, "init_db");
+
+       /*
+        * Create .git/refs/{heads,tags}
+        */
+       safe_create_dir(git_path("refs/heads"), 1);
+       safe_create_dir(git_path("refs/tags"), 1);
+       if (get_shared_repository()) {
+               adjust_shared_perm(git_path("refs/heads"));
+               adjust_shared_perm(git_path("refs/tags"));
+       }
+       return 0;
+}
+
 struct ref_storage_be refs_be_files = {
        NULL,
        "files",
-       files_ref_store_create
+       files_ref_store_create,
+       files_init_db,
+       files_transaction_commit,
+       files_initial_transaction_commit,
+
+       files_pack_refs,
+       files_peel_ref,
+       files_create_symref,
+       files_delete_refs,
+       files_rename_ref,
+
+       files_ref_iterator_begin,
+       files_read_raw_ref,
+       files_verify_refname_available,
+
+       files_reflog_iterator_begin,
+       files_for_each_reflog_ent,
+       files_for_each_reflog_ent_reverse,
+       files_reflog_exists,
+       files_create_reflog,
+       files_delete_reflog,
+       files_reflog_expire
 };