Skip to content

Commit 44878b4

Browse files
jeffhostetlerGit for Windows Build Agent
authored andcommitted
fsmonitor: never set CE_FSMONITOR_VALID on submodules
Never set CE_FSMONITOR_VALID on the cache-entry of submodule directories. During a client command like 'git status', we may need to recurse into each submodule to compute a status summary for the submodule. Since the purpose of the ce_flag is to let Git avoid scanning a cache-entry, setting the flag causes the recursive call to be avoided and we report incorrect (no status) for the submodule. We created an OS watch on the root directory of our working directory and we receive events for everything in the cone under it. When submodules are present inside our working directory, we receive events for both our repo (the super) and any subs within it. Since our index doesn't have any information for items within the submodules, we can't use those events. We could try to truncate the paths of those events back to the submodule boundary and mark the GITLINK as dirty, but that feels expensive since we would have to prefix compare every FS event that we receive against a list of submodule roots. And it still wouldn't be sufficient to correctly report status on the submodule, since we don't have any space in the cache-entry to cache the submodule's status (the 'SCMU' bits in porcelain V2 speak). That is, the CE_FSMONITOR_VALID bit just says that we don't need to scan/inspect it because we already know the answer -- it doesn't say that the item is clean -- and we don't have space in the cache-entry to store those answers. So we should always do the recursive scan. Therefore, we should never set the flag on GITLINK cache-entries. Signed-off-by: Jeff Hostetler <[email protected]>
1 parent 8f8a1e8 commit 44878b4

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

fsmonitor.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ void tweak_fsmonitor(struct index_state *istate)
580580
if (fsmonitor_enabled) {
581581
/* Mark all entries valid */
582582
for (i = 0; i < istate->cache_nr; i++) {
583+
if (S_ISGITLINK(istate->cache[i]->ce_mode))
584+
continue;
583585
istate->cache[i]->ce_flags |= CE_FSMONITOR_VALID;
584586
}
585587

fsmonitor.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,24 @@ static inline int is_fsmonitor_refreshed(const struct index_state *istate)
6868
* Set the given cache entries CE_FSMONITOR_VALID bit. This should be
6969
* called any time the cache entry has been updated to reflect the
7070
* current state of the file on disk.
71+
*
72+
* However, never mark submodules as valid. When commands like "git
73+
* status" run they might need to recurse into the submodule (using a
74+
* child process) to get a summary of the submodule state. We don't
75+
* have (and don't want to create) the facility to translate every
76+
* FS event that we receive and that happens to be deep inside of a
77+
* submodule back to the submodule root, so we cannot correctly keep
78+
* track of this bit on the gitlink directory. Therefore, we never
79+
* set it on submodules.
7180
*/
7281
static inline void mark_fsmonitor_valid(struct index_state *istate, struct cache_entry *ce)
7382
{
7483
enum fsmonitor_mode fsm_mode = fsm_settings__get_mode(istate->repo);
7584

7685
if (fsm_mode > FSMONITOR_MODE_DISABLED &&
7786
!(ce->ce_flags & CE_FSMONITOR_VALID)) {
87+
if (S_ISGITLINK(ce->ce_mode))
88+
return;
7889
istate->cache_changed = 1;
7990
ce->ce_flags |= CE_FSMONITOR_VALID;
8091
trace_printf_key(&trace_fsmonitor, "mark_fsmonitor_clean '%s'", ce->name);

t/t7527-builtin-fsmonitor.sh

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,4 +721,97 @@ do
721721
'
722722
done
723723

724+
# Test fsmonitor interaction with submodules.
725+
#
726+
# If we start the daemon in the super, it will see FS events for
727+
# everything in the working directory cone and this includes any
728+
# files/directories contained *within* the submodules.
729+
#
730+
# A `git status` at top level will get events for items within the
731+
# submodule and ignore them, since they aren't named in the index
732+
# of the super repo. This makes the fsmonitor response a little
733+
# noisy, but it doesn't alter the correctness of the state of the
734+
# super-proper.
735+
#
736+
# When we have submodules, `git status` normally does a recursive
737+
# status on each of the submodules and adds a summary row for any
738+
# dirty submodules. (See the "S..." bits in porcelain V2 output.)
739+
#
740+
# It is therefore important that the top level status not be tricked
741+
# by the FSMonitor response to skip those recursive calls.
742+
743+
my_match_and_clean () {
744+
git -C super --no-optional-locks status --porcelain=v2 >actual.with &&
745+
git -C super --no-optional-locks -c core.fsmonitor=false \
746+
status --porcelain=v2 >actual.without &&
747+
test_cmp actual.with actual.without &&
748+
749+
git -C super/dir_1/dir_2/sub reset --hard &&
750+
git -C super/dir_1/dir_2/sub clean -d -f
751+
}
752+
753+
test_expect_success "Submodule" '
754+
test_when_finished "git -C super fsmonitor--daemon stop" &&
755+
756+
git init "super" &&
757+
echo x >super/file_1 &&
758+
echo y >super/file_2 &&
759+
echo z >super/file_3 &&
760+
mkdir super/dir_1 &&
761+
echo a >super/dir_1/file_11 &&
762+
echo b >super/dir_1/file_12 &&
763+
mkdir super/dir_1/dir_2 &&
764+
echo a >super/dir_1/dir_2/file_21 &&
765+
echo b >super/dir_1/dir_2/file_22 &&
766+
git -C super add . &&
767+
git -C super commit -m "initial super commit" &&
768+
769+
git init "sub" &&
770+
echo x >sub/file_x &&
771+
echo y >sub/file_y &&
772+
echo z >sub/file_z &&
773+
mkdir sub/dir_x &&
774+
echo a >sub/dir_x/file_a &&
775+
echo b >sub/dir_x/file_b &&
776+
mkdir sub/dir_x/dir_y &&
777+
echo a >sub/dir_x/dir_y/file_a &&
778+
echo b >sub/dir_x/dir_y/file_b &&
779+
git -C sub add . &&
780+
git -C sub commit -m "initial sub commit" &&
781+
782+
git -C super submodule add ../sub ./dir_1/dir_2/sub &&
783+
git -C super commit -m "add sub" &&
784+
785+
start_daemon -C super &&
786+
git -C super config core.fsmonitor true &&
787+
git -C super update-index --fsmonitor &&
788+
git -C super status &&
789+
790+
# Now run pairs of commands w/ and w/o FSMonitor while we make
791+
# some dirt in the submodule and confirm matching output.
792+
793+
# Completely clean status.
794+
my_match_and_clean &&
795+
796+
# .M S..U
797+
echo z >super/dir_1/dir_2/sub/dir_x/dir_y/foobar_u &&
798+
my_match_and_clean &&
799+
800+
# .M S.M.
801+
echo z >super/dir_1/dir_2/sub/dir_x/dir_y/foobar_m &&
802+
git -C super/dir_1/dir_2/sub add . &&
803+
my_match_and_clean &&
804+
805+
# .M S.M.
806+
echo z >>super/dir_1/dir_2/sub/dir_x/dir_y/file_a &&
807+
git -C super/dir_1/dir_2/sub add . &&
808+
my_match_and_clean &&
809+
810+
# .M SC..
811+
echo z >>super/dir_1/dir_2/sub/dir_x/dir_y/file_a &&
812+
git -C super/dir_1/dir_2/sub add . &&
813+
git -C super/dir_1/dir_2/sub commit -m "SC.." &&
814+
my_match_and_clean
815+
'
816+
724817
test_done

0 commit comments

Comments
 (0)