Skip to content

Commit a255195

Browse files
jonathantanmygitster
authored andcommitted
pack: move find_pack_entry_one(), is_pack_valid()
Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9e0f45f commit a255195

File tree

4 files changed

+82
-84
lines changed

4 files changed

+82
-84
lines changed

cache.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,14 +1620,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
16201620
*/
16211621
extern int odb_pack_keep(const char *name);
16221622

1623-
/*
1624-
* If the object named sha1 is present in the specified packfile,
1625-
* return its offset within the packfile; otherwise, return 0.
1626-
*/
1627-
extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
1628-
1629-
extern int is_pack_valid(struct packed_git *);
1630-
16311623
/*
16321624
* Iterate over the files in the loose-object parts of the object
16331625
* directory "path", triggering the following callbacks:

packfile.c

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "delta.h"
88
#include "list.h"
99
#include "streaming.h"
10+
#include "sha1-lookup.h"
1011

1112
char *odb_pack_name(struct strbuf *buf,
1213
const unsigned char *sha1,
@@ -509,7 +510,7 @@ static int open_packed_git_1(struct packed_git *p)
509510
return 0;
510511
}
511512

512-
int open_packed_git(struct packed_git *p)
513+
static int open_packed_git(struct packed_git *p)
513514
{
514515
if (!open_packed_git_1(p))
515516
return 0;
@@ -1700,3 +1701,76 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
17001701
ntohl(*((uint32_t *)(index + 4)));
17011702
}
17021703
}
1704+
1705+
off_t find_pack_entry_one(const unsigned char *sha1,
1706+
struct packed_git *p)
1707+
{
1708+
const uint32_t *level1_ofs = p->index_data;
1709+
const unsigned char *index = p->index_data;
1710+
unsigned hi, lo, stride;
1711+
static int debug_lookup = -1;
1712+
1713+
if (debug_lookup < 0)
1714+
debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
1715+
1716+
if (!index) {
1717+
if (open_pack_index(p))
1718+
return 0;
1719+
level1_ofs = p->index_data;
1720+
index = p->index_data;
1721+
}
1722+
if (p->index_version > 1) {
1723+
level1_ofs += 2;
1724+
index += 8;
1725+
}
1726+
index += 4 * 256;
1727+
hi = ntohl(level1_ofs[*sha1]);
1728+
lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1729+
if (p->index_version > 1) {
1730+
stride = 20;
1731+
} else {
1732+
stride = 24;
1733+
index += 4;
1734+
}
1735+
1736+
if (debug_lookup)
1737+
printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
1738+
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
1739+
1740+
while (lo < hi) {
1741+
unsigned mi = (lo + hi) / 2;
1742+
int cmp = hashcmp(index + mi * stride, sha1);
1743+
1744+
if (debug_lookup)
1745+
printf("lo %u hi %u rg %u mi %u\n",
1746+
lo, hi, hi - lo, mi);
1747+
if (!cmp)
1748+
return nth_packed_object_offset(p, mi);
1749+
if (cmp > 0)
1750+
hi = mi;
1751+
else
1752+
lo = mi+1;
1753+
}
1754+
return 0;
1755+
}
1756+
1757+
int is_pack_valid(struct packed_git *p)
1758+
{
1759+
/* An already open pack is known to be valid. */
1760+
if (p->pack_fd != -1)
1761+
return 1;
1762+
1763+
/* If the pack has one window completely covering the
1764+
* file size, the pack is known to be valid even if
1765+
* the descriptor is not currently open.
1766+
*/
1767+
if (p->windows) {
1768+
struct pack_window *w = p->windows;
1769+
1770+
if (!w->offset && w->len == p->pack_size)
1771+
return 1;
1772+
}
1773+
1774+
/* Force the pack to open to prove its valid. */
1775+
return !open_packed_git(p);
1776+
}

packfile.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,20 @@ extern const struct object_id *nth_packed_object_oid(struct object_id *, struct
9393
*/
9494
extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
9595

96+
/*
97+
* If the object named sha1 is present in the specified packfile,
98+
* return its offset within the packfile; otherwise, return 0.
99+
*/
100+
extern off_t find_pack_entry_one(const unsigned char *sha1, struct packed_git *);
101+
102+
extern int is_pack_valid(struct packed_git *);
96103
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
97104
extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
98105
extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
99106
extern int unpack_object_header(struct packed_git *, struct pack_window **, off_t *, unsigned long *);
100107

101108
extern void release_pack_memory(size_t);
102109

103-
extern int open_packed_git(struct packed_git *p);
104-
105110
/* global flag to enable extra checks when accessing packed objects */
106111
extern int do_check_packed_object_crc;
107112

sha1_file.c

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,79 +1075,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
10751075
return parse_sha1_header_extended(hdr, &oi, 0);
10761076
}
10771077

1078-
off_t find_pack_entry_one(const unsigned char *sha1,
1079-
struct packed_git *p)
1080-
{
1081-
const uint32_t *level1_ofs = p->index_data;
1082-
const unsigned char *index = p->index_data;
1083-
unsigned hi, lo, stride;
1084-
static int debug_lookup = -1;
1085-
1086-
if (debug_lookup < 0)
1087-
debug_lookup = !!getenv("GIT_DEBUG_LOOKUP");
1088-
1089-
if (!index) {
1090-
if (open_pack_index(p))
1091-
return 0;
1092-
level1_ofs = p->index_data;
1093-
index = p->index_data;
1094-
}
1095-
if (p->index_version > 1) {
1096-
level1_ofs += 2;
1097-
index += 8;
1098-
}
1099-
index += 4 * 256;
1100-
hi = ntohl(level1_ofs[*sha1]);
1101-
lo = ((*sha1 == 0x0) ? 0 : ntohl(level1_ofs[*sha1 - 1]));
1102-
if (p->index_version > 1) {
1103-
stride = 20;
1104-
} else {
1105-
stride = 24;
1106-
index += 4;
1107-
}
1108-
1109-
if (debug_lookup)
1110-
printf("%02x%02x%02x... lo %u hi %u nr %"PRIu32"\n",
1111-
sha1[0], sha1[1], sha1[2], lo, hi, p->num_objects);
1112-
1113-
while (lo < hi) {
1114-
unsigned mi = (lo + hi) / 2;
1115-
int cmp = hashcmp(index + mi * stride, sha1);
1116-
1117-
if (debug_lookup)
1118-
printf("lo %u hi %u rg %u mi %u\n",
1119-
lo, hi, hi - lo, mi);
1120-
if (!cmp)
1121-
return nth_packed_object_offset(p, mi);
1122-
if (cmp > 0)
1123-
hi = mi;
1124-
else
1125-
lo = mi+1;
1126-
}
1127-
return 0;
1128-
}
1129-
1130-
int is_pack_valid(struct packed_git *p)
1131-
{
1132-
/* An already open pack is known to be valid. */
1133-
if (p->pack_fd != -1)
1134-
return 1;
1135-
1136-
/* If the pack has one window completely covering the
1137-
* file size, the pack is known to be valid even if
1138-
* the descriptor is not currently open.
1139-
*/
1140-
if (p->windows) {
1141-
struct pack_window *w = p->windows;
1142-
1143-
if (!w->offset && w->len == p->pack_size)
1144-
return 1;
1145-
}
1146-
1147-
/* Force the pack to open to prove its valid. */
1148-
return !open_packed_git(p);
1149-
}
1150-
11511078
static int fill_pack_entry(const unsigned char *sha1,
11521079
struct pack_entry *e,
11531080
struct packed_git *p)

0 commit comments

Comments
 (0)