use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant, SystemTime};
use super::store::SharedIndex;
pub fn spawn_git_temporal_computation(index: SharedIndex, repo_root: PathBuf) {
if tokio::runtime::Handle::try_current().is_err() {
return;
}
let was_ready = index.git_temporal().state == GitTemporalState::Ready;
if !was_ready {
index.update_git_temporal(GitTemporalIndex {
state: GitTemporalState::Computing,
..GitTemporalIndex::pending()
});
}
tokio::spawn(async move {
let result =
tokio::task::spawn_blocking(move || GitTemporalIndex::compute(&repo_root)).await;
match result {
Ok(temporal) => {
tracing::info!(
files = temporal.files.len(),
commits = temporal.stats.total_commits_analyzed,
duration_ms = temporal.stats.compute_duration.as_millis() as u64,
"git temporal index computed"
);
index.update_git_temporal(temporal);
}
Err(error) => {
tracing::warn!("git temporal computation panicked: {error}");
if !was_ready {
index.update_git_temporal(GitTemporalIndex::unavailable(format!(
"computation panicked: {error}"
)));
}
}
}
});
}
const MAX_COMMITS: u32 = 500;
const WINDOW_DAYS: u32 = 90;
const HALF_LIFE_DAYS: f64 = 14.0;
const CO_CHANGE_CAP_PER_FILE: usize = 5;
const HOTSPOT_CAP: usize = 10;
const COUPLED_PAIRS_CAP: usize = 10;
const MIN_SHARED_COMMITS: u32 = 2;
const MIN_JACCARD: f32 = 0.15;
const CONTRIBUTOR_CAP: usize = 5;
const MEGA_COMMIT_THRESHOLD: usize = 50;
#[derive(Debug, Clone)]
pub struct GitFileHistory {
pub commit_count: u32,
pub churn_score: f32,
pub last_commit: CommitSummary,
pub contributors: Vec<ContributorShare>,
pub co_changes: Vec<CoChangeEntry>,
}
#[derive(Debug, Clone)]
pub struct CommitSummary {
pub hash: String,
pub timestamp: String,
pub author: String,
pub message_head: String,
pub days_ago: f64,
}
#[derive(Debug, Clone)]
pub struct ContributorShare {
pub author: String,
pub commit_count: u32,
pub percentage: f32,
}
#[derive(Debug, Clone)]
pub struct CoChangeEntry {
pub path: String,
pub coupling_score: f32,
pub shared_commits: u32,
}
#[derive(Debug, Clone)]
pub struct GitTemporalStats {
pub total_commits_analyzed: u32,
pub analysis_window_days: u32,
pub hotspots: Vec<(String, f32)>,
pub most_coupled: Vec<(String, String, f32)>,
pub computed_at: SystemTime,
pub compute_duration: Duration,
}
#[derive(Debug, Clone)]
pub struct GitTemporalIndex {
pub files: HashMap<String, GitFileHistory>,
pub stats: GitTemporalStats,
pub state: GitTemporalState,
}
#[derive(Debug, Clone, PartialEq)]
pub enum GitTemporalState {
Pending,
Computing,
Ready,
Unavailable(String),
}
#[derive(Debug)]
struct ParsedCommit {
hash: String,
timestamp: String,
author: String,
message: String,
days_ago: f64,
files: Vec<String>,
}
pub fn churn_bar(score: f32) -> String {
let clamped = score.clamp(0.0, 1.0);
let filled = (clamped * 10.0).round() as usize;
let empty = 10_usize.saturating_sub(filled);
format!("{}{}", "█".repeat(filled), "░".repeat(empty))
}
pub fn churn_label(score: f32) -> &'static str {
if score >= 0.8 {
"critical"
} else if score >= 0.6 {
"hot"
} else if score >= 0.4 {
"warm"
} else if score >= 0.2 {
"cool"
} else {
"frozen"
}
}
pub fn relative_time(days_ago: f64) -> String {
if days_ago < 0.0 {
return "just now".to_string();
}
if days_ago < 1.0 {
return "today".to_string();
}
if days_ago < 7.0 {
return format!("{}d ago", days_ago.round() as u32);
}
if days_ago < 30.0 {
return format!("{}w ago", (days_ago / 7.0).round() as u32);
}
format!("{}mo ago", (days_ago / 30.0).round() as u32)
}
impl GitTemporalIndex {
pub fn pending() -> Self {
Self {
files: HashMap::new(),
stats: GitTemporalStats {
total_commits_analyzed: 0,
analysis_window_days: WINDOW_DAYS,
hotspots: Vec::new(),
most_coupled: Vec::new(),
computed_at: SystemTime::now(),
compute_duration: Duration::ZERO,
},
state: GitTemporalState::Pending,
}
}
pub fn unavailable(reason: String) -> Self {
Self {
state: GitTemporalState::Unavailable(reason),
..Self::pending()
}
}
pub fn compute(repo_root: &Path) -> Self {
let start = Instant::now();
let commits = match load_commits(repo_root) {
Ok(c) => c,
Err(reason) => return Self::unavailable(reason),
};
if commits.is_empty() {
return Self {
files: HashMap::new(),
stats: GitTemporalStats {
total_commits_analyzed: 0,
analysis_window_days: WINDOW_DAYS,
hotspots: Vec::new(),
most_coupled: Vec::new(),
computed_at: SystemTime::now(),
compute_duration: start.elapsed(),
},
state: GitTemporalState::Ready,
};
}
let total_commits = commits.len() as u32;
let decay_lambda = (2.0_f64).ln() / HALF_LIFE_DAYS;
let mut file_commit_indices: HashMap<String, Vec<usize>> = HashMap::new();
let mut file_authors: HashMap<String, HashMap<String, u32>> = HashMap::new();
let mut file_last_commit_idx: HashMap<String, usize> = HashMap::new();
let mut file_raw_churn: HashMap<String, f64> = HashMap::new();
for (idx, commit) in commits.iter().enumerate() {
let weight = (-decay_lambda * commit.days_ago).exp();
let unique_files: HashSet<&String> = commit.files.iter().collect();
for file_path in unique_files {
file_commit_indices
.entry(file_path.clone())
.or_default()
.push(idx);
*file_authors
.entry(file_path.clone())
.or_default()
.entry(commit.author.clone())
.or_insert(0) += 1;
file_last_commit_idx
.entry(file_path.clone())
.and_modify(|existing| {
if commit.days_ago < commits[*existing].days_ago {
*existing = idx;
}
})
.or_insert(idx);
*file_raw_churn.entry(file_path.clone()).or_insert(0.0) += weight;
}
}
let mut churn_entries: Vec<(String, f64)> = file_raw_churn.into_iter().collect();
churn_entries.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
let file_count = churn_entries.len();
let mut normalized_churn: HashMap<String, f32> = HashMap::with_capacity(file_count);
for (rank, (path, _raw)) in churn_entries.iter().enumerate() {
let score = if file_count <= 1 {
if churn_entries[0].1 > 0.0 { 1.0 } else { 0.0 }
} else {
rank as f32 / (file_count - 1) as f32
};
normalized_churn.insert(path.clone(), score);
}
let mut pair_counts: HashMap<(String, String), u32> = HashMap::new();
for commit in &commits {
let mut sorted_files: Vec<&str> = commit.files.iter().map(|s| s.as_str()).collect();
sorted_files.sort_unstable();
sorted_files.dedup();
if sorted_files.len() > MEGA_COMMIT_THRESHOLD {
continue;
}
for i in 0..sorted_files.len() {
for j in (i + 1)..sorted_files.len() {
let key = (sorted_files[i].to_string(), sorted_files[j].to_string());
*pair_counts.entry(key).or_insert(0) += 1;
}
}
}
let mut file_co_changes: HashMap<String, Vec<CoChangeEntry>> = HashMap::new();
for ((file_a, file_b), shared) in &pair_counts {
if *shared < MIN_SHARED_COMMITS {
continue;
}
let count_a = file_commit_indices
.get(file_a)
.map(|v| v.len() as u32)
.unwrap_or(0);
let count_b = file_commit_indices
.get(file_b)
.map(|v| v.len() as u32)
.unwrap_or(0);
let union = count_a + count_b - shared;
if union == 0 {
continue;
}
let jaccard = *shared as f32 / union as f32;
if jaccard < MIN_JACCARD {
continue;
}
file_co_changes
.entry(file_a.clone())
.or_default()
.push(CoChangeEntry {
path: file_b.clone(),
coupling_score: jaccard,
shared_commits: *shared,
});
file_co_changes
.entry(file_b.clone())
.or_default()
.push(CoChangeEntry {
path: file_a.clone(),
coupling_score: jaccard,
shared_commits: *shared,
});
}
for entries in file_co_changes.values_mut() {
entries.sort_by(|a, b| {
b.coupling_score
.partial_cmp(&a.coupling_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
entries.truncate(CO_CHANGE_CAP_PER_FILE);
}
let mut files: HashMap<String, GitFileHistory> = HashMap::with_capacity(file_count);
for (path, _commit_indices) in &file_commit_indices {
let commit_count = _commit_indices.len() as u32;
let churn_score = normalized_churn.get(path).copied().unwrap_or(0.0);
let last_idx = file_last_commit_idx.get(path).copied().unwrap_or(0);
let last = &commits[last_idx];
let last_commit = CommitSummary {
hash: last.hash.clone(),
timestamp: last.timestamp.clone(),
author: last.author.clone(),
message_head: truncate_message(&last.message, 72),
days_ago: last.days_ago,
};
let contributors = file_authors
.get(path)
.map(|authors| {
let total = authors.values().sum::<u32>() as f32;
let mut shares: Vec<ContributorShare> = authors
.iter()
.map(|(author, count)| ContributorShare {
author: author.clone(),
commit_count: *count,
percentage: (*count as f32 / total) * 100.0,
})
.collect();
shares.sort_by(|a, b| {
b.percentage
.partial_cmp(&a.percentage)
.unwrap_or(std::cmp::Ordering::Equal)
});
shares.truncate(CONTRIBUTOR_CAP);
shares
})
.unwrap_or_default();
let co_changes = file_co_changes.remove(path).unwrap_or_default();
files.insert(
path.clone(),
GitFileHistory {
commit_count,
churn_score,
last_commit,
contributors,
co_changes,
},
);
}
let mut hotspots: Vec<(String, f32)> = files
.iter()
.map(|(path, h)| (path.clone(), h.churn_score))
.collect();
hotspots.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
hotspots.truncate(HOTSPOT_CAP);
let mut most_coupled: Vec<(String, String, f32)> = pair_counts
.iter()
.filter_map(|((a, b), shared)| {
if *shared < MIN_SHARED_COMMITS {
return None;
}
let count_a = file_commit_indices
.get(a)
.map(|v| v.len() as u32)
.unwrap_or(0);
let count_b = file_commit_indices
.get(b)
.map(|v| v.len() as u32)
.unwrap_or(0);
let union = count_a + count_b - shared;
if union == 0 {
return None;
}
let jaccard = *shared as f32 / union as f32;
if jaccard < MIN_JACCARD {
return None;
}
Some((a.clone(), b.clone(), jaccard))
})
.collect();
most_coupled.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
most_coupled.truncate(COUPLED_PAIRS_CAP);
Self {
files,
stats: GitTemporalStats {
total_commits_analyzed: total_commits,
analysis_window_days: WINDOW_DAYS,
hotspots,
most_coupled,
computed_at: SystemTime::now(),
compute_duration: start.elapsed(),
},
state: GitTemporalState::Ready,
}
}
}
fn load_commits(repo_root: &Path) -> Result<Vec<ParsedCommit>, String> {
use crate::git::GitRepo;
let repo = GitRepo::open(repo_root)?;
let entries = repo.log_with_stats(MAX_COMMITS as usize, WINDOW_DAYS)?;
let now = SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs() as f64;
Ok(entries
.into_iter()
.map(|e| {
let days_ago = (now - e.unix_timestamp as f64) / 86400.0;
ParsedCommit {
hash: e.hash,
timestamp: e.timestamp,
author: e.author,
message: e.message,
days_ago: days_ago.max(0.0),
files: e.files,
}
})
.collect())
}
fn truncate_message(msg: &str, max_len: usize) -> String {
if msg.chars().count() <= max_len {
msg.to_string()
} else {
let truncated: String = msg.chars().take(max_len.saturating_sub(3)).collect();
format!("{truncated}...")
}
}
#[cfg(test)]
const COMMIT_DELIMITER: &str = "SYMFORGE_GIT_TEMPORAL_DELIM";
#[cfg(test)]
fn parse_git_log(raw: &str, now_unix: u64) -> Vec<ParsedCommit> {
let mut commits: Vec<ParsedCommit> = Vec::new();
let mut current: Option<ParsedCommitBuilder> = None;
for line in raw.lines() {
let line = line.trim_end();
if line == COMMIT_DELIMITER {
if let Some(builder) = current.take()
&& let Some(commit) = builder.build()
{
commits.push(commit);
}
current = Some(ParsedCommitBuilder::new());
continue;
}
let Some(builder) = current.as_mut() else {
continue;
};
if let Some(rest) = line.strip_prefix("H:") {
builder.hash = rest.to_string();
} else if let Some(rest) = line.strip_prefix("U:") {
if let Ok(unix_ts) = rest.parse::<u64>() {
builder.unix_timestamp = Some(unix_ts);
builder.days_ago = (now_unix as f64 - unix_ts as f64) / 86400.0;
}
} else if let Some(rest) = line.strip_prefix("D:") {
builder.timestamp = rest.to_string();
} else if let Some(rest) = line.strip_prefix("A:") {
builder.author = rest.to_string();
} else if let Some(rest) = line.strip_prefix("M:") {
builder.message = rest.to_string();
} else if !line.is_empty()
&& let Some(path) = parse_numstat_line(line)
{
builder.files.push(normalize_git_path(&path));
}
}
if let Some(builder) = current.take()
&& let Some(commit) = builder.build()
{
commits.push(commit);
}
commits
}
#[cfg(test)]
fn parse_numstat_line(line: &str) -> Option<String> {
let mut parts = line.splitn(3, '\t');
let added = parts.next()?;
let _removed = parts.next()?;
let path = parts.next()?;
if added == "-" {
return None;
}
if !added.bytes().all(|b| b.is_ascii_digit()) {
return None;
}
if path.is_empty() {
return None;
}
Some(path.to_string())
}
#[cfg(test)]
fn normalize_git_path(path: &str) -> String {
path.replace('\\', "/")
}
#[cfg(test)]
#[derive(Debug, Default)]
struct ParsedCommitBuilder {
hash: String,
timestamp: String,
author: String,
message: String,
unix_timestamp: Option<u64>,
days_ago: f64,
files: Vec<String>,
}
#[cfg(test)]
impl ParsedCommitBuilder {
fn new() -> Self {
Self::default()
}
fn build(self) -> Option<ParsedCommit> {
if self.hash.is_empty() || self.files.is_empty() {
return None;
}
Some(ParsedCommit {
hash: self.hash,
timestamp: self.timestamp,
author: self.author,
message: self.message,
days_ago: self.days_ago,
files: self.files,
})
}
}
#[cfg(test)]
impl GitTemporalIndex {
fn compute_from_log(raw_log: &str, now_unix: u64) -> Self {
let start = Instant::now();
let commits = parse_git_log(raw_log, now_unix);
Self::compute_from_parsed(commits, start)
}
fn compute_from_parsed(commits: Vec<ParsedCommit>, start: Instant) -> Self {
if commits.is_empty() {
return Self {
files: HashMap::new(),
stats: GitTemporalStats {
total_commits_analyzed: 0,
analysis_window_days: WINDOW_DAYS,
hotspots: Vec::new(),
most_coupled: Vec::new(),
computed_at: SystemTime::now(),
compute_duration: start.elapsed(),
},
state: GitTemporalState::Ready,
};
}
let total_commits = commits.len() as u32;
let decay_lambda = (2.0_f64).ln() / HALF_LIFE_DAYS;
let mut file_commit_indices: HashMap<String, Vec<usize>> = HashMap::new();
let mut file_authors: HashMap<String, HashMap<String, u32>> = HashMap::new();
let mut file_last_commit_idx: HashMap<String, usize> = HashMap::new();
let mut file_raw_churn: HashMap<String, f64> = HashMap::new();
for (idx, commit) in commits.iter().enumerate() {
let weight = (-decay_lambda * commit.days_ago).exp();
for file_path in &commit.files {
file_commit_indices
.entry(file_path.clone())
.or_default()
.push(idx);
*file_authors
.entry(file_path.clone())
.or_default()
.entry(commit.author.clone())
.or_insert(0) += 1;
file_last_commit_idx
.entry(file_path.clone())
.and_modify(|existing| {
if commit.days_ago < commits[*existing].days_ago {
*existing = idx;
}
})
.or_insert(idx);
*file_raw_churn.entry(file_path.clone()).or_insert(0.0) += weight;
}
}
let mut churn_entries: Vec<(String, f64)> = file_raw_churn.into_iter().collect();
churn_entries.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(std::cmp::Ordering::Equal));
let file_count = churn_entries.len();
let mut normalized_churn: HashMap<String, f32> = HashMap::with_capacity(file_count);
for (rank, (path, _)) in churn_entries.iter().enumerate() {
let score = if file_count <= 1 {
if churn_entries[0].1 > 0.0 { 1.0 } else { 0.0 }
} else {
rank as f32 / (file_count - 1) as f32
};
normalized_churn.insert(path.clone(), score);
}
let mut pair_counts: HashMap<(String, String), u32> = HashMap::new();
for commit in &commits {
let mut sorted_files: Vec<&str> = commit.files.iter().map(|s| s.as_str()).collect();
sorted_files.sort_unstable();
sorted_files.dedup();
if sorted_files.len() > MEGA_COMMIT_THRESHOLD {
continue;
}
for i in 0..sorted_files.len() {
for j in (i + 1)..sorted_files.len() {
let key = (sorted_files[i].to_string(), sorted_files[j].to_string());
*pair_counts.entry(key).or_insert(0) += 1;
}
}
}
let mut file_co_changes: HashMap<String, Vec<CoChangeEntry>> = HashMap::new();
for ((file_a, file_b), shared) in &pair_counts {
if *shared < MIN_SHARED_COMMITS {
continue;
}
let count_a = file_commit_indices
.get(file_a)
.map(|v| v.len() as u32)
.unwrap_or(0);
let count_b = file_commit_indices
.get(file_b)
.map(|v| v.len() as u32)
.unwrap_or(0);
let union = count_a + count_b - shared;
if union == 0 {
continue;
}
let jaccard = *shared as f32 / union as f32;
if jaccard < MIN_JACCARD {
continue;
}
file_co_changes
.entry(file_a.clone())
.or_default()
.push(CoChangeEntry {
path: file_b.clone(),
coupling_score: jaccard,
shared_commits: *shared,
});
file_co_changes
.entry(file_b.clone())
.or_default()
.push(CoChangeEntry {
path: file_a.clone(),
coupling_score: jaccard,
shared_commits: *shared,
});
}
for entries in file_co_changes.values_mut() {
entries.sort_by(|a, b| {
b.coupling_score
.partial_cmp(&a.coupling_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
entries.truncate(CO_CHANGE_CAP_PER_FILE);
}
let mut files: HashMap<String, GitFileHistory> = HashMap::with_capacity(file_count);
for (path, commit_indices) in &file_commit_indices {
let commit_count = commit_indices.len() as u32;
let churn_score = normalized_churn.get(path).copied().unwrap_or(0.0);
let last_idx = file_last_commit_idx.get(path).copied().unwrap_or(0);
let last = &commits[last_idx];
let last_commit = CommitSummary {
hash: last.hash.clone(),
timestamp: last.timestamp.clone(),
author: last.author.clone(),
message_head: truncate_message(&last.message, 72),
days_ago: last.days_ago,
};
let contributors = file_authors
.get(path)
.map(|authors| {
let total = authors.values().sum::<u32>() as f32;
let mut shares: Vec<ContributorShare> = authors
.iter()
.map(|(author, count)| ContributorShare {
author: author.clone(),
commit_count: *count,
percentage: (*count as f32 / total) * 100.0,
})
.collect();
shares.sort_by(|a, b| {
b.percentage
.partial_cmp(&a.percentage)
.unwrap_or(std::cmp::Ordering::Equal)
});
shares.truncate(CONTRIBUTOR_CAP);
shares
})
.unwrap_or_default();
let co_changes = file_co_changes.remove(path).unwrap_or_default();
files.insert(
path.clone(),
GitFileHistory {
commit_count,
churn_score,
last_commit,
contributors,
co_changes,
},
);
}
let mut hotspots: Vec<(String, f32)> = files
.iter()
.map(|(p, h)| (p.clone(), h.churn_score))
.collect();
hotspots.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
hotspots.truncate(HOTSPOT_CAP);
let mut most_coupled: Vec<(String, String, f32)> = pair_counts
.iter()
.filter_map(|((a, b), shared)| {
if *shared < MIN_SHARED_COMMITS {
return None;
}
let ca = file_commit_indices
.get(a)
.map(|v| v.len() as u32)
.unwrap_or(0);
let cb = file_commit_indices
.get(b)
.map(|v| v.len() as u32)
.unwrap_or(0);
let union = ca + cb - shared;
if union == 0 {
return None;
}
let j = *shared as f32 / union as f32;
if j < MIN_JACCARD {
return None;
}
Some((a.clone(), b.clone(), j))
})
.collect();
most_coupled.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
most_coupled.truncate(COUPLED_PAIRS_CAP);
Self {
files,
stats: GitTemporalStats {
total_commits_analyzed: total_commits,
analysis_window_days: WINDOW_DAYS,
hotspots,
most_coupled,
computed_at: SystemTime::now(),
compute_duration: start.elapsed(),
},
state: GitTemporalState::Ready,
}
}
}
#[cfg(test)]
mod tests;