123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- use strict;
- use warnings;
- use IPC::Open2;
- my ($version, $last_update_token) = @ARGV;
- if ($version ne 2) {
- die "Unsupported query-fsmonitor hook version '$version'.\n" .
- "Falling back to scanning...\n";
- }
- my $git_work_tree = get_working_dir();
- my $retry = 1;
- my $json_pkg;
- eval {
- require JSON::XS;
- $json_pkg = "JSON::XS";
- 1;
- } or do {
- require JSON::PP;
- $json_pkg = "JSON::PP";
- };
- launch_watchman();
- sub launch_watchman {
- my $o = watchman_query();
- if (is_work_tree_watched($o)) {
- output_result($o->{clock}, @{$o->{files}});
- }
- }
- sub output_result {
- my ($clockid, @files) = @_;
-
-
-
-
-
- binmode STDOUT, ":utf8";
- print $clockid;
- print "\0";
- local $, = "\0";
- print @files;
- }
- sub watchman_clock {
- my $response = qx/watchman clock "$git_work_tree"/;
- die "Failed to get clock id on '$git_work_tree'.\n" .
- "Falling back to scanning...\n" if $? != 0;
- return $json_pkg->new->utf8->decode($response);
- }
- sub watchman_query {
- my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
- or die "open2() failed: $!\n" .
- "Falling back to scanning...\n";
-
-
-
-
-
-
-
- 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,];
- }
- my $query = <<" END";
- ["query", "$git_work_tree", {$last_update_line
- "fields": ["name"],
- "expression": ["not", ["dirname", ".git"]]
- }]
- END
-
-
-
-
- print CHLD_IN $query;
- close CHLD_IN;
- my $response = do {local $/; <CHLD_OUT>};
-
-
-
-
- die "Watchman: command returned no output.\n" .
- "Falling back to scanning...\n" if $response eq "";
- die "Watchman: command returned invalid output: $response\n" .
- "Falling back to scanning...\n" unless $response =~ /^\{/;
- return $json_pkg->new->utf8->decode($response);
- }
- sub is_work_tree_watched {
- my ($output) = @_;
- my $error = $output->{error};
- if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) {
- $retry--;
- my $response = qx/watchman watch "$git_work_tree"/;
- die "Failed to make watchman watch '$git_work_tree'.\n" .
- "Falling back to scanning...\n" if $? != 0;
- $output = $json_pkg->new->utf8->decode($response);
- $error = $output->{error};
- die "Watchman: $error.\n" .
- "Falling back to scanning...\n" if $error;
-
-
-
-
-
-
-
- my $o = watchman_clock();
- $error = $output->{error};
- die "Watchman: $error.\n" .
- "Falling back to scanning...\n" if $error;
- output_result($o->{clock}, ("/"));
- $last_update_token = $o->{clock};
- eval { launch_watchman() };
- return 0;
- }
- die "Watchman: $error.\n" .
- "Falling back to scanning...\n" if $error;
- return 1;
- }
- sub get_working_dir {
- my $working_dir;
- if ($^O =~ 'msys' || $^O =~ 'cygwin') {
- $working_dir = Win32::GetCwd();
- $working_dir =~ tr/\\/\//;
- } else {
- require Cwd;
- $working_dir = Cwd::cwd();
- }
- return $working_dir;
- }
|