codetree 0.2.0

AI-native version control system for autonomous agents
Documentation
use crate::error::Result;
use serde::{Deserialize, Serialize};
use std::path::Path;

/// Tracks which commits are hidden (not shown in smartlog but still in object store).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct HiddenCommits {
    /// Set of hidden commit hashes.
    pub hidden: Vec<String>,
}

impl HiddenCommits {
    /// Load from `.codetree/hidden.json`.
    pub fn load(root: &Path) -> Result<Self> {
        let path = root.join(".codetree").join("hidden.json");
        if path.exists() {
            let content = std::fs::read_to_string(&path)?;
            Ok(serde_json::from_str(&content)?)
        } else {
            Ok(Self::default())
        }
    }

    /// Save to `.codetree/hidden.json`.
    pub fn save(&self, root: &Path) -> Result<()> {
        let path = root.join(".codetree").join("hidden.json");
        crate::atomic::write_atomic(&path, serde_json::to_string_pretty(self)?.as_bytes())?;
        Ok(())
    }

    /// Hide a commit.
    pub fn hide(&mut self, hash: &str) {
        if !self.hidden.contains(&hash.to_string()) {
            self.hidden.push(hash.to_string());
        }
    }

    /// Unhide a commit.
    pub fn unhide(&mut self, hash: &str) {
        self.hidden.retain(|h| h != hash);
    }

    /// Check if a commit is hidden.
    pub fn is_hidden(&self, hash: &str) -> bool {
        self.hidden.contains(&hash.to_string())
    }
}