Skip to content

Commit 4bc5e21

Browse files
authored
Merge pull request #5 from eugener/develop
refactor: replace hardcoded /tmp paths with OS-agnostic temp directories
2 parents 12c52dd + b460d57 commit 4bc5e21

14 files changed

+318
-338
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ cargo run --example error_handling
854854
- **`commit_history.rs`** - Comprehensive commit history & log operations showing all querying APIs, filtering, analysis, and advanced LogOptions usage
855855
- **`error_handling.rs`** - Comprehensive error handling patterns showing GitError variants, recovery strategies, and best practices
856856

857-
All examples use temporary directories in `/tmp/` and include automatic cleanup for safe execution.
857+
All examples use OS-appropriate temporary directories and include automatic cleanup for safe execution.
858858

859859
## Testing
860860

@@ -864,7 +864,7 @@ Run the test suite:
864864
cargo test
865865
```
866866

867-
All tests create temporary repositories in `/tmp/` and clean up after themselves.
867+
All tests create temporary repositories in OS-appropriate temporary directories and clean up after themselves.
868868

869869
## Contributing
870870

examples/basic_usage.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,45 @@
1010
//! Run with: cargo run --example basic_usage
1111
1212
use rustic_git::{Repository, Result};
13+
use std::env;
1314
use std::fs;
14-
use std::path::Path;
1515

1616
fn main() -> Result<()> {
1717
println!("Rustic Git - Basic Usage Example\n");
1818

1919
// Use a temporary directory for this example
20-
let repo_path = "/tmp/rustic_git_basic_example";
20+
let repo_path = env::temp_dir().join("rustic_git_basic_example");
2121

2222
// 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");
23+
if repo_path.exists() {
24+
fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
2525
}
2626

27-
println!("Initializing new repository at: {}", repo_path);
27+
println!("Initializing new repository at: {}", repo_path.display());
2828

2929
// Initialize a new repository
30-
let repo = Repository::init(repo_path, false)?;
30+
let repo = Repository::init(&repo_path, false)?;
3131
println!("Repository initialized successfully\n");
3232

3333
// Create some example files
3434
println!("Creating example files...");
35-
fs::create_dir_all(format!("{}/src", repo_path))?;
35+
fs::create_dir_all(repo_path.join("src"))?;
3636

3737
fs::write(
38-
format!("{}/README.md", repo_path),
38+
repo_path.join("README.md"),
3939
"# My Awesome Project\n\nThis is a demo project for rustic-git!\n",
4040
)?;
4141

4242
fs::write(
43-
format!("{}/src/main.rs", repo_path),
43+
repo_path.join("src/main.rs"),
4444
r#"fn main() {
4545
println!("Hello from rustic-git example!");
4646
}
4747
"#,
4848
)?;
4949

5050
fs::write(
51-
format!("{}/src/lib.rs", repo_path),
51+
repo_path.join("src/lib.rs"),
5252
"// Library code goes here\npub fn hello() -> &'static str {\n \"Hello, World!\"\n}\n",
5353
)?;
5454

@@ -125,7 +125,7 @@ fn main() -> Result<()> {
125125

126126
// Clean up
127127
println!("Cleaning up example repository...");
128-
fs::remove_dir_all(repo_path)?;
128+
fs::remove_dir_all(&repo_path)?;
129129
println!("Example completed successfully!");
130130

131131
Ok(())

examples/branch_operations.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,20 @@
11
use rustic_git::{Repository, Result};
2-
use std::fs;
3-
use std::path::Path;
2+
use std::{env, fs};
43

54
fn main() -> Result<()> {
6-
let test_path = "/tmp/rustic_git_branch_example";
5+
let test_path = env::temp_dir().join("rustic_git_branch_example");
76

87
// Clean up if exists
9-
if Path::new(test_path).exists() {
10-
fs::remove_dir_all(test_path).unwrap();
8+
if test_path.exists() {
9+
fs::remove_dir_all(&test_path).unwrap();
1110
}
1211

1312
// Create a test repository
14-
let repo = Repository::init(test_path, false)?;
15-
println!("Created repository at: {}", test_path);
13+
let repo = Repository::init(&test_path, false)?;
14+
println!("Created repository at: {}", test_path.display());
1615

1716
// Create initial commit so we have a valid HEAD
18-
fs::write(
19-
format!("{}/README.md", test_path),
20-
"# Branch Operations Demo\n",
21-
)
22-
.unwrap();
17+
fs::write(test_path.join("README.md"), "# Branch Operations Demo\n").unwrap();
2318
repo.add(&["README.md"])?;
2419
repo.commit("Initial commit")?;
2520
println!("Created initial commit");
@@ -61,7 +56,7 @@ fn main() -> Result<()> {
6156
println!("Created and checked out: {}", dev_branch.name);
6257

6358
// Make a commit on the new branch
64-
fs::write(format!("{}/feature.txt", test_path), "New feature code\n").unwrap();
59+
fs::write(test_path.join("feature.txt"), "New feature code\n").unwrap();
6560
repo.add(&["feature.txt"])?;
6661
repo.commit("Add new feature")?;
6762
println!("Made commit on develop branch");
@@ -139,7 +134,7 @@ fn main() -> Result<()> {
139134
println!("\nFinal branch count: {}", final_branches.len());
140135

141136
// Clean up
142-
fs::remove_dir_all(test_path).unwrap();
137+
fs::remove_dir_all(&test_path).unwrap();
143138
println!("\nCleaned up test repository");
144139

145140
Ok(())

examples/commit_history.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
use chrono::{Duration, Utc};
22
use rustic_git::{LogOptions, Repository, Result};
3-
use std::fs;
4-
use std::path::Path;
3+
use std::{env, fs};
54

65
fn main() -> Result<()> {
7-
let test_path = "/tmp/rustic_git_commit_history_example";
6+
let test_path = env::temp_dir().join("rustic_git_commit_history_example");
87

98
// Clean up if exists
10-
if Path::new(test_path).exists() {
11-
fs::remove_dir_all(test_path).unwrap();
9+
if test_path.exists() {
10+
fs::remove_dir_all(&test_path).unwrap();
1211
}
1312

1413
// Create a test repository
15-
let repo = Repository::init(test_path, false)?;
16-
println!("Created repository at: {}", test_path);
14+
let repo = Repository::init(&test_path, false)?;
15+
println!("Created repository at: {}", test_path.display());
1716

1817
// Create several commits to build history
1918
println!("\n=== Building Commit History ===");
2019

2120
// First commit
2221
fs::write(
23-
format!("{}/README.md", test_path),
22+
test_path.join("README.md"),
2423
"# Commit History Demo\n\nA demonstration of rustic-git log functionality.",
2524
)
2625
.unwrap();
@@ -29,9 +28,9 @@ fn main() -> Result<()> {
2928
println!("Created commit 1: {} - Initial commit", commit1.short());
3029

3130
// Second commit
32-
fs::create_dir_all(format!("{}/src", test_path)).unwrap();
31+
fs::create_dir_all(test_path.join("src")).unwrap();
3332
fs::write(
34-
format!("{}/src/main.rs", test_path),
33+
test_path.join("src/main.rs"),
3534
"fn main() {\n println!(\"Hello, world!\");\n}",
3635
)
3736
.unwrap();
@@ -41,7 +40,7 @@ fn main() -> Result<()> {
4140

4241
// Third commit
4342
fs::write(
44-
format!("{}/src/lib.rs", test_path),
43+
test_path.join("src/lib.rs"),
4544
"pub fn greet(name: &str) -> String {\n format!(\"Hello, {}!\", name)\n}",
4645
)
4746
.unwrap();
@@ -51,7 +50,7 @@ fn main() -> Result<()> {
5150

5251
// Fourth commit
5352
fs::write(
54-
format!("{}/Cargo.toml", test_path),
53+
test_path.join("Cargo.toml"),
5554
"[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"",
5655
)
5756
.unwrap();
@@ -61,7 +60,7 @@ fn main() -> Result<()> {
6160

6261
// Fifth commit - bug fix
6362
fs::write(
64-
format!("{}/src/main.rs", test_path),
63+
test_path.join("src/main.rs"),
6564
"fn main() {\n println!(\"Hello, rustic-git!\");\n}",
6665
)
6766
.unwrap();
@@ -70,7 +69,7 @@ fn main() -> Result<()> {
7069
println!("Created commit 5: {} - Fix greeting", commit5.short());
7170

7271
// Sixth commit - documentation
73-
fs::write(format!("{}/README.md", test_path), "# Commit History Demo\n\nA demonstration of rustic-git log functionality.\n\n## Features\n\n- Greeting functionality\n- Command line interface\n").unwrap();
72+
fs::write(test_path.join("README.md"), "# Commit History Demo\n\nA demonstration of rustic-git log functionality.\n\n## Features\n\n- Greeting functionality\n- Command line interface\n").unwrap();
7473
repo.add(&["README.md"])?;
7574
let commit6 = repo.commit("Update README with features section")?;
7675
println!("Created commit 6: {} - Update README", commit6.short());
@@ -292,7 +291,7 @@ fn main() -> Result<()> {
292291
println!("\n=== Summary ===");
293292

294293
println!("Commit history demonstration completed!");
295-
println!(" Repository: {}", test_path);
294+
println!(" Repository: {}", test_path.display());
296295
println!(" Total commits analyzed: {}", all_commits.len());
297296
println!(" Hash examples:");
298297
for commit in all_commits.iter().take(3) {
@@ -301,7 +300,7 @@ fn main() -> Result<()> {
301300
}
302301

303302
// Clean up
304-
fs::remove_dir_all(test_path).unwrap();
303+
fs::remove_dir_all(&test_path).unwrap();
305304
println!("\nCleaned up test repository");
306305

307306
Ok(())

examples/commit_workflows.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,45 +10,44 @@
1010
//! Run with: cargo run --example commit_workflows
1111
1212
use rustic_git::{Hash, Repository, Result};
13-
use std::fs;
14-
use std::path::Path;
13+
use std::{env, fs};
1514

1615
fn main() -> Result<()> {
1716
println!("Rustic Git - Commit Workflows Example\n");
1817

19-
let repo_path = "/tmp/rustic_git_commit_example";
18+
let repo_path = env::temp_dir().join("rustic_git_commit_example");
2019

2120
// Clean up any previous run
22-
if Path::new(repo_path).exists() {
23-
fs::remove_dir_all(repo_path).expect("Failed to clean up previous example");
21+
if repo_path.exists() {
22+
fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
2423
}
2524

2625
// Initialize repository
2726
println!("Setting up repository for commit demonstrations...");
28-
let repo = Repository::init(repo_path, false)?;
27+
let repo = Repository::init(&repo_path, false)?;
2928
println!("Repository initialized\n");
3029

3130
println!("=== Basic Commit Operations ===\n");
3231

3332
// Create initial files
3433
println!("Creating initial project files...");
35-
fs::create_dir_all(format!("{}/src", repo_path))?;
34+
fs::create_dir_all(repo_path.join("src"))?;
3635

3736
fs::write(
38-
format!("{}/README.md", repo_path),
37+
repo_path.join("README.md"),
3938
"# Commit Demo Project\n\nThis project demonstrates commit workflows with rustic-git.\n",
4039
)?;
4140

4241
fs::write(
43-
format!("{}/src/main.rs", repo_path),
42+
repo_path.join("src/main.rs"),
4443
r#"fn main() {
4544
println!("Hello, Commit Demo!");
4645
}
4746
"#,
4847
)?;
4948

5049
fs::write(
51-
format!("{}/Cargo.toml", repo_path),
50+
repo_path.join("Cargo.toml"),
5251
r#"[package]
5352
name = "commit-demo"
5453
version = "0.1.0"
@@ -109,10 +108,10 @@ edition = "2021"
109108

110109
// Create more files to commit with custom author
111110
println!("Adding features for custom author commit...");
112-
fs::create_dir_all(format!("{}/tests", repo_path))?;
111+
fs::create_dir_all(repo_path.join("tests"))?;
113112

114113
fs::write(
115-
format!("{}/src/lib.rs", repo_path),
114+
repo_path.join("src/lib.rs"),
116115
r#"//! Commit demo library
117116
118117
pub fn greet(name: &str) -> String {
@@ -132,7 +131,7 @@ mod tests {
132131
)?;
133132

134133
fs::write(
135-
format!("{}/tests/integration_test.rs", repo_path),
134+
repo_path.join("tests/integration_test.rs"),
136135
r#"use commit_demo::greet;
137136
138137
#[test]
@@ -166,7 +165,7 @@ fn test_integration() {
166165
// Commit 3: Update version
167166
println!("Step 1: Update version information...");
168167
fs::write(
169-
format!("{}/Cargo.toml", repo_path),
168+
repo_path.join("Cargo.toml"),
170169
r#"[package]
171170
name = "commit-demo"
172171
version = "0.2.0"
@@ -182,7 +181,7 @@ description = "A demo project for commit workflows"
182181
// Commit 4: Add documentation
183182
println!("Step 2: Add documentation...");
184183
fs::write(
185-
format!("{}/CHANGELOG.md", repo_path),
184+
repo_path.join("CHANGELOG.md"),
186185
r#"# Changelog
187186
188187
## [0.2.0] - 2024-01-01
@@ -210,7 +209,7 @@ description = "A demo project for commit workflows"
210209
// Commit 5: Final polish
211210
println!("Step 3: Final polish...");
212211
fs::write(
213-
format!("{}/README.md", repo_path),
212+
repo_path.join("README.md"),
214213
r#"# Commit Demo Project
215214
216215
This project demonstrates commit workflows with rustic-git.
@@ -298,10 +297,7 @@ See CHANGELOG.md for version history.
298297
println!("\nTesting commit with empty message:");
299298

300299
// Create a change to commit
301-
fs::write(
302-
format!("{}/temp_for_empty_message.txt", repo_path),
303-
"temp content",
304-
)?;
300+
fs::write(repo_path.join("temp_for_empty_message.txt"), "temp content")?;
305301
repo.add(&["temp_for_empty_message.txt"])?;
306302

307303
match repo.commit("") {
@@ -336,7 +332,7 @@ See CHANGELOG.md for version history.
336332

337333
// Clean up
338334
println!("\nCleaning up example repository...");
339-
fs::remove_dir_all(repo_path)?;
335+
fs::remove_dir_all(&repo_path)?;
340336
println!("Commit workflows example completed!");
341337

342338
Ok(())

0 commit comments

Comments
 (0)