123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- use strict;
- use warnings;
- use IPC::Open2;
- my ($version, $time) = @ARGV;
- if ($version == 1) {
-
- $time = int $time / 1000000000;
- } else {
- die "Unsupported query-fsmonitor hook version '$version'.\n" .
- "Falling back to scanning...\n";
- }
- my $git_work_tree;
- if ($^O =~ 'msys' || $^O =~ 'cygwin') {
- $git_work_tree = Win32::GetCwd();
- $git_work_tree =~ tr/\\/\//;
- } else {
- require Cwd;
- $git_work_tree = Cwd::cwd();
- }
- my $retry = 1;
- launch_watchman();
- sub launch_watchman {
- my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
- or die "open2() failed: $!\n" .
- "Falling back to scanning...\n";
-
-
-
-
-
-
-
-
-
-
-
-
- my $query = <<" END";
- ["query", "$git_work_tree", {
- "since": $time,
- "fields": ["name"],
- "expression": ["not", ["allof", ["since", $time, "cclock"], ["not", "exists"]]]
- }]
- 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 =~ /^\{/;
- my $json_pkg;
- eval {
- require JSON::XS;
- $json_pkg = "JSON::XS";
- 1;
- } or do {
- require JSON::PP;
- $json_pkg = "JSON::PP";
- };
- my $o = $json_pkg->new->utf8->decode($response);
- if ($retry > 0 and $o->{error} and $o->{error} =~ m/unable to resolve root .* directory (.*) is not watched/) {
- print STDERR "Adding '$git_work_tree' to watchman's watch list.\n";
- $retry--;
- qx/watchman watch "$git_work_tree"/;
- die "Failed to make watchman watch '$git_work_tree'.\n" .
- "Falling back to scanning...\n" if $? != 0;
-
-
-
-
- print "/\0";
- eval { launch_watchman() };
- exit 0;
- }
- die "Watchman: $o->{error}.\n" .
- "Falling back to scanning...\n" if $o->{error};
- binmode STDOUT, ":utf8";
- local $, = "\0";
- print @{$o->{files}};
- }
|