codetree 0.1.2

AI-native version control system for autonomous agents
Documentation
// Shared helpers for all integration test binaries.
// Each tests/<area>.rs starts with `mod common; use common::*;` and gets the
// original import surface via the `pub use` block below, so test bodies are
// untouched by the split (AGENTS.md Step 9: behavior-neutral refactors).
//
// allows: each test binary uses a different subset of these helpers/re-exports.
#![allow(dead_code)]
#![allow(unused_imports)]

pub use codetree::config::Config;
pub use codetree::error::{Context, Result};
pub use codetree::branch::{BranchManager, BranchStatus};
pub use codetree::vcs::VcsManager;
pub use codetree::resolution::{PendingResolution, ResolutionCache};
pub use codetree::snapshot::SnapshotManager;
pub use codetree::commands;
pub use codetree::commands::BisectAction;
pub use codetree::commands::ConfigAction;
pub use codetree::commands::TagAction;
pub use std::path::PathBuf;
pub use tempfile::TempDir;
pub use std::env;
pub use tokio::sync::Mutex;

/// Helper function to create a temporary test directory with native VCS repo
pub async fn create_test_repo() -> Result<(TempDir, PathBuf)> {
    let temp_dir = TempDir::new()?;
    let repo_path = temp_dir.path().to_path_buf();

    // Initialize native VCS repository with an initial commit on 'main'
    let mgr = VcsManager::init_at(&repo_path)?;
    mgr.commit("Initial commit").await?;

    Ok((temp_dir, repo_path))
}

/// Global lock for tests that change the process working directory.
/// `std::env::set_current_dir()` is process-wide, so tests that use it must run
/// sequentially. This lock is **per test binary**; cargo runs test binaries
/// sequentially, so cross-binary cwd races cannot happen — revisit if test
/// binaries ever run in parallel.
pub static WORKDIR_LOCK: Mutex<()> = Mutex::const_new(());

/// Temporarily change working directory and restore on drop
pub struct DirGuard {
    original: PathBuf,
}

impl DirGuard {
    pub fn new(path: &PathBuf) -> Self {
        let original = env::current_dir().unwrap();
        env::set_current_dir(path).unwrap();
        Self { original }
    }
}

impl Drop for DirGuard {
    fn drop(&mut self) {
        let _ = env::set_current_dir(&self.original);
    }
}

/// Count commits reachable from `start` by walking first parents.
pub fn count_commits_from(repo: &codetree::vcs::Repository, start: &str) -> usize {
    let mut count = 0;
    let mut current = Some(start.to_string());
    while let Some(id) = current {
        count += 1;
        match repo.read_commit(&id) {
            Ok(c) => {
                current = c.parents.first().cloned();
            }
            Err(_) => break,
        }
    }
    count
}