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