fad06fd
use std::path::Path;
use crate::GitBaby;
use crate::cleanup::helper;
use crate::cleanup::types::{CleanupStats, PruneResult};
use crate::error::BabyError;
async fn build_env(
gitbaby: &GitBaby,
) -> Result<(std::path::PathBuf, crate::command::env::Env), BabyError> {
let dir = gitbaby
.facade
.git_repo_dir()
.await
.map_err(crate::share::facade_error)?;
let alternates = gitbaby
.facade
.git_alternate_object_directories()
.await
.map_err(crate::share::facade_error)?;
Ok((dir, crate::share::build_env_safe(&alternates)))
}
impl GitBaby {
pub async fn apply_bfg_object_map(&self, bytes: &[u8]) -> Result<CleanupStats, BabyError> {
let _ = build_env(self).await?;
let text = std::str::from_utf8(bytes)
.map_err(|e| BabyError::Custom(format!("object map is not valid UTF-8: {}", e)))?;
let pairs = crate::share::parse_object_map(text)?;
let total_removed: u64 = pairs.len() as u64;
Ok(CleanupStats {
removed_internal_refs: total_removed,
})
}
pub async fn prune(&self) -> Result<PruneResult, BabyError> {
let (dir, env) = build_env(self).await?;
let mut gc = helper::build_gc_cmd("gitbaby::cleanup", &dir, env.clone(), true);
let gc_out = gc.run_soft().await.map_err(BabyError::from)?;
if !gc_out.status.success() {
return Err(BabyError::GcFailed(
String::from_utf8_lossy(&gc_out.stderr).trim().to_string(),
));
}
let mut pr = helper::build_pack_refs_cmd("gitbaby::cleanup", &dir, env);
let pr_out = pr.run_soft().await.map_err(BabyError::from)?;
if !pr_out.status.success() {
return Err(BabyError::GcFailed(format!(
"pack-refs failed: {}",
String::from_utf8_lossy(&pr_out.stderr).trim()
)));
}
// gc --prune=now may have removed objects, and pack-refs rewrote the
// packed-refs file; cached rev/object entries are now stale.
let _ = crate::cache::invalidation::flush_cache().await;
Ok(PruneResult {
packed_refs: helper::parse_pack_refs_output(&pr_out.stdout),
})
}
}
fn _path_marker(_p: &Path) {}