fsmonitor-watchman.sample 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 1) and a time in nanoseconds
  10. # formatted as a string and outputs to stdout all files that have been
  11. # modified since the given time. Paths must be relative to the root of
  12. # 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, $time) = @ARGV;
  18. # Check the hook interface version
  19. if ($version == 1) {
  20. # convert nanoseconds to seconds
  21. $time = int $time / 1000000000;
  22. } else {
  23. die "Unsupported query-fsmonitor hook version '$version'.\n" .
  24. "Falling back to scanning...\n";
  25. }
  26. my $git_work_tree;
  27. if ($^O =~ 'msys' || $^O =~ 'cygwin') {
  28. $git_work_tree = Win32::GetCwd();
  29. $git_work_tree =~ tr/\\/\//;
  30. } else {
  31. require Cwd;
  32. $git_work_tree = Cwd::cwd();
  33. }
  34. my $retry = 1;
  35. launch_watchman();
  36. sub launch_watchman {
  37. my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
  38. or die "open2() failed: $!\n" .
  39. "Falling back to scanning...\n";
  40. # In the query expression below we're asking for names of files that
  41. # changed since $time but were not transient (ie created after
  42. # $time but no longer exist).
  43. #
  44. # To accomplish this, we're using the "since" generator to use the
  45. # recency index to select candidate nodes and "fields" to limit the
  46. # output to file names only. Then we're using the "expression" term to
  47. # further constrain the results.
  48. #
  49. # The category of transient files that we want to ignore will have a
  50. # creation clock (cclock) newer than $time_t value and will also not
  51. # currently exist.
  52. my $query = <<" END";
  53. ["query", "$git_work_tree", {
  54. "since": $time,
  55. "fields": ["name"],
  56. "expression": ["not", ["allof", ["since", $time, "cclock"], ["not", "exists"]]]
  57. }]
  58. END
  59. print CHLD_IN $query;
  60. close CHLD_IN;
  61. my $response = do {local $/; <CHLD_OUT>};
  62. die "Watchman: command returned no output.\n" .
  63. "Falling back to scanning...\n" if $response eq "";
  64. die "Watchman: command returned invalid output: $response\n" .
  65. "Falling back to scanning...\n" unless $response =~ /^\{/;
  66. my $json_pkg;
  67. eval {
  68. require JSON::XS;
  69. $json_pkg = "JSON::XS";
  70. 1;
  71. } or do {
  72. require JSON::PP;
  73. $json_pkg = "JSON::PP";
  74. };
  75. my $o = $json_pkg->new->utf8->decode($response);
  76. if ($retry > 0 and $o->{error} and $o->{error} =~ m/unable to resolve root .* directory (.*) is not watched/) {
  77. print STDERR "Adding '$git_work_tree' to watchman's watch list.\n";
  78. $retry--;
  79. qx/watchman watch "$git_work_tree"/;
  80. die "Failed to make watchman watch '$git_work_tree'.\n" .
  81. "Falling back to scanning...\n" if $? != 0;
  82. # Watchman will always return all files on the first query so
  83. # return the fast "everything is dirty" flag to git and do the
  84. # Watchman query just to get it over with now so we won't pay
  85. # the cost in git to look up each individual file.
  86. print "/\0";
  87. eval { launch_watchman() };
  88. exit 0;
  89. }
  90. die "Watchman: $o->{error}.\n" .
  91. "Falling back to scanning...\n" if $o->{error};
  92. binmode STDOUT, ":utf8";
  93. local $, = "\0";
  94. print @{$o->{files}};
  95. }