Skip to content

Commit 148405f

Browse files
jeffhostetlergitster
authored andcommitted
t/helper/fsmonitor-client: create IPC client to talk to FSMonitor Daemon
Create an IPC client to send query and flush commands to the daemon. Signed-off-by: Jeff Hostetler <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent dd77cf6 commit 148405f

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ TEST_BUILTINS_OBJS += test-dump-split-index.o
716716
TEST_BUILTINS_OBJS += test-dump-untracked-cache.o
717717
TEST_BUILTINS_OBJS += test-example-decorate.o
718718
TEST_BUILTINS_OBJS += test-fast-rebase.o
719+
TEST_BUILTINS_OBJS += test-fsmonitor-client.o
719720
TEST_BUILTINS_OBJS += test-genrandom.o
720721
TEST_BUILTINS_OBJS += test-genzeros.o
721722
TEST_BUILTINS_OBJS += test-getcwd.o

t/helper/test-fsmonitor-client.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* test-fsmonitor-client.c: client code to send commands/requests to
3+
* a `git fsmonitor--daemon` daemon.
4+
*/
5+
6+
#include "test-tool.h"
7+
#include "cache.h"
8+
#include "parse-options.h"
9+
#include "fsmonitor-ipc.h"
10+
11+
#ifndef HAVE_FSMONITOR_DAEMON_BACKEND
12+
int cmd__fsmonitor_client(int argc, const char **argv)
13+
{
14+
die("fsmonitor--daemon not available on this platform");
15+
}
16+
#else
17+
18+
/*
19+
* Read the `.git/index` to get the last token written to the
20+
* FSMonitor Index Extension.
21+
*/
22+
static const char *get_token_from_index(void)
23+
{
24+
struct index_state *istate = the_repository->index;
25+
26+
if (do_read_index(istate, the_repository->index_file, 0) < 0)
27+
die("unable to read index file");
28+
if (!istate->fsmonitor_last_update)
29+
die("index file does not have fsmonitor extension");
30+
31+
return istate->fsmonitor_last_update;
32+
}
33+
34+
/*
35+
* Send an IPC query to a `git-fsmonitor--daemon` daemon and
36+
* ask for the changes since the given token or from the last
37+
* token in the index extension.
38+
*
39+
* This will implicitly start a daemon process if necessary. The
40+
* daemon process will persist after we exit.
41+
*/
42+
static int do_send_query(const char *token)
43+
{
44+
struct strbuf answer = STRBUF_INIT;
45+
int ret;
46+
47+
if (!token || !*token)
48+
token = get_token_from_index();
49+
50+
ret = fsmonitor_ipc__send_query(token, &answer);
51+
if (ret < 0)
52+
die("could not query fsmonitor--daemon");
53+
54+
write_in_full(1, answer.buf, answer.len);
55+
strbuf_release(&answer);
56+
57+
return 0;
58+
}
59+
60+
/*
61+
* Send a "flush" command to the `git-fsmonitor--daemon` (if running)
62+
* and tell it to flush its cache.
63+
*
64+
* This feature is primarily used by the test suite to simulate a loss of
65+
* sync with the filesystem where we miss kernel events.
66+
*/
67+
static int do_send_flush(void)
68+
{
69+
struct strbuf answer = STRBUF_INIT;
70+
int ret;
71+
72+
ret = fsmonitor_ipc__send_command("flush", &answer);
73+
if (ret)
74+
return ret;
75+
76+
write_in_full(1, answer.buf, answer.len);
77+
strbuf_release(&answer);
78+
79+
return 0;
80+
}
81+
82+
int cmd__fsmonitor_client(int argc, const char **argv)
83+
{
84+
const char *subcmd;
85+
const char *token = NULL;
86+
87+
const char * const fsmonitor_client_usage[] = {
88+
"test-tool fsmonitor-client query [<token>]",
89+
"test-tool fsmonitor-client flush",
90+
NULL,
91+
};
92+
93+
struct option options[] = {
94+
OPT_STRING(0, "token", &token, "token",
95+
"command token to send to the server"),
96+
OPT_END()
97+
};
98+
99+
argc = parse_options(argc, argv, NULL, options, fsmonitor_client_usage, 0);
100+
101+
if (argc != 1)
102+
usage_with_options(fsmonitor_client_usage, options);
103+
104+
subcmd = argv[0];
105+
106+
setup_git_directory();
107+
108+
if (!strcmp(subcmd, "query"))
109+
return !!do_send_query(token);
110+
111+
if (!strcmp(subcmd, "flush"))
112+
return !!do_send_flush();
113+
114+
die("Unhandled subcommand: '%s'", subcmd);
115+
}
116+
#endif

t/helper/test-tool.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ static struct test_cmd cmds[] = {
3232
{ "dump-untracked-cache", cmd__dump_untracked_cache },
3333
{ "example-decorate", cmd__example_decorate },
3434
{ "fast-rebase", cmd__fast_rebase },
35+
{ "fsmonitor-client", cmd__fsmonitor_client },
3536
{ "genrandom", cmd__genrandom },
3637
{ "genzeros", cmd__genzeros },
3738
{ "getcwd", cmd__getcwd },

t/helper/test-tool.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ int cmd__dump_untracked_cache(int argc, const char **argv);
2323
int cmd__dump_reftable(int argc, const char **argv);
2424
int cmd__example_decorate(int argc, const char **argv);
2525
int cmd__fast_rebase(int argc, const char **argv);
26+
int cmd__fsmonitor_client(int argc, const char **argv);
2627
int cmd__genrandom(int argc, const char **argv);
2728
int cmd__genzeros(int argc, const char **argv);
2829
int cmd__getcwd(int argc, const char **argv);

0 commit comments

Comments
 (0)