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