Skip to content

Commit

Permalink
feat(git): add grep to search file in git repo (#2180)
Browse files Browse the repository at this point in the history
* chore: extract rev_to_commit

* feat(git): add grep to search file in git repo

* [autofix.ci] apply automated fixes

* improve test coverage

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
wsxiaoys and autofix-ci[bot] authored May 19, 2024
1 parent d391ac9 commit 0d63bea
Show file tree
Hide file tree
Showing 9 changed files with 777 additions and 31 deletions.
123 changes: 118 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion crates/tabby-git/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ mime_guess.workspace = true
futures.workspace = true
async-stream.workspace = true
tokio.workspace = true
tracing.workspace = true
ignore.workspace = true
grep = "0.3.1"
base64-serde = "0.7"
base64 = "0.22"

[dev-dependencies]
assert_matches.workspace = true
assert_matches.workspace = true
24 changes: 5 additions & 19 deletions crates/tabby-git/src/file_search.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use std::path::{Path, PathBuf};
use std::path::PathBuf;

use async_stream::stream;
use futures::{Stream, StreamExt};
use git2::TreeWalkResult;

use super::rev_to_commit;
use crate::bytes2path;

pub struct GitFileSearch {
pub r#type: &'static str,
pub path: String,
Expand All @@ -27,13 +30,7 @@ fn walk(
rev: Option<&str>,
tx: tokio::sync::mpsc::Sender<(bool, PathBuf)>,
) -> anyhow::Result<()> {
let commit = if let Some(rev) = rev {
let reference = repository.revparse_single(rev)?;
reference.peel_to_commit()?
} else {
repository.head()?.peel_to_commit()?
};

let commit = rev_to_commit(&repository, rev)?;
let tree = commit.tree()?;

tree.walk(git2::TreeWalkMode::PreOrder, |path, entry| {
Expand Down Expand Up @@ -107,17 +104,6 @@ pub async fn search(
Ok(entries)
}

#[cfg(unix)]
pub fn bytes2path(b: &[u8]) -> &Path {
use std::os::unix::prelude::*;
Path::new(std::ffi::OsStr::from_bytes(b))
}
#[cfg(windows)]
pub fn bytes2path(b: &[u8]) -> &Path {
use std::str;
Path::new(str::from_utf8(b).unwrap())
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading

0 comments on commit 0d63bea

Please sign in to comment.