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