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