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 <pclouds@gmail.com> Acked-by: Nicolas Pitre <nico@fluxnic.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Feb 1, 2012 at 20:48 UTC
95099731bf2c79ccf5870655e36caa4215f0ced0
1 file changed
+35
-26
sha1_file.c
+35
-26
@@ -2010,11 +2010,44 @@ int is_pack_valid(struct packed_git *p)
2010
return !open_packed_git(p);
2011
}
2012
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
+
2047
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
2048
{
2049
static struct packed_git *last_found = (void *)1;
2050
struct packed_git *p;
2017
- off_t offset;
2051
2052
prepare_packed_git();
2053
if (!packed_git)
@@ -2022,35 +2055,11 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
2055
p = (last_found == (void *)1) ? packed_git : last_found;
2056
2057
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)) {
2059
last_found = p;
2060
return 1;
2061
}
2062
2053
- next:
2063
if (p == last_found)
2064
p = packed_git;
2065
else