summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 2c5ef82)
raw | patch | inline | side by side (parent: 2c5ef82)
author | Nicolas Pitre <nico@cam.org> | |
Fri, 2 May 2008 19:11:49 +0000 (15:11 -0400) | ||
committer | Junio C Hamano <gitster@pobox.com> | |
Sun, 4 May 2008 01:35:38 +0000 (18:35 -0700) |
A later patch will make use of that code too.
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin-pack-objects.c | patch | blob | history |
diff --git a/builtin-pack-objects.c b/builtin-pack-objects.c
index 69ac27788b37c89ede1dadf293c13de05fb03e82..3395acada4c98767ced062b0ad1b0415a81c3961 100644 (file)
--- a/builtin-pack-objects.c
+++ b/builtin-pack-objects.c
return delta_buf;
}
+static unsigned long do_compress(void **pptr, unsigned long size)
+{
+ z_stream stream;
+ void *in, *out;
+ unsigned long maxsize;
+
+ memset(&stream, 0, sizeof(stream));
+ deflateInit(&stream, pack_compression_level);
+ maxsize = deflateBound(&stream, size);
+
+ in = *pptr;
+ out = xmalloc(maxsize);
+ *pptr = out;
+
+ stream.next_in = in;
+ stream.avail_in = size;
+ stream.next_out = out;
+ stream.avail_out = maxsize;
+ while (deflate(&stream, Z_FINISH) == Z_OK)
+ ; /* nothing */
+ deflateEnd(&stream);
+
+ free(in);
+ return stream.total_out;
+}
+
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size", then three bits of "type",
struct object_entry *entry,
off_t write_offset)
{
- unsigned long size, limit;
+ unsigned long size, limit, datalen;
void *buf;
unsigned char header[10], dheader[10];
unsigned hdrlen;
- off_t datalen;
enum object_type type;
int usable_delta, to_reuse;
*/
if (!to_reuse) {
- z_stream stream;
- unsigned long maxsize;
- void *out;
if (!usable_delta) {
buf = read_sha1_file(entry->idx.sha1, &type, &size);
if (!buf)
type = (allow_ofs_delta && entry->delta->idx.offset) ?
OBJ_OFS_DELTA : OBJ_REF_DELTA;
}
- /* compress the data to store and put compressed length in datalen */
- memset(&stream, 0, sizeof(stream));
- deflateInit(&stream, pack_compression_level);
- maxsize = deflateBound(&stream, size);
- out = xmalloc(maxsize);
- /* Compress it */
- stream.next_in = buf;
- stream.avail_in = size;
- stream.next_out = out;
- stream.avail_out = maxsize;
- while (deflate(&stream, Z_FINISH) == Z_OK)
- /* nothing */;
- deflateEnd(&stream);
- datalen = stream.total_out;
+ datalen = do_compress(&buf, size);
/*
* The object header is a byte of 'type' followed by zero or
while (ofs >>= 7)
dheader[--pos] = 128 | (--ofs & 127);
if (limit && hdrlen + sizeof(dheader) - pos + datalen + 20 >= limit) {
- free(out);
free(buf);
return 0;
}
* an additional 20 bytes for the base sha1.
*/
if (limit && hdrlen + 20 + datalen + 20 >= limit) {
- free(out);
free(buf);
return 0;
}
hdrlen += 20;
} else {
if (limit && hdrlen + datalen + 20 >= limit) {
- free(out);
free(buf);
return 0;
}
sha1write(f, header, hdrlen);
}
- sha1write(f, out, datalen);
- free(out);
+ sha1write(f, buf, datalen);
free(buf);
}
else {