Skip to main content

Repository

Struct Repository 

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

High-level wrapper around a git repository.

Implementations§

Source§

impl Repository

Source

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.

Source

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

pub fn create_fixup_commit(&self, target: Oid) -> Result<Oid>

Create a fixup commit targeting the specified commit.

Equivalent to git commit --fixup=<target>. Staged changes must exist before calling this.

§Errors

Returns error if commit creation fails.

Source

pub fn is_ancestor(&self, ancestor: Oid, descendant: Oid) -> Result<bool>

Check if a commit is an ancestor of another commit.

Returns true if ancestor is reachable from descendant.

§Errors

Returns error if the check fails.

Source§

impl Repository

Source

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.

Source

pub fn open_current() -> Result<Self>

Open the repository containing the current directory.

§Errors

Returns error if not inside a git repository.

Source

pub fn workdir(&self) -> Option<&Path>

Get the path to the repository root (workdir).

Source

pub fn git_dir(&self) -> &Path

Get the path to the .git directory.

Source

pub fn state(&self) -> RepositoryState

Get the current repository state.

Source

pub fn is_rebasing(&self) -> bool

Check if there’s a rebase in progress.

Source

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

Source

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

Get the name of the current branch.

§Errors

Returns error if HEAD is detached.

Source

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

Get the commit SHA for a branch.

§Errors

Returns error if branch doesn’t exist.

Source

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.

Source

pub fn create_branch(&self, name: &str) -> Result<Oid>

Create a new branch at the current HEAD.

§Errors

Returns error if branch creation fails.

Source

pub fn checkout(&self, branch_name: &str) -> Result<()>

Checkout a branch.

§Errors

Returns error if checkout fails.

Source

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

List all local branches.

§Errors

Returns error if branch listing fails.

Source

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

Check if a branch exists.

Source

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

Delete a local branch.

§Errors

Returns error if branch deletion fails.

Source

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.

Source

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

Ensure working directory is clean, returning error if not.

§Errors

Returns DirtyWorkingDirectory if there are uncommitted changes.

Source

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

Stage all changes (tracked and untracked files).

Equivalent to git add -A.

§Errors

Returns error if staging fails.

Source

pub fn has_staged_changes(&self) -> Result<bool>

Check if there are staged changes ready to commit.

§Errors

Returns error if status check fails.

Source

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.

Source

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.

Source

pub fn find_commit(&self, oid: Oid) -> Result<Commit<'_>>

Get a commit by its SHA.

§Errors

Returns error if commit not found.

Source

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.

Source

pub fn merge_base(&self, one: Oid, two: Oid) -> Result<Oid>

Get the merge base between two commits.

§Errors

Returns error if merge base calculation fails.

Source

pub fn count_commits_between(&self, from: Oid, to: Oid) -> Result<usize>

Count commits between two points.

§Errors

Returns error if revwalk fails.

Source

pub fn commits_between(&self, from: Oid, to: Oid) -> Result<Vec<Oid>>

Get commits between two points.

§Errors

Return error if revwalk fails.

Source

pub fn reset_branch(&self, branch_name: &str, target: Oid) -> Result<()>

Hard reset a branch to a specific commit.

§Errors

Returns error if reset fails.

Source

pub fn signature(&self) -> Result<Signature<'_>>

Get the default signature for commits.

§Errors

Returns error if git config doesn’t have user.name/email.

Source

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.

Source

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.

Source

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

Get list of files with conflicts.

§Errors

Returns error if status check fails.

Source

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 conflicts
  • onto - 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.

Source

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

Abort an in-progress rebase.

§Errors

Returns error if abort fails.

Source

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

Continue an in-progress rebase.

§Errors

Returns error if continue fails or new conflicts occur.

Source

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.

Source

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

Get the URL of the origin remote.

§Errors

Returns error if origin remote is not found.

Source

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

Source

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.git
  • git@github.com:owner/repo.git
§Errors

Returns error if URL cannot be parsed.

Source

pub fn push(&self, branch: &str, force: bool) -> Result<()>

Push a branch to the remote.

§Errors

Returns error if push fails.

Source

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

Fetch all remote tracking refs from origin.

§Errors

Returns error if fetch fails.

Source

pub fn fetch(&self, branch: &str) -> Result<()>

Fetch a branch from origin.

§Errors

Returns error if fetch fails.

Source

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.

Source

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

Source§

fn staged_diff_hunks(&self) -> Result<Vec<Hunk>>

Get the staged diff as a list of hunks.
Source§

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.
Source§

fn is_ancestor(&self, ancestor: Oid, descendant: Oid) -> Result<bool>

Check if a commit is an ancestor of another commit.
Source§

fn create_fixup_commit(&self, target: Oid) -> Result<Oid>

Create a fixup commit targeting the specified commit.
Source§

impl Debug for Repository

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl GitOps for Repository

Source§

fn workdir(&self) -> Option<&Path>

Get the working directory path.
Source§

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

Get the current branch name. Read more
Source§

fn head_detached(&self) -> Result<bool>

Check if HEAD is detached.
Source§

fn is_rebasing(&self) -> bool

Check if a rebase is in progress.
Source§

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

Check if a branch exists.
Source§

fn create_branch(&self, name: &str) -> Result<Oid>

Create a new branch at the current HEAD. Read more
Source§

fn checkout(&self, branch: &str) -> Result<()>

Checkout a branch.
Source§

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

Delete a local branch.
Source§

fn list_branches(&self) -> Result<Vec<String>>

List all local branches.
Source§

fn branch_commit(&self, branch: &str) -> Result<Oid>

Get the commit ID for a branch.
Source§

fn remote_branch_commit(&self, branch: &str) -> Result<Oid>

Get the commit ID for a remote branch.
Source§

fn branch_commit_message(&self, branch: &str) -> Result<String>

Get the commit message for a branch’s tip.
Source§

fn merge_base(&self, one: Oid, two: Oid) -> Result<Oid>

Find the merge base of two commits.
Source§

fn commits_between(&self, from: Oid, to: Oid) -> Result<Vec<Oid>>

Get commits between two OIDs.
Source§

fn count_commits_between(&self, from: Oid, to: Oid) -> Result<usize>

Count commits between two OIDs.
Source§

fn is_clean(&self) -> Result<bool>

Check if the working directory is clean.
Source§

fn require_clean(&self) -> Result<()>

Require that the working directory is clean.
Source§

fn stage_all(&self) -> Result<()>

Stage all changes.
Source§

fn has_staged_changes(&self) -> Result<bool>

Check if there are staged changes.
Source§

fn create_commit(&self, message: &str) -> Result<Oid>

Create a commit with the staged changes.
Source§

fn amend_commit(&self, new_message: Option<&str>) -> Result<Oid>

Amend the last commit with staged changes.
Source§

fn rebase_onto(&self, target: Oid) -> Result<()>

Rebase the current branch onto a target commit.
Source§

fn rebase_onto_from(&self, onto: Oid, from: Oid) -> Result<()>

Rebase using –onto semantics (rebase commits from from onto onto).
Source§

fn conflicting_files(&self) -> Result<Vec<String>>

Get files with conflicts during a rebase.
Source§

fn predict_rebase_conflicts( &self, branch: &str, onto: Oid, ) -> Result<Vec<ConflictPrediction>>

Predict conflicts that would occur when rebasing a branch onto a target. Read more
Source§

fn rebase_abort(&self) -> Result<()>

Abort a rebase in progress.
Source§

fn rebase_continue(&self) -> Result<()>

Continue a rebase after resolving conflicts.
Source§

fn origin_url(&self) -> Result<String>

Get the origin URL.
Source§

fn remote_divergence(&self, branch: &str) -> Result<RemoteDivergence>

Check divergence between local and remote branch.
Source§

fn detect_default_branch(&self) -> Option<String>

Detect the default branch (main/master). Read more
Source§

fn push(&self, branch: &str, force: bool) -> Result<()>

Push a branch to the remote.
Source§

fn fetch_all(&self) -> Result<()>

Fetch all remotes.
Source§

fn fetch(&self, branch: &str) -> Result<()>

Fetch a specific branch.
Source§

fn pull_ff(&self) -> Result<()>

Pull with fast-forward only.
Source§

fn reset_branch(&self, branch: &str, commit: Oid) -> Result<()>

Reset a branch to a specific commit.

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.