]> git.ipfire.org Git - thirdparty/git.git/blobdiff - refs.c
refs API: make parse_loose_ref_contents() not set errno
[thirdparty/git.git] / refs.c
diff --git a/refs.c b/refs.c
index c28bd6a8189b5e9b121ea7178204b96686b0d6e4..bb09993c2f96512602b033c08e4fc540650fe881 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -10,6 +10,7 @@
 #include "refs.h"
 #include "refs/refs-internal.h"
 #include "run-command.h"
+#include "hook.h"
 #include "object-store.h"
 #include "object.h"
 #include "tag.h"
@@ -33,11 +34,6 @@ static struct ref_storage_be *find_ref_storage_backend(const char *name)
        return NULL;
 }
 
-int ref_storage_backend_exists(const char *name)
-{
-       return find_ref_storage_backend(name) != NULL;
-}
-
 /*
  * How to handle various characters in refnames:
  * 0: An acceptable character for refs
@@ -698,7 +694,7 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
                strbuf_addf(&path, *p, len, str);
                ref = refs_resolve_ref_unsafe(refs, path.buf,
                                              RESOLVE_REF_READING,
-                                             &hash, NULL);
+                                             oid ? &hash : NULL, NULL);
                if (!ref)
                        continue;
                if (refs_reflog_exists(refs, path.buf))
@@ -710,7 +706,8 @@ int repo_dwim_log(struct repository *r, const char *str, int len,
                        continue;
                if (!logs_found++) {
                        *log = xstrdup(it);
-                       oidcpy(oid, &hash);
+                       if (oid)
+                               oidcpy(oid, &hash);
                }
                if (!warn_ambiguous_refs)
                        break;
@@ -1418,10 +1415,16 @@ struct ref_iterator *refs_ref_iterator_begin(
 {
        struct ref_iterator *iter;
 
-       if (ref_paranoia < 0)
-               ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 0);
-       if (ref_paranoia)
-               flags |= DO_FOR_EACH_INCLUDE_BROKEN;
+       if (!(flags & DO_FOR_EACH_INCLUDE_BROKEN)) {
+               static int ref_paranoia = -1;
+
+               if (ref_paranoia < 0)
+                       ref_paranoia = git_env_bool("GIT_REF_PARANOIA", 1);
+               if (ref_paranoia) {
+                       flags |= DO_FOR_EACH_INCLUDE_BROKEN;
+                       flags |= DO_FOR_EACH_OMIT_DANGLING_SYMREFS;
+               }
+       }
 
        iter = refs->be->iterator_begin(refs, prefix, flags);
 
@@ -1516,25 +1519,16 @@ int for_each_ref_in(const char *prefix, each_ref_fn fn, void *cb_data)
        return refs_for_each_ref_in(get_main_ref_store(the_repository), prefix, fn, cb_data);
 }
 
-int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data, unsigned int broken)
+int for_each_fullref_in(const char *prefix, each_ref_fn fn, void *cb_data)
 {
-       enum do_for_each_ref_flags flag = 0;
-
-       if (broken)
-               flag = DO_FOR_EACH_INCLUDE_BROKEN;
        return do_for_each_ref(get_main_ref_store(the_repository),
-                              prefix, fn, 0, flag, cb_data);
+                              prefix, fn, 0, 0, cb_data);
 }
 
 int refs_for_each_fullref_in(struct ref_store *refs, const char *prefix,
-                            each_ref_fn fn, void *cb_data,
-                            unsigned int broken)
+                            each_ref_fn fn, void *cb_data)
 {
-       enum do_for_each_ref_flags flag = 0;
-
-       if (broken)
-               flag = DO_FOR_EACH_INCLUDE_BROKEN;
-       return do_for_each_ref(refs, prefix, fn, 0, flag, cb_data);
+       return do_for_each_ref(refs, prefix, fn, 0, 0, cb_data);
 }
 
 int for_each_replace_ref(struct repository *r, each_repo_ref_fn fn, void *cb_data)
@@ -1626,8 +1620,7 @@ static void find_longest_prefixes(struct string_list *out,
 
 int for_each_fullref_in_prefixes(const char *namespace,
                                 const char **patterns,
-                                each_ref_fn fn, void *cb_data,
-                                unsigned int broken)
+                                each_ref_fn fn, void *cb_data)
 {
        struct string_list prefixes = STRING_LIST_INIT_DUP;
        struct string_list_item *prefix;
@@ -1642,7 +1635,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, broken);
+               ret = for_each_fullref_in(buf.buf, fn, cb_data);
                if (ret)
                        break;
                strbuf_setlen(&buf, namespace_len);
@@ -1655,7 +1648,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;
@@ -1665,7 +1659,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);
@@ -1673,30 +1668,33 @@ 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);
+                                          type, failure_errno);
 }
 
-/* This function needs to return a meaningful errno on failure */
-const char *refs_resolve_ref_unsafe(struct ref_store *refs,
+const char *refs_werrres_ref_unsafe(struct ref_store *refs,
                                    const char *refname,
                                    int resolve_flags,
-                                   struct object_id *oid, int *flags)
+                                   struct object_id *oid,
+                                   int *flags, int *failure_errno)
 {
        static struct strbuf sb_refname = STRBUF_INIT;
        struct object_id unused_oid;
        int unused_flags;
        int symref_count;
 
+       assert(failure_errno);
+
        if (!oid)
                oid = &unused_oid;
        if (!flags)
@@ -1707,7 +1705,7 @@ 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;
+                       *failure_errno = EINVAL;
                        return NULL;
                }
 
@@ -1725,9 +1723,11 @@ 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;
 
-               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;
+                       if (errno)
+                               *failure_errno = errno;
 
                        /* In reading mode, refs must eventually resolve */
                        if (resolve_flags & RESOLVE_REF_READING)
@@ -1738,9 +1738,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);
@@ -1767,7 +1767,7 @@ 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;
+                               *failure_errno = EINVAL;
                                return NULL;
                        }
 
@@ -1775,10 +1775,23 @@ const char *refs_resolve_ref_unsafe(struct ref_store *refs,
                }
        }
 
-       errno = ELOOP;
+       *failure_errno = ELOOP;
        return NULL;
 }
 
+const char *refs_resolve_ref_unsafe(struct ref_store *refs, const char *refname,
+                                   int resolve_flags, struct object_id *oid,
+                                   int *flags)
+{
+       int failure_errno = 0;
+       const char *refn;
+       refn = refs_werrres_ref_unsafe(refs, refname, resolve_flags,
+                                      oid, flags, &failure_errno);
+       if (!refn)
+               errno = failure_errno;
+       return refn;
+}
+
 /* backend functions */
 int refs_init_db(struct strbuf *err)
 {
@@ -2229,6 +2242,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);
 
@@ -2240,7 +2260,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;
@@ -2372,19 +2393,19 @@ int delete_reflog(const char *refname)
 }
 
 int refs_reflog_expire(struct ref_store *refs,
-                      const char *refname, const struct object_id *oid,
+                      const char *refname,
                       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)
 {
-       return refs->be->reflog_expire(refs, refname, oid, flags,
+       return refs->be->reflog_expire(refs, refname, flags,
                                       prepare_fn, should_prune_fn,
                                       cleanup_fn, policy_cb_data);
 }
 
-int reflog_expire(const char *refname, const struct object_id *oid,
+int reflog_expire(const char *refname,
                  unsigned int flags,
                  reflog_expiry_prepare_fn prepare_fn,
                  reflog_expiry_should_prune_fn should_prune_fn,
@@ -2392,7 +2413,7 @@ int reflog_expire(const char *refname, const struct object_id *oid,
                  void *policy_cb_data)
 {
        return refs_reflog_expire(get_main_ref_store(the_repository),
-                                 refname, oid, flags,
+                                 refname, flags,
                                  prepare_fn, should_prune_fn,
                                  cleanup_fn, policy_cb_data);
 }