Skip to content

Commit 779412b

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Acked-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e5dcd78 commit 779412b

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

packfile.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,19 +2082,31 @@ int for_each_object_in_pack(struct packed_git *p,
20822082
}
20832083

20842084
for (i = 0; i < p->num_objects; i++) {
2085-
uint32_t pos;
2085+
uint32_t index_pos;
20862086
struct object_id oid;
20872087

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+
*/
20882100
if (flags & FOR_EACH_OBJECT_PACK_ORDER)
2089-
pos = pack_pos_to_index(p, i);
2101+
index_pos = pack_pos_to_index(p, i);
20902102
else
2091-
pos = i;
2103+
index_pos = i;
20922104

2093-
if (nth_packed_object_id(&oid, p, pos) < 0)
2105+
if (nth_packed_object_id(&oid, p, index_pos) < 0)
20942106
return error("unable to get sha1 of object %u in %s",
2095-
pos, p->pack_name);
2107+
index_pos, p->pack_name);
20962108

2097-
r = cb(&oid, p, pos, data);
2109+
r = cb(&oid, p, index_pos, data);
20982110
if (r)
20992111
break;
21002112
}

0 commit comments

Comments
 (0)