]> git.ipfire.org Git - thirdparty/git.git/commitdiff
fsmonitor: refactor refresh callback for non-directory events
authorJeff Hostetler <jeffhostetler@github.com>
Mon, 26 Feb 2024 21:39:16 +0000 (21:39 +0000)
committerJunio C Hamano <gitster@pobox.com>
Mon, 26 Feb 2024 23:34:02 +0000 (15:34 -0800)
Move the code that handles unqualified FSEvents (without a trailing
slash) into a helper function.

Signed-off-by: Jeff Hostetler <jeffhostetler@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
fsmonitor.c

index 29cce32d81c4d621e14b6f007ba5e7601d1b18bc..364198d258ff45d921363590923e5ca7b7fd10aa 100644 (file)
@@ -183,6 +183,43 @@ static int query_fsmonitor_hook(struct repository *r,
        return result;
 }
 
+static void handle_path_without_trailing_slash(
+       struct index_state *istate, const char *name, int pos)
+{
+       int i;
+
+       if (pos >= 0) {
+               /*
+                * We have an exact match for this path and can just
+                * invalidate it.
+                */
+               istate->cache[pos]->ce_flags &= ~CE_FSMONITOR_VALID;
+       } else {
+               /*
+                * The path is not a tracked file -or- it is a
+                * directory event on a platform that cannot
+                * distinguish between file and directory events in
+                * the event handler, such as Windows.
+                *
+                * Scan as if it is a directory and invalidate the
+                * cone under it.  (But remember to ignore items
+                * between "name" and "name/", such as "name-" and
+                * "name.".
+                */
+               int len = strlen(name);
+               pos = -pos - 1;
+
+               for (i = pos; i < istate->cache_nr; i++) {
+                       if (!starts_with(istate->cache[i]->name, name))
+                               break;
+                       if ((unsigned char)istate->cache[i]->name[len] > '/')
+                               break;
+                       if (istate->cache[i]->name[len] == '/')
+                               istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
+               }
+       }
+}
+
 /*
  * The daemon can decorate directory events, such as a move or rename,
  * by adding a trailing slash to the observed name.  Use this to
@@ -225,7 +262,7 @@ static void handle_path_with_trailing_slash(
 
 static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
 {
-       int i, len = strlen(name);
+       int len = strlen(name);
        int pos = index_name_pos(istate, name, len);
 
        trace_printf_key(&trace_fsmonitor,
@@ -240,34 +277,8 @@ static void fsmonitor_refresh_callback(struct index_state *istate, char *name)
                 * for the untracked cache.
                 */
                name[len - 1] = '\0';
-       } else if (pos >= 0) {
-               /*
-                * We have an exact match for this path and can just
-                * invalidate it.
-                */
-               istate->cache[pos]->ce_flags &= ~CE_FSMONITOR_VALID;
        } else {
-               /*
-                * The path is not a tracked file -or- it is a
-                * directory event on a platform that cannot
-                * distinguish between file and directory events in
-                * the event handler, such as Windows.
-                *
-                * Scan as if it is a directory and invalidate the
-                * cone under it.  (But remember to ignore items
-                * between "name" and "name/", such as "name-" and
-                * "name.".
-                */
-               pos = -pos - 1;
-
-               for (i = pos; i < istate->cache_nr; i++) {
-                       if (!starts_with(istate->cache[i]->name, name))
-                               break;
-                       if ((unsigned char)istate->cache[i]->name[len] > '/')
-                               break;
-                       if (istate->cache[i]->name[len] == '/')
-                               istate->cache[i]->ce_flags &= ~CE_FSMONITOR_VALID;
-               }
+               handle_path_without_trailing_slash(istate, name, pos);
        }
 
        /*