basic_usage/
basic_usage.rs

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
12use rustic_git::{Repository, Result};
13use std::env;
14use std::fs;
15
16fn main() -> Result<()> {
17    println!("Rustic Git - Basic Usage Example\n");
18
19    // Use a temporary directory for this example
20    let repo_path = env::temp_dir().join("rustic_git_basic_example");
21
22    // Clean up any previous run
23    if 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.display());
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(repo_path.join("src"))?;
36
37    fs::write(
38        repo_path.join("README.md"),
39        "# My Awesome Project\n\nThis is a demo project for rustic-git!\n",
40    )?;
41
42    fs::write(
43        repo_path.join("src/main.rs"),
44        r#"fn main() {
45    println!("Hello from rustic-git example!");
46}
47"#,
48    )?;
49
50    fs::write(
51        repo_path.join("src/lib.rs"),
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!("   Unstaged files: {}", status.unstaged_files().count());
66        println!("   Untracked files: {}", status.untracked_entries().count());
67
68        // Show untracked files
69        for entry in status.untracked_entries() {
70            println!("      - {}", entry.path.display());
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.entries.len()
95        );
96        for entry in &status_after_staging.entries {
97            println!(
98                "      Index {:?}, Worktree {:?}: {}",
99                entry.index_status,
100                entry.worktree_status,
101                entry.path.display()
102            );
103        }
104    }
105    println!();
106
107    // Create a commit
108    println!("Creating commit...");
109    let hash = repo.commit("Initial commit: Add project structure and basic files")?;
110
111    println!("Commit created successfully!");
112    println!("   Full hash: {}", hash);
113    println!("   Short hash: {}", hash.short());
114    println!();
115
116    // Verify final status
117    println!("Final repository status:");
118    let final_status = repo.status()?;
119    if final_status.is_clean() {
120        println!("   Repository is clean - all changes committed!");
121    } else {
122        println!("   Repository still has uncommitted changes");
123    }
124    println!();
125
126    // Clean up
127    println!("Cleaning up example repository...");
128    fs::remove_dir_all(&repo_path)?;
129    println!("Example completed successfully!");
130
131    Ok(())
132}