Skip to content

Commit 9509973

Browse files
pcloudsgitster
authored andcommitted
sha1_file.c: move the core logic of find_pack_entry() into fill_pack_entry()
The new helper function implements the logic to find the offset for the object in one pack and fill a pack_entry structure. The next patch will restructure the loop and will call the helper from two places. Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Acked-by: Nicolas Pitre <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 828ea97 commit 9509973

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

sha1_file.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,47 +2010,56 @@ int is_pack_valid(struct packed_git *p)
20102010
return !open_packed_git(p);
20112011
}
20122012

2013+
static int fill_pack_entry(const unsigned char *sha1,
2014+
struct pack_entry *e,
2015+
struct packed_git *p)
2016+
{
2017+
off_t offset;
2018+
2019+
if (p->num_bad_objects) {
2020+
unsigned i;
2021+
for (i = 0; i < p->num_bad_objects; i++)
2022+
if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
2023+
return 0;
2024+
}
2025+
2026+
offset = find_pack_entry_one(sha1, p);
2027+
if (!offset)
2028+
return 0;
2029+
2030+
/*
2031+
* We are about to tell the caller where they can locate the
2032+
* requested object. We better make sure the packfile is
2033+
* still here and can be accessed before supplying that
2034+
* answer, as it may have been deleted since the index was
2035+
* loaded!
2036+
*/
2037+
if (!is_pack_valid(p)) {
2038+
warning("packfile %s cannot be accessed", p->pack_name);
2039+
return 0;
2040+
}
2041+
e->offset = offset;
2042+
e->p = p;
2043+
hashcpy(e->sha1, sha1);
2044+
return 1;
2045+
}
2046+
20132047
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
20142048
{
20152049
static struct packed_git *last_found = (void *)1;
20162050
struct packed_git *p;
2017-
off_t offset;
20182051

20192052
prepare_packed_git();
20202053
if (!packed_git)
20212054
return 0;
20222055
p = (last_found == (void *)1) ? packed_git : last_found;
20232056

20242057
do {
2025-
if (p->num_bad_objects) {
2026-
unsigned i;
2027-
for (i = 0; i < p->num_bad_objects; i++)
2028-
if (!hashcmp(sha1, p->bad_object_sha1 + 20 * i))
2029-
goto next;
2030-
}
2031-
2032-
offset = find_pack_entry_one(sha1, p);
2033-
if (offset) {
2034-
/*
2035-
* We are about to tell the caller where they can
2036-
* locate the requested object. We better make
2037-
* sure the packfile is still here and can be
2038-
* accessed before supplying that answer, as
2039-
* it may have been deleted since the index
2040-
* was loaded!
2041-
*/
2042-
if (!is_pack_valid(p)) {
2043-
warning("packfile %s cannot be accessed", p->pack_name);
2044-
goto next;
2045-
}
2046-
e->offset = offset;
2047-
e->p = p;
2048-
hashcpy(e->sha1, sha1);
2058+
if (fill_pack_entry(sha1, e, p)) {
20492059
last_found = p;
20502060
return 1;
20512061
}
20522062

2053-
next:
20542063
if (p == last_found)
20552064
p = packed_git;
20562065
else

0 commit comments

Comments
 (0)