fsmonitor-watchman.sample 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use IPC::Open2;
  5. # An example hook script to integrate Watchman
  6. # (https://facebook.github.io/watchman/) with git to speed up detecting
  7. # new and modified files.
  8. #
  9. # The hook is passed a version (currently 2) and last update token
  10. # formatted as a string and outputs to stdout a new update token and
  11. # all files that have been modified since the update token. Paths must
  12. # be relative to the root of the working tree and separated by a single NUL.
  13. #
  14. # To enable this hook, rename this file to "query-watchman" and set
  15. # 'git config core.fsmonitor .git/hooks/query-watchman'
  16. #
  17. my ($version, $last_update_token) = @ARGV;
  18. # Uncomment for debugging
  19. # print STDERR "$0 $version $last_update_token\n";
  20. # Check the hook interface version
  21. if ($version ne 2) {
  22. die "Unsupported query-fsmonitor hook version '$version'.\n" .
  23. "Falling back to scanning...\n";
  24. }
  25. my $git_work_tree = get_working_dir();
  26. my $retry = 1;
  27. my $json_pkg;
  28. eval {
  29. require JSON::XS;
  30. $json_pkg = "JSON::XS";
  31. 1;
  32. } or do {
  33. require JSON::PP;
  34. $json_pkg = "JSON::PP";
  35. };
  36. launch_watchman();
  37. sub launch_watchman {
  38. my $o = watchman_query();
  39. if (is_work_tree_watched($o)) {
  40. output_result($o->{clock}, @{$o->{files}});
  41. }
  42. }
  43. sub output_result {
  44. my ($clockid, @files) = @_;
  45. # Uncomment for debugging watchman output
  46. # open (my $fh, ">", ".git/watchman-output.out");
  47. # binmode $fh, ":utf8";
  48. # print $fh "$clockid\n@files\n";
  49. # close $fh;
  50. binmode STDOUT, ":utf8";
  51. print $clockid;
  52. print "\0";
  53. local $, = "\0";
  54. print @files;
  55. }
  56. sub watchman_clock {
  57. my $response = qx/watchman clock "$git_work_tree"/;
  58. die "Failed to get clock id on '$git_work_tree'.\n" .
  59. "Falling back to scanning...\n" if $? != 0;
  60. return $json_pkg->new->utf8->decode($response);
  61. }
  62. sub watchman_query {
  63. my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
  64. or die "open2() failed: $!\n" .
  65. "Falling back to scanning...\n";
  66. # In the query expression below we're asking for names of files that
  67. # changed since $last_update_token but not from the .git folder.
  68. #
  69. # To accomplish this, we're using the "since" generator to use the
  70. # recency index to select candidate nodes and "fields" to limit the
  71. # output to file names only. Then we're using the "expression" term to
  72. # further constrain the results.
  73. my $last_update_line = "";
  74. if (substr($last_update_token, 0, 1) eq "c") {
  75. $last_update_token = "\"$last_update_token\"";
  76. $last_update_line = qq[\n"since": $last_update_token,];
  77. }
  78. my $query = <<" END";
  79. ["query", "$git_work_tree", {$last_update_line
  80. "fields": ["name"],
  81. "expression": ["not", ["dirname", ".git"]]
  82. }]
  83. END
  84. # Uncomment for debugging the watchman query
  85. # open (my $fh, ">", ".git/watchman-query.json");
  86. # print $fh $query;
  87. # close $fh;
  88. print CHLD_IN $query;
  89. close CHLD_IN;
  90. my $response = do {local $/; <CHLD_OUT>};
  91. # Uncomment for debugging the watch response
  92. # open ($fh, ">", ".git/watchman-response.json");
  93. # print $fh $response;
  94. # close $fh;
  95. die "Watchman: command returned no output.\n" .
  96. "Falling back to scanning...\n" if $response eq "";
  97. die "Watchman: command returned invalid output: $response\n" .
  98. "Falling back to scanning...\n" unless $response =~ /^\{/;
  99. return $json_pkg->new->utf8->decode($response);
  100. }
  101. sub is_work_tree_watched {
  102. my ($output) = @_;
  103. my $error = $output->{error};
  104. if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) {
  105. $retry--;
  106. my $response = qx/watchman watch "$git_work_tree"/;
  107. die "Failed to make watchman watch '$git_work_tree'.\n" .
  108. "Falling back to scanning...\n" if $? != 0;
  109. $output = $json_pkg->new->utf8->decode($response);
  110. $error = $output->{error};
  111. die "Watchman: $error.\n" .
  112. "Falling back to scanning...\n" if $error;
  113. # Uncomment for debugging watchman output
  114. # open (my $fh, ">", ".git/watchman-output.out");
  115. # close $fh;
  116. # Watchman will always return all files on the first query so
  117. # return the fast "everything is dirty" flag to git and do the
  118. # Watchman query just to get it over with now so we won't pay
  119. # the cost in git to look up each individual file.
  120. my $o = watchman_clock();
  121. $error = $output->{error};
  122. die "Watchman: $error.\n" .
  123. "Falling back to scanning...\n" if $error;
  124. output_result($o->{clock}, ("/"));
  125. $last_update_token = $o->{clock};
  126. eval { launch_watchman() };
  127. return 0;
  128. }
  129. die "Watchman: $error.\n" .
  130. "Falling back to scanning...\n" if $error;
  131. return 1;
  132. }
  133. sub get_working_dir {
  134. my $working_dir;
  135. if ($^O =~ 'msys' || $^O =~ 'cygwin') {
  136. $working_dir = Win32::GetCwd();
  137. $working_dir =~ tr/\\/\//;
  138. } else {
  139. require Cwd;
  140. $working_dir = Cwd::cwd();
  141. }
  142. return $working_dir;
  143. }