use rustic_git::{GitError, Repository, Result};
use std::env;
use std::fs;
fn main() -> Result<()> {
println!("Rustic Git - Repository Operations Example\n");
let base_path = env::temp_dir().join("rustic_git_repo_example");
let regular_repo_path = base_path.join("regular");
let bare_repo_path = base_path.join("bare");
let nonexistent_path = base_path.join("nonexistent");
if base_path.exists() {
fs::remove_dir_all(&base_path).expect("Failed to clean up previous example");
}
fs::create_dir_all(&base_path)?;
println!("=== Repository Initialization ===\n");
println!("Initializing regular repository...");
let regular_repo = Repository::init(®ular_repo_path, false)?;
println!(
"Regular repository created at: {}",
regular_repo_path.display()
);
println!(" Repository path: {:?}", regular_repo.repo_path());
if regular_repo_path.join(".git").exists() {
println!(" .git directory found");
}
println!();
println!("Initializing bare repository...");
let bare_repo = Repository::init(&bare_repo_path, true)?;
println!("Bare repository created at: {}", bare_repo_path.display());
println!(" Repository path: {:?}", bare_repo.repo_path());
if bare_repo_path.join("HEAD").exists() {
println!(" HEAD file found (bare repository structure)");
}
if bare_repo_path.join("objects").exists() {
println!(" objects directory found");
}
println!();
println!("=== Repository Opening ===\n");
println!("Opening existing regular repository...");
match Repository::open(®ular_repo_path) {
Ok(opened_repo) => {
println!("Successfully opened regular repository");
println!(" Repository path: {:?}", opened_repo.repo_path());
let status = opened_repo.status()?;
println!(" Repository status: {} files", status.entries.len());
}
Err(e) => {
println!("Failed to open regular repository: {:?}", e);
}
}
println!();
println!("Opening existing bare repository...");
match Repository::open(&bare_repo_path) {
Ok(opened_bare) => {
println!("Successfully opened bare repository");
println!(" Repository path: {:?}", opened_bare.repo_path());
match opened_bare.status() {
Ok(status) => println!(" Bare repository status: {} files", status.entries.len()),
Err(e) => println!(
" Note: Status check on bare repo failed (expected): {:?}",
e
),
}
}
Err(e) => {
println!("Failed to open bare repository: {:?}", e);
}
}
println!();
println!("=== Error Handling ===\n");
println!("Attempting to open non-existent repository...");
match Repository::open(&nonexistent_path) {
Ok(_repo) => {
println!("Unexpectedly succeeded opening non-existent repo");
}
Err(GitError::CommandFailed(msg)) => {
println!("Expected error caught: CommandFailed");
println!(" Error message: {}", msg);
}
Err(GitError::IoError(msg)) => {
println!("Expected error caught: IoError");
println!(" Error message: {}", msg);
}
}
println!();
let fake_repo_path = base_path.join("fake.txt");
fs::write(&fake_repo_path, "This is not a git repository")?;
println!("Attempting to open regular file as repository...");
match Repository::open(&fake_repo_path) {
Ok(_repo) => {
println!("Unexpectedly succeeded opening regular file as repo");
}
Err(GitError::CommandFailed(msg)) => {
println!("Expected error caught: CommandFailed");
println!(" Error message: {}", msg);
}
Err(GitError::IoError(msg)) => {
println!("Expected error caught: IoError");
println!(" Error message: {}", msg);
}
}
println!();
println!("=== Repository Information ===\n");
println!("Comparing repository types:");
let regular_path = regular_repo.repo_path();
let bare_path = bare_repo.repo_path();
println!(" Regular repo path: {:?}", regular_path);
println!(" Bare repo path: {:?}", bare_path);
if let Ok(entries) = fs::read_dir(regular_path) {
let mut files: Vec<_> = entries.filter_map(|e| e.ok()).collect();
files.sort_by_key(|e| e.file_name());
println!(" Regular repo contents:");
for entry in files {
if let Some(name) = entry.file_name().to_str() {
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
let marker = if is_dir { "[DIR]" } else { "[FILE]" };
println!(" {} {}", marker, name);
}
}
}
if let Ok(entries) = fs::read_dir(bare_path) {
let mut files: Vec<_> = entries.filter_map(|e| e.ok()).collect();
files.sort_by_key(|e| e.file_name());
println!(" Bare repo contents:");
for entry in files {
if let Some(name) = entry.file_name().to_str() {
let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
let marker = if is_dir { "[DIR]" } else { "[FILE]" };
println!(" {} {}", marker, name);
}
}
}
println!();
println!("Cleaning up example repositories...");
fs::remove_dir_all(&base_path)?;
println!("Repository operations example completed!");
Ok(())
}