use super::outcome::{AppliedAutofixCapture, AutofixSidecarFiles};
use super::summary::summarize_optional_fix_results;
use std::collections::HashSet;
#[cfg(not(test))]
pub fn changed_file_set(local_path: &str) -> crate::Result<HashSet<String>> {
let uncommitted = crate::git::get_uncommitted_changes(local_path)?;
let mut files = HashSet::new();
files.extend(uncommitted.staged);
files.extend(uncommitted.unstaged);
files.extend(uncommitted.untracked);
Ok(files)
}
#[cfg(test)]
pub fn changed_file_set(local_path: &str) -> crate::Result<HashSet<String>> {
let path = std::path::Path::new(local_path);
if path.exists() {
Ok(HashSet::new())
} else {
crate::git::get_uncommitted_changes(local_path).map(|changes| {
let mut files = HashSet::new();
files.extend(changes.staged);
files.extend(changes.unstaged);
files.extend(changes.untracked);
files
})
}
}
pub fn count_newly_changed(before: &HashSet<String>, after: &HashSet<String>) -> usize {
after.difference(before).count()
}
pub fn newly_changed_files(before: &HashSet<String>, after: &HashSet<String>) -> Vec<String> {
let mut changed: Vec<String> = after.difference(before).cloned().collect();
changed.sort();
changed
}
pub fn begin_applied_fix_capture(local_path: &str) -> crate::Result<HashSet<String>> {
changed_file_set(local_path)
}
pub fn finish_applied_fix_capture(
local_path: &str,
before_fix_files: &HashSet<String>,
sidecars: &AutofixSidecarFiles,
) -> crate::Result<AppliedAutofixCapture> {
let after_fix_files = changed_file_set(local_path)?;
let files_modified = count_newly_changed(before_fix_files, &after_fix_files);
let fix_results = sidecars.consume_fix_results();
let fix_summary = summarize_optional_fix_results(&fix_results);
Ok(AppliedAutofixCapture {
files_modified,
fix_results,
fix_summary,
})
}