for_each_object_in_pack(): clarify pack vs index ordering

We may return objects in one of two orders: how they appear in the .idx (sorted by object id) or how they appear in the packfile itself. To further complicate matters, we have two ordering variables, "i" and "pos", and it is not clear to which order they apply. Let's clarify this by using an unambiguous name where possible, and leaving a comment for the variable that does double-duty. Signed-off-by: Jeff King <peff@peff.net> Acked-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jan 14, 2021 at 15:11 UTC 779412b9d99544ae71eefabb699a109b1638f96c
1 file changed +18 -6
packfile.c
+18 -6
@@ -2082,19 +2082,31 @@ int for_each_object_in_pack(struct packed_git *p,
2082 }
2083
2084 for (i = 0; i < p->num_objects; i++) {
2085 - uint32_t pos;
2085 + uint32_t index_pos;
2086 struct object_id oid;
2087
2088 + /*
2089 + * We are iterating "i" from 0 up to num_objects, but its
2090 + * meaning may be different, depending on the requested output
2091 + * order:
2092 + *
2093 + * - in object-name order, it is the same as the index order
2094 + * used by nth_packed_object_id(), so we can pass it
2095 + * directly
2096 + *
2097 + * - in pack-order, it is pack position, which we must
2098 + * convert to an index position in order to get the oid.
2099 + */
2100 if (flags & FOR_EACH_OBJECT_PACK_ORDER)
2089 - pos = pack_pos_to_index(p, i);
2101 + index_pos = pack_pos_to_index(p, i);
2102 else
2091 - pos = i;
2103 + index_pos = i;
2104
2093 - if (nth_packed_object_id(&oid, p, pos) < 0)
2105 + if (nth_packed_object_id(&oid, p, index_pos) < 0)
2106 return error("unable to get sha1 of object %u in %s",
2095 - pos, p->pack_name);
2107 + index_pos, p->pack_name);
2108
2097 - r = cb(&oid, p, pos, data);
2109 + r = cb(&oid, p, index_pos, data);
2110 if (r)
2111 break;
2112 }