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>
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 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 rebase_abort(&self) -> Result<()>
pub fn rebase_abort(&self) -> Result<()>
Sourcepub fn rebase_continue(&self) -> Result<()>
pub fn rebase_continue(&self) -> Result<()>
Sourcepub fn origin_url(&self) -> Result<String>
pub fn origin_url(&self) -> Result<String>
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.