pub struct Repository { /* private fields */ }Expand description
High-level wrapper around a git repository.
Implementations§
Source§impl Repository
impl Repository
Sourcepub fn staged_diff_hunks(&self) -> Result<Vec<Hunk>>
pub fn staged_diff_hunks(&self) -> Result<Vec<Hunk>>
Get the staged diff as a list of hunks.
Parses git diff --cached output to extract individual hunks
with file paths and line ranges.
§Errors
Returns error if git diff fails or output cannot be parsed.
Sourcepub fn blame_lines(
&self,
file_path: &str,
start: u32,
end: u32,
) -> Result<Vec<BlameResult>>
pub fn blame_lines( &self, file_path: &str, start: u32, end: u32, ) -> Result<Vec<BlameResult>>
Query git blame for a specific line range in a file.
Returns the commits that last modified lines in the given range.
Uses git blame -L <start>,<end> for targeted queries.
§Errors
Returns error if blame fails or commit cannot be found.
Source§impl Repository
impl Repository
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Open a repository at the given path.
§Errors
Returns error if no repository found at path or any parent.
Sourcepub fn open_current() -> Result<Self>
pub fn open_current() -> Result<Self>
Open the repository containing the current directory.
§Errors
Returns error if not inside a git repository.
Sourcepub fn state(&self) -> RepositoryState
pub fn state(&self) -> RepositoryState
Get the current repository state.
Sourcepub fn is_rebasing(&self) -> bool
pub fn is_rebasing(&self) -> bool
Check if there’s a rebase in progress.
Sourcepub fn head_detached(&self) -> Result<bool>
pub fn head_detached(&self) -> Result<bool>
Check if HEAD is detached (not pointing at a branch).
§Errors
Returns error if HEAD cannot be read (e.g. unborn repo).
Sourcepub fn current_branch(&self) -> Result<String>
pub fn current_branch(&self) -> Result<String>
Sourcepub fn branch_commit(&self, branch_name: &str) -> Result<Oid>
pub fn branch_commit(&self, branch_name: &str) -> Result<Oid>
Sourcepub fn remote_branch_commit(&self, branch_name: &str) -> Result<Oid>
pub fn remote_branch_commit(&self, branch_name: &str) -> Result<Oid>
Get the commit ID of a remote branch tip.
Uses the configured upstream if set, otherwise falls back to origin/<branch>.
§Errors
Returns error if branch not found.
Sourcepub fn create_branch(&self, name: &str) -> Result<Oid>
pub fn create_branch(&self, name: &str) -> Result<Oid>
Sourcepub fn list_branches(&self) -> Result<Vec<String>>
pub fn list_branches(&self) -> Result<Vec<String>>
Sourcepub fn branch_exists(&self, name: &str) -> bool
pub fn branch_exists(&self, name: &str) -> bool
Check if a branch exists.
Sourcepub fn delete_branch(&self, name: &str) -> Result<()>
pub fn delete_branch(&self, name: &str) -> Result<()>
Sourcepub fn is_clean(&self) -> Result<bool>
pub fn is_clean(&self) -> Result<bool>
Check if the working directory is clean (no modified or staged files).
Untracked files are ignored - only tracked files that have been modified or staged count as “dirty”.
§Errors
Returns error if status check fails.
Sourcepub fn require_clean(&self) -> Result<()>
pub fn require_clean(&self) -> Result<()>
Ensure working directory is clean, returning error if not.
§Errors
Returns DirtyWorkingDirectory if there are uncommitted changes.
Sourcepub fn stage_all(&self) -> Result<()>
pub fn stage_all(&self) -> Result<()>
Stage all changes (tracked and untracked files).
Equivalent to git add -A.
§Errors
Returns error if staging fails.
Sourcepub fn has_staged_changes(&self) -> Result<bool>
pub fn has_staged_changes(&self) -> Result<bool>
Sourcepub fn create_commit(&self, message: &str) -> Result<Oid>
pub fn create_commit(&self, message: &str) -> Result<Oid>
Create a commit with the given message on HEAD.
Handles both normal commits (with parent) and initial commits (no parent).
§Errors
Returns error if commit creation fails.
Sourcepub fn amend_commit(&self, new_message: Option<&str>) -> Result<Oid>
pub fn amend_commit(&self, new_message: Option<&str>) -> Result<Oid>
Amend the last commit with staged changes.
Equivalent to git commit --amend --no-edit or
git commit --amend -m "message" if a new message is provided.
§Errors
Returns error if amend fails or no commits exist.
Sourcepub fn find_commit(&self, oid: Oid) -> Result<Commit<'_>>
pub fn find_commit(&self, oid: Oid) -> Result<Commit<'_>>
Sourcepub fn branch_commit_message(&self, branch_name: &str) -> Result<String>
pub fn branch_commit_message(&self, branch_name: &str) -> Result<String>
Get the commit message from a branch’s tip commit.
§Errors
Returns error if branch doesn’t exist or has no commits.
Sourcepub fn signature(&self) -> Result<Signature<'_>>
pub fn signature(&self) -> Result<Signature<'_>>
Get the default signature for commits.
§Errors
Returns error if git config doesn’t have user.name/email.
Sourcepub fn rebase_onto(&self, target: Oid) -> Result<()>
pub fn rebase_onto(&self, target: Oid) -> Result<()>
Rebase the current branch onto a target commit.
Returns Ok(()) on success, or Err(RebaseConflict) if there are conflicts.
§Errors
Returns error if rebase fails or conflicts occur.
Sourcepub fn rebase_onto_from(&self, new_base: Oid, old_base: Oid) -> Result<()>
pub fn rebase_onto_from(&self, new_base: Oid, old_base: Oid) -> Result<()>
Rebase the current branch onto a new base, replaying only commits after old_base.
This is equivalent to git rebase --onto <new_base> <old_base>.
Use this when the old_base was squash-merged and you want to bring only
the unique commits from the current branch.
§Errors
Returns error if rebase fails or conflicts occur.
Sourcepub fn conflicting_files(&self) -> Result<Vec<String>>
pub fn conflicting_files(&self) -> Result<Vec<String>>
Sourcepub fn predict_rebase_conflicts(
&self,
branch: &str,
onto: Oid,
) -> Result<Vec<ConflictPrediction>>
pub fn predict_rebase_conflicts( &self, branch: &str, onto: Oid, ) -> Result<Vec<ConflictPrediction>>
Predict conflicts that would occur when rebasing a branch onto a target.
This simulates the rebase by using git merge-tree to check if each
commit would conflict when applied to the target. Unlike an actual rebase,
this does not modify any files or refs.
§Arguments
branch- The branch to check for conflictsonto- The target commit to rebase onto
§Returns
A list of commits that would cause conflicts, along with the conflicting files. An empty list means no conflicts are predicted.
§Errors
Returns error if git operations fail.
Sourcepub fn rebase_abort(&self) -> Result<()>
pub fn rebase_abort(&self) -> Result<()>
Sourcepub fn rebase_continue(&self) -> Result<()>
pub fn rebase_continue(&self) -> Result<()>
Sourcepub fn remote_divergence(&self, branch: &str) -> Result<RemoteDivergence>
pub fn remote_divergence(&self, branch: &str) -> Result<RemoteDivergence>
Check how a local branch relates to its remote counterpart.
Uses the configured upstream if set, otherwise falls back to origin/<branch>.
Compares the local branch tip with the remote tracking branch to determine
if the local branch is ahead, behind, diverged, or in sync with the remote.
Uses graph_ahead_behind for efficient single-traversal computation.
§Errors
Returns error if branch doesn’t exist or git operations fail.
Sourcepub fn origin_url(&self) -> Result<String>
pub fn origin_url(&self) -> Result<String>
Sourcepub fn detect_default_branch(&self) -> Option<String>
pub fn detect_default_branch(&self) -> Option<String>
Detect the default branch from the remote’s HEAD.
Checks refs/remotes/origin/HEAD to determine the remote’s default branch.
Returns None if the remote HEAD is not set (e.g., fresh clone without --set-upstream).
Sourcepub fn parse_github_remote(url: &str) -> Result<(String, String)>
pub fn parse_github_remote(url: &str) -> Result<(String, String)>
Parse owner and repo name from a GitHub URL.
Supports both HTTPS and SSH URLs:
https://github.com/owner/repo.gitgit@github.com:owner/repo.git
§Errors
Returns error if URL cannot be parsed.
Sourcepub fn pull_ff(&self) -> Result<()>
pub fn pull_ff(&self) -> Result<()>
Pull (fast-forward only) the current branch from origin.
This fetches and merges origin/<branch> into the current branch,
but only if it can be fast-forwarded.
§Errors
Returns error if pull fails or fast-forward is not possible.
Sourcepub const fn inner(&self) -> &Repository
pub const fn inner(&self) -> &Repository
Get a reference to the underlying git2 repository.
Use sparingly - prefer high-level methods.
Trait Implementations§
Source§impl AbsorbOps for Repository
impl AbsorbOps for Repository
Source§impl Debug for Repository
impl Debug for Repository
Source§impl GitOps for Repository
impl GitOps for Repository
Source§fn head_detached(&self) -> Result<bool>
fn head_detached(&self) -> Result<bool>
Source§fn is_rebasing(&self) -> bool
fn is_rebasing(&self) -> bool
Source§fn branch_exists(&self, name: &str) -> bool
fn branch_exists(&self, name: &str) -> bool
Source§fn create_branch(&self, name: &str) -> Result<Oid>
fn create_branch(&self, name: &str) -> Result<Oid>
Source§fn remote_branch_commit(&self, branch: &str) -> Result<Oid>
fn remote_branch_commit(&self, branch: &str) -> Result<Oid>
Source§fn branch_commit_message(&self, branch: &str) -> Result<String>
fn branch_commit_message(&self, branch: &str) -> Result<String>
Source§fn commits_between(&self, from: Oid, to: Oid) -> Result<Vec<Oid>>
fn commits_between(&self, from: Oid, to: Oid) -> Result<Vec<Oid>>
Source§fn count_commits_between(&self, from: Oid, to: Oid) -> Result<usize>
fn count_commits_between(&self, from: Oid, to: Oid) -> Result<usize>
Source§fn require_clean(&self) -> Result<()>
fn require_clean(&self) -> Result<()>
Source§fn has_staged_changes(&self) -> Result<bool>
fn has_staged_changes(&self) -> Result<bool>
Source§fn create_commit(&self, message: &str) -> Result<Oid>
fn create_commit(&self, message: &str) -> Result<Oid>
Source§fn amend_commit(&self, new_message: Option<&str>) -> Result<Oid>
fn amend_commit(&self, new_message: Option<&str>) -> Result<Oid>
Source§fn rebase_onto(&self, target: Oid) -> Result<()>
fn rebase_onto(&self, target: Oid) -> Result<()>
Source§fn rebase_onto_from(&self, onto: Oid, from: Oid) -> Result<()>
fn rebase_onto_from(&self, onto: Oid, from: Oid) -> Result<()>
from onto onto).