Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc changes #23

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,19 @@ Watchman provides 4 generators:

* **since**: generates a list of files that were modified since a specific
clockspec. If this is not specified, this will be treated the same as if a
clockspec from a different instance of watchman was passed in.
* **suffix**: generates a list of files that have a particular suffix
* **path**: generates a list of files based on their path and depth
clockspec from a different instance of watchman was passed in. You can
use either a string oclock value, or a integer number of epoch seconds.
* **suffix**: generates a list of files that have a particular suffix or set
of suffixes. The value can be either a string or an array of strings.
* **path**: generates a list of files based on their path and depth. Depth
controls how far watchman will search down the directory tree for files.
Depth = 0 means only files and directories which are contained in this
path. The value of path can be either an array, a string, or an object.
If it is a string, it is treated as a path, and depth is infinite. If
an object, the fields path (a string) and depth (an integer) must be
supplied. An array can contain either strings or objects, each with the
same meaning as single strings or objects. Paths are relative to
the root, so if watchman is watching /foo/, path "bar" refers to /foo/bar.
* **all**: generates a list of all known files

Generators are analogous to the list of *paths* that you specify when using the
Expand Down Expand Up @@ -641,12 +651,13 @@ fields will return a response something like this:
}
```

The `is_fresh_instance` member is true if the particular clock value indicates
that it was returned by a different instance of watchman, or a named cursor
hasn't been seen before. In that case, only files that currently exist will be
returned, and all files will have `new` set to `true`. Advanced users may set
the input parameter `empty_on_fresh_instance` to true, in which case no files
will be returned for fresh instances.
For queries using the `since` generator, the `is_fresh_instance` member is true
if the particular clock value indicates that it was returned by a different
instance of watchman, or a named cursor hasn't been seen before. In that case,
only files that currently exist will be returned, and all files will have `new`
set to `true`. For all other queries, is_fresh_instance will always be true.
Advanced users may set the input parameter `empty_on_fresh_instance` to true,
in which case no files will be returned for fresh instances.

If the `fields` member consists of a single entry, the files result will be a
simple array of values; ```"fields": ["name"]``` produces:
Expand Down
2 changes: 1 addition & 1 deletion cmds/watch.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void cmd_watch_delete(struct watchman_client *client, json_t *args)

/* resolve the root */
if (json_array_size(args) != 2) {
send_error_response(client, "wrong number of arguments to 'watch'");
send_error_response(client, "wrong number of arguments to 'watch-del'");
return;
}

Expand Down
37 changes: 22 additions & 15 deletions query/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ static bool parse_since(w_query *res, json_t *query)
return false;
}

static bool set_suffix(w_query *res, json_t *ele, w_string_t **suffix)
{
if (!json_is_string(ele)) {
res->errmsg = strdup("'suffix' must be a string or an array of strings");
return false;
}

*suffix = w_string_new_lower(json_string_value(ele));

return true;
}

static bool parse_suffixes(w_query *res, json_t *query)
{
json_t *suffixes;
Expand All @@ -92,8 +104,15 @@ static bool parse_suffixes(w_query *res, json_t *query)
return true;
}

if (json_is_string(suffixes)) {
json_t *ele = suffixes;
res->nsuffixes = 1;
res->suffixes = calloc(res->nsuffixes, sizeof(w_string_t*));
return set_suffix(res, ele, res->suffixes);
}

if (!json_is_array(suffixes)) {
res->errmsg = strdup("'suffix' must be an array of strings");
res->errmsg = strdup("'suffix' must be a string or an array of strings");
return false;
}

Expand All @@ -106,27 +125,15 @@ static bool parse_suffixes(w_query *res, json_t *query)

for (i = 0; i < json_array_size(suffixes); i++) {
json_t *ele = json_array_get(suffixes, i);
char *low;
int j;

if (!json_is_string(ele)) {
res->errmsg = strdup("'suffix' must be an array of strings");
res->errmsg = strdup("'suffix' must be a string or an array of strings");
return false;
}

low = strdup(json_string_value(ele));
if (!low) {
res->errmsg = strdup("out of memory");
if (!set_suffix(res, ele, res->suffixes + i)) {
return false;
}

for (j = 0; low[j]; j++) {
low[j] = tolower(low[j]);
}

res->suffixes[i] = w_string_new(low);

free(low);
}

return true;
Expand Down
46 changes: 46 additions & 0 deletions tests/integration/suffixgenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/* Copyright 2014-present Facebook, Inc.
* Licensed under the Apache License, Version 2.0 */

class SuffixGeneratorTestCase extends WatchmanTestCase {
function testGeneratorExpr() {
$dir = PhutilDirectoryFixture::newEmptyFixture();
$root = realpath($dir->getPath());

touch("$root/foo.c");
mkdir("$root/subdir");
touch("$root/subdir/bar.txt");

$this->watch($root);

$res = $this->watchmanCommand('query', $root, array(
'expression' => array('true'),
'fields' => array('name'),
'suffix' => 'c'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a variation that exercises the array form and a non-string suffix

));
$this->assertEqual(array('foo.c'), $res['files']);

$res = $this->watchmanCommand('query', $root, array(
'expression' => array('true'),
'fields' => array('name'),
'suffix' => array('c','txt')
));
sort($res['files']);
$this->assertEqual(array('foo.c', 'subdir/bar.txt'), $res['files']);


$res = $this->watchmanCommand('query', $root, array(
'expression' => array('true'),
'fields' => array('name'),
'suffix' => array('a' => 'b')
));
$this->assertEqual(
'failed to parse query: \'suffix\' must be a string or an array of strings',
$res['error']
);

}
}

// vim:ts=2:sw=2:et: