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.

109 lines
3.0 KiB

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