]> git.ipfire.org Git - thirdparty/git.git/blobdiff - refs.c
environment.h: move declarations for environment.c functions from cache.h
[thirdparty/git.git] / refs.c
diff --git a/refs.c b/refs.c
index d7cc0a23a3b65502242650cce698179e3cca27d4..04520e5a6b82fb566d1244c8da9ac573576d8003 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -3,8 +3,12 @@
  */
 
 #include "cache.h"
+#include "alloc.h"
 #include "config.h"
+#include "environment.h"
 #include "hashmap.h"
+#include "gettext.h"
+#include "hex.h"
 #include "lockfile.h"
 #include "iterator.h"
 #include "refs.h"
@@ -19,6 +23,9 @@
 #include "strvec.h"
 #include "repository.h"
 #include "sigchain.h"
+#include "date.h"
+#include "commit.h"
+#include "wrapper.h"
 
 /*
  * List of all available backends
@@ -55,6 +62,88 @@ static unsigned char refname_disposition[256] = {
        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 4, 4
 };
 
+struct ref_namespace_info ref_namespace[] = {
+       [NAMESPACE_HEAD] = {
+               .ref = "HEAD",
+               .decoration = DECORATION_REF_HEAD,
+               .exact = 1,
+       },
+       [NAMESPACE_BRANCHES] = {
+               .ref = "refs/heads/",
+               .decoration = DECORATION_REF_LOCAL,
+       },
+       [NAMESPACE_TAGS] = {
+               .ref = "refs/tags/",
+               .decoration = DECORATION_REF_TAG,
+       },
+       [NAMESPACE_REMOTE_REFS] = {
+               /*
+                * The default refspec for new remotes copies refs from
+                * refs/heads/ on the remote into refs/remotes/<remote>/.
+                * As such, "refs/remotes/" has special handling.
+                */
+               .ref = "refs/remotes/",
+               .decoration = DECORATION_REF_REMOTE,
+       },
+       [NAMESPACE_STASH] = {
+               /*
+                * The single ref "refs/stash" stores the latest stash.
+                * Older stashes can be found in the reflog.
+                */
+               .ref = "refs/stash",
+               .exact = 1,
+               .decoration = DECORATION_REF_STASH,
+       },
+       [NAMESPACE_REPLACE] = {
+               /*
+                * This namespace allows Git to act as if one object ID
+                * points to the content of another. Unlike the other
+                * ref namespaces, this one can be changed by the
+                * GIT_REPLACE_REF_BASE environment variable. This
+                * .namespace value will be overwritten in setup_git_env().
+                */
+               .ref = "refs/replace/",
+               .decoration = DECORATION_GRAFTED,
+       },
+       [NAMESPACE_NOTES] = {
+               /*
+                * The refs/notes/commit ref points to the tip of a
+                * parallel commit history that adds metadata to commits
+                * in the normal history. This ref can be overwritten
+                * by the core.notesRef config variable or the
+                * GIT_NOTES_REFS environment variable.
+                */
+               .ref = "refs/notes/commit",
+               .exact = 1,
+       },
+       [NAMESPACE_PREFETCH] = {
+               /*
+                * Prefetch refs are written by the background 'fetch'
+                * maintenance task. It allows faster foreground fetches
+                * by advertising these previously-downloaded tips without
+                * updating refs/remotes/ without user intervention.
+                */
+               .ref = "refs/prefetch/",
+       },
+       [NAMESPACE_REWRITTEN] = {
+               /*
+                * Rewritten refs are used by the 'label' command in the
+                * sequencer. These are particularly useful during an
+                * interactive rebase that uses the 'merge' command.
+                */
+               .ref = "refs/rewritten/",
+       },
+};
+
+void update_ref_namespace(enum ref_namespace namespace, char *ref)
+{
+       struct ref_namespace_info *info = &ref_namespace[namespace];
+       if (info->ref_updated)
+               free(info->ref);
+       info->ref = ref;
+       info->ref_updated = 1;
+}
+
 /*
  * Try to read one refname component from the front of refname.
  * Return the length of the component found, or -1 if the component is
@@ -291,20 +380,16 @@ struct ref_filter {
        void *cb_data;
 };
 
-int refs_read_ref_full(struct ref_store *refs, const char *refname,
-                      int resolve_flags, struct object_id *oid, int *flags)
+int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
 {
-       if (refs_resolve_ref_unsafe(refs, refname, resolve_flags, oid, flags))
+       struct ref_store *refs = get_main_ref_store(the_repository);
+
+       if (refs_resolve_ref_unsafe(refs, refname, resolve_flags,
+                                   oid, flags))
                return 0;
        return -1;
 }
 
-int read_ref_full(const char *refname, int resolve_flags, struct object_id *oid, int *flags)
-{
-       return refs_read_ref_full(get_main_ref_store(the_repository), refname,
-                                 resolve_flags, oid, flags);
-}
-
 int read_ref(const char *refname, struct object_id *oid)
 {
        return read_ref_full(refname, RESOLVE_REF_READING, oid, NULL);
@@ -312,7 +397,8 @@ int read_ref(const char *refname, struct object_id *oid)
 
 int refs_ref_exists(struct ref_store *refs, const char *refname)
 {
-       return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING, NULL, NULL);
+       return !!refs_resolve_ref_unsafe(refs, refname, RESOLVE_REF_READING,
+                                        NULL, NULL);
 }
 
 int ref_exists(const char *refname)
@@ -360,7 +446,8 @@ struct warn_if_dangling_data {
        const char *msg_fmt;
 };
 
-static int warn_if_dangling_symref(const char *refname, const struct object_id *oid,
+static int warn_if_dangling_symref(const char *refname,
+                                  const struct object_id *oid UNUSED,
                                   int flags, void *cb_data)
 {
        struct warn_if_dangling_data *d = cb_data;
@@ -457,11 +544,16 @@ void normalize_glob_ref(struct string_list_item *item, const char *prefix,
        if (*pattern == '/')
                BUG("pattern must not start with '/'");
 
-       if (prefix) {
+       if (prefix)
                strbuf_addstr(&normalized_pattern, prefix);
-       }
-       else if (!starts_with(pattern, "refs/"))
+       else if (!starts_with(pattern, "refs/") &&
+                  strcmp(pattern, "HEAD"))
                strbuf_addstr(&normalized_pattern, "refs/");
+       /*
+        * NEEDSWORK: Special case other symrefs such as REBASE_HEAD,
+        * MERGE_HEAD, etc.
+        */
+
        strbuf_addstr(&normalized_pattern, pattern);
        strbuf_strip_suffix(&normalized_pattern, "/");
 
@@ -655,12 +747,13 @@ int expand_ref(struct repository *repo, const char *str, int len,
                struct object_id oid_from_ref;
                struct object_id *this_result;
                int flag;
+               struct ref_store *refs = get_main_ref_store(repo);
 
                this_result = refs_found ? &oid_from_ref : oid;
                strbuf_reset(&fullref);
                strbuf_addf(&fullref, *p, len, str);
-               r = refs_resolve_ref_unsafe(get_main_ref_store(repo),
-                                           fullref.buf, RESOLVE_REF_READING,
+               r = refs_resolve_ref_unsafe(refs, fullref.buf,
+                                           RESOLVE_REF_READING,
                                            this_result, &flag);
                if (r) {
                        if (!refs_found++)
@@ -723,7 +816,7 @@ int dwim_log(const char *str, int len, struct object_id *oid, char **log)
        return repo_dwim_log(the_repository, str, len, oid, log);
 }
 
-static int is_per_worktree_ref(const char *refname)
+int is_per_worktree_ref(const char *refname)
 {
        return starts_with(refname, "refs/worktree/") ||
               starts_with(refname, "refs/bisect/") ||
@@ -739,37 +832,63 @@ static int is_pseudoref_syntax(const char *refname)
                        return 0;
        }
 
+       /*
+        * HEAD is not a pseudoref, but it certainly uses the
+        * pseudoref syntax.
+        */
        return 1;
 }
 
-static int is_main_pseudoref_syntax(const char *refname)
-{
-       return skip_prefix(refname, "main-worktree/", &refname) &&
-               *refname &&
-               is_pseudoref_syntax(refname);
+static int is_current_worktree_ref(const char *ref) {
+       return is_pseudoref_syntax(ref) || is_per_worktree_ref(ref);
 }
 
-static int is_other_pseudoref_syntax(const char *refname)
+enum ref_worktree_type parse_worktree_ref(const char *maybe_worktree_ref,
+                                         const char **worktree_name, int *worktree_name_length,
+                                         const char **bare_refname)
 {
-       if (!skip_prefix(refname, "worktrees/", &refname))
-               return 0;
-       refname = strchr(refname, '/');
-       if (!refname || !refname[1])
-               return 0;
-       return is_pseudoref_syntax(refname + 1);
-}
+       const char *name_dummy;
+       int name_length_dummy;
+       const char *ref_dummy;
 
-enum ref_type ref_type(const char *refname)
-{
-       if (is_per_worktree_ref(refname))
-               return REF_TYPE_PER_WORKTREE;
-       if (is_pseudoref_syntax(refname))
-               return REF_TYPE_PSEUDOREF;
-       if (is_main_pseudoref_syntax(refname))
-               return REF_TYPE_MAIN_PSEUDOREF;
-       if (is_other_pseudoref_syntax(refname))
-               return REF_TYPE_OTHER_PSEUDOREF;
-       return REF_TYPE_NORMAL;
+       if (!worktree_name)
+               worktree_name = &name_dummy;
+       if (!worktree_name_length)
+               worktree_name_length = &name_length_dummy;
+       if (!bare_refname)
+               bare_refname = &ref_dummy;
+
+       if (skip_prefix(maybe_worktree_ref, "worktrees/", bare_refname)) {
+               const char *slash = strchr(*bare_refname, '/');
+
+               *worktree_name = *bare_refname;
+               if (!slash) {
+                       *worktree_name_length = strlen(*worktree_name);
+
+                       /* This is an error condition, and the caller tell because the bare_refname is "" */
+                       *bare_refname = *worktree_name + *worktree_name_length;
+                       return REF_WORKTREE_OTHER;
+               }
+
+               *worktree_name_length = slash - *bare_refname;
+               *bare_refname = slash + 1;
+
+               if (is_current_worktree_ref(*bare_refname))
+                       return REF_WORKTREE_OTHER;
+       }
+
+       *worktree_name = NULL;
+       *worktree_name_length = 0;
+
+       if (skip_prefix(maybe_worktree_ref, "main-worktree/", bare_refname)
+           && is_current_worktree_ref(*bare_refname))
+               return REF_WORKTREE_MAIN;
+
+       *bare_refname = maybe_worktree_ref;
+       if (is_current_worktree_ref(maybe_worktree_ref))
+               return REF_WORKTREE_CURRENT;
+
+       return REF_WORKTREE_SHARED;
 }
 
 long get_files_ref_lock_timeout_ms(void)
@@ -894,8 +1013,9 @@ static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
 }
 
 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
-               const char *email, timestamp_t timestamp, int tz,
-               const char *message, void *cb_data)
+                          const char *email UNUSED,
+                          timestamp_t timestamp, int tz,
+                          const char *message, void *cb_data)
 {
        struct read_ref_at_cb *cb = cb_data;
        int reached_count;
@@ -935,9 +1055,11 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
        return cb->found_it;
 }
 
-static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
-                                 const char *email, timestamp_t timestamp,
-                                 int tz, const char *message, void *cb_data)
+static int read_ref_at_ent_newest(struct object_id *ooid UNUSED,
+                                 struct object_id *noid,
+                                 const char *email UNUSED,
+                                 timestamp_t timestamp, int tz,
+                                 const char *message, void *cb_data)
 {
        struct read_ref_at_cb *cb = cb_data;
 
@@ -948,8 +1070,9 @@ static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid
 }
 
 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
-                                 const char *email, timestamp_t timestamp,
-                                 int tz, const char *message, void *cb_data)
+                                 const char *email UNUSED,
+                                 timestamp_t timestamp, int tz,
+                                 const char *message, void *cb_data)
 {
        struct read_ref_at_cb *cb = cb_data;
 
@@ -1078,9 +1201,10 @@ int ref_transaction_update(struct ref_transaction *transaction,
 {
        assert(err);
 
-       if ((new_oid && !is_null_oid(new_oid)) ?
-           check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
-           !refname_is_safe(refname)) {
+       if (!(flags & REF_SKIP_REFNAME_VERIFICATION) &&
+           ((new_oid && !is_null_oid(new_oid)) ?
+                    check_refname_format(refname, REFNAME_ALLOW_ONELEVEL) :
+                          !refname_is_safe(refname))) {
                strbuf_addf(err, _("refusing to update ref with bad name '%s'"),
                            refname);
                return -1;
@@ -1089,6 +1213,13 @@ int ref_transaction_update(struct ref_transaction *transaction,
        if (flags & ~REF_TRANSACTION_UPDATE_ALLOWED_FLAGS)
                BUG("illegal flags 0x%x passed to ref_transaction_update()", flags);
 
+       /*
+        * Clear flags outside the allowed set; this should be a noop because
+        * of the BUG() check above, but it works around a -Wnonnull warning
+        * with some versions of "gcc -O3".
+        */
+       flags &= REF_TRANSACTION_UPDATE_ALLOWED_FLAGS;
+
        flags |= (new_oid ? REF_HAVE_NEW : 0) | (old_oid ? REF_HAVE_OLD : 0);
 
        ref_transaction_add_update(transaction, refname, flags,
@@ -1102,8 +1233,10 @@ int ref_transaction_create(struct ref_transaction *transaction,
                           unsigned int flags, const char *msg,
                           struct strbuf *err)
 {
-       if (!new_oid || is_null_oid(new_oid))
-               BUG("create called without valid new_oid");
+       if (!new_oid || is_null_oid(new_oid)) {
+               strbuf_addf(err, "'%s' has a null OID", refname);
+               return 1;
+       }
        return ref_transaction_update(transaction, refname, new_oid,
                                      null_oid(), flags, msg, err);
 }
@@ -1182,65 +1315,68 @@ int update_ref(const char *msg, const char *refname,
                               old_oid, flags, onerr);
 }
 
+/*
+ * Check that the string refname matches a rule of the form
+ * "{prefix}%.*s{suffix}". So "foo/bar/baz" would match the rule
+ * "foo/%.*s/baz", and return the string "bar".
+ */
+static const char *match_parse_rule(const char *refname, const char *rule,
+                                   size_t *len)
+{
+       /*
+        * Check that rule matches refname up to the first percent in the rule.
+        * We can bail immediately if not, but otherwise we leave "rule" at the
+        * %-placeholder, and "refname" at the start of the potential matched
+        * name.
+        */
+       while (*rule != '%') {
+               if (!*rule)
+                       BUG("rev-parse rule did not have percent");
+               if (*refname++ != *rule++)
+                       return NULL;
+       }
+
+       /*
+        * Check that our "%" is the expected placeholder. This assumes there
+        * are no other percents (placeholder or quoted) in the string, but
+        * that is sufficient for our rev-parse rules.
+        */
+       if (!skip_prefix(rule, "%.*s", &rule))
+               return NULL;
+
+       /*
+        * And now check that our suffix (if any) matches.
+        */
+       if (!strip_suffix(refname, rule, len))
+               return NULL;
+
+       return refname; /* len set by strip_suffix() */
+}
+
 char *refs_shorten_unambiguous_ref(struct ref_store *refs,
                                   const char *refname, int strict)
 {
        int i;
-       static char **scanf_fmts;
-       static int nr_rules;
-       char *short_name;
        struct strbuf resolved_buf = STRBUF_INIT;
 
-       if (!nr_rules) {
-               /*
-                * Pre-generate scanf formats from ref_rev_parse_rules[].
-                * Generate a format suitable for scanf from a
-                * ref_rev_parse_rules rule by interpolating "%s" at the
-                * location of the "%.*s".
-                */
-               size_t total_len = 0;
-               size_t offset = 0;
-
-               /* the rule list is NULL terminated, count them first */
-               for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
-                       /* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
-                       total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;
-
-               scanf_fmts = xmalloc(st_add(st_mult(sizeof(char *), nr_rules), total_len));
-
-               offset = 0;
-               for (i = 0; i < nr_rules; i++) {
-                       assert(offset < total_len);
-                       scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
-                       offset += xsnprintf(scanf_fmts[i], total_len - offset,
-                                           ref_rev_parse_rules[i], 2, "%s") + 1;
-               }
-       }
-
-       /* bail out if there are no rules */
-       if (!nr_rules)
-               return xstrdup(refname);
-
-       /* buffer for scanf result, at most refname must fit */
-       short_name = xstrdup(refname);
-
        /* skip first rule, it will always match */
-       for (i = nr_rules - 1; i > 0 ; --i) {
+       for (i = NUM_REV_PARSE_RULES - 1; i > 0 ; --i) {
                int j;
                int rules_to_fail = i;
-               int short_name_len;
+               const char *short_name;
+               size_t short_name_len;
 
-               if (1 != sscanf(refname, scanf_fmts[i], short_name))
+               short_name = match_parse_rule(refname, ref_rev_parse_rules[i],
+                                             &short_name_len);
+               if (!short_name)
                        continue;
 
-               short_name_len = strlen(short_name);
-
                /*
                 * in strict mode, all (except the matched one) rules
                 * must fail to resolve to a valid non-ambiguous ref
                 */
                if (strict)
-                       rules_to_fail = nr_rules;
+                       rules_to_fail = NUM_REV_PARSE_RULES;
 
                /*
                 * check if the short name resolves to a valid ref,
@@ -1260,7 +1396,8 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
                         */
                        strbuf_reset(&resolved_buf);
                        strbuf_addf(&resolved_buf, rule,
-                                   short_name_len, short_name);
+                                   cast_size_t_to_int(short_name_len),
+                                   short_name);
                        if (refs_ref_exists(refs, resolved_buf.buf))
                                break;
                }
@@ -1271,12 +1408,11 @@ char *refs_shorten_unambiguous_ref(struct ref_store *refs,
                 */
                if (j == rules_to_fail) {
                        strbuf_release(&resolved_buf);
-                       return short_name;
+                       return xmemdupz(short_name, short_name_len);
                }
        }
 
        strbuf_release(&resolved_buf);
-       free(short_name);
        return xstrdup(refname);
 }
 
@@ -1286,9 +1422,8 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
                                            refname, strict);
 }
 
-static struct string_list *hide_refs;
-
-int parse_hide_refs_config(const char *var, const char *value, const char *section)
+int parse_hide_refs_config(const char *var, const char *value, const char *section,
+                          struct string_list *hide_refs)
 {
        const char *key;
        if (!strcmp("transfer.hiderefs", var) ||
@@ -1303,21 +1438,16 @@ int parse_hide_refs_config(const char *var, const char *value, const char *secti
                len = strlen(ref);
                while (len && ref[len - 1] == '/')
                        ref[--len] = '\0';
-               if (!hide_refs) {
-                       CALLOC_ARRAY(hide_refs, 1);
-                       hide_refs->strdup_strings = 1;
-               }
-               string_list_append(hide_refs, ref);
+               string_list_append_nodup(hide_refs, ref);
        }
        return 0;
 }
 
-int ref_is_hidden(const char *refname, const char *refname_full)
+int ref_is_hidden(const char *refname, const char *refname_full,
+                 const struct string_list *hide_refs)
 {
        int i;
 
-       if (!hide_refs)
-               return 0;
        for (i = hide_refs->nr - 1; i >= 0; i--) {
                const char *match = hide_refs->items[i].string;
                const char *subject;
@@ -1373,32 +1503,13 @@ const char *find_descendant_ref(const char *dirname,
        return NULL;
 }
 
-int refs_rename_ref_available(struct ref_store *refs,
-                             const char *old_refname,
-                             const char *new_refname)
-{
-       struct string_list skip = STRING_LIST_INIT_NODUP;
-       struct strbuf err = STRBUF_INIT;
-       int ok;
-
-       string_list_insert(&skip, old_refname);
-       ok = !refs_verify_refname_available(refs, new_refname,
-                                           NULL, &skip, &err);
-       if (!ok)
-               error("%s", err.buf);
-
-       string_list_clear(&skip, 0);
-       strbuf_release(&err);
-       return ok;
-}
-
 int refs_head_ref(struct ref_store *refs, each_ref_fn fn, void *cb_data)
 {
        struct object_id oid;
        int flag;
 
-       if (!refs_read_ref_full(refs, "HEAD", RESOLVE_REF_READING,
-                               &oid, &flag))
+       if (refs_resolve_ref_unsafe(refs, "HEAD", RESOLVE_REF_READING,
+                                   &oid, &flag))
                return fn("HEAD", &oid, flag, cb_data);
 
        return 0;
@@ -1534,6 +1645,7 @@ int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
 
 int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
 {
+       const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
        return do_for_each_repo_ref(r, git_replace_ref_base, fn,
                                    strlen(git_replace_ref_base),
                                    DO_FOR_EACH_INCLUDE_BROKEN, cb_data);
@@ -1619,9 +1731,10 @@ static void find_longest_prefixes(struct string_list *out,
        strbuf_release(&prefix);
 }
 
-int for_each_fullref_in_prefixes(const char *namespace,
-                                const char **patterns,
-                                each_ref_fn fn, void *cb_data)
+int refs_for_each_fullref_in_prefixes(struct ref_store *ref_store,
+                                     const char *namespace,
+                                     const char **patterns,
+                                     each_ref_fn fn, void *cb_data)
 {
        struct string_list prefixes = STRING_LIST_INIT_DUP;
        struct string_list_item *prefix;
@@ -1636,7 +1749,7 @@ int for_each_fullref_in_prefixes(const char *namespace,
 
        for_each_string_list_item(prefix, &prefixes) {
                strbuf_addstr(&buf, prefix->string);
-               ret = for_each_fullref_in(buf.buf, fn, cb_data);
+               ret = refs_for_each_fullref_in(ref_store, buf.buf, fn, cb_data);
                if (ret)
                        break;
                strbuf_setlen(&buf, namespace_len);
@@ -1649,7 +1762,8 @@ int for_each_fullref_in_prefixes(const char *namespace,
 
 static int refs_read_special_head(struct ref_store *ref_store,
                                  const char *refname, struct object_id *oid,
-                                 struct strbuf *referent, unsigned int *type)
+                                 struct strbuf *referent, unsigned int *type,
+                                 int *failure_errno)
 {
        struct strbuf full_path = STRBUF_INIT;
        struct strbuf content = STRBUF_INIT;
@@ -1659,7 +1773,8 @@ static int refs_read_special_head(struct ref_store *ref_store,
        if (strbuf_read_file(&content, full_path.buf, 0) < 0)
                goto done;
 
-       result = parse_loose_ref_contents(content.buf, oid, referent, type);
+       result = parse_loose_ref_contents(content.buf, oid, referent, type,
+                                         failure_errno);
 
 done:
        strbuf_release(&full_path);
@@ -1667,24 +1782,31 @@ done:
        return result;
 }
 
-int refs_read_raw_ref(struct ref_store *ref_store,
-                     const char *refname, struct object_id *oid,
-                     struct strbuf *referent, unsigned int *type)
+int refs_read_raw_ref(struct ref_store *ref_store, const char *refname,
+                     struct object_id *oid, struct strbuf *referent,
+                     unsigned int *type, int *failure_errno)
 {
+       assert(failure_errno);
        if (!strcmp(refname, "FETCH_HEAD") || !strcmp(refname, "MERGE_HEAD")) {
                return refs_read_special_head(ref_store, refname, oid, referent,
-                                             type);
+                                             type, failure_errno);
        }
 
        return ref_store->be->read_raw_ref(ref_store, refname, oid, referent,
-                                          type, &errno);
+                                          type, failure_errno);
+}
+
+int refs_read_symbolic_ref(struct ref_store *ref_store, const char *refname,
+                          struct strbuf *referent)
+{
+       return ref_store->be->read_symbolic_ref(ref_store, refname, referent);
 }
 
-/* This function needs to return a meaningful errno on failure */
 const char *refs_resolve_ref_unsafe(struct ref_store *refs,
                                    const char *refname,
                                    int resolve_flags,
-                                   struct object_id *oid, int *flags)
+                                   struct object_id *oid,
+                                   int *flags)
 {
        static struct strbuf sb_refname = STRBUF_INIT;
        struct object_id unused_oid;
@@ -1700,10 +1822,8 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
 
        if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
                if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
-                   !refname_is_safe(refname)) {
-                       errno = EINVAL;
+                   !refname_is_safe(refname))
                        return NULL;
-               }
 
                /*
                 * dwim_ref() uses REF_ISBROKEN to distinguish between
@@ -1718,9 +1838,10 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
 
        for (symref_count = 0; symref_count < SYMREF_MAXDEPTH; symref_count++) {
                unsigned int read_flags = 0;
+               int failure_errno;
 
-               if (refs_read_raw_ref(refs, refname,
-                                     oid, &sb_refname, &read_flags)) {
+               if (refs_read_raw_ref(refs, refname, oid, &sb_refname,
+                                     &read_flags, &failure_errno)) {
                        *flags |= read_flags;
 
                        /* In reading mode, refs must eventually resolve */
@@ -1732,9 +1853,9 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
                         * may show errors besides ENOENT if there are
                         * similarly-named refs.
                         */
-                       if (errno != ENOENT &&
-                           errno != EISDIR &&
-                           errno != ENOTDIR)
+                       if (failure_errno != ENOENT &&
+                           failure_errno != EISDIR &&
+                           failure_errno != ENOTDIR)
                                return NULL;
 
                        oidclr(oid);
@@ -1760,16 +1881,13 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
                }
                if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL)) {
                        if (!(resolve_flags & RESOLVE_REF_ALLOW_BAD_NAME) ||
-                           !refname_is_safe(refname)) {
-                               errno = EINVAL;
+                           !refname_is_safe(refname))
                                return NULL;
-                       }
 
                        *flags |= REF_ISBROKEN | REF_BAD_NAME;
                }
        }
 
-       errno = ELOOP;
        return NULL;
 }
 
@@ -1815,7 +1933,7 @@ struct ref_store_hash_entry
        char name[FLEX_ARRAY];
 };
 
-static int ref_store_hash_cmp(const void *unused_cmp_data,
+static int ref_store_hash_cmp(const void *cmp_data UNUSED,
                              const struct hashmap_entry *eptr,
                              const struct hashmap_entry *entry_or_key,
                              const void *keydata)
@@ -2002,10 +2120,12 @@ struct ref_store *get_worktree_ref_store(const struct worktree *wt)
        return refs;
 }
 
-void base_ref_store_init(struct ref_store *refs,
-                        const struct ref_storage_be *be)
+void base_ref_store_init(struct ref_store *refs, struct repository *repo,
+                        const char *path, const struct ref_storage_be *be)
 {
        refs->be = be;
+       refs->repo = repo;
+       refs->gitdir = xstrdup(path);
 }
 
 /* backend functions */
@@ -2102,8 +2222,11 @@ static int run_transaction_hook(struct ref_transaction *transaction,
                            update->refname);
 
                if (write_in_full(proc.in, buf.buf, buf.len) < 0) {
-                       if (errno != EPIPE)
+                       if (errno != EPIPE) {
+                               /* Don't leak errno outside this API */
+                               errno = 0;
                                ret = -1;
+                       }
                        break;
                }
        }
@@ -2137,7 +2260,7 @@ int ref_transaction_prepare(struct ref_transaction *transaction,
                break;
        }
 
-       if (getenv(GIT_QUARANTINE_ENVIRONMENT)) {
+       if (refs->repo->objects->odb->disable_ref_updates) {
                strbuf_addstr(err,
                              _("ref updates forbidden inside quarantine environment"));
                return -1;
@@ -2238,6 +2361,13 @@ int refs_verify_refname_available(struct ref_store *refs,
 
        strbuf_grow(&dirname, strlen(refname) + 1);
        for (slash = strchr(refname, '/'); slash; slash = strchr(slash + 1, '/')) {
+               /*
+                * Just saying "Is a directory" when we e.g. can't
+                * lock some multi-level ref isn't very informative,
+                * the user won't be told *what* is a directory, so
+                * let's not use strerror() below.
+                */
+               int ignore_errno;
                /* Expand dirname to the new prefix, not including the trailing slash: */
                strbuf_add(&dirname, refname + dirname.len, slash - refname - dirname.len);
 
@@ -2249,7 +2379,8 @@ int refs_verify_refname_available(struct ref_store *refs,
                if (skip && string_list_has_string(skip, dirname.buf))
                        continue;
 
-               if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent, &type)) {
+               if (!refs_read_raw_ref(refs, dirname.buf, &oid, &referent,
+                                      &type, &ignore_errno)) {
                        strbuf_addf(err, _("'%s' exists; cannot create '%s'"),
                                    dirname.buf, refname);
                        goto cleanup;
@@ -2358,16 +2489,15 @@ int reflog_exists(const char *refname)
 }
 
 int refs_create_reflog(struct ref_store *refs, const char *refname,
-                      int force_create, struct strbuf *err)
+                      struct strbuf *err)
 {
-       return refs->be->create_reflog(refs, refname, force_create, err);
+       return refs->be->create_reflog(refs, refname, err);
 }
 
-int safe_create_reflog(const char *refname, int force_create,
-                      struct strbuf *err)
+int safe_create_reflog(const char *refname, struct strbuf *err)
 {
        return refs_create_reflog(get_main_ref_store(the_repository), refname,
-                                 force_create, err);
+                                 err);
 }
 
 int refs_delete_reflog(struct ref_store *refs, const char *refname)
@@ -2414,6 +2544,22 @@ int initial_ref_transaction_commit(struct ref_transaction *transaction,
        return refs->be->initial_transaction_commit(refs, transaction, err);
 }
 
+void ref_transaction_for_each_queued_update(struct ref_transaction *transaction,
+                                           ref_transaction_for_each_queued_update_fn cb,
+                                           void *cb_data)
+{
+       int i;
+
+       for (i = 0; i < transaction->nr; i++) {
+               struct ref_update *update = transaction->updates[i];
+
+               cb(update->refname,
+                  (update->flags & REF_HAVE_OLD) ? &update->old_oid : NULL,
+                  (update->flags & REF_HAVE_NEW) ? &update->new_oid : NULL,
+                  cb_data);
+       }
+}
+
 int refs_delete_refs(struct ref_store *refs, const char *logmsg,
                     struct string_list *refnames, unsigned int flags)
 {