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: RepositoryImplementations§
Source§impl GitRepo
impl GitRepo
Sourcepub fn init_with_branch(path: &Path, branch_name: Option<&str>) -> Result<Self>
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,
HEADattaches to it. - If it doesn’t exist yet,
HEADbecomes an unborn (orphan) branch and will gain its first commit when you callGitRepo::commit_all_changes.
§Errors
- Repository open/init failures.
- Config access or
user.name/user.emailwrite failures. - Any failure from
GitRepo::create_branchwhenbranch_nameisSome(...).
§Examples
use std::path::Path;
let repo = oci2git::GitRepo::init_with_branch(Path::new("./.temp"), Some("main"))?;Sourcepub fn create_branch(
&self,
branch_name: &str,
from_commit: Option<Oid>,
) -> Result<()>
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)— createbranch_nameatoid, setHEADto it, and hard-reset the worktree to the target commit (deterministic clean start).from_commit: None— select an unborn/orphan branch by settingHEADtorefs/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)?;Sourcepub fn commit_all_changes(&self, message: &str) -> Result<bool>
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
HEADcommit (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);Sourcepub fn get_branch_commits(&self, branch_name: &str) -> Result<Vec<Oid>>
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.
Sourcepub fn get_all_branches(&self) -> Result<Vec<String>>
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.
Sourcepub fn branch_exists(&self, branch_name: &str) -> bool
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().
Sourcepub fn exists_and_has_commits(&self) -> bool
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.
Sourcepub fn get_commit_successors(&self, commit_oid: Option<Oid>) -> Result<Vec<Oid>>
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 afteroidon 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.