fsmonitor: refactor refresh callback on directory events

Move the code to handle directory FSEvents (containing pathnames with a trailing slash) into a helper function. Signed-off-by: Jeff Hostetler <jeffhostetler@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff Hostetler committed Feb 26, 2024 at 21:39 UTC e5da3ddbe9b21cbf73ba451794d90792693009e2
1 file changed +30 -22
fsmonitor.c
+30 -22
@@ -183,6 +183,35 @@ static int query_fsmonitor_hook(struct repository *r,
183 return result;
184 }
185
186 +static void handle_path_with_trailing_slash(
187 + struct index_state *istate, const char *name, int pos)
188 +{
189 + int i;
190 +
191 + /*
192 + * The daemon can decorate directory events, such as
193 + * moves or renames, with a trailing slash if the OS
194 + * FS Event contains sufficient information, such as
195 + * MacOS.
196 + *
197 + * Use this to invalidate the entire cone under that
198 + * directory.
199 + *
200 + * We do not expect an exact match because the index
201 + * does not normally contain directory entries, so we
202 + * start at the insertion point and scan.
203 + */
204 + if (pos < 0)
205 + pos = -pos - 1;
206 +
207 + /* Mark all entries for the folder invalid */
208 + for (i = pos; i < istate->cache_nr; i++) {
209 + if (!starts_with(istate->cache[i]->name, name))
210 + break;
211 + istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
212 + }
213 +}
214 +
215 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
216 {
217 int i, len = strlen(name);
@@ -193,28 +222,7 @@ static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
222 name, pos);
223
224 if (name[len - 1] == '/') {
196 - /*
197 - * The daemon can decorate directory events, such as
198 - * moves or renames, with a trailing slash if the OS
199 - * FS Event contains sufficient information, such as
200 - * MacOS.
201 - *
202 - * Use this to invalidate the entire cone under that
203 - * directory.
204 - *
205 - * We do not expect an exact match because the index
206 - * does not normally contain directory entries, so we
207 - * start at the insertion point and scan.
208 - */
209 - if (pos < 0)
210 - pos = -pos - 1;
211 -
212 - /* Mark all entries for the folder invalid */
213 - for (i = pos; i < istate->cache_nr; i++) {
214 - if (!starts_with(istate->cache[i]->name, name))
215 - break;
216 - istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
217 - }
225 + handle_path_with_trailing_slash(istate, name, pos);
226
227 /*
228 * We need to remove the traling "/" from the path