scalar: enable built-in FSMonitor on `register`

Using the built-in FSMonitor makes many common commands quite a bit faster. So let's teach the `scalar register` command to enable the built-in FSMonitor and kick-start the fsmonitor--daemon process (for convenience). For simplicity, we only support the built-in FSMonitor (and no external file system monitor such as e.g. Watchman). Helped-by: Junio C Hamano <gitster@pobox.com> Helped-by: Derrick Stolee <derrickstolee@github.com> Signed-off-by: Matthew John Cheetham <mjcheetham@outlook.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Victoria Dye <vdye@github.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthew John Cheetham committed Aug 18, 2022 at 21:40 UTC 3f1917dc608021d7e94f4aaed9ecc9f58f9e32a4
2 files changed +38
contrib/scalar/scalar.c
+30
@@ -7,6 +7,9 @@
7 #include "parse-options.h"
8 #include "config.h"
9 #include "run-command.h"
10 +#include "simple-ipc.h"
11 +#include "fsmonitor-ipc.h"
12 +#include "fsmonitor-settings.h"
13 #include "refs.h"
14 #include "dir.h"
15 #include "packfile.h"
@@ -109,6 +112,12 @@ static int set_scalar_config(const struct scalar_config *config, int reconfigure
112 return res;
113 }
114
115 +static int have_fsmonitor_support(void)
116 +{
117 + return fsmonitor_ipc__is_supported() &&
118 + fsm_settings__get_reason(the_repository) == FSMONITOR_REASON_OK;
119 +}
120 +
121 static int set_recommended_config(int reconfigure)
122 {
123 struct scalar_config config[] = {
@@ -170,6 +179,13 @@ static int set_recommended_config(int reconfigure)
179 config[i].key, config[i].value);
180 }
181
182 + if (have_fsmonitor_support()) {
183 + struct scalar_config fsmonitor = { "core.fsmonitor", "true" };
184 + if (set_scalar_config(&fsmonitor, reconfigure))
185 + return error(_("could not configure %s=%s"),
186 + fsmonitor.key, fsmonitor.value);
187 + }
188 +
189 /*
190 * The `log.excludeDecoration` setting is special because it allows
191 * for multiple values.
@@ -218,6 +234,16 @@ static int add_or_remove_enlistment(int add)
234 "scalar.repo", the_repository->worktree, NULL);
235 }
236
237 +static int start_fsmonitor_daemon(void)
238 +{
239 + assert(have_fsmonitor_support());
240 +
241 + if (fsmonitor_ipc__get_state() != IPC_STATE__LISTENING)
242 + return run_git("fsmonitor--daemon", "start", NULL);
243 +
244 + return 0;
245 +}
246 +
247 static int register_dir(void)
248 {
249 if (add_or_remove_enlistment(1))
@@ -229,6 +255,10 @@ static int register_dir(void)
255 if (toggle_maintenance(1))
256 return error(_("could not turn on maintenance"));
257
258 + if (have_fsmonitor_support() && start_fsmonitor_daemon()) {
259 + return error(_("could not start the FSMonitor daemon"));
260 + }
261 +
262 return 0;
263 }
264
contrib/scalar/t/t9099-scalar.sh
+8
@@ -102,6 +102,14 @@ test_expect_success 'scalar enlistments need a worktree' '
102 grep "Scalar enlistments require a worktree" err
103 '
104
105 +test_expect_success FSMONITOR_DAEMON 'scalar register starts fsmon daemon' '
106 + git init test/src &&
107 + test_must_fail git -C test/src fsmonitor--daemon status &&
108 + scalar register test/src &&
109 + git -C test/src fsmonitor--daemon status &&
110 + test_cmp_config -C test/src true core.fsmonitor
111 +'
112 +
113 test_expect_success 'scalar unregister' '
114 git init vanish/src &&
115 scalar register vanish/src &&