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.

§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 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 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 origin_url(&self) -> Result<String>

Get the URL of the origin remote.

§Errors

Returns error if origin remote is not found.

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(&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 Debug for Repository

Source§

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

Formats the value using the given formatter. Read more

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.