use std::sync::atomic::{AtomicBool, Ordering};
async fn is_command_available(command: &str) -> bool {
crate::utils::which(command).await.is_some()
}
static GIT_AVAILABLE: AtomicBool = AtomicBool::new(true);
static GIT_AVAILABLE_INITIALIZED: AtomicBool = AtomicBool::new(false);
pub async fn check_git_available() -> bool {
if GIT_AVAILABLE_INITIALIZED.load(Ordering::SeqCst) {
return GIT_AVAILABLE.load(Ordering::SeqCst);
}
let available = is_command_available("git").await;
GIT_AVAILABLE.store(available, Ordering::SeqCst);
GIT_AVAILABLE_INITIALIZED.store(true, Ordering::SeqCst);
available
}
pub fn mark_git_unavailable() {
GIT_AVAILABLE.store(false, Ordering::SeqCst);
GIT_AVAILABLE_INITIALIZED.store(true, Ordering::SeqCst);
}
pub fn clear_git_availability_cache() {
GIT_AVAILABLE.store(true, Ordering::SeqCst);
GIT_AVAILABLE_INITIALIZED.store(false, Ordering::SeqCst);
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_clear_cache() {
clear_git_availability_cache();
assert!(!GIT_AVAILABLE_INITIALIZED.load(Ordering::SeqCst));
}
#[test]
fn test_mark_unavailable() {
mark_git_unavailable();
assert!(!GIT_AVAILABLE.load(Ordering::SeqCst));
assert!(GIT_AVAILABLE_INITIALIZED.load(Ordering::SeqCst));
clear_git_availability_cache();
}
}