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§
Sourcefn get_staged_diff(&self) -> Result<String>
fn get_staged_diff(&self) -> Result<String>
Sourcefn get_uncommitted_diff(&self) -> Result<String>
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
Sourcefn get_commit_diff(&self, commit_hash: &str) -> Result<String>
fn get_commit_diff(&self, commit_hash: &str) -> Result<String>
Sourcefn get_range_diff(&self, range: &str) -> Result<String>
fn get_range_diff(&self, range: &str) -> Result<String>
Returns the diff for a commit range.
Supports multiple formats:
HEAD~3..HEAD- last 3 commitsmain..feature- difference between branchesabc123..def456- difference between two commits
§Parameters
range: Git range expression
§Returns
Ok(diff)- diff textErr(_)- invalid range or git operation failed
Sourcefn get_file_content(&self, path: &str) -> Result<String>
fn get_file_content(&self, path: &str) -> Result<String>
Sourcefn commit(&self, message: &str) -> Result<()>
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 succeededErr(_)- no staged changes, hook failure, or another git error
§Errors
GcopError::GitCommand- no staged changesGcopError::Git- libgit2 error
§Notes
- Triggers pre-commit and commit-msg hooks.
- Uses name/email configured in git config.
Sourcefn commit_amend(&self, message: &str) -> Result<()>
fn commit_amend(&self, message: &str) -> Result<()>
Sourcefn get_current_branch(&self) -> Result<Option<String>>
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 HEADErr(_)- 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");
}Sourcefn get_diff_stats(&self, diff: &str) -> Result<DiffStats>
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 (fromget_*_diff()methods)
§Returns
Ok(stats)- parsed statisticsErr(_)- 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);Sourcefn has_staged_changes(&self) -> Result<bool>
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 existOk(false)- staging area is emptyErr(_)- git operation failed
Sourcefn get_commit_history(&self) -> Result<Vec<CommitInfo>>
fn get_commit_history(&self) -> Result<Vec<CommitInfo>>
Sourcefn get_commit_line_stats(&self, hash: &str) -> Result<(usize, usize)>
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 countsErr(_)- commit not found or git error
Sourcefn is_empty(&self) -> Result<bool>
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 commitErr(_)- git operation failed
Sourcefn get_staged_files(&self) -> Result<Vec<String>>
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.
Sourcefn unstage_all(&self) -> Result<()>
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.
Sourcefn stage_files(&self, files: &[String]) -> Result<()>
fn stage_files(&self, files: &[String]) -> Result<()>
Stages the specified files.
Equivalent to git add <files>.
Sourcefn get_workdir(&self) -> Result<PathBuf>
fn get_workdir(&self) -> Result<PathBuf>
Returns the repository working directory path.
§Returns
Ok(path)- absolute path to the repository working directoryErr(_)- bare repository or git operation failed