pack-write.c: extract 'write_rev_file_order'

Existing callers provide the reverse index code with an array of 'struct pack_idx_entry *'s, which is then sorted by pack order (comparing the offsets of each object within the pack). Prepare for the multi-pack index to write a .rev file by providing a way to write the reverse index without an array of pack_idx_entry (which the MIDX code does not have). Instead, callers can invoke 'write_rev_index_positions()', which takes an array of uint32_t's. The ith entry in this array specifies the ith object's (in index order) position within the pack (in pack order). Expose this new function for use in a later patch, and rewrite the existing write_rev_file() in terms of this new function. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Mar 30, 2021 at 11:04 UTC a587b5a7869e9a399d4de6edea0b3f32a3548639
2 files changed +26 -11
pack-write.c
+25 -11
@@ -201,21 +201,12 @@ static void write_rev_header(struct hashfile *f)
201 }
202
203 static void write_rev_index_positions(struct hashfile *f,
204 - struct pack_idx_entry **objects,
204 + uint32_t *pack_order,
205 uint32_t nr_objects)
206 {
207 - uint32_t *pack_order;
207 uint32_t i;
209 -
210 - ALLOC_ARRAY(pack_order, nr_objects);
211 - for (i = 0; i < nr_objects; i++)
212 - pack_order[i] = i;
213 - QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
214 -
208 for (i = 0; i < nr_objects; i++)
209 hashwrite_be32(f, pack_order[i]);
217 -
218 - free(pack_order);
210 }
211
212 static void write_rev_trailer(struct hashfile *f, const unsigned char *hash)
@@ -228,6 +219,29 @@ const char *write_rev_file(const char *rev_name,
219 uint32_t nr_objects,
220 const unsigned char *hash,
221 unsigned flags)
222 +{
223 + uint32_t *pack_order;
224 + uint32_t i;
225 + const char *ret;
226 +
227 + ALLOC_ARRAY(pack_order, nr_objects);
228 + for (i = 0; i < nr_objects; i++)
229 + pack_order[i] = i;
230 + QSORT_S(pack_order, nr_objects, pack_order_cmp, objects);
231 +
232 + ret = write_rev_file_order(rev_name, pack_order, nr_objects, hash,
233 + flags);
234 +
235 + free(pack_order);
236 +
237 + return ret;
238 +}
239 +
240 +const char *write_rev_file_order(const char *rev_name,
241 + uint32_t *pack_order,
242 + uint32_t nr_objects,
243 + const unsigned char *hash,
244 + unsigned flags)
245 {
246 struct hashfile *f;
247 int fd;
@@ -262,7 +276,7 @@ const char *write_rev_file(const char *rev_name,
276
277 write_rev_header(f);
278
265 - write_rev_index_positions(f, objects, nr_objects);
279 + write_rev_index_positions(f, pack_order, nr_objects);
280 write_rev_trailer(f, hash);
281
282 if (rev_name && adjust_shared_perm(rev_name) < 0)
pack.h
+1
@@ -94,6 +94,7 @@ struct ref;
94 void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought);
95
96 const char *write_rev_file(const char *rev_name, struct pack_idx_entry **objects, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
97 +const char *write_rev_file_order(const char *rev_name, uint32_t *pack_order, uint32_t nr_objects, const unsigned char *hash, unsigned flags);
98
99 /*
100 * The "hdr" output buffer should be at least this big, which will handle sizes