Repo

Struct Repo 

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

Represents a Git repository with high-level operation methods

This struct wraps the libgit2 Repository type and provides simplified methods for common Git operations.

§Examples

use git::repo::Repo;

// Open an existing repository
let repo = Repo::open("./my-repo").expect("Failed to open repository");

// Create a new branch
repo.create_branch("feature-branch").expect("Failed to create branch");

// Make changes and commit them
repo.add_all().expect("Failed to stage changes");
let commit_id = repo.commit("feat: add new feature").expect("Failed to commit");

Implementations§

Source§

impl Repo

Source

pub fn create(path: &str) -> Result<Self, RepoError>

Creates a new Git repository at the specified path

This initializes a new Git repository with an initial commit on the ‘main’ branch.

§Arguments
  • path - The path where the repository should be created
§Returns
  • Result<Self, RepoError> - A new Repo instance or an error
§Errors

This function will return an error if:

  • The path cannot be canonicalized
  • The directory cannot be created
  • Git repository initialization fails
  • The repository cannot be opened after creation
§Examples
use git::repo::Repo;

let repo = Repo::create("/tmp/new-repo").expect("Failed to create repository");
println!("Repository created at: {}", repo.get_repo_path().display());
Source

pub fn open(path: &str) -> Result<Self, RepoError>

Opens an existing Git repository at the specified path

§Arguments
  • path - The path to the existing repository
§Returns
  • Result<Self, RepoError> - A Repo instance or an error
§Errors

This function will return an error if:

  • The path cannot be canonicalized
  • The path does not contain a valid Git repository
  • The repository cannot be opened due to permission issues
  • The repository is corrupted or invalid
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-project").expect("Failed to open repository");
let branch = repo.get_current_branch().expect("Failed to get current branch");
println!("Current branch: {}", branch);
Source

pub fn clone(url: &str, path: &str) -> Result<Self, RepoError>

Clones a Git repository from a URL to a local path

§Arguments
  • url - The URL of the repository to clone
  • path - The local path where the repository should be cloned
§Returns
  • Result<Self, RepoError> - A Repo instance or an error
§Errors

This function will return an error if:

  • The path cannot be canonicalized
  • The URL is invalid or unreachable
  • Network connectivity issues prevent cloning
  • Authentication is required but not provided or fails
  • The destination path already exists or cannot be created
  • Insufficient disk space or permissions
§Examples
use git::repo::Repo;

let repo = Repo::clone("https://github.com/example/repo.git", "./cloned-repo")
    .expect("Failed to clone repository");
Source

pub fn get_repo_path(&self) -> &Path

Gets the local path of the repository

§Returns
  • &Path - The path to the repository
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
println!("Repository path: {}", repo.get_repo_path().display());
Source

pub fn config(&self, username: &str, email: &str) -> Result<&Self, RepoError>

Configures the repository with user information and core settings

§Arguments
  • username - The Git user name
  • email - The Git user email
§Returns
  • Result<&Self, RepoError> - A reference to self for method chaining, or an error
§Errors

This function will return an error if:

  • The repository configuration cannot be accessed
  • The configuration settings cannot be written
  • Invalid configuration values are provided
  • File system permissions prevent configuration changes
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.config("Jane Doe", "jane@example.com").expect("Failed to configure repository");
Source

pub fn create_branch(&self, branch_name: &str) -> Result<&Self, RepoError>

Creates a new branch based on the current HEAD

§Arguments
  • branch_name - The name for the new branch
§Returns
  • Result<&Self, RepoError> - A reference to self for method chaining, or an error
§Errors

This function will return an error if:

  • The current HEAD reference cannot be accessed
  • The HEAD cannot be peeled to a commit
  • A branch with the same name already exists
  • The repository is in an invalid state
  • Insufficient permissions to create the branch
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.create_branch("feature/new-feature").expect("Failed to create branch");
Source

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

Lists all local branches in the repository

§Returns
  • Result<Vec<String>, RepoError> - A list of branch names, or an error
§Errors

This function will return an error if:

  • The repository’s branch references cannot be accessed
  • Branch iteration fails due to corrupted references
  • Branch names contain invalid UTF-8 sequences
  • The repository is in an invalid state
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let branches = repo.list_branches().expect("Failed to list branches");
for branch in branches {
    println!("Branch: {}", branch);
}
Source

pub fn branch_exists(&self, branch_name: &str) -> Result<bool, RepoError>

Check if a branch exists in the repository

§Arguments
  • branch_name - The name of the branch to check
§Returns
  • Result<bool, RepoError> - True if the branch exists, false otherwise
§Errors

This function will return an error if:

  • The repository is not accessible
  • Branch references cannot be accessed
  • The branch name contains invalid characters
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let exists = repo.branch_exists("main").expect("Failed to check branch");
if exists {
    println!("Branch 'main' exists");
}
Source

pub fn get_merge_base( &self, branch1: &str, branch2: &str, ) -> Result<String, RepoError>

Get the merge base between two branches or commits

§Arguments
  • branch1 - First branch or commit reference
  • branch2 - Second branch or commit reference
§Returns
  • Result<String, RepoError> - The SHA of the merge base commit
§Errors

This function will return an error if:

  • Either branch or commit reference doesn’t exist
  • No common ancestor exists between the references
  • Repository access fails
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let merge_base = repo.get_merge_base("main", "feature-branch")
    .expect("Failed to get merge base");
println!("Merge base: {}", merge_base);
Source

pub fn get_files_changed_between( &self, from_ref: &str, to_ref: &str, ) -> Result<Vec<GitChangedFile>, RepoError>

Get files changed between two commits or branches

§Arguments
  • from_ref - Starting commit or branch reference
  • to_ref - Ending commit or branch reference
§Returns
  • Result<Vec<GitChangedFile>, RepoError> - List of changed files with status
§Errors

This function will return an error if:

  • Either reference doesn’t exist
  • Repository access fails
  • Diff calculation fails
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let changed_files = repo.get_files_changed_between("main", "feature-branch")
    .expect("Failed to get changed files");
for file in changed_files {
    println!("{}: {:?}", file.path, file.status);
}
Source

pub fn get_files_changed_in_commit( &self, commit_hash: &str, ) -> Result<Vec<GitChangedFile>, RepoError>

Gets the list of files changed in a specific commit.

Returns the files that were added, modified, or deleted in the given commit by comparing it with its parent commit.

§Arguments
  • commit_hash - The commit hash to analyze
§Returns
  • Result<Vec<GitChangedFile>, RepoError> - List of changed files with their status
§Errors

This function will return an error if:

  • The commit hash is invalid or not found
  • The commit has no parent (initial commit)
  • The diff operation fails
  • Tree objects cannot be accessed
§Examples
use sublime_git_tools::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let files = repo.get_files_changed_in_commit("abc123def456")
    .expect("Failed to get changed files");

for file in files {
    println!("{}: {:?}", file.path, file.status);
}
Source

pub fn list_config(&self) -> Result<HashMap<String, String>, RepoError>

Lists all configuration entries for the repository

§Returns
  • Result<HashMap<String, String>, RepoError> - A map of config keys to values, or an error
§Errors

This function will return an error if:

  • The repository configuration cannot be accessed
  • Configuration entries cannot be read or iterated
  • Configuration values contain invalid data
  • File system permissions prevent reading configuration
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let config = repo.list_config().expect("Failed to list config");
for (key, value) in config {
    println!("{} = {}", key, value);
}
Source

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

Checks out a local branch

§Arguments
  • branch_name - The name of the branch to checkout
§Returns
  • Result<&Self, RepoError> - A reference to self for method chaining, or an error
§Errors

This function will return an error if:

  • The specified branch does not exist
  • The branch reference is invalid or corrupted
  • The HEAD reference cannot be updated
  • There are uncommitted changes that would be lost
  • File system permissions prevent checkout
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.checkout("feature-branch").expect("Failed to checkout branch");
Source

pub fn get_current_branch(&self) -> Result<String, RepoError>

Gets the name of the currently checked out branch

§Returns
  • Result<String, RepoError> - The current branch name, or an error
§Errors

This function will return an error if:

  • The HEAD reference cannot be accessed
  • The HEAD reference is invalid or corrupted
  • The repository is in a detached HEAD state
  • The branch name contains invalid characters
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let branch = repo.get_current_branch().expect("Failed to get current branch");
println!("Current branch: {}", branch);
Source

pub fn create_tag( &self, tag: &str, message: Option<String>, ) -> Result<&Self, RepoError>

Creates a new tag at the current HEAD

§Arguments
  • tag - The name for the new tag
  • message - Optional message for the tag
§Returns
  • Result<(), RepoError> - Success or an error
§Errors

This function will return an error if:

  • A tag with the same name already exists
  • The repository signature cannot be created
  • The HEAD reference cannot be accessed
  • The target object for the tag cannot be found
  • Insufficient permissions to create the tag
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.create_tag("v1.0.0", Some("Version 1.0.0 release".to_string()))
    .expect("Failed to create tag");
Source

pub fn add(&self, file_path: &str) -> Result<&Self, RepoError>

Adds a file to the Git index

§Arguments
  • file_path - The path to the file to add
§Returns
  • Result<&Self, RepoError> - A reference to self for method chaining, or an error
§Errors

This function will return an error if:

  • The specified file does not exist
  • The file path is invalid or inaccessible
  • The Git index cannot be accessed or modified
  • The index cannot be written to disk
  • Insufficient permissions to read the file or write the index
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.add("src/main.rs").expect("Failed to add file");
Source

pub fn add_all(&self) -> Result<&Self, RepoError>

Adds all changed files to the Git index

§Returns
  • Result<&Self, RepoError> - A reference to self for method chaining, or an error
§Errors

This function will return an error if:

  • The Git index cannot be accessed or modified
  • Files cannot be read due to permission issues
  • The index cannot be written to disk
  • Some files are locked or in use by other processes
  • The working directory contains invalid or corrupted files
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
repo.add_all().expect("Failed to add all changes");
Source

pub fn get_last_tag(&self) -> Result<String, RepoError>

Gets the name of the last tag in the repository

§Returns
  • Result<String, RepoError> - The last tag name, or an error
§Errors

This function will return an error if:

  • No tags exist in the repository
  • Tag references cannot be accessed
  • Tag names are corrupted or invalid
  • The repository state is invalid
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
match repo.get_last_tag() {
    Ok(tag) => println!("Last tag: {}", tag),
    Err(e) => println!("No tags found or error: {}", e),
}
Source

pub fn get_current_sha(&self) -> Result<String, RepoError>

Gets the SHA of the current HEAD commit

§Returns
  • Result<String, RepoError> - The current commit SHA, or an error
§Errors

This function will return an error if:

  • The HEAD reference cannot be accessed
  • The HEAD reference has no target (repository is empty)
  • The repository is in an invalid state
  • The commit object cannot be found
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let sha = repo.get_current_sha().expect("Failed to get current SHA");
println!("Current commit: {}", sha);
Source

pub fn get_previous_sha(&self) -> Result<String, RepoError>

Gets the SHA of the parent of the current HEAD commit

§Returns
  • Result<String, RepoError> - The previous commit SHA, or an error
§Errors

This function will return an error if:

  • The HEAD reference cannot be accessed
  • The HEAD cannot be peeled to a commit
  • The parent commit cannot be found or accessed
  • The repository has no commits (empty repository)
  • The commit objects are corrupted
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let prev_sha = repo.get_previous_sha().expect("Failed to get previous SHA");
println!("Previous commit: {}", prev_sha);
Source

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

Creates a new commit with the current index

§Arguments
  • message - The commit message
§Returns
  • Result<String, RepoError> - The new commit’s SHA, or an error
§Errors

This function will return an error if:

  • The repository signature cannot be created
  • The HEAD reference cannot be accessed
  • The index cannot be accessed or is empty
  • The tree cannot be written or found
  • The commit cannot be created due to repository state issues
  • Insufficient permissions to write the commit
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
// First add some files
repo.add("src/main.rs").expect("Failed to add file");
// Then commit
let commit_id = repo.commit("fix: update main.rs").expect("Failed to commit");
println!("Created commit: {}", commit_id);
Source

pub fn commit_changes(&self, message: &str) -> Result<String, RepoError>

Adds all changes and creates a new commit

This method performs both add_all() and commit() in one step.

§Arguments
  • message - The commit message
§Returns
  • Result<String, RepoError> - The new commit’s SHA, or an error
§Errors

This function will return an error if:

  • The repository signature cannot be created
  • The HEAD reference cannot be accessed
  • Files cannot be added to the index due to permission issues
  • The tree cannot be written or found
  • The commit cannot be created due to repository state issues
  • There are no changes to commit
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let commit_id = repo.commit_changes("feat: add new feature").expect("Failed to commit changes");
println!("Created commit: {}", commit_id);
Source

pub fn status_porcelain(&self) -> Result<Vec<String>, RepoError>

Gets the status of the repository in porcelain format

Returns a list of changed file paths.

§Returns
  • Result<Vec<String>, RepoError> - List of changed file paths, or an error
§Errors

This function will return an error if:

  • The repository status cannot be read
  • File system permissions prevent accessing working directory files
  • The index is corrupted or cannot be read
  • The working directory contains invalid or inaccessible files
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let status = repo.status_porcelain().expect("Failed to get status");
for file in status {
    println!("Changed file: {}", file);
}
Source

pub fn get_status_detailed(&self) -> Result<Vec<GitChangedFile>, RepoError>

Get detailed file status information including staging state

Returns detailed information about file changes including whether files are staged, have working directory changes, and their status type.

§Returns
  • Result<Vec<GitChangedFile>, RepoError> - List of files with detailed status
§Errors

This function will return an error if:

  • Git status cannot be read
  • Repository is in an invalid state
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let detailed_status = repo.get_status_detailed()
    .expect("Failed to get detailed status");

for file in detailed_status {
    if file.staged {
        println!("Staged: {} ({:?})", file.path, file.status);
    }
    if file.workdir {
        println!("Working dir: {} ({:?})", file.path, file.status);
    }
}
Source

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

Get only staged files (files in the index ready for commit)

Returns a list of files that are currently staged and ready to be committed.

§Returns
  • Result<Vec<String>, RepoError> - List of staged file paths
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let staged_files = repo.get_staged_files()
    .expect("Failed to get staged files");

println!("Files ready for commit: {:?}", staged_files);
Source

pub fn get_branch_from_commit( &self, sha: &str, ) -> Result<Option<String>, RepoError>

Finds the branch that contains a specific commit

§Arguments
  • sha - The commit SHA to find
§Returns
  • Result<Option<String>, RepoError> - The branch name if found, None if not found, or an error
§Errors

This function will return an error if:

  • The provided SHA string is not a valid commit hash
  • The commit object cannot be found in the repository
  • Branch references cannot be accessed or iterated
  • Branch names are corrupted or invalid
  • The repository graph cannot be analyzed
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let commit_sha = repo.get_current_sha().expect("Failed to get current SHA");

match repo.get_branch_from_commit(&commit_sha) {
    Ok(Some(branch)) => println!("Commit {} is in branch: {}", commit_sha, branch),
    Ok(None) => println!("Commit {} is not in any branch", commit_sha),
    Err(e) => println!("Error: {}", e),
}
Source

pub fn get_branches_containing_commit( &self, sha: &str, ) -> Result<Vec<String>, RepoError>

Finds all branches that contain a specific commit

§Arguments
  • sha - The commit SHA to find
§Returns
  • Result<Vec<String>, RepoError> - List of branch names containing the commit, or an error
§Errors

This function will return an error if:

  • The provided SHA string is not a valid commit hash
  • The commit object cannot be found in the repository
  • Branch references cannot be accessed or iterated
  • Branch names are corrupted or invalid
  • The repository graph cannot be analyzed
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let commit_sha = repo.get_current_sha().expect("Failed to get current SHA");

let branches = repo.get_branches_containing_commit(&commit_sha)
    .expect("Failed to find branches");
for branch in branches {
    println!("Branch contains commit: {}", branch);
}
Source

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

Merges the specified branch into the current HEAD

This function attempts to merge the given branch_name into the currently checked out branch. It handles fast-forward merges and normal merges. If merge conflicts occur, it returns a MergeConflictError.

§Arguments
  • branch_name - The name of the branch to merge into the current branch.
§Returns
  • Result<(), RepoError> - Success or an error, including MergeConflictError.
§Errors

This function will return an error if:

  • The current HEAD reference cannot be accessed
  • The branch to merge does not exist or cannot be resolved
  • Merge conflicts occur that cannot be automatically resolved
  • The repository is in an invalid state for merging
  • File system permissions prevent merge operations
  • The working directory has uncommitted changes that would conflict
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo")?;
repo.checkout("main")?; // Ensure we are on the target branch
repo.merge("feature-branch")?; // Merge feature-branch into main
println!("Merge successful!");
Source

pub fn push( &self, remote_name: &str, follow_tags: Option<bool>, ) -> Result<bool, RepoError>

Pushes the current branch to a remote repository

§Arguments
  • remote_name - The name of the remote (e.g., “origin”)
  • follow_tags - Whether to also push tags
§Returns
  • Result<bool, RepoError> - Success indicator or an error
§Errors

This function will return an error if:

  • The specified remote does not exist
  • Authentication fails (SSH keys, credentials)
  • Network connectivity issues prevent pushing
  • The remote repository rejects the push (non-fast-forward, etc.)
  • The current branch has no commits to push
  • File system permissions prevent accessing SSH keys
  • The remote server is unreachable or down
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
// Push current branch with tags
repo.push("origin", Some(true)).expect("Failed to push");
Source

pub fn fetch( &self, remote_name: &str, refspecs: Option<&[&str]>, prune: bool, ) -> Result<bool, RepoError>

Fetches changes from a remote repository

§Arguments
  • remote_name - The name of the remote (e.g., “origin”)
  • refspecs - Optional reference specs to fetch
  • prune - Whether to prune deleted references
§Returns
  • Result<bool, RepoError> - Success indicator or an error
§Errors

This function will return an error if:

  • The specified remote does not exist
  • Authentication fails (SSH keys, credentials)
  • Network connectivity issues prevent fetching
  • Invalid refspecs are provided
  • The remote repository is unreachable
  • File system permissions prevent accessing SSH keys
  • The local repository cannot be updated with fetched data
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
// Fetch with default refspecs and no pruning
repo.fetch("origin", None, false).expect("Failed to fetch");

// Fetch a specific branch and prune
repo.fetch("origin", Some(&["refs/heads/main:refs/remotes/origin/main"]), true)
    .expect("Failed to fetch specific branch");
Source

pub fn pull( &self, remote_name: &str, branch_name: Option<&str>, ) -> Result<bool, RepoError>

Pulls changes from a remote repository

This fetches from the remote and merges the changes into the current branch.

§Arguments
  • remote_name - The name of the remote (e.g., “origin”)
  • branch_name - Optional branch name to pull from (defaults to tracking branch)
§Returns
  • Result<bool, RepoError> - Success indicator or an error
§Errors

This function will return an error if:

  • The specified remote does not exist
  • Authentication fails during fetch operation
  • Network connectivity issues prevent fetching
  • The remote branch does not exist
  • Merge conflicts occur during the pull
  • The current branch cannot be fast-forwarded
  • The working directory has uncommitted changes that would be lost
  • The repository state prevents merging
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
// Pull from the tracking branch
repo.pull("origin", None).expect("Failed to pull");

// Pull from a specific branch
repo.pull("origin", Some("feature-branch")).expect("Failed to pull from feature branch");
Source

pub fn push_with_ssh_config( &self, remote_name: &str, follow_tags: Option<bool>, ssh_key_paths: Vec<PathBuf>, ) -> Result<bool, RepoError>

Pushes the current branch to a remote repository with custom SSH key paths

§Arguments
  • remote_name - The name of the remote (e.g., “origin”)
  • follow_tags - Whether to also push tags
  • ssh_key_paths - Paths to SSH keys to try for authentication
§Returns
  • Result<bool, RepoError> - Success indicator or an error
§Errors

This function will return an error if:

  • The specified remote does not exist
  • None of the provided SSH keys are valid or accessible
  • Authentication fails with all provided SSH keys
  • Network connectivity issues prevent pushing
  • The remote repository rejects the push
  • The current branch has no commits to push
  • File system permissions prevent accessing SSH key files
§Examples
use git::repo::Repo;
use std::path::PathBuf;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let key_paths = vec![
    PathBuf::from("/custom/path/to/id_ed25519"),
    PathBuf::from("/custom/path/to/id_rsa"),
];
repo.push_with_ssh_config("origin", Some(true), key_paths).expect("Failed to push");
Source

pub fn get_diverged_commit(&self, git_ref: &str) -> Result<String, RepoError>

Finds the common ancestor (merge base) between HEAD and another reference

§Arguments
  • git_ref - The reference to compare with HEAD (branch name, tag, or commit SHA)
§Returns
  • Result<String, RepoError> - The SHA of the common ancestor commit, or an error
§Errors

This function will return an error if:

  • The provided git reference does not exist or is invalid
  • The reference cannot be resolved to a commit object
  • The HEAD reference cannot be accessed
  • No common ancestor exists between the references
  • The repository graph is corrupted or invalid
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let merge_base = repo.get_diverged_commit("feature-branch")
    .expect("Failed to find common ancestor");
println!("Common ancestor commit: {}", merge_base);
Source

pub fn get_all_files_changed_since_sha_with_status( &self, git_ref: &str, ) -> Result<Vec<GitChangedFile>, RepoError>

Gets all files changed since a specific reference with their status

§Arguments
  • git_ref - The reference to compare with HEAD (branch name, tag, or commit SHA)
§Returns
  • Result<Vec<GitChangedFile>, RepoError> - List of changed files with status, or an error
§Errors

This function will return an error if:

  • The provided git reference does not exist or is invalid
  • The reference cannot be resolved to a commit object
  • The HEAD reference cannot be accessed
  • Commit trees cannot be accessed or are corrupted
  • The diff operation fails due to repository state issues
  • File paths cannot be processed due to encoding issues
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let changed_files = repo.get_all_files_changed_since_sha_with_status("v1.0.0")
    .expect("Failed to get changed files");

for file in changed_files {
    println!("File: {} - {:?}", file.path, file.status);
}
Source

pub fn get_all_files_changed_since_sha( &self, git_ref: &str, ) -> Result<Vec<String>, RepoError>

Gets all files changed since a specific reference

§Arguments
  • git_ref - The reference to compare with HEAD (branch name, tag, or commit SHA)
§Returns
  • Result<Vec<String>, RepoError> - List of changed file paths, or an error
§Errors

This function will return an error if:

  • The provided git reference does not exist or is invalid
  • The reference cannot be resolved to a commit object
  • The HEAD reference cannot be accessed
  • Commit trees cannot be accessed or are corrupted
  • The diff operation fails due to repository state issues
  • File paths cannot be processed due to encoding issues
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let changed_files = repo.get_all_files_changed_since_sha("v1.0.0")
    .expect("Failed to get changed files");

for file in changed_files {
    println!("Changed file: {}", file);
}
Source

pub fn get_all_files_changed_since_branch( &self, packages_paths: &[String], branch: &str, ) -> Result<Vec<String>, RepoError>

Gets all files changed since a specific branch within specified package paths

§Arguments
  • packages_paths - List of package paths to filter by
  • branch - The branch to compare against
§Returns
  • Result<Vec<String>, RepoError> - List of changed file paths within the packages, or an error
§Errors

This function will return an error if:

  • The provided branch reference does not exist or is invalid
  • Package paths cannot be canonicalized or are invalid
  • The underlying get_all_files_changed_since_sha function fails
  • File paths cannot be processed due to encoding issues
  • File system permissions prevent path access
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");
let packages = vec!["packages/pkg1".to_string(), "packages/pkg2".to_string()];
let changed_files = repo.get_all_files_changed_since_branch(&packages, "main")
    .expect("Failed to get changed files");

for file in changed_files {
    println!("Changed file: {}", file);
}
Source

pub fn get_commits_since( &self, since: Option<String>, relative: &Option<String>, ) -> Result<Vec<RepoCommit>, RepoError>

Gets commits made since a specific reference or from the beginning

§Arguments
  • since - Optional reference to start from (branch, tag, or commit SHA)
  • relative - Optional path to filter commits by (only commits touching this path)
§Returns
  • Result<Vec<RepoCommit>, RepoError> - List of commits, or an error
§Errors

This function will return an error if:

  • The provided reference (since) does not exist or is invalid
  • The reference cannot be resolved to a commit object
  • The revision walk cannot be initialized
  • Commit objects cannot be accessed or are corrupted
  • File path filtering fails due to invalid paths
  • The repository state is invalid or corrupted
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");

// Get all commits since v1.0.0
let commits = repo.get_commits_since(
    Some("v1.0.0".to_string()),
    &None
).expect("Failed to get commits");

// Get all commits that touched a specific file
let file_commits = repo.get_commits_since(
    None,
    &Some("src/main.rs".to_string())
).expect("Failed to get commits");

for commit in commits {
    println!("{}: {} ({})",
        commit.hash,
        commit.message,
        commit.author_name
    );
}
Source

pub fn get_commits_between( &self, from_ref: &str, to_ref: &str, relative: &Option<String>, ) -> Result<Vec<RepoCommit>, RepoError>

Gets all commits between two references

This method retrieves commits that exist in to_ref but not in from_ref, effectively getting the commits between the two references.

§Arguments
  • from_ref - Starting reference (commits after this point)
  • to_ref - Ending reference (commits up to this point)
  • relative - Optional path to filter commits that touch specific files
§Returns
  • Result<Vec<RepoCommit>, RepoError> - Vector of commits between the references
§Errors

This function will return an error if:

  • Either reference cannot be resolved
  • Git operations fail
  • Commit objects are corrupted
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");

// Get commits between two tags
let commits = repo.get_commits_between("v1.0.0", "v1.1.0", &None)
    .expect("Failed to get commits");

println!("Found {} commits between versions", commits.len());
Source

pub fn get_remote_or_local_tags( &self, local: Option<bool>, ) -> Result<Vec<RepoTags>, RepoError>

Gets tags from either local repository or remote

§Arguments
  • local - If Some(true), gets local tags; if Some(false) or None, gets remote tags
§Returns
  • Result<Vec<RepoTags>, RepoError> - List of tags, or an error
§Errors

This function will return an error if:

  • Local tags: Tag references cannot be accessed or are corrupted
  • Local tags: Tag objects cannot be found or are invalid
  • Remote tags: The ‘origin’ remote does not exist
  • Remote tags: Authentication fails when connecting to remote
  • Remote tags: Network connectivity issues prevent remote access
  • Remote tags: The remote repository is unreachable
  • Tag names contain invalid UTF-8 sequences
§Examples
use git::repo::Repo;

let repo = Repo::open("./my-repo").expect("Failed to open repository");

// Get local tags
let local_tags = repo.get_remote_or_local_tags(Some(true))
    .expect("Failed to get local tags");

// Get remote tags (default)
let remote_tags = repo.get_remote_or_local_tags(None)
    .expect("Failed to get remote tags");

for tag in local_tags {
    println!("Tag: {} ({})", tag.tag, tag.hash);
}

Trait Implementations§

Source§

impl Debug for Repo

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Repo

§

impl RefUnwindSafe for Repo

§

impl Send for Repo

§

impl !Sync for Repo

§

impl Unpin for Repo

§

impl UnwindSafe for Repo

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.