t/helper/test-hashmap: use custom data instead of duplicate cmp functions
[gitweb.git] / refs / files-backend.c
index dbfd03f989faab91f788aaff8c61f97324068871..0404f2c2333c0fadeb57d225deedc6f0d4afadad 100644 (file)
@@ -1,4 +1,5 @@
 #include "../cache.h"
+#include "../config.h"
 #include "../refs.h"
 #include "refs-internal.h"
 #include "ref-cache.h"
@@ -291,7 +292,7 @@ static struct packed_ref_cache *read_packed_refs(const char *packed_refs_file)
                                oidclr(&oid);
                                flag |= REF_BAD_NAME | REF_ISBROKEN;
                        }
-                       last = create_ref_entry(refname, &oid, flag, 0);
+                       last = create_ref_entry(refname, &oid, flag);
                        if (peeled == PEELED_FULLY ||
                            (peeled == PEELED_TAGS && starts_with(refname, "refs/tags/")))
                                last->flag |= REF_KNOWS_PEELED;
@@ -369,6 +370,18 @@ static void files_ref_path(struct files_ref_store *refs,
        }
 }
 
+/*
+ * Check that the packed refs cache (if any) still reflects the
+ * contents of the file. If not, clear the cache.
+ */
+static void validate_packed_ref_cache(struct files_ref_store *refs)
+{
+       if (refs->packed &&
+           !stat_validity_check(&refs->packed->validity,
+                                files_packed_refs_path(refs)))
+               clear_packed_ref_cache(refs);
+}
+
 /*
  * Get the packed_ref_cache for the specified files_ref_store,
  * creating and populating it if it hasn't been read before or if the
@@ -381,10 +394,8 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
 {
        const char *packed_refs_file = files_packed_refs_path(refs);
 
-       if (refs->packed &&
-           !is_lock_file_locked(&refs->packed_refs_lock) &&
-           !stat_validity_check(&refs->packed->validity, packed_refs_file))
-               clear_packed_ref_cache(refs);
+       if (!is_lock_file_locked(&refs->packed_refs_lock))
+               validate_packed_ref_cache(refs);
 
        if (!refs->packed)
                refs->packed = read_packed_refs(packed_refs_file);
@@ -415,8 +426,12 @@ static void add_packed_ref(struct files_ref_store *refs,
 
        if (!is_lock_file_locked(&refs->packed_refs_lock))
                die("BUG: packed refs not locked");
+
+       if (check_refname_format(refname, REFNAME_ALLOW_ONELEVEL))
+               die("Reference has invalid format: '%s'", refname);
+
        add_ref_entry(get_packed_ref_dir(packed_ref_cache),
-                     create_ref_entry(refname, oid, REF_ISPACKED, 1));
+                     create_ref_entry(refname, oid, REF_ISPACKED));
 }
 
 /*
@@ -493,7 +508,7 @@ static void loose_fill_ref_dir(struct ref_store *ref_store,
                                flag |= REF_BAD_NAME | REF_ISBROKEN;
                        }
                        add_entry_to_dir(dir,
-                                        create_ref_entry(refname.buf, &oid, flag, 0));
+                                        create_ref_entry(refname.buf, &oid, flag));
                }
                strbuf_setlen(&refname, dirnamelen);
                strbuf_setlen(&path, path_baselen);
@@ -1074,15 +1089,12 @@ static struct ref_iterator *files_ref_iterator_begin(
        struct ref_iterator *loose_iter, *packed_iter;
        struct files_ref_iterator *iter;
        struct ref_iterator *ref_iterator;
+       unsigned int required_flags = REF_STORE_READ;
 
-       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))
+               required_flags |= REF_STORE_ODB;
 
-       refs = files_downcast(ref_store,
-                             REF_STORE_READ | (ref_paranoia ? 0 : REF_STORE_ODB),
-                             "ref_iterator_begin");
+       refs = files_downcast(ref_store, required_flags, "ref_iterator_begin");
 
        iter = xcalloc(1, sizeof(*iter));
        ref_iterator = &iter->base;
@@ -1310,13 +1322,17 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
                            &refs->packed_refs_lock, files_packed_refs_path(refs),
                            flags, timeout_value) < 0)
                return -1;
+
        /*
-        * Get the current packed-refs while holding the lock. It is
-        * important that we call `get_packed_ref_cache()` before
-        * setting `packed_ref_cache->lock`, because otherwise the
-        * former will see that the file is locked and assume that the
-        * cache can't be stale.
+        * Now that we hold the `packed-refs` lock, make sure that our
+        * cache matches the current version of the file. Normally
+        * `get_packed_ref_cache()` does that for us, but that
+        * function assumes that when the file is locked, any existing
+        * cache is still valid. We've just locked the file, but it
+        * might have changed the moment *before* we locked it.
         */
+       validate_packed_ref_cache(refs);
+
        packed_ref_cache = get_packed_ref_cache(refs);
        /* Increment the reference count to prevent it from being freed: */
        acquire_packed_ref_cache(packed_ref_cache);
@@ -1544,7 +1560,7 @@ static int files_pack_refs(struct ref_store *ref_store, unsigned int flags)
                        oidcpy(&packed_entry->u.value.oid, iter->oid);
                } else {
                        packed_entry = create_ref_entry(iter->refname, iter->oid,
-                                                       REF_ISPACKED, 0);
+                                                       REF_ISPACKED);
                        add_ref_entry(packed_refs, packed_entry);
                }
                oidclr(&packed_entry->u.value.peeled);
@@ -2943,8 +2959,7 @@ static int files_transaction_prepare(struct ref_store *ref_store,
                                       head_oid.hash, &head_type);
 
        if (head_ref && !(head_type & REF_ISSYMREF)) {
-               free(head_ref);
-               head_ref = NULL;
+               FREE_AND_NULL(head_ref);
        }
 
        /*