Skip to content

Commit 77b30bc

Browse files
committed
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
2 parents ca97d26 + f965c52 commit 77b30bc

4 files changed

Lines changed: 33 additions & 55 deletions

File tree

builtin-pack-objects.c

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -154,33 +154,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)
154154
return stream.total_out;
155155
}
156156

157-
/*
158-
* The per-object header is a pretty dense thing, which is
159-
* - first byte: low four bits are "size", then three bits of "type",
160-
* and the high bit is "size continues".
161-
* - each byte afterwards: low seven bits are size continuation,
162-
* with the high bit being "size continues"
163-
*/
164-
static int encode_header(enum object_type type, unsigned long size, unsigned char *hdr)
165-
{
166-
int n = 1;
167-
unsigned char c;
168-
169-
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
170-
die("bad type %d", type);
171-
172-
c = (type << 4) | (size & 15);
173-
size >>= 4;
174-
while (size) {
175-
*hdr++ = c | 0x80;
176-
c = size & 0x7f;
177-
size >>= 7;
178-
n++;
179-
}
180-
*hdr = c;
181-
return n;
182-
}
183-
184157
/*
185158
* we are going to reuse the existing object data as is. make
186159
* sure it is not corrupt.
@@ -321,7 +294,7 @@ static unsigned long write_object(struct sha1file *f,
321294
* The object header is a byte of 'type' followed by zero or
322295
* more bytes of length.
323296
*/
324-
hdrlen = encode_header(type, size, header);
297+
hdrlen = encode_in_pack_object_header(type, size, header);
325298

326299
if (type == OBJ_OFS_DELTA) {
327300
/*
@@ -372,7 +345,7 @@ static unsigned long write_object(struct sha1file *f,
372345
if (entry->delta)
373346
type = (allow_ofs_delta && entry->delta->idx.offset) ?
374347
OBJ_OFS_DELTA : OBJ_REF_DELTA;
375-
hdrlen = encode_header(type, entry->size, header);
348+
hdrlen = encode_in_pack_object_header(type, entry->size, header);
376349

377350
offset = entry->in_pack_offset;
378351
revidx = find_pack_revindex(p, offset);

fast-import.c

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -980,29 +980,6 @@ static void cycle_packfile(void)
980980
start_packfile();
981981
}
982982

983-
static size_t encode_header(
984-
enum object_type type,
985-
uintmax_t size,
986-
unsigned char *hdr)
987-
{
988-
int n = 1;
989-
unsigned char c;
990-
991-
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
992-
die("bad type %d", type);
993-
994-
c = (type << 4) | (size & 15);
995-
size >>= 4;
996-
while (size) {
997-
*hdr++ = c | 0x80;
998-
c = size & 0x7f;
999-
size >>= 7;
1000-
n++;
1001-
}
1002-
*hdr = c;
1003-
return n;
1004-
}
1005-
1006983
static int store_object(
1007984
enum object_type type,
1008985
struct strbuf *dat,
@@ -1103,7 +1080,7 @@ static int store_object(
11031080
delta_count_by_type[type]++;
11041081
e->depth = last->depth + 1;
11051082

1106-
hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
1083+
hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
11071084
sha1write(pack_file, hdr, hdrlen);
11081085
pack_size += hdrlen;
11091086

@@ -1114,7 +1091,7 @@ static int store_object(
11141091
pack_size += sizeof(hdr) - pos;
11151092
} else {
11161093
e->depth = 0;
1117-
hdrlen = encode_header(type, dat->len, hdr);
1094+
hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
11181095
sha1write(pack_file, hdr, hdrlen);
11191096
pack_size += hdrlen;
11201097
}
@@ -1188,7 +1165,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
11881165
memset(&s, 0, sizeof(s));
11891166
deflateInit(&s, pack_compression_level);
11901167

1191-
hdrlen = encode_header(OBJ_BLOB, len, out_buf);
1168+
hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
11921169
if (out_sz <= hdrlen)
11931170
die("impossibly large object header");
11941171

pack-write.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,3 +253,30 @@ char *index_pack_lockfile(int ip_out)
253253
}
254254
return NULL;
255255
}
256+
257+
/*
258+
* The per-object header is a pretty dense thing, which is
259+
* - first byte: low four bits are "size", then three bits of "type",
260+
* and the high bit is "size continues".
261+
* - each byte afterwards: low seven bits are size continuation,
262+
* with the high bit being "size continues"
263+
*/
264+
int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
265+
{
266+
int n = 1;
267+
unsigned char c;
268+
269+
if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
270+
die("bad type %d", type);
271+
272+
c = (type << 4) | (size & 15);
273+
size >>= 4;
274+
while (size) {
275+
*hdr++ = c | 0x80;
276+
c = size & 0x7f;
277+
size >>= 7;
278+
n++;
279+
}
280+
*hdr = c;
281+
return n;
282+
}

pack.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ extern int check_pack_crc(struct packed_git *p, struct pack_window **w_curs, off
6060
extern int verify_pack(struct packed_git *);
6161
extern void fixup_pack_header_footer(int, unsigned char *, const char *, uint32_t, unsigned char *, off_t);
6262
extern char *index_pack_lockfile(int fd);
63+
extern int encode_in_pack_object_header(enum object_type, uintmax_t, unsigned char *);
6364

6465
#define PH_ERROR_EOF (-1)
6566
#define PH_ERROR_PACK_SIGNATURE (-2)

0 commit comments

Comments
 (0)