dir.c: ignore paths containing .git when invalidating untracked cache

read_directory() code ignores all paths named ".git" even if it's not a valid git repository. See treat_path() for details. Since ".git" is basically invisible to read_directory(), when we are asked to invalidate a path that contains ".git", we can safely ignore it because the slow path would not consider it anyway. This helps when fsmonitor is used and we have a real ".git" repo at worktree top. Occasionally .git/index will be updated and if the fsmonitor hook does not filter it, untracked cache is asked to invalidate the path ".git/index". Without this patch, we invalidate the root directory unncessarily, which: - makes read_directory() fall back to slow path for root directory (slower) - makes the index dirty (because UNTR extension is updated). Depending on the index size, writing it down could also be slow. A note about the new "safe_path" knob. Since this new check could be relatively expensive, avoid it when we know it's not needed. If the path comes from the index, it can't contain ".git". If it does contain, we may be screwed up at many more levels, not just this one. Noticed-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Feb 7, 2018 at 16:21 UTC 0cacebf099dd6467845419f212acdcfe5f8d923f
6 files changed +49 -8
dir.c
+6 -4
@@ -1712,7 +1712,7 @@ static enum path_treatment treat_path(struct dir_struct *dir,
1712 if (!de)
1713 return treat_path_fast(dir, untracked, cdir, istate, path,
1714 baselen, pathspec);
1715 - if (is_dot_or_dotdot(de->d_name) || !strcmp(de->d_name, ".git"))
1715 + if (is_dot_or_dotdot(de->d_name) || !fspathcmp(de->d_name, ".git"))
1716 return path_none;
1717 strbuf_setlen(path, baselen);
1718 strbuf_addstr(path, de->d_name);
@@ -2909,10 +2909,12 @@ static int invalidate_one_component(struct untracked_cache *uc,
2909 }
2910
2911 void untracked_cache_invalidate_path(struct index_state *istate,
2912 - const char *path)
2912 + const char *path, int safe_path)
2913 {
2914 if (!istate->untracked || !istate->untracked->root)
2915 return;
2916 + if (!safe_path && !verify_path(path))
2917 + return;
2918 invalidate_one_component(istate->untracked, istate->untracked->root,
2919 path, strlen(path));
2920 }
@@ -2920,13 +2922,13 @@ void untracked_cache_invalidate_path(struct index_state *istate,
2922 void untracked_cache_remove_from_index(struct index_state *istate,
2923 const char *path)
2924 {
2923 - untracked_cache_invalidate_path(istate, path);
2925 + untracked_cache_invalidate_path(istate, path, 1);
2926 }
2927
2928 void untracked_cache_add_to_index(struct index_state *istate,
2929 const char *path)
2930 {
2929 - untracked_cache_invalidate_path(istate, path);
2931 + untracked_cache_invalidate_path(istate, path, 1);
2932 }
2933
2934 /* Update gitfile and core.worktree setting to connect work tree and git dir */
dir.h
+1 -1
@@ -347,7 +347,7 @@ static inline int dir_path_match(const struct dir_entry *ent,
347 int cmp_dir_entry(const void *p1, const void *p2);
348 int check_dir_entry_contains(const struct dir_entry *out, const struct dir_entry *in);
349
350 -void untracked_cache_invalidate_path(struct index_state *, const char *);
350 +void untracked_cache_invalidate_path(struct index_state *, const char *, int safe_path);
351 void untracked_cache_remove_from_index(struct index_state *, const char *);
352 void untracked_cache_add_to_index(struct index_state *, const char *);
353
fsmonitor.c
+1 -1
@@ -130,7 +130,7 @@ static void fsmonitor_refresh_callback(struct index_state *istate, const char *n
130 * as it could be a new untracked file.
131 */
132 trace_printf_key(&trace_fsmonitor, "fsmonitor_refresh_callback '%s'", name);
133 - untracked_cache_invalidate_path(istate, name);
133 + untracked_cache_invalidate_path(istate, name, 0);
134 }
135
136 void refresh_fsmonitor(struct index_state *istate)
fsmonitor.h
+1 -1
@@ -65,7 +65,7 @@ static inline void mark_fsmonitor_invalid(struct index_state *istate, struct cac
65 {
66 if (core_fsmonitor) {
67 ce->ce_flags &= ~CE_FSMONITOR_VALID;
68 - untracked_cache_invalidate_path(istate, ce->name);
68 + untracked_cache_invalidate_path(istate, ce->name, 1);
69 trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_invalid '%s'", ce->name);
70 }
71 }
t/t7519-status-fsmonitor.sh
+39
@@ -314,4 +314,43 @@ test_expect_success 'splitting the index results in the same state' '
314 test_cmp expect actual
315 '
316
317 +test_expect_success UNTRACKED_CACHE 'ignore .git changes when invalidating UNTR' '
318 + test_create_repo dot-git &&
319 + (
320 + cd dot-git &&
321 + mkdir -p .git/hooks &&
322 + : >tracked &&
323 + : >modified &&
324 + mkdir dir1 &&
325 + : >dir1/tracked &&
326 + : >dir1/modified &&
327 + mkdir dir2 &&
328 + : >dir2/tracked &&
329 + : >dir2/modified &&
330 + write_integration_script &&
331 + git config core.fsmonitor .git/hooks/fsmonitor-test &&
332 + git update-index --untracked-cache &&
333 + git update-index --fsmonitor &&
334 + GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace-before" \
335 + git status &&
336 + test-dump-untracked-cache >../before
337 + ) &&
338 + cat >>dot-git/.git/hooks/fsmonitor-test <<-\EOF &&
339 + printf ".git\0"
340 + printf ".git/index\0"
341 + printf "dir1/.git\0"
342 + printf "dir1/.git/index\0"
343 + EOF
344 + (
345 + cd dot-git &&
346 + GIT_TRACE_UNTRACKED_STATS="$TRASH_DIRECTORY/trace-after" \
347 + git status &&
348 + test-dump-untracked-cache >../after
349 + ) &&
350 + grep "directory invalidation" trace-before >>before &&
351 + grep "directory invalidation" trace-after >>after &&
352 + # UNTR extension unchanged, dir invalidation count unchanged
353 + test_cmp before after
354 +'
355 +
356 test_done
unpack-trees.c
+1 -1
@@ -1506,7 +1506,7 @@ static void invalidate_ce_path(const struct cache_entry *ce,
1506 if (!ce)
1507 return;
1508 cache_tree_invalidate_path(o->src_index, ce->name);
1509 - untracked_cache_invalidate_path(o->src_index, ce->name);
1509 + untracked_cache_invalidate_path(o->src_index, ce->name, 1);
1510 }
1511
1512 /*