@@ -370,6 +370,18 @@ static void files_ref_path(struct files_ref_store *refs,
370370 }
371371}
372372
373+ /*
374+ * Check that the packed refs cache (if any) still reflects the
375+ * contents of the file. If not, clear the cache.
376+ */
377+ static void validate_packed_ref_cache (struct files_ref_store * refs )
378+ {
379+ if (refs -> packed &&
380+ !stat_validity_check (& refs -> packed -> validity ,
381+ files_packed_refs_path (refs )))
382+ clear_packed_ref_cache (refs );
383+ }
384+
373385/*
374386 * Get the packed_ref_cache for the specified files_ref_store,
375387 * creating and populating it if it hasn't been read before or if the
@@ -382,10 +394,8 @@ static struct packed_ref_cache *get_packed_ref_cache(struct files_ref_store *ref
382394{
383395 const char * packed_refs_file = files_packed_refs_path (refs );
384396
385- if (refs -> packed &&
386- !is_lock_file_locked (& refs -> packed_refs_lock ) &&
387- !stat_validity_check (& refs -> packed -> validity , packed_refs_file ))
388- clear_packed_ref_cache (refs );
397+ if (!is_lock_file_locked (& refs -> packed_refs_lock ))
398+ validate_packed_ref_cache (refs );
389399
390400 if (!refs -> packed )
391401 refs -> packed = read_packed_refs (packed_refs_file );
@@ -1312,13 +1322,17 @@ static int lock_packed_refs(struct files_ref_store *refs, int flags)
13121322 & refs -> packed_refs_lock , files_packed_refs_path (refs ),
13131323 flags , timeout_value ) < 0 )
13141324 return -1 ;
1325+
13151326 /*
1316- * Get the current packed-refs while holding the lock. It is
1317- * important that we call `get_packed_ref_cache()` before
1318- * setting `packed_ref_cache->lock`, because otherwise the
1319- * former will see that the file is locked and assume that the
1320- * cache can't be stale.
1327+ * Now that we hold the `packed-refs` lock, make sure that our
1328+ * cache matches the current version of the file. Normally
1329+ * `get_packed_ref_cache()` does that for us, but that
1330+ * function assumes that when the file is locked, any existing
1331+ * cache is still valid. We've just locked the file, but it
1332+ * might have changed the moment *before* we locked it.
13211333 */
1334+ validate_packed_ref_cache (refs );
1335+
13221336 packed_ref_cache = get_packed_ref_cache (refs );
13231337 /* Increment the reference count to prevent it from being freed: */
13241338 acquire_packed_ref_cache (packed_ref_cache );
0 commit comments