@matheusbsbs / matheus-bagliano / commits / 93821bd97a
Fork 0

sha1_file: add the ability to parse objects in "pack file format"

The pack-file format is slightly different from the traditional git object format, in that it has a much denser binary header encoding. The traditional format uses an ASCII string with type and length information, which is somewhat wasteful. A new object format starts with uncompressed binary header followed by compressed payload -- this will allow us later to copy the payload straight to packfiles. Obviously they cannot be read by older versions of git, so for now new object files are created with the traditional format. core.legacyheaders configuration item, when set to false makes the code write in new format for people to experiment with. Signed-off-by: Linus Torvalds <torvalds@osdl.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Linus Torvalds committed Jul 11, 2006 at 12:48 UTC 93821bd97aa516b3b557fc5d1b32fd3431536e44
5 files changed +111 -8
Documentation/config.txt
+6
@@ -97,6 +97,12 @@ core.compression::
97 compression, and 1..9 are various speed/size tradeoffs, 9 being
98 slowest.
99
100 +core.legacyheaders::
101 + A boolean which enables the legacy object header format in case
102 + you want to interoperate with old clients accessing the object
103 + database directly (where the "http://" and "rsync://" protocols
104 + count as direct access).
105 +
106 alias.*::
107 Command aliases for the gitlink:git[1] command wrapper - e.g.
108 after defining "alias.last = cat-file commit HEAD", the invocation
cache.h
+1
@@ -176,6 +176,7 @@ extern int commit_lock_file(struct lock_file *);
176 extern void rollback_lock_file(struct lock_file *);
177
178 /* Environment bits from configuration mechanism */
179 +extern int use_legacy_headers;
180 extern int trust_executable_bit;
181 extern int assume_unchanged;
182 extern int prefer_symlink_refs;
config.c
+5
@@ -279,6 +279,11 @@ int git_default_config(const char *var, const char *value)
279 return 0;
280 }
281
282 + if (!strcmp(var, "core.legacyheaders")) {
283 + use_legacy_headers = git_config_bool(var, value);
284 + return 0;
285 + }
286 +
287 if (!strcmp(var, "core.compression")) {
288 int level = git_config_int(var, value);
289 if (level == -1)
environment.c
+1
@@ -11,6 +11,7 @@
11
12 char git_default_email[MAX_GITNAME];
13 char git_default_name[MAX_GITNAME];
14 +int use_legacy_headers = 1;
15 int trust_executable_bit = 1;
16 int assume_unchanged = 0;
17 int prefer_symlink_refs = 0;
sha1_file.c
+98 -8
@@ -684,26 +684,74 @@ static void *map_sha1_file_internal(const unsigned char *sha1,
684 return map;
685 }
686
687 -static int unpack_sha1_header(z_stream *stream, void *map, unsigned long mapsize, void *buffer, unsigned long size)
687 +static int unpack_sha1_header(z_stream *stream, unsigned char *map, unsigned long mapsize, void *buffer, unsigned long bufsiz)
688 {
689 + unsigned char c;
690 + unsigned int word, bits;
691 + unsigned long size;
692 + static const char *typename[8] = {
693 + NULL, /* OBJ_EXT */
694 + "commit", "tree", "blob", "tag",
695 + NULL, NULL, NULL
696 + };
697 + const char *type;
698 +
699 /* Get the data stream */
700 memset(stream, 0, sizeof(*stream));
701 stream->next_in = map;
702 stream->avail_in = mapsize;
703 stream->next_out = buffer;
694 - stream->avail_out = size;
704 + stream->avail_out = bufsiz;
705 +
706 + /*
707 + * Is it a zlib-compressed buffer? If so, the first byte
708 + * must be 0x78 (15-bit window size, deflated), and the
709 + * first 16-bit word is evenly divisible by 31
710 + */
711 + word = (map[0] << 8) + map[1];
712 + if (map[0] == 0x78 && !(word % 31)) {
713 + inflateInit(stream);
714 + return inflate(stream, 0);
715 + }
716 +
717 + c = *map++;
718 + mapsize--;
719 + type = typename[(c >> 4) & 7];
720 + if (!type)
721 + return -1;
722 +
723 + bits = 4;
724 + size = c & 0xf;
725 + while ((c & 0x80)) {
726 + if (bits >= 8*sizeof(long))
727 + return -1;
728 + c = *map++;
729 + size += (c & 0x7f) << bits;
730 + bits += 7;
731 + mapsize--;
732 + }
733
734 + /* Set up the stream for the rest.. */
735 + stream->next_in = map;
736 + stream->avail_in = mapsize;
737 inflateInit(stream);
697 - return inflate(stream, 0);
738 +
739 + /* And generate the fake traditional header */
740 + stream->total_out = 1 + snprintf(buffer, bufsiz, "%s %lu", type, size);
741 + return 0;
742 }
743
744 static void *unpack_sha1_rest(z_stream *stream, void *buffer, unsigned long size)
745 {
746 int bytes = strlen(buffer) + 1;
747 unsigned char *buf = xmalloc(1+size);
748 + unsigned long n;
749
705 - memcpy(buf, (char *) buffer + bytes, stream->total_out - bytes);
706 - bytes = stream->total_out - bytes;
750 + n = stream->total_out - bytes;
751 + if (n > size)
752 + n = size;
753 + memcpy(buf, (char *) buffer + bytes, n);
754 + bytes = n;
755 if (bytes < size) {
756 stream->next_out = buf + bytes;
757 stream->avail_out = size - bytes;
@@ -1414,6 +1462,49 @@ static int write_buffer(int fd, const void *buf, size_t len)
1462 return 0;
1463 }
1464
1465 +static int write_binary_header(unsigned char *hdr, enum object_type type, unsigned long len)
1466 +{
1467 + int hdr_len;
1468 + unsigned char c;
1469 +
1470 + c = (type << 4) | (len & 15);
1471 + len >>= 4;
1472 + hdr_len = 1;
1473 + while (len) {
1474 + *hdr++ = c | 0x80;
1475 + hdr_len++;
1476 + c = (len & 0x7f);
1477 + len >>= 7;
1478 + }
1479 + *hdr = c;
1480 + return hdr_len;
1481 +}
1482 +
1483 +static void setup_object_header(z_stream *stream, const char *type, unsigned long len)
1484 +{
1485 + int obj_type, hdr;
1486 +
1487 + if (use_legacy_headers) {
1488 + while (deflate(stream, 0) == Z_OK)
1489 + /* nothing */;
1490 + return;
1491 + }
1492 + if (!strcmp(type, blob_type))
1493 + obj_type = OBJ_BLOB;
1494 + else if (!strcmp(type, tree_type))
1495 + obj_type = OBJ_TREE;
1496 + else if (!strcmp(type, commit_type))
1497 + obj_type = OBJ_COMMIT;
1498 + else if (!strcmp(type, tag_type))
1499 + obj_type = OBJ_TAG;
1500 + else
1501 + die("trying to generate bogus object of type '%s'", type);
1502 + hdr = write_binary_header(stream->next_out, obj_type, len);
1503 + stream->total_out = hdr;
1504 + stream->next_out += hdr;
1505 + stream->avail_out -= hdr;
1506 +}
1507 +
1508 int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned char *returnsha1)
1509 {
1510 int size;
@@ -1459,7 +1550,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1550 /* Set it up */
1551 memset(&stream, 0, sizeof(stream));
1552 deflateInit(&stream, zlib_compression_level);
1462 - size = deflateBound(&stream, len+hdrlen);
1553 + size = 8 + deflateBound(&stream, len+hdrlen);
1554 compressed = xmalloc(size);
1555
1556 /* Compress it */
@@ -1469,8 +1560,7 @@ int write_sha1_file(void *buf, unsigned long len, const char *type, unsigned cha
1560 /* First header.. */
1561 stream.next_in = hdr;
1562 stream.avail_in = hdrlen;
1472 - while (deflate(&stream, 0) == Z_OK)
1473 - /* nothing */;
1563 + setup_object_header(&stream, type, len);
1564
1565 /* Then the data itself.. */
1566 stream.next_in = buf;