Skip to content

Commit 3c12b6d

Browse files
benpeartgitster
authored andcommitted
fsmonitor: add a sample query-fsmonitor hook script for Watchman
This hook script integrates the new fsmonitor capabilities of git with the cross platform Watchman file watching service. To use the script: Download and install Watchman from https://facebook.github.io/watchman/ and instruct Watchman to watch your working directory for changes ('watchman watch-project /usr/src/git'). Rename the sample integration hook from query-fsmonitor.sample to query-fsmonitor. Configure git to use the extension ('git config core.fsmonitor true') and optionally turn on the untracked cache for optimal performance ('git config core.untrackedcache true'). Signed-off-by: Ben Peart <[email protected]> Signed-off-by: Johannes Schindelin <[email protected]> Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 0170f8b commit 3c12b6d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to integrate Watchman
4+
# (https://facebook.github.io/watchman/) with git to provide fast
5+
# git status.
6+
#
7+
# The hook is passed a version (currently 1) and a time in nanoseconds
8+
# formatted as a string and outputs to stdout all files that have been
9+
# modified since the given time. Paths must be relative to the root of
10+
# the working tree and separated by a single NUL.
11+
#
12+
# To enable this hook, rename this file to "query-fsmonitor"
13+
14+
# check the hook interface version
15+
if [ $1 -eq 1 ]
16+
then
17+
# convert nanoseconds to seconds
18+
time_t=$(($2/1000000000))
19+
else
20+
echo -e "Unsupported query-fsmonitor hook version.\nFalling back to scanning...\n" >&2
21+
exit 1;
22+
fi
23+
24+
# Convert unix style paths to escaped Windows style paths
25+
case "$(uname -s)" in
26+
MINGW*|MSYS_NT*)
27+
GIT_WORK_TREE="$(cygpath -aw "$PWD" | sed 's,\\,\\\\,g')"
28+
;;
29+
*)
30+
GIT_WORK_TREE="$PWD"
31+
;;
32+
esac
33+
34+
# Query Watchman for all the changes since the requested time
35+
echo "[\"query\", \"$GIT_WORK_TREE\", {\"since\": $time_t, \"fields\":[\"name\"]}]" | \
36+
watchman -j |
37+
perl -0666 -e '
38+
use strict;
39+
use warnings;
40+
41+
my $stdin = <>;
42+
die "Watchman: command returned no output.\nFalling back to scanning...\n" if $stdin eq "";
43+
die "Watchman: command returned invalid output: $stdin\nFalling back to scanning...\n" unless $stdin =~ /^\{/;
44+
45+
my $json_pkg;
46+
eval {
47+
require JSON::XS;
48+
$json_pkg = "JSON::XS";
49+
1;
50+
} or do {
51+
require JSON::PP;
52+
$json_pkg = "JSON::PP";
53+
};
54+
55+
my $o = $json_pkg->new->utf8->decode($stdin);
56+
die "Watchman: $o->{error}.\nFalling back to scanning...\n" if $o->{error};
57+
58+
local $, = "\0";
59+
print @{$o->{files}};
60+
'

0 commit comments

Comments
 (0)