@matheusbsbs / matheus-bagliano / commits / fb956c1f44
Fork 0

write_idx_file: need_large_offset() helper function

Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Feb 25, 2011 at 16:54 UTC fb956c1f44b5c99273eb9e3850717c8b489ff78f
1 file changed +19 -10
pack-write.c
+19 -10
@@ -16,6 +16,11 @@ static int sha1_compare(const void *_a, const void *_b)
16 return hashcmp(a->sha1, b->sha1);
17 }
18
19 +static int need_large_offset(off_t offset, const struct pack_idx_option *opts)
20 +{
21 + return (offset >> 31) || (opts->off32_limit < offset);
22 +}
23 +
24 /*
25 * On entry *sha1 contains the pack content SHA1 hash, on exit it is
26 * the SHA1 hash of sorted object names. The objects array passed in
@@ -65,7 +70,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
70 }
71
72 /* if last object's offset is >= 2^31 we should use index V2 */
68 - index_version = (last_obj_offset >> 31) ? 2 : opts->version;
73 + index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version;
74
75 /* index versions 2 and above need a header */
76 if (index_version >= 2) {
@@ -125,8 +130,11 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
130 list = sorted_by_sha;
131 for (i = 0; i < nr_objects; i++) {
132 struct pack_idx_entry *obj = *list++;
128 - uint32_t offset = (obj->offset <= opts->off32_limit) ?
129 - obj->offset : (0x80000000 | nr_large_offset++);
133 + uint32_t offset;
134 +
135 + offset = (need_large_offset(obj->offset, opts)
136 + ? (0x80000000 | nr_large_offset++)
137 + : obj->offset);
138 offset = htonl(offset);
139 sha1write(f, &offset, 4);
140 }
@@ -136,13 +144,14 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
144 while (nr_large_offset) {
145 struct pack_idx_entry *obj = *list++;
146 uint64_t offset = obj->offset;
139 - if (offset > opts->off32_limit) {
140 - uint32_t split[2];
141 - split[0] = htonl(offset >> 32);
142 - split[1] = htonl(offset & 0xffffffff);
143 - sha1write(f, split, 8);
144 - nr_large_offset--;
145 - }
147 + uint32_t split[2];
148 +
149 + if (!need_large_offset(offset, opts))
150 + continue;
151 + split[0] = htonl(offset >> 32);
152 + split[1] = htonl(offset & 0xffffffff);
153 + sha1write(f, split, 8);
154 + nr_large_offset--;
155 }
156 }
157