Merge branch 'ml/encode-header-refactor'

* ml/encode-header-refactor:
  move encode_in_pack_object_header() to a better place
  refactor duplicated encode_header in pack-objects and fast-import
This commit is contained in:
Junio C Hamano 2010-03-02 12:44:11 -08:00
commit 77b30bcecd
4 changed files with 33 additions and 55 deletions

View File

@ -154,33 +154,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)
return stream.total_out; 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",
* and the high bit is "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
{
int n = 1;
unsigned char c;
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
die("bad type %d", type);
c = (type << 4) | (size & 15);
size >>= 4;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
n++;
}
*hdr = c;
return n;
}
/* /*
* we are going to reuse the existing object data as is. make * we are going to reuse the existing object data as is. make
* sure it is not corrupt. * sure it is not corrupt.
@ -321,7 +294,7 @@ static unsigned long write_object(struct sha1file *f,
* The object header is a byte of 'type' followed by zero or * The object header is a byte of 'type' followed by zero or
* more bytes of length. * more bytes of length.
*/ */
hdrlen = encode_header(type, size, header); hdrlen = encode_in_pack_object_header(type, size, header);
if (type == OBJ_OFS_DELTA) { if (type == OBJ_OFS_DELTA) {
/* /*
@ -372,7 +345,7 @@ static unsigned long write_object(struct sha1file *f,
if (entry->delta) if (entry->delta)
type = (allow_ofs_delta && entry->delta->idx.offset) ? type = (allow_ofs_delta && entry->delta->idx.offset) ?
OBJ_OFS_DELTA : OBJ_REF_DELTA; OBJ_OFS_DELTA : OBJ_REF_DELTA;
hdrlen = encode_header(type, entry->size, header); hdrlen = encode_in_pack_object_header(type, entry->size, header);
offset = entry->in_pack_offset; offset = entry->in_pack_offset;
revidx = find_pack_revindex(p, offset); revidx = find_pack_revindex(p, offset);

View File

@ -980,29 +980,6 @@ static void cycle_packfile(void)
start_packfile(); start_packfile();
} }
static size_t encode_header(
enum object_type type,
uintmax_t size,
unsigned char *hdr)
{
int n = 1;
unsigned char c;
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
die("bad type %d", type);
c = (type << 4) | (size & 15);
size >>= 4;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
n++;
}
*hdr = c;
return n;
}
static int store_object( static int store_object(
enum object_type type, enum object_type type,
struct strbuf *dat, struct strbuf *dat,
@ -1103,7 +1080,7 @@ static int store_object(
delta_count_by_type[type]++; delta_count_by_type[type]++;
e->depth = last->depth + 1; e->depth = last->depth + 1;
hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr); hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
sha1write(pack_file, hdr, hdrlen); sha1write(pack_file, hdr, hdrlen);
pack_size += hdrlen; pack_size += hdrlen;
@ -1114,7 +1091,7 @@ static int store_object(
pack_size += sizeof(hdr) - pos; pack_size += sizeof(hdr) - pos;
} else { } else {
e->depth = 0; e->depth = 0;
hdrlen = encode_header(type, dat->len, hdr); hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
sha1write(pack_file, hdr, hdrlen); sha1write(pack_file, hdr, hdrlen);
pack_size += hdrlen; pack_size += hdrlen;
} }
@ -1188,7 +1165,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
memset(&s, 0, sizeof(s)); memset(&s, 0, sizeof(s));
deflateInit(&s, pack_compression_level); deflateInit(&s, pack_compression_level);
hdrlen = encode_header(OBJ_BLOB, len, out_buf); hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
if (out_sz <= hdrlen) if (out_sz <= hdrlen)
die("impossibly large object header"); die("impossibly large object header");

View File

@ -253,3 +253,30 @@ char *index_pack_lockfile(int ip_out)
} }
return NULL; return NULL;
} }
/*
* The per-object header is a pretty dense thing, which is
* - first byte: low four bits are "size", then three bits of "type",
* and the high bit is "size continues".
* - each byte afterwards: low seven bits are size continuation,
* with the high bit being "size continues"
*/
int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
{
int n = 1;
unsigned char c;
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
die("bad type %d", type);
c = (type << 4) | (size & 15);
size >>= 4;
while (size) {
*hdr++ = c | 0x80;
c = size & 0x7f;
size >>= 7;
n++;
}
*hdr = c;
return n;
}

1
pack.h
View File

@ -60,6 +60,7 @@ extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off
extern int verify_pack(struct packed_git *); extern int verify_pack(struct packed_git *);
extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t); extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
extern char *index_pack_lockfile(int fd); extern char *index_pack_lockfile(int fd);
extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned char *);
#define PH_ERROR_EOF (-1) #define PH_ERROR_EOF (-1)
#define PH_ERROR_PACK_SIGNATURE (-2) #define PH_ERROR_PACK_SIGNATURE (-2)