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

[PATCH 11/11] pack-objects: support writing pack v4

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Sep 8, 2013, 15:04 UTC
Message-ID
<1378652660-6731-12-git-send-email-pclouds@gmail.com>
In-Reply-To
<1378652660-6731-1-git-send-email-pclouds@gmail.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin/pack-objects.c | 85 ++++++++++++++++++++++++++++++++++++++++++++------
 pack.h                 |  2 +-
 2 files changed, 76 insertions(+), 11 deletions(-)
diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
index daa4349..f6586a1 100644
--- a/builtin/pack-objects.c
+++ b/builtin/pack-objects.c
@@ -254,8 +254,10 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 	enum object_type type;
 	void *buf;
 	struct git_istream *st = NULL;
+	char *result = "OK";
 
-	if (!usable_delta) {
+	if (!usable_delta ||
+	    (pack_version == 4 || entry->type == OBJ_TREE)) {
 		if (entry->type == OBJ_BLOB &&
 		    entry->size > big_file_threshold &&
 		    (st = open_istream(entry->idx.sha1, &type, &size, NULL)) != NULL)
@@ -287,7 +289,37 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 
 	if (st)	/* large blob case, just assume we don't compress well */
 		datalen = size;
-	else if (entry->z_delta_size)
+	else if (pack_version == 4 && entry->type == OBJ_COMMIT) {
+		datalen = size;
+		result = pv4_encode_commit(&v4, buf, &datalen);
+		if (result) {
+			free(buf);
+			buf = result;
+			type = OBJ_PV4_COMMIT;
+		}
+	} else if (pack_version == 4 && entry->type == OBJ_TREE) {
+		datalen = size;
+		if (usable_delta) {
+			unsigned long base_size;
+			char *base_buf;
+			base_buf = read_sha1_file(entry->delta->idx.sha1, &type,
+						  &base_size);
+			if (!base_buf || type != OBJ_TREE)
+				die("unable to read %s",
+				    sha1_to_hex(entry->delta->idx.sha1));
+			result = pv4_encode_tree(&v4, buf, &datalen,
+						 base_buf, base_size,
+						 entry->delta->idx.sha1);
+			free(base_buf);
+		} else
+			result = pv4_encode_tree(&v4, buf, &datalen,
+						 NULL, 0, NULL);
+		if (result) {
+			free(buf);
+			buf = result;
+			type = OBJ_PV4_TREE;
+		}
+	} else if (entry->z_delta_size)
 		datalen = entry->z_delta_size;
 	else
 		datalen = do_compress(&buf, size);
@@ -296,7 +328,10 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 	 * The object header is a byte of 'type' followed by zero or
 	 * more bytes of length.
 	 */
-	hdrlen = encode_in_pack_object_header(type, size, header);
+	if (pack_version < 4)
+		hdrlen = encode_in_pack_object_header(type, size, header);
+	else
+		hdrlen = pv4_encode_in_pack_object_header(type, size, header);
 
 	if (type == OBJ_OFS_DELTA) {
 		/*
@@ -318,7 +353,7 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 		sha1write(f, header, hdrlen);
 		sha1write(f, dheader + pos, sizeof(dheader) - pos);
 		hdrlen += sizeof(dheader) - pos;
-	} else if (type == OBJ_REF_DELTA) {
+	} else if (type == OBJ_REF_DELTA && pack_version < 4) {
 		/*
 		 * Deltas with a base reference contain
 		 * an additional 20 bytes for the base sha1.
@@ -332,6 +367,10 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 		sha1write(f, header, hdrlen);
 		sha1write(f, entry->delta->idx.sha1, 20);
 		hdrlen += 20;
+	} else if (type == OBJ_REF_DELTA && pack_version == 4) {
+		hdrlen += encode_sha1ref(&v4, entry->delta->idx.sha1,
+					header + hdrlen);
+		sha1write(f, header, hdrlen);
 	} else {
 		if (limit && hdrlen + datalen + 20 >= limit) {
 			if (st)
@@ -341,14 +380,26 @@ static unsigned long write_no_reuse_object(struct sha1file *f, struct object_ent
 		}
 		sha1write(f, header, hdrlen);
 	}
+
 	if (st) {
 		datalen = write_large_blob_data(st, f, entry->idx.sha1);
 		close_istream(st);
-	} else {
-		sha1write(f, buf, datalen);
-		free(buf);
+		return hdrlen + datalen;
 	}
 
+	if (!result) {
+		warning(_("can't convert %s object %s"),
+			typename(entry->type),
+			sha1_to_hex(entry->idx.sha1));
+		free(buf);
+		buf = read_sha1_file(entry->idx.sha1, &type, &size);
+		if (!buf)
+			die(_("unable to read %s"),
+			    sha1_to_hex(entry->idx.sha1));
+		datalen = do_compress(&buf, size);
+	}
+	sha1write(f, buf, datalen);
+	free(buf);
 	return hdrlen + datalen;
 }
 
@@ -368,7 +419,10 @@ static unsigned long write_reuse_object(struct sha1file *f, struct object_entry
 	if (entry->delta)
 		type = (allow_ofs_delta && entry->delta->idx.offset) ?
 			OBJ_OFS_DELTA : OBJ_REF_DELTA;
-	hdrlen = encode_in_pack_object_header(type, entry->size, header);
+	if (pack_version < 4)
+		hdrlen = encode_in_pack_object_header(type, entry->size, header);
+	else
+		hdrlen = pv4_encode_in_pack_object_header(type, entry->size, header);
 
 	offset = entry->in_pack_offset;
 	revidx = find_pack_revindex(p, offset);
@@ -404,7 +458,7 @@ static unsigned long write_reuse_object(struct sha1file *f, struct object_entry
 		sha1write(f, dheader + pos, sizeof(dheader) - pos);
 		hdrlen += sizeof(dheader) - pos;
 		reused_delta++;
-	} else if (type == OBJ_REF_DELTA) {
+	} else if (type == OBJ_REF_DELTA && pack_version < 4) {
 		if (limit && hdrlen + 20 + datalen + 20 >= limit) {
 			unuse_pack(&w_curs);
 			return 0;
@@ -413,6 +467,11 @@ static unsigned long write_reuse_object(struct sha1file *f, struct object_entry
 		sha1write(f, entry->delta->idx.sha1, 20);
 		hdrlen += 20;
 		reused_delta++;
+	} else if (type == OBJ_REF_DELTA && pack_version == 4) {
+		hdrlen += encode_sha1ref(&v4, entry->delta->idx.sha1,
+					header + hdrlen);
+		sha1write(f, header, hdrlen);
+		reused_delta++;
 	} else {
 		if (limit && hdrlen + datalen + 20 >= limit) {
 			unuse_pack(&w_curs);
@@ -477,7 +536,9 @@ static unsigned long write_object(struct sha1file *f,
 				 * and we do not need to deltify it.
 				 */
 
-	if (!to_reuse)
+	if (!to_reuse ||
+	    (pack_version == 4 &&
+	     (entry->type == OBJ_TREE || entry->type == OBJ_COMMIT)))
 		len = write_no_reuse_object(f, entry, limit, usable_delta);
 	else
 		len = write_reuse_object(f, entry, limit, usable_delta);
@@ -742,6 +803,8 @@ static void write_pack_file(void)
 		offset = write_pack_header(f, pack_version, nr_remaining);
 		if (!offset)
 			die_errno("unable to write pack header");
+		if (pack_version == 4)
+			offset += packv4_write_tables(f, &v4);
 		nr_written = 0;
 		for (; i < nr_objects; i++) {
 			struct object_entry *e = write_order[i];
@@ -2083,6 +2146,8 @@ static void prepare_pack(int window, int depth)
 		sort_dict_entries_by_hits(v4.commit_ident_table);
 		sort_dict_entries_by_hits(v4.tree_path_table);
 		prepare_sha1_table();
+		pack_idx_opts.version = 3;
+		allow_ofs_delta = 0;
 	}
 
 	get_object_details();
diff --git a/pack.h b/pack.h
index 38f869d..fde60ec 100644
--- a/pack.h
+++ b/pack.h
@@ -8,7 +8,7 @@
  * Packed object header
  */
 #define PACK_SIGNATURE 0x5041434b	/* "PACK" */
-#define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3))
+#define pack_version_ok(v) ((v) == htonl(2) || (v) == htonl(3) || (v) == htonl(4))
 struct pack_header {
 	uint32_t hdr_signature;
 	uint32_t hdr_version;
-- 
1.8.2.83.gc99314b
Previous: Nguyễn Thái Ngọc DuyNext: Nguyễn Thái Ngọc Duy
Message 98 of 124 in “pack version 4 basic functionalities”
  1. 00/38 pack version 4 basic functionalitiesNicolas Pitre, Sep 5, 2013
  2. 01/38 pack v4: initial pack dictionary structure and codeNicolas Pitre, Sep 5, 2013
  3. 02/38 export packed_object_info()Nicolas Pitre, Sep 5, 2013
  4. 03/38 pack v4: scan tree objectsNicolas Pitre, Sep 5, 2013
  5. 04/38 pack v4: add tree entry mode support to dictionary entriesNicolas Pitre, Sep 5, 2013
  6. 05/38 pack v4: add commit object parsingNicolas Pitre, Sep 5, 2013
  7. SZEDER GáborSep 5, 2013
  8. Nicolas PitreSep 5, 2013
  9. 06/38 pack v4: split the object list and dictionary creationNicolas Pitre, Sep 5, 2013
  10. 07/38 pack v4: move to struct pack_idx_entry and get rid of our own struct idx_entryNicolas Pitre, Sep 5, 2013
  11. 08/38 pack v4: basic SHA1 reference encodingNicolas Pitre, Sep 5, 2013
  12. 09/38 introduce get_sha1_lowhex()Nicolas Pitre, Sep 5, 2013
  13. 10/38 pack v4: commit object encodingNicolas Pitre, Sep 5, 2013
  14. Junio C HamanoSep 6, 2013
  15. Nicolas PitreSep 6, 2013
  16. Junio C HamanoSep 6, 2013
  17. Nicolas PitreSep 7, 2013
  18. 11/38 pack v4: tree object encodingNicolas Pitre, Sep 5, 2013
  19. 12/38 pack v4: dictionary table outputNicolas Pitre, Sep 5, 2013
  20. 13/38 pack v4: creation codeNicolas Pitre, Sep 5, 2013
  21. 14/38 pack v4: object headersNicolas Pitre, Sep 5, 2013
  22. 15/38 pack v4: object data copyNicolas Pitre, Sep 5, 2013
  23. 16/38 pack v4: object writingNicolas Pitre, Sep 5, 2013
  24. 17/38 pack v4: tree object delta encodingNicolas Pitre, Sep 5, 2013
  25. 18/38 pack v4: load delta candidate for encoding tree objectsNicolas Pitre, Sep 5, 2013
  26. 19/38 packv4-create: optimize delta encodingNicolas Pitre, Sep 5, 2013
  27. 20/38 pack v4: honor pack.compression config optionNicolas Pitre, Sep 5, 2013
  28. 21/38 pack v4: relax commit parsing a bitNicolas Pitre, Sep 5, 2013
  29. 22/38 pack index v3Nicolas Pitre, Sep 5, 2013
  30. 23/38 packv4-create: normalize pack name to properly generate the pack index file nameNicolas Pitre, Sep 5, 2013
  31. 24/38 packv4-create: add progress displayNicolas Pitre, Sep 5, 2013
  32. 25/38 pack v4: initial pack index v3 support on the read sideNicolas Pitre, Sep 5, 2013
  33. 26/38 pack v4: object header decodeNicolas Pitre, Sep 5, 2013
  34. 27/38 pack v4: code to obtain a SHA1 from a sha1refNicolas Pitre, Sep 5, 2013
  35. 28/38 pack v4: code to load and prepare a pack dictionary table for useNicolas Pitre, Sep 5, 2013
  36. 29/38 pack v4: code to retrieve a nameNicolas Pitre, Sep 5, 2013
  37. 30/38 pack v4: code to recreate a canonical commit objectNicolas Pitre, Sep 5, 2013
  38. 31/38 sha1_file.c: make use of decode_varint()Nicolas Pitre, Sep 5, 2013
  39. SZEDER GáborSep 5, 2013
  40. 32/38 pack v4: parse delta base referenceNicolas Pitre, Sep 5, 2013
  41. 33/38 pack v4: we can read commit objects nowNicolas Pitre, Sep 5, 2013
  42. 34/38 pack v4: code to retrieve a path componentNicolas Pitre, Sep 5, 2013
  43. 35/38 pack v4: decode tree objectsNicolas Pitre, Sep 5, 2013
  44. 36/38 pack v4: get tree objectsNicolas Pitre, Sep 5, 2013
  45. 37/38 pack v4: introduce "escape hatches" in the name and path indexesNicolas Pitre, Sep 5, 2013
  46. Nicolas PitreSep 5, 2013
  47. Nicolas PitreSep 5, 2013
  48. Duy NguyenSep 5, 2013
  49. 38/38 packv4-create: add a command line argument to limit tree copy sequencesNicolas Pitre, Sep 5, 2013
  50. 00/12 pack v4 support in index-packNguyễn Thái Ngọc Duy, Sep 7, 2013
  51. 01/12 pack v4: split pv4_create_dict() out of load_dict()Nguyễn Thái Ngọc Duy, Sep 7, 2013
  52. 02/12 index-pack: split out varint decoding codeNguyễn Thái Ngọc Duy, Sep 7, 2013
  53. 03/12 index-pack: do not allocate buffer for unpacking deltas in the first passNguyễn Thái Ngọc Duy, Sep 7, 2013
  54. 04/12 index-pack: split inflate/digest code out of unpack_entry_dataNguyễn Thái Ngọc Duy, Sep 7, 2013
  55. 05/12 index-pack: parse v4 header and dictionariesNguyễn Thái Ngọc Duy, Sep 7, 2013
  56. Nicolas PitreSep 8, 2013
  57. 06/12 index-pack: make sure all objects are registered in v4's SHA-1 tableNguyễn Thái Ngọc Duy, Sep 7, 2013
  58. 07/12 index-pack: parse v4 commit formatNguyễn Thái Ngọc Duy, Sep 7, 2013
  59. 08/12 index-pack: parse v4 tree formatNguyễn Thái Ngọc Duy, Sep 7, 2013
  60. Nicolas PitreSep 8, 2013
  61. 09/12 index-pack: move delta base queuing code to unpack_raw_entryNguyễn Thái Ngọc Duy, Sep 7, 2013
  62. 10/12 index-pack: record all delta bases in v4 (tree and ref-delta)Nguyễn Thái Ngọc Duy, Sep 7, 2013
  63. 11/12 index-pack: skip looking for ofs-deltas in v4 as they are not allowedNguyễn Thái Ngọc Duy, Sep 7, 2013
  64. 12/12 index-pack: resolve v4 one-base treesNguyễn Thái Ngọc Duy, Sep 7, 2013
  65. Nicolas PitreSep 8, 2013
  66. Duy NguyenSep 8, 2013
  67. 00/14 pack v4 support in index-packNguyễn Thái Ngọc Duy, Sep 8, 2013
  68. 01/14 pack v4: split pv4_create_dict() out of load_dict()Nguyễn Thái Ngọc Duy, Sep 8, 2013
  69. 02/14 pack v4: add pv4_free_dict()Nguyễn Thái Ngọc Duy, Sep 8, 2013
  70. 03/14 index-pack: add more comments on some big functionsNguyễn Thái Ngọc Duy, Sep 8, 2013
  71. 04/14 index-pack: split out varint decoding codeNguyễn Thái Ngọc Duy, Sep 8, 2013
  72. 05/14 index-pack: do not allocate buffer for unpacking deltas in the first passNguyễn Thái Ngọc Duy, Sep 8, 2013
  73. 06/14 index-pack: split inflate/digest code out of unpack_entry_dataNguyễn Thái Ngọc Duy, Sep 8, 2013
  74. 07/14 index-pack: parse v4 header and dictionariesNguyễn Thái Ngọc Duy, Sep 8, 2013
  75. 08/14 index-pack: make sure all objects are registered in v4's SHA-1 tableNguyễn Thái Ngọc Duy, Sep 8, 2013
  76. 09/14 index-pack: parse v4 commit formatNguyễn Thái Ngọc Duy, Sep 8, 2013
  77. 10/14 index-pack: parse v4 tree formatNguyễn Thái Ngọc Duy, Sep 8, 2013
  78. 11/14 index-pack: move delta base queuing code to unpack_raw_entryNguyễn Thái Ngọc Duy, Sep 8, 2013
  79. 12/14 index-pack: record all delta bases in v4 (tree and ref-delta)Nguyễn Thái Ngọc Duy, Sep 8, 2013
  80. 13/14 index-pack: skip looking for ofs-deltas in v4 as they are not allowedNguyễn Thái Ngọc Duy, Sep 8, 2013
  81. 14/14 index-pack: resolve v4 one-base treesNguyễn Thái Ngọc Duy, Sep 8, 2013
  82. 00/11 pack v4 support in pack-objectsNguyễn Thái Ngọc Duy, Sep 8, 2013
  83. 01/11 pack v4: allocate dicts from the beginningNguyễn Thái Ngọc Duy, Sep 8, 2013
  84. 02/11 pack v4: stop using static/global variables in packv4-create.cNguyễn Thái Ngọc Duy, Sep 8, 2013
  85. 03/11 pack v4: move packv4-create.c to libgit.aNguyễn Thái Ngọc Duy, Sep 8, 2013
  86. Nicolas PitreSep 8, 2013
  87. 04/11 pack v4: add version argument to write_pack_headerNguyễn Thái Ngọc Duy, Sep 8, 2013
  88. 05/11 pack-write.c: add pv4_encode_in_pack_object_headerNguyễn Thái Ngọc Duy, Sep 8, 2013
  89. Nicolas PitreSep 8, 2013
  90. 06/11 pack-objects: add --version to specify written pack versionNguyễn Thái Ngọc Duy, Sep 8, 2013
  91. 07/11 list-objects.c: add show_tree_entry callback to traverse_commit_listNguyễn Thái Ngọc Duy, Sep 8, 2013
  92. 08/11 pack-objects: create pack v4 tablesNguyễn Thái Ngọc Duy, Sep 8, 2013
  93. Duy NguyenSep 9, 2013
  94. Nicolas PitreSep 9, 2013
  95. Junio C HamanoSep 9, 2013
  96. 09/11 pack-objects: do not cache delta for v4 treesNguyễn Thái Ngọc Duy, Sep 8, 2013
  97. 10/11 pack-objects: exclude commits out of delta objects in v4Nguyễn Thái Ngọc Duy, Sep 8, 2013
  98. 11/11 pack-objects: support writing pack v4Nguyễn Thái Ngọc Duy, Sep 8, 2013
  99. 00/16 pack v4 support in pack-objectsNguyễn Thái Ngọc Duy, Sep 9, 2013
  100. 01/16 pack v4: allocate dicts from the beginningNguyễn Thái Ngọc Duy, Sep 9, 2013
  101. 02/16 pack v4: stop using static/global variables in packv4-create.cNguyễn Thái Ngọc Duy, Sep 9, 2013
  102. 03/16 pack v4: move packv4-create.c to libgit.aNguyễn Thái Ngọc Duy, Sep 9, 2013
  103. 04/16 pack v4: add version argument to write_pack_headerNguyễn Thái Ngọc Duy, Sep 9, 2013
  104. 05/16 pack_write: tighten valid object type check in encode_in_pack_object_headerNguyễn Thái Ngọc Duy, Sep 9, 2013
  105. 06/16 pack-write.c: add pv4_encode_object_headerNguyễn Thái Ngọc Duy, Sep 9, 2013
  106. 07/16 pack-objects: add --version to specify written pack versionNguyễn Thái Ngọc Duy, Sep 9, 2013
  107. 08/16 list-objects.c: add show_tree_entry callback to traverse_commit_listNguyễn Thái Ngọc Duy, Sep 9, 2013
  108. 09/16 pack-objects: do not cache delta for v4 treesNguyễn Thái Ngọc Duy, Sep 9, 2013
  109. 10/16 pack-objects: exclude commits out of delta objects in v4Nguyễn Thái Ngọc Duy, Sep 9, 2013
  110. 11/16 pack-objects: create pack v4 tablesNguyễn Thái Ngọc Duy, Sep 9, 2013
  111. 12/16 pack-objects: prepare SHA-1 table in v4Nguyễn Thái Ngọc Duy, Sep 9, 2013
  112. 13/16 pack-objects: support writing pack v4Nguyễn Thái Ngọc Duy, Sep 9, 2013
  113. 14/16 pack v4: support "end-of-pack" indicator in index-pack and pack-objectsNguyễn Thái Ngọc Duy, Sep 9, 2013
  114. 15/16 index-pack: use nr_objects_final as sha1_table sizeNguyễn Thái Ngọc Duy, Sep 9, 2013
  115. Nicolas PitreSep 9, 2013
  116. Junio C HamanoSep 9, 2013
  117. Nicolas PitreSep 9, 2013
  118. Junio C HamanoSep 9, 2013
  119. Nicolas PitreSep 9, 2013
  120. Junio C HamanoSep 9, 2013
  121. Nicolas PitreSep 9, 2013
  122. Duy NguyenSep 10, 2013
  123. Nicolas PitreSep 12, 2013
  124. 16/16 index-pack: support completing thin packs v4Nguyễn Thái Ngọc Duy, Sep 9, 2013

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.