cargo-worktree 1.0.0

Deterministic build namespace layer for Cargo
use super::namespace::{NamespaceComponents, NamespaceSource};
use anyhow::Result;
use std::path::PathBuf;

// abbrev=<number>: Instead of using the default number of hexadecimal digits
// (which will vary according to the number of objects in the repository with a default of 7)
// of the abbreviated object name, use <number> digits, or as many digits as needed to form a unique object name.
// Reference: https://git-scm.com/docs/git-rev-list
const ABBREV_LENGTH: usize = 7;

pub fn resolve_target_dir(namespace: &NamespaceComponents) -> Result<Option<PathBuf>> {
    let root = PathBuf::from(&namespace.workspace_root);

    let target_dir = match &namespace.source {
        NamespaceSource::Branch { escaped_branch, .. } => {
            Some(root.join("target").join("git").join(escaped_branch))
        }
        NamespaceSource::Commit { rev } => Some({
            let abbrev = rev[0..ABBREV_LENGTH].to_string();
            root.join("target").join("git").join(abbrev)
        }),
        NamespaceSource::DefaultTarget => None,
    };

    Ok(target_dir)
}