cargo-port 0.1.4

A TUI for inspecting and managing Rust projects
use std::io;
use std::path::Path;
use std::process::Command;
use std::process::Output;

use super::constants::GIT_BINARY;
use super::constants::GIT_NO_OPTIONAL_LOCKS_ARG;

/// Build a git subprocess rooted at `repo_root` with `--no-optional-locks`
/// set. The flag prevents `git status` (and any other read-only command
/// that touches the index stat cache) from rewriting `.git/index`,
/// which the file watcher would otherwise observe as an external
/// change and re-emit a refresh signal — a self-sustaining CPU and
/// rate-limit loop.
pub(super) fn git_command(repo_root: &Path) -> Command {
    let mut cmd = Command::new(GIT_BINARY);
    cmd.arg(GIT_NO_OPTIONAL_LOCKS_ARG).current_dir(repo_root);
    cmd
}

pub(super) fn git_output_logged<const N: usize>(
    repo_root: &Path,
    op: &str,
    args: [&str; N],
) -> io::Result<Output> {
    let started = std::time::Instant::now();
    let output = git_command(repo_root).args(args).output();
    let status = output
        .as_ref()
        .ok()
        .and_then(|out| out.status.code())
        .map_or_else(|| "signal".to_string(), |code| code.to_string());
    tracing::trace!(
        target: tui_pane::PERF_LOG_TARGET,
        elapsed_ms = tui_pane::perf_log_ms(started.elapsed().as_millis()),
        repo_root = %repo_root.display(),
        op,
        status,
        "git_info_get_call"
    );
    output
}