Skip to main content

GitRepo

Struct GitRepo 

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

Git repository operations wrapper.

This struct provides a clean API for common git operations, executing them via std::process::Command internally.

Implementations§

Source§

impl GitRepo

Source

pub fn new(workdir: impl Into<PathBuf>) -> Self

Create a new GitRepo for the given working directory.

§Arguments
  • workdir - Path to the git repository working directory
§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new("/path/to/repo");
Source

pub fn workdir(&self) -> &Path

Get the working directory path.

Source

pub fn current_branch(&self) -> Result<String>

Get the current branch name.

§Errors

Returns an error if the git command fails or if HEAD is detached.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
let branch = repo.current_branch()?;
println!("Current branch: {}", branch);
Source

pub fn delete_branch(&self, name: &str, force: bool) -> Result<()>

Delete a local branch.

§Arguments
  • name - The branch name to delete
  • force - If true, use -D instead of -d for force deletion
§Errors

Returns an error if the git command fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.delete_branch("feature/old-branch", false)?;
Source

pub fn detect_default_branch(&self) -> String

Detect the default branch (main/master) of the repository.

Queries the remote origin to determine the default branch. Falls back to “main” if detection fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
let default = repo.detect_default_branch();
println!("Default branch: {}", default);
Source

pub fn create_worktree(&self, path: &Path, branch: &str) -> Result<()>

Create a new worktree with a new branch.

§Arguments
  • path - Path where the worktree should be created
  • branch - Name of the new branch to create
§Errors

Returns an error if the git command fails.

§Example
use std::path::Path;
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.create_worktree(Path::new(".trees/feature"), "feature/new-feature")?;
Source

pub fn remove_worktree(&self, path: &str, force: bool) -> Result<()>

Remove a worktree.

§Arguments
  • path - Path of the worktree to remove
  • force - If true, force removal even with uncommitted changes
§Errors

Returns an error if the git command fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.remove_worktree(".trees/feature", false)?;
Source

pub fn head_short_sha(&self) -> Result<Option<String>>

Get the short SHA of HEAD.

Returns None if the repository has no commits yet.

§Errors

Returns an error if the git command fails for reasons other than an empty repository.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
if let Some(sha) = repo.head_short_sha()? {
    println!("Current commit: {}", sha);
}
Source

pub fn add(&self, path: &str) -> Result<()>

Stage a file or path pattern.

§Arguments
  • path - The file or path pattern to stage (e.g., “.”, “src/”, “*.rs”)
§Errors

Returns an error if the git command fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.add("src/main.rs")?;
Source

pub fn commit(&self, message: &str) -> Result<()>

Create a commit with the given message.

§Arguments
  • message - The commit message
§Errors

Returns an error if the git command fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.add(".")?;
repo.commit("feat: add new feature")?;
Source

pub fn push(&self) -> Result<()>

Push to origin.

§Errors

Returns an error if the git command fails.

§Example
use gba_core::git::GitRepo;

let repo = GitRepo::new(".");
repo.push()?;

Trait Implementations§

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> 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, 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<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