pack: move find_pack_entry_one(), is_pack_valid()

Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jonathan Tan committed Aug 18, 2017 at 15:20 UTC a2551953b9619b285128c1e9cf35120fb7555d8f
4 files changed +82 -84
cache.h
-8
@@ -1620,14 +1620,6 @@ extern int odb_mkstemp(struct strbuf *template, const char *pattern);
1620 */
1621 extern int odb_pack_keep(const char *name);
1622
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 -
1623 /*
1624 * Iterate over the files in the loose-object parts of the object
1625 * directory "path", triggering the following callbacks:
packfile.c
+75 -1
@@ -7,6 +7,7 @@
7 #include "delta.h"
8 #include "list.h"
9 #include "streaming.h"
10 +#include "sha1-lookup.h"
11
12 char *odb_pack_name(struct strbuf *buf,
13 const unsigned char *sha1,
@@ -509,7 +510,7 @@ static int open_packed_git_1(struct packed_git *p)
510 return 0;
511 }
512
512 -int open_packed_git(struct packed_git *p)
513 +static int open_packed_git(struct packed_git *p)
514 {
515 if (!open_packed_git_1(p))
516 return 0;
@@ -1700,3 +1701,76 @@ off_t nth_packed_object_offset(const struct packed_git *p, uint32_t n)
1701 ntohl(*((uint32_t *)(index + 4)));
1702 }
1703 }
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
+7 -2
@@ -93,6 +93,13 @@ extern const struct object_id *nth_packed_object_oid(struct object_id *, struct
93 */
94 extern off_t nth_packed_object_offset(const struct packed_git *, uint32_t n);
95
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 *);
103 extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
104 extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
105 extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
@@ -100,8 +107,6 @@ extern int unpack_object_header(struct packed_git *, struct pack_window **, off_
107
108 extern void release_pack_memory(size_t);
109
103 -extern int open_packed_git(struct packed_git *p);
104 -
110 /* global flag to enable extra checks when accessing packed objects */
111 extern int do_check_packed_object_crc;
112
sha1_file.c
-73
@@ -1075,79 +1075,6 @@ int parse_sha1_header(const char *hdr, unsigned long *sizep)
1075 return parse_sha1_header_extended(hdr, &oi, 0);
1076 }
1077
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 -
1078 static int fill_pack_entry(const unsigned char *sha1,
1079 struct pack_entry *e,
1080 struct packed_git *p)