7 # An example hook script to integrate Watchman
8 # (https://facebook.github.io/watchman/) with git to speed up detecting
9 # new and modified files.
11 # The hook is passed a version (currently 1) and a time in nanoseconds
12 # formatted as a string and outputs to stdout all files that have been
13 # modified since the given time. Paths must be relative to the root of
14 # the working tree and separated by a single NUL.
16 # To enable this hook, rename this file to "query-watchman" and set
17 # 'git config core.fsmonitor .git/hooks/query-watchman'
19 my ($version, $time) = @ARGV;
21 # Check the hook interface version
24 # convert nanoseconds to seconds
25 $time = int $time / 1000000000;
27 die "Unsupported query-fsmonitor hook version '$version'.\n" .
28 "Falling back to scanning...\n";
31 # Convert unix style paths to escaped Windows style paths when running
32 # in Windows command prompt
34 my $system = `uname -s`;
35 $system =~ s/[\r\n]+//g;
38 if ($system =~ m/^MSYS_NT/) {
39 $git_work_tree = `cygpath -aw "\$PWD"`;
40 $git_work_tree =~ s/[\r\n]+//g;
41 $git_work_tree =~ s
,\\,/,g
;
43 $git_work_tree = $ENV{'PWD'};
52 # Set input record separator
55 my $pid = open2
(\
*CHLD_OUT
, \
*CHLD_IN
, 'watchman -j')
56 or die "open2() failed: $!\n" .
57 "Falling back to scanning...\n";
59 # In the query expression below we're asking for names of files that
60 # changed since $time but were not transient (ie created after
61 # $time but no longer exist).
63 # To accomplish this, we're using the "since" generator to use the
64 # recency index to select candidate nodes and "fields" to limit the
65 # output to file names only. Then we're using the "expression" term to
66 # further constrain the results.
68 # The category of transient files that we want to ignore will have a
69 # creation clock (cclock) newer than $time_t value and will also not
73 ["query", "$git_work_tree", {
76 "expression": ["not", ["allof", ["since", $time, "cclock"], ["not", "exists"]]]
81 my $response = <CHLD_OUT
>;
83 die "Watchman: command returned no output.\n" .
84 "Falling back to scanning...\n" if $response eq "";
85 die "Watchman: command returned invalid output: $response\n" .
86 "Falling back to scanning...\n" unless $response =~ /^\{/;
91 $json_pkg = "JSON::XS";
95 $json_pkg = "JSON::PP";
98 my $o = $json_pkg->new->utf8->decode($response);
100 if ($retry > 0 and $o->{error
} and $o->{error
} =~ m/unable to resolve root .* directory (.*) is not watched/) {
101 print STDERR
"Adding '$git_work_tree' to watchman's watch list.\n";
103 qx/watchman watch "$git_work_tree"/;
104 die "Failed to make watchman watch '$git_work_tree'.\n" .
105 "Falling back to scanning...\n" if $?
!= 0;
107 # Watchman will always return all files on the first query so
108 # return the fast "everything is dirty" flag to git and do the
109 # Watchman query just to get it over with now so we won't pay
110 # the cost in git to look up each individual file.
112 eval { launch_watchman
() };
116 die "Watchman: $o->{error}.\n" .
117 "Falling back to scanning...\n" if $o->{error
};
119 binmode STDOUT
, ":utf8";
121 print @
{$o->{files
}};