Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use watchman's watch-project if available #390

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@ def paths!

UNIXSocket.open(sockname) do |socket|
root = Pathname.new(@path).realpath.to_s
roots = Watchman::Utils.query(['watch-list'], socket)['roots']
if !roots.include?(root)
# this path isn't being watched yet; try to set up watch
result = Watchman::Utils.query(['watch', root], socket)
# use `watch-project` for efficiency if it's available
if use_watch_project?
result = Watchman::Utils.query(['watch-project', root], socket)
root = extract_value(result, 'watch')
relative_root = extract_value(result, 'relative_path') if result.has_key?('relative_path')
else
roots = Watchman::Utils.query(['watch-list'], socket)['roots']
if !roots.include?(root)
# this path isn't being watched yet; try to set up watch
result = Watchman::Utils.query(['watch', root], socket)

# root_restrict_files setting may prevent Watchman from working
# or enforce_root_files/root_files (>= version 3.1)
extract_value(result)
# root_restrict_files setting may prevent Watchman from working
# or enforce_root_files/root_files (>= version 3.1)
extract_value(result)
end
end

query = ['query', root, {
query_params = {
'expression' => ['type', 'f'],
'fields' => ['name'],
}]
}
query_params['relative_root'] = relative_root if relative_root;
query = ['query', root, query_params]
paths = Watchman::Utils.query(query, socket)

# could return error if watch is removed
Expand Down Expand Up @@ -68,6 +77,15 @@ def get_raw_sockname
end
raw_sockname
end

# watch_project is available in 3.1+ but it's awkward to use without
# relative_root (3.3+), so use the latter as our minimum version.
def use_watch_project?
return @use_watch_project if defined?(@use_watch_project)
version = %x{watchman --version 2>/dev/null}
major, minor = version.split('.')[0..1] if !$?.exitstatus.nil? && $?.exitstatus.zero? && version
@use_watch_project = major.to_i > 3 || (major.to_i == 3 && minor.to_i >= 3)
end
end
end
end
Expand Down