Skip to content

Commit 176d051

Browse files
committed
Merge pull request #1344 from jeffhostetler/perf_add_excludes_with_fscache
dir.c: make add_excludes aware of fscache during status
2 parents 65b7a08 + 577c5a4 commit 176d051

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

compat/win32/fscache.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static void fscache_clear(void)
274274
/*
275275
* Checks if the cache is enabled for the given path.
276276
*/
277-
static inline int fscache_enabled(const char *path)
277+
int fscache_enabled(const char *path)
278278
{
279279
return enabled > 0 && !is_absolute_path(path);
280280
}

compat/win32/fscache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
int fscache_enable(int enable);
55
#define enable_fscache(x) fscache_enable(x)
66

7+
int fscache_enabled(const char *path);
8+
#define is_fscache_enabled(path) fscache_enabled(path)
9+
710
DIR *fscache_opendir(const char *dir);
811
int fscache_lstat(const char *file_name, struct stat *buf);
912

dir.c

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -786,12 +786,55 @@ static int add_excludes(const char *fname, const char *base, int baselen,
786786
size_t size = 0;
787787
char *buf;
788788

789-
fd = open(fname, O_RDONLY);
790-
if (fd < 0 || fstat(fd, &st) < 0) {
791-
if (fd < 0)
792-
warn_on_fopen_errors(fname);
793-
else
794-
close(fd);
789+
/*
790+
* A performance optimization for status.
791+
*
792+
* During a status scan, git looks in each directory for a .gitignore
793+
* file before scanning the directory. Since .gitignore files are not
794+
* that common, we can waste a lot of time looking for files that are
795+
* not there. Fortunately, the fscache already knows if the directory
796+
* contains a .gitignore file, since it has already read the directory
797+
* and it already has the stat-data.
798+
*
799+
* If the fscache is enabled, use the fscache-lstat() interlude to see
800+
* if the file exists (in the fscache hash maps) before trying to open()
801+
* it.
802+
*
803+
* This causes problem when the .gitignore file is a symlink, because
804+
* we call lstat() rather than stat() on the symlnk and the resulting
805+
* stat-data is for the symlink itself rather than the target file.
806+
* We CANNOT use stat() here because the fscache DOES NOT install an
807+
* interlude for stat() and mingw_stat() always calls "open-fstat-close"
808+
* on the file and defeats the purpose of the optimization here. Since
809+
* symlinks are even more rare than .gitignore files, we force a fstat()
810+
* after our open() to get stat-data for the target file.
811+
*/
812+
if (is_fscache_enabled(fname)) {
813+
if (lstat(fname, &st) < 0) {
814+
fd = -1;
815+
} else {
816+
fd = open(fname, O_RDONLY);
817+
if (fd < 0)
818+
warn_on_fopen_errors(fname);
819+
else if (S_ISLNK(st.st_mode) && fstat(fd, &st) < 0) {
820+
warn_on_fopen_errors(fname);
821+
close(fd);
822+
fd = -1;
823+
}
824+
}
825+
} else {
826+
fd = open(fname, O_RDONLY);
827+
if (fd < 0 || fstat(fd, &st) < 0) {
828+
if (fd < 0)
829+
warn_on_fopen_errors(fname);
830+
else {
831+
close(fd);
832+
fd = -1;
833+
}
834+
}
835+
}
836+
837+
if (fd < 0) {
795838
if (!istate)
796839
return -1;
797840
r = read_skip_worktree_file_from_index(istate, fname,

git-compat-util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,6 +1297,10 @@ static inline int is_missing_file_error(int errno_)
12971297
#define enable_fscache(x) /* noop */
12981298
#endif
12991299

1300+
#ifndef is_fscache_enabled
1301+
#define is_fscache_enabled(path) (0)
1302+
#endif
1303+
13001304
extern int cmd_main(int, const char **);
13011305

13021306
/*

0 commit comments

Comments
 (0)