fsmonitor: integrate with sparse index

If we need to expand a sparse-index into a full one, then the FS Monitor bitmap is going to be incorrect. Ensure that we start fresh at such an event. While this is currently a performance drawback, the eventual hope of the sparse-index feature is that these expansions will be rare and hence we will be able to keep the FS Monitor data accurate across multiple Git commands. These tests are added to demonstrate that the behavior is the same across a full index and a sparse index, but also that file modifications to a tracked directory outside of the sparse cone will trigger ensure_full_index(). Reviewed-by: Elijah Newren <newren@gmail.com> Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed Jul 14, 2021 at 13:12 UTC f8fe49e53958f19b0e62e9549a80fcaa56d2f3cf
2 files changed +56
sparse-index.c
+7
@@ -186,6 +186,10 @@ int convert_to_sparse(struct index_state *istate)
186 cache_tree_free(&istate->cache_tree);
187 cache_tree_update(istate, 0);
188
189 + istate->fsmonitor_has_run_once = 0;
190 + FREE_AND_NULL(istate->fsmonitor_dirty);
191 + FREE_AND_NULL(istate->fsmonitor_last_update);
192 +
193 istate->sparse_index = 1;
194 trace2_region_leave("index", "convert_to_sparse", istate->repo);
195 return 0;
@@ -282,6 +286,9 @@ void ensure_full_index(struct index_state *istate)
286 istate->cache = full->cache;
287 istate->cache_nr = full->cache_nr;
288 istate->cache_alloc = full->cache_alloc;
289 + istate->fsmonitor_has_run_once = 0;
290 + FREE_AND_NULL(istate->fsmonitor_dirty);
291 + FREE_AND_NULL(istate->fsmonitor_last_update);
292
293 strbuf_release(&base);
294 free(full);
t/t7519-status-fsmonitor.sh
+49
@@ -73,6 +73,7 @@ test_expect_success 'setup' '
73 expect*
74 actual*
75 marker*
76 + trace2*
77 EOF
78 '
79
@@ -383,4 +384,52 @@ test_expect_success 'status succeeds after staging/unstaging' '
384 )
385 '
386
387 +# Usage:
388 +# check_sparse_index_behavior [!]
389 +# If "!" is supplied, then we verify that we do not call ensure_full_index
390 +# during a call to 'git status'. Otherwise, we verify that we _do_ call it.
391 +check_sparse_index_behavior () {
392 + git status --porcelain=v2 >expect &&
393 + git sparse-checkout init --cone --sparse-index &&
394 + git sparse-checkout set dir1 dir2 &&
395 + GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
396 + git status --porcelain=v2 >actual &&
397 + test_region $1 index ensure_full_index trace2.txt &&
398 + test_region fsm_hook query trace2.txt &&
399 + test_cmp expect actual &&
400 + rm trace2.txt &&
401 + git sparse-checkout disable
402 +}
403 +
404 +test_expect_success 'status succeeds with sparse index' '
405 + git reset --hard &&
406 +
407 + test_config core.fsmonitor "$TEST_DIRECTORY/t7519/fsmonitor-all" &&
408 + check_sparse_index_behavior ! &&
409 +
410 + write_script .git/hooks/fsmonitor-test<<-\EOF &&
411 + printf "last_update_token\0"
412 + EOF
413 + git config core.fsmonitor .git/hooks/fsmonitor-test &&
414 + check_sparse_index_behavior ! &&
415 +
416 + write_script .git/hooks/fsmonitor-test<<-\EOF &&
417 + printf "last_update_token\0"
418 + printf "dir1/modified\0"
419 + EOF
420 + check_sparse_index_behavior ! &&
421 +
422 + cp -r dir1 dir1a &&
423 + git add dir1a &&
424 + git commit -m "add dir1a" &&
425 +
426 + # This one modifies outside the sparse-checkout definition
427 + # and hence we expect to expand the sparse-index.
428 + write_script .git/hooks/fsmonitor-test<<-\EOF &&
429 + printf "last_update_token\0"
430 + printf "dir1a/modified\0"
431 + EOF
432 + check_sparse_index_behavior
433 +'
434 +
435 test_done