@matheusbsbs / matheus-bagliano / commits / 1b22b6c897
Fork 0

refactor duplicated encode_header in pack-objects and fast-import

The following function is duplicated: encode_header Move this function to sha1_file.c and rename it 'encode_in_pack_object_header', as suggested by Junio C Hamano Signed-off-by: Michael Lukashov <michael.lukashov@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Lukashov committed Feb 16, 2010 at 23:42 UTC 1b22b6c897efa8aec6eeb51f72a73d507d5e336f
4 files changed +33 -55
builtin-pack-objects.c
+2 -29
@@ -154,33 +154,6 @@ static unsigned long do_compress(void **pptr, unsigned long size)
154 return stream.total_out;
155 }
156
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 -
157 /*
158 * we are going to reuse the existing object data as is. make
159 * sure it is not corrupt.
@@ -321,7 +294,7 @@ static unsigned long write_object(struct sha1file *f,
294 * The object header is a byte of 'type' followed by zero or
295 * more bytes of length.
296 */
324 - hdrlen = encode_header(type, size, header);
297 + hdrlen = encode_in_pack_object_header(type, size, header);
298
299 if (type == OBJ_OFS_DELTA) {
300 /*
@@ -372,7 +345,7 @@ static unsigned long write_object(struct sha1file *f,
345 if (entry->delta)
346 type = (allow_ofs_delta && entry->delta->idx.offset) ?
347 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);
349
350 offset = entry->in_pack_offset;
351 revidx = find_pack_revindex(p, offset);
cache.h
+8
@@ -911,6 +911,14 @@ extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsign
911 extern unsigned long unpack_object_header_buffer(const unsigned char *buf, unsigned long len, enum object_type *type, unsigned long *sizep);
912 extern unsigned long get_size_from_delta(struct packed_git *, struct pack_window **, off_t);
913 extern const char *packed_object_info_detail(struct packed_git *, off_t, unsigned long *, unsigned long *, unsigned int *, unsigned char *);
914 +/*
915 + * The per-object header is a pretty dense thing, which is
916 + * - first byte: low four bits are "size", then three bits of "type",
917 + * and the high bit is "size continues".
918 + * - each byte afterwards: low seven bits are size continuation,
919 + * with the high bit being "size continues"
920 + */
921 +int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr);
922
923 /* Dumb servers support */
924 extern int update_server_info(int);
fast-import.c
+3 -26
@@ -980,29 +980,6 @@ static void cycle_packfile(void)
980 start_packfile();
981 }
982
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 -
983 static int store_object(
984 enum object_type type,
985 struct strbuf *dat,
@@ -1103,7 +1080,7 @@ static int store_object(
1080 delta_count_by_type[type]++;
1081 e->depth = last->depth + 1;
1082
1106 - hdrlen = encode_header(OBJ_OFS_DELTA, deltalen, hdr);
1083 + hdrlen = encode_in_pack_object_header(OBJ_OFS_DELTA, deltalen, hdr);
1084 sha1write(pack_file, hdr, hdrlen);
1085 pack_size += hdrlen;
1086
@@ -1114,7 +1091,7 @@ static int store_object(
1091 pack_size += sizeof(hdr) - pos;
1092 } else {
1093 e->depth = 0;
1117 - hdrlen = encode_header(type, dat->len, hdr);
1094 + hdrlen = encode_in_pack_object_header(type, dat->len, hdr);
1095 sha1write(pack_file, hdr, hdrlen);
1096 pack_size += hdrlen;
1097 }
@@ -1188,7 +1165,7 @@ static void stream_blob(uintmax_t len, unsigned char *sha1out, uintmax_t mark)
1165 memset(&s, 0, sizeof(s));
1166 deflateInit(&s, pack_compression_level);
1167
1191 - hdrlen = encode_header(OBJ_BLOB, len, out_buf);
1168 + hdrlen = encode_in_pack_object_header(OBJ_BLOB, len, out_buf);
1169 if (out_sz <= hdrlen)
1170 die("impossibly large object header");
1171
sha1_file.c
+20
@@ -1475,6 +1475,26 @@ const char *packed_object_info_detail(struct packed_git *p,
1475 }
1476 }
1477
1478 +int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
1479 +{
1480 + int n = 1;
1481 + unsigned char c;
1482 +
1483 + if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
1484 + die("bad type %d", type);
1485 +
1486 + c = (type << 4) | (size & 15);
1487 + size >>= 4;
1488 + while (size) {
1489 + *hdr++ = c | 0x80;
1490 + c = size & 0x7f;
1491 + size >>= 7;
1492 + n++;
1493 + }
1494 + *hdr = c;
1495 + return n;
1496 +}
1497 +
1498 static int packed_object_info(struct packed_git *p, off_t obj_offset,
1499 unsigned long *sizep)
1500 {