@@ -1063,16 +1063,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
1063
1063
size_t size = 0 ;
1064
1064
char * buf ;
1065
1065
1066
- if (flags & PATTERN_NOFOLLOW )
1067
- fd = open_nofollow (fname , O_RDONLY );
1068
- else
1069
- fd = open (fname , O_RDONLY );
1070
-
1071
- if (fd < 0 || fstat (fd , & st ) < 0 ) {
1072
- if (fd < 0 )
1073
- warn_on_fopen_errors (fname );
1066
+ /*
1067
+ * A performance optimization for status.
1068
+ *
1069
+ * During a status scan, git looks in each directory for a .gitignore
1070
+ * file before scanning the directory. Since .gitignore files are not
1071
+ * that common, we can waste a lot of time looking for files that are
1072
+ * not there. Fortunately, the fscache already knows if the directory
1073
+ * contains a .gitignore file, since it has already read the directory
1074
+ * and it already has the stat-data.
1075
+ *
1076
+ * If the fscache is enabled, use the fscache-lstat() interlude to see
1077
+ * if the file exists (in the fscache hash maps) before trying to open()
1078
+ * it.
1079
+ *
1080
+ * This causes problem when the .gitignore file is a symlink, because
1081
+ * we call lstat() rather than stat() on the symlnk and the resulting
1082
+ * stat-data is for the symlink itself rather than the target file.
1083
+ * We CANNOT use stat() here because the fscache DOES NOT install an
1084
+ * interlude for stat() and mingw_stat() always calls "open-fstat-close"
1085
+ * on the file and defeats the purpose of the optimization here. Since
1086
+ * symlinks are even more rare than .gitignore files, we force a fstat()
1087
+ * after our open() to get stat-data for the target file.
1088
+ */
1089
+ if (is_fscache_enabled (fname )) {
1090
+ if (lstat (fname , & st ) < 0 ) {
1091
+ fd = -1 ;
1092
+ } else {
1093
+ fd = open (fname , O_RDONLY );
1094
+ if (fd < 0 )
1095
+ warn_on_fopen_errors (fname );
1096
+ else if (S_ISLNK (st .st_mode ) && fstat (fd , & st ) < 0 ) {
1097
+ warn_on_fopen_errors (fname );
1098
+ close (fd );
1099
+ fd = -1 ;
1100
+ }
1101
+ }
1102
+ } else {
1103
+ if (flags & PATTERN_NOFOLLOW )
1104
+ fd = open_nofollow (fname , O_RDONLY );
1074
1105
else
1075
- close (fd );
1106
+ fd = open (fname , O_RDONLY );
1107
+
1108
+ if (fd < 0 || fstat (fd , & st ) < 0 ) {
1109
+ if (fd < 0 )
1110
+ warn_on_fopen_errors (fname );
1111
+ else {
1112
+ close (fd );
1113
+ fd = -1 ;
1114
+ }
1115
+ }
1116
+ }
1117
+
1118
+ if (fd < 0 ) {
1076
1119
if (!istate )
1077
1120
return -1 ;
1078
1121
r = read_skip_worktree_file_from_index (istate , fname ,
0 commit comments