| 56 | |
| 57 | impl ChangeSource for WatchmanChangeSource { |
| 58 | fn changes( |
| 59 | &self, |
| 60 | request: ChangeSourceRequest<'_>, |
| 61 | ) -> Result<ChangeSourceResult, ChangeSourceError> { |
| 62 | validate_token(request.previous_token)?; |
| 63 | let root = request.root.to_str().ok_or_else(|| { |
| 64 | ChangeSourceError::Unavailable("Watchman requires a UTF-8 repository path".into()) |
| 65 | })?; |
| 66 | let since = request |
| 67 | .previous_token |
| 68 | .map(|token| &token.0[TOKEN_PREFIX.len()..]) |
| 69 | .map(std::str::from_utf8) |
| 70 | .transpose() |
| 71 | .map_err(|_| ChangeSourceError::UnknownToken)?; |
| 72 | let mut query = json!({ |
| 73 | "expression": ["type", "f"], |
| 74 | "fields": ["name"] |
| 75 | }); |
| 76 | if let Some(since) = since { |
| 77 | query["since"] = Value::String(since.to_owned()); |
| 78 | } |
| 79 | let mut stdin = serde_json::to_vec(&json!(["query", root, query])) |
| 80 | .map_err(|error| ChangeSourceError::MalformedResponse(error.to_string()))?; |
| 81 | stdin.push(b'\n'); |
| 82 | let output = self.watchman(request.root, &["-j", "--no-pretty"], stdin)?; |
| 83 | if output.status != 0 { |
| 84 | return Err(unavailable("Watchman query", &output)); |
| 85 | } |
| 86 | let (clock, raw_paths) = parse_response( |
| 87 | &output.stdout, |
| 88 | request.previous_token.is_some(), |
| 89 | self.max_candidates, |
| 90 | )?; |
| 91 | let tracked: BTreeSet<_> = request.tracked_paths.iter().cloned().collect(); |
| 92 | let mut paths = BTreeSet::new(); |
| 93 | for bytes in raw_paths { |
| 94 | let path = |
| 95 | RepoPath::new(bytes).map_err(|error| ChangeSourceError::Path(error.to_string()))?; |
| 96 | if tracked.contains(&path) { |
| 97 | paths.insert(path); |
| 98 | } |
| 99 | } |
| 100 | let mut candidates = Vec::with_capacity(paths.len()); |
| 101 | for path in paths { |
| 102 | candidates.push(observe_candidate( |
| 103 | request.root, |
| 104 | path, |
| 105 | ChangeCandidateReason::Explicit, |
| 106 | )?); |
| 107 | } |
| 108 | let mut token = TOKEN_PREFIX.to_vec(); |
| 109 | token.extend_from_slice(clock.as_bytes()); |
| 110 | Ok(ChangeSourceResult { |
| 111 | root: request.root.to_path_buf(), |
| 112 | source: super::ChangeSourceKind::Watchman, |
| 113 | fallback_source: None, |
| 114 | token: ChangeSourceToken(token), |
| 115 | complete: false, |