git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v3 3/6] new representation types in the packstream

From
Junio C Hamano <gitster@pobox.com>
Date
Dec 2, 2011, 00:40 UTC
Message-ID
<1322786449-25753-4-git-send-email-gitster@pobox.com>
In-Reply-To
<1322786449-25753-1-git-send-email-gitster@pobox.com>

In addition to four basic types (commit, tree, blob and tag), the pack stream can encode a few other "representation" types, such as REF_DELTA and OFS_DELTA. As we allocate 3 bits in the first byte for this purpose, we do not have much room to add new representation types in place, but we do have one value reserved for future expansion.

When encoding a new representation type, the early part of the in-pack object header is encoded as if its type is OBJ_EXT (= 5) using exactly the same way as before. That is, the lower 4-bit of the first byte is used for the lowest 4-bit of the size information, the next 3-bit has the type information, and the MSB says if the subsequent bytes encodes higher bits for the size information.

An in-pack object header that records OBJ_EXT as the type is followed by an integer in the same variable-length encoding as OFS_DELTA offset is encoded. This value is the real type of the representation minus 8 (as we do not need to use OBJ_EXT to encode types smaller than 8). Because we do not foresee very many representation types, in practice we would have a single byte with its MSB clear, to represent types 8-135.

The code does not type=8 and upwards for anything yet.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 cache.h      |    4 +++-
 pack-write.c |   23 +++++++++++++++++------
 sha1_file.c  |   11 +++++++++++
 3 files changed, 31 insertions(+), 7 deletions(-)
diff --git a/cache.h b/cache.h
index 4f20861..4a3b421 100644
--- a/cache.h
+++ b/cache.h
@@ -381,12 +381,14 @@ enum object_type {
 	OBJ_TREE = 2,
 	OBJ_BLOB = 3,
 	OBJ_TAG = 4,
-	/* 5 for future expansion */
+	OBJ_EXT = 5,
 	OBJ_OFS_DELTA = 6,
 	OBJ_REF_DELTA = 7,
 	OBJ_ANY,
 	OBJ_MAX
 };
+#define OBJ_LAST_BASE_TYPE OBJ_REF_DELTA
+#define OBJ_LAST_VALID_TYPE OBJ_REF_DELTA
 
 static inline enum object_type object_type(unsigned int mode)
 {
diff --git a/pack-write.c b/pack-write.c
index 5702cec..9309dd1 100644
--- a/pack-write.c
+++ b/pack-write.c
@@ -338,22 +338,33 @@ int encode_in_pack_varint(uintmax_t value, unsigned char *buf)
  */
 int encode_in_pack_object_header(enum object_type type, uintmax_t size, unsigned char *hdr)
 {
-	int n = 1;
+	unsigned char *hdr_base;
 	unsigned char c;
+	enum object_type header_type;
 
-	if (type < OBJ_COMMIT || type > OBJ_REF_DELTA)
+	if (type < OBJ_COMMIT || OBJ_LAST_VALID_TYPE < type)
 		die("bad type %d", type);
+	else if (OBJ_LAST_BASE_TYPE < type)
+		header_type = OBJ_EXT;
+	else
+		header_type = type;
 
-	c = (type << 4) | (size & 15);
+	c = (header_type << 4) | (size & 15);
 	size >>= 4;
+	hdr_base = hdr;
 	while (size) {
 		*hdr++ = c | 0x80;
 		c = size & 0x7f;
 		size >>= 7;
-		n++;
 	}
-	*hdr = c;
-	return n;
+	*hdr++ = c;
+	if (header_type != type) {
+		int sz;
+		type = type - (OBJ_LAST_BASE_TYPE + 1);
+		sz = encode_in_pack_varint(type, hdr);
+		hdr += sz;
+	}
+	return hdr - hdr_base;
 }
 
 struct sha1file *create_tmp_packfile(char **pack_tmp_name)
diff --git a/sha1_file.c b/sha1_file.c
index f066c2b..14902cc 100644
--- a/sha1_file.c
+++ b/sha1_file.c
@@ -1275,6 +1275,17 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
 		shift += 7;
 	}
 	*sizep = size;
+	if (*type == OBJ_EXT) {
+		const unsigned char *p = buf + used;
+		uintmax_t val = decode_in_pack_varint(&p);
+
+		if (p == buf + used && !val) {
+			error("bad extended object type");
+			return 0;
+		}
+		*type = val + (OBJ_LAST_BASE_TYPE + 1);
+		used = p - buf;
+	}
 	return used;
 }
 
-- 
1.7.8.rc4.177.g4d64
Previous: Junio C HamanoNext: Junio C Hamano
Message 12 of 15 in “Bulk Check-in”
  1. 0/5 Bulk Check-inJunio C Hamano, Dec 1, 2011
  2. 1/5 write_pack_header(): a helper functionJunio C Hamano, Dec 1, 2011
  3. 2/5 create_tmp_packfile(): a helper functionJunio C Hamano, Dec 1, 2011
  4. 3/5 finish_tmp_packfile(): a helper functionJunio C Hamano, Dec 1, 2011
  5. 4/5 csum-file: introduce sha1file_checkpointJunio C Hamano, Dec 1, 2011
  6. 5/5 bulk-checkin: replace fast-import based implementationJunio C Hamano, Dec 1, 2011
  7. Nguyen Thai Ngoc DuyDec 1, 2011
  8. Junio C HamanoDec 1, 2011
  9. 0/6 Bulk check-inJunio C Hamano, Dec 2, 2011
  10. 1/6 bulk-checkin: replace fast-import based implementationJunio C Hamano, Dec 2, 2011
  11. 2/6 varint-in-pack: refactor varint encoding/decodingJunio C Hamano, Dec 2, 2011
  12. 3/6 new representation types in the packstreamJunio C Hamano, Dec 2, 2011
  13. 4/6 bulk-checkin: allow the same data to be multiply hashedJunio C Hamano, Dec 2, 2011
  14. 5/6 bulk-checkin: support chunked-object encodingJunio C Hamano, Dec 2, 2011
  15. 6/6 chunked-object: fallback checkout codepathsJunio C Hamano, Dec 2, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.