GitRepo

Struct GitRepo 

Source
pub struct GitRepo { /* private fields */ }
Expand description

A Git repository handle providing async operations via CLI commands.

Implementations§

Source§

impl GitRepo

Source

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
Source

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 cloned
  • progress - 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
Source

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 repositories
  • progress - 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
Source

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
Source

pub async fn list_tags(&self) -> Result<Vec<String>>

Lists all tags in the repository, sorted by Git’s default ordering.

§Return Value
§Errors
  • The repository path doesn’t exist
  • The directory is not a valid Git repository
  • Git command execution fails
  • File system permissions prevent access
  • Lock conflicts persist after retry attempts
Source

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
Source

pub fn is_git_repo(&self) -> bool

Checks if the directory contains a valid Git repository.\n ///

Source

pub fn path(&self) -> &Path

Returns the filesystem path to the Git repository.

§Return Value
Source

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
Source

pub async fn clone_bare(url: &str, target: impl AsRef<Path>) -> Result<Self>

Clone a repository as a bare repository (no working directory).

§Arguments
  • url - The remote repository URL
  • target - The local directory where the bare repository will be stored
  • progress - Optional progress bar for user feedback
§Returns
Source

pub async fn clone_bare_with_context( url: &str, target: impl AsRef<Path>, context: Option<&str>, ) -> Result<Self>

Clone a repository as a bare repository with logging context.

§Arguments
  • url - The remote repository URL
  • target - The local directory where the bare repository will be stored
  • progress - Optional progress bar for user feedback
  • context - Optional context for logging (e.g., dependency name)
§Returns
Source

pub async fn create_worktree( &self, worktree_path: impl AsRef<Path>, reference: Option<&str>, ) -> Result<Self>

Create a new worktree from this repository.

§Arguments
  • worktree_path - The path where the worktree will be created
  • reference - Optional Git reference (branch/tag/commit) to checkout
§Returns
Source

pub async fn create_worktree_with_context( &self, worktree_path: impl AsRef<Path>, reference: Option<&str>, context: Option<&str>, ) -> Result<Self>

Create a new worktree from this repository with logging context.

§Arguments
  • worktree_path - The path where the worktree will be created
  • reference - Optional Git reference (branch/tag/commit) to checkout
  • context - Optional context for logging (e.g., dependency name)
§Returns
Source

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
Source

pub async fn list_worktrees(&self) -> Result<Vec<PathBuf>>

List all worktrees associated with this repository.

Source

pub async fn prune_worktrees(&self) -> Result<()>

Prune stale worktree administrative files.

Source

pub async fn is_bare(&self) -> Result<bool>

Check if this repository is a bare repository.

Source

pub async fn get_current_commit(&self) -> Result<String>

Get the current commit SHA of the repository.

§Returns
§Errors
  • The repository is not valid
  • HEAD is not pointing to a valid commit
  • Git command fails
Source

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);
    }
}
Source

pub async fn resolve_to_sha(&self, ref_spec: Option<&str>) -> Result<String>

Resolves a Git reference (tag, branch, commit) to its full SHA-1 hash.

§Arguments
  • ref_spec - The Git reference to resolve (tag, branch, short/full SHA, or None for HEAD)
§Returns
§Errors
  • The reference doesn’t exist in the repository
  • The repository is invalid or corrupted
  • Git command execution fails
Source

pub async fn get_current_branch(&self) -> Result<String>

Source

pub async fn get_default_branch(&self) -> Result<String>

Gets the default branch name for the repository.

§Returns
§Errors
  • Git commands fail with non-recoverable errors
  • Lock conflicts occur (propagated for caller to retry)
  • Default branch cannot be determined

Trait Implementations§

Source§

impl Clone for GitRepo

Source§

fn clone(&self) -> GitRepo

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for GitRepo

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more