heroforge-core 0.2.2

Pure Rust core library for reading and writing Fossil SCM repositories
Documentation
//! Example: Read from a Heroforge repository using builder pattern
//!
//! Usage: cargo run --example read_repo -- path/to/repo.forge

use heroforge_core::{Repository, Result};
use std::env;

fn main() -> Result<()> {
    // Get repository path from command line
    let args: Vec<String> = env::args().collect();
    let repo_path = args
        .get(1)
        .map(|s| s.as_str())
        .unwrap_or("../code/heroforge.forge");

    println!("Opening repository: {}", repo_path);
    println!("{}", "=".repeat(60));

    let repo = Repository::open(repo_path)?;

    // Show project info
    let project_code = repo.project_code()?;
    let project_name = repo.project_name()?;
    println!(
        "Project: {}",
        project_name.as_deref().unwrap_or("(unnamed)")
    );
    println!("Code: {}", &project_code[..16]);
    println!();

    // List branches using builder pattern
    println!("Branches:");
    let branches = repo.branches().list()?;
    for branch in branches.iter().take(10) {
        println!("  - {}", branch);
    }
    if branches.len() > 10 {
        println!("  ... and {} more", branches.len() - 10);
    }
    println!();

    // Get trunk tip using history builder
    println!("Latest check-in (trunk):");
    let tip = repo.history().trunk_tip()?;
    println!("  Hash: {}", &tip.hash[..16]);
    println!("  Date: {}", tip.timestamp);
    println!("  User: {}", tip.user);
    println!("  Comment: {}", tip.comment.lines().next().unwrap_or(""));
    println!();

    // List files in the root directory using files builder
    println!("Root directory files:");
    let root_files = repo.files().on_trunk().list()?;
    let root_only: Vec<_> = root_files
        .iter()
        .filter(|f| !f.name.contains('/'))
        .collect();
    for file in root_only.iter().take(15) {
        println!("  {}", file.name);
    }
    if root_only.len() > 15 {
        println!("  ... and {} more files", root_only.len() - 15);
    }
    println!();

    // List subdirectories using files builder
    println!("Root subdirectories:");
    let subdirs = repo.files().on_trunk().subdirs("")?;
    for dir in &subdirs {
        println!("  {}/", dir);
    }
    println!();

    // Find all C source files using files builder
    println!("C source files (src/*.c):");
    let c_files = repo.files().on_trunk().find("src/*.c")?;
    for file in c_files.iter().take(10) {
        println!("  {}", file.name);
    }
    if c_files.len() > 10 {
        println!("  ... and {} more", c_files.len() - 10);
    }
    println!();

    // Read README.md if it exists using files builder
    println!("README.md content (first 500 chars):");
    match repo.files().on_trunk().read_string("README.md") {
        Ok(text) => {
            let preview: String = text.chars().take(500).collect();
            println!("{}", preview);
            if text.len() > 500 {
                println!("...");
            }
        }
        Err(e) => {
            println!("  Could not read README.md: {}", e);
        }
    }
    println!();

    // Show recent check-ins using history builder
    println!("Recent check-ins:");
    let recent = repo.history().recent(5)?;
    for ci in &recent {
        println!(
            "  {} {} - {}",
            &ci.hash[..8],
            ci.user,
            ci.comment.lines().next().unwrap_or("")
        );
    }

    Ok(())
}