wlk 0.1.0

File-centric, event-sourced version control system with implicit branching
Documentation
// WLK branching workflow example
// Run with: cargo run --example branching_workflow

use std::fs;
use tempfile::TempDir;
use wlk::{DeltaOperation, ShadowFile};

fn main() -> anyhow::Result<()> {
    println!("=== WLK Branching Workflow Example ===\n");
    println!("This demonstrates WLK's implicit branching feature.\n");

    let temp_dir = TempDir::new()?;
    let file_path = temp_dir.path().join("story.txt");

    // Initial content
    println!("1. Starting with initial content...");
    let initial_content = "Once upon a time, there was a developer.";
    fs::write(&file_path, initial_content)?;

    let mut shadow = ShadowFile::new("story.txt", initial_content.to_string());
    let shadow_path = temp_dir.path().join(".wlk").join("story.txt.wlk");
    fs::create_dir_all(shadow_path.parent().unwrap())?;

    println!("   Initial: {:?}\n", initial_content);

    // Branch 1: Happy ending
    println!("2. Creating Branch 1: Happy ending...");
    shadow.apply_delta(
        DeltaOperation::Insert {
            index: initial_content.len(),
            value: " They lived happily ever after.".to_string(),
        },
        Some("Happy ending".to_string()),
    )?;
    let happy_id = shadow.current_head.clone();
    let happy_content = shadow.current_content()?;
    println!("   Delta ID: {}", happy_id);
    println!("   Content: {:?}\n", happy_content);

    // Rewind to initial
    println!("3. Rewinding to initial state...");
    let initial_id = shadow.initial_snapshot.id.clone();
    shadow.set_head(&initial_id)?;
    println!("   Back at: d0\n");

    // Branch 2: Adventure ending
    println!("4. Creating Branch 2: Adventure ending...");
    shadow.apply_delta(
        DeltaOperation::Insert {
            index: initial_content.len(),
            value: " They embarked on an epic adventure.".to_string(),
        },
        Some("Adventure ending".to_string()),
    )?;
    let adventure_id = shadow.current_head.clone();
    let adventure_content = shadow.current_content()?;
    println!("   Delta ID: {}", adventure_id);
    println!("   Content: {:?}\n", adventure_content);

    // Rewind again
    println!("5. Rewinding to initial state again...");
    let initial_id = shadow.initial_snapshot.id.clone();
    shadow.set_head(&initial_id)?;

    // Branch 3: Mystery ending
    println!("6. Creating Branch 3: Mystery ending...");
    shadow.apply_delta(
        DeltaOperation::Insert {
            index: initial_content.len(),
            value: " They vanished without a trace.".to_string(),
        },
        Some("Mystery ending".to_string()),
    )?;
    let mystery_id = shadow.current_head.clone();
    let mystery_content = shadow.current_content()?;
    println!("   Delta ID: {}", mystery_id);
    println!("   Content: {:?}\n", mystery_content);

    // Show branch structure
    println!("7. Branch structure:");
    println!("   d0 (initial)");
    println!("   ├─ {} (happy ending)", happy_id);
    println!("   ├─ {} (adventure ending)", adventure_id);
    println!("   └─ {} (mystery ending) ← HEAD\n", mystery_id);

    // Find all branches
    let leaves = shadow.find_leaves();
    println!("8. All branch endpoints:");
    for leaf in &leaves {
        let content = shadow.reconstruct_at(&leaf.id)?;
        println!("   {} - {:?}", leaf.id, leaf.annotation);
        println!("{:?}", content);
    }

    println!("\n=== Key Insight ===");
    println!("In WLK, you don't explicitly create branches.");
    println!("Just rewind to any point and make changes - a new branch is created automatically!");
    println!("Each file can have multiple parallel histories without any complexity.");

    Ok(())
}