Skip to contentSkip to conversation

git-add has gone lstat() mad

DISCUSSION Andy Parkins 5 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello,

I was interested in trying out the GIT_WORK_DIR stuff, but ended up being unable to. The thing that stopped me is present in master as well:

 $ git --version
 git version 1.5.1.rc3.20.gaa453
 $ cd $HOME
 $ git init
 $ git add .bashrc

At this point the CPU pegs at 100% systime. An strace shows that git is calling lstat64() on every file in my home directory. I killed git before it scanned everything I've ever done.

I only want to track one file; is git meant to scan every file in the directory even though I'm not adding any of them?

Andy

Signature · 2 lines
Dr Andy Parkins, M Eng (hons), MIET
[email protected]
#1 lore raw
Quoted 3 lines · Andy Parkins

At this point the CPU pegs at 100% systime. An strace shows that git is calling lstat64() on every file in my home directory. I killed git before it scanned everything I've ever done.

Okay. I've tracked down the culprit function, but I have no idea what the fix is.

builtin-add.c:fill_directory() calls
dir.c:read_directory() which calls
dir.c:read_directory_recursive()

I can't see why git feels that it has to recurse the entire subtree. It seems to be something to do with the gitignore stuff. Surely there is no need to use a recursive search when no directories are being added?

If git-add were given

file1
dir1/file2
dir2/dir3/file3

Then only the directories "."; "dir1/"; "dir2"; and "dir2/dir3" need checking for .gitignore files; and in none of those cases does the search need to be recursive.

Andy

Signature · 2 lines
Dr Andy Parkins, M Eng (hons), MIET
[email protected]
#2 lore raw
Quoted 3 lines · Andy Parkins

I can't see why git feels that it has to recurse the entire subtree. It seems to be something to do with the gitignore stuff. Surely there is no need to use a recursive search when no directories are being added?

I do not think this is anything new.

Quoted 5 lines

If git-add were given

file1
dir1/file2
dir2/dir3/file3

The thing is, you are not giving the above three pathnames, although you might think you are.

You are giving three path *patterns* and asking git-add: "please run 'git ls-files --others' and add the ones that match these patterns".

You can teach it to detect cases where you do not have wildcard (that is both shell glob wildcard and directory names; the latter means "grab everything in that named directory") to limit the set of directories to descend into.

Patches are welcome, but applying them to 'master' needs to wait post 1.5.1.

#3 lore raw
Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Andy Parkins

I can't see why git feels that it has to recurse the entire subtree. It seems to be something to do with the gitignore stuff. Surely there is no need to use a recursive search when no directories are being added?

I do not think this is anything new.

Yeah, it's probably old. That said, it's still ugly.

Quoted 2 lines

Patches are welcome, but applying them to 'master' needs to wait post 1.5.1.

Here's a patch. It passes all tests. It's not that complex. But people should double-check. ESPECIALLY the list of special characters (currently '?' '*' '\\' and '[').

Andy, does it work for you?

NOTE! It changes the "const char **pathspec" into an array of "struct path_simplify", because that way it doesn't need to check for the magic shell expansion characters over and over and over and over again, and can just do it once up-front. All the real meat of the patch is really that conversion, and somebody should double-check that I actually got all the special characters..

The way things are set up, you can now pass a "pathspec" to the "read_directory()" function. If you pass NULL, it acts exactly like it used to do (read everything). If you pass a non-NULL pointer, it will simplify it into a "these are the prefixes without any special characters", and stop any readdir() early if the path in question doesn't match any of the prefixes.

NOTE! This does *not* obviate the need for the caller to do the *exact* pathspec match later. It's a first-level filter on "read_directory()", but it does not do the full pathspec thing. Maybe it should. But in the meantime, builtin-add.c really does need to do first

	read_directory(dir, .., pathspec);
	if (pathspec)
		prune_directory(dir, pathspec, baselen);

ie the "prune_directory()" part will do the *exact* pathspec pruning, while the "read_directory()" will use the pathspec just to do some quick high-level pruning of the directories it will recurse into.

Does this matter? I can say that:

git ls-files -o Makefile~

on the kernel took 0.110s for me before, and it now takes 0.014s. And maybe Andy's case more noticeable. Andy?

Linus

---
 builtin-add.c      |    2 +-
 builtin-ls-files.c |    2 +-
 dir.c              |   96 +++++++++++++++++++++++++++++++++++++++++++++++++---
 dir.h              |    2 +-
 wt-status.c        |    2 +-
 5 files changed, 95 insertions(+), 9 deletions(-)
builtin-add.c +1 -1
index 9fcf514..871e23f 100644
@@ -87,7 +87,7 @@ static void fill_directory(struct dir_struct *dir, const char **pathspec)
 	}
 
 	/* Read the directory and prune it */
-	read_directory(dir, path, base, baselen);
+	read_directory(dir, path, base, baselen, pathspec);
 	if (pathspec)
 		prune_directory(dir, pathspec, baselen);
 }
builtin-ls-files.c +1 -1
index 4e1d5af..74a6aca 100644
@@ -216,7 +216,7 @@ static void show_files(struct dir_struct *dir, const char *prefix)
 
 		if (baselen)
 			path = base = prefix;
-		read_directory(dir, path, base, baselen);
+		read_directory(dir, path, base, baselen, pathspec);
 		if (show_others)
 			show_other_files(dir);
 		if (show_killed)
dir.c +91 -5

144 lines not on this page · raw message

dir.h +1 -1
index 7233d65..33c31f2 100644
@@ -48,7 +48,7 @@ extern int common_prefix(const char **pathspec);
 #define MATCHED_EXACTLY 3
 extern int match_pathspec(const char **pathspec, const char *name, int namelen, int prefix, char *seen);
 
-extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen);
+extern int read_directory(struct dir_struct *, const char *path, const char *base, int baselen, const char **pathspec);
 extern int push_exclude_per_directory(struct dir_struct *, const char *, int);
 extern void pop_exclude_per_directory(struct dir_struct *, int);
 
wt-status.c +1 -1
index a25632b..a055990 100644
@@ -260,7 +260,7 @@ static void wt_status_print_untracked(struct wt_status *s)
 	if (file_exists(x))
 		add_excludes_from_file(&dir, x);
 
-	read_directory(&dir, ".", "", 0);
+	read_directory(&dir, ".", "", 0, NULL);
 	for(i = 0; i < dir.nr; i++) {
 		/* check for matching entry, which is unmerged; lifted from
 		 * builtin-ls-files:show_other_files */
#4 lore raw
Quoted 2 lines · Linus Torvalds

on the kernel took 0.110s for me before, and it now takes 0.014s. And maybe Andy's case more noticeable. Andy?

Blindingly fast.

Previously, I never completed a git add .bashrc as it was taking so long. Now it's instant. This is back to true git form - I wasn't entirely sure git-add had done anything :-). git-status confirmed that it had worked successfully though.

I've not done any extensive tests for regressions, but I've done

cd $HOME
git init
git add .bashrc
git add somedirectory/

And they work fine. So - it's works for me from me, and a big happy grin.

Andy

Signature · 2 lines
Dr Andy Parkins, M Eng (hons), MIET
[email protected]
#5 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"Andy" == Andy Parkins <[email protected]> writes:

Andy> I've not done any extensive tests for regressions, but I've done

Andy>  cd $HOME
Andy>  git init
Andy>  git add .bashrc
Andy>  git add somedirectory/

Andy> And they work fine. So - it's works for me from me, and a big happy Andy> grin.

Given that git currently doesn't maintain any metadata other than +x/-x, how are you maintaining the metadata for your homedir items? I know some schemes were discussed here in the past, but I'm curious as to what you settled on. For example, your .netrc file needs to be 600, but git won't track that.

Signature · 4 lines
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[email protected]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
#6 lore raw
Quoted 5 lines · Randal L. Schwartz

Given that git currently doesn't maintain any metadata other than +x/-x, how are you maintaining the metadata for your homedir items? I know some schemes were discussed here in the past, but I'm curious as to what you settled on. For example, your .netrc file needs to be 600, but git won't track that.

I'm not maintaining anything as yet; the experiment so far consists of .bashrc only :-)

Just storing my .inputrc, .bashrc, .vimrc and .gitconfig in a repository is probably going to give me all that I want. There aren't many other config files that I regularly update, and certainly not many that I update differently on different machines.

However, I plan to use a script to wrap the git calls so that I don't have to type GIT_WORK_DIR and GIT_DIR for every file I want to track. I plan to make it so that if you run this script with a checkout or other working tree changing command then I will run a function within it that reads the metadata out of a file that is stored in the repository which will restore those settings.

Ideally this functionality would be in hooks for pre-update-index and post-checkout-index or similar. But I have a feeling that the potential for conflicts makes it hard to do in reality.

Andy

Signature · 2 lines
Dr Andy Parkins, M Eng (hons), MIET
[email protected]
#7 lore raw
In reply to Randal L. Schwartz
Quoted 5 lines · Randal L. Schwartz

Given that git currently doesn't maintain any metadata other than +x/-x, how are you maintaining the metadata for your homedir items? I know some schemes
were discussed here in the past, but I'm curious as to what you settled on. For example, your .netrc file needs to be 600, but git won't track that.

I personally just don't track files that have sensitive data. I tend to have copies of my home dir on somewhat public servers, so I don't want that data copied around anyway.

Tom

#8 lore raw
In reply to Linus Torvalds
Quoted 3 lines · Linus Torvalds

Here's a patch. It passes all tests. It's not that complex. But people should double-check. ESPECIALLY the list of special characters (currently '?' '*' '\\' and '[').

I think the above is a good set.

This is an optimization different from what I was thinking about. I was hoping that we do not even need to call into read_directory() if all the pathspec[] elements succeeds to lstat() and they are not directories; in such a case we can just stuff them to dir structure by hand, and use the remainder for directory walk.

But I like this patch better. We need to look at .gitignore to warn about adding ignored files, so we cannot just stuff what are found to dir without checking if they are ignored.

#9 lore raw
Quoted 3 lines · Junio C Hamano

But I like this patch better. We need to look at .gitignore to warn about adding ignored files, so we cannot just stuff what are found to dir without checking if they are ignored.

I needed the following needed on top of current pu:

unpack-trees.c +1 -1
index fa36495..d4f7589 100644
@@ -521,7 +521,7 @@ static void verify_clean_subdirectory(const char *path, const char *action,
 	memset(&d, 0, sizeof(d));
 	if (o->dir)
 		d.exclude_per_dir = o->dir->exclude_per_dir;
-	i = read_directory(&d, path, pathbuf, namelen+1);
+	i = read_directory(&d, path, pathbuf, namelen+1, NULL);
 	if (i)
 		die("Updating '%s' would lose untracked files in it",
 		    path);