Merge pull request #3082 from dscho/fsmonitor-gfw

Add an experimental built-in FSMonitor
This commit is contained in:
Jeff Hostetler
2021-03-08 10:40:00 -05:00
committed by Johannes Schindelin
26 changed files with 3852 additions and 25 deletions

1
.gitignore vendored
View File

@@ -72,6 +72,7 @@
/git-format-patch
/git-fsck
/git-fsck-objects
/git-fsmonitor--daemon
/git-gc
/git-get-tar-commit-id
/git-grep

View File

@@ -66,18 +66,45 @@ core.fsmonitor::
will identify all files that may have changed since the
requested date/time. This information is used to speed up git by
avoiding unnecessary processing of files that have not changed.
See the "fsmonitor-watchman" section of linkgit:githooks[5].
+
See the "fsmonitor-watchman" section of linkgit:githooks[5].
+
Note: FSMonitor hooks (and this config setting) are ignored if the
(experimental) built-in FSMonitor is enabled (see
`core.useBuiltinFSMonitor`).
core.fsmonitorHookVersion::
Sets the version of hook that is to be used when calling fsmonitor.
There are currently versions 1 and 2. When this is not set,
version 2 will be tried first and if it fails then version 1
will be tried. Version 1 uses a timestamp as input to determine
which files have changes since that time but some monitors
like watchman have race conditions when used with a timestamp.
Version 2 uses an opaque string so that the monitor can return
something that can be used to determine what files have changed
without race conditions.
Sets the version of hook that is to be used when calling the
FSMonitor hook (as configured via `core.fsmonitor`).
+
There are currently versions 1 and 2. When this is not set,
version 2 will be tried first and if it fails then version 1
will be tried. Version 1 uses a timestamp as input to determine
which files have changes since that time but some monitors
like watchman have race conditions when used with a timestamp.
Version 2 uses an opaque string so that the monitor can return
something that can be used to determine what files have changed
without race conditions.
+
Note: FSMonitor hooks (and this config setting) are ignored if the
built-in FSMonitor is enabled (see `core.useBuiltinFSMonitor`).
core.useBuiltinFSMonitor::
(EXPERIMENTAL) If set to true, enable the built-in filesystem
event watcher (for technical details, see
linkgit:git-fsmonitor--daemon[1]).
+
Like external (hook-based) FSMonitors, the built-in FSMonitor can speed up
Git commands that need to refresh the Git index (e.g. `git status`) in a
worktree with many files. The built-in FSMonitor facility eliminates the
need to install and maintain an external third-party monitoring tool.
+
The built-in FSMonitor is currently available only on a limited set of
supported platforms.
+
Note: if this config setting is set to `true`, any FSMonitor hook
configured via `core.fsmonitor` (and possibly `core.fsmonitorHookVersion`)
is ignored.
core.trustctime::
If false, the ctime differences between the index and the

View File

@@ -0,0 +1,107 @@
git-fsmonitor--daemon(1)
========================
NAME
----
git-fsmonitor--daemon - (EXPERIMENTAL) Builtin file system monitor daemon
SYNOPSIS
--------
[verse]
'git fsmonitor--daemon' --start
'git fsmonitor--daemon' --run
'git fsmonitor--daemon' --stop
'git fsmonitor--daemon' --is-running
'git fsmonitor--daemon' --is-supported
'git fsmonitor--daemon' --query <token>
'git fsmonitor--daemon' --query-index
'git fsmonitor--daemon' --flush
DESCRIPTION
-----------
NOTE! This command is still only an experiment, subject to change dramatically
(or even to be abandoned).
Monitors files and directories in the working directory for changes using
platform-specific file system notification facilities.
It communicates directly with commands like `git status` using the
link:technical/api-simple-ipc.html[simple IPC] interface instead of
the slower linkgit:githooks[5] interface.
OPTIONS
-------
--start::
Starts the fsmonitor daemon in the background.
--run::
Runs the fsmonitor daemon in the foreground.
--stop::
Stops the fsmonitor daemon running for the current working
directory, if present.
--is-running::
Exits with zero status if the fsmonitor daemon is watching the
current working directory.
--is-supported::
Exits with zero status if the fsmonitor daemon feature is supported
on this platform.
--query <token>::
Connects to the fsmonitor daemon (starting it if necessary) and
requests the list of changed files and directories since the
given token.
This is intended for testing purposes.
--query-index::
Read the current `<token>` from the File System Monitor index
extension (if present) and use it to query the fsmonitor daemon.
This is intended for testing purposes.
--flush::
Force the fsmonitor daemon to flush its in-memory cache and
re-sync with the file system.
This is intended for testing purposes.
REMARKS
-------
The fsmonitor daemon is a long running process that will watch a single
working directory. Commands, such as `git status`, should automatically
start it (if necessary) when `core.useBuiltinFSMonitor` is set to `true`
(see linkgit:git-config[1]).
Configure the built-in FSMonitor via `core.useBuiltinFSMonitor` in each
working directory separately, or globally via `git config --global
core.useBuiltinFSMonitor true`.
Tokens are opaque strings. They are used by the fsmonitor daemon to
mark a point in time and the associated internal state. Callers should
make no assumptions about the content of the token. In particular,
the should not assume that it is a timestamp.
Query commands send a request-token to the daemon and it responds with
a summary of the changes that have occurred since that token was
created. The daemon also returns a response-token that the client can
use in a future query.
For more information see the "File System Monitor" section in
linkgit:git-update-index[1].
CAVEATS
-------
The fsmonitor daemon does not currently know about submodules and does
not know to filter out file system events that happen within a
submodule. If fsmonitor daemon is watching a super repo and a file is
modified within the working directory of a submodule, it will report
the change (as happening against the super repo). However, the client
should properly ignore these extra events, so performance may be affected
but it should not cause an incorrect result.
GIT
---
Part of the linkgit:git[1] suite

View File

@@ -498,7 +498,9 @@ FILE SYSTEM MONITOR
This feature is intended to speed up git operations for repos that have
large working directories.
It enables git to work together with a file system monitor (see the
It enables git to work together with a file system monitor (see
linkgit:git-fsmonitor--daemon[1]
and the
"fsmonitor-watchman" section of linkgit:githooks[5]) that can
inform it as to what files have been modified. This enables git to avoid
having to lstat() every file to find modified files.

View File

@@ -593,7 +593,8 @@ fsmonitor-watchman
This hook is invoked when the configuration option `core.fsmonitor` is
set to `.git/hooks/fsmonitor-watchman` or `.git/hooks/fsmonitor-watchmanv2`
depending on the version of the hook to use.
depending on the version of the hook to use, unless overridden via
`core.useBuiltinFSMonitor` (see linkgit:git-config[1]).
Version 1 takes two arguments, a version (1) and the time in elapsed
nanoseconds since midnight, January 1, 1970.

View File

@@ -467,6 +467,11 @@ all::
# directory, and the JSON compilation database 'compile_commands.json' will be
# created at the root of the repository.
#
# If your platform supports an built-in fsmonitor backend, set
# FSMONITOR_DAEMON_BACKEND to the name of the corresponding
# `compat/fsmonitor/fsmonitor-fs-listen-<name>.c` that implements the
# `fsmonitor_fs_listen__*()` routines.
#
# Define DEVELOPER to enable more compiler warnings. Compiler version
# and family are auto detected, but could be overridden by defining
# COMPILER_FEATURES (see config.mak.dev). You can still set
@@ -893,6 +898,7 @@ LIB_OBJS += fetch-pack.o
LIB_OBJS += fmt-merge-msg.o
LIB_OBJS += fsck.o
LIB_OBJS += fsmonitor.o
LIB_OBJS += fsmonitor-ipc.o
LIB_OBJS += gettext.o
LIB_OBJS += gpg-interface.o
LIB_OBJS += graph.o
@@ -1096,6 +1102,7 @@ BUILTIN_OBJS += builtin/fmt-merge-msg.o
BUILTIN_OBJS += builtin/for-each-ref.o
BUILTIN_OBJS += builtin/for-each-repo.o
BUILTIN_OBJS += builtin/fsck.o
BUILTIN_OBJS += builtin/fsmonitor--daemon.o
BUILTIN_OBJS += builtin/gc.o
BUILTIN_OBJS += builtin/get-tar-commit-id.o
BUILTIN_OBJS += builtin/grep.o
@@ -1908,6 +1915,11 @@ ifdef NEED_ACCESS_ROOT_HANDLER
COMPAT_OBJS += compat/access.o
endif
ifdef FSMONITOR_DAEMON_BACKEND
COMPAT_CFLAGS += -DHAVE_FSMONITOR_DAEMON_BACKEND
COMPAT_OBJS += compat/fsmonitor/fsmonitor-fs-listen-$(FSMONITOR_DAEMON_BACKEND).o
endif
ifeq ($(TCLTK_PATH),)
NO_TCLTK = NoThanks
endif
@@ -2774,6 +2786,9 @@ GIT-BUILD-OPTIONS: FORCE
@echo PAGER_ENV=\''$(subst ','\'',$(subst ','\'',$(PAGER_ENV)))'\' >>$@+
@echo DC_SHA1=\''$(subst ','\'',$(subst ','\'',$(DC_SHA1)))'\' >>$@+
@echo X=\'$(X)\' >>$@+
ifdef FSMONITOR_DAEMON_BACKEND
@echo FSMONITOR_DAEMON_BACKEND=\''$(subst ','\'',$(subst ','\'',$(FSMONITOR_DAEMON_BACKEND)))'\' >>$@+
endif
ifdef TEST_OUTPUT_DIRECTORY
@echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@+
endif

View File

@@ -159,6 +159,7 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix);
int cmd_for_each_repo(int argc, const char **argv, const char *prefix);
int cmd_format_patch(int argc, const char **argv, const char *prefix);
int cmd_fsck(int argc, const char **argv, const char *prefix);
int cmd_fsmonitor__daemon(int argc, const char **argv, const char *prefix);
int cmd_gc(int argc, const char **argv, const char *prefix);
int cmd_get_tar_commit_id(int argc, const char **argv, const char *prefix);
int cmd_grep(int argc, const char **argv, const char *prefix);

1611
builtin/fsmonitor--daemon.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1216,14 +1216,14 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)
}
if (fsmonitor > 0) {
if (git_config_get_fsmonitor() == 0)
if (repo_config_get_fsmonitor(r) == 0)
warning(_("core.fsmonitor is unset; "
"set it if you really want to "
"enable fsmonitor"));
add_fsmonitor(&the_index);
report(_("fsmonitor enabled"));
} else if (!fsmonitor) {
if (git_config_get_fsmonitor() == 1)
if (repo_config_get_fsmonitor(r) == 1)
warning(_("core.fsmonitor is set; "
"remove it if you really want to "
"disable fsmonitor"));

View File

@@ -0,0 +1,484 @@
#if defined(__GNUC__)
/*
* It is possible to #include CoreFoundation/CoreFoundation.h when compiling
* with clang, but not with GCC as of time of writing.
*
* See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93082 for details.
*/
typedef unsigned int FSEventStreamCreateFlags;
#define kFSEventStreamEventFlagNone 0x00000000
#define kFSEventStreamEventFlagMustScanSubDirs 0x00000001
#define kFSEventStreamEventFlagUserDropped 0x00000002
#define kFSEventStreamEventFlagKernelDropped 0x00000004
#define kFSEventStreamEventFlagEventIdsWrapped 0x00000008
#define kFSEventStreamEventFlagHistoryDone 0x00000010
#define kFSEventStreamEventFlagRootChanged 0x00000020
#define kFSEventStreamEventFlagMount 0x00000040
#define kFSEventStreamEventFlagUnmount 0x00000080
#define kFSEventStreamEventFlagItemCreated 0x00000100
#define kFSEventStreamEventFlagItemRemoved 0x00000200
#define kFSEventStreamEventFlagItemInodeMetaMod 0x00000400
#define kFSEventStreamEventFlagItemRenamed 0x00000800
#define kFSEventStreamEventFlagItemModified 0x00001000
#define kFSEventStreamEventFlagItemFinderInfoMod 0x00002000
#define kFSEventStreamEventFlagItemChangeOwner 0x00004000
#define kFSEventStreamEventFlagItemXattrMod 0x00008000
#define kFSEventStreamEventFlagItemIsFile 0x00010000
#define kFSEventStreamEventFlagItemIsDir 0x00020000
#define kFSEventStreamEventFlagItemIsSymlink 0x00040000
#define kFSEventStreamEventFlagOwnEvent 0x00080000
#define kFSEventStreamEventFlagItemIsHardlink 0x00100000
#define kFSEventStreamEventFlagItemIsLastHardlink 0x00200000
#define kFSEventStreamEventFlagItemCloned 0x00400000
typedef struct __FSEventStream *FSEventStreamRef;
typedef const FSEventStreamRef ConstFSEventStreamRef;
typedef unsigned int CFStringEncoding;
#define kCFStringEncodingUTF8 0x08000100
typedef const struct __CFString *CFStringRef;
typedef const struct __CFArray *CFArrayRef;
typedef const struct __CFRunLoop *CFRunLoopRef;
struct FSEventStreamContext {
long long version;
void *cb_data, *retain, *release, *copy_description;
};
typedef struct FSEventStreamContext FSEventStreamContext;
typedef unsigned int FSEventStreamEventFlags;
#define kFSEventStreamCreateFlagNoDefer 0x02
#define kFSEventStreamCreateFlagWatchRoot 0x04
#define kFSEventStreamCreateFlagFileEvents 0x10
typedef unsigned long long FSEventStreamEventId;
#define kFSEventStreamEventIdSinceNow 0xFFFFFFFFFFFFFFFFULL
typedef void (*FSEventStreamCallback)(ConstFSEventStreamRef streamRef,
void *context,
__SIZE_TYPE__ num_of_events,
void *event_paths,
const FSEventStreamEventFlags event_flags[],
const FSEventStreamEventId event_ids[]);
typedef double CFTimeInterval;
FSEventStreamRef FSEventStreamCreate(void *allocator,
FSEventStreamCallback callback,
FSEventStreamContext *context,
CFArrayRef paths_to_watch,
FSEventStreamEventId since_when,
CFTimeInterval latency,
FSEventStreamCreateFlags flags);
CFStringRef CFStringCreateWithCString(void *allocator, const char *string,
CFStringEncoding encoding);
CFArrayRef CFArrayCreate(void *allocator, const void **items, long long count,
void *callbacks);
void CFRunLoopRun(void);
void CFRunLoopStop(CFRunLoopRef run_loop);
CFRunLoopRef CFRunLoopGetCurrent(void);
extern CFStringRef kCFRunLoopDefaultMode;
void FSEventStreamScheduleWithRunLoop(FSEventStreamRef stream,
CFRunLoopRef run_loop,
CFStringRef run_loop_mode);
unsigned char FSEventStreamStart(FSEventStreamRef stream);
void FSEventStreamStop(FSEventStreamRef stream);
void FSEventStreamInvalidate(FSEventStreamRef stream);
void FSEventStreamRelease(FSEventStreamRef stream);
#else
/*
* Let Apple's headers declare `isalnum()` first, before
* Git's headers override it via a constant
*/
#include <string.h>
#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>
#endif
#include "cache.h"
#include "fsmonitor.h"
#include "fsmonitor-fs-listen.h"
#include "fsmonitor--daemon.h"
struct fsmonitor_daemon_backend_data
{
CFStringRef cfsr_worktree_path;
CFStringRef cfsr_gitdir_path;
CFArrayRef cfar_paths_to_watch;
int nr_paths_watching;
FSEventStreamRef stream;
CFRunLoopRef rl;
enum shutdown_style {
SHUTDOWN_EVENT = 0,
FORCE_SHUTDOWN,
FORCE_ERROR_STOP,
} shutdown_style;
unsigned int stream_scheduled:1;
unsigned int stream_started:1;
};
static void log_flags_set(const char *path, const FSEventStreamEventFlags flag)
{
struct strbuf msg = STRBUF_INIT;
if (flag & kFSEventStreamEventFlagMustScanSubDirs)
strbuf_addstr(&msg, "MustScanSubDirs|");
if (flag & kFSEventStreamEventFlagUserDropped)
strbuf_addstr(&msg, "UserDropped|");
if (flag & kFSEventStreamEventFlagKernelDropped)
strbuf_addstr(&msg, "KernelDropped|");
if (flag & kFSEventStreamEventFlagEventIdsWrapped)
strbuf_addstr(&msg, "EventIdsWrapped|");
if (flag & kFSEventStreamEventFlagHistoryDone)
strbuf_addstr(&msg, "HistoryDone|");
if (flag & kFSEventStreamEventFlagRootChanged)
strbuf_addstr(&msg, "RootChanged|");
if (flag & kFSEventStreamEventFlagMount)
strbuf_addstr(&msg, "Mount|");
if (flag & kFSEventStreamEventFlagUnmount)
strbuf_addstr(&msg, "Unmount|");
if (flag & kFSEventStreamEventFlagItemChangeOwner)
strbuf_addstr(&msg, "ItemChangeOwner|");
if (flag & kFSEventStreamEventFlagItemCreated)
strbuf_addstr(&msg, "ItemCreated|");
if (flag & kFSEventStreamEventFlagItemFinderInfoMod)
strbuf_addstr(&msg, "ItemFinderInfoMod|");
if (flag & kFSEventStreamEventFlagItemInodeMetaMod)
strbuf_addstr(&msg, "ItemInodeMetaMod|");
if (flag & kFSEventStreamEventFlagItemIsDir)
strbuf_addstr(&msg, "ItemIsDir|");
if (flag & kFSEventStreamEventFlagItemIsFile)
strbuf_addstr(&msg, "ItemIsFile|");
if (flag & kFSEventStreamEventFlagItemIsHardlink)
strbuf_addstr(&msg, "ItemIsHardlink|");
if (flag & kFSEventStreamEventFlagItemIsLastHardlink)
strbuf_addstr(&msg, "ItemIsLastHardlink|");
if (flag & kFSEventStreamEventFlagItemIsSymlink)
strbuf_addstr(&msg, "ItemIsSymlink|");
if (flag & kFSEventStreamEventFlagItemModified)
strbuf_addstr(&msg, "ItemModified|");
if (flag & kFSEventStreamEventFlagItemRemoved)
strbuf_addstr(&msg, "ItemRemoved|");
if (flag & kFSEventStreamEventFlagItemRenamed)
strbuf_addstr(&msg, "ItemRenamed|");
if (flag & kFSEventStreamEventFlagItemXattrMod)
strbuf_addstr(&msg, "ItemXattrMod|");
if (flag & kFSEventStreamEventFlagOwnEvent)
strbuf_addstr(&msg, "OwnEvent|");
if (flag & kFSEventStreamEventFlagItemCloned)
strbuf_addstr(&msg, "ItemCloned|");
trace_printf_key(&trace_fsmonitor, "fsevent: '%s', flags=%u %s",
path, flag, msg.buf);
strbuf_release(&msg);
}
static int ef_is_root_delete(const FSEventStreamEventFlags ef)
{
return (ef & kFSEventStreamEventFlagItemIsDir &&
ef & kFSEventStreamEventFlagItemRemoved);
}
static int ef_is_root_renamed(const FSEventStreamEventFlags ef)
{
return (ef & kFSEventStreamEventFlagItemIsDir &&
ef & kFSEventStreamEventFlagItemRenamed);
}
static void fsevent_callback(ConstFSEventStreamRef streamRef,
void *ctx,
size_t num_of_events,
void *event_paths,
const FSEventStreamEventFlags event_flags[],
const FSEventStreamEventId event_ids[])
{
struct fsmonitor_daemon_state *state = ctx;
struct fsmonitor_daemon_backend_data *data = state->backend_data;
char **paths = (char **)event_paths;
struct fsmonitor_batch *batch = NULL;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
const char *path_k;
const char *slash;
int k;
/*
* Build a list of all filesystem changes into a private/local
* list and without holding any locks.
*/
for (k = 0; k < num_of_events; k++) {
/*
* On Mac, we receive an array of absolute paths.
*/
path_k = paths[k];
/*
* If you want to debug FSEvents, log them to GIT_TRACE_FSMONITOR.
* Please don't log them to Trace2.
*
* trace_printf_key(&trace_fsmonitor, "XXX '%s'", path_k);
*/
/*
* If event[k] is marked as dropped, we assume that we have
* lost sync with the filesystem and should flush our cached
* data. We need to:
*
* [1] Abort/wake any client threads waiting for a cookie and
* flush the cached state data (the current token), and
* create a new token.
*
* [2] Discard the batch that we were locally building (since
* they are conceptually relative to the just flushed
* token).
*/
if ((event_flags[k] & kFSEventStreamEventFlagKernelDropped) ||
(event_flags[k] & kFSEventStreamEventFlagUserDropped)) {
/*
* see also kFSEventStreamEventFlagMustScanSubDirs
*/
trace2_data_string("fsmonitor", NULL,
"fsm-listen/kernel", "dropped");
fsmonitor_force_resync(state);
if (fsmonitor_batch__free(batch))
BUG("batch should not have a next");
string_list_clear(&cookie_list, 0);
/*
* We assume that any events that we received
* in this callback after this dropped event
* may still be valid, so we continue rather
* than break. (And just in case there is a
* delete of ".git" hiding in there.)
*/
continue;
}
switch (fsmonitor_classify_path_absolute(state, path_k)) {
case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
/* special case cookie files within .git or gitdir */
/* Use just the filename of the cookie file. */
slash = find_last_dir_sep(path_k);
string_list_append(&cookie_list,
slash ? slash + 1 : path_k);
break;
case IS_INSIDE_DOT_GIT:
case IS_INSIDE_GITDIR:
/* ignore all other paths inside of .git or gitdir */
break;
case IS_DOT_GIT:
case IS_GITDIR:
/*
* If .git directory is deleted or renamed away,
* we have to quit.
*/
if (ef_is_root_delete(event_flags[k])) {
trace2_data_string("fsmonitor", NULL,
"fsm-listen/gitdir",
"removed");
goto force_shutdown;
}
if (ef_is_root_renamed(event_flags[k])) {
trace2_data_string("fsmonitor", NULL,
"fsm-listen/gitdir",
"renamed");
goto force_shutdown;
}
break;
case IS_WORKDIR_PATH:
/* try to queue normal pathnames */
if (trace_pass_fl(&trace_fsmonitor))
log_flags_set(path_k, event_flags[k]);
/* fsevent could be marked as both a file and directory */
if (event_flags[k] & kFSEventStreamEventFlagItemIsFile) {
const char *rel = path_k +
state->path_worktree_watch.len + 1;
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, rel);
}
if (event_flags[k] & kFSEventStreamEventFlagItemIsDir) {
const char *rel = path_k +
state->path_worktree_watch.len + 1;
char *p = xstrfmt("%s/", rel);
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, p);
free(p);
}
break;
case IS_OUTSIDE_CONE:
default:
trace_printf_key(&trace_fsmonitor,
"ignoring '%s'", path_k);
break;
}
}
fsmonitor_publish(state, batch, &cookie_list);
string_list_clear(&cookie_list, 0);
return;
force_shutdown:
if (fsmonitor_batch__free(batch))
BUG("batch should not have a next");
string_list_clear(&cookie_list, 0);
data->shutdown_style = FORCE_SHUTDOWN;
CFRunLoopStop(data->rl);
return;
}
/*
* TODO Investigate the proper value for the `latency` argument in the call
* TODO to `FSEventStreamCreate()`. I'm not sure that this needs to be a
* TODO config setting or just something that we tune after some testing.
* TODO
* TODO With a latency of 0.1, I was seeing lots of dropped events during
* TODO the "touch 100000" files test within t/perf/p7519, but with a
* TODO latency of 0.001 I did not see any dropped events. So the "correct"
* TODO value may be somewhere in between.
* TODO
* TODO https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate
*/
int fsmonitor_fs_listen__ctor(struct fsmonitor_daemon_state *state)
{
FSEventStreamCreateFlags flags = kFSEventStreamCreateFlagNoDefer |
kFSEventStreamCreateFlagWatchRoot |
kFSEventStreamCreateFlagFileEvents;
FSEventStreamContext ctx = {
0,
state,
NULL,
NULL,
NULL
};
struct fsmonitor_daemon_backend_data *data;
const void *dir_array[2];
data = xcalloc(1, sizeof(*data));
state->backend_data = data;
data->cfsr_worktree_path = CFStringCreateWithCString(
NULL, state->path_worktree_watch.buf, kCFStringEncodingUTF8);
dir_array[data->nr_paths_watching++] = data->cfsr_worktree_path;
if (state->nr_paths_watching > 1) {
data->cfsr_gitdir_path = CFStringCreateWithCString(
NULL, state->path_gitdir_watch.buf,
kCFStringEncodingUTF8);
dir_array[data->nr_paths_watching++] = data->cfsr_gitdir_path;
}
data->cfar_paths_to_watch = CFArrayCreate(NULL, dir_array,
data->nr_paths_watching,
NULL);
data->stream = FSEventStreamCreate(NULL, fsevent_callback, &ctx,
data->cfar_paths_to_watch,
kFSEventStreamEventIdSinceNow,
0.001, flags);
if (data->stream == NULL)
goto failed;
/*
* `data->rl` needs to be set inside the listener thread.
*/
return 0;
failed:
error("Unable to create FSEventStream.");
FREE_AND_NULL(state->backend_data);
return -1;
}
void fsmonitor_fs_listen__dtor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
if (!state || !state->backend_data)
return;
data = state->backend_data;
if (data->stream) {
if (data->stream_started)
FSEventStreamStop(data->stream);
if (data->stream_scheduled)
FSEventStreamInvalidate(data->stream);
FSEventStreamRelease(data->stream);
}
FREE_AND_NULL(state->backend_data);
}
void fsmonitor_fs_listen__stop_async(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
data = state->backend_data;
data->shutdown_style = SHUTDOWN_EVENT;
CFRunLoopStop(data->rl);
}
void fsmonitor_fs_listen__loop(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
data = state->backend_data;
data->rl = CFRunLoopGetCurrent();
FSEventStreamScheduleWithRunLoop(data->stream, data->rl, kCFRunLoopDefaultMode);
data->stream_scheduled = 1;
if (!FSEventStreamStart(data->stream)) {
error("Failed to start the FSEventStream");
goto force_error_stop_without_loop;
}
data->stream_started = 1;
CFRunLoopRun();
switch (data->shutdown_style) {
case FORCE_ERROR_STOP:
state->error_code = -1;
/* fall thru */
case FORCE_SHUTDOWN:
ipc_server_stop_async(state->ipc_server_data);
/* fall thru */
case SHUTDOWN_EVENT:
default:
break;
}
return;
force_error_stop_without_loop:
state->error_code = -1;
ipc_server_stop_async(state->ipc_server_data);
return;
}

View File

@@ -0,0 +1,514 @@
#include "cache.h"
#include "config.h"
#include "fsmonitor.h"
#include "fsmonitor-fs-listen.h"
#include "fsmonitor--daemon.h"
/*
* The documentation of ReadDirectoryChangesW() states that the maximum
* buffer size is 64K when the monitored directory is remote.
*
* Larger buffers may be used when the monitored directory is local and
* will help us receive events faster from the kernel and avoid dropped
* events.
*
* So we try to use a very large buffer and silently fallback to 64K if
* we get an error.
*/
#define MAX_RDCW_BUF_FALLBACK (65536)
#define MAX_RDCW_BUF (65536 * 8)
struct one_watch
{
char buffer[MAX_RDCW_BUF];
DWORD buf_len;
DWORD count;
struct strbuf path;
HANDLE hDir;
HANDLE hEvent;
OVERLAPPED overlapped;
/*
* Is there an active ReadDirectoryChangesW() call pending. If so, we
* need to later call GetOverlappedResult() and possibly CancelIoEx().
*/
BOOL is_active;
};
struct fsmonitor_daemon_backend_data
{
struct one_watch *watch_worktree;
struct one_watch *watch_gitdir;
HANDLE hEventShutdown;
HANDLE hListener[3]; /* we don't own these handles */
#define LISTENER_SHUTDOWN 0
#define LISTENER_HAVE_DATA_WORKTREE 1
#define LISTENER_HAVE_DATA_GITDIR 2
int nr_listener_handles;
};
/*
* Convert the WCHAR path from the notification into UTF8 and
* then normalize it.
*/
static int normalize_path_in_utf8(FILE_NOTIFY_INFORMATION *info,
struct strbuf *normalized_path)
{
int reserve;
int len = 0;
strbuf_reset(normalized_path);
if (!info->FileNameLength)
goto normalize;
/*
* Pre-reserve enough space in the UTF8 buffer for
* each Unicode WCHAR character to be mapped into a
* sequence of 2 UTF8 characters. That should let us
* avoid ERROR_INSUFFICIENT_BUFFER 99.9+% of the time.
*/
reserve = info->FileNameLength + 1;
strbuf_grow(normalized_path, reserve);
for (;;) {
len = WideCharToMultiByte(CP_UTF8, 0, info->FileName,
info->FileNameLength / sizeof(WCHAR),
normalized_path->buf,
strbuf_avail(normalized_path) - 1,
NULL, NULL);
if (len > 0)
goto normalize;
if (GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
error("[GLE %ld] could not convert path to UTF-8: '%.*ls'",
GetLastError(),
(int)(info->FileNameLength / sizeof(WCHAR)),
info->FileName);
return -1;
}
strbuf_grow(normalized_path,
strbuf_avail(normalized_path) + reserve);
}
normalize:
strbuf_setlen(normalized_path, len);
return strbuf_normalize_path(normalized_path);
}
void fsmonitor_fs_listen__stop_async(struct fsmonitor_daemon_state *state)
{
SetEvent(state->backend_data->hListener[LISTENER_SHUTDOWN]);
}
static struct one_watch *create_watch(struct fsmonitor_daemon_state *state,
const char *path)
{
struct one_watch *watch = NULL;
DWORD desired_access = FILE_LIST_DIRECTORY;
DWORD share_mode =
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE;
HANDLE hDir;
hDir = CreateFileA(path,
desired_access, share_mode, NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
NULL);
if (hDir == INVALID_HANDLE_VALUE) {
error(_("[GLE %ld] could not watch '%s'"),
GetLastError(), path);
return NULL;
}
watch = xcalloc(1, sizeof(*watch));
watch->buf_len = sizeof(watch->buffer); /* assume full MAX_RDCW_BUF */
strbuf_init(&watch->path, 0);
strbuf_addstr(&watch->path, path);
watch->hDir = hDir;
watch->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
return watch;
}
static void destroy_watch(struct one_watch *watch)
{
if (!watch)
return;
strbuf_release(&watch->path);
if (watch->hDir != INVALID_HANDLE_VALUE)
CloseHandle(watch->hDir);
if (watch->hEvent != INVALID_HANDLE_VALUE)
CloseHandle(watch->hEvent);
free(watch);
}
static int start_rdcw_watch(struct fsmonitor_daemon_backend_data *data,
struct one_watch *watch)
{
DWORD dwNotifyFilter =
FILE_NOTIFY_CHANGE_FILE_NAME |
FILE_NOTIFY_CHANGE_DIR_NAME |
FILE_NOTIFY_CHANGE_ATTRIBUTES |
FILE_NOTIFY_CHANGE_SIZE |
FILE_NOTIFY_CHANGE_LAST_WRITE |
FILE_NOTIFY_CHANGE_CREATION;
ResetEvent(watch->hEvent);
memset(&watch->overlapped, 0, sizeof(watch->overlapped));
watch->overlapped.hEvent = watch->hEvent;
start_watch:
watch->is_active = ReadDirectoryChangesW(
watch->hDir, watch->buffer, watch->buf_len, TRUE,
dwNotifyFilter, &watch->count, &watch->overlapped, NULL);
if (!watch->is_active &&
GetLastError() == ERROR_INVALID_PARAMETER &&
watch->buf_len > MAX_RDCW_BUF_FALLBACK) {
watch->buf_len = MAX_RDCW_BUF_FALLBACK;
goto start_watch;
}
if (watch->is_active)
return 0;
error("ReadDirectoryChangedW failed on '%s' [GLE %ld]",
watch->path.buf, GetLastError());
return -1;
}
static int recv_rdcw_watch(struct one_watch *watch)
{
watch->is_active = FALSE;
if (GetOverlappedResult(watch->hDir, &watch->overlapped, &watch->count,
TRUE))
return 0;
// TODO If an external <gitdir> is deleted, the above returns an error.
// TODO I'm not sure that there's anything that we can do here other
// TODO than failing -- the <worktree>/.git link file would be broken
// TODO anyway. We might try to check for that and return a better
// TODO error message.
error("GetOverlappedResult failed on '%s' [GLE %ld]",
watch->path.buf, GetLastError());
return -1;
}
static void cancel_rdcw_watch(struct one_watch *watch)
{
DWORD count;
if (!watch || !watch->is_active)
return;
CancelIoEx(watch->hDir, &watch->overlapped);
GetOverlappedResult(watch->hDir, &watch->overlapped, &count, TRUE);
watch->is_active = FALSE;
}
/*
* Process filesystem events that happen anywhere (recursively) under the
* <worktree> root directory. For a normal working directory, this includes
* both version controlled files and the contents of the .git/ directory.
*
* If <worktree>/.git is a file, then we only see events for the file
* itself.
*/
static int process_worktree_events(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct one_watch *watch = data->watch_worktree;
struct strbuf path = STRBUF_INIT;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
struct fsmonitor_batch *batch = NULL;
const char *p = watch->buffer;
/*
* If the kernel gets more events than will fit in the kernel
* buffer associated with our RDCW handle, it drops them and
* returns a count of zero. (A successful call, but with
* length zero.)
*/
if (!watch->count) {
trace2_data_string("fsmonitor", NULL, "fsm-listen/kernel",
"overflow");
fsmonitor_force_resync(state);
return LISTENER_HAVE_DATA_WORKTREE;
}
/*
* On Windows, `info` contains an "array" of paths that are
* relative to the root of whichever directory handle received
* the event.
*/
for (;;) {
FILE_NOTIFY_INFORMATION *info = (void *)p;
const char *slash;
enum fsmonitor_path_type t;
strbuf_reset(&path);
if (normalize_path_in_utf8(info, &path) == -1)
goto skip_this_path;
t = fsmonitor_classify_path_workdir_relative(path.buf);
switch (t) {
case IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX:
/* special case cookie files within .git */
/* Use just the filename of the cookie file. */
slash = find_last_dir_sep(path.buf);
string_list_append(&cookie_list,
slash ? slash + 1 : path.buf);
break;
case IS_INSIDE_DOT_GIT:
/* ignore everything inside of "<worktree>/.git/" */
break;
case IS_DOT_GIT:
/* "<worktree>/.git" was deleted (or renamed away) */
if ((info->Action == FILE_ACTION_REMOVED) ||
(info->Action == FILE_ACTION_RENAMED_OLD_NAME)) {
trace2_data_string("fsmonitor", NULL,
"fsm-listen/dotgit",
"removed");
goto force_shutdown;
}
break;
case IS_WORKDIR_PATH:
/* queue normal pathname */
if (!batch)
batch = fsmonitor_batch__new();
fsmonitor_batch__add_path(batch, path.buf);
break;
case IS_GITDIR:
case IS_INSIDE_GITDIR:
case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
default:
BUG("unexpected path classification '%d' for '%s'",
t, path.buf);
goto skip_this_path;
}
skip_this_path:
if (!info->NextEntryOffset)
break;
p += info->NextEntryOffset;
}
fsmonitor_publish(state, batch, &cookie_list);
batch = NULL;
string_list_clear(&cookie_list, 0);
strbuf_release(&path);
return LISTENER_HAVE_DATA_WORKTREE;
force_shutdown:
fsmonitor_batch__free(batch);
string_list_clear(&cookie_list, 0);
strbuf_release(&path);
return LISTENER_SHUTDOWN;
}
/*
* Process filesystem events that happend anywhere (recursively) under the
* external <gitdir> (such as non-primary worktrees or submodules).
* We only care about cookie files that our client threads created here.
*
* Note that we DO NOT get filesystem events on the external <gitdir>
* itself (it is not inside something that we are watching). In particular,
* we do not get an event if the external <gitdir> is deleted.
*/
static int process_gitdir_events(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
struct one_watch *watch = data->watch_gitdir;
struct strbuf path = STRBUF_INIT;
struct string_list cookie_list = STRING_LIST_INIT_DUP;
const char *p = watch->buffer;
if (!watch->count) {
trace2_data_string("fsmonitor", NULL, "fsm-listen/kernel",
"overflow");
fsmonitor_force_resync(state);
return LISTENER_HAVE_DATA_GITDIR;
}
for (;;) {
FILE_NOTIFY_INFORMATION *info = (void *)p;
const char *slash;
enum fsmonitor_path_type t;
strbuf_reset(&path);
if (normalize_path_in_utf8(info, &path) == -1)
goto skip_this_path;
t = fsmonitor_classify_path_gitdir_relative(path.buf);
trace_printf_key(&trace_fsmonitor, "BBB: %s", path.buf);
switch (t) {
case IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX:
/* special case cookie files within gitdir */
/* Use just the filename of the cookie file. */
slash = find_last_dir_sep(path.buf);
string_list_append(&cookie_list,
slash ? slash + 1 : path.buf);
break;
case IS_INSIDE_GITDIR:
goto skip_this_path;
default:
BUG("unexpected path classification '%d' for '%s'",
t, path.buf);
goto skip_this_path;
}
skip_this_path:
if (!info->NextEntryOffset)
break;
p += info->NextEntryOffset;
}
fsmonitor_publish(state, NULL, &cookie_list);
string_list_clear(&cookie_list, 0);
strbuf_release(&path);
return LISTENER_HAVE_DATA_GITDIR;
}
void fsmonitor_fs_listen__loop(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data = state->backend_data;
DWORD dwWait;
state->error_code = 0;
if (start_rdcw_watch(data, data->watch_worktree) == -1)
goto force_error_stop;
if (data->watch_gitdir &&
start_rdcw_watch(data, data->watch_gitdir) == -1)
goto force_error_stop;
for (;;) {
dwWait = WaitForMultipleObjects(data->nr_listener_handles,
data->hListener,
FALSE, INFINITE);
if (dwWait == WAIT_OBJECT_0 + LISTENER_HAVE_DATA_WORKTREE) {
if (recv_rdcw_watch(data->watch_worktree) == -1)
goto force_error_stop;
if (process_worktree_events(state) == LISTENER_SHUTDOWN)
goto force_shutdown;
if (start_rdcw_watch(data, data->watch_worktree) == -1)
goto force_error_stop;
continue;
}
if (dwWait == WAIT_OBJECT_0 + LISTENER_HAVE_DATA_GITDIR) {
if (recv_rdcw_watch(data->watch_gitdir) == -1)
goto force_error_stop;
if (process_gitdir_events(state) == LISTENER_SHUTDOWN)
goto force_shutdown;
if (start_rdcw_watch(data, data->watch_gitdir) == -1)
goto force_error_stop;
continue;
}
if (dwWait == WAIT_OBJECT_0 + LISTENER_SHUTDOWN)
goto clean_shutdown;
error(_("could not read directory changes [GLE %ld]"),
GetLastError());
goto force_error_stop;
}
force_error_stop:
state->error_code = -1;
force_shutdown:
/*
* Tell the IPC thead pool to stop (which completes the await
* in the main thread (which will also signal this thread (if
* we are still alive))).
*/
ipc_server_stop_async(state->ipc_server_data);
clean_shutdown:
cancel_rdcw_watch(data->watch_worktree);
cancel_rdcw_watch(data->watch_gitdir);
}
int fsmonitor_fs_listen__ctor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
data = xcalloc(1, sizeof(*data));
data->hEventShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
data->watch_worktree = create_watch(state,
state->path_worktree_watch.buf);
if (!data->watch_worktree)
goto failed;
if (state->nr_paths_watching > 1) {
data->watch_gitdir = create_watch(state,
state->path_gitdir_watch.buf);
if (!data->watch_gitdir)
goto failed;
}
data->hListener[LISTENER_SHUTDOWN] = data->hEventShutdown;
data->nr_listener_handles++;
data->hListener[LISTENER_HAVE_DATA_WORKTREE] =
data->watch_worktree->hEvent;
data->nr_listener_handles++;
if (data->watch_gitdir) {
data->hListener[LISTENER_HAVE_DATA_GITDIR] =
data->watch_gitdir->hEvent;
data->nr_listener_handles++;
}
state->backend_data = data;
return 0;
failed:
CloseHandle(data->hEventShutdown);
destroy_watch(data->watch_worktree);
destroy_watch(data->watch_gitdir);
return -1;
}
void fsmonitor_fs_listen__dtor(struct fsmonitor_daemon_state *state)
{
struct fsmonitor_daemon_backend_data *data;
if (!state || !state->backend_data)
return;
data = state->backend_data;
CloseHandle(data->hEventShutdown);
destroy_watch(data->watch_worktree);
destroy_watch(data->watch_gitdir);
FREE_AND_NULL(state->backend_data);
}

View File

@@ -0,0 +1,49 @@
#ifndef FSMONITOR_FS_LISTEN_H
#define FSMONITOR_FS_LISTEN_H
/* This needs to be implemented by each backend */
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
struct fsmonitor_daemon_state;
/*
* Initialize platform-specific data for the fsmonitor listener thread.
* This will be called from the main thread PRIOR to staring the
* fsmonitor_fs_listener thread.
*
* Returns 0 if successful.
* Returns -1 otherwise.
*/
int fsmonitor_fs_listen__ctor(struct fsmonitor_daemon_state *state);
/*
* Cleanup platform-specific data for the fsmonitor listener thread.
* This will be called from the main thread AFTER joining the listener.
*/
void fsmonitor_fs_listen__dtor(struct fsmonitor_daemon_state *state);
/*
* The main body of the platform-specific event loop to watch for
* filesystem events. This will run in the fsmonitor_fs_listen thread.
*
* It should call `ipc_server_stop_async()` if the listener thread
* prematurely terminates (because of a filesystem error or if it
* detects that the .git directory has been deleted). (It should NOT
* do so if the listener thread receives a normal shutdown signal from
* the IPC layer.)
*
* It should set `state->error_code` to -1 if the daemon should exit
* with an error.
*/
void fsmonitor_fs_listen__loop(struct fsmonitor_daemon_state *state);
/*
* Gently request that the fsmonitor listener thread shutdown.
* It does not wait for it to stop. The caller should do a JOIN
* to wait for it.
*/
void fsmonitor_fs_listen__stop_async(struct fsmonitor_daemon_state *state);
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
#endif /* FSMONITOR_FS_LISTEN_H */

View File

@@ -2517,9 +2517,14 @@ int git_config_get_max_percent_split_change(void)
return -1; /* default value */
}
int git_config_get_fsmonitor(void)
int repo_config_get_fsmonitor(struct repository *r)
{
if (git_config_get_pathname("core.fsmonitor", &core_fsmonitor))
if (r->settings.use_builtin_fsmonitor > 0) {
core_fsmonitor = "(built-in daemon)";
return 1;
}
if (repo_config_get_pathname(r, "core.fsmonitor", &core_fsmonitor))
core_fsmonitor = getenv("GIT_TEST_FSMONITOR");
if (core_fsmonitor && !*core_fsmonitor)

View File

@@ -609,7 +609,7 @@ int git_config_get_index_threads(int *dest);
int git_config_get_untracked_cache(void);
int git_config_get_split_index(void);
int git_config_get_max_percent_split_change(void);
int git_config_get_fsmonitor(void);
int repo_config_get_fsmonitor(struct repository *r);
/* This dies if the configured or default date is in the future */
int git_config_get_expiry(const char *key, const char **output);

View File

@@ -147,6 +147,8 @@ ifeq ($(uname_S),Darwin)
MSGFMT = /usr/local/opt/gettext/bin/msgfmt
endif
endif
FSMONITOR_DAEMON_BACKEND = macos
BASIC_LDFLAGS += -framework CoreServices
endif
ifeq ($(uname_S),SunOS)
NEEDS_SOCKET = YesPlease
@@ -420,6 +422,7 @@ ifeq ($(uname_S),Windows)
# so we don't need this:
#
# SNPRINTF_RETURNS_BOGUS = YesPlease
FSMONITOR_DAEMON_BACKEND = win32
NO_SVN_TESTS = YesPlease
RUNTIME_PREFIX = YesPlease
HAVE_WPGMPTR = YesWeDo
@@ -607,6 +610,7 @@ ifneq (,$(findstring MINGW,$(uname_S)))
NO_STRTOUMAX = YesPlease
NO_MKDTEMP = YesPlease
NO_SVN_TESTS = YesPlease
FSMONITOR_DAEMON_BACKEND = win32
RUNTIME_PREFIX = YesPlease
HAVE_WPGMPTR = YesWeDo
NO_ST_BLOCKS_IN_STRUCT_STAT = YesPlease

View File

@@ -259,6 +259,14 @@ else()
list(APPEND compat_SOURCES compat/simple-ipc/ipc-shared.c compat/simple-ipc/ipc-unix-socket.c)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
list(APPEND compat_SOURCES compat/fsmonitor/fsmonitor-fs-listen-win32.c)
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
add_compile_definitions(HAVE_FSMONITOR_DAEMON_BACKEND)
list(APPEND compat_SOURCES compat/fsmonitor/fsmonitor-fs-listen-macos.c)
endif()
set(EXE_EXTENSION ${CMAKE_EXECUTABLE_SUFFIX})
#header checks

142
fsmonitor--daemon.h Normal file
View File

@@ -0,0 +1,142 @@
#ifndef FSMONITOR_DAEMON_H
#define FSMONITOR_DAEMON_H
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
#include "cache.h"
#include "dir.h"
#include "run-command.h"
#include "simple-ipc.h"
#include "thread-utils.h"
struct fsmonitor_batch;
struct fsmonitor_token_data;
/*
* Create a new batch of path(s). The returned batch is considered
* private and not linked into the fsmonitor daemon state. The caller
* should fill this batch with one or more paths and then publish it.
*/
struct fsmonitor_batch *fsmonitor_batch__new(void);
/*
* Free this batch and return the value of the batch->next field.
*/
struct fsmonitor_batch *fsmonitor_batch__free(struct fsmonitor_batch *batch);
/*
* Add this path to this batch of modified files.
*
* The batch should be private and NOT (yet) linked into the fsmonitor
* daemon state and therefore not yet visible to worker threads and so
* no locking is required.
*/
void fsmonitor_batch__add_path(struct fsmonitor_batch *batch, const char *path);
struct fsmonitor_daemon_backend_data; /* opaque platform-specific data */
struct fsmonitor_daemon_state {
pthread_t listener_thread;
pthread_mutex_t main_lock;
struct strbuf path_worktree_watch;
struct strbuf path_gitdir_watch;
int nr_paths_watching;
struct fsmonitor_token_data *current_token_data;
struct strbuf path_cookie_prefix;
pthread_cond_t cookies_cond;
int cookie_seq;
struct hashmap cookies;
int error_code;
struct fsmonitor_daemon_backend_data *backend_data;
struct ipc_server_data *ipc_server_data;
int test_client_delay_ms;
};
/*
* Pathname classifications.
*
* The daemon classifies the pathnames that it receives from file
* system notification events into the following categories and uses
* that to decide whether clients are told about them. (And to watch
* for file system synchronization events.)
*
* The client should only care about paths within the working
* directory proper (inside the working directory and not ".git" nor
* inside of ".git/"). That is, the client has read the index and is
* asking for a list of any paths in the working directory that have
* been modified since the last token. The client does not care about
* file system changes within the .git directory (such as new loose
* objects or packfiles). So the client will only receive paths that
* are classified as IS_WORKDIR_PATH.
*
* The daemon uses the IS_DOT_GIT and IS_GITDIR internally to mean the
* exact ".git" directory or GITDIR. If the daemon receives a delete
* event for either of these directories, it will automatically
* shutdown, for example.
*
* Note that the daemon DOES NOT explicitly watch nor special case the
* ".git/index" file. The daemon does not read the index and does not
* have any internal index-relative state. The daemon only collects
* the set of modified paths within the working directory.
*/
enum fsmonitor_path_type {
IS_WORKDIR_PATH = 0,
IS_DOT_GIT,
IS_INSIDE_DOT_GIT,
IS_INSIDE_DOT_GIT_WITH_COOKIE_PREFIX,
IS_GITDIR,
IS_INSIDE_GITDIR,
IS_INSIDE_GITDIR_WITH_COOKIE_PREFIX,
IS_OUTSIDE_CONE,
};
/*
* Classify a pathname relative to the root of the working directory.
*/
enum fsmonitor_path_type fsmonitor_classify_path_workdir_relative(
const char *relative_path);
/*
* Classify a pathname relative to a <gitdir> that is external to the
* worktree directory.
*/
enum fsmonitor_path_type fsmonitor_classify_path_gitdir_relative(
const char *relative_path);
/*
* Classify an absolute pathname received from a filesystem event.
*/
enum fsmonitor_path_type fsmonitor_classify_path_absolute(
struct fsmonitor_daemon_state *state,
const char *path);
/*
* Prepend the this batch of path(s) onto the list of batches associated
* with the current token. This makes the batch visible to worker threads.
*
* The caller no longer owns the batch and must not free it.
*
* Wake up the client threads waiting on these cookies.
*/
void fsmonitor_publish(struct fsmonitor_daemon_state *state,
struct fsmonitor_batch *batch,
const struct string_list *cookie_names);
/*
* If the platform-specific layer loses sync with the filesystem,
* it should call this to invalidate cached data and abort waiting
* threads.
*/
void fsmonitor_force_resync(struct fsmonitor_daemon_state *state);
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
#endif /* FSMONITOR_DAEMON_H */

153
fsmonitor-ipc.c Normal file
View File

@@ -0,0 +1,153 @@
#include "cache.h"
#include "fsmonitor.h"
#include "fsmonitor-ipc.h"
#include "run-command.h"
#include "strbuf.h"
#include "trace2.h"
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
#define FSMONITOR_DAEMON_IS_SUPPORTED 1
#else
#define FSMONITOR_DAEMON_IS_SUPPORTED 0
#endif
/*
* A trivial function so that this source file always defines at least
* one symbol even when the feature is not supported. This quiets an
* annoying compiler error.
*/
int fsmonitor_ipc__is_supported(void)
{
return FSMONITOR_DAEMON_IS_SUPPORTED;
}
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
GIT_PATH_FUNC(fsmonitor_ipc__get_path, "fsmonitor")
enum ipc_active_state fsmonitor_ipc__get_state(void)
{
return ipc_get_active_state(fsmonitor_ipc__get_path());
}
static int spawn_daemon(void)
{
const char *args[] = { "fsmonitor--daemon", "--start", NULL };
return run_command_v_opt_tr2(args, RUN_COMMAND_NO_STDIN | RUN_GIT_CMD,
"fsmonitor");
}
int fsmonitor_ipc__send_query(const char *since_token,
struct strbuf *answer)
{
int ret = -1;
int tried_to_spawn = 0;
enum ipc_active_state state = IPC_STATE__OTHER_ERROR;
struct ipc_client_connection *connection = NULL;
struct ipc_client_connect_options options
= IPC_CLIENT_CONNECT_OPTIONS_INIT;
options.wait_if_busy = 1;
options.wait_if_not_found = 0;
trace2_region_enter("fsm_client", "query", NULL);
trace2_data_string("fsm_client", NULL, "query/command",
since_token);
try_again:
state = ipc_client_try_connect(fsmonitor_ipc__get_path(), &options,
&connection);
switch (state) {
case IPC_STATE__LISTENING:
ret = ipc_client_send_command_to_connection(
connection, since_token, answer);
ipc_client_close_connection(connection);
trace2_data_intmax("fsm_client", NULL,
"query/response-length", answer->len);
if (fsmonitor_is_trivial_response(answer))
trace2_data_intmax("fsm_client", NULL,
"query/trivial-response", 1);
goto done;
case IPC_STATE__NOT_LISTENING:
ret = error(_("fsmonitor_ipc__send_query: daemon not available"));
goto done;
case IPC_STATE__PATH_NOT_FOUND:
if (tried_to_spawn)
goto done;
tried_to_spawn++;
if (spawn_daemon())
goto done;
/*
* Try again, but this time give the daemon a chance to
* actually create the pipe/socket.
*
* Granted, the daemon just started so it can't possibly have
* any FS cached yet, so we'll always get a trivial answer.
* BUT the answer should include a new token that can serve
* as the basis for subsequent requests.
*/
options.wait_if_not_found = 1;
goto try_again;
case IPC_STATE__INVALID_PATH:
ret = error(_("fsmonitor_ipc__send_query: invalid path '%s'"),
fsmonitor_ipc__get_path());
goto done;
case IPC_STATE__OTHER_ERROR:
default:
ret = error(_("fsmonitor_ipc__send_query: unspecified error on '%s'"),
fsmonitor_ipc__get_path());
goto done;
}
done:
trace2_region_leave("fsm_client", "query", NULL);
return ret;
}
int fsmonitor_ipc__send_command(const char *command,
struct strbuf *answer)
{
struct ipc_client_connection *connection = NULL;
struct ipc_client_connect_options options
= IPC_CLIENT_CONNECT_OPTIONS_INIT;
int ret;
enum ipc_active_state state;
strbuf_reset(answer);
options.wait_if_busy = 1;
options.wait_if_not_found = 0;
state = ipc_client_try_connect(fsmonitor_ipc__get_path(), &options,
&connection);
if (state != IPC_STATE__LISTENING) {
die("fsmonitor--daemon is not running");
return -1;
}
ret = ipc_client_send_command_to_connection(connection, command, answer);
ipc_client_close_connection(connection);
if (ret == -1) {
die("could not send '%s' command to fsmonitor--daemon",
command);
return -1;
}
return 0;
}
#endif

48
fsmonitor-ipc.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef FSMONITOR_IPC_H
#define FSMONITOR_IPC_H
/*
* Returns true if a filesystem notification backend is defined
* for this platform. This symbol must always be visible and
* outside of the HAVE_ ifdef.
*/
int fsmonitor_ipc__is_supported(void);
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
#include "run-command.h"
#include "simple-ipc.h"
/*
* Returns the pathname to the IPC named pipe or Unix domain socket
* where a `git-fsmonitor--daemon` process will listen. This is a
* per-worktree value.
*/
const char *fsmonitor_ipc__get_path(void);
/*
* Try to determine whether there is a `git-fsmonitor--daemon` process
* listening on the IPC pipe/socket.
*/
enum ipc_active_state fsmonitor_ipc__get_state(void);
/*
* Connect to a `git-fsmonitor--daemon` process via simple-ipc
* and ask for the set of changed files since the given token.
*
* This DOES NOT use the hook interface.
*
* Spawn a daemon process in the background if necessary.
*/
int fsmonitor_ipc__send_query(const char *since_token,
struct strbuf *answer);
/*
* Connect to a `git-fsmonitor--daemon` process via simple-ipc and
* send a command verb. If no daemon is available, we DO NOT try to
* start one.
*/
int fsmonitor_ipc__send_command(const char *command,
struct strbuf *answer);
#endif /* HAVE_FSMONITOR_DAEMON_BACKEND */
#endif /* FSMONITOR_IPC_H */

View File

@@ -3,6 +3,7 @@
#include "dir.h"
#include "ewah/ewok.h"
#include "fsmonitor.h"
#include "fsmonitor-ipc.h"
#include "run-command.h"
#include "strbuf.h"
@@ -148,14 +149,27 @@ void write_fsmonitor_extension(struct strbuf *sb, struct index_state *istate)
/*
* Call the query-fsmonitor hook passing the last update token of the saved results.
*/
static int query_fsmonitor(int version, const char *last_update, struct strbuf *query_result)
static int query_fsmonitor(int version, struct index_state *istate, struct strbuf *query_result)
{
struct repository *r = istate->repo ? istate->repo : the_repository;
const char *last_update = istate->fsmonitor_last_update;
struct child_process cp = CHILD_PROCESS_INIT;
int result;
if (!core_fsmonitor)
return -1;
if (r->settings.use_builtin_fsmonitor > 0) {
#ifdef HAVE_FSMONITOR_DAEMON_BACKEND
return fsmonitor_ipc__send_query(last_update, query_result);
#else
/* Fake a trivial response. */
warning(_("fsmonitor--daemon unavailable; falling back"));
strbuf_add(query_result, "/", 2);
return 0;
#endif
}
strvec_push(&cp.args, core_fsmonitor);
strvec_pushf(&cp.args, "%d", version);
strvec_pushf(&cp.args, "%s", last_update);
@@ -263,7 +277,7 @@ void refresh_fsmonitor(struct index_state *istate)
if (istate->fsmonitor_last_update) {
if (hook_version == -1 || hook_version == HOOK_INTERFACE_VERSION2) {
query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION2,
istate->fsmonitor_last_update, &query_result);
istate, &query_result);
if (query_success) {
if (hook_version < 0)
@@ -293,7 +307,7 @@ void refresh_fsmonitor(struct index_state *istate)
if (hook_version == HOOK_INTERFACE_VERSION1) {
query_success = !query_fsmonitor(HOOK_INTERFACE_VERSION1,
istate->fsmonitor_last_update, &query_result);
istate, &query_result);
}
trace_performance_since(last_update, "fsmonitor process '%s'", core_fsmonitor);
@@ -339,6 +353,16 @@ void refresh_fsmonitor(struct index_state *istate)
}
strbuf_release(&query_result);
/*
* If the fsmonitor response and the subsequent scan of the disk
* did not cause the in-memory index to be marked dirty, then force
* it so that we advance the fsmonitor token in our extension, so
* that future requests don't keep re-requesting the same range.
*/
if (istate->fsmonitor_last_update &&
strcmp(istate->fsmonitor_last_update, last_update_token.buf))
istate->cache_changed |= FSMONITOR_CHANGED;
/* Now that we've updated istate, save the last_update_token */
FREE_AND_NULL(istate->fsmonitor_last_update);
istate->fsmonitor_last_update = strbuf_detach(&last_update_token, NULL);
@@ -411,7 +435,7 @@ void remove_fsmonitor(struct index_state *istate)
void tweak_fsmonitor(struct index_state *istate)
{
unsigned int i;
int fsmonitor_enabled = git_config_get_fsmonitor();
int fsmonitor_enabled = repo_config_get_fsmonitor(istate->repo ? istate->repo : the_repository);
if (istate->fsmonitor_dirty) {
if (fsmonitor_enabled) {

1
git.c
View File

@@ -533,6 +533,7 @@ static struct cmd_struct commands[] = {
{ "format-patch", cmd_format_patch, RUN_SETUP },
{ "fsck", cmd_fsck, RUN_SETUP },
{ "fsck-objects", cmd_fsck, RUN_SETUP },
{ "fsmonitor--daemon", cmd_fsmonitor__daemon, RUN_SETUP },
{ "gc", cmd_gc, RUN_SETUP },
{ "get-tar-commit-id", cmd_get_tar_commit_id, NO_PARSEOPT },
{ "grep", cmd_grep, RUN_SETUP_GENTLY },

4
help.c
View File

@@ -11,6 +11,7 @@
#include "version.h"
#include "refs.h"
#include "parse-options.h"
#include "fsmonitor-ipc.h"
struct category_description {
uint32_t category;
@@ -664,6 +665,9 @@ void get_version_info(struct strbuf *buf, int show_build_options)
strbuf_addf(buf, "sizeof-size_t: %d\n", (int)sizeof(size_t));
strbuf_addf(buf, "shell-path: %s\n", SHELL_PATH);
/* NEEDSWORK: also save and output GIT-BUILD_OPTIONS? */
if (fsmonitor_ipc__is_supported())
strbuf_addstr(buf, "feature: fsmonitor--daemon\n");
}
}

View File

@@ -2,12 +2,13 @@
#include "config.h"
#include "repository.h"
#include "midx.h"
#include "fsmonitor-ipc.h"
#define UPDATE_DEFAULT_BOOL(s,v) do { if (s == -1) { s = v; } } while(0)
void prepare_repo_settings(struct repository *r)
{
int value;
int value, feature_many_files = 0;
char *strval;
if (r->settings.initialized)
@@ -58,7 +59,11 @@ void prepare_repo_settings(struct repository *r)
r->settings.core_multi_pack_index = value;
UPDATE_DEFAULT_BOOL(r->settings.core_multi_pack_index, 1);
if (!repo_config_get_bool(r, "core.usebuiltinfsmonitor", &value) && value)
r->settings.use_builtin_fsmonitor = 1;
if (!repo_config_get_bool(r, "feature.manyfiles", &value) && value) {
feature_many_files = 1;
UPDATE_DEFAULT_BOOL(r->settings.index_version, 4);
UPDATE_DEFAULT_BOOL(r->settings.core_untracked_cache, UNTRACKED_CACHE_WRITE);
}
@@ -67,8 +72,12 @@ void prepare_repo_settings(struct repository *r)
r->settings.fetch_write_commit_graph = value;
UPDATE_DEFAULT_BOOL(r->settings.fetch_write_commit_graph, 0);
if (!repo_config_get_bool(r, "feature.experimental", &value) && value)
if (!repo_config_get_bool(r, "feature.experimental", &value) && value) {
UPDATE_DEFAULT_BOOL(r->settings.fetch_negotiation_algorithm, FETCH_NEGOTIATION_SKIPPING);
if (feature_many_files && fsmonitor_ipc__is_supported())
UPDATE_DEFAULT_BOOL(r->settings.use_builtin_fsmonitor,
1);
}
/* Hack for test programs like test-dump-untracked-cache */
if (ignore_untracked_cache_config)

View File

@@ -42,6 +42,8 @@ struct repo_settings {
int core_multi_pack_index;
int use_builtin_fsmonitor;
unsigned command_requires_full_index:1,
sparse_index:1;
};

View File

@@ -24,7 +24,8 @@ test_description="Test core.fsmonitor"
# GIT_PERF_7519_SPLIT_INDEX: used to configure core.splitIndex
# GIT_PERF_7519_FSMONITOR: used to configure core.fsMonitor. May be an
# absolute path to an integration. May be a space delimited list of
# absolute paths to integrations.
# absolute paths to integrations. (This hook or list of hooks does not
# include the built-in fsmonitor--daemon.)
#
# The big win for using fsmonitor is the elimination of the need to scan the
# working directory looking for changed and untracked files. If the file
@@ -135,10 +136,16 @@ test_expect_success "one time repo setup" '
setup_for_fsmonitor() {
# set INTEGRATION_SCRIPT depending on the environment
if test -n "$INTEGRATION_PATH"
if test -n "$USE_FSMONITOR_DAEMON"
then
git config core.useBuiltinFSMonitor true &&
INTEGRATION_SCRIPT=false
elif test -n "$INTEGRATION_PATH"
then
git config core.useBuiltinFSMonitor false &&
INTEGRATION_SCRIPT="$INTEGRATION_PATH"
else
git config core.useBuiltinFSMonitor false &&
#
# Choose integration script based on existence of Watchman.
# Fall back to an empty integration script.
@@ -285,4 +292,30 @@ test_expect_success "setup without fsmonitor" '
test_fsmonitor_suite
trace_stop
#
# Run a full set of perf tests using the built-in fsmonitor--daemon.
# It does not use the Hook API, so it has a different setup.
# Explicitly start the daemon here and before we start client commands
# so that we can later add custom tracing.
#
test_lazy_prereq HAVE_FSMONITOR_DAEMON '
git version --build-options | grep "feature:" | grep "fsmonitor--daemon"
'
if test_have_prereq HAVE_FSMONITOR_DAEMON
then
USE_FSMONITOR_DAEMON=t
trace_start fsmonitor--daemon--server
git fsmonitor--daemon --start
trace_start fsmonitor--daemon--client
test_expect_success "setup for fsmonitor--daemon" 'setup_for_fsmonitor'
test_fsmonitor_suite
git fsmonitor--daemon --stop
trace_stop
fi
test_done

582
t/t7527-builtin-fsmonitor.sh Executable file
View File

@@ -0,0 +1,582 @@
#!/bin/sh
test_description='built-in file system watcher'
. ./test-lib.sh
# Ask the fsmonitor daemon to insert a little delay before responding to
# client commands like `git status` and `git fsmonitor--daemon --query` to
# allow recent filesystem events to be received by the daemon. This helps
# the CI/PR builds be more stable.
#
# An arbitrary millisecond value.
#
GIT_TEST_FSMONITOR_CLIENT_DELAY=1000
export GIT_TEST_FSMONITOR_CLIENT_DELAY
git version --build-options | grep "feature:" | grep "fsmonitor--daemon" || {
skip_all="The built-in FSMonitor is not supported on this platform"
test_done
}
kill_repo () {
r=$1
git -C $r fsmonitor--daemon --stop >/dev/null 2>/dev/null
rm -rf $1
return 0
}
start_daemon () {
case "$#" in
1) r="-C $1";;
*) r="";
esac
git $r fsmonitor--daemon --start || return $?
git $r fsmonitor--daemon --is-running || return $?
return 0
}
test_expect_success 'explicit daemon start and stop' '
test_when_finished "kill_repo test_explicit" &&
git init test_explicit &&
start_daemon test_explicit &&
git -C test_explicit fsmonitor--daemon --stop &&
test_must_fail git -C test_explicit fsmonitor--daemon --is-running
'
test_expect_success 'implicit daemon start' '
test_when_finished "kill_repo test_implicit" &&
git init test_implicit &&
test_must_fail git -C test_implicit fsmonitor--daemon --is-running &&
# query will implicitly start the daemon.
#
# for test-script simplicity, we send a V1 timestamp rather than
# a V2 token. either way, the daemon response to any query contains
# a new V2 token. (the daemon may complain that we sent a V1 request,
# but this test case is only concerned with whether the daemon was
# implicitly started.)
GIT_TRACE2_EVENT="$PWD/.git/trace" \
git -C test_implicit fsmonitor--daemon --query 0 >actual &&
nul_to_q <actual >actual.filtered &&
grep "builtin:" actual.filtered &&
# confirm that a daemon was started in the background.
#
# since the mechanism for starting the background daemon is platform
# dependent, just confirm that the foreground command received a
# response from the daemon.
grep :\"query/response-length\" .git/trace &&
git -C test_implicit fsmonitor--daemon --is-running &&
git -C test_implicit fsmonitor--daemon --stop &&
test_must_fail git -C test_implicit fsmonitor--daemon --is-running
'
test_expect_success 'implicit daemon stop (delete .git)' '
test_when_finished "kill_repo test_implicit_1" &&
git init test_implicit_1 &&
start_daemon test_implicit_1 &&
# deleting the .git directory will implicitly stop the daemon.
rm -rf test_implicit_1/.git &&
# Create an empty .git directory so that the following Git command
# will stay relative to the `-C` directory. Without this, the Git
# command will (override the requested -C argument) and crawl out
# to the containing Git source tree. This would make the test
# result dependent upon whether we were using fsmonitor on our
# development worktree.
sleep 1 &&
mkdir test_implicit_1/.git &&
test_must_fail git -C test_implicit_1 fsmonitor--daemon --is-running
'
test_expect_success 'implicit daemon stop (rename .git)' '
test_when_finished "kill_repo test_implicit_2" &&
git init test_implicit_2 &&
start_daemon test_implicit_2 &&
# renaming the .git directory will implicitly stop the daemon.
mv test_implicit_2/.git test_implicit_2/.xxx &&
# Create an empty .git directory so that the following Git command
# will stay relative to the `-C` directory. Without this, the Git
# command will (override the requested -C argument) and crawl out
# to the containing Git source tree. This would make the test
# result dependent upon whether we were using fsmonitor on our
# development worktree.
sleep 1 &&
mkdir test_implicit_2/.git &&
test_must_fail git -C test_implicit_2 fsmonitor--daemon --is-running
'
test_expect_success 'cannot start multiple daemons' '
test_when_finished "kill_repo test_multiple" &&
git init test_multiple &&
start_daemon test_multiple &&
test_must_fail git -C test_multiple fsmonitor--daemon --start 2>actual &&
grep "fsmonitor--daemon is already running" actual &&
git -C test_multiple fsmonitor--daemon --stop &&
test_must_fail git -C test_multiple fsmonitor--daemon --is-running
'
test_expect_success 'setup' '
>tracked &&
>modified &&
>delete &&
>rename &&
mkdir dir1 &&
>dir1/tracked &&
>dir1/modified &&
>dir1/delete &&
>dir1/rename &&
mkdir dir2 &&
>dir2/tracked &&
>dir2/modified &&
>dir2/delete &&
>dir2/rename &&
mkdir dirtorename &&
>dirtorename/a &&
>dirtorename/b &&
cat >.gitignore <<-\EOF &&
.gitignore
expect*
actual*
flush*
trace*
EOF
git -c core.useBuiltinFSMonitor= add . &&
test_tick &&
git -c core.useBuiltinFSMonitor= commit -m initial &&
git config core.useBuiltinFSMonitor true
'
test_expect_success 'update-index implicitly starts daemon' '
test_must_fail git fsmonitor--daemon --is-running &&
GIT_TRACE2_EVENT="$PWD/.git/trace_implicit_1" \
git update-index --fsmonitor &&
git fsmonitor--daemon --is-running &&
test_might_fail git fsmonitor--daemon --stop &&
grep \"event\":\"start\".*\"fsmonitor--daemon\" .git/trace_implicit_1
'
test_expect_success 'status implicitly starts daemon' '
test_must_fail git fsmonitor--daemon --is-running &&
GIT_TRACE2_EVENT="$PWD/.git/trace_implicit_2" \
git status >actual &&
git fsmonitor--daemon --is-running &&
test_might_fail git fsmonitor--daemon --stop &&
grep \"event\":\"start\".*\"fsmonitor--daemon\" .git/trace_implicit_2
'
edit_files() {
echo 1 >modified
echo 2 >dir1/modified
echo 3 >dir2/modified
>dir1/untracked
}
delete_files() {
rm -f delete
rm -f dir1/delete
rm -f dir2/delete
}
create_files() {
echo 1 >new
echo 2 >dir1/new
echo 3 >dir2/new
}
rename_files() {
mv rename renamed
mv dir1/rename dir1/renamed
mv dir2/rename dir2/renamed
}
file_to_directory() {
rm -f delete
mkdir delete
echo 1 >delete/new
}
directory_to_file() {
rm -rf dir1
echo 1 >dir1
}
verify_status() {
git status >actual &&
GIT_INDEX_FILE=.git/fresh-index git read-tree master &&
GIT_INDEX_FILE=.git/fresh-index git -c core.useBuiltinFSMonitor= status >expect &&
test_cmp expect actual &&
echo HELLO AFTER &&
cat .git/trace &&
echo HELLO AFTER
}
# The next few test cases confirm that our fsmonitor daemon sees each type
# of OS filesystem notification that we care about. At this layer we just
# ensure we are getting the OS notifications and do not try to confirm what
# is reported by `git status`.
#
# We run a simple query after modifying the filesystem just to introduce
# a bit of a delay so that the trace logging from the daemon has time to
# get flushed to disk.
#
# We `reset` and `clean` at the bottom of each test (and before stopping the
# daemon) because these commands might implicitly restart the daemon.
clean_up_repo_and_stop_daemon () {
git reset --hard HEAD
git clean -fd
git fsmonitor--daemon --stop
rm -f .git/trace
}
test_expect_success 'edit some files' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
edit_files &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dir1/modified$" .git/trace &&
grep "^event: dir2/modified$" .git/trace &&
grep "^event: modified$" .git/trace &&
grep "^event: dir1/untracked$" .git/trace
'
test_expect_success 'create some files' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
create_files &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dir1/new$" .git/trace &&
grep "^event: dir2/new$" .git/trace &&
grep "^event: new$" .git/trace
'
test_expect_success 'delete some files' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
delete_files &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dir1/delete$" .git/trace &&
grep "^event: dir2/delete$" .git/trace &&
grep "^event: delete$" .git/trace
'
test_expect_success 'rename some files' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
rename_files &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dir1/rename$" .git/trace &&
grep "^event: dir2/rename$" .git/trace &&
grep "^event: rename$" .git/trace &&
grep "^event: dir1/renamed$" .git/trace &&
grep "^event: dir2/renamed$" .git/trace &&
grep "^event: renamed$" .git/trace
'
test_expect_success 'rename directory' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
mv dirtorename dirrenamed &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dirtorename/*$" .git/trace &&
grep "^event: dirrenamed/*$" .git/trace
'
test_expect_success 'file changes to directory' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
file_to_directory &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: delete$" .git/trace &&
grep "^event: delete/new$" .git/trace
'
test_expect_success 'directory changes to a file' '
test_when_finished "clean_up_repo_and_stop_daemon" &&
(
GIT_TRACE_FSMONITOR="$PWD/.git/trace" &&
export GIT_TRACE_FSMONITOR &&
start_daemon
) &&
directory_to_file &&
git fsmonitor--daemon --query 0 >/dev/null 2>&1 &&
grep "^event: dir1$" .git/trace
'
# The next few test cases exercise the token-resync code. When filesystem
# drops events (because of filesystem velocity or because the daemon isn't
# polling fast enough), we need to discard the cached data (relative to the
# current token) and start collecting events under a new token.
#
# the 'git fsmonitor--daemon --flush' command can be used to send a "flush"
# message to a running daemon and ask it to do a flush/resync.
test_expect_success 'flush cached data' '
test_when_finished "kill_repo test_flush" &&
git init test_flush &&
(
GIT_TEST_FSMONITOR_TOKEN=true &&
export GIT_TEST_FSMONITOR_TOKEN &&
GIT_TRACE_FSMONITOR="$PWD/.git/trace_daemon" &&
export GIT_TRACE_FSMONITOR &&
start_daemon test_flush
) &&
# The daemon should have an initial token with no events in _0 and
# then a few (probably platform-specific number of) events in _1.
# These should both have the same <token_id>.
git -C test_flush fsmonitor--daemon --query "builtin:test_00000001:0" >actual_0 &&
nul_to_q <actual_0 >actual_q0 &&
touch test_flush/file_1 &&
touch test_flush/file_2 &&
git -C test_flush fsmonitor--daemon --query "builtin:test_00000001:0" >actual_1 &&
nul_to_q <actual_1 >actual_q1 &&
grep "file_1" actual_q1 &&
# Force a flush. This will change the <token_id>, reset the <seq_nr>, and
# flush the file data. Then create some events and ensure that the file
# again appears in the cache. It should have the new <token_id>.
git -C test_flush fsmonitor--daemon --flush >flush_0 &&
nul_to_q <flush_0 >flush_q0 &&
grep "^builtin:test_00000002:0Q/Q$" flush_q0 &&
git -C test_flush fsmonitor--daemon --query "builtin:test_00000002:0" >actual_2 &&
nul_to_q <actual_2 >actual_q2 &&
grep "^builtin:test_00000002:0Q$" actual_q2 &&
touch test_flush/file_3 &&
git -C test_flush fsmonitor--daemon --query "builtin:test_00000002:0" >actual_3 &&
nul_to_q <actual_3 >actual_q3 &&
grep "file_3" actual_q3
'
# The next few test cases create repos where the .git directory is NOT
# inside the one of the working directory. That is, where .git is a file
# that points to a directory elsewhere. This happens for submodules and
# non-primary worktrees.
test_expect_success 'setup worktree base' '
git init wt-base &&
echo 1 >wt-base/file1 &&
git -C wt-base add file1 &&
git -C wt-base commit -m "c1"
'
test_expect_success 'worktree with .git file' '
git -C wt-base worktree add ../wt-secondary &&
(
GIT_TRACE2_PERF="$PWD/trace2_wt_secondary" &&
export GIT_TRACE2_PERF &&
GIT_TRACE_FSMONITOR="$PWD/trace_wt_secondary" &&
export GIT_TRACE_FSMONITOR &&
start_daemon wt-secondary
) &&
git -C wt-secondary fsmonitor--daemon --stop &&
test_must_fail git -C wt-secondary fsmonitor--daemon --is-running
'
# TODO Repeat one of the "edit" tests on wt-secondary and confirm that
# TODO we get the same events and behavior -- that is, that fsmonitor--daemon
# TODO correctly listens to events on both the working directory and to the
# TODO referenced GITDIR.
test_expect_success 'cleanup worktrees' '
kill_repo wt-secondary &&
kill_repo wt-base
'
# The next few tests perform arbitrary/contrived file operations and
# confirm that status is correct. That is, that the data (or lack of
# data) from fsmonitor doesn't cause incorrect results. And doesn't
# cause incorrect results when the untracked-cache is enabled.
test_lazy_prereq UNTRACKED_CACHE '
{ git update-index --test-untracked-cache; ret=$?; } &&
test $ret -ne 1
'
test_expect_success 'Matrix: setup for untracked-cache,fsmonitor matrix' '
test_might_fail git config --unset core.useBuiltinFSMonitor &&
git update-index --no-fsmonitor &&
test_might_fail git fsmonitor--daemon --stop
'
matrix_clean_up_repo () {
git reset --hard HEAD
git clean -fd
}
matrix_try () {
uc=$1
fsm=$2
fn=$3
test_expect_success "Matrix[uc:$uc][fsm:$fsm] $fn" '
matrix_clean_up_repo &&
$fn &&
if test $uc = false -a $fsm = false
then
git status --porcelain=v1 >.git/expect.$fn
else
git status --porcelain=v1 >.git/actual.$fn
test_cmp .git/expect.$fn .git/actual.$fn
fi
'
return $?
}
uc_values="false"
test_have_prereq UNTRACKED_CACHE && uc_values="false true"
for uc_val in $uc_values
do
if test $uc_val = false
then
test_expect_success "Matrix[uc:$uc_val] disable untracked cache" '
git config core.untrackedcache false &&
git update-index --no-untracked-cache
'
else
test_expect_success "Matrix[uc:$uc_val] enable untracked cache" '
git config core.untrackedcache true &&
git update-index --untracked-cache
'
fi
fsm_values="false true"
for fsm_val in $fsm_values
do
if test $fsm_val = false
then
test_expect_success "Matrix[uc:$uc_val][fsm:$fsm_val] disable fsmonitor" '
test_might_fail git config --unset core.useBuiltinFSMonitor &&
git update-index --no-fsmonitor &&
test_might_fail git fsmonitor--daemon --stop 2>/dev/null
'
else
test_expect_success "Matrix[uc:$uc_val][fsm:$fsm_val] enable fsmonitor" '
git config core.useBuiltinFSMonitor true &&
git fsmonitor--daemon --start &&
git update-index --fsmonitor
'
fi
matrix_try $uc_val $fsm_val edit_files
matrix_try $uc_val $fsm_val delete_files
matrix_try $uc_val $fsm_val create_files
matrix_try $uc_val $fsm_val rename_files
matrix_try $uc_val $fsm_val file_to_directory
matrix_try $uc_val $fsm_val directory_to_file
done
done
test_done