use std::path::PathBuf;
use serde::Serialize;
use super::pack::{is_edited, write_lock};
use crate::{
Error, RepoPath, RepoRoot, Result, check,
config::PathArg,
pack::{self, Accepted},
};
#[derive(Debug, Clone, Default)]
pub struct PackAcceptOptions {
pub paths: Vec<String>,
pub note: Option<String>,
pub withdraw: bool,
pub cwd: PathBuf,
}
#[derive(Debug, Clone, Default, Serialize)]
pub struct PackAccepted {
pub accepted: Vec<String>,
pub withdrawn: Vec<String>,
}
impl PackAccepted {
pub fn to_text(&self) -> String {
let mut out = String::new();
for p in &self.accepted {
out.push_str(&format!("accepted {p}\n"));
}
for p in &self.withdrawn {
out.push_str(&format!("withdrew the acceptance of {p}\n"));
}
if out.is_empty() {
out.push_str("no acceptance changed\n");
}
out
}
}
pub fn pack_accept(root: &RepoRoot, options: &PackAcceptOptions) -> Result<PackAccepted> {
let note = options.note.as_deref().map(str::trim).unwrap_or_default();
if !options.withdraw && note.is_empty() {
return Err(Error::Pack(
"an acceptance needs a note; pass --note with the reason that the project keeps the edit"
.into(),
));
}
let mut lock = pack::read_lock(root)?;
let mut refused = Vec::new();
let mut targets: Vec<(RepoPath, String, String, Option<Accepted>)> = Vec::new();
for arg in &options.paths {
let Some(dest) = PathArg(arg).resolve_new(root, &options.cwd) else {
refused.push(format!("{arg}: not a path in the repository"));
continue;
};
let Some((name, rel)) = lock
.owner_of(dest.as_str())
.map(|(n, r)| (n.to_string(), r.to_string()))
else {
refused.push(format!("{dest}: no pack in the lock owns the file"));
continue;
};
if targets.iter().any(|(d, ..)| *d == dest) {
continue;
}
if options.withdraw {
targets.push((dest, name, rel, None));
continue;
}
if !is_edited(root, &rel, &lock.packs[&name].files[&rel]) {
refused.push(format!(
"{dest}: the file holds the pack's version, so there is no edit to accept"
));
continue;
}
let path = dest.to_fs(root);
let bytes = match std::fs::read(&path) {
Ok(b) => Some(b),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => None,
Err(e) => {
refused.push(format!("{dest}: the file can't be read: {e}"));
continue;
}
};
if let Some(b) = &bytes
&& !check::conflict_lines(&dest, &String::from_utf8_lossy(b)).is_empty()
{
refused.push(format!(
"{dest}: the file holds a conflict; resolve it, then accept it"
));
continue;
}
let bit = bytes.is_some() && pack::carries_mode(&rel) && pack::file_is_executable(&path);
targets.push((
dest,
name,
rel,
Some(Accepted::of(bytes.as_deref(), bit, note)),
));
}
if !refused.is_empty() {
return Err(Error::Pack(refused.join("; ")));
}
let mut report = PackAccepted::default();
let mut changed = false;
for (dest, name, rel, accepted) in targets {
let file = lock
.packs
.get_mut(&name)
.and_then(|p| p.files.get_mut(&rel))
.ok_or_else(|| Error::Internal(format!("no record of {dest}")))?;
match accepted {
None if file.accepted.take().is_some() => {
report.withdrawn.push(dest.to_string());
changed = true;
}
None => {}
Some(a) => {
changed |= file.accepted.as_ref() != Some(&a);
file.accepted = Some(a);
report.accepted.push(dest.to_string());
}
}
}
if changed {
write_lock(root, &lock)?;
}
Ok(report)
}