use crate::GitError;
use crate::hook::sync::HookMetaDataSync;
use std::process::Command;

impl HookMetaDataSync {
    pub async fn run_gc(&self) -> Result<(), GitError> {
        let storage_path = self.repo.storage_path.clone();

        tokio::task::spawn_blocking(move || {
            let status = Command::new("git")
                .arg("-C")
                .arg(&storage_path)
                .arg("gc")
                .arg("--auto")
                .arg("--quiet")
                .status()
                .map_err(|e| GitError::IoError(format!("git gc failed: {}", e)))?;

            if !status.success() {
                // git gc --auto exits non-zero when there's nothing to collect,
                // or when another gc is already running — both are benign.
                tracing::warn!(code = ?status.code(), "git gc exited with non-zero status");
            }

            Ok::<(), GitError>(())
        })
        .await
        .map_err(|e| GitError::Internal(format!("spawn_blocking join error: {}", e)))?
    }
}
