diff-lib: fix check_removed when fsmonitor is on

`git diff-index` may return incorrect deleted entries when fsmonitor is used in a repository with git submodules. This can be observed on Mac machines, but it can affect all other supported platforms too. If fsmonitor is used, `stat *st` is not initialized if cache_entry has CE_FSMONITOR_VALID set. But, there are three call sites that rely on stat afterwards, which can result in incorrect results. This change partially reverts commit 4f3d6d02 (fsmonitor: skip lstat deletion check during git diff-index, 2021-03-17). Signed-off-by: Josip Sokcevic <sokcevic@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Josip Sokcevic committed Sep 11, 2023 at 10:09 UTC 6a044a20480a8ef56f7ddb8142f660ca01a3391e
2 files changed +11 -6
diff-lib.c
+6 -6
@@ -28,14 +28,14 @@
28 * exists for ce that is a submodule -- it is a submodule that is not
29 * checked out). Return negative for an error.
30 */
31 -static int check_removed(const struct index_state *istate, const struct cache_entry *ce, struct stat *st)
31 +static int check_removed(const struct cache_entry *ce, struct stat *st)
32 {
33 - assert(is_fsmonitor_refreshed(istate));
34 - if (!(ce->ce_flags & CE_FSMONITOR_VALID) && lstat(ce->name, st) < 0) {
33 + if (lstat(ce->name, st) < 0) {
34 if (!is_missing_file_error(errno))
35 return -1;
36 return 1;
37 }
38 +
39 if (has_symlink_leading_path(ce->name, ce_namelen(ce)))
40 return 1;
41 if (S_ISDIR(st->st_mode)) {
@@ -141,7 +141,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
141 memset(&(dpath->parent[0]), 0,
142 sizeof(struct combine_diff_parent)*5);
143
144 - changed = check_removed(istate, ce, &st);
144 + changed = check_removed(ce, &st);
145 if (!changed)
146 wt_mode = ce_mode_from_stat(ce, st.st_mode);
147 else {
@@ -221,7 +221,7 @@ int run_diff_files(struct rev_info *revs, unsigned int option)
221 } else {
222 struct stat st;
223
224 - changed = check_removed(istate, ce, &st);
224 + changed = check_removed(ce, &st);
225 if (changed) {
226 if (changed < 0) {
227 perror(ce->name);
@@ -296,7 +296,7 @@ static int get_stat_data(const struct index_state *istate,
296 if (!cached && !ce_uptodate(ce)) {
297 int changed;
298 struct stat st;
299 - changed = check_removed(istate, ce, &st);
299 + changed = check_removed(ce, &st);
300 if (changed < 0)
301 return -1;
302 else if (changed) {
t/t7527-builtin-fsmonitor.sh
+5
@@ -809,6 +809,11 @@ my_match_and_clean () {
809 status --porcelain=v2 >actual.without &&
810 test_cmp actual.with actual.without &&
811
812 + git -C super --no-optional-locks diff-index --name-status HEAD >actual.with &&
813 + git -C super --no-optional-locks -c core.fsmonitor=false \
814 + diff-index --name-status HEAD >actual.without &&
815 + test_cmp actual.with actual.without &&
816 +
817 git -C super/dir_1/dir_2/sub reset --hard &&
818 git -C super/dir_1/dir_2/sub clean -d -f
819 }