Skip to content

Commit 3498c82

Browse files
committed
Merge branch 'master' into eugener-patch-1
2 parents 96f747e + 660fdef commit 3498c82

File tree

11 files changed

+1694
-5
lines changed

11 files changed

+1694
-5
lines changed

.claude/settings.local.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"permissions": {
33
"allow": [
4-
"Bash(cargo:*)"
4+
"Bash(cargo:*)",
5+
"Bash(sed:*)"
56
],
67
"deny": [],
78
"ask": []

CLAUDE.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
- We are using Rust edition of 2024
22
- Follow the Rust style guide for naming conventions and formatting
33
- Implement best practices for code organization and maintainability
4+
- Do not use emoji while coding
45

56
## Design Choices
67
- **Repository-centric API**: Static lifecycle methods (init, open) return Repository instances, instance methods for git operations
@@ -21,3 +22,17 @@
2122
- Command modules: status.rs, add.rs, commit.rs (in src/commands/)
2223
- Core types: Hash (in src/types.rs)
2324
- Run `cargo build && cargo test` after code changes
25+
- Make sure all examples are running
26+
27+
## Examples
28+
The `examples/` directory contains comprehensive demonstrations of library functionality:
29+
30+
- **basic_usage.rs**: Complete workflow from init to commit - demonstrates fundamental rustic-git usage
31+
- **repository_operations.rs**: Repository lifecycle - init regular/bare repos, open existing repos, error handling
32+
- **status_checking.rs**: GitStatus and FileStatus usage - all status query methods and file state filtering
33+
- **staging_operations.rs**: Staging operations - add(), add_all(), add_update() with before/after comparisons
34+
- **commit_workflows.rs**: Commit operations and Hash type - commit(), commit_with_author(), Hash methods
35+
- **error_handling.rs**: Comprehensive error handling patterns - GitError variants, recovery strategies
36+
37+
Run examples with: `cargo run --example <example_name>`
38+
All examples use temporary directories and include cleanup for safe execution.

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,43 @@ fn main() -> rustic_git::Result<()> {
260260
}
261261
```
262262

263+
## Examples
264+
265+
The `examples/` directory contains comprehensive demonstrations of library functionality:
266+
267+
### Running Examples
268+
269+
```bash
270+
# Complete workflow from init to commit
271+
cargo run --example basic_usage
272+
273+
# Repository lifecycle operations
274+
cargo run --example repository_operations
275+
276+
# Status checking and file state filtering
277+
cargo run --example status_checking
278+
279+
# Staging operations (add, add_all, add_update)
280+
cargo run --example staging_operations
281+
282+
# Commit workflows and Hash type usage
283+
cargo run --example commit_workflows
284+
285+
# Error handling patterns and recovery strategies
286+
cargo run --example error_handling
287+
```
288+
289+
### Example Files
290+
291+
- **`basic_usage.rs`** - Demonstrates the fundamental rustic-git workflow: initialize a repository, create files, check status, stage changes, and create commits
292+
- **`repository_operations.rs`** - Shows repository lifecycle operations including initializing regular and bare repositories, opening existing repos, and handling errors
293+
- **`status_checking.rs`** - Comprehensive demonstration of GitStatus and FileStatus usage with all query methods and filtering capabilities
294+
- **`staging_operations.rs`** - Shows all staging methods (add, add_all, add_update) with before/after status comparisons
295+
- **`commit_workflows.rs`** - Demonstrates commit operations and Hash type methods, including custom authors and hash management
296+
- **`error_handling.rs`** - Comprehensive error handling patterns showing GitError variants, recovery strategies, and best practices
297+
298+
All examples use temporary directories in `/tmp/` and include automatic cleanup for safe execution.
299+
263300
## Testing
264301

265302
Run the test suite:

examples/basic_usage.rs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//! Basic Usage Example
2+
//!
3+
//! This example demonstrates the fundamental workflow of the rustic-git library:
4+
//! - Initialize a new repository
5+
//! - Create some files
6+
//! - Check repository status
7+
//! - Stage files
8+
//! - Create a commit
9+
//!
10+
//! Run with: cargo run --example basic_usage
11+
12+
use rustic_git::{Repository, Result};
13+
use std::fs;
14+
use std::path::Path;
15+
16+
fn main() -> Result<()> {
17+
println!("Rustic Git - Basic Usage Example\n");
18+
19+
// Use a temporary directory for this example
20+
let repo_path = "/tmp/rustic_git_basic_example";
21+
22+
// Clean up any previous run
23+
if Path::new(repo_path).exists() {
24+
fs::remove_dir_all(repo_path).expect("Failed to clean up previous example");
25+
}
26+
27+
println!("Initializing new repository at: {}", repo_path);
28+
29+
// Initialize a new repository
30+
let repo = Repository::init(repo_path, false)?;
31+
println!("Repository initialized successfully\n");
32+
33+
// Create some example files
34+
println!("Creating example files...");
35+
fs::create_dir_all(format!("{}/src", repo_path))?;
36+
37+
fs::write(
38+
format!("{}/README.md", repo_path),
39+
"# My Awesome Project\n\nThis is a demo project for rustic-git!\n",
40+
)?;
41+
42+
fs::write(
43+
format!("{}/src/main.rs", repo_path),
44+
r#"fn main() {
45+
println!("Hello from rustic-git example!");
46+
}
47+
"#,
48+
)?;
49+
50+
fs::write(
51+
format!("{}/src/lib.rs", repo_path),
52+
"// Library code goes here\npub fn hello() -> &'static str {\n \"Hello, World!\"\n}\n",
53+
)?;
54+
55+
println!("Created 3 files: README.md, src/main.rs, src/lib.rs\n");
56+
57+
// Check repository status
58+
println!("Checking repository status...");
59+
let status = repo.status()?;
60+
61+
if status.is_clean() {
62+
println!(" Repository is clean (no changes)");
63+
} else {
64+
println!(" Repository has changes:");
65+
println!(" Modified files: {}", status.modified_files().len());
66+
println!(" Untracked files: {}", status.untracked_files().len());
67+
68+
// Show untracked files
69+
for filename in status.untracked_files() {
70+
println!(" - {}", filename);
71+
}
72+
}
73+
println!();
74+
75+
// Stage specific files first
76+
println!("Staging files...");
77+
78+
// Stage README.md first
79+
repo.add(&["README.md"])?;
80+
println!("Staged README.md");
81+
82+
// Stage all remaining files
83+
repo.add_all()?;
84+
println!("Staged all remaining files");
85+
86+
// Check status after staging
87+
let status_after_staging = repo.status()?;
88+
println!("\nStatus after staging:");
89+
if status_after_staging.is_clean() {
90+
println!(" Repository is clean (all changes staged)");
91+
} else {
92+
println!(
93+
" Files staged for commit: {}",
94+
status_after_staging.files.len()
95+
);
96+
for (file_status, filename) in &status_after_staging.files {
97+
println!(" {:?}: {}", file_status, filename);
98+
}
99+
}
100+
println!();
101+
102+
// Create a commit
103+
println!("Creating commit...");
104+
let hash = repo.commit("Initial commit: Add project structure and basic files")?;
105+
106+
println!("Commit created successfully!");
107+
println!(" Full hash: {}", hash);
108+
println!(" Short hash: {}", hash.short());
109+
println!();
110+
111+
// Verify final status
112+
println!("Final repository status:");
113+
let final_status = repo.status()?;
114+
if final_status.is_clean() {
115+
println!(" Repository is clean - all changes committed!");
116+
} else {
117+
println!(" Repository still has uncommitted changes");
118+
}
119+
println!();
120+
121+
// Clean up
122+
println!("Cleaning up example repository...");
123+
fs::remove_dir_all(repo_path)?;
124+
println!("Example completed successfully!");
125+
126+
Ok(())
127+
}

0 commit comments

Comments
 (0)