@matheusbsbs / matheus-bagliano / commits / 2c5ef82463
Fork 0

pack-objects: clean up write_object() a bit

... for improved readability. No functional changes. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nicolas Pitre committed May 2, 2008 at 15:11 UTC 2c5ef824637cffa574bdce7149791a9cb520595e
1 file changed +33 -31
builtin-pack-objects.c
+33 -31
@@ -226,41 +226,43 @@ static unsigned long write_object(struct sha1file *f,
226 struct object_entry *entry,
227 off_t write_offset)
228 {
229 - unsigned long size;
229 + unsigned long size, limit;
230 void *buf;
231 - unsigned char header[10];
232 - unsigned char dheader[10];
231 + unsigned char header[10], dheader[10];
232 unsigned hdrlen;
233 off_t datalen;
235 - enum object_type obj_type;
236 - int to_reuse = 0;
237 - /* write limit if limited packsize and not first object */
238 - unsigned long limit = pack_size_limit && nr_written ?
239 - pack_size_limit - write_offset : 0;
240 - /* no if no delta */
241 - int usable_delta = !entry->delta ? 0 :
242 - /* yes if unlimited packfile */
243 - !pack_size_limit ? 1 :
244 - /* no if base written to previous pack */
245 - entry->delta->idx.offset == (off_t)-1 ? 0 :
246 - /* otherwise double-check written to this
247 - * pack, like we do below
248 - */
249 - entry->delta->idx.offset ? 1 : 0;
234 + enum object_type type;
235 + int usable_delta, to_reuse;
236
237 if (!pack_to_stdout)
238 crc32_begin(f);
239
254 - obj_type = entry->type;
240 + type = entry->type;
241 +
242 + /* write limit if limited packsize and not first object */
243 + limit = pack_size_limit && nr_written ?
244 + pack_size_limit - write_offset : 0;
245 +
246 + if (!entry->delta)
247 + usable_delta = 0; /* no delta */
248 + else if (!pack_size_limit)
249 + usable_delta = 1; /* unlimited packfile */
250 + else if (entry->delta->idx.offset == (off_t)-1)
251 + usable_delta = 0; /* base was written to another pack */
252 + else if (entry->delta->idx.offset)
253 + usable_delta = 1; /* base already exists in this pack */
254 + else
255 + usable_delta = 0; /* base could end up in another pack */
256 +
257 if (!reuse_object)
258 to_reuse = 0; /* explicit */
259 else if (!entry->in_pack)
260 to_reuse = 0; /* can't reuse what we don't have */
259 - else if (obj_type == OBJ_REF_DELTA || obj_type == OBJ_OFS_DELTA)
261 + else if (type == OBJ_REF_DELTA || type == OBJ_OFS_DELTA)
262 /* check_object() decided it for us ... */
263 to_reuse = usable_delta;
264 /* ... but pack split may override that */
263 - else if (obj_type != entry->in_pack_type)
265 + else if (type != entry->in_pack_type)
266 to_reuse = 0; /* pack has delta which is unusable */
267 else if (entry->delta)
268 to_reuse = 0; /* we want to pack afresh */
@@ -274,19 +276,19 @@ static unsigned long write_object(struct sha1file *f,
276 unsigned long maxsize;
277 void *out;
278 if (!usable_delta) {
277 - buf = read_sha1_file(entry->idx.sha1, &obj_type, &size);
279 + buf = read_sha1_file(entry->idx.sha1, &type, &size);
280 if (!buf)
281 die("unable to read %s", sha1_to_hex(entry->idx.sha1));
282 } else if (entry->delta_data) {
283 size = entry->delta_size;
284 buf = entry->delta_data;
285 entry->delta_data = NULL;
284 - obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
286 + type = (allow_ofs_delta && entry->delta->idx.offset) ?
287 OBJ_OFS_DELTA : OBJ_REF_DELTA;
288 } else {
289 buf = get_delta(entry);
290 size = entry->delta_size;
289 - obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
291 + type = (allow_ofs_delta && entry->delta->idx.offset) ?
292 OBJ_OFS_DELTA : OBJ_REF_DELTA;
293 }
294 /* compress the data to store and put compressed length in datalen */
@@ -308,9 +310,9 @@ static unsigned long write_object(struct sha1file *f,
310 * The object header is a byte of 'type' followed by zero or
311 * more bytes of length.
312 */
311 - hdrlen = encode_header(obj_type, size, header);
313 + hdrlen = encode_header(type, size, header);
314
313 - if (obj_type == OBJ_OFS_DELTA) {
315 + if (type == OBJ_OFS_DELTA) {
316 /*
317 * Deltas with relative base contain an additional
318 * encoding of the relative offset for the delta
@@ -329,7 +331,7 @@ static unsigned long write_object(struct sha1file *f,
331 sha1write(f, header, hdrlen);
332 sha1write(f, dheader + pos, sizeof(dheader) - pos);
333 hdrlen += sizeof(dheader) - pos;
332 - } else if (obj_type == OBJ_REF_DELTA) {
334 + } else if (type == OBJ_REF_DELTA) {
335 /*
336 * Deltas with a base reference contain
337 * an additional 20 bytes for the base sha1.
@@ -361,11 +363,11 @@ static unsigned long write_object(struct sha1file *f,
363 off_t offset;
364
365 if (entry->delta) {
364 - obj_type = (allow_ofs_delta && entry->delta->idx.offset) ?
366 + type = (allow_ofs_delta && entry->delta->idx.offset) ?
367 OBJ_OFS_DELTA : OBJ_REF_DELTA;
368 reused_delta++;
369 }
368 - hdrlen = encode_header(obj_type, entry->size, header);
370 + hdrlen = encode_header(type, entry->size, header);
371 offset = entry->in_pack_offset;
372 revidx = find_pack_revindex(p, offset);
373 datalen = revidx[1].offset - offset;
@@ -374,7 +376,7 @@ static unsigned long write_object(struct sha1file *f,
376 die("bad packed object CRC for %s", sha1_to_hex(entry->idx.sha1));
377 offset += entry->in_pack_header_size;
378 datalen -= entry->in_pack_header_size;
377 - if (obj_type == OBJ_OFS_DELTA) {
379 + if (type == OBJ_OFS_DELTA) {
380 off_t ofs = entry->idx.offset - entry->delta->idx.offset;
381 unsigned pos = sizeof(dheader) - 1;
382 dheader[pos] = ofs & 127;
@@ -385,7 +387,7 @@ static unsigned long write_object(struct sha1file *f,
387 sha1write(f, header, hdrlen);
388 sha1write(f, dheader + pos, sizeof(dheader) - pos);
389 hdrlen += sizeof(dheader) - pos;
388 - } else if (obj_type == OBJ_REF_DELTA) {
390 + } else if (type == OBJ_REF_DELTA) {
391 if (limit && hdrlen + 20 + datalen + 20 >= limit)
392 return 0;
393 sha1write(f, header, hdrlen);