Skip to contentSkip to conversation

save another call to git-update-index

PATCH In review Alex Riesen 4 participants 16 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 4/4 save another call to git-update-index

and a small cleanup

---
 merge-recursive.c |   41 ++++++++++++++++++-----------------------
 1 files changed, 18 insertions(+), 23 deletions(-)
merge-recursive.c +18 -23
index 9a18e06..f3f8e7d 100644
@@ -21,7 +21,7 @@ #include "graph.h"
 #include "path-list.h"
 
 #define HAVE_ALLOCA
-#define DEBUG
+/*#define DEBUG*/
 
 #ifdef DEBUG
 #define debug(args, ...) fprintf(stderr, args, ## __VA_ARGS__)
@@ -629,8 +629,10 @@ static struct rename_entry *get_renames(
 					struct tree *bTree,
 					struct index_entry **entries)
 {
+#ifdef DEBUG
 	time_t t = time(0);
 	debug("getRenames ...\n");
+#endif
 	struct rename_entry *renames = NULL;
 	struct rename_entry **rptr = &renames;
 	struct diff_options opts;
@@ -691,22 +693,16 @@ static int update_stages(FILE *up_index,
 {
 	if ( !up_index )
 		return -1;
-	if ( clear ) {
+	if ( clear )
 		fprintf(up_index, "0 %s\t%s", sha1_to_hex(null_sha1), path);
-		fputc('\0', up_index);
-	}
-	if ( omode ) {
+	if ( omode )
 		fprintf(up_index, "0%o %s 1\t%s", omode, sha1_to_hex(osha), path);
-		fputc('\0', up_index);
-	}
-	if ( amode ) {
+	if ( amode )
 		fprintf(up_index, "0%o %s 2\t%s", amode, sha1_to_hex(asha), path);
-		fputc('\0', up_index);
-	}
-	if ( bmode ) {
+	if ( bmode )
 		fprintf(up_index, "0%o %s 3\t%s", bmode, sha1_to_hex(bsha), path);
+	if ( clear || omode || amode || bmode )
 		fputc('\0', up_index);
-	}
 	return 0;
 }
 
@@ -1080,7 +1076,8 @@ static void conflict_rename_rename_2(FIL
 	free(newPath1);
 }
 
-static int process_renames(struct rename_entry *renamesA,
+static int process_renames(FILE* fp,
+			   struct rename_entry *renamesA,
 			   struct rename_entry *renamesB,
 			   const char *branchNameA,
 			   const char *branchNameB)
@@ -1097,7 +1094,6 @@ static int process_renames(struct rename
 	for (sre = renamesB; sre; sre = sre->next)
 		path_list_insert(sre->pair->one->path, &srcNames);
 
-	FILE *fp = git_update_index_pipe();
 	for_each_path(src,&srcNames) {
 		struct rename_entry *renames1, *renames2, *ren1, *ren2;
 		const char *branchName1, *branchName2;
@@ -1282,9 +1278,6 @@ static int process_renames(struct rename
 		}
 	}
 	path_list_clear(&srcNames, 0);
-	if (pclose(fp)) {
-		die("git update-index --index-info failed");
-	}
 	debug("  processRenames done\n");
 	return cleanMerge;
 }
@@ -1467,24 +1460,26 @@ static struct merge_tree_result merge_tr
 		struct rename_entry *re_head, *re_merge;
 		re_head  = get_renames(head, common, head, merge, &entries);
 		re_merge = get_renames(merge, common, head, merge, &entries);
-		result.clean = process_renames(re_head, re_merge,
+		FILE *up_index = git_update_index_pipe();
+		result.clean = process_renames(up_index,
+					       re_head, re_merge,
 					       branch1Name, branch2Name);
 		debug("\tprocessing entries...\n");
-		FILE *fp = git_update_index_pipe();
 		struct index_entry *e;
 		for (e = entries; e; e = e->next) {
 			if (e->processed)
 				continue;
-			if (!process_entry(fp, e, branch1Name, branch2Name))
+			if (!process_entry(up_index, e,
+					   branch1Name, branch2Name))
 				result.clean = 0;
 		}
+		if (pclose(up_index))
+			die("updating entry failed in git update-index");
+
 		free_rename_entries(&re_merge);
 		free_rename_entries(&re_head);
 		free_index_entries(&entries);
 
-		if (pclose(fp))
-			die("updating entry failed in git update-index");
-
 		if (result.clean || index_only)
 			result.tree = git_write_tree();
 		else
Signature · 1 line
1.4.1.rc1.g17dc
#1 lore raw

Hi,

FYI I've been just battling this pipe for a couple of hours. The first steps were easy, but since I wanted to do it incrementally, the index has to be written every so often, and I seem not to be able to get that right.

But now it's time to go to bed, so this will wait for tomorrow.

Ciao,
Dscho

#2 lore raw
Quoted 5 lines · Johannes Schindelin

FYI I've been just battling this pipe for a couple of hours. The first steps were easy, but since I wanted to do it incrementally, the index has to be written every so often, and I seem not to be able to get that right.

Are you sure? Does something use the index between starting update-index pipe and pclose?

#3 lore raw

Hi,

Quoted 8 lines · Alex Riesen
Quoted 5 lines · Johannes Schindelin

FYI I've been just battling this pipe for a couple of hours. The first steps were easy, but since I wanted to do it incrementally, the index has to be written every so often, and I seem not to be able to get that right.

Are you sure? Does something use the index between starting update-index pipe and pclose?

Yes, I am sure you have to write the cache before git-read-tree and git-write-tree ;-) I must have done something severely wrong, though, since there are not too many places where they are called. The problem is more likely that the cache has to be _read_, and _before_ the first substituted call to git-update-index.

Ciao,
Dscho

#4 lore raw
Quoted 10 lines · Johannes Schindelin
Quoted 7 lines
Quoted 5 lines

FYI I've been just battling this pipe for a couple of hours. The first steps were easy, but since I wanted to do it incrementally, the index has to be written every so often, and I seem not to be able to get that right.

Are you sure? Does something use the index between starting update-index pipe and pclose?

Yes, I am sure you have to write the cache before git-read-tree and git-write-tree ;-)

Indeed :) That's what pclose before git-write-tree is for (which was not the case as I published this optimization. Noticed it later. The last patch series has pclose before git-write-tree)

Quoted 4 lines

I must have done something severely wrong, though,
since there are not too many places where they are called. The problem is more likely that the cache has to be _read_, and _before_ the first substituted call to git-update-index.

git-write-tree change index?

#5 lore raw
In reply to Johannes Schindelin

Hi,

Quoted 3 lines · Johannes Schindelin

FYI I've been just battling this pipe for a couple of hours. The first steps were easy, but since I wanted to do it incrementally, the index has to be written every so often, and I seem not to be able to get that right.

I just finished it. See my upcoming series of three patches. These apply on top of your last cumulative patch (sometimes yesterday), although I am certain we can merge our efforts.

Ciao,
Dscho

#6 lore raw

PATCH 1/3 Add read_cache_from() and discard_cache()

In reply to Johannes Schindelin

These functions will be useful for the inbuilt merge-recursive.

---
 cache.h      |    2 ++
 read-cache.c |   57 +++++++++++++++++++++++++++++++++++++++------------------
 2 files changed, 41 insertions(+), 18 deletions(-)
cache.h +2 -0
index 8719939..a5343db 100644
@@ -142,7 +142,9 @@ #define alloc_nr(x) (((x)+16)*3/2)
 
 /* Initialize and use the cache information */
 extern int read_cache(void);
+extern int read_cache_from(const char *path);
 extern int write_cache(int newfd, struct cache_entry **cache, int entries);
+extern int discard_cache(void);
 extern int verify_path(const char *path);
 extern int cache_name_pos(const char *name, int namelen);
 #define ADD_CACHE_OK_TO_ADD 1		/* Ok to add */
read-cache.c +39 -18

117 lines not on this page · raw message

Signature · 1 line
1.4.1.rc1.gb2d14
#7 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Quoted 4 lines · Johannes Schindelin
read-cache.c +4 -0
+int discard_cache()
+{
+	int ret;
+	

read-cache.c int read_cache(void) · line 823

Quoted 10 lines
read-cache.c +10 -0
+	if (cache_mmap == NULL)
+		return 0;
+	ret = munmap(cache_mmap, cache_mmap_size);
+	cache_mmap = NULL;
+	cache_mmap_size = 0;
+	active_nr = active_cache_changed = 0;
+	/* no need to throw away allocated active_cache */
+	return ret;
+}
+

I haven't been following the details of the patches in this thread while they are being cooked actively, but two things to look out for are:

- I am guessing you run discard_cache() because you want to read in a new cache (or start from a clean slate). I am not sure what you are doing with the old cache tree data structure. If you are starting from a clean slate (i.e. there is no read_cache_from() after you call discard_cache), you would probably need to discard the old cache tree; otherwise your next write-tree may produce an incorrect index file. If you keep the old one and later swap it in, the problem might be even more severe.

- index_timestamp is left as the old value in this patch when you switch cache using read_cache_from() directly. I have a suspicion you may be bitten by "Racy Git" problem, especially because the operations are supposed to happen quickly thanks to the effort of you two ;-) increasing the risks that the file timestamp of the working tree file and the cached entry match.

#14 lore raw

PATCH 2/3 Make refresh_cache_entry() public

In reply to Johannes Schindelin

This renames refresh_entry() to refresh_cache_entry(), to make clashes more unlikely, and makes it public. It also rethinks the rather awkward way to pass an error: this is done by returning a NULL pointer and setting cache_errno now.

---
 cache.h      |    2 ++
 read-cache.c |   44 +++++++++++++++++---------------------------
 2 files changed, 19 insertions(+), 27 deletions(-)
cache.h +2 -0
index a5343db..a59b319 100644
@@ -115,6 +115,7 @@ #define cache_entry_size(len) ((offsetof
 extern struct cache_entry **active_cache;
 extern unsigned int active_nr, active_alloc, active_cache_changed;
 extern struct cache_tree *active_cache_tree;
+extern int cache_errno;
 
 #define GIT_DIR_ENVIRONMENT "GIT_DIR"
 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
@@ -151,6 +152,7 @@ #define ADD_CACHE_OK_TO_ADD 1		/* Ok to 
 #define ADD_CACHE_OK_TO_REPLACE 2	/* Ok to replace file/directory */
 #define ADD_CACHE_SKIP_DFCHECK 4	/* Ok to skip DF conflict checks */
 extern int add_cache_entry(struct cache_entry *ce, int option);
+extern struct cache_entry *refresh_cache_entry(struct cache_entry *ce, int really);
 extern int remove_cache_entry_at(int pos);
 extern int remove_file_from_cache(const char *path);
 extern int ce_same_name(struct cache_entry *a, struct cache_entry *b);
read-cache.c +17 -27

83 lines not on this page · raw message

Signature · 1 line
1.4.1.rc1.gb2d14
#15 lore raw

PATCH 3/3 merge-recursive: avoid the pipe to update-index

In reply to Johannes Schindelin

Instead of a fork, we can use the plumbing ;-)

---
 graph.c           |   26 -----
 merge-recursive.c |  270 +++++++++++++++++++++++++++++++++++++----------------
 2 files changed, 191 insertions(+), 105 deletions(-)
graph.c +0 -26
index fa2bfee..b784ea2 100644
@@ -5,32 +5,6 @@ #include "cache.h"
 #include "commit.h"
 #include "graph.h"
 
-// does not belong here
-struct tree *git_write_tree()
-{
-#if 0
-	fprintf(stderr, "GIT_INDEX_FILE='%s' git-write-tree\n",
-		getenv("GIT_INDEX_FILE"));
-#endif
-	FILE *fp = popen("git-write-tree 2>/dev/null", "r");
-	char buf[41];
-	unsigned char sha1[20];
-	int ch;
-	unsigned i = 0;
-	while ( (ch = fgetc(fp)) != EOF )
-		if ( i < sizeof(buf)-1 && ch >= '0' && ch <= 'f' )
-			buf[i++] = ch;
-		else
-			break;
-	int rc = pclose(fp);
-	if ( rc == -1 || WEXITSTATUS(rc) )
-		return NULL;
-	buf[i] = '\0';
-	if ( get_sha1(buf, sha1) != 0 )
-		return NULL;
-	return lookup_tree(sha1);
-}
-
 const char *node_title(struct node *node, int *len)
 {
 	const char *s = "(null commit)";
merge-recursive.c +191 -79

499 lines not on this page · raw message

Signature · 1 line
1.4.1.rc1.gb2d14
#8 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

In reply to Junio C Hamano

Hi,

Quoted 21 lines · Junio C Hamano
Quoted 4 lines · Johannes Schindelin
read-cache.c +4 -0
+int discard_cache()
+{
+	int ret;
+	

read-cache.c int read_cache(void) · line 823

Quoted 10 lines
read-cache.c +10 -0
+	if (cache_mmap == NULL)
+		return 0;
+	ret = munmap(cache_mmap, cache_mmap_size);
+	cache_mmap = NULL;
+	cache_mmap_size = 0;
+	active_nr = active_cache_changed = 0;
+	/* no need to throw away allocated active_cache */
+	return ret;
+}
+

I haven't been following the details of the patches in this thread while they are being cooked actively, but two things to look out for are:

Quoted 9 lines

- I am guessing you run discard_cache() because you want to read in a new cache (or start from a clean slate). I am not sure what you are doing with the old cache tree data structure. If you are starting from a clean slate (i.e. there is no read_cache_from() after you call discard_cache), you would probably need to discard the old cache tree; otherwise your next write-tree may produce an incorrect index file. If you keep the old one and later swap it in, the problem might be even more severe.

True, I missed that one. But it is just a call to cache_tree_free(active_cache_tree); in discard_cache(), right?

Quoted 7 lines

- index_timestamp is left as the old value in this patch when you switch cache using read_cache_from() directly. I have a suspicion you may be bitten by "Racy Git" problem, especially because the operations are supposed to happen quickly thanks to the effort of you two ;-) increasing the risks that the file timestamp of the working tree file and the cached entry match.

Yes. Again, just one line to discard_cache(), right?

	index_file_timestamp = 0;

If there is more to it, please don't let me die dumb.

Ciao,
Dscho

#9 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Quoted 2 lines · Johannes Schindelin

True, I missed that one. But it is just a call to cache_tree_free(active_cache_tree); in discard_cache(), right?

On the codepath to write out the new index file, calling cache_free_tree(&active_cache_tree) before write_cache() is all that should be needed. When "active_cache_tree == NULL", write_cache() would write out an index file without the cached tree information.

Currently not many things take advantage of cached tree information to optimize its operation. But I'd like to change that. For example, tree merges by read-tree should be able to take advantage of the fact that a cached tree read from the index and three trees being read all match for a subdirectory and do the merge of the directory without descending into it.

Quoted 11 lines
Quoted 7 lines

- index_timestamp is left as the old value in this patch when you switch cache using read_cache_from() directly. I have a suspicion you may be bitten by "Racy Git" problem, especially because the operations are supposed to happen quickly thanks to the effort of you two ;-) increasing the risks that the file timestamp of the working tree file and the cached entry match.

Yes. Again, just one line to discard_cache(), right?

	index_file_timestamp = 0;

This one I am not sure. Read the comment in ce_match_stat() and see the problematic sequence of events that this variable tries to help resolve applies to your use.

#10 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Hi,

Quoted 10 lines · Junio C Hamano
Quoted 2 lines · Johannes Schindelin

True, I missed that one. But it is just a call to cache_tree_free(active_cache_tree); in discard_cache(), right?

On the codepath to write out the new index file, calling cache_free_tree(&active_cache_tree) before write_cache() is all that should be needed. When "active_cache_tree == NULL", write_cache() would write out an index file without the cached tree information.

Quoted 6 lines

Currently not many things take advantage of cached tree information to optimize its operation. But I'd like to change that. For example, tree merges by read-tree should be able to take advantage of the fact that a cached tree read from the index and three trees being read all match for a subdirectory and do the merge of the directory without descending into it.

Together with my argument, that a library function should make life easier for you, not harder, you are making a fine point that the _clean() version of get_merge_bases() should clean the active_cache_tree, too.

Besides, is it not better to clean up now-bogus memory anyway?

Quoted 8 lines
Quoted 8 lines
Quoted 7 lines

- index_timestamp is left as the old value in this patch when you switch cache using read_cache_from() directly. I have a suspicion you may be bitten by "Racy Git" problem, especially because the operations are supposed to happen quickly thanks to the effort of you two ;-) increasing the risks that the file timestamp of the working tree file and the cached entry match.

Quoted 7 lines
Quoted 3 lines

Yes. Again, just one line to discard_cache(), right?

	index_file_timestamp = 0;

This one I am not sure. Read the comment in ce_match_stat() and see the problematic sequence of events that this variable tries to help resolve applies to your use.

Okay. After reading the comment, I am quite certain we can just set the index_file_timestamp to 0.

Either we start with an empty cache; In that case it is obviously correct to set the timestamp to 0.

Or, after we discard the cache, we have to read a new cache before working on it. Now, reading the cache means calling read_cache() (or read_cache_from()), and -- alas -- line number 12 in the body of that function sets the timestamp to 0 _anyway_, to be reset to the correct value later.

So, I still think that these two lines should be in the cleanup part of get_merge_bases().

BTW personally, I prefer the one-function approach, i.e. a flag which says if it is okay not to clean up.

Ciao,
Dscho

#11 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Quoted 2 lines · Johannes Schindelin

Okay. After reading the comment, I am quite certain we can just set the index_file_timestamp to 0.

Thanks.

Quoted 2 lines

So, I still think that these two lines should be in the cleanup part of get_merge_bases().

I think that is sane -- please make it so.

Quoted 2 lines

BTW personally, I prefer the one-function approach, i.e. a flag which says if it is okay not to clean up.

Yup. Agreed.

#12 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Hi,

Quoted 11 lines · Junio C Hamano
Quoted 2 lines · Johannes Schindelin

Okay. After reading the comment, I am quite certain we can just set the index_file_timestamp to 0.

Thanks.

Quoted 2 lines

So, I still think that these two lines should be in the cleanup part of get_merge_bases().

I think that is sane -- please make it so.

Ummm. After reading my original mail again: bad tobacco that was. Of course, I meant discard_cache(), not get_merge_bases(). Setting index_file_timestamp in get_merge_bases() would be wrong.

Sorry for the noise,
Dscho

#13 lore raw

Re: [PATCH 1/3] Add read_cache_from() and discard_cache()

Quoted 3 lines · Johannes Schindelin

Ummm. After reading my original mail again: bad tobacco that was. Of course, I meant discard_cache(), not get_merge_bases(). Setting index_file_timestamp in get_merge_bases() would be wrong.

I was well aware you were talking about discard_cache(). I think these two clean-ups are good to have there.