use crate::scan::{Unreadable, decode_owned};
use crate::tokens::{self, lexical};
use std::path::{Path, PathBuf};
pub(crate) struct File {
pub(crate) full: PathBuf,
pub(crate) shown: String,
pub(crate) text: Result<String, Unreadable>,
}
pub(crate) const NOTHING_TRACKED: &str =
"no git-tracked files to check -- `git add` them or name paths";
pub(crate) fn files(
root: &Path,
paths: &[String],
) -> Result<impl Iterator<Item = Result<File, String>>, String> {
let selected = tokens::select(root, paths).map_err(|bad| {
format!("{}: {}", refused_path(root, &bad.path), plain(&bad.reason))
})?;
if paths.is_empty() && selected.is_empty() {
return Err(NOTHING_TRACKED.to_owned());
}
let root = root.to_owned();
Ok(selected.into_iter().map(move |full| read(&root, full)))
}
fn read(root: &Path, full: PathBuf) -> Result<File, String> {
let bytes =
std::fs::read(&full).map_err(|e| format!("{}: {e}", full.display()))?;
Ok(File {
shown: shown_path(root, &full),
text: decode_owned(bytes),
full,
})
}
#[must_use]
pub(crate) fn plain(reason: &str) -> &str {
reason
.rfind(" (os error ")
.and_then(|at| reason.get(..at))
.unwrap_or(reason)
}
pub(crate) fn shown_path(root: &Path, full: &Path) -> String {
let (root, full) = (lexical(root), lexical(full));
full.strip_prefix(&root)
.unwrap_or(&full)
.to_string_lossy()
.into_owned()
}
fn refused_path(root: &Path, bad: &Path) -> String {
let shown = shown_path(root, bad);
if shown.is_empty() {
root.display().to_string()
} else {
shown
}
}
#[cfg(test)]
mod tests {
use super::{NOTHING_TRACKED, files, shown_path};
use std::path::Path;
#[test]
fn a_missing_file_is_named_as_typed() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"));
let asked = [String::from("no-such.md")];
let said = files(root, &asked).err().unwrap_or_default();
assert_eq!(said, "no-such.md: No such file or directory");
}
#[test]
fn a_bare_run_with_nothing_tracked_is_refused() {
let root = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("target")
.join("ctrm-nothing-tracked-fixture");
let made = std::fs::create_dir_all(&root).and_then(|()| {
std::process::Command::new("git")
.args(["init", "-q"])
.current_dir(&root)
.status()
});
if !made.is_ok_and(|status| status.success()) {
return;
}
let refused = files(&root, &[]).err();
assert_eq!(refused.as_deref(), Some(NOTHING_TRACKED));
}
#[test]
fn a_refused_root_is_named() {
let root = std::env::temp_dir().join("ctrm-no-such-root-b78");
let said = files(&root, &[]).err().unwrap_or_default();
let want = format!("{}: is not a directory", root.display());
assert_eq!(said, want);
}
#[test]
fn a_path_is_shown_relative_to_the_root() {
let root = Path::new("/repo");
assert_eq!(shown_path(root, Path::new("/repo/src/a.rs")), "src/a.rs");
assert_eq!(
shown_path(root, Path::new("/elsewhere/a.rs")),
"/elsewhere/a.rs"
);
}
#[test]
fn a_path_is_shown_in_lexical_normal_form() {
let root = Path::new("/repo/./x/..");
let shown = |full: &str| shown_path(root, Path::new(full));
assert_eq!(shown("/repo/sub/../sub/c.md"), "sub/c.md");
assert_eq!(shown("/repo/./a.md"), "a.md");
assert_eq!(shown("/repo/../repo/a.md"), "a.md");
assert_eq!(shown("/elsewhere/../b/a.rs"), "/b/a.rs");
}
}