commit_packed_refs(): use reference iteration

Use reference iteration rather than do_for_each_entry_in_dir() in the definition of commit_packed_refs(). Note that an internal consistency check that was previously done in `write_packed_entry_fn()` is not there anymore. This is actually an improvement: The old error message was emitted when there is an entry in the packed-ref cache that is not `REF_KNOWS_PEELED`, and when we attempted to peel the reference, the result was `PEEL_INVALID`, `PEEL_IS_SYMREF`, or `PEEL_BROKEN`. Since a packed ref cannot be a symref, `PEEL_IS_SYMREF` and `PEEL_BROKEN` can be ruled out. So we're left with `PEEL_INVALID`. An entry without `REF_KNOWS_PEELED` can get into the packed-refs cache in the following two ways: * The reference was read from a `packed-refs` file that didn't have the `fully-peeled` attribute. In that case, we *don't want* to emit an error, because the broken value is presumably a stale value of the reference that is now masked by a loose version of the same reference (which we just don't happen to be packing this time). This is a perfectly legitimate situation and doesn't indicate that the repository is corrupt. The old code incorrectly emits an error message in this case. (It was probably never reported as a bug because this scenario is rare.) * The reference was a loose reference that was just added to the packed ref cache by `files_packed_refs()` via `pack_if_possible_fn()` in preparation for being packed. The latter function refuses to pack a reference for which `entry_resolves_to_object()` returns false, and otherwise calls `peel_entry()` itself and checks the return value. So an entry added this way should always have `REF_KNOWS_PEELED` and shouldn't trigger the error message in either the old code or the new. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Apr 16, 2017 at 08:41 UTC 1710fbafb699a651243ea1af9d21a8b571a66965
1 file changed +17 -21
refs/files-backend.c
+17 -21
@@ -1290,30 +1290,15 @@ static struct ref_lock *lock_ref_sha1_basic(struct files_ref_store *refs,
1290 * Write an entry to the packed-refs file for the specified refname.
1291 * If peeled is non-NULL, write it as the entry's peeled value.
1292 */
1293 -static void write_packed_entry(FILE *fh, char *refname, unsigned char *sha1,
1294 - unsigned char *peeled)
1293 +static void write_packed_entry(FILE *fh, const char *refname,
1294 + const unsigned char *sha1,
1295 + const unsigned char *peeled)
1296 {
1297 fprintf_or_die(fh, "%s %s\n", sha1_to_hex(sha1), refname);
1298 if (peeled)
1299 fprintf_or_die(fh, "^%s\n", sha1_to_hex(peeled));
1300 }
1301
1301 -/*
1302 - * An each_ref_entry_fn that writes the entry to a packed-refs file.
1303 - */
1304 -static int write_packed_entry_fn(struct ref_entry *entry, void *cb_data)
1305 -{
1306 - enum peel_status peel_status = peel_entry(entry, 0);
1307 -
1308 - if (peel_status != PEEL_PEELED && peel_status != PEEL_NON_TAG)
1309 - error("internal error: %s is not a valid packed reference!",
1310 - entry->name);
1311 - write_packed_entry(cb_data, entry->name, entry->u.value.oid.hash,
1312 - peel_status == PEEL_PEELED ?
1313 - entry->u.value.peeled.hash : NULL);
1314 - return 0;
1315 -}
1316 -
1302 /*
1303 * Lock the packed-refs file for writing. Flags is passed to
1304 * hold_lock_file_for_update(). Return 0 on success. On errors, set
@@ -1359,9 +1344,10 @@ static int commit_packed_refs(struct files_ref_store *refs)
1344 {
1345 struct packed_ref_cache *packed_ref_cache =
1346 get_packed_ref_cache(refs);
1362 - int error = 0;
1347 + int ok, error = 0;
1348 int save_errno = 0;
1349 FILE *out;
1350 + struct ref_iterator *iter;
1351
1352 files_assert_main_repository(refs, "commit_packed_refs");
1353
@@ -1373,8 +1359,18 @@ static int commit_packed_refs(struct files_ref_store *refs)
1359 die_errno("unable to fdopen packed-refs descriptor");
1360
1361 fprintf_or_die(out, "%s", PACKED_REFS_HEADER);
1376 - do_for_each_entry_in_dir(get_packed_ref_dir(packed_ref_cache),
1377 - write_packed_entry_fn, out);
1362 +
1363 + iter = cache_ref_iterator_begin(packed_ref_cache->cache, NULL, 0);
1364 + while ((ok = ref_iterator_advance(iter)) == ITER_OK) {
1365 + struct object_id peeled;
1366 + int peel_error = ref_iterator_peel(iter, &peeled);
1367 +
1368 + write_packed_entry(out, iter->refname, iter->oid->hash,
1369 + peel_error ? NULL : peeled.hash);
1370 + }
1371 +
1372 + if (ok != ITER_DONE)
1373 + die("error while iterating over references");
1374
1375 if (commit_lock_file(packed_ref_cache->lock)) {
1376 save_errno = errno;