Skip to content

Commit

Permalink
feat(repository): add async list_files function for retrieving file e…
Browse files Browse the repository at this point in the history
…ntries
  • Loading branch information
wsxiaoys committed Jan 11, 2025
1 parent 6e33341 commit e74a963
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
19 changes: 19 additions & 0 deletions crates/tabby-git/src/file_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,25 @@ pub async fn search(
Ok(entries)
}

pub async fn list(
repository: git2::Repository,
rev: Option<&str>,
limit: Option<usize>,
) -> anyhow::Result<Vec<GitFileSearch>> {
let entries: Vec<GitFileSearch> = stream! {
for await (is_file, basepath) in walk_stream(repository, rev).await {
let r#type = if is_file { "file" } else { "dir" };
let basepath = basepath.display().to_string();
yield GitFileSearch::new(r#type, basepath, Vec::new());
}
}
.take(limit.unwrap_or(usize::MAX))
.collect()
.await;

Ok(entries)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions crates/tabby-git/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub async fn search_files(
file_search::search(git2::Repository::open(root)?, rev, pattern, limit).await
}

pub async fn list_files(
root: &Path,
rev: Option<&str>,
limit: Option<usize>,
) -> anyhow::Result<Vec<GitFileSearch>> {
file_search::list(git2::Repository::open(root)?, rev, limit).await
}

pub async fn grep(
root: &Path,
rev: Option<&str>,
Expand Down
4 changes: 1 addition & 3 deletions ee/tabby-schema/src/schema/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ pub trait RepositoryService: Send + Sync {
id: &ID,
rev: Option<&str>,
top_n: Option<usize>,
) -> Result<Vec<FileEntrySearchResult>> {
Ok(vec![])
}
) -> Result<Vec<FileEntrySearchResult>>;

async fn grep(
&self,
Expand Down
24 changes: 24 additions & 0 deletions ee/tabby-webserver/src/service/repository/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ impl RepositoryService for RepositoryServiceImpl {
Ok(matching)
}

async fn list_files(
&self,
policy: &AccessPolicy,
kind: &RepositoryKind,
id: &ID,
rev: Option<&str>,
top_n: Option<usize>,
) -> Result<Vec<FileEntrySearchResult>> {
let dir = self.resolve_repository(policy, kind, id).await?.dir;
let files = tabby_git::list_files(&dir, rev, top_n)
.await
.map(|x| {
x.into_iter()
.map(|f| FileEntrySearchResult {
r#type: f.r#type,
path: f.path,
indices: f.indices,
})
.collect()
})
.map_err(anyhow::Error::from)?;
Ok(files)
}

async fn grep(
&self,
policy: &AccessPolicy,
Expand Down

0 comments on commit e74a963

Please sign in to comment.