git-ops
A crate providing ergonomics around common git2 operations.
Features
| Feature | Description |
|---|---|
| Git Client | Simple interface for git operations |
| Clone Operations | Clone repositories via SSH or HTTPS |
| Branch Checkout | Checkout branches in repositories |
| Pull Operations | Pull changes from remote repositories |
| Commit Operations | Stage all changes and create commits, with optional GPG/SSH signing |
| Push Operations | Push commits to a remote, with optional force and upstream setup |
| SSH Authentication | Built-in SSH credential handling |
| HTTPS Authentication | Credential helper and environment variable support |
| Error Handling | Comprehensive error types for git operations |
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage
Basic Usage
use GitClient;
use Path;
// Create a git client (uses SSH config from environment)
let client = new?;
// Clone a repository (SSH or HTTPS detected automatically)
client.clone?;
// Pull changes
client.pull?;
// Checkout a branch
client.checkout_branch?;
Custom SSH Configuration
use ;
use Path;
// Create SSH config with custom key paths
let ssh_config = from_environment?;
// Create client with custom SSH config
let client = with_ssh_config;
// Use the client
client.pull?;
API Documentation
GitClient
The main client for git operations:
GitClient::new()- Creates a client with SSH config from environment variablesGitClient::with_ssh_config(ssh_config)- Creates a client with custom SSH configurationclone(url, destination)- Clones a repository to the given path (SSH or HTTPS)pull(repo_path)- Pulls updates for an existing repositorycheckout_branch(repo_path, branch_name)- Checkouts a branch in the repositorycommit(repo_path, message)- Stages all changes and creates a commit; signs ifcommit.gpgsign = truein git configpush(repo_path)- Pushes the current branch to its configured remote (defaults toorigin)push_with_options(repo_path, options)- Full control viaPushOptions(remote, branch, force, upstream)extract_repo_name(url)- Extracts the repository name from a Git URLconvert_ssh_to_https(url)- Converts an SSH URL to HTTPSis_valid_git_url(url)- Validates that a string is a recognized Git URLhas_https_credentials()- Checks if HTTPS credentials are available
HTTPS authentication tries git credential helpers first, then falls back to the GITHUB_TOKEN, GH_TOKEN, or GITHUB_ACCESS_TOKEN environment variables.
Other Types
SshConfig: SSH authentication configurationGitError: Error type for git operationsPushOptions: Configuration forpush_with_options(remote override, branch override, force, set_upstream)
Requirements
- Rust 2021 edition or later
- libgit2 (provided via git2-rs)
License
This project is licensed under the MIT License - see the LICENSE file for details.