sublime_git_tools
A high-level Rust interface to Git operations with robust error handling, built on libgit2.
Overview
sublime_git_tools provides a user-friendly API for working with Git repositories. It wraps the
powerful but complex libgit2 library to offer a more ergonomic interface for common Git operations.
This crate is designed for Rust applications that need to:
- Create, clone, or manipulate Git repositories
- Manage branches, commits, and tags
- Track file changes between commits or branches
- Push/pull with remote repositories
- Get detailed commit histories
- Detect changes in specific parts of a repository
- Handle SSH authentication for remote operations
- Perform advanced Git operations like merging and status checking
Table of Contents
- Repository Management
- Configuration
- Branch Operations
- Commit Operations
- Tag Operations
- File Status and Change Detection
- Remote Operations
- Advanced Git Operations
- Error Handling
- Cross-Platform Support
API Reference
For complete API documentation with detailed method signatures, parameters, return types, and comprehensive examples, see the SPEC.md file.
Repository Management
Creating, Opening, and Cloning Repositories
use Repo;
// Create a new repository
let repo = create?;
println!;
// Open an existing repository
let repo = open?;
// Clone a remote repository
let repo = clone?;
// Get repository path
let repo_path = repo.get_repo_path;
println!;
Configuration
Setting and Viewing Git Configuration
use Repo;
let repo = open?;
// Configure user name and email
repo.config?;
// List all configuration entries
let config_entries = repo.list_config?;
for in config_entries
Branch Operations
Creating and Managing Branches
use Repo;
let repo = open?;
// Create a new branch
repo.create_branch?;
// Check if a branch exists
if repo.branch_exists?
// Checkout a branch
repo.checkout?;
// Get current branch name
let current_branch = repo.get_current_branch?;
println!;
// List all branches
let branches = repo.list_branches?;
for branch in branches
Advanced Branch Information
use Repo;
let repo = open?;
// Get branch from a specific commit
let commit_sha = "abcdef123456";
if let Some = repo.get_branch_from_commit?
// Get all branches containing a commit
let branches = repo.get_branches_containing_commit?;
for branch in branches
// Find merge base between branches
let merge_base = repo.get_merge_base?;
println!;
Commit Operations
Staging and Committing
use Repo;
let repo = open?;
// Add a specific file
repo.add?;
// Add all changes
repo.add_all?;
// Commit staged changes
let commit_id = repo.commit?;
println!;
// Add all changes and commit in one step
let commit_id = repo.commit_changes?;
println!;
Commit Information
use Repo;
let repo = open?;
// Get current commit SHA
let current_sha = repo.get_current_sha?;
println!;
// Get previous commit SHA
let previous_sha = repo.get_previous_sha?;
println!;
// Get commit history since a specific reference
let commits = repo.get_commits_since?;
for commit in commits
// Get commits affecting a specific file
let file_commits = repo.get_commits_since?;
for commit in file_commits
Tag Operations
Creating and Managing Tags
use Repo;
let repo = open?;
// Create an annotated tag
repo.create_tag?;
// Create a lightweight tag
repo.create_tag?;
// Get the last tag
let last_tag = repo.get_last_tag?;
println!;
// Get all local tags
let local_tags = repo.get_remote_or_local_tags?;
for tag in local_tags
// Get all remote tags
let remote_tags = repo.get_remote_or_local_tags?;
for tag in remote_tags
File Status and Change Detection
Repository Status
use ;
let repo = open?;
// Get porcelain status (simple format)
let status_lines = repo.status_porcelain?;
for line in status_lines
// Get detailed status with file information
let detailed_status = repo.get_status_detailed?;
for file in detailed_status
// Get only staged files
let staged_files = repo.get_staged_files?;
for file in staged_files
Change Detection Between References
use Repo;
let repo = open?;
// Get all changed files since a commit/tag with status
let changed_files = repo.get_all_files_changed_since_sha_with_status?;
for file in changed_files
// Get simple list of changed files since a commit/tag
let changed_files = repo.get_all_files_changed_since_sha?;
for file in changed_files
// Get files changed between two references
let files = repo.get_files_changed_between?;
for file in files
// Get changes in specific packages/directories since a branch
let packages = vec!;
let package_changes = repo.get_all_files_changed_since_branch?;
for file in package_changes
Remote Operations
Basic Remote Operations
use Repo;
use PathBuf;
let repo = open?;
// Push to remote (without tags)
let success = repo.push?;
if success
// Push with tags
repo.push?;
// Fetch from remote
repo.fetch?;
// Fetch a specific branch
repo.fetch?;
// Pull from remote (current branch)
let success = repo.pull?;
if success
// Pull a specific branch
repo.pull?;
SSH Authentication
use Repo;
use PathBuf;
let repo = open?;
// Push using SSH key paths (tries multiple keys in order)
let ssh_key_paths = vec!;
let success = repo.push_with_ssh_config?;
if success
Advanced Git Operations
Merging
use ;
let repo = open?;
// Merge a branch
match repo.merge
// Find diverged commit (common ancestor)
let diverged_commit = repo.get_diverged_commit?;
println!;
Error Handling
The crate uses a comprehensive error type (RepoError) that provides detailed information about Git operation failures:
use ;
match open
// Handle merge conflicts
match repo.merge
Available Error Types
The RepoError enum provides specific error variants for different Git operations:
CreateRepoFailure- Repository creation errorsOpenRepoFailure- Repository opening errorsCloneRepoFailure- Repository cloning errorsBranchError/BranchNameError/CheckoutBranchError- Branch operation errorsCommitError/CommitOidError- Commit operation errorsMergeError/MergeConflictError- Merge operation errorsPushError/RemoteError- Remote operation errorsTagError/CreateTagError/LastTagError- Tag operation errorsConfigError/ConfigEntriesError- Configuration errorsStatusError/DiffError- Status and diff errors- And many more for comprehensive error coverage
Data Types
Repository Types
use ;
// RepoCommit - represents a commit
let commit = RepoCommit ;
// RepoTags - represents a tag
let tag = RepoTags ;
// GitChangedFile - represents a file with change information
let changed_file = GitChangedFile ;
// GitFileStatus - file status enumeration
match changed_file.status
Common Workflows
Feature Branch Workflow
use Repo;
let repo = open?;
// 1. Create and switch to feature branch
repo.create_branch?;
repo.checkout?;
// 2. Make changes and commit
repo.add?;
repo.commit?;
// 3. Push feature branch
repo.push?;
// 4. Switch back to main and merge
repo.checkout?;
repo.merge?;
// 5. Tag the release
repo.create_tag?;
repo.push?; // Push with tags
Change Detection Workflow
use Repo;
let repo = open?;
// Check what changed since last release
let last_tag = repo.get_last_tag?;
let changed_files = repo.get_all_files_changed_since_sha_with_status?;
println!;
for file in changed_files
// Get commits for changelog
let commits = repo.get_commits_since?;
for commit in commits
Cross-Platform Support
The crate is designed to work on all major platforms:
- Windows - Full support with vendored OpenSSL and libgit2
- macOS - Full support with vendored dependencies
- Linux - Full support with vendored dependencies
SSH operations are supported across all platforms with the vendored SSH implementation.
Performance Considerations
- The crate uses vendored libgit2 for consistent behavior across platforms
- Repository operations are optimized for common use cases
- Large repositories may benefit from specific Git configuration tuning
- File change detection operations scale with repository size
License
This crate is licensed under the terms specified in the workspace configuration.
Contributing
Please see the workspace-level contribution guidelines for information on how to contribute to this crate.