Skip to content

Commit

Permalink
Handle single suffixes as well as arrays
Browse files Browse the repository at this point in the history
Refs #23
  • Loading branch information
dturner-tw authored and wez committed Feb 6, 2014
1 parent dce442d commit 552cc42
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ 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
* **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
* **all**: generates a list of all known files

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
47 changes: 47 additions & 0 deletions tests/integration/suffixgenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?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'
));
$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:

0 comments on commit 552cc42

Please sign in to comment.