Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t7519/fsmonitor-watchman-v2 # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t7519 / fsmonitor-watchman-v2` 167 lines ยท 4540 bytes Raw Blame History 1 #!/usr/bin/perl 2 3 use strict; 4 use warnings; 5 use IPC::Open2; 6 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. 10 # 11 # The hook is passed a version (currently 2) and last update token 12 # formatted as a string and outputs to stdout a new update token and 13 # all files that have been modified since the update token. Paths must 14 # be relative to the root of the working tree and separated by a single NUL. 15 # 16 # To enable this hook, rename this file to "query-watchman" and set 17 # 'git config core.fsmonitor .git/hooks/query-watchman' 18 # 19 my ($version, $last_update_token) = @ARGV; 20 21 # Uncomment for debugging 22 # print STDERR "$0 $version $last_update_token\n"; 23 24 # Check the hook interface version 25 if ($version ne 2) { 26 die "Unsupported query-fsmonitor hook version '$version'.\n" . 27 "Falling back to scanning...\n"; 28 } 29 30 my $git_work_tree = get_working_dir(); 31 32 my $json_pkg; 33 eval { 34 require JSON::XS; 35 $json_pkg = "JSON::XS"; 36 1; 37 } or do { 38 require JSON::PP; 39 $json_pkg = "JSON::PP"; 40 }; 41 42 launch_watchman(); 43 44 sub launch_watchman { 45 my $o = watchman_query(); 46 if (is_work_tree_watched($o)) { 47 output_result($o->{clock}, @{$o->{files}}); 48 } 49 } 50 51 sub output_result { 52 my ($clockid, @files) = @_; 53 54 # Uncomment for debugging watchman output 55 # open (my $fh, ">", ".git/watchman-output.out"); 56 # binmode $fh, ":utf8"; 57 # print $fh "$clockid\n@files\n"; 58 # close $fh; 59 60 binmode STDOUT, ":utf8"; 61 print $clockid; 62 print "\0"; 63 local $, = "\0"; 64 print @files; 65 } 66 67 sub watchman_clock { 68 my $response = qx/watchman clock "$git_work_tree"/; 69 die "Failed to get clock id on '$git_work_tree'.\n" . 70 "Falling back to scanning...\n" if $? != 0; 71 72 return $json_pkg->new->utf8->decode($response); 73 } 74 75 sub watchman_query { 76 my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty') 77 or die "open2() failed: $!\n" . 78 "Falling back to scanning...\n"; 79 80 # In the query expression below we're asking for names of files that 81 # changed since $last_update_token but not from the .git folder. 82 # 83 # To accomplish this, we're using the "since" generator to use the 84 # recency index to select candidate nodes and "fields" to limit the 85 # output to file names only. Then we're using the "expression" term to 86 # further constrain the results. 87 if (substr($last_update_token, 0, 1) eq "c") { 88 $last_update_token = "\"$last_update_token\""; 89 } 90 my $query = <<" END"; 91 ["query", "$git_work_tree", { 92 "since": $last_update_token, 93 "fields": ["name"], 94 "expression": ["not", ["dirname", ".git"]] 95 }] 96 END 97 98 # Uncomment for debugging the watchman query 99 # open (my $fh, ">", ".git/watchman-query.json"); 100 # print $fh $query; 101 # close $fh; 102 103 print CHLD_IN $query; 104 close CHLD_IN; 105 my $response = do {local $/; }; 106 107 # Uncomment for debugging the watch response 108 # open ($fh, ">", ".git/watchman-response.json"); 109 # print $fh $response; 110 # close $fh; 111 112 die "Watchman: command returned no output.\n" . 113 "Falling back to scanning...\n" if $response eq ""; 114 die "Watchman: command returned invalid output: $response\n" . 115 "Falling back to scanning...\n" unless $response =~ /^\{/; 116 117 return $json_pkg->new->utf8->decode($response); 118 } 119 120 sub is_work_tree_watched { 121 my ($output) = @_; 122 my $error = $output->{error}; 123 if ($error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) { 124 my $response = qx/watchman watch "$git_work_tree"/; 125 die "Failed to make watchman watch '$git_work_tree'.\n" . 126 "Falling back to scanning...\n" if $? != 0; 127 $output = $json_pkg->new->utf8->decode($response); 128 $error = $output->{error}; 129 die "Watchman: $error.\n" . 130 "Falling back to scanning...\n" if $error; 131 132 # Uncomment for debugging watchman output 133 # open (my $fh, ">", ".git/watchman-output.out"); 134 # close $fh; 135 136 # Watchman will always return all files on the first query so 137 # return the fast "everything is dirty" flag to git and do the 138 # Watchman query just to get it over with now so we won't pay 139 # the cost in git to look up each individual file. 140 my $o = watchman_clock(); 141 $error = $o->{error}; 142 143 die "Watchman: $error.\n" . 144 "Falling back to scanning...\n" if $error; 145 146 output_result($o->{clock}, ("/")); 147 return 0; 148 } 149 150 die "Watchman: $error.\n" . 151 "Falling back to scanning...\n" if $error; 152 153 return 1; 154 } 155 156 sub get_working_dir { 157 my $working_dir; 158 if ($^O =~ 'msys' || $^O =~ 'cygwin') { 159 $working_dir = Win32::GetCwd(); 160 $working_dir =~ tr/\\/\//; 161 } else { 162 require Cwd; 163 $working_dir = Cwd::cwd(); 164 } 165 166 return $working_dir; 167 }