use std::fs;
use tempfile::TempDir;
use wlk::{DeltaOperation, Repository, ShadowFile};
fn main() -> anyhow::Result<()> {
println!("=== WLK Basic Usage Example ===\n");
let temp_dir = TempDir::new()?;
let work_dir = temp_dir.path();
println!("1. Initializing WLK repository...");
let mut repo = Repository::init(work_dir)?;
println!(" ✓ Repository initialized\n");
println!("2. Creating and tracking a file...");
let file_path = work_dir.join("example.txt");
fs::write(&file_path, "Hello, WLK!")?;
repo.track_file(&file_path)?;
println!(" ✓ File tracked: example.txt\n");
println!("3. Making changes...");
fs::write(&file_path, "Hello, WLK!\nThis is a new line.")?;
let shadow_path = ShadowFile::shadow_path(&file_path)?;
let mut shadow = ShadowFile::load(&shadow_path)?;
let content = fs::read_to_string(&file_path)?;
shadow.apply_delta(
DeltaOperation::Snapshot { content },
Some("Added new line".to_string()),
)?;
shadow.save(&shadow_path)?;
println!(" ✓ Snapshot created\n");
println!("4. Checking repository status...");
let status = repo.get_status()?;
println!(" Files tracked: {}", status.len());
for item in &status {
println!(
" - {:?} ({:?})",
item.path.file_name().unwrap(),
item.status
);
}
println!();
println!("5. Viewing file history...");
let shadow = ShadowFile::load(&shadow_path)?;
let history = shadow.get_history();
println!(" Total deltas: {}", history.len());
for delta in history {
if let Some(annotation) = &delta.annotation {
println!(" - {} ({})", delta.id, annotation);
}
}
println!();
println!("6. Rewinding to previous version...");
let initial_id = shadow.initial_snapshot.id.clone();
let initial_content = shadow.reconstruct_at(&initial_id)?;
println!(" Original content: {:?}", initial_content);
let current_content = shadow.current_content()?;
println!(" Current content: {:?}\n", current_content);
println!("7. Listing all tracked files...");
let tracked = repo.list_tracked_files()?;
println!(" Tracked files: {}", tracked.len());
for file in tracked {
println!(" - {:?}", file.file_name().unwrap());
}
println!("\n=== Example Complete! ===");
println!("WLK provides file-centric version control with implicit branching.");
println!("Each file has its own complete history and can branch independently.");
Ok(())
}