use std::collections::BTreeSet;
use std::ffi::OsStr;
use std::fmt;
use std::io;
use std::path::Path;
use crumbkit::{Document, Failure, Listing, Row};
use schemars::JsonSchema;
use serde::Serialize;
use starmap::{Name, Source, Spec, Type, Variable};
use crate::cli::DiffArgs;
use crate::codes;
use crate::explain::{Values, bind_text, cache_text, source_text, type_text, unset_text};
use crate::git::{self, Entry, GitError};
use crate::spec::{self, SpecFailure};
#[cfg(test)]
mod tests;
pub(crate) const DIFF: Document<Listing<Change>> = Document::new(
"starmap",
"diff",
1,
"Each variable that was added, changed or removed between a git revision's spec and the spec in the working tree.",
);
#[derive(Debug, PartialEq, Eq, Serialize, JsonSchema)]
pub(crate) struct Change {
change: Kind,
name: String,
field: Option<String>,
was: Option<String>,
now: Option<String>,
}
impl Row for Change {
const COLUMNS: &'static [&'static str] = &["change", "name", "field", "was", "now"];
const UNCUT_COLUMNS: &'static [&'static str] = &["name"];
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
enum Kind {
Added,
Changed,
Removed,
}
#[derive(Debug, Failure)]
pub(crate) enum RevisionFailure {
#[failure(
code = codes::REVISION_NO_REPOSITORY,
hint = "run starmap diff in the app's checkout",
field(dir = dir.as_str()),
)]
NoRepository {
dir: String,
},
#[failure(
code = codes::REVISION_NOT_FOUND,
hint = "run `git log` and pick a revision",
field(revision = revision.as_str()),
)]
NotFound {
revision: String,
},
#[failure(
code = codes::REVISION_UNREADABLE,
hint = "check that git is installed and can read the repository",
field(revision = revision.as_str()),
)]
Unreadable {
revision: String,
error: io::Error,
},
}
impl fmt::Display for RevisionFailure {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoRepository { dir } if dir.is_empty() || dir == "." => {
formatter.write_str("the current directory is not in a git repository")
}
Self::NoRepository { dir } => write!(formatter, "{dir} is not in a git repository"),
Self::NotFound { revision } => {
write!(formatter, "{revision} names no revision in this repository")
}
Self::Unreadable { revision, .. } => write!(formatter, "git cannot read {revision}"),
}
}
}
impl std::error::Error for RevisionFailure {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Unreadable { error, .. } => Some(error),
Self::NoRepository { .. } | Self::NotFound { .. } => None,
}
}
}
pub(crate) fn diff(args: &DiffArgs) -> Result<Listing<Change>, crumbkit::Error> {
let spec::Opened { spec: now, located } = spec::open(&args.file.spec)?;
let path = &located.file;
let file = path.display().to_string();
let revision = args.revision.as_str();
let dir = match path.parent() {
Some(dir) if !dir.as_os_str().is_empty() => dir,
_ => Path::new("."),
};
let Some(name) = path.file_name().and_then(OsStr::to_str) else {
return Err(SpecFailure::Unreadable {
file,
error: io::Error::other("the file name is not UTF-8, so git cannot look it up"),
}
.into());
};
let at = git::read_at(dir, revision, name).map_err(|error| match error {
GitError::NoRepository => RevisionFailure::NoRepository {
dir: dir.display().to_string(),
},
GitError::NotFound => RevisionFailure::NotFound {
revision: revision.to_owned(),
},
GitError::Failed(error) => RevisionFailure::Unreadable {
revision: revision.to_owned(),
error,
},
})?;
let label = format!("{revision}:{file}");
let short: String = at.commit.chars().take(7).collect();
let mut note = format!("Compared {file} with {revision} ({short}).");
let was = match at.entry {
Entry::File(bytes) => Some(spec::parse(label, bytes)?),
Entry::Missing => None,
Entry::TooLarge => return Err(SpecFailure::TooLarge { file: label }.into()),
Entry::NotFile => {
return Err(SpecFailure::Unreadable {
file: label,
error: io::Error::other(
"the revision holds a directory, a symlink or a submodule at this path",
),
}
.into());
}
};
if was.is_none() {
note.push_str(&format!(
" {revision} has no {file}, so every variable counts as added."
));
}
let changes = changes(was.as_ref(), &now);
let count = changes.len();
let noun = if count == 1 { "change" } else { "changes" };
Ok(Listing::new(changes, count).note(note).described(noun))
}
fn changes(was: Option<&Spec>, now: &Spec) -> Vec<Change> {
let before = |name: &Name| was.and_then(|spec| spec.variable(name));
let names: BTreeSet<&Name> = now
.variables()
.map(|(name, _)| name)
.chain(
was.into_iter()
.flat_map(|spec| spec.variables().map(|(name, _)| name)),
)
.collect();
let mut changes = Vec::new();
for name in names {
let text = name.as_str().to_owned();
match (before(name), now.variable(name)) {
(None, Some(added)) => changes.push(Change {
change: Kind::Added,
name: text,
field: None,
was: None,
now: Some(summary(added)),
}),
(Some(removed), None) => changes.push(Change {
change: Kind::Removed,
name: text,
field: None,
was: Some(summary(removed)),
now: None,
}),
(Some(old), Some(new)) => {
let group = group_change(was, now, name);
for (field, was, now) in changed_fields(old, new).into_iter().chain(group) {
changes.push(Change {
change: Kind::Changed,
name: text.clone(),
field: Some(field.to_owned()),
was,
now,
});
}
}
(None, None) => {}
}
}
changes
}
type FieldChange = (&'static str, Option<String>, Option<String>);
fn changed_fields(old: &Variable, new: &Variable) -> Vec<FieldChange> {
let mut changed = Vec::new();
if old.value_type() != new.value_type() {
let (was, now) = (type_text(old.value_type()), type_text(new.value_type()));
let text = |variable: &Variable| Values::of(variable.value_type()).map(|v| v.to_string());
let (was_values, now_values) = (text(old), text(new));
let values_differ = was_values != now_values;
if was != now || !values_differ {
changed.push(("type", Some(was), Some(now)));
}
if values_differ {
changed.push(("values", was_values, now_values));
}
}
if old.unset() != new.unset() {
changed.push(("unset", Some(unset_text(old)), Some(unset_text(new))));
}
if old.show() != new.show() {
changed.push((
"show",
Some(old.show().to_string()),
Some(new.show().to_string()),
));
}
if old.source() != new.source() {
let (was, now) = (source_text(old.source()), source_text(new.source()));
let (was_bind, now_bind) = (bind_text(old.source()), bind_text(new.source()));
let (was_cache, now_cache) = (cache_text(old.source()), cache_text(new.source()));
let bind_differs = was_bind != now_bind;
let cache_differs = was_cache != now_cache;
if was != now || !(bind_differs || cache_differs) {
changed.push(("source", Some(was), Some(now)));
}
if bind_differs {
changed.push(("bind", was_bind, now_bind));
}
if cache_differs {
let word = |cache: Option<&str>| Some(cache.unwrap_or("allowed").to_owned());
changed.push(("cache", word(was_cache), word(now_cache)));
}
}
if old.purpose() != new.purpose() {
changed.push((
"purpose",
Some(old.purpose().to_owned()),
Some(new.purpose().to_owned()),
));
}
if old.effect() != new.effect() {
changed.push((
"effect",
old.effect().map(str::to_owned),
new.effect().map(str::to_owned),
));
}
if old.check() != new.check() {
changed.push((
"check",
old.check().map(str::to_owned),
new.check().map(str::to_owned),
));
}
changed
}
fn group_change(was: Option<&Spec>, now: &Spec, name: &Name) -> Option<FieldChange> {
let group = |spec: &Spec| spec.group_of(name).map(|group| group.name().to_string());
let (before, after) = (was.and_then(group), group(now));
(before != after).then_some(("group", before, after))
}
fn summary(variable: &Variable) -> String {
format!(
"{}, {}, {}",
type_word(variable.value_type()),
variable.show(),
source_word(variable.source())
)
}
fn type_word(ty: &Type) -> &'static str {
match ty {
Type::Path { .. } => "path",
Type::Port => "port",
Type::Url { .. } => "url",
Type::SocketAddress => "ip socket address",
Type::Boolean => "boolean",
Type::Integer(_) => "integer",
Type::Enumeration(_) => "enumeration",
Type::String { .. } => "string",
Type::Bytes(_) => "bytes",
_ => "another type",
}
}
fn source_word(source: &Source) -> &'static str {
match source {
Source::Setting { .. } => "setting",
Source::Secret { .. } => "secret",
Source::Generated { .. } => "generated",
Source::Inherited => "inherited",
Source::Provided(_) => "provided",
_ => "another source",
}
}