use crate::remote::models::repo_config::RepositoryConfiguration;
use super::super::common::{ErrorType, Method};
use cause::{Cause, cause};
use git2::Repository;
use std::process::Command;
#[derive(Clone)]
pub struct RepositoryFetcher;
impl RepositoryFetcher {
pub async fn fetch_repository(
&self,
config: &RepositoryConfiguration,
cache_path: &str,
) -> Result<(), Cause<ErrorType>> {
let config = config.clone();
let cache_path = cache_path.to_string();
if Self::is_cache_valid(&config, &cache_path) {
println!("Using cached repository: {}", config.url);
return Ok(());
}
println!("Fetching repository: {} to cache", config.url);
let cache_path_clone = cache_path.clone();
tokio::task::spawn_blocking(move || {
Self::execute_git_clone(&config, &cache_path_clone)?;
if !matches!(config.mtd, Some(Method::ShallowNoSparse)) {
Self::execute_git_checkout(&cache_path_clone, &config.branch)?;
}
Ok(())
})
.await
.map_err(|e| cause!(ErrorType::GitCloneCommand).msg(format!("Task join error: {e:?}")))??;
println!("Repository fetched and cached at: {cache_path}");
Ok(())
}
fn execute_git_clone(
config: &RepositoryConfiguration,
cache_path: &str,
) -> Result<(), Cause<ErrorType>> {
if std::path::Path::new(cache_path).exists() {
std::fs::remove_dir_all(cache_path)
.map_err(|e| cause!(ErrorType::GitCloneCommand).src(e))?;
}
if matches!(config.mtd, Some(Method::ShallowNoSparse)) {
let output = Command::new("git")
.args([
"clone",
"--depth",
"1",
"--branch",
&config.branch,
&config.url,
cache_path,
])
.output()
.map_err(|e| cause!(ErrorType::GitCloneCommand).src(e))?;
if !output.status.success() {
return Err(
cause!(ErrorType::GitCloneCommand).msg(String::from_utf8_lossy(&output.stderr))
);
}
} else {
Repository::clone(&config.url, cache_path)
.map_err(|e| cause!(ErrorType::GitCloneCommand).src(e))?;
}
Ok(())
}
fn execute_git_checkout(cache_path: &str, rev: &str) -> Result<(), Cause<ErrorType>> {
let repo = Repository::open(cache_path)
.map_err(|e| cause!(ErrorType::GitCheckoutCommand).src(e))?;
let obj = repo
.revparse_single(rev)
.map_err(|e| cause!(ErrorType::GitCheckoutCommand).src(e))?
.peel(git2::ObjectType::Commit)
.map_err(|e| cause!(ErrorType::GitCheckoutCommand).src(e))?;
repo.checkout_tree(&obj, None)
.map_err(|e| cause!(ErrorType::GitCheckoutCommand).src(e))?;
repo.set_head_detached(obj.id())
.map_err(|e| cause!(ErrorType::GitCheckoutCommand).src(e))?;
Ok(())
}
fn is_cache_valid(_config: &RepositoryConfiguration, cache_path: &str) -> bool {
std::path::Path::new(cache_path).exists()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fetcher_creation() {
let _ = RepositoryFetcher;
}
#[test]
fn test_is_cache_valid_with_nonexistent_path() {
let config = RepositoryConfiguration::new(
"https://github.com/example/repo.git".to_string(),
"main".to_string(),
"./src/module1".to_string(),
vec!["src/".to_string()],
None,
None,
);
let result = RepositoryFetcher::is_cache_valid(&config, "/definitely/does/not/exist");
assert!(!result);
}
}