GitRepo

Struct GitRepo 

Source
pub struct GitRepo {
    pub repo: Repository,
}
Expand description

A convenience wrapper around git2::Repository with helper methods for creating branches, staging all changes, committing, and simple history lookups.

GitRepo owns a live Repository handle; it neither deletes the on-disk repo nor spawns threads. See the upstream git2 docs for lower-level primitives.

Fields§

§repo: Repository

Implementations§

Source§

impl GitRepo

Source

pub fn init_with_branch(path: &Path, branch_name: Option<&str>) -> Result<Self>

Open an existing Git repository at path or initialize a new one, then set user.name / user.email. If branch_name is provided, move HEAD to that branch:

  • If the branch already exists, HEAD attaches to it.
  • If it doesn’t exist yet, HEAD becomes an unborn (orphan) branch and will gain its first commit when you call GitRepo::commit_all_changes.
§Errors
  • Repository open/init failures.
  • Config access or user.name/user.email write failures.
  • Any failure from GitRepo::create_branch when branch_name is Some(...).
§Examples
use std::path::Path;
let repo = oci2git::GitRepo::init_with_branch(Path::new("./.temp"), Some("main"))?;
Source

pub fn create_branch( &self, branch_name: &str, from_commit: Option<Oid>, ) -> Result<()>

Create/select a local branch and make HEAD point to it.

  • from_commit: Some(oid) — create branch_name at oid, set HEAD to it, and hard-reset the worktree to the target commit (deterministic clean start).
  • from_commit: Noneselect an unborn/orphan branch by setting HEAD to refs/heads/{branch_name}. The ref will be created on the first commit.
§Errors
  • Invalid OID or missing commit when branching from a commit.
  • Branch creation, setting HEAD, or checkout/reset failures.
§Examples

// Orphan branch:
repo.create_branch("scratch", None)?;
Source

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

Stage all paths and create a commit on HEAD.

Returns Ok(true) if the index had changes, Ok(false) if the commit was made with an empty tree diff (useful for metadata-only commits).

Internally, this:

  • creates a signature with USERNAME/EMAIL,
  • add_all(["*"], ...) to stage paths,
  • writes the index and tree,
  • looks up the current HEAD commit (if any) as the parent,
  • and calls commit("HEAD", ...). (For unborn branches, this becomes the root commit.)
§Errors
  • Index operations, tree writes, signature creation, or commit creation can fail.
§Examples

let changed = repo.commit_all_changes("init")?;
assert!(changed);
Source

pub fn get_branch_commits(&self, branch_name: &str) -> Result<Vec<Oid>>

Return all commit OIDs for branch_name, ordered oldest → newest.

§Errors
  • Branch not found or revwalk/iteration failures.
Source

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

List names of all local branches (e.g., ["ubuntu#latest...", "nginx#latest#linux-arm64#..."]).

§Errors
  • Branch iteration or name resolution failures.
Source

pub fn branch_exists(&self, branch_name: &str) -> bool

Return true if a local branch named branch_name exists.

This is a convenience wrapper around repo.find_branch(...).is_ok().

Source

pub fn exists_and_has_commits(&self) -> bool

Heuristic: does the repo have any local branches?

Because branch creation on an unborn branch only materializes after the first commit, a freshly initialized repo with HEAD pointing at an unborn branch will still report “no branches” here until the first commit lands.

Source

pub fn read_file_from_commit( &self, commit_oid: Oid, file_path: &str, ) -> Result<String>

Read a UTF-8 file file_path from the given commit_oid.

§Errors
  • Unknown commit, missing tree entries, non-UTF-8 blob content.
§Examples
// read the file from that commit
let text = repo.read_file_from_commit(oid, "Image.md")?;
println!("{}", text);
Source

pub fn get_commit_successors(&self, commit_oid: Option<Oid>) -> Result<Vec<Oid>>

Find the next commits (successors) after commit_oid across all local branches.

  • If Some(oid), returns the commit immediately after oid on any branch where it appears. Multiple branches may yield multiple successors.
  • If None, returns root commits (commits with no parents) for each branch.

The output is deduplicated.

§Errors
  • Branch enumeration or history traversal failures.

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