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