@@ -1114,16 +1114,59 @@ static int add_patterns(const char *fname, const char *base, int baselen,
1114
1114
size_t size = 0 ;
1115
1115
char * buf ;
1116
1116
1117
- if (flags & PATTERN_NOFOLLOW )
1118
- fd = open_nofollow (fname , O_RDONLY );
1119
- else
1120
- fd = open (fname , O_RDONLY );
1121
-
1122
- if (fd < 0 || fstat (fd , & st ) < 0 ) {
1123
- if (fd < 0 )
1124
- warn_on_fopen_errors (fname );
1117
+ /*
1118
+ * A performance optimization for status.
1119
+ *
1120
+ * During a status scan, git looks in each directory for a .gitignore
1121
+ * file before scanning the directory. Since .gitignore files are not
1122
+ * that common, we can waste a lot of time looking for files that are
1123
+ * not there. Fortunately, the fscache already knows if the directory
1124
+ * contains a .gitignore file, since it has already read the directory
1125
+ * and it already has the stat-data.
1126
+ *
1127
+ * If the fscache is enabled, use the fscache-lstat() interlude to see
1128
+ * if the file exists (in the fscache hash maps) before trying to open()
1129
+ * it.
1130
+ *
1131
+ * This causes problem when the .gitignore file is a symlink, because
1132
+ * we call lstat() rather than stat() on the symlnk and the resulting
1133
+ * stat-data is for the symlink itself rather than the target file.
1134
+ * We CANNOT use stat() here because the fscache DOES NOT install an
1135
+ * interlude for stat() and mingw_stat() always calls "open-fstat-close"
1136
+ * on the file and defeats the purpose of the optimization here. Since
1137
+ * symlinks are even more rare than .gitignore files, we force a fstat()
1138
+ * after our open() to get stat-data for the target file.
1139
+ */
1140
+ if (is_fscache_enabled (fname )) {
1141
+ if (lstat (fname , & st ) < 0 ) {
1142
+ fd = -1 ;
1143
+ } else {
1144
+ fd = open (fname , O_RDONLY );
1145
+ if (fd < 0 )
1146
+ warn_on_fopen_errors (fname );
1147
+ else if (S_ISLNK (st .st_mode ) && fstat (fd , & st ) < 0 ) {
1148
+ warn_on_fopen_errors (fname );
1149
+ close (fd );
1150
+ fd = -1 ;
1151
+ }
1152
+ }
1153
+ } else {
1154
+ if (flags & PATTERN_NOFOLLOW )
1155
+ fd = open_nofollow (fname , O_RDONLY );
1125
1156
else
1126
- close (fd );
1157
+ fd = open (fname , O_RDONLY );
1158
+
1159
+ if (fd < 0 || fstat (fd , & st ) < 0 ) {
1160
+ if (fd < 0 )
1161
+ warn_on_fopen_errors (fname );
1162
+ else {
1163
+ close (fd );
1164
+ fd = -1 ;
1165
+ }
1166
+ }
1167
+ }
1168
+
1169
+ if (fd < 0 ) {
1127
1170
if (!istate )
1128
1171
return -1 ;
1129
1172
r = read_skip_worktree_file_from_index (istate , fname ,
0 commit comments