From: Abhishek Agarwal <agab0323@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
Patrick Steinhardt <ps@pks.im>,
Paul Tarjan <github@paulisageek.com>,
Erik Cervin-Edin <erik@cervined.in>,
John Mellor <johnme@google.com>,
Nicolas Riebesel <nicolas@adamatec.com>
Subject: [PATCH 2/3] fsmonitor: keep watch paths below a renamed directory up to date
Date: Sun, 6 Dec 2026 12:30:44 +0530 [thread overview]
Message-ID: <20261206070045.12607-3-agab0323@gmail.com> (raw)
In-Reply-To: <20261206070045.12607-1-agab0323@gmail.com>
An inotify watch belongs to a directory, not to its path, so after a
directory is renamed, the watches on its subdirectories keep working.
The Linux listener only records the new path of the renamed directory
itself, though. When register_inotify() then walks the renamed
directory, add_watch() asks for each subdirectory's watch with
IN_MASK_CREATE, gets EEXIST, and returns without updating its maps.
Those watches stay known by their old paths, so changes below them
are reported for paths that no longer exist, and "git status" misses
them until something resets the daemon's state.
When the directory is already watched, ask for its watch again without
IN_MASK_CREATE, which returns the existing watch descriptor and leaves
the watch as it is, and record the new path in both maps.
Erik's report [1] points out that this case was already asked about
when the listener was reviewed.
[1] https://lore.kernel.org/git/as4z1SskiPyrSKIa@vader/
Reported-by: Erik Cervin-Edin <erik@cervined.in>
Signed-off-by: Abhishek Agarwal <agab0323@gmail.com>
---
compat/fsmonitor/fsm-listen-linux.c | 102 ++++++++++++++++++++++------
t/t7527-builtin-fsmonitor.sh | 11 +++
2 files changed, 91 insertions(+), 22 deletions(-)
diff --git a/compat/fsmonitor/fsm-listen-linux.c b/compat/fsmonitor/fsm-listen-linux.c
index 8b5fe492d1..df91714f76 100644
--- a/compat/fsmonitor/fsm-listen-linux.c
+++ b/compat/fsmonitor/fsm-listen-linux.c
@@ -92,29 +92,17 @@ static int rename_entry_cmp(const void *cmp_data UNUSED,
return e1->cookie != e2->cookie;
}
-/*
- * Register an inotify watch, add watch descriptor to path mapping
- * and the reverse mapping.
- */
-static int add_watch(const char *path, struct fsm_listen_data *data)
-{
- const char *interned = strintern(path);
- struct watch_entry *w1, *w2;
+/* All events but reads and opens, and only for directories. */
+#define WATCH_MASK \
+ ((IN_ALL_EVENTS | IN_ONLYDIR) ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN)
- /* add the inotify watch, don't allow watches to be modified */
- int wd = inotify_add_watch(data->fd_inotify, interned,
- (IN_ALL_EVENTS | IN_ONLYDIR | IN_MASK_CREATE)
- ^ IN_ACCESS ^ IN_CLOSE ^ IN_OPEN);
- if (wd < 0) {
- if (errno == ENOENT || errno == ENOTDIR)
- return 0; /* directory was deleted or is not a directory */
- if (errno == EEXIST)
- return 0; /* watch already exists, no action needed */
- if (errno == ENOSPC)
- return error(_("inotify watch limit reached; "
- "increase fs.inotify.max_user_watches"));
- return error_errno(_("inotify_add_watch('%s') failed"), interned);
- }
+/*
+ * Add watch descriptor to path mapping and the reverse mapping.
+ */
+static void record_watch(int wd, const char *interned,
+ struct fsm_listen_data *data)
+{
+ struct watch_entry *w1, *w2;
/* add watch descriptor -> directory mapping */
CALLOC_ARRAY(w1, 1);
@@ -129,7 +117,77 @@ static int add_watch(const char *path, struct fsm_listen_data *data)
w2->dir = interned;
hashmap_entry_init(&w2->ent, strhash(w2->dir));
hashmap_add(&data->revwatches, &w2->ent);
+}
+/*
+ * The directory is already watched, for example because it is below a
+ * directory that was renamed: a watch follows the directory, not its
+ * path, so we still know it by the path it had before the rename.
+ * Record its new path.
+ */
+static int update_watch_path(const char *interned,
+ struct fsm_listen_data *data)
+{
+ struct watch_entry k, *w1, *w2;
+
+ /* without IN_MASK_CREATE, this returns the existing watch */
+ int wd = inotify_add_watch(data->fd_inotify, interned, WATCH_MASK);
+ if (wd < 0) {
+ if (errno == ENOENT || errno == ENOTDIR)
+ return 0; /* deleted, or not a directory */
+ return error_errno(_("inotify_add_watch('%s') failed"),
+ interned);
+ }
+
+ k.wd = wd;
+ hashmap_entry_init(&k.ent, memhash(&k.wd, sizeof(int)));
+ w1 = hashmap_get_entry(&data->watches, &k, ent, NULL);
+ if (!w1) {
+ /* the old watch went away in the meantime */
+ record_watch(wd, interned, data);
+ return 0;
+ }
+ if (w1->dir == interned)
+ return 0; /* already known under this path */
+
+ k.dir = w1->dir;
+ hashmap_entry_init(&k.ent, strhash(k.dir));
+ w2 = hashmap_remove_entry(&data->revwatches, &k, ent, &wd);
+ if (!w2)
+ BUG("no reverse watch for '%s'", w1->dir);
+
+ trace_printf_key(&trace_fsmonitor, "watch for '%s' is now '%s'",
+ w1->dir, interned);
+ w1->dir = interned;
+ w2->dir = interned;
+ hashmap_entry_init(&w2->ent, strhash(w2->dir));
+ hashmap_add(&data->revwatches, &w2->ent);
+ return 0;
+}
+
+/*
+ * Register an inotify watch, add watch descriptor to path mapping
+ * and the reverse mapping.
+ */
+static int add_watch(const char *path, struct fsm_listen_data *data)
+{
+ const char *interned = strintern(path);
+
+ /* add the inotify watch, don't allow watches to be modified */
+ int wd = inotify_add_watch(data->fd_inotify, interned,
+ WATCH_MASK | IN_MASK_CREATE);
+ if (wd < 0) {
+ if (errno == ENOENT || errno == ENOTDIR)
+ return 0; /* directory was deleted or is not a directory */
+ if (errno == EEXIST)
+ return update_watch_path(interned, data);
+ if (errno == ENOSPC)
+ return error(_("inotify watch limit reached; "
+ "increase fs.inotify.max_user_watches"));
+ return error_errno(_("inotify_add_watch('%s') failed"), interned);
+ }
+
+ record_watch(wd, interned, data);
return 0;
}
diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh
index e0c4fa0aac..b7041e1825 100755
--- a/t/t7527-builtin-fsmonitor.sh
+++ b/t/t7527-builtin-fsmonitor.sh
@@ -1497,4 +1497,15 @@ test_expect_success 'rename a directory again after its old path came back' '
check_fsmonitor_status back
'
+test_expect_success 'edits below a renamed directory are reported' '
+ test_when_finished "stop_daemon_delete_repo below" &&
+ start_watching below old old/sub &&
+ git -C below mv old new &&
+ git -C below commit -q -m rename &&
+ echo b >>below/new/sub/file &&
+ echo " M new/sub/file" >expect &&
+ git -C below status --porcelain >actual &&
+ test_cmp expect actual
+'
+
test_done
--
2.56.0
next prev parent reply other threads:[~2026-12-06 7:01 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 7:00 [PATCH 0/3] fsmonitor: fix rename handling in the Linux listener Abhishek Agarwal
2026-12-06 7:00 ` [PATCH 1/3] fsmonitor: remove the right reverse entry of a watch on Linux Abhishek Agarwal
2026-12-06 7:00 ` Abhishek Agarwal [this message]
2026-12-06 7:00 ` [PATCH 3/3] fsmonitor: watch directories moved into the worktree " Abhishek Agarwal
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261206070045.12607-3-agab0323@gmail.com \
--to=agab0323@gmail.com \
--cc=erik@cervined.in \
--cc=git@vger.kernel.org \
--cc=github@paulisageek.com \
--cc=gitster@pobox.com \
--cc=johnme@google.com \
--cc=nicolas@adamatec.com \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.