use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};
use gitpoulpi_core::domain::rewrite::{
Counts, PlanDigest, Recipe, RefChange, RewritePlan, RewriteWarning, RuleCount, RunId,
};
use gitpoulpi_core::domain::Oid;
use gitpoulpi_core::{Error, Result};
use serde::{Deserialize, Serialize};
pub(crate) const PACK: &str = "objects.pack";
pub(crate) const PLAN: &str = "plan.json";
pub(crate) const COMMIT_MAP: &str = "commit-map";
pub(crate) const REF_MAP: &str = "ref-map";
#[derive(Serialize, Deserialize)]
pub(crate) struct StoredPlan {
pub(crate) digest: PlanDigest,
pub(crate) refs: Vec<RefChange>,
#[serde(default)]
pub(crate) recipe: Option<Recipe>,
#[serde(default)]
pub(crate) counts: Counts,
#[serde(default)]
pub(crate) warnings: Vec<RewriteWarning>,
#[serde(default)]
pub(crate) per_rule: Vec<RuleCount>,
}
fn io(what: &str, path: &Path, e: std::io::Error) -> Error {
Error::Backend(format!("{what} {}: {e}", path.display()))
}
pub(crate) fn valid_run(run: &RunId) -> bool {
!run.0.is_empty()
&& run
.0
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}
pub(crate) fn run_dir(git_dir: &Path, run: &RunId) -> PathBuf {
git_dir.join("gitpoulpi").join("rewrite").join(&run.0)
}
pub(crate) fn write_plan(dir: &Path, plan: &RewritePlan, recipe: &Recipe) -> Result<()> {
let stored = StoredPlan {
digest: plan.digest.clone(),
refs: plan.refs.clone(),
recipe: Some(recipe.saved()),
counts: plan.counts,
warnings: plan.warnings.clone(),
per_rule: plan.per_rule.clone(),
};
let path = dir.join(PLAN);
let text = serde_json::to_vec(&stored).map_err(|e| Error::Backend(e.to_string()))?;
std::fs::write(&path, text).map_err(|e| io("cannot write", &path, e))
}
pub(crate) fn read_plan(dir: &Path) -> Option<StoredPlan> {
serde_json::from_slice(&std::fs::read(dir.join(PLAN)).ok()?).ok()
}
pub(crate) fn write_commit_map(
dir: &Path,
order: &[Oid],
new_id: &HashMap<Oid, Oid>,
dropped: &HashSet<Oid>,
) -> Result<()> {
let mut text = format!("{:<40} new\n", "old");
for old in order {
if dropped.contains(old) {
text.push_str(&format!("{} {}\n", old.to_hex(), "0".repeat(40)));
} else if let Some(new) = new_id.get(old) {
text.push_str(&format!("{} {}\n", old.to_hex(), new.to_hex()));
}
}
let path = dir.join(COMMIT_MAP);
std::fs::write(&path, text).map_err(|e| io("cannot write", &path, e))
}
pub(crate) fn write_ref_map(dir: &Path, refs: &[RefChange]) -> Result<()> {
let mut text = format!("{:<40} {:<40} ref\n", "old", "new");
for r in refs {
let new = r.new.map(|n| n.to_hex()).unwrap_or_else(|| "0".repeat(40));
text.push_str(&format!("{} {} {}\n", r.old.to_hex(), new, r.name));
}
let path = dir.join(REF_MAP);
std::fs::write(&path, text).map_err(|e| io("cannot write", &path, e))
}
pub(crate) const MOVED: &str = "moved.json";
pub(crate) fn write_moved(dir: &Path, refs: &[RefChange]) -> Result<()> {
let path = dir.join(MOVED);
let text = serde_json::to_vec(refs).map_err(|e| Error::Backend(e.to_string()))?;
std::fs::write(&path, text).map_err(|e| io("cannot write", &path, e))
}
pub(crate) fn read_moved(dir: &Path) -> Option<Vec<RefChange>> {
serde_json::from_slice(&std::fs::read(dir.join(MOVED)).ok()?).ok()
}
pub(crate) fn resolve_run(git_dir: &Path, run: &RunId) -> Result<RunId> {
if run.0 != "latest" {
return Ok(run.clone());
}
let dir = git_dir.join("gitpoulpi").join("rewrite");
let newest = std::fs::read_dir(&dir)
.into_iter()
.flatten()
.flatten()
.filter_map(|e| {
let modified = std::fs::metadata(e.path().join(MOVED))
.ok()?
.modified()
.ok()?;
Some((modified, e.file_name().to_string_lossy().into_owned()))
})
.max();
match newest {
Some((_, name)) => Ok(RunId(name)),
None => Err(Error::RewriteRefused(
gitpoulpi_core::domain::rewrite::RewriteRefusal::Stale,
"no rewrite was applied here (or its files were swept)".into(),
)),
}
}
pub(crate) fn drop_temporaries(dir: &Path) {
let _ = std::fs::remove_file(dir.join(PACK));
let _ = std::fs::remove_file(dir.join(PLAN));
let _ = std::fs::remove_dir_all(dir.join(super::lfs::DIR));
}
pub(crate) fn remove_run(dir: &Path) -> Result<()> {
match std::fs::remove_dir_all(dir) {
Ok(()) => Ok(()),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
Err(e) => Err(io("cannot remove", dir, e)),
}
}