use crate::error::Result;
use crate::object::{read_commit, write_commit};
use crate::refs::{list_refs, read_ref, write_ref};
use crate::repo::Repo;
use crate::types::Commit;
#[derive(Debug, Default)]
pub struct TruncateStats {
pub refs_processed: usize,
pub refs_truncated: usize,
}
pub fn truncate_history(repo: &Repo, dry_run: bool) -> Result<TruncateStats> {
let mut stats = TruncateStats::default();
for ref_name in list_refs(repo)? {
stats.refs_processed += 1;
let commit_hash = read_ref(repo, &ref_name)?;
let commit = read_commit(repo, &commit_hash)?;
if commit.parents.is_empty() {
continue;
}
stats.refs_truncated += 1;
if dry_run {
continue;
}
let new_commit = Commit {
tree: commit.tree,
parents: vec![],
message: commit.message,
author: commit.author,
timestamp: commit.timestamp,
metadata: commit.metadata,
};
let new_hash = write_commit(repo, &new_commit)?;
write_ref(repo, &ref_name, &new_hash)?;
}
Ok(stats)
}