Skip to contentSkip to conversation

Introduce index file format version 5

PATCH Superseded Thomas Gummerer 5 participants 34 messages Jun 15, 2016

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Introduce index file format version 5

Previous rounds of this series were on $gmane/202752 and $gmane/202923.

This round includes a major change, as it is splitting up read-cache.c into read-cache.c, read-cache-v2.c and read-cache-v5.c. It uses index->ops to call the specific functions as suggested by Duy.

It also changes the index format slightly, removing the size from the stat_crc and adding it as separate field, as suggested by Robin. This makes the format incompatible with the one in the previous two series.

[PATCH/RFC 01/13] Move index v2 specific functions to their own file This previously was patch 1/2/3/4, but since the code is now moved to a different file, it seems to make more sense to do it in one patch. Also moved the size check to the verify_hdr functions for the respective index formats.

[PATCH/RFC 02/13] t2104: Don't fail for index versions other than Do the update index at the very beginning.

[PATCH/RFC 03/13] t3700: Avoid interfering with the racy code Instead of sleeping for a second, test-chmtime now uses an absolute time, to avoid the racy code.

[PATCH/RFC 04/13] Add documentation of the index-v5 file format Added the size to the cache-entries.

[PATCH/RFC 05/13] Make in-memory format aware of stat_crc Removed size from the stat_crc.

[PATCH/RFC 06/13] Read index-v5
Changed the pointer arithmetic to not use void * directly, by casting it to a char * first.

[PATCH/RFC 07/13] Read resolve-undo data
[PATCH/RFC 08/13] Read cache-tree in index-v5 [PATCH/RFC 09/13] Write index-v5
[PATCH/RFC 10/13] Write index-v5 cache-tree data [PATCH/RFC 11/13] Write resolve-undo data for index-v5 [PATCH/RFC 12/13] update-index.c: always rewrite the index when Instead of using a force-rewrite option, always rewrite the index if a index-version is given.

[PATCH/RFC 13/13] p0002-index.sh: add perf test for the index Instead of using the force-rewrite option, which was removed from update-index, use git update-index --index-version=n for doing the preformance testing.

#1 lore raw

PATCH v3 1/13 Move index v2 specific functions to their own file

Move index version 2 specific functions to their own file, to prepare for the addition of a new index file format. With the split into two files we have the non-index specific functions in read-cache.c and the index-v2 specific functions in read-cache-v2.c

---
 Makefile             |    2 +
 cache.h              |   13 +-
 read-cache-v2.c      |  581 +++++++++++++++++++++++++++++++++++++++++++++++
 read-cache.c         |  613 +++-----------------------------------------------
 read-cache.h         |   57 +++++
 test-index-version.c |    7 +-
 6 files changed, 683 insertions(+), 590 deletions(-)
 create mode 100644 read-cache-v2.c
 create mode 100644 read-cache.h
Makefile +2 -0
index 4b58b91..b4a7c73 100644
@@ -645,6 +645,7 @@ LIB_H += progress.h
 LIB_H += prompt.h
 LIB_H += quote.h
 LIB_H += reachable.h
+LIB_H += read-cache.h
 LIB_H += reflog-walk.h
 LIB_H += refs.h
 LIB_H += remote.h
@@ -768,6 +769,7 @@ LIB_OBJS += prompt.o
 LIB_OBJS += quote.o
 LIB_OBJS += reachable.o
 LIB_OBJS += read-cache.o
+LIB_OBJS += read-cache-v2.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += remote.o
cache.h +3 -10
index 67f28b4..c77cdbe 100644
@@ -94,16 +94,8 @@ unsigned long git_deflate_bound(git_zstream *, unsigned long);
  */
 #define DEFAULT_GIT_PORT 9418
 
-/*
- * Basic data structures for the directory cache
- */
 
 #define CACHE_SIGNATURE 0x44495243	/* "DIRC" */
-struct cache_header {
-	unsigned int hdr_signature;
-	unsigned int hdr_version;
-	unsigned int hdr_entries;
-};
 
 #define INDEX_FORMAT_LB 2
 #define INDEX_FORMAT_UB 4
@@ -267,6 +259,7 @@ struct index_state {
 	unsigned name_hash_initialized : 1,
 		 initialized : 1;
 	struct hash_table name_hash;
+	struct index_ops *ops;
 };
 
 extern struct index_state the_index;
@@ -471,8 +464,8 @@ extern int index_name_is_other(const struct index_state *, const char *, int);
 #define CE_MATCH_RACY_IS_DIRTY		02
 /* do stat comparison even if CE_SKIP_WORKTREE is true */
 #define CE_MATCH_IGNORE_SKIP_WORKTREE	04
-extern int ie_match_stat(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
-extern int ie_modified(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, unsigned int);
 
 struct pathspec {
 	const char **raw; /* get_pathspec() result, not freed by free_pathspec() */
read-cache-v2.c new file +581 -0

581 lines not on this page · raw message

read-cache.c +34 -579

727 lines not on this page · raw message

read-cache.h new file +57 -0

57 lines not on this page · raw message

test-index-version.c +6 -1
index bfaad9e..3899a2f 100644
@@ -1,8 +1,13 @@
 #include "cache.h"
 
+struct cache_version_header {
+	unsigned int hdr_signature;
+	unsigned int hdr_version;
+};
+
 int main(int argc, const char **argv)
 {
-	struct cache_header hdr;
+	struct cache_version_header hdr;
 	int version;
 
 	memset(&hdr,0,sizeof(hdr));
Signature · 1 line
1.7.10.GIT
#2 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 5 lines · Thomas Gummerer

Move index version 2 specific functions to their own file, to prepare for the addition of a new index file format. With the split into two files we have the non-index specific functions in read-cache.c and the index-v2 specific functions in read-cache-v2.c

You still mix code changes and code move in one patch, but we can skip it for now.

cache.h struct index_state { · line 263

Quoted 8 lines
cache.h +1 -0
@@ -267,6 +259,7 @@ struct index_state {
        unsigned name_hash_initialized : 1,
                 initialized : 1;
        struct hash_table name_hash;
+       struct index_ops *ops;
 };

Do we really need to modify "ops" content? If not make it "const struct index_ops *ops;" which makes..

cache.h extern int index_name_is_other(const struct index_state *, const char *, int); · line 468

Quoted 8 lines
cache.h +2 -2
@@ -471,8 +464,8 @@ extern int index_name_is_other(const struct index_state *, const char *, int);
 #define CE_MATCH_RACY_IS_DIRTY         02
 /* do stat comparison even if CE_SKIP_WORKTREE is true */
 #define CE_MATCH_IGNORE_SKIP_WORKTREE  04
-extern int ie_match_stat(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
-extern int ie_modified(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, unsigned int);

..this hunk go away

Signature · 1 line
Duy
#3 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 22 lines · Nguyen Thai Ngoc Duy
Quoted 5 lines · Thomas Gummerer

Move index version 2 specific functions to their own file, to prepare for the addition of a new index file format. With the split into two files we have the non-index specific functions in read-cache.c and the index-v2 specific functions in read-cache-v2.c

You still mix code changes and code move in one patch, but we can skip it for now.

cache.h struct index_state { · line 263

Quoted 8 lines
cache.h +1 -0
@@ -267,6 +259,7 @@ struct index_state {
        unsigned name_hash_initialized : 1,
                 initialized : 1;
        struct hash_table name_hash;
+       struct index_ops *ops;
 };

Do we really need to modify "ops" content? If not make it "const struct index_ops *ops;" which makes..

Quoted 10 lines

cache.h extern int index_name_is_other(const struct index_state *, const char *, int); · line 468

Quoted 8 lines
cache.h +2 -2
@@ -471,8 +464,8 @@ extern int index_name_is_other(const struct index_state *, const char *, int);
 #define CE_MATCH_RACY_IS_DIRTY         02
 /* do stat comparison even if CE_SKIP_WORKTREE is true */
 #define CE_MATCH_IGNORE_SKIP_WORKTREE  04
-extern int ie_match_stat(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
-extern int ie_modified(const struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat *, unsigned int);
+extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, unsigned int);

..this hunk go away

Unfortunately I think we need to modify it, because in some cases when ie_modified/ie_match_stat is called, index->ops is not set yet (happens when the index is empty and thus not read first). Therefore we have to set it in ce_match_stat_basic to have access to the format specific match_stat_basic.

We could also call match_stat_basic directly from the v[25]_ops struct, but I do not think that would be the cleaner solution, or is there any other way to do it, which I can't see?

#4 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

In reply to Thomas Gummerer
Quoted 10 lines · Thomas Gummerer

/* remember to discard_cache() before reading a different cache! */ int read_index_from(struct index_state *istate, const char *path) {
...

read-cache.c +0 -1
 	mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	close(fd);
 	if (mmap == MAP_FAILED)
 		die_errno("unable to map index file");
 
 	hdr = mmap;

read-cache.c int read_index_from(struct index_state *istate, const char *path) · line 1217

Quoted 10 lines
read-cache.c +5 -1
-	if (verify_hdr(hdr, mmap_size) < 0)
+	if (verify_hdr_version(istate, hdr, mmap_size) < 0)
 		goto unmap;
...
+	if (istate->ops->verify_hdr(mmap, mmap_size) < 0)
+		goto unmap;
 
+	istate->ops->read_index(istate, mmap, mmap_size, fd);
...
+	close(fd);

This looks utterly wrong.

You already have mapped the whole thing, so there is nothing to be read from fd. You have everything in-core. Leaving fd open and pass it around looks like it is asking for trouble and confusion.

If you found that an entry you read halfway has an inconsistent crc, and if you suspect that is because somebody else was writing to the same index, it is a _sure_ sign that you are not alone, and all the entries you read so far to the core, even if they weren't touched by that sombody else when you read them, may be stale, and worse yet, what you are going to read may be inconsistent with what you've read and have in-core (e.g. you may have read "f" before somebody else that is racing with you have turned it into a directory, and your next read may find "f/d" in the index without crc error).

One sane way to avoid reading such an inconsistent state may be to redo this whole function, starting from the part that calls mmap(). IOW,

	do {
		fd = open()
		mmap = xmmap(fd);
		close(fd);
                verify_various_fields(mmap);
                status = istate->ops->read_index(istate, mmap, mmap_size));
	} while (status == READ_AGAIN);

I do not think the "pass fd around so that we can redo the mapping deep inside the callchain" is either a good idea or necessary.

#5 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 51 lines · Junio C Hamano
Quoted 10 lines · Thomas Gummerer

/* remember to discard_cache() before reading a different cache! */ int read_index_from(struct index_state *istate, const char *path) {
...

read-cache.c +0 -1
 	mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
-	close(fd);
 	if (mmap == MAP_FAILED)
 		die_errno("unable to map index file");
 
 	hdr = mmap;

read-cache.c int read_index_from(struct index_state *istate, const char *path) · line 1217

Quoted 10 lines
read-cache.c +5 -1
-	if (verify_hdr(hdr, mmap_size) < 0)
+	if (verify_hdr_version(istate, hdr, mmap_size) < 0)
 		goto unmap;
...
+	if (istate->ops->verify_hdr(mmap, mmap_size) < 0)
+		goto unmap;
 
+	istate->ops->read_index(istate, mmap, mmap_size, fd);
...
+	close(fd);

This looks utterly wrong.

You already have mapped the whole thing, so there is nothing to be read from fd. You have everything in-core. Leaving fd open and pass it around looks like it is asking for trouble and confusion.

If you found that an entry you read halfway has an inconsistent crc, and if you suspect that is because somebody else was writing to the same index, it is a _sure_ sign that you are not alone, and all the entries you read so far to the core, even if they weren't touched by that sombody else when you read them, may be stale, and worse yet, what you are going to read may be inconsistent with what you've read and have in-core (e.g. you may have read "f" before somebody else that is racing with you have turned it into a directory, and your next read may find "f/d" in the index without crc error).

One sane way to avoid reading such an inconsistent state may be to redo this whole function, starting from the part that calls mmap(). IOW,

	do {
		fd = open()
		mmap = xmmap(fd);
		close(fd);
                verify_various_fields(mmap);
                status = istate->ops->read_index(istate, mmap, mmap_size));
	} while (status == READ_AGAIN);
Quoted 2 lines

I do not think the "pass fd around so that we can redo the mapping deep inside the callchain" is either a good idea or necessary.

Thanks, that looks better. I'll change it for the re-roll.

#6 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

In reply to Junio C Hamano
Quoted 22 lines · Junio C Hamano

If you found that an entry you read halfway has an inconsistent crc, and if you suspect that is because somebody else was writing to the same index, it is a _sure_ sign that you are not alone, and all the entries you read so far to the core, even if they weren't touched by that sombody else when you read them, may be stale, and worse yet, what you are going to read may be inconsistent with what you've read and have in-core (e.g. you may have read "f" before somebody else that is racing with you have turned it into a directory, and your next read may find "f/d" in the index without crc error).

One sane way to avoid reading such an inconsistent state may be to redo this whole function, starting from the part that calls mmap(). IOW,

	do {
		fd = open()
		mmap = xmmap(fd);
		close(fd);
                verify_various_fields(mmap);
                status = istate->ops->read_index(istate, mmap, mmap_size));
	} while (status == READ_AGAIN);
Quoted 2 lines

I do not think the "pass fd around so that we can redo the mapping deep inside the callchain" is either a good idea or necessary.

By the way, you can only detect such inconsistency when you are lucky enough that you catch the other person in the middle of writing.

If the index you are looking at holds a large tree with very many paths, it is possible that there are two large directories, and after you read all entries from one, the other process starts modifying the paths in that directory, without you ever finding it out. If the goal of the topic is to make the index work better in projects with large trees, it may be wise to think about locking the whole thing, so that you do not have to rely on the per-entry crc and you being lucky to detect such a race. The per-entry crc, as far as I understand, may have been introduced primarily to detect on-disk data corruption; it is not a suitable mechanism to detect conflicting accesses.

#7 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 4 lines · Junio C Hamano

By the way, you can only detect such inconsistency when you are lucky enough that you catch the other person in the middle of writing.

Quoted 11 lines

If the index you are looking at holds a large tree with very many paths, it is possible that there are two large directories, and after you read all entries from one, the other process starts modifying the paths in that directory, without you ever finding it out. If the goal of the topic is to make the index work better in projects with large trees, it may be wise to think about locking the whole thing, so that you do not have to rely on the per-entry crc and you being lucky to detect such a race. The per-entry crc, as far as I understand, may have been introduced primarily to detect on-disk data corruption; it is not a suitable mechanism to detect conflicting accesses.

So we acquire the lock just before we need to write, or at the time we open for reading?

Signature · 1 line
Duy
#8 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano

If you found that an entry you read halfway has an inconsistent crc, and if you suspect that is because somebody else was writing to the same index, it is a _sure_ sign that you are not alone, and all the entries you read so far to the core, even if they weren't touched by that sombody else when you read them, may be stale, and worse yet, what you are going to read may be inconsistent with what you've read and have in-core (e.g. you may have read "f" before somebody else that is racing with you have turned it into a directory, and your next read may find "f/d" in the index without crc error).

The intention for v5 (admittedly this probably requires a lot more documentation) was to only allow an in-place update in two cases:

* updating the data fields (*not* the name) of a file entry,

* adding or removing conflict entries at the end.

The latter probably requires a bit more thought to make it safe, too. But I think the idea always was that any write that changes the basic layout of the file (so that you would read something wrong) will need a full rewrite. Otherwise we're too far in DB land. Most updates will be of the "update the stat and/or sha1 of a file" kind, anyway.

Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.ch
#9 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 6 lines · Thomas Rast

But I think the idea always was that any write that changes the basic layout of the file (so that you would read something wrong) will need a full rewrite. Otherwise we're too far in DB land.

Most updates will be
of the "update the stat and/or sha1 of a file" kind, anyway.

Yes, I agree the v5 format documented in the series does not let you do anything other than the kind of updates without rewriting [*1*]

But that does not fundamentally change the story that a new format and a new way to access the index to cope with larger projects would want to come up with a solution to address the competing read/write issue, or at least help to make it easier to solve the issue in the future.

"That problem is not new" is not an answer when the question is "We still have the problem".

[Footnote]

*1* While my gut feeling matches your guess that the kind of updates would be the majority, I do not think anybody did numbers to substanticate it, which we may want to see happen.

#10 lore raw

Re: [PATCH/RFC v3 01/13] Move index v2 specific functions to their own file

Quoted 22 lines · Junio C Hamano
Quoted 6 lines · Thomas Rast

But I think the idea always was that any write that changes the basic layout of the file (so that you would read something wrong) will need a full rewrite. Otherwise we're too far in DB land.

Most updates will be
of the "update the stat and/or sha1 of a file" kind, anyway.

Yes, I agree the v5 format documented in the series does not let you do anything other than the kind of updates without rewriting [*1*]

But that does not fundamentally change the story that a new format and a new way to access the index to cope with larger projects would want to come up with a solution to address the competing read/write issue, or at least help to make it easier to solve the issue in the future.

"That problem is not new" is not an answer when the question is "We still have the problem".

Quoted 5 lines

[Footnote]

*1* While my gut feeling matches your guess that the kind of updates would be the majority, I do not think anybody did numbers to substanticate it, which we may want to see happen.

Hrm anther way to solve this may be the following. The idea would be to just check if the index_file changed basically using the same heuristic we already use to detect file changes. (use the stat data, mtime, size, etc.)

With this code we do not rely on the crc sums to check if the index needs to be re-read anymore and don't have a problem if part of the index changes, after we read it (we know the index changed from its mtime and can just re-read it). Another thing that would have to change is that we can't use die if a crc is wrong, but some return code, but that shouldn't be a problem. I'm not sure I'm not missing something here though.

do {
	fd = open()
	fstat(fd, &st_old);
	mmap = xmmap(fd);
	verify_various_fields(mmap);
	istate->ops->read_index(istate,
				mmap,
				mmap_size));
	fstat(fd, &st_new);
	close(fd);
} while (stat_data_doesnt_match(st_old, st_new));
#11 lore raw

PATCH v3 2/13 t2104: Don't fail for index versions other than [23]

In reply to Thomas Gummerer

t2104 currently checks for the exact index version 2 or 3, depending if there is a skip-worktree flag or not. Other index versions do not use extended flags and thus cannot be tested for version changes.

Make this test update the index to version 2 at the beginning of the test. Testing the skip-worktree flags for the default index format is still covered by t7011 and t7012.

---
 t/t2104-update-index-skip-worktree.sh |    1 +
 1 file changed, 1 insertion(+)
t/t2104-update-index-skip-worktree.sh +1 -0
index 1d0879b..bd9644f 100755
@@ -22,6 +22,7 @@ H sub/2
 EOF
 
 test_expect_success 'setup' '
+	git update-index --index-version=2 &&
 	mkdir sub &&
 	touch ./1 ./2 sub/1 sub/2 &&
 	git add 1 2 sub/1 sub/2 &&
Signature · 1 line
1.7.10.GIT
#12 lore raw

PATCH v3 3/13 t3700: Avoid interfering with the racy code

In reply to Thomas Gummerer

The new git racy code uses the mtime of cache-entries as smudge marker for racily clean entries. The work of checking the file-system if the entry really changed is offloaded to the reader. This interferes with this test, because the entry is racily smudged and thus has mtime 0.

To avoid interfering with the racy code, we use a time relative to the time returned by time(3), instead of a time relative to the mtime of the cache entries.

---
 t/t3700-add.sh |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t3700-add.sh +1 -1
index 874b3a6..829d36d 100755
@@ -184,7 +184,7 @@ test_expect_success 'git add --refresh with pathspec' '
 	echo >foo && echo >bar && echo >baz &&
 	git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
 	echo "100644 $H 3	foo" | git update-index --index-info &&
-	test-chmtime -60 bar baz &&
+	test-chmtime =-60 bar baz &&
 	>expect &&
 	git add --refresh bar >actual &&
 	test_cmp expect actual &&
Signature · 1 line
1.7.10.GIT
#13 lore raw

PATCH v3 4/13 Add documentation of the index-v5 file format

In reply to Thomas Gummerer

Add a documentation of the index file format version 5 to Documentation/technical.

---
 Documentation/technical/index-file-format-v5.txt |  285 ++++++++++++++++++++++
 1 file changed, 285 insertions(+)
 create mode 100644 Documentation/technical/index-file-format-v5.txt
Documentation/technical/index-file-format-v5.txt new file +285 -0
new file mode 100644
index 0000000..6707f06
@@ -0,0 +1,285 @@
+GIT index format
+================
+
+== The git index file format
+
+   The git index file (.git/index) documents the status of the files
+     in the git staging area.
+
+   The staging area is used for preparing commits, merging, etc.
+
+   All binary numbers are in network byte order. Version 5 is described
+     here.
+
+   - A 20-byte header consisting of
+
+     sig (32-bits): Signature:
+       The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache")
+
+     vnr (32-bits): Version number:
+       The current supported versions are 2, 3, 4 and 5.
+
+     ndir (32-bits): number of directories in the index.
+
+     nfile (32-bits): number of file entries in the index.
+
+     fblockoffset (32-bits): offset to the file block, relative to the
+       beginning of the file.
+
+   - Offset to the extensions.
+
+     nextensions (32-bits): number of extensions.
+
+     extoffset (32-bits): offset to the extension. (Possibly none, as
+       many as indicated in the 4-byte number of extensions)
+
+     headercrc (32-bits): crc checksum for the header and extension
+       offsets
+
+   - diroffsets (ndir * directory offsets): A directory offset for each
+       of the ndir directories in the index, sorted by pathname (of the
+       directory it's pointing to) (see below). The diroffsets are relative
+       to the beginning of the direntries block. [1]
+
+   - direntries (ndir * directory entries): A directory entry for each
+       of the ndir directories in the index, sorted by pathname (see
+       below). [2]
+
+   - fileoffsets (nfile * file offsets): A file offset for each of the
+       nfile files in the index (see below). The file offsets are relative
+       to the beginning of the fileentries block. [1]
+
+   - fileentries (nfile * file entries): A file entry for each of the
+       nfile files in the index (see below).
+
+   - crdata: A number of entries for conflicted data/resolved conflicts
+       (see below).
+
+   - Extensions (Currently none, see below in the future)
+
+     Extensions are identified by signature. Optional extensions can
+     be ignored if GIT does not understand them.
+
+     GIT supports an arbitrary number of extension, but currently none
+     is implemented. [3]
+
+     extsig (32-bits): extension signature. If the first byte is 'A'..'Z'
+     the extension is optional and can be ignored.
+
+     extsize (32-bits): size of the extension, excluding the header
+       (extsig, extsize, extchecksum).
+
+     extchecksum (32-bits): crc32 checksum of the extension signature
+       and size.
+
+    - Extension data.
+
+
+== Directory offsets (diroffsets)
+
+  diroffset (32-bits): offset to the directory relative to the beginning
+    of the index file. There are ndir + 1 offsets in the diroffset table,
+    the last is pointing to the end of the last direntry. With this last
+    entry, we can replace the strlen when reading each filename, by
+    calculating its length with the offsets.
+
+  This part is needed for making the directory entries bisectable and
+    thus allowing a binary search.
+
+== Directory entry (direntries)
+  
+  Directory entries are sorted in lexicographic order by the name 
+    of their path starting with the root.
+  
+  pathname (variable length, nul terminated): relative to top level
+    directory (without the leading slash). '/' is used as path
+    separator. A string of length 0 ('') indicates the root directory.
+    The special path components ".", and ".." (without quotes) are
+    disallowed. The path also includes a trailing slash. [9]
+
+  foffset (32-bits): offset to the lexicographically first file in 
+    the file offsets (fileoffsets), relative to the beginning of
+    the fileoffset block.
+
+  cr (32-bits): offset to conflicted/resolved data at the end of the
+    index. 0 if there is no such data. [4]
+
+  ncr (32-bits): number of conflicted/resolved data entries at the
+    end of the index if the offset is non 0. If cr is 0, ncr is
+    also 0.
+
+  nsubtrees (32-bits): number of subtrees this tree has in the index.
+
+  nfiles (32-bits): number of files in the directory, that are in
+    the index.
+
+  nentries (32-bits): number of entries in the index that is covered
+    by the tree this entry represents. (-1 if the entry is invalid).
+    This number includes all the files in this tree, recursively.
+
+  objname (160-bits): object name for the object that would result
+    from writing this span of index as a tree. This is only valid
+    if nentries is valid, meaning the cache-tree is valid.
+
+  flags (16-bits): 'flags' field split into (high to low bits) (For
+    D/F conflicts)
+    
+    stage (2-bits): stage of the directory during merge
+
+    14-bit unused
+
+  dircrc (32-bits): crc32 checksum for each directory entry.
+
+  The last 24 bytes (4-byte number of entries + 160-bit object name) are
+    for the cache tree. An entry can be in an invalidated state which is
+    represented by having -1 in the entry_count field.
+
+  The entries are written out in the top-down, depth-first order. The
+    first entry represents the root level of the repository, followed by
+    the first subtree - let's call it A - of the root level, followed by
+    the first subtree of A, ... There is no prefix compression for
+    directories.
+
+== File offsets (fileoffsets)
+
+  fileoffset (32-bits): offset to the file.
+
+  This part is needed for making the file entries bisectable and
+    thus allowing a binary search. There are nfile + 1 offsets in the
+    fileoffset table, the last is pointing to the end of the last
+    fileentry. With this last entry, we can replace the strlen when
+    reading each filename, by calculating its length with the offsets.
+
+== File entry (fileentries)
+  
+  File entries are sorted in ascending order on the name field, after the
+  respective offset given by the directory entries. All file names are
+  prefix compressed, meaning the file name is relative to the directory.
+
+  filename (variable length, nul terminated). The exact encoding is 
+    undefined, but the filename cannot contain a NUL byte (iow, the same
+    encoding as a UNIX pathname).
+
+  flags (16-bits): 'flags' field split into (high to low bits)
+
+    assumevalid (1-bit): assume-valid flag
+
+    intenttoadd (1-bit): intent-to-add flag, used by "git add -N".
+      Extended flag in index v3.
+
+    stage (2-bit): stage of the file during merge
+
+    skipworktree (1-bit): skip-worktree flag, used by sparse checkout.
+      Extended flag in index v3.
+
+    11-bit unused, must be zero [6]
+
+  mode (16-bits): file mode, split into (high to low bits)
+
+    objtype (4-bits): object type
+      valid values in binary are 1000 (regular file), 1010 (symbolic
+      link) and 1110 (gitlink)
+
+    3-bit unused
+
+    permission (9-bits): unix permission. Only 0755 and 0644 are valid
+      for regular files. Symbolic links and gitlinks have value 0 in 
+      this field.
+
+  mtimes (32-bits): mtime seconds, the last time a file's data changed
+    this is stat(2) data
+
+  mtimens (32-bits): mtime nanosecond fractions
+    this is stat(2) data
+
+  file size (32-bits): The on-disk size, trucated to 32-bit.
+    this is stat(2) data
+
+  statcrc (32-bits): crc32 checksum over ctime seconds, ctime
+    nanoseconds, ino, dev, uid, gid (All stat(2) data
+    except mtime and file size). If the statcrc is 0 it will
+    be ignored. [7]
+
+  objhash (160-bits): SHA-1 for the represented object
+
+  entrycrc (32-bits): crc32 checksum for the file entry. The crc code
+    includes the offset to the offset to the file, relative to the
+    beginning of the file.
+
+== Conflict data
+
+  A conflict is represented in the index as a set of higher stage entries.
+  These entries are stored at the end of the index. When a conflict is 
+  resolved (e.g. with "git add path"). A bit is flipped, to indicate that
+  the conflict is resolved, but the entries will be kept, so that
+  conflicts can be recreated (e.g. with "git checkout -m", in case users
+  want to redo a conflict resolution from scratch.
+
+  The first part of a conflict (usually stage 1) will be stored both in
+  the entries part of the index and in the conflict part. All other parts
+  will only be stored in the conflict part.
+
+  filename (variable length, nul terminated): filename of the entry,
+    relative to its containing directory).
+
+  nfileconflicts (32-bits): number of conflicts for the file [8]
+
+  flags (nfileconflicts * flags) (16-bits): 'flags' field split into:
+    
+    conflicted (1-bit): conflicted state (conflicted/resolved) (1 if
+      conflicted)
+
+    stage (2-bits): stage during merge.
+   
+    13-bit unused
+
+  entry_mode (nfileconflicts * entry mode) (16-bits): octal numbers, entry
+    mode of eache entry in the different stages. (How many is defined by
+    the 4-byte number before)
+
+  objectnames (nfileconflicts * object names) (160-bits): object names 
+    of the different stages.
+
+  conflictcrc (32-bits): crc32 checksum over conflict data.
+
+== Design explanations
+
+[1] The directory and file offsets are included in the index format
+    to enable bisectability of the index, for binary searches.Updating
+    a single entry and partial reading will benefit from this.
+
+[2] The directories are saved in their own block, to be able to
+    quickly search for a directory in the index. They include a
+    offset to the (lexically) first file in the directory.
+
+[3] The data of the cache-tree extension and the resolve undo
+    extension is now part of the index itself, but if other extensions
+    come up in the future, there is no need to change the index, they
+    can simply be added at the end.
+
+[4] To avoid rewrites of the whole index when there are conflicts or
+    conflicts are being resolved, conflicted data will be stored at
+    the end of the index. To mark the conflict resolved, just a bit
+    has to be flipped. The data will still be there, if a user wants
+    to redo the conflict resolution.
+
+[5] Since only 4 modes are effectively allowed in git but 32-bit are
+    used to store them, having a two bit flag for the mode is enough
+    and saves 4 byte per entry.
+
+[6] The length of the file name was dropped, since each file name is
+    nul terminated anyway.
+
+[7] Since all stat data (except mtime and ctime) is just used for
+    checking if a file has changed a checksum of the data is enough.
+    In addition to that Thomas Rast suggested ctime could be ditched
+    completely (core.trustctime=false) and thus included in the
+    checksum. This would save 24 bytes per index entry, which would
+    be about 4 MB on the Webkit index.
+    (Thanks for the suggestion to Michael Haggerty)
+
+[8] Since there can be more stage #1 entries, it is necessary to know
+    the number of conflict data entries there are.
+
+[9] As Michael Haggerty pointed out on the mailing list, storing the
+    trailing slash will simplify a few operations.
Signature · 1 line
1.7.10.GIT
#14 lore raw

Re: [PATCH/RFC v3 04/13] Add documentation of the index-v5 file format

Documentation/technical/index-file-format-v5.txt line 9

Quoted 9 lines · Thomas Gummerer
Documentation/technical/index-file-format-v5.txt new file +9 -0
+GIT index format
+================
+
+== The git index file format
+
+   The git index file (.git/index) documents the status of the files
+     in the git staging area.
+
+   The staging area is used for preparing commits, merging, etc.

The above two are not about "index file format". It is an explanation of what the index is.

Documentation/technical/index-file-format-v5.txt line 12

Quoted 2 lines
Documentation/technical/index-file-format-v5.txt new file +2 -0
+   All binary numbers are in network byte order. Version 5 is described
+     here.

I had to read between these two lines something like

""The index file consists of various sections; the sections appear in the following order in the file."""

to make sense of the document.

Quoted 4 lines
Documentation/technical/index-file-format-v5.txt new file +4 -0
+   - A 20-byte header consisting of
+
+     sig (32-bits): Signature:
+       The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache")

Documentation/technical/index-file-format-v5.txt line 27

Quoted 10 lines
Documentation/technical/index-file-format-v5.txt new file +10 -0
+
+     vnr (32-bits): Version number:
+       The current supported versions are 2, 3, 4 and 5.
+
+     ndir (32-bits): number of directories in the index.
+
+     nfile (32-bits): number of file entries in the index.
+
+     fblockoffset (32-bits): offset to the file block, relative to the
+       beginning of the file.

Ok.

Documentation/technical/index-file-format-v5.txt line 34

Quoted 6 lines
Documentation/technical/index-file-format-v5.txt new file +5 -0
+   - Offset to the extensions.
 
+     nextensions (32-bits): number of extensions.
+
+     extoffset (32-bits): offset to the extension. (Possibly none, as
+       many as indicated in the 4-byte number of extensions)

OK.

Documentation/technical/index-file-format-v5.txt line 37

Quoted 2 lines
Documentation/technical/index-file-format-v5.txt new file +2 -0
+     headercrc (32-bits): crc checksum for the header and extension
+       offsets

This may have to have the same " - <section title>" at the same level as "A 20-byte header" and "Offset to the ext"; as it stands, it looks as if it is part of "Offset to the ext" which consists of 12 bytes.

Documentation/technical/index-file-format-v5.txt line 42

Quoted 4 lines
Documentation/technical/index-file-format-v5.txt new file +4 -0
+   - diroffsets (ndir * directory offsets): A directory offset for each
+       of the ndir directories in the index, sorted by pathname (of the
+       directory it's pointing to) (see below). The diroffsets are relative
+       to the beginning of the direntries block. [1]

"ndir * diroffsets" confused me. I think you meant to say that this "diroffsets" section consists of ndir entries of something and that each of that something is a directory offset. It is unclear how "a directory offset" is represented, except that it is "relative to the beginning of direntry block" (and it is unclear what and where the direntry block is from the information given up to this point) and the reader can guess it is in "network byte order" (assuming it is a binary number). Perhaps

diroffsets (ndir entries of "directory offset"): A 4-byte offset relative to the beginning of the "direntries block" (see below) for each of the ...

and drop the last sentence?

Other tables may want to be adjusted in a similar fashion.

Documentation/technical/index-file-format-v5.txt line 84

Quoted 7 lines

+== Directory offsets (diroffsets)

Documentation/technical/index-file-format-v5.txt new file +6 -0
+
+  diroffset (32-bits): offset to the directory relative to the beginning
+    of the index file. There are ndir + 1 offsets in the diroffset table,
+    the last is pointing to the end of the last direntry. With this last
+    entry, we can replace the strlen when reading each filename, by
+    calculating its length with the offsets.

The mention of "strlen" looks very out of place. The reader may be able to guess that you want to say that the nth "string" is between diroffset[n] and diroffset[n+1], and these "string"s are densely packed so strlen(diroffset[n]) and diroffset[n+1]-diroffset[n] are either the same thing (or with a fixed difference, if each "string" is accompanied by some fixed-length data), but it is unclear what these "strings" represent, especially because the name of the table implies that you are talking about directories but strlen talks about filename.

Documentation/technical/index-file-format-v5.txt line 258

Quoted 6 lines

+== Design explanations
+ ...
+[3] The data of the cache-tree extension and the resolve undo

Documentation/technical/index-file-format-v5.txt new file +3 -0
+    extension is now part of the index itself, but if other extensions
+    come up in the future, there is no need to change the index, they
+    can simply be added at the end.

Interesting. When we added extensions, we said that there is no need to change the index to add new features, they can simply be added at the end. Perhaps the file offset table can be added as an extension to v2 to give us the same bisectability, allowing us a single entry in-place replacementability, without defining an entirely different format?

#15 lore raw

Re: [PATCH/RFC v3 04/13] Add documentation of the index-v5 file format

Quoted 18 lines · Junio C Hamano

Documentation/technical/index-file-format-v5.txt line 9

Quoted 9 lines · Thomas Gummerer
Documentation/technical/index-file-format-v5.txt new file +9 -0
+GIT index format
+================
+
+== The git index file format
+
+   The git index file (.git/index) documents the status of the files
+     in the git staging area.
+
+   The staging area is used for preparing commits, merging, etc.

The above two are not about "index file format". It is an explanation of what the index is.

Documentation/technical/index-file-format-v5.txt line 12

Quoted 2 lines
Documentation/technical/index-file-format-v5.txt new file +2 -0
+   All binary numbers are in network byte order. Version 5 is described
+     here.
Quoted 6 lines

I had to read between these two lines something like

""The index file consists of various sections; the sections appear in the following order in the file."""

to make sense of the document.

Thanks, I'll add that.

Quoted 26 lines
Quoted 4 lines
Documentation/technical/index-file-format-v5.txt new file +4 -0
+   - A 20-byte header consisting of
+
+     sig (32-bits): Signature:
+       The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache")

Documentation/technical/index-file-format-v5.txt line 27

Quoted 10 lines
Documentation/technical/index-file-format-v5.txt new file +10 -0
+
+     vnr (32-bits): Version number:
+       The current supported versions are 2, 3, 4 and 5.
+
+     ndir (32-bits): number of directories in the index.
+
+     nfile (32-bits): number of file entries in the index.
+
+     fblockoffset (32-bits): offset to the file block, relative to the
+       beginning of the file.

Ok.

Documentation/technical/index-file-format-v5.txt line 34

Quoted 6 lines
Documentation/technical/index-file-format-v5.txt new file +5 -0
+   - Offset to the extensions.
 
+     nextensions (32-bits): number of extensions.
+
+     extoffset (32-bits): offset to the extension. (Possibly none, as
+       many as indicated in the 4-byte number of extensions)

OK.

Quoted 7 lines

Documentation/technical/index-file-format-v5.txt line 37

Quoted 2 lines
Documentation/technical/index-file-format-v5.txt new file +2 -0
+     headercrc (32-bits): crc checksum for the header and extension
+       offsets

This may have to have the same " - <section title>" at the same level as "A 20-byte header" and "Offset to the ext"; as it stands, it looks as if it is part of "Offset to the ext" which consists of 12 bytes.

Thanks, I'll try to write it down more clearly.

Quoted 14 lines

Documentation/technical/index-file-format-v5.txt line 42

Quoted 4 lines
Documentation/technical/index-file-format-v5.txt new file +4 -0
+   - diroffsets (ndir * directory offsets): A directory offset for each
+       of the ndir directories in the index, sorted by pathname (of the
+       directory it's pointing to) (see below). The diroffsets are relative
+       to the beginning of the direntries block. [1]

"ndir * diroffsets" confused me. I think you meant to say that this "diroffsets" section consists of ndir entries of something and that each of that something is a directory offset. It is unclear how "a directory offset" is represented, except that it is "relative to the beginning of direntry block" (and it is unclear what and where the direntry block is from the information given up to this point) and the reader can guess it is in "network byte order" (assuming it is a binary number). Perhaps

Quoted 7 lines

diroffsets (ndir entries of "directory offset"): A 4-byte offset relative to the beginning of the "direntries block" (see below) for each of the ...

and drop the last sentence?

Other tables may want to be adjusted in a similar fashion.

Yes, that's what I menat to say. Thanks.

Quoted 8 lines

Documentation/technical/index-file-format-v5.txt line 84

Quoted 7 lines

+== Directory offsets (diroffsets)

Documentation/technical/index-file-format-v5.txt new file +6 -0
+
+  diroffset (32-bits): offset to the directory relative to the beginning
+    of the index file. There are ndir + 1 offsets in the diroffset table,
+    the last is pointing to the end of the last direntry. With this last
+    entry, we can replace the strlen when reading each filename, by
+    calculating its length with the offsets.
Quoted 9 lines

The mention of "strlen" looks very out of place. The reader may be able to guess that you want to say that the nth "string" is between diroffset[n] and diroffset[n+1], and these "string"s are densely packed so strlen(diroffset[n]) and diroffset[n+1]-diroffset[n] are either the same thing (or with a fixed difference, if each "string" is accompanied by some fixed-length data), but it is unclear what these "strings" represent, especially because the name of the table implies that you are talking about directories but strlen talks about filename.

Hrm maybe better like this:

+ diroffset (32-bits): offset to the directory relative to the beginning

+    of the index file. There are ndir + 1 offsets in the diroffset table,
+    the last is pointing to the end of the last direntry. With this last
+    entry, we are able to replace the strlen of when reading the directory
+    name, by calculating it from diroffset[n+1]-diroffset[n]-61.  61 is the
+    size of the directory data, which follows each each directory + the
+    crc sum + the NUL byte.
Quoted 7 lines

Documentation/technical/index-file-format-v5.txt line 258

Quoted 6 lines

+== Design explanations
+ ...
+[3] The data of the cache-tree extension and the resolve undo

Documentation/technical/index-file-format-v5.txt new file +3 -0
+    extension is now part of the index itself, but if other extensions
+    come up in the future, there is no need to change the index, they
+    can simply be added at the end.
Quoted 6 lines

Interesting. When we added extensions, we said that there is no need to change the index to add new features, they can simply be added at the end. Perhaps the file offset table can be added as an extension to v2 to give us the same bisectability, allowing us a single entry in-place replacementability, without defining an entirely different format?

Only part of this is true. v2 would allow us to add the file offset table as extension, but the problem is the design of the sha-1 over the whole file at the end. That would only allow single entry replacements, if we then re-read the file and calculate the sha-1 at the end. Partial reading also could only be implemented when reading the whole file first to check the sha-1, which defeats it's purpose.

#16 lore raw

Re: [PATCH/RFC v3 04/13] Add documentation of the index-v5 file format

In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano

Documentation/technical/index-file-format-v5.txt line 258

Quoted 6 lines

+== Design explanations
+ ...
+[3] The data of the cache-tree extension and the resolve undo

Documentation/technical/index-file-format-v5.txt new file +3 -0
+    extension is now part of the index itself, but if other extensions
+    come up in the future, there is no need to change the index, they
+    can simply be added at the end.
Quoted 6 lines

Interesting. When we added extensions, we said that there is no need to change the index to add new features, they can simply be added at the end. Perhaps the file offset table can be added as an extension to v2 to give us the same bisectability, allowing us a single entry in-place replacementability, without defining an entirely different format?

Just to avoid wasting people's time, in case they try to respond to this part which was tongue-in-cheek.

There is a valid technical reason why the above cannot be done with the original index format and why a new extension does not make sense. There is no quick way to locate the extensions section in the file without reading all the entries first, so by the time you know where the extensions are, you already know all the paths, and have enough information to build a table necessary to do the bisection without a separate data in a new extension.

#17 lore raw

PATCH v3 5/13 Make in-memory format aware of stat_crc

In reply to Thomas Gummerer

Make the in-memory format aware of the stat_crc used by index-v5. It is simply ignored by index version prior to v5.

---
 cache.h      |    1 +
 read-cache.c |   25 +++++++++++++++++++++++++
 2 files changed, 26 insertions(+)
cache.h +1 -0
index c77cdbe..bfe3099 100644
@@ -122,6 +122,7 @@ struct cache_entry {
 	unsigned int ce_flags;
 	unsigned int ce_namelen;
 	unsigned char sha1[20];
+	uint32_t ce_stat_crc;
 	struct cache_entry *next;
 	struct cache_entry *dir_next;
 	char name[FLEX_ARRAY]; /* more */
read-cache.c +25 -0
index 125e6a0..d8f8b74 100644
@@ -51,6 +51,29 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n
 	add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 }
 
+static uint32_t calculate_stat_crc(struct cache_entry *ce)
+{
+	unsigned int ctimens = 0;
+	uint32_t stat, stat_crc;
+
+	stat = htonl(ce->ce_ctime.sec);
+	stat_crc = crc32(0, (Bytef*)&stat, 4);
+#ifdef USE_NSEC
+	ctimens = ce->ce_ctime.nsec;
+#endif
+	stat = htonl(ctimens);
+	stat_crc = crc32(stat_crc, (Bytef*)&stat, 4);
+	stat = htonl(ce->ce_ino);
+	stat_crc = crc32(stat_crc, (Bytef*)&stat, 4);
+	stat = htonl(ce->ce_dev);
+	stat_crc = crc32(stat_crc, (Bytef*)&stat, 4);
+	stat = htonl(ce->ce_uid);
+	stat_crc = crc32(stat_crc, (Bytef*)&stat, 4);
+	stat = htonl(ce->ce_gid);
+	stat_crc = crc32(stat_crc, (Bytef*)&stat, 4);
+	return stat_crc;
+}
+
 /*
  * This only updates the "non-critical" parts of the directory
  * cache, ie the parts that aren't tracked by GIT, and only used
@@ -73,6 +96,8 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)
 
 	if (S_ISREG(st->st_mode))
 		ce_mark_uptodate(ce);
+
+	ce->ce_stat_crc = calculate_stat_crc(ce);
 }
 
 static int ce_compare_data(struct cache_entry *ce, struct stat *st)
Signature · 1 line
1.7.10.GIT
#18 lore raw

PATCH v3 6/13 Read index-v5

In reply to Thomas Gummerer

Make git read the index file version 5 without complaining.

This version of the reader doesn't read neither the cache-tree nor the resolve undo data, but doesn't choke on an index that includes such data.

---
 Makefile        |    1 +
 cache.h         |   72 +++++++
 read-cache-v5.c |  589 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 read-cache.c    |    1 -
 4 files changed, 662 insertions(+), 1 deletion(-)
 create mode 100644 read-cache-v5.c
Makefile +1 -0
index b4a7c73..77be175 100644
@@ -770,6 +770,7 @@ LIB_OBJS += quote.o
 LIB_OBJS += reachable.o
 LIB_OBJS += read-cache.o
 LIB_OBJS += read-cache-v2.o
+LIB_OBJS += read-cache-v5.o
 LIB_OBJS += reflog-walk.o
 LIB_OBJS += refs.o
 LIB_OBJS += remote.o
cache.h +72 -0

107 lines not on this page · raw message

read-cache-v5.c new file +589 -0

589 lines not on this page · raw message

read-cache.c +0 -1
index d8f8b74..5fb4d2e 100644
@@ -1175,7 +1175,6 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int reall
 	return refresh_cache_ent(&the_index, ce, really, NULL, NULL);
 }
 
-
 static int verify_hdr_version(struct index_state *istate,
 		struct cache_version_header *hdr, unsigned long size)
 {
Signature · 1 line
1.7.10.GIT
#19 lore raw

Re: [PATCH/RFC v3 06/13] Read index-v5

Quoted 4 lines · Thomas Gummerer
read-cache-v5.c new file +4 -0
+static struct cache_entry *read_entry(struct directory_entry *de,
+                       unsigned long *entry_offset,
+                       void **mmap,
+                       unsigned long mmap_size,

read-cache-v5.c line 303

Quoted 10 lines
read-cache-v5.c new file +9 -0
+                       unsigned int *foffsetblock,
+                       int fd)
+{
....
+               if (crc_wrong) {
+                       /* wait for 10 milliseconds */
+                       usleep(10*1000);
+                       munmap(*mmap, mmap_size);
+                       *mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+               }

Do we really need to munmap and mmap again? I don't see mmap man page mention anything about "refreshing" the mmap'd memory with file changes, not sure how it works. msync() seems for writing only.

If remapping is necessary, how about mremap? What I want to see is whether we could avoid passing "fd" down to here.

read-cache-v5.c line 589

Quoted 6 lines
read-cache-v5.c new file +6 -0
+struct index_ops v5_ops = {
+       match_stat_basic,
+       verify_hdr,
+       read_index_v5,
+       NULL
+};

If you do it right, putting write_index_v2 here should work because in-core structure is not changed (except that write_index_v2 is static function, well..). Maybe putting write_index to this struct is a wrong decision. We should be able to read_index_v5+write_index_v2 and read_index_v2+write_index_v5.

Signature · 1 line
Duy
#20 lore raw

Re: [PATCH/RFC v3 06/13] Read index-v5

Am 8/8/2012 14:05, schrieb Nguyen Thai Ngoc Duy:

Quoted 17 lines
Quoted 4 lines · Thomas Gummerer
read-cache-v5.c new file +4 -0
+static struct cache_entry *read_entry(struct directory_entry *de,
+                       unsigned long *entry_offset,
+                       void **mmap,
+                       unsigned long mmap_size,

read-cache-v5.c line 303

Quoted 10 lines
read-cache-v5.c new file +9 -0
+                       unsigned int *foffsetblock,
+                       int fd)
+{
....
+               if (crc_wrong) {
+                       /* wait for 10 milliseconds */
+                       usleep(10*1000);
+                       munmap(*mmap, mmap_size);
+                       *mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+               }

Do we really need to munmap and mmap again?

Yes. mmap may be the pread()-based implementation from compat/mmap.c.

-- Hannes

#21 lore raw

Re: [PATCH/RFC v3 06/13] Read index-v5

In reply to Nguyen Thai Ngoc Duy
Quoted 4 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines
read-cache-v5.c new file +4 -0
+struct index_ops v5_ops = {
+       match_stat_basic,
+       verify_hdr,
+       read_index_v5,
Quoted 8 lines

read-cache-v5.c line 589

Quoted 2 lines
read-cache-v5.c new file +2 -0
+       NULL
+};

If you do it right, putting write_index_v2 here should work because in-core structure is not changed (except that write_index_v2 is static function, well..). Maybe putting write_index to this struct is a wrong decision. We should be able to read_index_v5+write_index_v2 and read_index_v2+write_index_v5.

The "right way" is to have a global API function

    write_index_in_format(int version, int fd);

which calls "the_index->index_ops.write_index[version](the_index, fd)", no?

#22 lore raw

Re: [PATCH/RFC v3 06/13] Read index-v5

In reply to Nguyen Thai Ngoc Duy
Quoted 23 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines · Thomas Gummerer
read-cache-v5.c new file +4 -0
+static struct cache_entry *read_entry(struct directory_entry *de,
+                       unsigned long *entry_offset,
+                       void **mmap,
+                       unsigned long mmap_size,

read-cache-v5.c line 303

Quoted 10 lines
read-cache-v5.c new file +9 -0
+                       unsigned int *foffsetblock,
+                       int fd)
+{
....
+               if (crc_wrong) {
+                       /* wait for 10 milliseconds */
+                       usleep(10*1000);
+                       munmap(*mmap, mmap_size);
+                       *mmap = xmmap(NULL, mmap_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
+               }

Do we really need to munmap and mmap again? I don't see mmap man page mention anything about "refreshing" the mmap'd memory with file changes, not sure how it works. msync() seems for writing only.

If remapping is necessary, how about mremap? What I want to see is whether we could avoid passing "fd" down to here.

Quoted 12 lines

read-cache-v5.c line 589

Quoted 6 lines
read-cache-v5.c new file +6 -0
+struct index_ops v5_ops = {
+       match_stat_basic,
+       verify_hdr,
+       read_index_v5,
+       NULL
+};

If you do it right, putting write_index_v2 here should work because in-core structure is not changed (except that write_index_v2 is static function, well..). Maybe putting write_index to this struct is a wrong decision. We should be able to read_index_v5+write_index_v2 and read_index_v2+write_index_v5.

Hrm I don't think it should be a problem putting write_index_v2 here, but the way I currently handle it is by replacing the whole index_ops. I think it's a good thing that the index_ops version always matches the index version, to avoid any problems there. The only place where this can happen in the current code is in update-index.c when we change the index version.

#23 lore raw

PATCH v3 7/13 Read resolve-undo data

In reply to Thomas Gummerer

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 is then converted to the in-memory format.

---
 read-cache-v5.c |    1 +
 resolve-undo.c  |   36 ++++++++++++++++++++++++++++++++++++
 resolve-undo.h  |    2 ++
 3 files changed, 39 insertions(+)
read-cache-v5.c +1 -0
index ec1201d..b47398d 100644
@@ -494,6 +494,7 @@ static struct directory_entry *read_entries(struct index_state *istate,
 	int i;
 
 	conflict_queue = read_conflicts(de, mmap, mmap_size, fd);
+	resolve_undo_convert_v5(istate, conflict_queue);
 	for (i = 0; i < de->de_nfiles; i++) {
 		ce = read_entry(de,
 				entry_offset,
resolve-undo.c +36 -0
index 72b4612..f96c6ba 100644
@@ -170,3 +170,39 @@ void unmerge_index(struct index_state *istate, const char **pathspec)
 		i = unmerge_index_entry_at(istate, i);
 	}
 }
+
+void resolve_undo_convert_v5(struct index_state *istate,
+					struct conflict_entry *ce)
+{
+	int i;
+
+	while (ce) {
+		struct string_list_item *lost;
+		struct resolve_undo_info *ui;
+		struct conflict_part *cp;
+
+		if (ce->entries && (ce->entries->flags & CONFLICT_CONFLICTED) != 0) {
+			ce = ce->next;
+			continue;
+		}
+		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, ce->name);
+		if (!lost->util)
+			lost->util = xcalloc(1, sizeof(*ui));
+		ui = lost->util;
+
+		cp = ce->entries;
+		for (i = 0; i < 3; i++)
+			ui->mode[i] = 0;
+		while (cp) {
+			ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
+			hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
+			cp = cp->next;
+		}
+		ce = ce->next;
+	}
+}
resolve-undo.h +2 -0
index 8458769..ab660a6 100644
@@ -13,4 +13,6 @@ extern void resolve_undo_clear_index(struct index_state *);
 extern int unmerge_index_entry_at(struct index_state *, int);
 extern void unmerge_index(struct index_state *, const char **);
 
+extern void resolve_undo_convert_v5(struct index_state *, struct conflict_entry *);
+
 #endif
Signature · 1 line
1.7.10.GIT
#24 lore raw

Re: [PATCH/RFC v3 07/13] Read resolve-undo data

Quoted 6 lines · Thomas Gummerer

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 is then converted to the in-memory format.

This, and the next one, are both about reading extension data from the v2 formatted index, no?

Again, mild NAK.

I think it is a lot more logical for the v5 code to read data stored in the resolve-undo and cache-tree extensions using the public API just like other users of these data do, and write out whatever in a way that is specific to the v5 index format.

If the v5 codepath needs some information that is not exposed to other users of istate->resolve_undo and istate->cache_tree, then the story is different, but I do not think that is the case.

Quoted 76 lines
---
 read-cache-v5.c |    1 +
 resolve-undo.c  |   36 ++++++++++++++++++++++++++++++++++++
 resolve-undo.h  |    2 ++
 3 files changed, 39 insertions(+)
read-cache-v5.c +1 -0
index ec1201d..b47398d 100644
@@ -494,6 +494,7 @@ static struct directory_entry *read_entries(struct index_state *istate,
 	int i;
 
 	conflict_queue = read_conflicts(de, mmap, mmap_size, fd);
+	resolve_undo_convert_v5(istate, conflict_queue);
 	for (i = 0; i < de->de_nfiles; i++) {
 		ce = read_entry(de,
 				entry_offset,
resolve-undo.c +36 -0
index 72b4612..f96c6ba 100644
@@ -170,3 +170,39 @@ void unmerge_index(struct index_state *istate, const char **pathspec)
 		i = unmerge_index_entry_at(istate, i);
 	}
 }
+
+void resolve_undo_convert_v5(struct index_state *istate,
+					struct conflict_entry *ce)
+{
+	int i;
+
+	while (ce) {
+		struct string_list_item *lost;
+		struct resolve_undo_info *ui;
+		struct conflict_part *cp;
+
+		if (ce->entries && (ce->entries->flags & CONFLICT_CONFLICTED) != 0) {
+			ce = ce->next;
+			continue;
+		}
+		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, ce->name);
+		if (!lost->util)
+			lost->util = xcalloc(1, sizeof(*ui));
+		ui = lost->util;
+
+		cp = ce->entries;
+		for (i = 0; i < 3; i++)
+			ui->mode[i] = 0;
+		while (cp) {
+			ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
+			hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
+			cp = cp->next;
+		}
+		ce = ce->next;
+	}
+}
resolve-undo.h +2 -0
index 8458769..ab660a6 100644
@@ -13,4 +13,6 @@ extern void resolve_undo_clear_index(struct index_state *);
 extern int unmerge_index_entry_at(struct index_state *, int);
 extern void unmerge_index(struct index_state *, const char **);
 
+extern void resolve_undo_convert_v5(struct index_state *, struct conflict_entry *);
+
 #endif
#25 lore raw

Re: [PATCH/RFC v3 07/13] Read resolve-undo data

Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Thomas Gummerer

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 is then converted to the in-memory format.

This, and the next one, are both about reading extension data from the v2 formatted index, no?

Yes, exactly.

Quoted 10 lines

Again, mild NAK.

I think it is a lot more logical for the v5 code to read data stored in the resolve-undo and cache-tree extensions using the public API just like other users of these data do, and write out whatever in a way that is specific to the v5 index format.

If the v5 codepath needs some information that is not exposed to other users of istate->resolve_undo and istate->cache_tree, then the story is different, but I do not think that is the case.

Sorry it's not clear to me what you mean with using the public API here. Do you mean using resolve_undo_write() and resolve_undo_read()? I wouldn't think those two methods would be really useful, as they expect the data mangled in to a char* or return it as struct strbuf*. And I don't see the other methods doing something more useful. Or I could use the resolve-undo string_list directly, and just move the function to read-cache-v5.c, or am I missing something here?

Quoted 76 lines
Quoted 76 lines
---
 read-cache-v5.c |    1 +
 resolve-undo.c  |   36 ++++++++++++++++++++++++++++++++++++
 resolve-undo.h  |    2 ++
 3 files changed, 39 insertions(+)
read-cache-v5.c +1 -0
index ec1201d..b47398d 100644
@@ -494,6 +494,7 @@ static struct directory_entry *read_entries(struct index_state *istate,
 	int i;
 
 	conflict_queue = read_conflicts(de, mmap, mmap_size, fd);
+	resolve_undo_convert_v5(istate, conflict_queue);
 	for (i = 0; i < de->de_nfiles; i++) {
 		ce = read_entry(de,
 				entry_offset,
resolve-undo.c +36 -0
index 72b4612..f96c6ba 100644
@@ -170,3 +170,39 @@ void unmerge_index(struct index_state *istate, const char **pathspec)
 		i = unmerge_index_entry_at(istate, i);
 	}
 }
+
+void resolve_undo_convert_v5(struct index_state *istate,
+					struct conflict_entry *ce)
+{
+	int i;
+
+	while (ce) {
+		struct string_list_item *lost;
+		struct resolve_undo_info *ui;
+		struct conflict_part *cp;
+
+		if (ce->entries && (ce->entries->flags & CONFLICT_CONFLICTED) != 0) {
+			ce = ce->next;
+			continue;
+		}
+		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, ce->name);
+		if (!lost->util)
+			lost->util = xcalloc(1, sizeof(*ui));
+		ui = lost->util;
+
+		cp = ce->entries;
+		for (i = 0; i < 3; i++)
+			ui->mode[i] = 0;
+		while (cp) {
+			ui->mode[conflict_stage(cp) - 1] = cp->entry_mode;
+			hashcpy(ui->sha1[conflict_stage(cp) - 1], cp->sha1);
+			cp = cp->next;
+		}
+		ce = ce->next;
+	}
+}
resolve-undo.h +2 -0
index 8458769..ab660a6 100644
@@ -13,4 +13,6 @@ extern void resolve_undo_clear_index(struct index_state *);
 extern int unmerge_index_entry_at(struct index_state *, int);
 extern void unmerge_index(struct index_state *, const char **);
 
+extern void resolve_undo_convert_v5(struct index_state *, struct conflict_entry *);
+
 #endif
#26 lore raw

Re: [PATCH/RFC v3 07/13] Read resolve-undo data

Quoted 22 lines · Thomas Gummerer
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Thomas Gummerer

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 is then converted to the in-memory format.

This, and the next one, are both about reading extension data from the v2 formatted index, no?

Yes, exactly.

Quoted 7 lines

Again, mild NAK.

I think it is a lot more logical for the v5 code to read data stored in the resolve-undo and cache-tree extensions using the public API just like other users of these data do, and write out whatever in a way that is specific to the v5 index format.

Quoted 6 lines
Quoted 3 lines

If the v5 codepath needs some information that is not exposed to other users of istate->resolve_undo and istate->cache_tree, then the story is different, but I do not think that is the case.

Sorry it's not clear to me what you mean with using the public API here. Do you mean using resolve_undo_write() and resolve_undo_read()?

The code that reads from istate->resolve_undo is fine to do the v5 specific conversion, but it does not belong to resolve-undo.c file which is about the resolve-undo extension. Moving it to v5 specific file you added for this topic, read-cache-v5.c, and everything looks more logical. When we taught ls-files to show the paths with resolve-undo data, we didn't add any function to resolve-undo.c that does ls-files's work for it. Instead, ls-files just uses the public API (the data structure you find at the_index.resolve_undo is part of the API) to find what it needs to learn, and I think v5 code can do the same.

"then the story is different" comment refers to a possibilty that v5 code might need something more than callers outside resolve-undo.c can find from its public interface, but I do not think it is the case.

#27 lore raw

Re: [PATCH/RFC v3 07/13] Read resolve-undo data

Quoted 42 lines · Junio C Hamano
Quoted 22 lines · Thomas Gummerer
Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Thomas Gummerer

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 is then converted to the in-memory format.

This, and the next one, are both about reading extension data from the v2 formatted index, no?

Yes, exactly.

Quoted 7 lines

Again, mild NAK.

I think it is a lot more logical for the v5 code to read data stored in the resolve-undo and cache-tree extensions using the public API just like other users of these data do, and write out whatever in a way that is specific to the v5 index format.

Quoted 6 lines
Quoted 3 lines

If the v5 codepath needs some information that is not exposed to other users of istate->resolve_undo and istate->cache_tree, then the story is different, but I do not think that is the case.

Sorry it's not clear to me what you mean with using the public API here. Do you mean using resolve_undo_write() and resolve_undo_read()?

The code that reads from istate->resolve_undo is fine to do the v5 specific conversion, but it does not belong to resolve-undo.c file which is about the resolve-undo extension. Moving it to v5 specific file you added for this topic, read-cache-v5.c, and everything looks more logical. When we taught ls-files to show the paths with resolve-undo data, we didn't add any function to resolve-undo.c that does ls-files's work for it. Instead, ls-files just uses the public API (the data structure you find at the_index.resolve_undo is part of the API) to find what it needs to learn, and I think v5 code can do the same.

Quoted 4 lines

"then the story is different" comment refers to a possibilty that v5 code might need something more than callers outside resolve-undo.c can find from its public interface, but I do not think it is the case.

Ok, thanks for the clarification, will change it for the re-roll.

#28 lore raw

PATCH v3 8/13 Read cache-tree in index-v5

In reply to Thomas Gummerer

Since the cache-tree data is saved as part of the directory data, we already read it at the beginning of the index. The cache-tree is only converted from this directory data.

The cache-tree data is arranged in a tree, with the children sorted by pathlen at each node, while the ondisk format is sorted lexically. So we have to rebuild this format from the on-disk directory list.

---
 cache-tree.c    |   93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 cache-tree.h    |   10 ++++++
 read-cache-v5.c |    1 +
 3 files changed, 104 insertions(+)
cache-tree.c +93 -0
index 28ed657..440cd04 100644
@@ -519,6 +519,99 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size)
 	return read_one(&buffer, &size);
 }
 
+static struct cache_tree *convert_one(struct directory_queue *queue, int dirnr)
+{
+	int i, subtree_nr;
+	struct cache_tree *it;
+	struct directory_queue *down;
+
+	it = cache_tree();
+	it->entry_count = queue[dirnr].de->de_nentries;
+	subtree_nr = queue[dirnr].de->de_nsubtrees;
+	if (0 <= it->entry_count)
+		hashcpy(it->sha1, queue[dirnr].de->sha1);
+
+	/*
+	* Just a heuristic -- we do not add directories that often but
+	* we do not want to have to extend it immediately when we do,
+	* hence +2.
+	*/
+	it->subtree_alloc = subtree_nr + 2;
+	it->down = xcalloc(it->subtree_alloc, sizeof(struct cache_tree_sub *));
+	down = queue[dirnr].down;
+	for (i = 0; i < subtree_nr; i++) {
+		struct cache_tree *sub;
+		struct cache_tree_sub *subtree;
+		char *buf, *name;
+
+		name = "";
+		buf = strtok(down[i].de->pathname, "/");
+		while (buf) {
+			name = buf;
+			buf = strtok(NULL, "/");
+		}
+		sub = convert_one(down, i);
+		if(!sub)
+			goto free_return;
+		subtree = cache_tree_sub(it, name);
+		subtree->cache_tree = sub;
+	}
+	if (subtree_nr != it->subtree_nr)
+		die("cache-tree: internal error");
+	return it;
+ free_return:
+	cache_tree_free(&it);
+	return NULL;
+}
+
+static int compare_cache_tree_elements(const void *a, const void *b)
+{
+	const struct directory_entry *de1, *de2;
+
+	de1 = ((const struct directory_queue *)a)->de;
+	de2 = ((const struct directory_queue *)b)->de;
+	return subtree_name_cmp(de1->pathname, de1->de_pathlen,
+				de2->pathname, de2->de_pathlen);
+}
+
+static struct directory_entry *sort_directories(struct directory_entry *de,
+						struct directory_queue *queue)
+{
+	int i, nsubtrees;
+
+	nsubtrees = de->de_nsubtrees;
+	for (i = 0; i < nsubtrees; i++) {
+		struct directory_entry *new_de;
+		de = de->next;
+		new_de = xmalloc(directory_entry_size(de->de_pathlen));
+		memcpy(new_de, de, directory_entry_size(de->de_pathlen));
+		queue[i].de = new_de;
+		if (de->de_nsubtrees) {
+			queue[i].down = xcalloc(de->de_nsubtrees,
+					sizeof(struct directory_queue));
+			de = sort_directories(de,
+					queue[i].down);
+		}
+	}
+	qsort(queue, nsubtrees, sizeof(struct directory_queue),
+			compare_cache_tree_elements);
+	return de;
+}
+
+struct cache_tree *cache_tree_convert_v5(struct directory_entry *de)
+{
+	struct directory_queue *queue;
+
+	if (!de->de_nentries)
+		return NULL;
+	queue = xcalloc(1, sizeof(struct directory_queue));
+	queue[0].de = de;
+	queue[0].down = xcalloc(de->de_nsubtrees, sizeof(struct directory_queue));
+
+	sort_directories(de, queue[0].down);
+	return convert_one(queue, 0);
+}
+
 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
 {
 	if (!it)
cache-tree.h +10 -0
index d8cb2e9..7f29d26 100644
@@ -20,6 +20,11 @@ struct cache_tree {
 	struct cache_tree_sub **down;
 };
 
+struct directory_queue {
+	struct directory_queue *down;
+	struct directory_entry *de;
+};
+
 struct cache_tree *cache_tree(void);
 void cache_tree_free(struct cache_tree **);
 void cache_tree_invalidate_path(struct cache_tree *, const char *);
@@ -27,6 +32,11 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *, const char *);
 
 void cache_tree_write(struct strbuf *, struct cache_tree *root);
 struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
+/*
+ * This function modifys the directory argument that is given to it.
+ * Don't use it if the directory entries are still needed after.
+ */
+struct cache_tree *cache_tree_convert_v5(struct directory_entry *de);
 
 int cache_tree_fully_valid(struct cache_tree *);
 int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
read-cache-v5.c +1 -0
index b47398d..57d0fb5 100644
@@ -580,6 +580,7 @@ static void read_index_v5(struct index_state *istate, void *mmap, int mmap_size,
 	while (de)
 		de = read_entries(istate, de, &entry_offset,
 				&mmap, mmap_size, &nr, &foffsetblock, fd);
+	istate->cache_tree = cache_tree_convert_v5(root_directory);
 }
 
 struct index_ops v5_ops = {
Signature · 1 line
1.7.10.GIT
#29 lore raw

PATCH v3 9/13 Write index-v5

In reply to Thomas Gummerer

Write the index version 5 file format to disk. This version doesn't write the cache-tree data and resolve-undo data to the file.

The main work is done when filtering out the directories from the current in-memory format, where in the same turn also the conflicts and the file data is calculated.

---
 cache.h         |   10 +-
 read-cache-v5.c |  589 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 read-cache.c    |   19 +-
 read-cache.h    |    3 +
 4 files changed, 611 insertions(+), 10 deletions(-)
cache.h +9 -1
index a0a1781..3fa348d 100644
@@ -98,7 +98,7 @@ unsigned long git_deflate_bound(git_zstream *, unsigned long);
 #define CACHE_SIGNATURE 0x44495243	/* "DIRC" */
 
 #define INDEX_FORMAT_LB 2
-#define INDEX_FORMAT_UB 4
+#define INDEX_FORMAT_UB 5
 
 /*
  * The "cache_time" is just the low 32 bits of the
@@ -510,6 +510,7 @@ extern int verify_path(const char *path);
 extern struct cache_entry *index_name_exists(struct index_state *istate, const char *name, int namelen, int igncase);
 extern int index_name_stage_pos(const struct index_state *, const char *name, int namelen, int stage);
 extern int index_name_pos(const struct index_state *, const char *name, int namelen);
+extern struct directory_entry *init_directory_entry(char *pathname, int len);
 #define ADD_CACHE_OK_TO_ADD 1		/* Ok to add */
 #define ADD_CACHE_OK_TO_REPLACE 2	/* Ok to replace file/directory */
 #define ADD_CACHE_SKIP_DFCHECK 4	/* Ok to skip DF conflict checks */
@@ -1244,6 +1245,13 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
 	return write_in_full(fd, str, strlen(str));
 }
 
+/* index-v5 helper functions */
+extern char *super_directory(const char *filename);
+extern void insert_directory_entry(struct directory_entry *, struct hash_table *, int *, unsigned int *, uint32_t);
+extern void add_conflict_to_directory_entry(struct directory_entry *, struct conflict_entry *);
+extern void add_part_to_conflict_entry(struct directory_entry *, struct conflict_entry *, struct conflict_part *);
+extern struct conflict_entry *create_new_conflict(char *, int, int);
+
 /* pager.c */
 extern void setup_pager(void);
 extern const char *pager_program;
read-cache-v5.c +588 -1

597 lines not on this page · raw message

read-cache.c +11 -8
index 5fb4d2e..199ba75 100644
@@ -51,7 +51,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n
 	add_index_entry(istate, new, ADD_CACHE_OK_TO_ADD|ADD_CACHE_OK_TO_REPLACE);
 }
 
-static uint32_t calculate_stat_crc(struct cache_entry *ce)
+uint32_t calculate_stat_crc(struct cache_entry *ce)
 {
 	unsigned int ctimens = 0;
 	uint32_t stat, stat_crc;
@@ -172,13 +172,15 @@ int ce_modified_check_fs(struct cache_entry *ce, struct stat *st)
 	return 0;
 }
 
-static void check_set_istate_ops(struct index_state *istate)
+void set_istate_ops(struct index_state *istate)
 {
 	if (!istate->version)
 		istate->version = INDEX_FORMAT_DEFAULT;
-	if (!istate->ops)
-		if (istate->version >= 2 && istate->version <=4)
-			istate->ops = &v2_ops;
+
+	if (istate->version >= 2 && istate->version <=4)
+		istate->ops = &v2_ops;
+	if (istate->version == 5)
+		istate->ops = &v5_ops;
 }
 
 int ce_match_stat_basic(struct index_state *istate,
@@ -215,7 +217,7 @@ int ce_match_stat_basic(struct index_state *istate,
 		die("internal error: ce_mode is %o", ce->ce_mode);
 	}
 
-	check_set_istate_ops(istate);
+	set_istate_ops(istate);
 	changed = istate->ops->match_stat_basic(ce, st, changed);
 	return changed;
 }
@@ -1185,6 +1187,8 @@ static int verify_hdr_version(struct index_state *istate,
 	hdr_version = ntohl(hdr->hdr_version);
 	if (hdr_version >= 2 && hdr_version <= 4)
 		istate->ops = &v2_ops;
+	else if (hdr_version == 5)
+		istate->ops = &v5_ops;
 	else
 		return error("bad index version %d", hdr_version);
 	return 0;
@@ -1306,10 +1310,9 @@ void update_index_if_able(struct index_state *istate, struct lock_file *lockfile
 	else
 		rollback_lock_file(lockfile);
 }
-
 int write_index(struct index_state *istate, int newfd)
 {
-	check_set_istate_ops(istate);
+	set_istate_ops(istate);
 
 	return istate->ops->write_index(istate, newfd);
 }
read-cache.h +3 -0
index 78de6a2..aa920ef 100644
@@ -29,6 +29,7 @@ struct index_ops {
 };
 
 extern struct index_ops v2_ops;
+extern struct index_ops v5_ops;
 
 #ifndef NEEDS_ALIGNED_ACCESS
 #define ntoh_s(var) ntohs(var)
@@ -55,3 +56,5 @@ extern int ce_match_stat_basic(struct index_state *istate,
 		struct cache_entry *ce, struct stat *st);
 extern int is_racy_timestamp(const struct index_state *istate, struct cache_entry *ce);
 extern void set_index_entry(struct index_state *istate, int nr, struct cache_entry *ce);
+extern uint32_t calculate_stat_crc(struct cache_entry *ce);
+extern void set_istate_ops(struct index_state *istate);
Signature · 1 line
1.7.10.GIT
#30 lore raw

PATCH v3 10/13 Write index-v5 cache-tree data

In reply to Thomas Gummerer

Write the cache-tree data for the index version 5 file format. The in-memory cache-tree data is converted to the ondisk format, by adding it to the directory entries, that were compiled from the cache-entries in the step before.

---
 cache-tree.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 cache-tree.h |    1 +
 read-cache.c |    1 +
 3 files changed, 54 insertions(+)
cache-tree.c +52 -0
index 440cd04..e167b61 100644
@@ -612,6 +612,58 @@ struct cache_tree *cache_tree_convert_v5(struct directory_entry *de)
 	return convert_one(queue, 0);
 }
 
+
+static void convert_one_to_ondisk_v5(struct hash_table *table, struct cache_tree *it,
+				const char *path, int pathlen, uint32_t crc)
+{
+	int i;
+	struct directory_entry *found, *search;
+
+	crc = crc32(crc, (Bytef*)path, pathlen);
+	found = lookup_hash(crc, table);
+	search = found;
+	while (search && strcmp(path, search->pathname + search->de_pathlen - strlen(path)) != 0)
+		search = search->next_hash;
+	if (!search)
+		return;
+	/*
+	 * The number of subtrees is already calculated by
+	 * compile_directory_data, therefore we only need to
+	 * add the entry_count
+	 */
+	search->de_nentries = it->entry_count;
+	if (0 <= it->entry_count)
+		hashcpy(search->sha1, it->sha1);
+	if (strcmp(path, "") != 0)
+		crc = crc32(crc, (Bytef*)"/", 1);
+
+#if DEBUG
+	if (0 <= it->entry_count)
+		fprintf(stderr, "cache-tree <%.*s> (%d ent, %d subtree) %s\n",
+			pathlen, path, it->entry_count, it->subtree_nr,
+			sha1_to_hex(it->sha1));
+	else
+		fprintf(stderr, "cache-tree <%.*s> (%d subtree) invalid\n",
+			pathlen, path, it->subtree_nr);
+#endif
+
+	for (i = 0; i < it->subtree_nr; i++) {
+		struct cache_tree_sub *down = it->down[i];
+		if (i) {
+			struct cache_tree_sub *prev = it->down[i-1];
+			if (subtree_name_cmp(down->name, down->namelen,
+					     prev->name, prev->namelen) <= 0)
+				die("fatal - unsorted cache subtree");
+		}
+		convert_one_to_ondisk_v5(table, down->cache_tree, down->name, down->namelen, crc);
+	}
+}
+
+void cache_tree_to_ondisk_v5(struct hash_table *table, struct cache_tree *root)
+{
+	convert_one_to_ondisk_v5(table, root, "", 0, 0);
+}
+
 static struct cache_tree *cache_tree_find(struct cache_tree *it, const char *path)
 {
 	if (!it)
cache-tree.h +1 -0
index 7f29d26..e08bc31 100644
@@ -37,6 +37,7 @@ struct cache_tree *cache_tree_read(const char *buffer, unsigned long size);
  * Don't use it if the directory entries are still needed after.
  */
 struct cache_tree *cache_tree_convert_v5(struct directory_entry *de);
+void cache_tree_to_ondisk_v5(struct hash_table *table, struct cache_tree *root);
 
 int cache_tree_fully_valid(struct cache_tree *);
 int cache_tree_update(struct cache_tree *, struct cache_entry **, int, int);
read-cache.c +1 -0
index 199ba75..962d6a2 100644
@@ -1310,6 +1310,7 @@ void update_index_if_able(struct index_state *istate, struct lock_file *lockfile
 	else
 		rollback_lock_file(lockfile);
 }
+
 int write_index(struct index_state *istate, int newfd)
 {
 	set_istate_ops(istate);
Signature · 1 line
1.7.10.GIT
#31 lore raw

PATCH v3 11/13 Write resolve-undo data for index-v5

In reply to Thomas Gummerer

Write the resolve undo data to the ondisk format, by joining the data in the resolve-undo string-list with the already existing conflicts that were compiled before, when searching the directories and add them to the corresponding directory entries.

---
 read-cache-v5.c |    3 ++
 resolve-undo.c  |   93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 resolve-undo.h  |    1 +
 3 files changed, 97 insertions(+)
read-cache-v5.c +3 -0
index 45f7acd..3d03111 100644
@@ -861,6 +861,9 @@ static struct directory_entry *compile_directory_data(struct index_state *istate
 			previous_entry->next = no_subtrees;
 		}
 	}
+	if (istate->cache_tree)
+		cache_tree_to_ondisk_v5(&table, istate->cache_tree);
+	resolve_undo_to_ondisk_v5(&table, istate->resolve_undo, ndir, total_dir_len, de);
 	return de;
 }
 
resolve-undo.c +93 -0

96 lines not on this page · raw message

resolve-undo.h +1 -0
index ab660a6..ff80d84 100644
@@ -14,5 +14,6 @@ extern int unmerge_index_entry_at(struct index_state *, int);
 extern void unmerge_index(struct index_state *, const char **);
 
 extern void resolve_undo_convert_v5(struct index_state *, struct conflict_entry *);
+extern void resolve_undo_to_ondisk_v5(struct hash_table *, struct string_list *, unsigned int *, int *, struct directory_entry *);
 
 #endif
Signature · 1 line
1.7.10.GIT
#32 lore raw

PATCH v3 12/13 update-index.c: always rewrite the index when index-version is given

In reply to Thomas Gummerer

Make git update-index always rewrite the index, if a index-version is given. This is used for performance testing, to have a reader and writer for the whole index.

---
 builtin/update-index.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
builtin/update-index.c +3 -1
index 4ce341c..c31d176 100644
@@ -6,6 +6,7 @@
 #include "cache.h"
 #include "quote.h"
 #include "cache-tree.h"
+#include "read-cache.h"
 #include "tree-walk.h"
 #include "builtin.h"
 #include "refs.h"
@@ -861,6 +862,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		if (the_index.version != preferred_index_format)
 			active_cache_changed = 1;
 		the_index.version = preferred_index_format;
+		set_istate_ops(&the_index);
 	}
 
 	if (read_from_stdin) {
@@ -886,7 +888,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
 		strbuf_release(&buf);
 	}
 
-	if (active_cache_changed) {
+	if (active_cache_changed || preferred_index_format) {
 		if (newfd < 0) {
 			if (refresh_args.flags & REFRESH_QUIET)
 				exit(128);
Signature · 1 line
1.7.10.GIT
#33 lore raw

PATCH v3 13/13 p0002-index.sh: add perf test for the index formats

In reply to Thomas Gummerer

From: Thomas Rast <[email protected]>

Add a performance test for index version [23]/4/5 by using git update-index --update-index=[345], thus testing both the reader and the writer speed of all index formats.

---
 t/perf/p0002-index.sh |   33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
 create mode 100755 t/perf/p0002-index.sh
t/perf/p0002-index.sh new file +33 -0
new file mode 100755
index 0000000..140c7a0
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description="Tests index versions [23]/4/5"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+test_expect_success 'convert to v3' '
+	git update-index --index-version=3
+'
+
+test_perf 'v[23]: update-index' '
+	git update-index --index-version=3 >/dev/null
+'
+
+test_expect_success 'convert to v4' '
+	git update-index --index-version=4
+'
+
+test_perf 'v4: update-index' '
+	git update-index --index-version=4 >/dev/null
+'
+
+test_expect_success 'convert to v5' '
+	git update-index --index-version=5
+'
+
+test_perf 'v5: update-index' '
+	git update-index --index-version=5 >/dev/null
+'
+
+test_done
Signature · 1 line
1.7.10.GIT