use rustic_git::{Repository, Result};
use std::env;
use std::fs;
fn main() -> Result<()> {
println!("Rustic Git - Basic Usage Example\n");
let repo_path = env::temp_dir().join("rustic_git_basic_example");
if repo_path.exists() {
fs::remove_dir_all(&repo_path).expect("Failed to clean up previous example");
}
println!("Initializing new repository at: {}", repo_path.display());
let repo = Repository::init(&repo_path, false)?;
println!("Repository initialized successfully\n");
println!("Creating example files...");
fs::create_dir_all(repo_path.join("src"))?;
fs::write(
repo_path.join("README.md"),
"# My Awesome Project\n\nThis is a demo project for rustic-git!\n",
)?;
fs::write(
repo_path.join("src/main.rs"),
r#"fn main() {
println!("Hello from rustic-git example!");
}
"#,
)?;
fs::write(
repo_path.join("src/lib.rs"),
"// Library code goes here\npub fn hello() -> &'static str {\n \"Hello, World!\"\n}\n",
)?;
println!("Created 3 files: README.md, src/main.rs, src/lib.rs\n");
println!("Checking repository status...");
let status = repo.status()?;
if status.is_clean() {
println!(" Repository is clean (no changes)");
} else {
println!(" Repository has changes:");
println!(" Unstaged files: {}", status.unstaged_files().count());
println!(" Untracked files: {}", status.untracked_entries().count());
for entry in status.untracked_entries() {
println!(" - {}", entry.path.display());
}
}
println!();
println!("Staging files...");
repo.add(&["README.md"])?;
println!("Staged README.md");
repo.add_all()?;
println!("Staged all remaining files");
let status_after_staging = repo.status()?;
println!("\nStatus after staging:");
if status_after_staging.is_clean() {
println!(" Repository is clean (all changes staged)");
} else {
println!(
" Files staged for commit: {}",
status_after_staging.entries.len()
);
for entry in &status_after_staging.entries {
println!(
" Index {:?}, Worktree {:?}: {}",
entry.index_status,
entry.worktree_status,
entry.path.display()
);
}
}
println!();
println!("Creating commit...");
let hash = repo.commit("Initial commit: Add project structure and basic files")?;
println!("Commit created successfully!");
println!(" Full hash: {}", hash);
println!(" Short hash: {}", hash.short());
println!();
println!("Final repository status:");
let final_status = repo.status()?;
if final_status.is_clean() {
println!(" Repository is clean - all changes committed!");
} else {
println!(" Repository still has uncommitted changes");
}
println!();
println!("Cleaning up example repository...");
fs::remove_dir_all(&repo_path)?;
println!("Example completed successfully!");
Ok(())
}