|
1
|
//! Reading and searching repository contents, and repo discovery across all |
|
2
|
//! repositories the user can see. Served by the MCP tools; see [`crate::mcp`]. |
|
3
|
|
|
4
|
use { |
|
5
|
crate::{ |
|
6
|
client::Client, |
|
7
|
error::Result, |
|
8
|
models::{CodeHit, FileContents, RepositorySummary}, |
|
9
|
}, |
|
10
|
serde_json::json, |
|
11
|
}; |
|
12
|
|
|
13
|
impl Client { |
|
14
|
/// Search repositories by an owner/name substring. Unlike |
|
15
|
/// [`Client::repos`], this spans every repo the user can see, not only the |
|
16
|
/// ones they own. |
|
17
|
pub async fn search_repos( |
|
18
|
&self, |
|
19
|
query: Option<&str>, |
|
20
|
limit: Option<u32>, |
|
21
|
) -> Result<Vec<RepositorySummary>> { |
|
22
|
let mut args = json!({}); |
|
23
|
if let Some(query) = query { |
|
24
|
args["query"] = query.into(); |
|
25
|
} |
|
26
|
if let Some(limit) = limit { |
|
27
|
args["limit"] = limit.into(); |
|
28
|
} |
|
29
|
self.call_tool_as("list_repositories", args).await |
|
30
|
} |
|
31
|
|
|
32
|
/// Read one file at a ref, defaulting to the repo's default branch. |
|
33
|
pub async fn file_contents( |
|
34
|
&self, |
|
35
|
repo: &str, |
|
36
|
path: &str, |
|
37
|
git_ref: Option<&str>, |
|
38
|
) -> Result<FileContents> { |
|
39
|
let mut args = json!({ "repo": repo, "path": path }); |
|
40
|
if let Some(git_ref) = git_ref { |
|
41
|
args["ref"] = git_ref.into(); |
|
42
|
} |
|
43
|
self.call_tool_as("get_file_contents", args).await |
|
44
|
} |
|
45
|
|
|
46
|
/// Fixed-string, case-insensitive search across a repository's tracked |
|
47
|
/// files at a ref. |
|
48
|
pub async fn search_code( |
|
49
|
&self, |
|
50
|
repo: &str, |
|
51
|
query: &str, |
|
52
|
git_ref: Option<&str>, |
|
53
|
limit: Option<u32>, |
|
54
|
) -> Result<Vec<CodeHit>> { |
|
55
|
let mut args = json!({ "repo": repo, "query": query }); |
|
56
|
if let Some(git_ref) = git_ref { |
|
57
|
args["ref"] = git_ref.into(); |
|
58
|
} |
|
59
|
if let Some(limit) = limit { |
|
60
|
args["limit"] = limit.into(); |
|
61
|
} |
|
62
|
self.call_tool_as("search_code", args).await |
|
63
|
} |
|
64
|
} |