Re: [PATCH v2] fsmonitor: query watchman with right valid json
- From
Ævar Arnfjörð Bjarmason <avarab@gmail.com>
- Date
- Jun 7, 2022, 14:39 UTC
- Message-ID
- <220607.867d5sv8bq.gmgdl@evledraar.gmail.com>
- In-Reply-To
- <20220607111419.15753-1-sluongng@gmail.com>
On Tue, Jun 07 2022, Son Luong Ngoc wrote:
Show 21 quoted lines
> In rare circumstances where the current git index does not carry the
> last_update_token, the fsmonitor v2 hook will be invoked with an
> empty string which would caused the final rendered json to be invalid.
>
> ["query", "/path/to/my/git/repository/", {
> "since": ,
> "fields": ["name"],
> "expression": ["not", ["dirname", ".git"]]
> }]
>
> Which will left user with the following error message
>
> > git status
> failed to parse command from stdin: line 2, column 13, position 67: unexpected token near ','
> Watchman: command returned no output.
> Falling back to scanning...
>
> Hide the "since" field in json query when "last_update_token" is empty.
>
> Co-authored-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
> Signed-off-by: Son Luong Ngoc <sluongng@gmail.com>Thanks for the quick turnaround.
Show 16 quoted lines
> templates/hooks--fsmonitor-watchman.sample | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/templates/hooks--fsmonitor-watchman.sample b/templates/hooks--fsmonitor-watchman.sample
> index 14ed0aa42d..23e856f5de 100755
> --- a/templates/hooks--fsmonitor-watchman.sample
> +++ b/templates/hooks--fsmonitor-watchman.sample
> @@ -86,12 +86,13 @@ sub watchman_query {
> # recency index to select candidate nodes and "fields" to limit the
> # output to file names only. Then we're using the "expression" term to
> # further constrain the results.
> + my $last_update_line = "";
> if (substr($last_update_token, 0, 1) eq "c") {
> $last_update_token = "\"$last_update_token\"";
> + $last_update_line = qq[\n"since": $last_update_token,];
> }This LGTM, just a note...
> my $query = <<" END";
> - ["query", "$git_work_tree", {
> - "since": $last_update_token,
> + ["query", "$git_work_tree", {$last_update_line...doesn't really need a re-roll, but doesn't this trade "we don't have too many \n" for not indenting the query properly anymore?
I think skipping both is fine, but between the two I think having indenting is better than having a redundant \n some of the time.
FWIW you could just add the variable on its own line, and then do this instead:
(my $query = <<" END") =~ s/(?<=\n)\t*\n//s;
To post-hoc fix the extra \n in this case :)
But I think this is also fine as-is, thanks!