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

[PATCH v4 13/24] read-cache: read resolve-undo data

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Nov 27, 2013, 12:00 UTC
Message-ID
<1385553659-9928-14-git-send-email-t.gummerer@gmail.com>
In-Reply-To
<1385553659-9928-1-git-send-email-t.gummerer@gmail.com>
Make git read the resolve-undo data from the index.

Since the resolve-undo data is joined with the conflicts in the ondisk format of the index file version 5, conflicts and resolved data is read at the same time, and the resolve-undo data are then converted to the in-memory format.

Helped-by: Thomas Rast <trast@student.ethz.ch>
Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---
 read-cache-v5.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 157 insertions(+), 3 deletions(-)
diff --git a/read-cache-v5.c b/read-cache-v5.c
index 9d8c8f0..a9c687f 100644
--- a/read-cache-v5.c
+++ b/read-cache-v5.c
@@ -1,5 +1,6 @@
 #include "cache.h"
 #include "read-cache.h"
+#include "string-list.h"
 #include "resolve-undo.h"
 #include "cache-tree.h"
 #include "dir.h"
@@ -13,13 +14,18 @@ struct cache_header_v5 {
 	uint32_t hdr_nextension;
 };
 
+struct extension_header {
+	char signature[4];
+	uint32_t size;
+	uint32_t crc;
+};
+
 struct directory_entry {
 	struct directory_entry **sub;
 	struct directory_entry *next;
 	struct directory_entry *next_hash;
 	struct cache_entry *ce;
 	struct cache_entry *ce_last;
-	uint32_t conflict_size;
 	uint32_t de_foffset;
 	uint32_t de_nsubtrees;
 	uint32_t de_nfiles;
@@ -42,7 +48,6 @@ struct conflict_entry {
 	uint32_t nfileconflicts;
 	struct conflict_part *entries;
 	uint32_t namelen;
-	uint32_t pathlen;
 	char name[FLEX_ARRAY];
 };
 
@@ -50,6 +55,12 @@ struct conflict_entry {
  * Index File I/O
  *****************************************************************/
 
+struct ondisk_conflict_part {
+	uint16_t flags;
+	uint16_t entry_mode;
+	unsigned char sha1[20];
+};
+
 struct ondisk_cache_entry {
 	uint16_t flags;
 	uint16_t mode;
@@ -145,7 +156,7 @@ static int verify_hdr(void *mmap, unsigned long size)
 	hdr = mmap;
 	hdr_v5 = ptr_add(mmap, sizeof(*hdr));
 	/* Size of the header + the size of the extensionoffsets */
-	header_size = sizeof(*hdr) + sizeof(*hdr_v5) + hdr_v5->hdr_nextension * 4;
+	header_size = sizeof(*hdr) + sizeof(*hdr_v5) + ntohl(hdr_v5->hdr_nextension) * 4;
 	/* Initialize crc */
 	filecrc = ptr_add(mmap, header_size);
 	if (!check_crc32(0, hdr, header_size, ntohl(*filecrc)))
@@ -279,6 +290,134 @@ static int read_entry(struct cache_entry **ce, char *pathname, size_t pathlen,
 	return 0;
 }
 
+static struct conflict_part *conflict_part_from_ondisk(struct ondisk_conflict_part *ondisk)
+{
+	struct conflict_part *cp = xmalloc(sizeof(struct conflict_part));
+
+	cp->flags      = ntoh_s(ondisk->flags);
+	cp->entry_mode = ntoh_s(ondisk->entry_mode);
+	hashcpy(cp->sha1, ondisk->sha1);
+	return cp;
+}
+
+struct conflict_entry *create_new_conflict(char *name, int len)
+{
+	struct conflict_entry *conflict_entry;
+
+	conflict_entry = xmalloc(conflict_entry_size(len));
+	memset(conflict_entry, 0, conflict_entry_size(len));
+	conflict_entry->namelen = len;
+	memcpy(conflict_entry->name, name, len);
+
+	return conflict_entry;
+}
+
+static void add_part_to_conflict_entry(struct conflict_entry *entry,
+				       struct conflict_part *conflict_part)
+{
+
+	struct conflict_part *conflict_search;
+
+	entry->nfileconflicts++;
+	if (!entry->entries)
+		entry->entries = conflict_part;
+	else {
+		conflict_search = entry->entries;
+		while (conflict_search->next)
+			conflict_search = conflict_search->next;
+		conflict_search->next = conflict_part;
+	}
+}
+
+/*
+ * Read the resolve undo data on disk and convert it to the internal
+ * resolve undo format.
+ */
+static int read_resolve_undo(struct index_state *istate,
+			     unsigned int offset, void *mmap,
+			     unsigned int entries)
+{
+	int i, k;
+
+	for (i = 0; i < entries; i++) {
+		char *name;
+		unsigned int len, *nfileconflicts, nc;
+		uint32_t *crc;
+		struct ondisk_conflict_part *ondisk;
+		struct conflict_part *cp;
+		struct string_list_item *lost;
+		struct resolve_undo_info *ui;
+
+		name = ptr_add(mmap, offset);
+		len = strlen(name);
+		offset += len + 1;
+		nfileconflicts = ptr_add(mmap, offset);
+		nc = ntoh_l(*nfileconflicts);
+		offset += 4;
+
+		crc = ptr_add(mmap, offset +
+			      nc * sizeof(struct ondisk_conflict_part));
+		if (!check_crc32(0, name, len + 1 + 4 +
+				 nc * sizeof(struct ondisk_conflict_part),
+				 ntoh_l(*crc)))
+			return -1;
+
+		ondisk = ptr_add(mmap, offset);
+		cp = conflict_part_from_ondisk(ondisk);
+		if (cp->flags & CONFLICT_CONFLICTED) {
+			offset += nc * sizeof(struct ondisk_conflict_part) + 4;
+			continue;
+		}
+		offset += sizeof(struct ondisk_conflict_part);
+		if (!istate->resolve_undo) {
+			istate->resolve_undo = xcalloc(1, sizeof(struct string_list));
+			istate->resolve_undo->strdup_strings = 1;
+		}
+
+		lost = string_list_insert(istate->resolve_undo, name);
+		if (!lost->util)
+			lost->util = xcalloc(1, sizeof(*ui));
+		ui = lost->util;
+		for (k = 0; k < 3; k++)
+			ui->mode[k] = 0;
+
+		ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
+		hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
+		for (k = 1; k < nc; k++) {
+			struct conflict_part *cp;
+
+			ondisk = ptr_add(mmap, offset);
+			cp = conflict_part_from_ondisk(ondisk);
+			ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
+			hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
+			offset += sizeof(struct ondisk_conflict_part);
+		}
+		offset += 4; /* crc */
+	}
+	return 0;
+}
+
+static int read_index_extension(struct index_state *istate,
+				void *mmap, unsigned int extoffset)
+{
+	struct extension_header *ehdr;
+
+	ehdr = ptr_add(mmap, extoffset);
+	/* -4 for the crc that's included in the struct */
+	if (!check_crc32(0, ptr_add(mmap, extoffset),
+			 sizeof(*ehdr) - 4, ntoh_l(ehdr->crc)))
+		return -1;
+
+	switch (CACHE_EXT(ehdr->signature)) {
+	case CACHE_EXT_RESOLVE_UNDO:
+		if (read_resolve_undo(istate, extoffset + sizeof(*ehdr),
+				      mmap, ntoh_l(ehdr->size)) < 0)
+			return -1;
+		break;
+	}
+	return 0;
+}
+
 /*
  * Read all file entries from the index.  This function is recursive to get
  * the ordering right. In the index file the entries are sorted def, abc/def,
@@ -404,6 +543,21 @@ static int read_index_v5(struct index_state *istate, void *mmap,
 		}
 		de = de->next;
 	}
+
+	if (!opts || opts->read_resolve_undo) {
+		for (i = 0; i < ntohl(hdr_v5->hdr_nextension); i++) {
+			/*
+			 * After the index entry there is a number of
+			 * extensions, which is written in the header.
+			 * The extensions are prefixed by extension name
+			 * (4-byte) and length of the extension (4-byte,
+			 * usually the number of entries in that section)
+			 * in network byte order
+			 */
+			if (read_index_extension(istate, mmap, extoffsets[i]) < 0)
+				return -1;
+		}
+	}
 	free_directory_tree(root_directory);
 	istate->cache_nr = nr;
 	return 0;
-- 
1.8.4.2
Previous: Thomas GummererNext: Thomas Gummerer
Message 24 of 41 in “Index-v5”
  1. 00/24 Index-v5Thomas Gummerer, Nov 27, 2013
  2. 01/24 t2104: Don't fail for index versions other than [23]Thomas Gummerer, Nov 27, 2013
  3. 02/24 read-cache: split index file version specific functionalityThomas Gummerer, Nov 27, 2013
  4. 03/24 read-cache: move index v2 specific functions to their own fileThomas Gummerer, Nov 27, 2013
  5. 04/24 read-cache: Re-read index if index file changedThomas Gummerer, Nov 27, 2013
  6. 05/24 add documentation for the index apiThomas Gummerer, Nov 27, 2013
  7. 06/24 read-cache: add index reading apiThomas Gummerer, Nov 27, 2013
  8. 07/24 make sure partially read index is not changedThomas Gummerer, Nov 27, 2013
  9. 08/24 grep.c: use index apiThomas Gummerer, Nov 27, 2013
  10. 09/24 ls-files.c: use index apiThomas Gummerer, Nov 27, 2013
  11. Duy NguyenNov 30, 2013
  12. Thomas GummererNov 30, 2013
  13. Antoine PelisseNov 30, 2013
  14. Thomas GummererNov 30, 2013
  15. 10/24 documentation: add documentation of the index-v5 file formatThomas Gummerer, Nov 27, 2013
  16. 11/24 read-cache: make in-memory format aware of stat_crcThomas Gummerer, Nov 27, 2013
  17. 12/24 read-cache: read index-v5Thomas Gummerer, Nov 27, 2013
  18. Duy NguyenNov 30, 2013
  19. Thomas GummererNov 30, 2013
  20. Antoine PelisseNov 30, 2013
  21. Thomas GummererNov 30, 2013
  22. Antoine PelisseNov 30, 2013
  23. Thomas GummererNov 30, 2013
  24. 13/24 read-cache: read resolve-undo dataThomas Gummerer, Nov 27, 2013
  25. 14/24 read-cache: read cache-tree in index-v5Thomas Gummerer, Nov 27, 2013
  26. 15/24 read-cache: write index-v5Thomas Gummerer, Nov 27, 2013
  27. 16/24 read-cache: write index-v5 cache-tree dataThomas Gummerer, Nov 27, 2013
  28. 17/24 read-cache: write resolve-undo data for index-v5Thomas Gummerer, Nov 27, 2013
  29. 18/24 update-index.c: rewrite index when index-version is givenThomas Gummerer, Nov 27, 2013
  30. 19/24 p0003-index.sh: add perf test for the index formatsThomas Gummerer, Nov 27, 2013
  31. 20/24 introduce GIT_INDEX_VERSION environment variableThomas Gummerer, Nov 27, 2013
  32. Eric SunshineNov 27, 2013
  33. Junio C HamanoNov 27, 2013
  34. Thomas GummererNov 28, 2013
  35. 21/24 test-lib: allow setting the index format versionThomas Gummerer, Nov 27, 2013
  36. 22/24 t1600: add index v5 specific testsThomas Gummerer, Nov 27, 2013
  37. 23/24 POC for partial writingThomas Gummerer, Nov 27, 2013
  38. Duy NguyenNov 30, 2013
  39. Thomas GummererNov 30, 2013
  40. 24/24 perf: add partial writing testThomas Gummerer, Nov 27, 2013
  41. Thomas GummererDec 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.