Skip to main content

GitOperations

Trait GitOperations 

Source
pub trait GitOperations {
Show 17 methods // Required methods fn get_staged_diff(&self) -> Result<String>; fn get_uncommitted_diff(&self) -> Result<String>; fn get_commit_diff(&self, commit_hash: &str) -> Result<String>; fn get_range_diff(&self, range: &str) -> Result<String>; fn get_file_content(&self, path: &str) -> Result<String>; fn commit(&self, message: &str) -> Result<()>; fn commit_amend(&self, message: &str) -> Result<()>; fn get_current_branch(&self) -> Result<Option<String>>; fn get_diff_stats(&self, diff: &str) -> Result<DiffStats>; fn has_staged_changes(&self) -> Result<bool>; fn get_commit_history(&self) -> Result<Vec<CommitInfo>>; fn get_commit_line_stats(&self, hash: &str) -> Result<(usize, usize)>; fn is_empty(&self) -> Result<bool>; fn get_staged_files(&self) -> Result<Vec<String>>; fn unstage_all(&self) -> Result<()>; fn stage_files(&self, files: &[String]) -> Result<()>; fn get_workdir(&self) -> Result<PathBuf>;
}
Expand description

Unified interface for Git operations.

This trait abstracts all Git repository operations, making it easier to test and extend. Main implementation: GitRepository.

§Design

  • Pure Rust interface, independent of concrete backend implementation.
  • Supports mocking in tests (via mockall).
  • Uses unified error handling via GcopError.

§Example

use gcop_rs::git::{GitOperations, repository::GitRepository};

let repo = GitRepository::open(None)?;
let diff = repo.get_staged_diff()?;
println!("Staged changes:\n{}", diff);

Required Methods§

Source

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

Returns the diff for staged changes.

Equivalent to git diff --cached --unified=3.

§Returns
  • Ok(diff) - diff text (possibly empty)
  • Err(_) - git operation failed
§Errors
  • Repository is not initialized
  • Insufficient permissions
Source

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

Returns the diff for unstaged changes.

Contains only index -> workdir changes (unstaged), equivalent to git diff (without --cached).

§Returns
  • Ok(diff) - diff text (possibly empty)
  • Err(_) - git operation failed
Source

fn get_commit_diff(&self, commit_hash: &str) -> Result<String>

Returns the diff for a specific commit.

Equivalent to git diff <commit_hash>^! (returns only the diff content).

§Parameters
  • commit_hash: commit SHA (supports short hash)
§Returns
  • Ok(diff) - diff text
  • Err(_) - commit does not exist or git operation failed
Source

fn get_range_diff(&self, range: &str) -> Result<String>

Returns the diff for a commit range.

Supports multiple formats:

  • HEAD~3..HEAD - last 3 commits
  • main..feature - difference between branches
  • abc123..def456 - difference between two commits
§Parameters
  • range: Git range expression
§Returns
  • Ok(diff) - diff text
  • Err(_) - invalid range or git operation failed
Source

fn get_file_content(&self, path: &str) -> Result<String>

Reads the complete content of a file.

Reads file contents from the working tree (not from git objects).

§Parameters
  • path: file path (relative to the current working directory or absolute path)
§Returns
  • Ok(content) - file contents
  • Err(_) - file does not exist, is not a regular file, or read failed
Source

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

Executes git commit.

Commits staged changes to the repository.

§Parameters
  • message: commit message (supports multiple lines)
§Returns
  • Ok(()) - commit succeeded
  • Err(_) - no staged changes, hook failure, or another git error
§Errors
§Notes
  • Triggers pre-commit and commit-msg hooks.
  • Uses name/email configured in git config.
Source

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

Executes git commit --amend.

Amends the most recent commit with a new message. If there are staged changes, they are included in the amended commit.

§Parameters
  • message: new commit message
§Returns
  • Ok(()) - amend succeeded
  • Err(_) - no commits to amend, hook failure, or another git error
Source

fn get_current_branch(&self) -> Result<Option<String>>

Returns the current branch name.

§Returns
  • Ok(Some(name)) - current branch name (for example "main")
  • Ok(None) - detached HEAD
  • Err(_) - git operation failed
§Example
let repo = GitRepository::open(None)?;
if let Some(branch) = repo.get_current_branch()? {
    println!("On branch: {}", branch);
} else {
    println!("Detached HEAD");
}
Source

fn get_diff_stats(&self, diff: &str) -> Result<DiffStats>

Calculates diff statistics.

Parses diff text and extracts changed files plus insert/delete counts.

§Parameters
  • diff: diff text (from get_*_diff() methods)
§Returns
  • Ok(stats) - parsed statistics
  • Err(_) - invalid diff format
§Example
let repo = GitRepository::open(None)?;
let diff = repo.get_staged_diff()?;
let stats = repo.get_diff_stats(&diff)?;
println!("{} files, +{} -{}",
    stats.files_changed.len(), stats.insertions, stats.deletions);
Source

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

Checks whether the index contains staged changes.

Fast check for files added to the index with git add.

§Returns
  • Ok(true) - staged changes exist
  • Ok(false) - staging area is empty
  • Err(_) - git operation failed
Source

fn get_commit_history(&self) -> Result<Vec<CommitInfo>>

Returns commit history for the current branch.

Returns commit entries in reverse chronological order.

§Returns
  • Ok(history) - commit list (newest first)
  • Err(_) - git operation failed
§Notes
  • Only includes history reachable from the current branch HEAD.
  • Empty repositories return an empty list.
Source

fn get_commit_line_stats(&self, hash: &str) -> Result<(usize, usize)>

Returns line-level diff statistics for a single commit.

Diffs the commit tree against its first parent (or empty tree for root commits). Uses git2’s native Diff::stats() for performance.

§Parameters
  • hash: commit SHA hex string
§Returns
  • Ok((insertions, deletions)) - line counts
  • Err(_) - commit not found or git error
Source

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

Checks whether the repository has no commits.

§Returns
  • Ok(true) - repository is empty (no commits yet)
  • Ok(false) - repository has at least one commit
  • Err(_) - git operation failed
Source

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

Returns the list of currently staged file paths.

Equivalent to collecting filenames from git diff --cached --name-only.

Source

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

Unstages all currently staged files.

Equivalent to git reset HEAD. For empty repositories (no commits), uses git rm --cached -r . instead.

Source

fn stage_files(&self, files: &[String]) -> Result<()>

Stages the specified files.

Equivalent to git add <files>.

Source

fn get_workdir(&self) -> Result<PathBuf>

Returns the repository working directory path.

§Returns
  • Ok(path) - absolute path to the repository working directory
  • Err(_) - bare repository or git operation failed

Implementors§