Skip to content

Commit 5df823f

Browse files
committed
refdb_fs: load packed-refs only when necessary
When checking if a ref exists, we load the packed-refs file and then return true if the ref is found either loose or in the packed-refs cache. This is subject to the same race described in the previous commit, where a simultaneous pack-refs may migrate a ref from loose to packed and cause us to see neither. We can fix it by making sure we load packed-refs only after checking the loose store. As a bonus, this is a minor optimization: we'll avoid even looking at the packed-refs file if we see a loose version.
1 parent 83d4060 commit 5df823f

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

‎src/refdb_fs.c‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,17 @@ static int refdb_fs_backend__exists(
333333

334334
assert(backend);
335335

336-
if ((error = packed_reload(backend)) < 0 ||
337-
(error = git_buf_joinpath(&ref_path, backend->gitpath, ref_name)) < 0)
336+
if ((error = git_buf_joinpath(&ref_path, backend->gitpath, ref_name)) < 0)
338337
return error;
338+
*exists = git_path_isfile(ref_path.ptr);
339+
git_buf_dispose(&ref_path);
339340

340-
*exists = git_path_isfile(ref_path.ptr) ||
341-
(git_sortedcache_lookup(backend->refcache, ref_name) != NULL);
341+
if (!*exists) {
342+
if ((error = packed_reload(backend)) < 0)
343+
return error;
344+
*exists = git_sortedcache_lookup(backend->refcache, ref_name) != NULL;
345+
}
342346

343-
git_buf_dispose(&ref_path);
344347
return 0;
345348
}
346349

0 commit comments

Comments
 (0)