You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

fsmonitor-watchman.sample 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. if (substr($last_update_token, 0, 1) eq "c") {
  74. $last_update_token = "\"$last_update_token\"";
  75. }
  76. my $query = <<" END";
  77. ["query", "$git_work_tree", {
  78. "since": $last_update_token,
  79. "fields": ["name"],
  80. "expression": ["not", ["dirname", ".git"]]
  81. }]
  82. END
  83. # Uncomment for debugging the watchman query
  84. # open (my $fh, ">", ".git/watchman-query.json");
  85. # print $fh $query;
  86. # close $fh;
  87. print CHLD_IN $query;
  88. close CHLD_IN;
  89. my $response = do {local $/; <CHLD_OUT>};
  90. # Uncomment for debugging the watch response
  91. # open ($fh, ">", ".git/watchman-response.json");
  92. # print $fh $response;
  93. # close $fh;
  94. die "Watchman: command returned no output.\n" .
  95. "Falling back to scanning...\n" if $response eq "";
  96. die "Watchman: command returned invalid output: $response\n" .
  97. "Falling back to scanning...\n" unless $response =~ /^\{/;
  98. return $json_pkg->new->utf8->decode($response);
  99. }
  100. sub is_work_tree_watched {
  101. my ($output) = @_;
  102. my $error = $output->{error};
  103. if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) {
  104. $retry--;
  105. my $response = qx/watchman watch "$git_work_tree"/;
  106. die "Failed to make watchman watch '$git_work_tree'.\n" .
  107. "Falling back to scanning...\n" if $? != 0;
  108. $output = $json_pkg->new->utf8->decode($response);
  109. $error = $output->{error};
  110. die "Watchman: $error.\n" .
  111. "Falling back to scanning...\n" if $error;
  112. # Uncomment for debugging watchman output
  113. # open (my $fh, ">", ".git/watchman-output.out");
  114. # close $fh;
  115. # Watchman will always return all files on the first query so
  116. # return the fast "everything is dirty" flag to git and do the
  117. # Watchman query just to get it over with now so we won't pay
  118. # the cost in git to look up each individual file.
  119. my $o = watchman_clock();
  120. $error = $output->{error};
  121. die "Watchman: $error.\n" .
  122. "Falling back to scanning...\n" if $error;
  123. output_result($o->{clock}, ("/"));
  124. $last_update_token = $o->{clock};
  125. eval { launch_watchman() };
  126. return 0;
  127. }
  128. die "Watchman: $error.\n" .
  129. "Falling back to scanning...\n" if $error;
  130. return 1;
  131. }
  132. sub get_working_dir {
  133. my $working_dir;
  134. if ($^O =~ 'msys' || $^O =~ 'cygwin') {
  135. $working_dir = Win32::GetCwd();
  136. $working_dir =~ tr/\\/\//;
  137. } else {
  138. require Cwd;
  139. $working_dir = Cwd::cwd();
  140. }
  141. return $working_dir;
  142. }