use std::fs;
use std::io::{BufReader, BufWriter, Write};
use std::path::{Path, PathBuf};
use std::process::Command;
use serde::{Serialize, de::DeserializeOwned};
use xxhash_rust::xxh3::xxh3_64;
const CACHE_VERSION: u8 = 1;
pub fn current_commit_sha(workspace_path: &Path) -> String {
Command::new("git")
.args(["rev-parse", "--short=12", "HEAD"])
.current_dir(workspace_path)
.output()
.ok()
.and_then(|o| {
if o.status.success() {
String::from_utf8(o.stdout)
.ok()
.map(|s| s.trim().to_string())
} else {
None
}
})
.unwrap_or_else(|| "no-git".to_string())
}
pub fn query_cache_dir(workspace_path: &Path) -> PathBuf {
workspace_path.join(".unfault").join("cache").join("query")
}
fn cache_path(workspace_path: &Path, query_type: &str, params: &str, commit_sha: &str) -> PathBuf {
let params_hash = xxh3_64(params.as_bytes());
let filename = format!(
"v{}_{query_type}_{params_hash:016x}_{commit_sha}.msgpack",
CACHE_VERSION
);
query_cache_dir(workspace_path).join(filename)
}
pub fn get<T: DeserializeOwned>(
workspace_path: &Path,
query_type: &str,
params: &str,
commit_sha: &str,
) -> Option<T> {
let path = cache_path(workspace_path, query_type, params, commit_sha);
let file = fs::File::open(&path).ok()?;
rmp_serde::from_read(BufReader::new(file)).ok()
}
pub fn set<T: Serialize>(
workspace_path: &Path,
query_type: &str,
params: &str,
commit_sha: &str,
value: &T,
) {
let dir = query_cache_dir(workspace_path);
if fs::create_dir_all(&dir).is_err() {
return;
}
let path = cache_path(workspace_path, query_type, params, commit_sha);
if let Ok(file) = fs::File::create(&path) {
let mut writer = BufWriter::new(file);
if rmp_serde::encode::write(&mut writer, value).is_ok() {
let _ = writer.flush();
}
}
}
pub fn clear(workspace_path: &Path) -> std::io::Result<()> {
let dir = query_cache_dir(workspace_path);
if !dir.exists() {
return Ok(());
}
for entry in fs::read_dir(&dir)? {
let entry = entry?;
if entry.path().extension().map_or(false, |e| e == "msgpack") {
fs::remove_file(entry.path())?;
}
}
Ok(())
}
use unfault_core::types::graph_query::{
CallersContext, FlowContext, GraphContext, WorkspaceContext,
};
pub fn get_callers(
workspace_path: &Path,
function_name: &str,
file_hint: Option<&str>,
commit_sha: &str,
) -> Option<CallersContext> {
let params = format!("{}|{}", function_name, file_hint.unwrap_or(""));
get(workspace_path, "callers", ¶ms, commit_sha)
}
pub fn set_callers(
workspace_path: &Path,
function_name: &str,
file_hint: Option<&str>,
commit_sha: &str,
ctx: &CallersContext,
) {
let params = format!("{}|{}", function_name, file_hint.unwrap_or(""));
set(workspace_path, "callers", ¶ms, commit_sha, ctx);
}
pub fn get_impact(
workspace_path: &Path,
file_path: &str,
max_depth: usize,
commit_sha: &str,
) -> Option<GraphContext> {
let params = format!("{}|{}", file_path, max_depth);
get(workspace_path, "impact", ¶ms, commit_sha)
}
pub fn set_impact(
workspace_path: &Path,
file_path: &str,
max_depth: usize,
commit_sha: &str,
ctx: &GraphContext,
) {
let params = format!("{}|{}", file_path, max_depth);
set(workspace_path, "impact", ¶ms, commit_sha, ctx);
}
pub fn get_function_impact(
workspace_path: &Path,
function: &str,
max_depth: usize,
commit_sha: &str,
) -> Option<FlowContext> {
let params = format!("{}|{}", function, max_depth);
get(workspace_path, "function_impact", ¶ms, commit_sha)
}
pub fn set_function_impact(
workspace_path: &Path,
function: &str,
max_depth: usize,
commit_sha: &str,
ctx: &FlowContext,
) {
let params = format!("{}|{}", function, max_depth);
set(workspace_path, "function_impact", ¶ms, commit_sha, ctx);
}
pub fn get_deps(workspace_path: &Path, target: &str, commit_sha: &str) -> Option<GraphContext> {
get(workspace_path, "deps", target, commit_sha)
}
pub fn set_deps(workspace_path: &Path, target: &str, commit_sha: &str, ctx: &GraphContext) {
set(workspace_path, "deps", target, commit_sha, ctx);
}
pub fn get_library(
workspace_path: &Path,
library_name: &str,
commit_sha: &str,
) -> Option<GraphContext> {
get(workspace_path, "library", library_name, commit_sha)
}
pub fn set_library(
workspace_path: &Path,
library_name: &str,
commit_sha: &str,
ctx: &GraphContext,
) {
set(workspace_path, "library", library_name, commit_sha, ctx);
}
pub fn get_stats(workspace_path: &Path, commit_sha: &str) -> Option<WorkspaceContext> {
get(workspace_path, "stats", "workspace", commit_sha)
}
pub fn set_stats(workspace_path: &Path, commit_sha: &str, ctx: &WorkspaceContext) {
set(workspace_path, "stats", "workspace", commit_sha, ctx);
}
pub fn get_critical(
workspace_path: &Path,
metric: &str,
limit: usize,
commit_sha: &str,
) -> Option<GraphContext> {
let params = format!("{}|{}", metric, limit);
get(workspace_path, "critical", ¶ms, commit_sha)
}
pub fn set_critical(
workspace_path: &Path,
metric: &str,
limit: usize,
commit_sha: &str,
ctx: &GraphContext,
) {
let params = format!("{}|{}", metric, limit);
set(workspace_path, "critical", ¶ms, commit_sha, ctx);
}