pub struct GitRepo { /* private fields */ }Expand description
A Git repository handle providing async operations via CLI commands.
Implementations§
Source§impl GitRepo
impl GitRepo
Sourcepub fn new(path: impl AsRef<Path>) -> Self
pub fn new(path: impl AsRef<Path>) -> Self
Creates a new GitRepo instance for an existing local repository.
§Arguments
path- The filesystem path to the Git repository root directory
Sourcepub async fn clone(url: &str, target: impl AsRef<Path>) -> Result<Self>
pub async fn clone(url: &str, target: impl AsRef<Path>) -> Result<Self>
Clones a Git repository from a remote URL to a local path.
§Arguments
url- The remote repository URL (HTTPS, SSH, or file://)target- The local directory where the repository will be clonedprogress- Optional progress bar for user feedback
§Errors
- The URL is invalid or unreachable
- Authentication fails
- The target directory already exists and is not empty
- Network connectivity issues
- Insufficient disk space
Sourcepub async fn fetch(&self, auth_url: Option<&str>) -> Result<()>
pub async fn fetch(&self, auth_url: Option<&str>) -> Result<()>
Fetches updates from the remote repository without modifying the working tree.
§Arguments
auth_url- Optional URL with authentication for private repositoriesprogress- Optional progress bar for network operation feedback
§Errors
- Network connectivity fails
- Authentication is rejected
- The remote repository is unavailable
- The local repository is in an invalid state
Sourcepub async fn checkout(&self, ref_name: &str) -> Result<()>
pub async fn checkout(&self, ref_name: &str) -> Result<()>
Checks out a specific Git reference (branch, tag, or commit hash).
§Arguments
ref_name- The Git reference to checkout (branch, tag, or commit)
§Errors
- The reference doesn’t exist in the repository
- The repository is in an invalid state
- File system permissions prevent checkout
- The working directory is locked by another process
Sourcepub async fn get_remote_url(&self) -> Result<String>
pub async fn get_remote_url(&self) -> Result<String>
Retrieves the URL of the remote ‘origin’ repository.
§Return Value
- HTTPS:
https://github.com/user/repo.git - SSH:
git@github.com:user/repo.git - File:
file:///path/to/repo.git
§Errors
- No ‘origin’ remote is configured
- The repository is not a valid Git repository
- Git command execution fails
- File system access is denied
Sourcepub fn is_git_repo(&self) -> bool
pub fn is_git_repo(&self) -> bool
Checks if the directory contains a valid Git repository.\n ///
Sourcepub async fn verify_url(url: &str) -> Result<()>
pub async fn verify_url(url: &str) -> Result<()>
Verifies that a Git repository URL is accessible without performing a full clone.
§Arguments
url- The repository URL to verify
§Errors
- Network issues: DNS resolution, connectivity, timeouts
- Authentication failures: Invalid credentials, expired tokens
- Repository issues: Repository doesn’t exist, access denied
- Local path issues: File doesn’t exist (for
file://URLs) - URL format issues: Malformed or unsupported URL schemes
Sourcepub async fn clone_bare_with_context(
url: &str,
target: impl AsRef<Path>,
context: Option<&str>,
) -> Result<Self>
pub async fn clone_bare_with_context( url: &str, target: impl AsRef<Path>, context: Option<&str>, ) -> Result<Self>
Sourcepub async fn create_worktree(
&self,
worktree_path: impl AsRef<Path>,
reference: Option<&str>,
) -> Result<Self>
pub async fn create_worktree( &self, worktree_path: impl AsRef<Path>, reference: Option<&str>, ) -> Result<Self>
Sourcepub async fn create_worktree_with_context(
&self,
worktree_path: impl AsRef<Path>,
reference: Option<&str>,
context: Option<&str>,
) -> Result<Self>
pub async fn create_worktree_with_context( &self, worktree_path: impl AsRef<Path>, reference: Option<&str>, context: Option<&str>, ) -> Result<Self>
Sourcepub async fn remove_worktree(
&self,
worktree_path: impl AsRef<Path>,
) -> Result<()>
pub async fn remove_worktree( &self, worktree_path: impl AsRef<Path>, ) -> Result<()>
Remove a worktree associated with this repository.
§Arguments
worktree_path- The path to the worktree to remove
Sourcepub async fn list_worktrees(&self) -> Result<Vec<PathBuf>>
pub async fn list_worktrees(&self) -> Result<Vec<PathBuf>>
List all worktrees associated with this repository.
Sourcepub async fn prune_worktrees(&self) -> Result<()>
pub async fn prune_worktrees(&self) -> Result<()>
Prune stale worktree administrative files.
Sourcepub async fn get_current_commit(&self) -> Result<String>
pub async fn get_current_commit(&self) -> Result<String>
Sourcepub async fn resolve_refs_batch(
&self,
refs: &[&str],
) -> Result<HashMap<String, Option<String>>>
pub async fn resolve_refs_batch( &self, refs: &[&str], ) -> Result<HashMap<String, Option<String>>>
Batch resolve multiple refs to SHAs in a single git process.
Uses git rev-parse <ref1> <ref2> ... to resolve all refs at once, reducing
process spawn overhead from O(n) to O(1). This is significantly faster
for Windows where process spawning has high overhead.
§Arguments
refs- Slice of ref specifications to resolve
§Returns
HashMap mapping each input ref to its resolved SHA (or None if not found)
§Performance
- Single process for all refs vs one per ref
- Reduces 100 refs from ~5-10 seconds to ~0.5 seconds on Windows
§Examples
use agpm_cli::git::GitRepo;
let repo = GitRepo::new("/path/to/repo");
let refs = vec!["v1.0.0", "main", "abc1234"];
let results = repo.resolve_refs_batch(&refs).await?;
for (ref_name, sha) in results {
if let Some(sha) = sha {
println!("{} -> {}", ref_name, sha);
} else {
println!("{} not found", ref_name);
}
}