Re-sync with internal repository
[hiphop-php.git] / third-party / watchman / src / website / _docs / file-query.md
blobf4c21e2ed0300d4df911a67dfb39624937f7b0c3
1 ---
2 pageid: file-query
3 title: File Queries
4 layout: docs
5 section: Queries
6 permalink: docs/file-query.html
7 redirect_from: docs/file-query/
8 ---
10 Watchman file queries consist of 1 or more *generators* that feed files through
11 the *expression evaluator*.
13 ### Generators
15 Generators are analogous to the list of *paths* that you specify when using the
16 `find(1)` utility, but are implemented in watchman with a bit of a twist
17 because watchman doesn't need to crawl the filesystem in realtime and instead
18 maintains a couple of indexes over the tree.
20 A query may specify any number of generators; each generator will emit its list
21 of files and this may mean that you see the same file output more than once if
22 you specified the use of multiple generators that all produce the same file.
24 Watchman provides 5 generators:
26  * **since**: produces a list of files that were modified since a specific
27    clockspec.
28  * **suffix**: produces a list of files that have a particular suffix.
29  * **glob**: efficiently pattern match a list of files based on their names.
30  * **path**: produces a list of files based on their path and depth.
31  * **all**: produces a list of all known files
34 ### De-duplicating results
36 *Since 4.7.*
38 If your query uses multiple generators, or configures the `path` generator with
39 paths that yield multiple results, the default behavior (for backwards
40 compatibility reasons) is to emit those duplicate results in the query output.
42 You may ask Watchman to de-duplicate results for you by enabling the
43 `dedup_results` boolean in your query:
45 ~~~bash
46 $ watchman -j <<-EOT
47 ["query", "/path/to/root", {
48   "path": ["bar", "bar"],
49   "dedup_results": true
51 EOT
52 ~~~
54 You may test for this feature using an extended version command and requesting
55 the capability name `dedup_results`.
57 ### Since Generator
59 The `since` generator produces a list of files that were modified since a
60 specific [clockspec](../docs/clockspec.html).
62 The following query will consider the set of files changed since the last
63 query using the named cursor `mycursor` and then pass them to the expression
64 evaluator to be filtered to just those that are files:
66 ~~~bash
67 $ watchman -j <<-EOT
68 ["query", "/path/to/root", {
69   "since": "n:mycursor",
70   "expression": ["type", "f"]
72 EOT
73 ~~~
75 If the `since` parameter value is blank, was produced by a different watchman
76 process (in other words, the watchman process was restarted between the time
77 that the value was obtained and the time the query was issued) or is a named
78 cursor that has not yet been used in a query, the `since` generator will
79 consider the state to be a *fresh instance* and its behavior is modified:
81 A *fresh instance* result set will only include files that currently exist
82 and will generate file nodes that are always considered to be `new`.
84 If the query was configured with the `empty_on_fresh_instance` property set to
85 `true` then the result set will be empty and the `is_fresh_instance` property
86 will be set to `true` in the result object.
88 The since generator also knows how to talk to source control;
89 [you can read more about that here](/watchman/docs/scm-query.html).
91 The `since` generator does not consider the targets of symlinks. In particular,
92 the `since` generator may *not* produce a symlink in the following cases:
94 * The symlink's target was a file, and the file is since modified.
95 * The symlink's target was a file, and the file is since deleted or replaced
96   with a different file.
97 * An ancestor of the symlink's target was created or deleted or modified.
98 * The symlink's target was a directory, and a file is since added or removed
99   from that directory.
101 ### Suffix Generator
103 The `suffix` generator produces a list of files that have a particular suffix
104 or set of suffixes.  The value can be either a string or an array of strings.
106 ~~~bash
107 $ watchman -j <<-EOT
108 ["query", "/path/to/root", {
109   "suffix": "js"
114 ~~~bash
115 $ watchman -j <<-EOT
116 ["query", "/path/to/root", {
117   "suffix": ["js", "css"]
122 If the `suffix` generator is given an empty array, it produces no files.
124 The `suffix` generator can produce symlinks.
126 The `suffix` generator does not follow symlinks. For example, a symlink to
127 `/etc` will not cause a `"suffix": "conf"` query to search within `/etc` and
128 produce `/etc/resolv.conf`.
130 ### Glob Generator
132 *Since 4.7.*
134 The `glob` generator produces a list of files by matching against your
135 input list of patterns.  It does this by building a tree from the glob
136 expression(s) and walking both the expression and the in-memory filesystem
137 tree concurrently.
139 This query will yield a list of all of the C source and header files found
140 directly in the `src` dir:
142 ~~~bash
143 $ watchman -j <<-EOT
144 ["query", "/path/to/root", {
145   "glob": ["src/*.c", "src/*.h"],
146   "fields": ["name"]
150 This query will yield a list of all of the C source and header files found
151 in any subdirectories of the root:
153 ~~~bash
154 $ watchman -j <<-EOT
155 ["query", "/path/to/root", {
156   "glob": ["**/*.c", "**/*.h"],
157   "fields": ["name"]
161 Note that it is more efficient to use the `suffix` generator together with a
162 `dirname` expression term for such a broadly scoped query as it results in
163 fewer comparisons.  This example is included as an illustration of recursive
164 globbing.
166 The glob generator implicitly enables `dedup_results` mode.
168 If the `glob` generator is given an empty array, it produces no files.
170 The `glob` generator can produce symlinks.
172 The `glob` generator does not follow symlinks. For example, a symlink to `/etc`
173 will not cause a `"glob": ["**/resolv.conf"]` query to search within `/etc` and
174 produce `/etc/resolv.conf`.
176 ### Path Generator
178 The `path` generator produces a list of files based on their path and depth.
179 Depth controls how far watchman will search down the directory tree for files.
181 The `path` generator expects an array of path specifiers.  Each path specifier
182 can be either a string or an object and each will produce a set of files.
184 If it is a string then it is treated as the value for `path` with `depth` set
185 to infinite.  If an object, the fields `path` (a string) and `depth` (an
186 integer) must be supplied.
188 Paths are relative to the root, so if watchman is watching `/foo/`, path `bar`
189 refers to `/foo/bar`.
191 A `depth` value of `0` means only files and directories which are contained in
192 this path.  A `depth` value of `-1` means no limit on the depth.
194 The following `path` generators are equivalent:
196 ~~~bash
197 $ watchman -j <<-EOT
198 ["query", "/path/to/root", {
199   "path": ["bar"]
204 ~~~bash
205 $ watchman -j <<-EOT
206 ["query", "/path/to/root", {
207   "path": [{"path": "bar", "depth": -1}]
212 If the `path` generator is given an empty array, it produces no files.
214 The `path` generator can produce symlinks.
216 The `path` generator does not follow symlinks.
218 ### All Generator
220 The `all` generator produces a list of all file nodes.  It is the default
221 generator and is used in the case where no other generators were explicitly
222 specified.
224 ~~~bash
225 $ watchman -j <<-EOT
226 ["query", "/path/to/root", {
231 The `all` generator can produce symlinks.
233 The `all` generator does not follow symlinks.
235 ### Expressions
237 A watchman query expression consists of 0 or more expression terms.  If no
238 terms are provided then each file evaluated is considered a match (equivalent
239 to specifying a single `true` expression term).
241 Otherwise, the expression is evaluated against the file and produces a boolean
242 result.  If that result is true then the file is considered a match and is
243 added to the output set.
245 An expression term is canonically represented as a JSON array whose zeroth
246 element is a string containing the term name.
248 ~~~json
249 ["termname", arg1, arg2]
252 If the term accepts no arguments you may use a short form that consists of just
253 the term name expressed as a string:
255 ~~~json
256 "true"
259 Expressions that match against file names may match against either the
260 *basename* or the *wholename* of the file.  The basename is the name of the
261 file within its containing directory.  The wholename is the name of the file
262 relative to the watched root.
264 You can find a list of all possible expression terms in the sidebar on the left
265 of this page.
267 ### Relative roots
269 *Since 3.3.*
271 Watchman supports optionally evaluating queries with respect to a path within a
272 watched root. This is used with the `relative_root` parameter:
274 ~~~json
275 ["query", "/path/to/watched/root", {
276   "relative_root": "project1",
280 Setting a relative root results in the following modifications to queries:
282 * The `path` generator is evaluated with respect to the relative root. In the
283   above example, `"path": ["dir"]` will return all files inside
284   `/path/to/watched/root/project1/dir`.
285 * The input expression is evaluated with respect to the relative root. In the
286   above example, `"expression": ["match", "dir/*.txt", "wholename"]` will return
287   all files inside `/path/to/watched/root/project1/dir/` that match the glob
288   `*.txt`.
289 * Paths inside the relative root are returned with the relative root stripped
290   off. For example, a path `project1/dir/file.txt` would be returned as
291   `dir/file.txt`.
292 * Paths outside the relative root are not returned.
294 Relative roots behave similarly to a separate Watchman watch on the
295 subdirectory, without any of the system overhead that that imposes. This is
296 useful for large repositories, where your script or tool is only interested in a
297 particular directory inside the repository.