read info/{attributes,exclude} only when in repository

The low-level attribute and gitignore code will try to look in $GIT_DIR/info for any repo-level configuration files, even if we have not actually determined that we are in a repository (e.g., running "git grep --no-index"). In such a case they end up looking for ".git/info/attributes", etc. This is generally harmless, as such a file is unlikely to exist outside of a repository, but it's still conceptually the wrong thing to do. Let's detect this situation explicitly and skip reading the file (i.e., the same behavior we'd get if we were in a repository and the file did not exist). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 20, 2016 at 02:16 UTC f0056f6419deb6b4e226976c571f6429875519f1
2 files changed +11 -7
attr.c
+5 -1
@@ -531,7 +531,11 @@ static void bootstrap_attr_stack(void)
531 debug_push(elem);
532 }
533
534 - elem = read_attr_from_file(git_path_info_attributes(), 1);
534 + if (startup_info->have_repository)
535 + elem = read_attr_from_file(git_path_info_attributes(), 1);
536 + else
537 + elem = NULL;
538 +
539 if (!elem)
540 elem = xcalloc(1, sizeof(*elem));
541 elem->origin = NULL;
dir.c
+6 -6
@@ -2237,8 +2237,6 @@ static GIT_PATH_FUNC(git_path_info_exclude, "info/exclude")
2237
2238 void setup_standard_excludes(struct dir_struct *dir)
2239 {
2240 - const char *path;
2241 -
2240 dir->exclude_per_dir = ".gitignore";
2241
2242 /* core.excludefile defaulting to $XDG_HOME/git/ignore */
@@ -2249,10 +2247,12 @@ void setup_standard_excludes(struct dir_struct *dir)
2247 dir->untracked ? &dir->ss_excludes_file : NULL);
2248
2249 /* per repository user preference */
2252 - path = git_path_info_exclude();
2253 - if (!access_or_warn(path, R_OK, 0))
2254 - add_excludes_from_file_1(dir, path,
2255 - dir->untracked ? &dir->ss_info_exclude : NULL);
2250 + if (startup_info->have_repository) {
2251 + const char *path = git_path_info_exclude();
2252 + if (!access_or_warn(path, R_OK, 0))
2253 + add_excludes_from_file_1(dir, path,
2254 + dir->untracked ? &dir->ss_info_exclude : NULL);
2255 + }
2256 }
2257
2258 int remove_path(const char *name)