pub fn ensure_valid_git_repo(path: &Path) -> Result<()>
Expand description
Ensures a directory contains a valid Git repository or returns a detailed error.
This function validates that the specified path contains a Git repository,
providing a AgpmError::GitRepoInvalid
with actionable guidance if the
validation fails. It’s the error-throwing equivalent of is_valid_git_repo()
.
§Arguments
path
- The directory path to validate as a Git repository
§Return Value
Ok(())
if the path contains a valid.git
directoryErr(AgpmError::GitRepoInvalid)
if the path is not a Git repository
§Error Context
The returned error includes:
- The specific path that failed validation
- Clear description of what constitutes a valid Git repository
- Suggestions for initializing or cloning repositories
§Examples
use agpm_cli::git::ensure_valid_git_repo;
use std::path::Path;
let path = Path::new("/home/user/my-project");
// Validate before operations
ensure_valid_git_repo(path)?;
// Path is guaranteed to be a Git repository beyond this point
println!("Validated Git repository at: {}", path.display());
§Error Handling Pattern
use agpm_cli::git::ensure_valid_git_repo;
use agpm_cli::core::AgpmError;
use std::path::Path;
let path = Path::new("/some/directory");
match ensure_valid_git_repo(path) {
Ok(_) => println!("Valid repository found"),
Err(e) => {
if let Some(AgpmError::GitRepoInvalid { path }) = e.downcast_ref::<AgpmError>() {
eprintln!("Directory {} is not a Git repository", path);
eprintln!("Try: git clone <url> {} or git init {}", path, path);
}
}
}
§Integration with GitRepo
This function provides validation before creating GitRepo
instances:
use agpm_cli::git::{ensure_valid_git_repo, GitRepo};
use std::path::Path;
let path = Path::new("/path/to/repo");
// Validate first
ensure_valid_git_repo(path)?;
// Then create repository handle
let repo = GitRepo::new(path);
let tags = repo.list_tags().await?;
§Use Cases
- Precondition validation: Ensure paths are Git repositories before operations
- Error-first APIs: Provide detailed errors rather than boolean returns
- Pipeline validation: Fail fast in processing pipelines
- User feedback: Give actionable error messages with suggestions