mod env;
#[cfg(test)]
mod fake;
mod op;
mod reach;
mod reply;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::{Mutex, OnceLock};
use cap_std::fs::Dir;
use super::CliError;
pub(super) use op::Op;
use op::{Answer, Contact, Direction, OpShape, SKIP_CHECKS_ATOM};
use reply::split_nul_paths;
pub(super) use reply::Reply;
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Reads {
Text,
Paths,
}
const TRACE2: [&str; 3] = ["GIT_TRACE2", "GIT_TRACE2_EVENT", "GIT_TRACE2_PERF"];
fn traces_to_a_file(value: &std::ffi::OsStr) -> bool {
Path::new(value).is_absolute()
}
pub(super) fn untrace(command: &mut Command) {
untrace_from(command, std::env::vars_os());
}
fn untrace_from(
command: &mut Command,
env: impl IntoIterator<Item = (std::ffi::OsString, std::ffi::OsString)>,
) {
let mut keeps_a_file = [false; TRACE2.len()];
for (name, value) in env {
let Some(name) = name.to_str() else { continue };
if !name.starts_with("GIT_TRACE") {
continue;
}
let to_a_file = traces_to_a_file(&value);
if let Some(at) = TRACE2.iter().position(|trace2| *trace2 == name) {
keeps_a_file[at] = to_a_file;
}
if !to_a_file {
command.env_remove(name);
}
}
for (at, name) in TRACE2.iter().enumerate() {
if !keeps_a_file[at] {
command.env(name, "0");
}
}
}
fn hides_part_of_the_tree(said: &str) -> bool {
let lower = said.to_ascii_lowercase();
[
"could not open directory",
"cannot opendir",
"opendir(",
"cannot lstat",
]
.iter()
.any(|form| lower.contains(form))
|| said.lines().map(str::trim).any(|line| {
!GIT_PREFIXES.iter().any(|prefix| line.starts_with(prefix))
&& LSTAT_ERRORS.iter().any(|error| {
line.strip_suffix(error)
.and_then(|head| head.strip_suffix(": "))
.is_some_and(|path| !path.is_empty() && !path.contains(": "))
})
})
}
const GIT_PREFIXES: [&str; 6] = ["BUG:", "error:", "fatal:", "hint:", "note:", "warning:"];
const LSTAT_ERRORS: [&str; 7] = [
"Permission denied",
"Input/output error",
"Too many levels of symbolic links",
"File name too long",
"Operation not permitted",
"Stale file handle",
"Stale NFS file handle",
];
fn unreadable_stack(stack: &Path) -> Option<String> {
let list = stack.join("tables.list");
let mut names = String::new();
if let Err(why) = open_regular(&list).and_then(|mut file| {
std::io::Read::read_to_string(&mut file, &mut names)
.map_err(|err| format!("`{}` could not be read: {err}", list.display()))
}) {
return Some(why);
}
names
.lines()
.filter(|name| !name.is_empty())
.find_map(|name| unreadable_table(&stack.join(name)))
}
fn unreadable_table(table: &Path) -> Option<String> {
let mut file = match open_regular(table) {
Ok(file) => file,
Err(why) => return Some(why),
};
let mut magic = [0_u8; 4];
match std::io::Read::read_exact(&mut file, &mut magic) {
Ok(()) if &magic == b"REFT" => None,
Ok(()) => Some(format!("`{}` is not a reftable table", table.display())),
Err(err) => Some(format!("`{}` could not be read: {err}", table.display())),
}
}
fn open_regular(path: &Path) -> Result<std::fs::File, String> {
let shown = path.display();
let mut options = std::fs::OpenOptions::new();
options.read(true);
#[cfg(unix)]
{
use std::os::unix::fs::OpenOptionsExt as _;
options.custom_flags(libc::O_NONBLOCK);
}
let not_regular = || format!("`{shown}` is not a regular file");
let file = options.open(path).map_err(|err| {
if std::fs::metadata(path).is_ok_and(|meta| !meta.is_file()) {
not_regular()
} else {
format!("`{shown}` could not be opened: {err}")
}
})?;
if !file.metadata().is_ok_and(|meta| meta.is_file()) {
return Err(not_regular());
}
Ok(file)
}
fn names_a_tag_directory(line: &str) -> bool {
let Some((quoted, _)) = line
.split_once('\'')
.and_then(|(_, rest)| rest.rsplit_once("': "))
else {
return true;
};
let parts: Vec<&str> = quoted.split(['/', '\\']).collect();
parts.windows(2).any(|pair| pair == ["refs", "tags"])
}
fn skips_a_ref(said: &str) -> bool {
said.to_ascii_lowercase().contains("ignoring broken ref")
}
fn names_a_promisor(stdout: &str) -> bool {
stdout
.split('\0')
.map(|entry| entry.trim_start_matches('\n'))
.filter(|entry| !entry.is_empty())
.any(|entry| match entry.split_once('\n') {
None | Some(("extensions.partialclone", _)) => true,
Some((_, value)) => {
let value = value.to_ascii_lowercase();
matches!(value.as_str(), "true" | "yes" | "on")
|| git_integer(&value).unwrap_or(0) != 0
}
})
}
fn git_integer(value: &str) -> Option<i64> {
let (digits, unit) = match value.char_indices().last()? {
(at, 'k') => (&value[..at], 1 << 10),
(at, 'm') => (&value[..at], 1 << 20),
(at, 'g') => (&value[..at], 1 << 30),
_ => (value, 1),
};
let (negative, digits) = match digits.strip_prefix('-') {
Some(rest) => (true, rest),
None => (false, digits.strip_prefix('+').unwrap_or(digits)),
};
let number = if let Some(hex) = digits.strip_prefix("0x") {
i64::from_str_radix(hex, 16).ok()?
} else if digits.len() > 1 && digits.starts_with('0') {
i64::from_str_radix(&digits[1..], 8).ok()?
} else {
digits.parse().ok()?
};
if negative { -number } else { number }.checked_mul(unit)
}
fn parse_followed(stdout: &[u8]) -> Option<(Followed, &[u8])> {
let end = stdout.iter().position(|byte| *byte == b'\n')?;
let header = std::str::from_utf8(&stdout[..end]).ok()?;
if header.ends_with(" missing") {
return Some((Followed::Absent, &stdout[end + 1..]));
}
let words: Vec<&str> = header.split(' ').collect();
let size: usize = words.last()?.parse().ok()?;
let body = stdout.get(end + 1..end + 1 + size)?;
let rest = stdout.get(end + 1 + size..)?.strip_prefix(b"\n")?;
let followed = match words.as_slice() {
[_, "blob", _] => Followed::File(body.to_vec()),
["dangling", _] => Followed::Dangling,
["loop", _] => Followed::Unfollowed("a symlink loop"),
["notdir", _] => Followed::Unfollowed("a path through a file"),
["symlink", _] => Followed::Unfollowed(
"a symlink git will not follow (absolute, or out of the repository)",
),
[_, _, _] => Followed::Unfollowed("not a file"),
_ => return None,
};
Some((followed, rest))
}
#[cfg(test)]
pub(super) enum Scripted {
Says(Reply),
NoAnswer,
}
#[cfg(test)]
impl Scripted {
fn into_outcome(self) -> Result<Reply, env::RemoteFailure> {
match self {
Self::Says(reply) => Ok(reply),
Self::NoAnswer => Err(env::RemoteFailure::Deadline {
limit: std::time::Duration::from_secs(2),
reach: oakum::discover::child::Reach::Killed(Vec::new()),
}),
}
}
}
enum Runner {
Child,
#[cfg(test)]
Fake(fake::Fake),
}
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(super) struct Commit(String);
#[derive(Debug)]
pub(super) enum BlobKind {
Absent,
Other,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct TreeFile {
pub(super) mode: String,
pub(super) object: String,
pub(super) path: String,
pub(super) exact: bool,
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Followed {
File(Vec<u8>),
Absent,
Dangling,
Unfollowed(&'static str),
}
impl Commit {
pub(super) fn as_str(&self) -> &str {
&self.0
}
pub(super) fn from_advertised(sha: &str) -> Result<Self, CliError> {
if sha.is_empty() {
return Err(CliError::unverified(
"unverified: ls-remote advertised an empty commit id",
));
}
Ok(Self(sha.to_owned()))
}
pub(super) fn from_tag_listing(sha: &str) -> Result<Self, CliError> {
if sha.is_empty() {
return Err(CliError::unverified(
"unverified: the tag listing named an empty commit id",
));
}
Ok(Self(sha.to_owned()))
}
pub(super) fn from_log(sha: &str) -> Result<Self, CliError> {
if sha.is_empty() {
return Err(CliError::unverified(
"unverified: the commit listing named an empty commit id",
));
}
Ok(Self(sha.to_owned()))
}
}
#[derive(Debug)]
pub(super) struct CommitText {
pub(super) message: String,
pub(super) skip_checks: String,
}
pub(super) struct Git {
repo: PathBuf,
held: Option<Dir>,
runner: Runner,
transport: OnceLock<std::sync::Arc<Result<env::BatchSsh, env::TransportUnknown>>>,
reach_by_remote: Mutex<BTreeMap<(String, Direction), reach::Reach>>,
warned: Mutex<BTreeSet<String>>,
shallow: OnceLock<Result<bool, CliError>>,
commit_listings: Mutex<BTreeMap<Option<String>, Result<String, CliError>>>,
default_base: OnceLock<Result<String, CliError>>,
offline: bool,
promisor: OnceLock<bool>,
empty_tree: OnceLock<Commit>,
}
impl Git {
pub(super) fn at(repo: impl Into<PathBuf>) -> Self {
Self::new(repo.into(), Runner::Child, None)
}
pub(super) fn at_repository(
repo: &super::repository::Repository,
) -> Result<Self, Box<dyn std::error::Error>> {
let path = repo.ambient_path()?.to_path_buf();
let held = repo.dir().try_clone().map_err(|err| {
CliError::new(format!("failed to clone the repository capability: {err}"))
})?;
Ok(Self::new(path, Runner::Child, Some(held)))
}
#[cfg(test)]
pub(super) fn answering(replies: impl IntoIterator<Item = (&'static str, Reply)>) -> Self {
Self::scripted(
replies
.into_iter()
.map(|(op, reply)| (op, Scripted::Says(reply))),
)
}
#[cfg(test)]
pub(super) fn scripted(outcomes: impl IntoIterator<Item = (&'static str, Scripted)>) -> Self {
Self::new(
PathBuf::new(),
Runner::Fake(fake::Fake::answering(
Ok(env::BatchSsh::Composed(String::from(
"ssh -o BatchMode=yes",
))),
outcomes
.into_iter()
.map(|(op, scripted)| (op, scripted.into_outcome())),
)),
None,
)
}
#[cfg(test)]
fn answering_over(
transport: Result<env::BatchSsh, env::TransportUnknown>,
replies: impl IntoIterator<Item = (&'static str, Reply)>,
) -> Self {
Self::new(
PathBuf::new(),
Runner::Fake(fake::Fake::answering(
transport,
replies.into_iter().map(|(op, reply)| (op, Ok(reply))),
)),
None,
)
}
fn new(repo: PathBuf, runner: Runner, held: Option<Dir>) -> Self {
Self {
repo,
held,
runner,
transport: OnceLock::new(),
reach_by_remote: Mutex::new(BTreeMap::new()),
warned: Mutex::new(BTreeSet::new()),
shallow: OnceLock::new(),
commit_listings: Mutex::new(BTreeMap::new()),
default_base: OnceLock::new(),
offline: false,
promisor: OnceLock::new(),
empty_tree: OnceLock::new(),
}
}
pub(super) fn offline(mut self) -> Self {
self.offline = true;
self
}
pub(super) fn default_base(
&self,
find: impl FnOnce() -> Result<String, CliError>,
) -> Result<String, CliError> {
self.default_base.get_or_init(find).clone()
}
pub(super) fn commits(&self, from: Option<&str>) -> Result<String, CliError> {
let key = from.map(str::to_owned);
let mut listings = self
.commit_listings
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
listings
.entry(key)
.or_insert_with(|| self.text(Op::Commits { from }))
.clone()
}
pub(super) fn shallow(
&self,
read: impl FnOnce() -> Result<bool, CliError>,
) -> Result<bool, CliError> {
self.shallow.get_or_init(read).clone()
}
#[cfg(test)]
pub(super) fn asked(&self) -> Vec<String> {
match &self.runner {
Runner::Fake(fake) => fake.asked(),
Runner::Child => panic!("only a scripted Git records what it was asked"),
}
}
#[cfg(test)]
pub(super) fn said(&self) -> Vec<String> {
match &self.runner {
Runner::Fake(fake) => fake.heard(),
Runner::Child => panic!("only a scripted Git records what it said"),
}
}
pub(super) fn blob_kind(&self, commit: &Commit, path: &str) -> Result<BlobKind, CliError> {
let entries = self.paths(Op::TreeEntry { commit, path })?;
Ok(if entries.is_empty() {
BlobKind::Absent
} else {
BlobKind::Other
})
}
pub(super) fn followed_blob(&self, commit: &Commit, path: &str) -> Result<Followed, CliError> {
let mut answers = self.followed_blobs(&[(commit, path)])?;
Ok(answers.swap_remove(0))
}
pub(super) fn followed_blobs(
&self,
queries: &[(&Commit, &str)],
) -> Result<Vec<Followed>, CliError> {
if let Some((commit, path)) = queries.iter().find(|(_, path)| path.contains('\n')) {
return Err(CliError::unanswered(format!(
"unverified: `{}` holds a newline, which git's batch read cannot frame, so it was not read at {}",
path.escape_debug(),
commit.as_str()
)));
}
if queries.is_empty() {
return Ok(Vec::new());
}
let shape = Op::FollowedBlob { queries }.shape();
let reply = self.checked(&shape, Reads::Text)?;
let mut rest = reply.stdout.as_slice();
queries
.iter()
.map(|(commit, path)| {
let (followed, after) = parse_followed(rest)
.ok_or_else(|| shape.fail("answered in a shape oakum cannot read"))?;
rest = after;
if matches!(followed, Followed::Absent | Followed::Dangling) {
self.absent(commit, path)?;
}
Ok(followed)
})
.collect()
}
pub(super) fn unreadable_reftable(&self) -> Result<Option<String>, CliError> {
let shape = Op::RefStore.shape();
let text = self.blob(Op::RefStore)?;
let Some((format, common)) = text.strip_suffix('\n').unwrap_or(&text).split_once('\n')
else {
return Err(shape.fail("answered in a shape oakum cannot read"));
};
if format != "reftable" {
return Ok(None);
}
let git_dir = self.blob(Op::GitDir)?;
let git_dir = git_dir.strip_suffix('\n').unwrap_or(&git_dir);
let mut stacks = vec![Path::new(common).join("reftable")];
if git_dir != common {
stacks.push(Path::new(git_dir).join("reftable"));
}
Ok(stacks.iter().find_map(|stack| unreadable_stack(stack)))
}
pub(super) fn tag_refs_complaint(&self) -> Result<Option<String>, CliError> {
let reply = self.child(&Op::VerifyRefs.shape())?;
if reply.code.is_none() {
return Ok(Some(String::from(
"it was terminated by a signal before it finished",
)));
}
Ok(reply.diagnostic().and_then(|said| {
let mut unopened = said
.lines()
.filter(|line| line.contains("error opening directory"))
.peekable();
let first = (*unopened.peek()?).to_owned();
let continued = said.lines().any(|line| {
!line.trim().is_empty()
&& !GIT_PREFIXES.iter().any(|prefix| line.starts_with(prefix))
});
if continued {
return Some(first);
}
unopened
.find(|line| names_a_tag_directory(line))
.map(str::to_owned)
}))
}
pub(super) fn text(&self, op: Op<'_>) -> Result<String, CliError> {
let shape = op.shape();
let reply = self.checked(&shape, Reads::Text)?;
if shape.spec.lossy() {
return Ok(String::from_utf8_lossy(&reply.stdout).trim().to_owned());
}
String::from_utf8(reply.stdout)
.map(|text| text.trim().to_owned())
.map_err(|_| shape.fail("output is not valid UTF-8"))
}
pub(super) fn line(&self, op: Op<'_>) -> Result<String, CliError> {
let shape = op.shape();
let reply = self.checked(&shape, Reads::Text)?;
let mut text =
String::from_utf8(reply.stdout).map_err(|_| shape.fail("output is not valid UTF-8"))?;
if text.ends_with('\n') {
text.pop();
}
Ok(text)
}
pub(super) fn head(&self) -> Result<Commit, CliError> {
self.text(Op::Head).map(Commit)
}
pub(super) fn tag_commit(&self, tag: &str) -> Result<Option<Commit>, CliError> {
Ok(self.optional_text(Op::LocalTagCommit { tag })?.map(Commit))
}
pub(super) fn merge_base(&self, tip: &str) -> Result<Option<Commit>, CliError> {
Ok(self.optional_text(Op::MergeBase { tip })?.map(Commit))
}
pub(super) fn first_parent_or_empty(&self, commit: &Commit) -> Result<Commit, CliError> {
let parents = self.text(Op::Parents { commit })?;
if let Some(parent) = parents.lines().next() {
return Commit::from_log(parent);
}
if let Some(tree) = self.empty_tree.get() {
return Ok(tree.clone());
}
let tree = Commit(self.text(Op::EmptyTree)?.trim().to_owned());
Ok(self.empty_tree.get_or_init(|| tree).clone())
}
pub(super) fn first_parent_writers(&self, paths: &[&str]) -> Result<Vec<Commit>, CliError> {
Ok(self
.text(Op::FirstParentWriters { paths })?
.lines()
.filter(|line| !line.is_empty())
.map(|line| Commit(line.to_owned()))
.collect())
}
pub(super) fn blob(&self, op: Op<'_>) -> Result<String, CliError> {
let shape = op.shape();
let reply = self.checked(&shape, Reads::Text)?;
String::from_utf8(reply.stdout).map_err(|_| shape.fail("blob is not valid UTF-8"))
}
pub(super) fn blobs(&self, commit: &Commit, paths: &[&str]) -> Result<Vec<String>, CliError> {
Ok(self
.batch_blobs(commit, paths, false)?
.into_iter()
.flatten()
.collect())
}
pub(super) fn root(&self) -> &Path {
&self.repo
}
pub(super) fn tree_files(&self, commit: &Commit) -> Result<Vec<TreeFile>, CliError> {
let shape = Op::TreeFiles { commit }.shape();
let stdout = self.checked(&shape, Reads::Text)?.stdout;
stdout
.split(|byte| *byte == 0)
.filter(|record| !record.is_empty())
.map(|record| {
let exact = std::str::from_utf8(record).is_ok();
let record = String::from_utf8_lossy(record);
let (head, path) = record
.split_once('\t')
.ok_or_else(|| shape.fail("answered in a shape oakum cannot read"))?;
match head.split(' ').collect::<Vec<_>>().as_slice() {
[mode, _, object] => Ok(TreeFile {
mode: (*mode).to_owned(),
object: (*object).to_owned(),
path: path.to_owned(),
exact,
}),
_ => Err(shape.fail("answered in a shape oakum cannot read")),
}
})
.collect()
}
pub(super) fn objects(&self, objects: &[&str]) -> Result<Vec<Option<Vec<u8>>>, CliError> {
if objects.is_empty() {
return Ok(Vec::new());
}
let shape = Op::Objects { objects }.shape();
let stdout = self.checked(&shape, Reads::Text)?.stdout;
let mut rest = stdout.as_slice();
objects
.iter()
.map(|object| {
let cut = || shape.fail(&format!("stopped at {object}"));
let end = rest
.iter()
.position(|byte| *byte == b'\n')
.ok_or_else(cut)?;
let header = std::str::from_utf8(&rest[..end]).map_err(|_| cut())?;
if header == format!("{object} missing") {
rest = &rest[end + 1..];
return Ok(None);
}
let size = match header.split(' ').collect::<Vec<_>>().as_slice() {
[_, _, size] => size.parse::<usize>().map_err(|_| cut())?,
_ => return Err(cut()),
};
let body = rest.get(end + 1..end + 1 + size).ok_or_else(cut)?;
if rest.get(end + 1 + size) != Some(&b'\n') {
return Err(cut());
}
let body = body.to_vec();
rest = &rest[end + 2 + size..];
Ok(Some(body))
})
.collect()
}
pub(super) fn blobs_or_absent(
&self,
commit: &Commit,
paths: &[&str],
) -> Result<Vec<Option<String>>, CliError> {
self.batch_blobs(commit, paths, true)
}
fn absent(&self, commit: &Commit, path: &str) -> Result<(), CliError> {
if !self.offline {
return Ok(());
}
let lacked = |what: &str| {
CliError::unverified(format!(
"unverified: `{path}` at {} reaches {what}, which this repository does not hold, and `check` reads without fetching; a clone without `--filter` lets it read",
commit.as_str()
))
};
let mut walked = path.to_owned();
for _ in 0..40 {
let parts: Vec<&str> = walked.split('/').collect();
let mut through_link = None;
for end in 1..=parts.len() {
let prefix = parts[..end].join("/");
let entries = self
.paths(Op::TreeEntry {
commit,
path: &prefix,
})
.map_err(|_| lacked(&format!("a tree holding `{prefix}`")))?;
let Some(entry) = entries.first() else {
return Ok(());
};
let head = entry
.split_once('\t')
.map_or(entry.as_str(), |(head, _)| head);
let [mode, _, object] = head.split(' ').collect::<Vec<_>>()[..] else {
return Err(lacked(&format!(
"`{prefix}`, listed in a shape oakum cannot read"
)));
};
if mode == "040000" {
continue;
}
if mode == "160000" {
return Ok(());
}
let Some(Some(bytes)) = self.objects(&[object])?.pop() else {
return Err(lacked(&format!("`{prefix}`")));
};
if mode != "120000" {
return Ok(());
}
let parent = prefix.rsplit_once('/').map_or("", |(parent, _)| parent);
let Some(mut target) =
super::base_tree::normalized(parent, &String::from_utf8_lossy(&bytes))
else {
return Ok(());
};
for rest in &parts[end..] {
target = super::base_tree::in_dir(&target, rest);
}
if target.is_empty() {
return Ok(());
}
through_link = Some(target);
break;
}
match through_link {
Some(target) => walked = target,
None => return Ok(()),
}
}
Ok(())
}
fn batch_blobs(
&self,
commit: &Commit,
paths: &[&str],
absent_ok: bool,
) -> Result<Vec<Option<String>>, CliError> {
if paths.is_empty() {
return Ok(Vec::new());
}
let op = Op::Blobs { commit, paths };
let shape = op.shape();
if paths.iter().any(|path| path.contains('\n')) {
return Err(
shape.fail("was asked for a path holding a newline, which stdin cannot frame")
);
}
let stdout = self.checked(&shape, Reads::Text)?.stdout;
let mut rest = stdout.as_slice();
let mut texts = Vec::with_capacity(paths.len());
for path in paths {
let end = rest
.iter()
.position(|byte| *byte == b'\n')
.ok_or_else(|| shape.fail(&format!("stopped before {path}")))?;
let header = std::str::from_utf8(&rest[..end]).unwrap_or_default();
if absent_ok && header == format!("{}:{path} missing", commit.as_str()) {
self.absent(commit, path)?;
texts.push(None);
rest = &rest[end + 1..];
continue;
}
let size = match header.split(' ').collect::<Vec<_>>().as_slice() {
[_, "blob", size] => size.parse::<usize>().ok(),
_ => None,
}
.ok_or_else(|| shape.fail(&format!("{path} is not a file: {header}")))?;
let body = rest
.get(end + 1..end + 1 + size)
.ok_or_else(|| shape.fail(&format!("stopped inside {path}")))?;
if rest.get(end + 1 + size) != Some(&b'\n') {
return Err(shape.fail(&format!("stopped inside {path}")));
}
texts
.push(Some(String::from_utf8(body.to_vec()).map_err(|_| {
shape.fail(&format!("{path} is not valid UTF-8"))
})?));
rest = &rest[end + 2 + size..];
}
Ok(texts)
}
pub(super) fn commit_paths(
&self,
hashes: &[&str],
) -> BTreeMap<String, Result<Vec<String>, CliError>> {
match self.commit_paths_batched(hashes) {
Ok(by_commit) => by_commit
.into_iter()
.map(|(hash, paths)| (hash, Ok(paths)))
.collect(),
Err(_) => hashes
.iter()
.map(|hash| ((*hash).to_owned(), self.paths(Op::CommitPath { hash })))
.collect(),
}
}
fn commit_paths_batched(
&self,
hashes: &[&str],
) -> Result<BTreeMap<String, Vec<String>>, CliError> {
let mut by_commit: BTreeMap<String, Vec<String>> = hashes
.iter()
.map(|hash| ((*hash).to_owned(), Vec::new()))
.collect();
if hashes.is_empty() {
return Ok(by_commit);
}
let op = Op::CommitPaths { hashes };
let shape = op.shape();
let records =
split_nul_paths(&self.checked(&shape, Reads::Paths)?.stdout).ok_or_else(|| {
shape.fail(
"listed a path that is not valid UTF-8; oakum cannot attribute it to a package",
)
})?;
let mut current: Option<String> = None;
let mut first = false;
for record in records {
if let Some(hash) = record.strip_prefix('\u{1}') {
if by_commit.contains_key(hash) {
current = Some(hash.to_owned());
first = true;
continue;
}
}
let path = if first {
record.strip_prefix('\n')
} else {
Some(record.as_str())
};
let (Some(paths), Some(path)) = (
current.as_ref().and_then(|hash| by_commit.get_mut(hash)),
path,
) else {
return Err(shape.fail("listed a path under no commit it was asked about"));
};
paths.push(path.to_owned());
first = false;
}
Ok(by_commit)
}
pub(super) fn paths(&self, op: Op<'_>) -> Result<Vec<String>, CliError> {
let shape = op.shape();
let stdout = self.checked(&shape, Reads::Paths)?.stdout;
split_nul_paths(&stdout).ok_or_else(|| {
shape.fail(
"listed a path that is not valid UTF-8; oakum cannot attribute it to a package",
)
})
}
pub(super) fn run(&self, op: Op<'_>) -> Result<(), CliError> {
self.checked(&op.shape(), Reads::Text)?;
Ok(())
}
pub(super) fn predicate(&self, op: Op<'_>) -> Result<bool, CliError> {
let shape = op.shape();
let reply = self.answered(&shape, Reads::Text)?;
if reply.succeeded() {
return Ok(true);
}
if reply.said_no() {
return Ok(false);
}
Err(self.failed(&shape, &reply))
}
pub(super) fn matched_paths(&self, op: Op<'_>) -> Result<Option<Vec<String>>, CliError> {
let shape = op.shape();
let reply = self.answered(&shape, Reads::Paths)?;
if !reply.succeeded() {
if reply.said_no() {
return Ok(None);
}
return Err(self.failed(&shape, &reply));
}
split_nul_paths(&reply.stdout).map(Some).ok_or_else(|| {
shape.fail("listed a path that is not valid UTF-8; oakum cannot name it as a gate")
})
}
pub(super) fn optional_text(&self, op: Op<'_>) -> Result<Option<String>, CliError> {
let shape = op.shape();
let reply = self.answered(&shape, Reads::Text)?;
if !reply.succeeded() {
if reply.said_no() {
return Ok(None);
}
return Err(self.failed(&shape, &reply));
}
let text =
String::from_utf8(reply.stdout).map_err(|_| shape.fail("output is not valid UTF-8"))?;
Ok(Some(text.trim().to_owned()).filter(|text| !text.is_empty()))
}
fn checked(&self, shape: &OpShape<'_>, reads: Reads) -> Result<Reply, CliError> {
let reply = self.answered(shape, reads)?;
if reply.succeeded() {
return Ok(reply);
}
Err(self.failed(shape, &reply))
}
fn failed(&self, shape: &OpShape<'_>, reply: &Reply) -> CliError {
let detail = reply.detail();
if self.offline
&& (detail.contains("unable to read") || detail.contains("lazy fetching disabled"))
&& self.has_promisor()
{
return CliError::unverified(format!(
"unverified: {}; this partial clone lacks it, and `check` reads without fetching",
shape
.fail(&detail)
.detail()
.trim_start_matches("unverified: ")
));
}
shape.fail(&detail)
}
fn has_promisor(&self) -> bool {
*self.promisor.get_or_init(|| {
let shape = Op::Promisors.shape();
self.answered(&shape, Reads::Text)
.is_ok_and(|reply| names_a_promisor(&String::from_utf8_lossy(&reply.stdout)))
})
}
fn answered(&self, shape: &OpShape<'_>, reads: Reads) -> Result<Reply, CliError> {
let reply = self.child(shape)?;
if let Some(whole) = shape.spec.whole().filter(|_| reply.succeeded()) {
if let Some(said) = reply.diagnostic().filter(|said| whole.skipped(said)) {
return Err(shape.phrase(|what| {
format!(
"git {what} may have listed only {}, reporting: {said}",
whole.part()
)
}));
}
}
if !reply.succeeded() || reply.spoke(reads) {
return Ok(reply);
}
match shape.spec.answer() {
Answer::Always => Err(Self::empty_answer(shape, &reply)),
Answer::Sometimes if reply.diagnostic().is_some() => {
Err(Self::empty_answer(shape, &reply))
}
Answer::Sometimes | Answer::Never => Ok(reply),
}
}
pub(super) fn commit_text(&self, commit: &str) -> Result<CommitText, CliError> {
let raw = self.text(Op::CommitMessage { commit })?;
let Some((message, trailers)) = raw.split_once('\0') else {
return Err(CliError::new(format!(
"git log for `{commit}` omitted the trailer separator oakum's \
format requests"
)));
};
if trailers.trim() == SKIP_CHECKS_ATOM && !message.contains(SKIP_CHECKS_ATOM) {
return Err(CliError::new(format!(
"this git did not parse the skip-checks trailers for \
`{commit}`; oakum cannot tell whether the commit suppresses \
CI"
)));
}
Ok(CommitText {
message: message.to_owned(),
skip_checks: trailers.to_owned(),
})
}
fn transport(&self) -> Result<&env::BatchSsh, &env::TransportUnknown> {
use std::collections::HashMap;
use std::sync::{Arc, Mutex, OnceLock};
type Resolved =
Mutex<HashMap<std::path::PathBuf, Arc<Result<env::BatchSsh, env::TransportUnknown>>>>;
static RESOLVED: OnceLock<Resolved> = OnceLock::new();
#[cfg(test)]
if let Runner::Fake(fake) = &self.runner {
return fake.transport();
}
self.transport
.get_or_init(|| {
let mut resolved = RESOLVED
.get_or_init(|| Mutex::new(HashMap::new()))
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
resolved
.entry(self.repo.clone())
.or_insert_with(|| Arc::new(env::batch_transport(&self.repo)))
.clone()
})
.as_ref()
.as_ref()
}
fn say_once(&self, note: &str) {
match &self.runner {
Runner::Child => {
self.say_once_with(note, env::warn);
}
#[cfg(test)]
Runner::Fake(fake) => {
self.say_once_with(note, |note| fake.hear(note));
}
}
}
fn say_once_with(&self, note: &str, say: impl FnOnce(&str) -> bool) -> bool {
let key = note.to_owned();
let mut warned = self
.warned
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if !warned.insert(key.clone()) {
return false;
}
let said = say(note);
if !said {
warned.remove(&key);
}
said
}
fn remote_reach(&self, contact: Contact<'_>) -> reach::Reach {
let key = contact.key();
if let Some(answer) = self.remembered_reach(&key) {
return answer;
}
let parsed = self.text(Op::RemoteNames).and_then(|names| {
let names: Vec<&str> = names.lines().collect();
let listing = self.text(Op::RemoteUrls)?;
reach::parse_remote_urls(&listing, &names)
});
let mut cache = self
.reach_by_remote
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let answer = match parsed {
Ok(urls) => {
for (entry, urls) in urls {
cache.insert(entry, reach::classify(Ok(&urls)));
}
cache.get(&key).cloned().unwrap_or_else(|| {
let direction = match contact.direction {
Direction::Fetch => "fetch",
Direction::Push => "push",
};
reach::classify(Err(&CliError::new(format!(
"the listing shows no {direction} URL for remote {:?}",
contact.remote
))))
})
}
Err(err) => reach::classify(Err(&err)),
};
cache.insert(key, answer.clone());
answer
}
fn remembered_reach(&self, key: &(String, Direction)) -> Option<reach::Reach> {
self.reach_by_remote
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.get(key)
.cloned()
}
fn child(&self, shape: &OpShape<'_>) -> Result<Reply, CliError> {
if let Some(held) = &self.held {
super::repository::confirm_ambient(held, &self.repo)
.map_err(|err| shape.fail(&err.to_string()))?;
}
let batch = self
.transport()
.map_err(|unknown| shape.unreadable_transport(unknown))?;
if let Some(contact) = shape.contact {
let reach = self.remote_reach(contact);
for note in reach::notes_for(contact, &reach, batch) {
self.say_once(¬e);
}
}
self.spawn(shape, batch)
}
fn spawn(&self, shape: &OpShape<'_>, batch: &env::BatchSsh) -> Result<Reply, CliError> {
let answered = match &self.runner {
Runner::Child => {
let args: Vec<&str> = shape.argv.iter().map(String::as_str).collect();
let command = env::deadlined_command(&self.repo, &args, batch);
let command = if self.offline || shape.spec.offline() {
command.without_lazy_fetch()
} else {
command
};
command
.output(shape.input.as_deref().map(str::as_bytes))
.map(Reply::from)
}
#[cfg(test)]
Runner::Fake(fake) => fake.answer(shape.name),
};
answered.map_err(|failure| {
if failure.left_no_answer() {
shape.no_answer(&failure.to_string())
} else {
shape.fail(&failure.to_string())
}
})
}
fn empty_answer(shape: &OpShape<'_>, reply: &Reply) -> CliError {
shape.phrase(|what| match reply.diagnostic() {
Some(said) => format!("git {what} answered nothing while reporting: {said}"),
None => format!("git {what} exited 0 without answering"),
})
}
}
#[cfg(test)]
mod tests {
use super::env::{BatchSsh, TransportUnknown};
use super::op::fixture_commit;
use super::{parse_followed, split_nul_paths, Contact, Direction, Followed, Git, Op, Reply};
#[test]
fn a_promisor_is_whatever_git_reads_as_true() {
for named in [
"remote.origin.promisor\ntrue\0",
"remote.origin.promisor\nTRUE\0",
"remote.origin.promisor\n2\0",
"remote.origin.promisor\n1k\0",
"remote.origin.promisor\n0x1\0",
"remote.origin.promisor\0",
"remote.a.promisor\nfalse\0remote.origin.promisor\nyes\0",
"extensions.partialclone\norigin\0",
] {
assert!(super::names_a_promisor(named), "{named:?}");
}
for unnamed in [
"",
"remote.origin.promisor\nfalse\0",
"remote.origin.promisor\n0\0",
"remote.origin.promisor\n0k\0",
] {
assert!(!super::names_a_promisor(unnamed), "{unnamed:?}");
}
}
#[test]
fn blobs_split_one_batch_by_size() {
let git = Git::answering([(
"cat-file --batch",
Reply::said("o1 blob 4\na\nb\n\no2 blob 0\n\no3 blob 2\nhi\n"),
)]);
let texts = git
.blobs(fixture_commit(), &["x.md", "y.md", "z.md"])
.expect("texts");
assert_eq!(texts, ["a\nb\n", "", "hi"]);
}
#[test]
fn blobs_refuse_what_is_not_a_whole_file() {
for (reply, why) in [
("o1 commit 3\nabc\n", "not a file"),
(".changeset/x.md missing\n", "not a file"),
("o1 blob 9\nabc", "stopped inside"),
("o1 blob 3\nabcX", "stopped inside"),
("", "stopped before"),
] {
let err = Git::answering([("cat-file --batch", Reply::said(reply))])
.blobs(fixture_commit(), &["x.md"])
.expect_err(reply);
assert!(err.to_string().contains(why), "{reply:?}: {err}");
}
}
#[test]
fn blobs_ask_nothing_for_no_paths() {
let git = Git::answering([]);
assert_eq!(
git.blobs(fixture_commit(), &[]).expect("empty"),
Vec::<String>::new()
);
assert!(git.asked().is_empty(), "{:?}", git.asked());
}
#[test]
fn commit_paths_splits_one_listing_by_commit() {
let git = Git::answering([(
"diff-tree --stdin",
Reply::said("\u{1}a1\0\nx/lib.rs\0\nodd\0\u{1}c3\0\n\nlead\0"),
)]);
let paths = git.commit_paths(&["a1", "b2", "c3"]);
assert_eq!(paths["a1"].as_deref().expect("a1"), ["x/lib.rs", "\nodd"]);
assert!(paths["b2"].as_deref().expect("b2").is_empty(), "{paths:?}");
assert_eq!(paths["c3"].as_deref().expect("c3"), ["\nlead"]);
}
#[test]
fn a_batch_that_cannot_be_attributed_is_read_again_per_commit() {
let git = Git::answering([
("diff-tree --stdin", Reply::said("\u{1}zz\0\nx\0")),
("diff-tree", Reply::said("x/lib.rs\0")),
]);
let paths = git.commit_paths(&["a1"]);
assert_eq!(
paths["a1"].as_deref().expect("per-commit read"),
["x/lib.rs"]
);
}
#[test]
fn a_failed_batch_is_worded_as_the_per_commit_read() {
let not_utf8 = || Reply::said(b"\x01a1\0\nbad\xff\0".to_vec());
let alone = Git::answering([("diff-tree", Reply::said(b"bad\xff\0".to_vec()))])
.paths(Op::CommitPath { hash: "a1" })
.expect_err("not UTF-8");
let paths = Git::answering([
("diff-tree --stdin", not_utf8()),
("diff-tree", Reply::said(b"bad\xff\0".to_vec())),
])
.commit_paths(&["a1"]);
let err = paths["a1"].as_ref().expect_err("not UTF-8");
assert_eq!(err.to_string(), alone.to_string());
}
#[test]
fn a_failed_batch_fails_only_the_commits_that_fail_alone() {
let paths = Git::answering([
(
"diff-tree --stdin",
Reply::said(b"\x01a1\0\nbad\xff\0".to_vec()),
),
("diff-tree", Reply::said(b"one\xff\0".to_vec())),
("diff-tree", Reply::said("b/lib.rs\0")),
])
.commit_paths(&["a1", "b2"]);
let err = paths["a1"].as_ref().expect_err("not UTF-8");
assert!(err.to_string().contains("a1"), "{err}");
assert_eq!(
paths["b2"].as_deref().expect("b2 reads alone"),
["b/lib.rs"]
);
}
#[test]
fn commit_paths_asks_nothing_for_no_commits() {
let git = Git::answering([]);
assert!(git.commit_paths(&[]).is_empty());
assert!(git.asked().is_empty(), "{:?}", git.asked());
}
#[test]
fn a_path_holding_a_newline_is_refused_before_git_is_asked() {
let git = Git::answering([]);
let err = git
.followed_blob(fixture_commit(), "pkg\nage/CHANGELOG.md")
.expect_err("unframeable");
assert!(err.to_string().starts_with("unverified: "), "{err}");
assert!(git.said().is_empty(), "{:?}", git.said());
}
#[test]
fn a_followed_blob_reply_reads_every_kind_git_answers() {
let parse_followed = |reply: &[u8]| super::parse_followed(reply).map(|(kind, _)| kind);
let oid = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057";
assert_eq!(
parse_followed(format!("{oid} blob 3\nhi\n\n").as_bytes()),
Some(Followed::File(b"hi\n".to_vec()))
);
assert_eq!(
parse_followed(b"HEAD:my notes.md missing\n"),
Some(Followed::Absent)
);
assert_eq!(
parse_followed(b"dangling 9\nHEAD:DANG\n"),
Some(Followed::Dangling)
);
for (reply, why) in [
("loop 10\nHEAD:LOOP1\n", "a symlink loop"),
("notdir 7\nHEAD:ND\n", "a path through a file"),
(
"symlink 6\n/etc/x\n",
"a symlink git will not follow (absolute, or out of the repository)",
),
(&*format!("{oid} tree 4\nabcd\n"), "not a file"),
] {
assert_eq!(
parse_followed(reply.as_bytes()),
Some(Followed::Unfollowed(why)),
"{reply}"
);
}
for unframed in [
format!("{oid} blob 9\nhi\n"),
format!("{oid} blob x\nhi\n"),
String::from("no newline"),
] {
assert_eq!(parse_followed(unframed.as_bytes()), None, "{unframed}");
}
}
#[test]
fn followed_replies_are_read_one_after_another() {
let oid = "45b983be36b73c0788dc9cbcb76cbb80fc7bb057";
let stdout = format!("dangling 9\nHEAD:DANG\nHEAD:a b missing\n{oid} blob 3\nhi\n\n");
let (first, rest) = parse_followed(stdout.as_bytes()).expect("first");
let (second, rest) = parse_followed(rest).expect("second");
let (third, rest) = parse_followed(rest).expect("third");
assert_eq!(
[first, second, third],
[
Followed::Dangling,
Followed::Absent,
Followed::File(b"hi\n".to_vec())
]
);
assert!(rest.is_empty(), "{rest:?}");
}
const SSH_URL: &str = "git@example.invalid:demo/demo.git";
const HTTPS_URL: &str = "https://example.invalid/demo/demo.git";
const HELPER_URL: &str = "ext::ssh -p 22 git@example.invalid %S demo.git";
const WORKTREE: &str = "status --porcelain -z";
#[test]
fn a_failure_quotes_git_without_its_colour() {
let reply = Reply::failed(128, "\u{1b}[31mfatal\u{1b}[0m: denied\r\n");
assert_eq!(reply.detail(), "exit 128: fatal: denied");
}
#[test]
fn a_whole_listing_that_names_unread_tree_is_refused() {
let err = Git::answering([(
WORKTREE,
Reply::said_and_warned(
" M alpha/src/lib.rs\0",
"warning: could not open directory 'beta/hidden/': Permission denied",
),
)])
.paths(Op::UncommittedPaths)
.expect_err("refused");
assert_eq!(err.exit_code(), 2, "{err}");
assert!(err.to_string().contains("only part of the tree"), "{err}");
}
#[test]
fn a_whole_listing_is_kept_when_the_warning_is_not_about_unread_tree() {
assert_eq!(
Git::answering([(
WORKTREE,
Reply::said_and_warned(
" M alpha/src/lib.rs\0",
"warning: unable to access '.git/info/exclude': Permission denied",
),
)])
.paths(Op::UncommittedPaths)
.expect("kept"),
[" M alpha/src/lib.rs"]
);
}
#[test]
fn a_whole_listing_beside_an_unprefixed_line_is_refused() {
let err = Git::answering([(
WORKTREE,
Reply::said_and_warned(
" M alpha/src/lib.rs\0",
"beta/src/lib.rs: Permission denied",
),
)])
.paths(Op::UncommittedPaths)
.expect_err("refused");
assert_eq!(err.exit_code(), 2, "{err}");
assert!(err.to_string().contains("only part of the tree"), "{err}");
}
#[test]
fn a_line_that_names_no_unread_tree_is_kept() {
for said in [
"BUG: something odd",
"error: something odd",
"fatal: something odd",
"hint: the hook was ignored because it is not executable",
"note: something odd",
"warning: unable to access '.git/info/exclude': Permission denied",
"warning: Negative patterns are ignored in git attributes\nUse '\\!' for literal leading exclamation.",
"chat: cleaning",
"cat: optional-cache: Permission denied",
"cat: /nonexistent: No such file or directory",
"chat: access was Permission denied",
"warning: x\n\nhint: y",
] {
Git::answering([(
WORKTREE,
Reply::said_and_warned(" M alpha/src/lib.rs\0", said),
)])
.paths(Op::UncommittedPaths)
.expect(said);
}
}
#[test]
fn a_clean_tree_is_kept_when_the_warning_is_not_about_unread_tree() {
let kept = Git::answering([(
WORKTREE,
Reply::warned("warning: unable to access '.git/info/exclude': Permission denied"),
)])
.paths(Op::UncommittedPaths)
.expect("a clean tree that warned benignly is still a clean tree");
assert!(kept.is_empty(), "{kept:?}");
}
#[test]
fn every_shape_of_unread_tree_is_recognised() {
for said in [
"cannot lstat 'beta/x'",
"Cannot lstat 'beta/x'",
"cannot opendir 'beta/hidden'",
"cannot opendir beta/hidden",
"warning: could not open directory 'beta/hidden/': Permission denied",
"Could not open directory beta/hidden",
"opendir('beta/hidden') failed",
] {
let err = Git::answering([(
WORKTREE,
Reply::said_and_warned(" M alpha/src/lib.rs\0", said),
)])
.paths(Op::UncommittedPaths)
.expect_err(said);
assert!(
err.to_string().contains("only part of the tree"),
"{said}: {err}"
);
}
}
#[test]
fn a_whole_listing_that_failed_outright_is_not_called_partial() {
let err = Git::answering([(
WORKTREE,
Reply::failed(128, "fatal: cannot opendir 'beta/hidden'"),
)])
.paths(Op::UncommittedPaths)
.expect_err("refused");
assert_eq!(err.exit_code(), 2, "{err}");
assert!(err.to_string().contains("failed: exit 128"), "{err}");
assert!(
!err.to_string().contains("only part of the tree"),
"it listed none of it: {err}"
);
}
fn at_remote(url: &str, replies: impl IntoIterator<Item = (&'static str, Reply)>) -> Git {
Git::answering(listing(url).into_iter().chain(replies))
}
fn listing(url: &str) -> [(&'static str, Reply); 2] {
[
("remote", Reply::said("origin")),
(
"remote -v",
Reply::said(format!("origin\t{url} (fetch)\norigin\t{url} (push)")),
),
]
}
fn advertised() -> (&'static str, Reply) {
("ls-remote --tags", Reply::said("cafe\trefs/tags/v1.0.0\n"))
}
fn opaque() -> BatchSsh {
BatchSsh::Unprotected(String::from(
"GIT_SSH names `my-ssh`, which takes its arguments from git, not from oakum",
))
}
#[test]
fn exit_one_with_nothing_written_is_how_git_says_absent() {
assert_eq!(
Git::answering([("config --get-regexp url|tagopt|fetch", Reply::absent())])
.optional_text(Op::RemoteTagConfig)
.expect("absent"),
None
);
assert!(!Git::answering([("rev-parse --verify", Reply::absent())])
.predicate(Op::RefExists {
reference: "refs/tags/v1.0.0",
})
.expect("absent"));
}
#[test]
fn only_exit_one_with_both_streams_empty_means_absent() {
let diagnosed = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::failed(1, "fatal: not a git repository"),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("a diagnosed exit must not read as absence");
assert!(diagnosed.is_unverified(), "{diagnosed:?}");
assert!(
diagnosed.to_string().contains("not a git repository"),
"{diagnosed}"
);
let silent = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::exactly(Some(128), b"", b""),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("exit 128 is not exit 1");
assert!(silent.to_string().contains("exit 128"), "{silent}");
let spoke = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::exactly(Some(1), b"remote.origin.tagopt --no-tags", b""),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("a child that wrote an answer did not say no");
assert!(spoke.to_string().contains("exit 1"), "{spoke}");
let killed = Git::answering([("rev-parse --verify", Reply::was_signalled())])
.predicate(Op::RefExists {
reference: "refs/tags/v1.0.0",
})
.expect_err("a signal is not an answer of no");
assert!(
killed.to_string().contains("terminated by a signal"),
"{killed}"
);
}
#[test]
fn a_cached_transport_failure_is_repeated_verbatim() {
let git = Git::at("/nonexistent");
git.transport
.set(
Err(super::env::TransportUnknown::SshConfig(String::from(
"git config was killed by a signal",
)))
.into(),
)
.expect("the cache starts empty");
for _ in 0..3 {
assert_eq!(
git.transport().expect_err("a cached failure").detail(),
"git config was killed by a signal"
);
}
let raised = Op::AdvertisedTags { remote: "origin" }
.shape()
.unreadable_transport(&super::env::TransportUnknown::SshConfig(String::from(
"git config was killed by a signal",
)));
assert!(raised.is_unverified(), "{raised:?}");
assert!(
raised.to_string().contains("killed by a signal"),
"{raised}"
);
}
#[test]
fn only_a_trace_that_names_a_file_is_left_in_place() {
for value in ["1", "2", "true", "relative.log", ""] {
assert!(
!super::traces_to_a_file(std::ffi::OsStr::new(value)),
"{value:?} reaches our stderr"
);
}
let file = std::env::temp_dir().join("oakum-trace.event");
assert!(super::traces_to_a_file(file.as_os_str()));
}
#[test]
fn untrace_silences_every_channel_and_leaves_the_rest_alone() {
let file = std::env::temp_dir().join("oakum-trace.perf");
let inherited = [
(
std::ffi::OsString::from("GIT_TRACE"),
std::ffi::OsString::from("1"),
),
(
std::ffi::OsString::from("GIT_TRACE_PACKET"),
std::ffi::OsString::from("1"),
),
(
std::ffi::OsString::from("GIT_TRACE_A_CHANNEL_ADDED_LATER"),
std::ffi::OsString::from("1"),
),
(
std::ffi::OsString::from("GIT_TRACE2_EVENT"),
std::ffi::OsString::from("relative.log"),
),
(
std::ffi::OsString::from("GIT_TRACE2_PERF"),
file.into_os_string(),
),
];
let mut command = std::process::Command::new("git");
command.env("GIT_TERMINAL_PROMPT", "0");
super::untrace_from(&mut command, inherited);
let envs: Vec<(String, Option<String>)> = command
.get_envs()
.map(|(name, value)| {
(
name.to_string_lossy().into_owned(),
value.map(|value| value.to_string_lossy().into_owned()),
)
})
.collect();
let setting = |name: &str| {
envs.iter()
.find(|(key, _)| key == name)
.map(|(_, value)| value.clone())
};
for name in [
"GIT_TRACE",
"GIT_TRACE_PACKET",
"GIT_TRACE_A_CHANNEL_ADDED_LATER",
] {
assert_eq!(setting(name), Some(None), "{name} still reaches the child");
}
assert_eq!(
setting("GIT_TRACE2_EVENT"),
Some(Some(String::from("0"))),
"config-driven trace2 is still live"
);
assert_eq!(
setting("GIT_TRACE2_PERF"),
None,
"a trace2 file target was overridden"
);
assert_eq!(
setting("GIT_TERMINAL_PROMPT"),
Some(Some(String::from("0")))
);
}
#[test]
#[should_panic(expected = "nothing scripted answers")]
fn a_command_that_only_shares_a_character_prefix_does_not_answer() {
let git = Git::answering([(
"rev-parse --verify --quiet refs/tags/v1.0.0",
Reply::said("cafebabe"),
)]);
let _ = git.optional_text(Op::LocalTagCommit { tag: "v1.0.0" });
}
#[test]
fn a_child_that_always_prints_and_printed_nothing_is_refused_on_every_accessor() {
let warning = "warning: unable to access '/etc/gitconfig': Permission denied";
let asked = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::warned(warning),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("optional_text");
assert!(asked.is_unverified(), "{asked:?}");
assert!(asked.to_string().contains("Permission denied"), "{asked}");
let voted = Git::answering([("rev-parse --verify", Reply::warned(warning))])
.predicate(Op::RefExists {
reference: "refs/tags/v1.0.0",
})
.expect_err("predicate");
assert!(voted.to_string().contains("answered nothing"), "{voted}");
let read = Git::answering([("rev-parse HEAD", Reply::warned(warning))])
.text(Op::Head)
.expect_err("text");
assert!(read.to_string().contains("Permission denied"), "{read}");
let mute = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::exactly(Some(0), b"", b""),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("silence is not absence");
assert!(
mute.to_string().contains("exited 0 without answering"),
"{mute}"
);
}
#[test]
fn a_verification_that_reported_nothing_while_warning_is_not_an_empty_look() {
let err = Git::answering([(
"for-each-ref --merged HEAD",
Reply::warned("warning: refname 'v1.0.0' is ambiguous"),
)])
.text(Op::ReachableTags)
.expect_err("a warned look must not read as no tags");
assert!(err.is_unverified(), "{err:?}");
assert!(err.to_string().contains("ambiguous"), "{err}");
}
#[test]
fn a_refusal_gate_that_never_looked_is_not_a_clean_answer() {
for (command, op) in [
("status --porcelain", Op::WorktreeStatus),
("log -1", Op::CommitMessage { commit: "HEAD" }),
(
"diff-tree --stdin",
Op::CommitPaths {
hashes: &["cafebabe"],
},
),
] {
let err = Git::answering([(command, Reply::warned("fatal: could not read index"))])
.text(op)
.expect_err("{op:?} must not read as an empty answer");
assert!(err.to_string().contains("could not read index"), "{err}");
}
}
#[test]
fn work_that_answers_through_its_exit_code_is_not_refused_for_writing_to_stderr() {
at_remote(
SSH_URL,
[(
"push",
Reply::warned("To github.com:oakoss/oakum.git\n * [new tag] v1.0.0 -> v1.0.0"),
)],
)
.run(Op::PushTag {
remote: "origin",
tag: "v1.0.0",
})
.expect("push reports through stderr on success");
assert!(Git::answering([(
"check-ref-format",
Reply::warned("warning: unable to access '/etc/gitconfig'"),
)])
.predicate(Op::ValidRefName {
reference: "v1.0.0",
})
.expect("check-ref-format reports through its exit code"));
}
#[test]
fn a_non_utf8_path_fails_in_the_operations_own_voice() {
let err = Git::answering([("diff --name-only", Reply::said(b"pkg/\xff.bin\0".to_vec()))])
.paths(Op::ChangedPaths { from: "v1.0.0" })
.expect_err("a non-UTF-8 path cannot name a package");
assert!(
err.to_string().contains("cannot attribute it to a package"),
"{err}"
);
assert!(
err.is_unverified(),
"`diff --name-only` is a verification: {err:?}"
);
}
#[test]
fn each_accessor_draws_the_silence_line_where_it_reads() {
for stdout in ["\u{0B}", "\u{A0}", "\u{2028}"] {
let err = Git::answering([(
"status --porcelain",
Reply::exactly(
Some(0),
stdout.as_bytes(),
b"fatal: could not read the index",
),
)])
.text(Op::WorktreeStatus)
.expect_err("whitespace is not a clean worktree");
assert!(
err.to_string().contains("could not read the index"),
"{err}"
);
}
let listed = Git::answering([(
"diff --name-only",
Reply::exactly(
Some(0),
b" \0",
b"warning: unable to access '/etc/gitconfig'",
),
)])
.paths(Op::ChangedPaths { from: "v1.0.0" })
.expect("a whitespace filename is a filename");
assert_eq!(listed, [" "]);
let none = Git::answering([(
"diff --name-only",
Reply::exactly(
Some(0),
b"\0\0",
b"warning: unable to access '/etc/gitconfig'",
),
)])
.paths(Op::ChangedPaths { from: "v1.0.0" })
.expect_err("empty records listed nothing");
assert!(none.to_string().contains("answered nothing"), "{none}");
let absent = Git::answering([("rev-parse --verify refs/tags", Reply::said(" \n"))])
.optional_text(Op::LocalTagCommit { tag: "v1.0.0" })
.expect_err("whitespace is not a commit");
assert!(absent.to_string().contains("without answering"), "{absent}");
assert_eq!(
Git::answering([("remote", Reply::exactly(Some(0), b"", b" \n"))])
.text(Op::RemoteNames)
.expect("a blank diagnostic is not a diagnostic"),
""
);
}
#[test]
fn whitespace_on_stdout_is_not_an_answer() {
let err = Git::answering([(
"config --get-regexp url|tagopt|fetch",
Reply::exactly(Some(0), b" \n\t", b"fatal: bad config line 9"),
)])
.optional_text(Op::RemoteTagConfig)
.expect_err("whitespace is not an answer");
assert!(err.to_string().contains("bad config line 9"), "{err}");
let looked = Git::answering([(
"status --porcelain",
Reply::exactly(Some(0), b"\n", b"fatal: could not read index"),
)])
.text(Op::WorktreeStatus)
.expect_err("nor for a gate whose empty answer means clean");
assert!(
looked.to_string().contains("could not read index"),
"{looked}"
);
}
#[test]
fn a_child_that_answered_or_had_nothing_to_answer_is_not_refused() {
let listed = Git::answering([(
"for-each-ref --merged HEAD",
Reply::said_and_warned(
"refs/tags/v1.0.0\0commit\0cafebabe\0\0",
"warning: unable to access '/home/u/.config/git/attributes': Permission denied",
),
)])
.text(Op::ReachableTags)
.expect("a warning alongside an answer leaves the answer standing");
assert!(listed.contains("v1.0.0"), "{listed}");
at_remote(
SSH_URL,
[(
"push",
Reply::warned("To github.com:oakoss/oakum.git\n * [new tag] v1.0.0"),
)],
)
.run(Op::PushTag {
remote: "origin",
tag: "v1.0.0",
})
.expect("push reports through stderr on success");
}
#[test]
fn a_tag_listing_that_skipped_a_broken_ref_is_refused() {
for (op, name, listed) in [
(
Op::ReachableTags,
"for-each-ref --merged HEAD",
"refs/tags/v1.0.0\0commit\0cafebabe\0\0",
),
(
Op::AllTags,
"for-each-ref refs/tags",
"refs/tags/v1.0.0\0cafebabe",
),
] {
let err = Git::answering([(
name,
Reply::said_and_warned(listed, "warning: ignoring broken ref refs/tags/v2.0.0"),
)])
.text(op)
.expect_err("a skipped tag leaves the listing short");
assert!(
err.to_string()
.contains("ignoring broken ref refs/tags/v2.0.0"),
"{err}"
);
assert!(err.to_string().contains("some of the refs"), "{err}");
assert!(!err.to_string().contains("part of the tree"), "{err}");
assert!(err.is_unverified(), "{err:?}");
}
}
#[test]
fn a_silent_failure_and_a_signal_do_not_render_alike() {
let silent = Git::answering([("rev-parse HEAD", Reply::exactly(Some(128), b"", b""))])
.text(Op::Head)
.expect_err("exit 128");
assert!(
silent.to_string().contains("exit 128 with no diagnostic"),
"{silent}"
);
let killed = Git::answering([("rev-parse HEAD", Reply::was_signalled())])
.text(Op::Head)
.expect_err("signalled");
assert!(
killed.to_string().contains("terminated by a signal"),
"{killed}"
);
let banner = at_remote(
SSH_URL,
[(
"push",
Reply::exactly(None, b"", b"To /private/tmp/origin.git\n"),
)],
)
.run(Op::PushTag {
remote: "origin",
tag: "v1.0.0",
})
.expect_err("a signal death with stderr");
assert!(
banner.to_string().contains("terminated by a signal"),
"{banner}"
);
assert!(
banner.to_string().contains("To /private/tmp/origin.git"),
"git's words stay as evidence: {banner}"
);
}
#[test]
fn only_a_lossy_read_accepts_bytes_that_are_not_utf8() {
let message = Git::answering([("log -1", Reply::said(b"fix: caf\xff\n".to_vec()))])
.text(Op::CommitMessage { commit: "HEAD" })
.expect("a commit message is read lossily");
assert_eq!(message, "fix: caf\u{fffd}");
let err = Git::answering([("rev-parse HEAD", Reply::said(b"\xff".to_vec()))])
.text(Op::Head)
.expect_err("an object name must be valid UTF-8");
assert!(err.to_string().contains("not valid UTF-8"), "{err}");
}
#[test]
fn a_failed_action_is_not_unverified() {
let err = Git::answering([("tag", Reply::failed(128, "fatal: tag already exists"))])
.run(Op::AnnotatedTag {
name: "v1.0.0",
commit: fixture_commit(),
})
.expect_err("tag failed");
assert!(!err.is_unverified(), "{err:?}");
}
#[test]
fn a_credential_starved_remote_failure_names_the_fix() {
for starved in [
"fatal: could not read Username for 'https://gitlab.com': prompts off",
"fatal: could not read Password for 'https://gitlab.com': prompts off",
"fatal: Authentication failed for 'https://gitlab.com/'",
"fatal: Username not available: terminal prompts disabled",
] {
let err = at_remote(
HTTPS_URL,
[("ls-remote --tags", Reply::failed(128, starved))],
)
.text(Op::AdvertisedTags { remote: "origin" })
.expect_err("a starved remote read fails");
let text = err.to_string();
assert!(text.contains(starved), "{text}");
assert!(text.contains("credential helper"), "{text}");
assert!(text.contains("gh auth setup-git"), "{text}");
assert!(text.starts_with("unverified:"), "{text}");
}
let err = at_remote(
SSH_URL,
[(
"ls-remote --tags",
Reply::failed(128, "fatal: repository not found"),
)],
)
.text(Op::AdvertisedTags { remote: "origin" })
.expect_err("an unrelated remote failure");
assert!(!err.to_string().contains("credential helper"), "{err}");
let starved =
"fatal: could not read Username for 'https://gitlab.com': terminal prompts disabled";
let err = Git::answering([("log -1", Reply::failed(128, starved))])
.text(Op::CommitMessage { commit: "HEAD" })
.expect_err("a local child never gets the note");
assert!(!err.to_string().contains("credential helper"), "{err}");
}
#[test]
fn the_unparsed_trailer_guard_spares_a_value_that_quotes_the_atom() {
let old_git = Git::answering([(
"log -1",
Reply::said(format!("chore: release\0{}\n", super::SKIP_CHECKS_ATOM)),
)]);
let err = old_git.commit_text("cafe").expect_err("old git refuses");
assert!(err.to_string().contains("did not parse"), "{err}");
let quoting = Git::answering([(
"log -1",
Reply::said(format!(
"chore: x\n\nskip-checks: {atom}\0{atom}\n",
atom = super::SKIP_CHECKS_ATOM
)),
)]);
let text = quoting
.commit_text("cafe")
.expect("a quoting value is a value");
assert_eq!(text.skip_checks.trim(), super::SKIP_CHECKS_ATOM);
}
#[test]
fn an_unlisted_remote_is_unread_not_safe() {
let git = Git::answering([
("remote", Reply::said("other")),
(
"remote -v",
Reply::said("other\thttps://host/r.git (fetch)"),
),
]);
let reach = git.remote_reach(Contact {
remote: "origin",
direction: Direction::Fetch,
});
let unread = reach.unread.expect("an unlisted remote is unread");
assert!(!reach.ssh && !reach.helper);
assert!(
unread.to_string().contains("shows no fetch URL"),
"{unread}"
);
}
#[test]
fn a_refused_note_stays_owed_until_it_lands() {
let git = Git::answering([]);
let mut offered = String::new();
assert!(
!git.say_once_with("a note", |note| {
offered.push_str(note);
false
}),
"a refused write must not report the note as said"
);
assert_eq!(offered, "a note", "the sayer is given the note verbatim");
assert!(
git.say_once_with("a note", |_| true),
"a refused note is still owed, so the next child may say it"
);
assert!(
!git.say_once_with("a note", |_| panic!("said twice")),
"once it lands it must not repeat, once per remote child"
);
}
#[test]
fn distinct_notes_say_themselves_and_identical_ones_do_not_repeat() {
let git = Git::answering([]);
assert!(git.say_once_with("fetch note", |_| true));
assert!(git.say_once_with("push note", |_| true));
assert!(!git.say_once_with("fetch note", |_| panic!("repeated")));
}
#[test]
fn an_unreadable_transport_stops_every_child_before_it_spawns() {
let git = Git::answering_over(
Err(TransportUnknown::SshConfig(String::from(
"fatal: unable to read config file",
))),
[
advertised(),
("push", Reply::warned("To example.invalid:demo/demo.git")),
("log -1", Reply::said("fix: typo\0\n")),
],
);
let looked = git
.text(Op::AdvertisedTags { remote: "origin" })
.expect_err("a look whose transport could not be resolved");
assert!(looked.is_unverified(), "{looked:?}");
let text = looked.to_string();
assert!(
text.contains(
"git ls-remote --tags origin needs an ssh configuration oakum could not read"
),
"{text}"
);
assert!(text.contains("unable to read config file"), "{text}");
assert!(text.contains("will not guess a transport"), "{text}");
let acted = git
.run(Op::PushTag {
remote: "origin",
tag: "v1.0.0",
})
.expect_err("a push that never ran");
assert!(!acted.is_unverified(), "{acted:?}");
let local = git
.commit_text("HEAD")
.expect_err("a local child carries the transport too");
assert!(
local
.to_string()
.contains("git log -1 HEAD needs an ssh configuration"),
"{local}"
);
assert!(
git.asked().is_empty(),
"no child may spawn without a resolved transport: {:?}",
git.asked()
);
assert!(git.said().is_empty(), "{:?}", git.said());
}
#[test]
fn a_local_child_reads_no_remote_listing() {
let git = Git::answering([("log -1", Reply::said("fix: typo\0\n"))]);
git.commit_text("HEAD").expect("a local read");
assert_eq!(git.asked(), ["log -1"]);
assert!(git.said().is_empty(), "{:?}", git.said());
}
#[test]
fn a_helper_remote_owes_its_note_under_a_composed_transport() {
let git = at_remote(HELPER_URL, [advertised()]);
git.text(Op::AdvertisedTags { remote: "origin" })
.expect("the look runs; the note is advisory");
let said = git.said();
assert_eq!(said.len(), 1, "{said:?}");
assert!(
said[0].contains("\"origin\" runs a helper command oakum cannot inspect"),
"{said:?}"
);
assert!(
!said[0].contains("could not be protected"),
"a composed transport owes no transport rider: {said:?}"
);
assert!(
!said[0].contains("could not read that remote's URL"),
"the note must not claim a read failed when it did not: {said:?}"
);
}
#[test]
fn only_an_ssh_remote_is_told_about_ssh_prompts() {
let quiet = Git::answering_over(
Ok(opaque()),
listing(HTTPS_URL).into_iter().chain([advertised()]),
);
quiet
.text(Op::AdvertisedTags { remote: "origin" })
.expect("an https look");
assert_eq!(
quiet.asked(),
["remote", "remote -v", "ls-remote --tags"],
"an absent note reads the same when no remote child ran"
);
assert!(quiet.said().is_empty(), "{:?}", quiet.said());
let told = Git::answering_over(
Ok(opaque()),
listing(SSH_URL).into_iter().chain([advertised()]),
);
told.text(Op::AdvertisedTags { remote: "origin" })
.expect("an ssh look");
let said = told.said();
assert_eq!(said.len(), 1, "{said:?}");
assert!(
said[0].contains("cannot refuse ssh prompts for the transport \"origin\" uses"),
"{said:?}"
);
assert!(said[0].contains("GIT_SSH names `my-ssh`"), "{said:?}");
}
#[test]
fn the_listing_is_read_once_and_a_note_said_once_per_run() {
let git = at_remote(
HELPER_URL,
[
advertised(),
("push", Reply::warned("To example.invalid:demo/demo.git")),
],
);
git.text(Op::AdvertisedTags { remote: "origin" })
.expect("the look");
git.run(Op::PushTag {
remote: "origin",
tag: "v1.0.0",
})
.expect("the push");
assert_eq!(
git.asked(),
["remote", "remote -v", "ls-remote --tags", "push"]
);
assert_eq!(git.said().len(), 1, "{:?}", git.said());
}
#[test]
fn an_unreadable_listing_still_gets_a_hedged_note() {
let unread = |transport| {
Git::answering_over(
transport,
[
("remote", Reply::said("origin")),
("remote -v", Reply::failed(2, "fatal: unreadable")),
advertised(),
],
)
};
let unprotected = unread(Ok(opaque()));
unprotected
.text(Op::AdvertisedTags { remote: "origin" })
.expect("the look still runs");
let said = unprotected.said();
assert_eq!(said.len(), 1, "{said:?}");
assert!(
said[0].contains("could not establish what transport \"origin\" uses"),
"{said:?}"
);
assert!(
said[0].contains("could not read that remote's URL"),
"{said:?}"
);
assert!(said[0].contains("fatal: unreadable"), "{said:?}");
assert!(
said[0].contains("could not be protected either: GIT_SSH names"),
"{said:?}"
);
assert!(
!said[0].contains("cannot refuse ssh prompts"),
"an unread URL is not an ssh URL: {said:?}"
);
let composed = unread(Ok(BatchSsh::Composed(String::from("ssh -o BatchMode=yes"))));
composed
.text(Op::AdvertisedTags { remote: "origin" })
.expect("the look still runs");
let said = composed.said();
assert_eq!(said.len(), 1, "{said:?}");
assert!(
said[0].contains("could not establish what transport"),
"{said:?}"
);
assert!(
!said[0].contains("could not be protected"),
"a composed transport owes no transport rider: {said:?}"
);
}
#[test]
fn a_repository_git_cannot_open_is_not_reported_as_an_ssh_problem() {
let refused = Op::AdvertisedTags { remote: "origin" }
.shape()
.unreadable_transport(&super::env::TransportUnknown::Repository(String::from(
"exit 128: fatal: Expected git repo version <= 1, found 99",
)))
.to_string();
assert!(
refused.contains("could not read this repository"),
"{refused}"
);
assert!(refused.contains("found 99"), "{refused}");
assert!(!refused.contains("ssh"), "{refused}");
assert!(!refused.contains("GIT_SSH_COMMAND"), "{refused}");
}
#[test]
fn an_unreadable_ssh_variable_is_not_answered_by_setting_it() {
let said = Op::AdvertisedTags { remote: "origin" }
.shape()
.unreadable_transport(&super::env::TransportUnknown::SshVariable(String::from(
"GIT_SSH_COMMAND is not valid UTF-8",
)))
.to_string();
assert!(
said.contains("GIT_SSH_COMMAND is not valid UTF-8"),
"{said}"
);
assert!(said.contains("repair or unset that variable"), "{said}");
assert!(
!said.contains("set both GIT_SSH_COMMAND and GIT_SSH_VARIANT"),
"circular: {said}"
);
}
#[test]
fn a_probe_that_never_reached_git_claims_neither_cause() {
let unasked = Op::AdvertisedTags { remote: "origin" }
.shape()
.unreadable_transport(&super::env::TransportUnknown::Unasked(String::from(
"could not run git: No such file or directory (os error 2)",
)))
.to_string();
assert!(unasked.contains("could not ask git"), "{unasked}");
assert!(unasked.contains("No such file or directory"), "{unasked}");
assert!(!unasked.contains("GIT_SSH_COMMAND"), "{unasked}");
assert!(
!unasked.contains("could not read this repository"),
"a probe that did not run says nothing about the repository: {unasked}"
);
}
#[test]
fn a_probe_that_never_answered_is_unverified_whatever_the_op() {
let push = Op::PushTag {
remote: "origin",
tag: "v1.0.0",
}
.shape();
let detail = String::from("gave up after 2s with no answer");
let unanswered =
push.unreadable_transport(&super::env::TransportUnknown::Unanswered(detail.clone()));
assert_eq!(unanswered.exit_code(), 2, "{unanswered}");
let unasked = push.unreadable_transport(&super::env::TransportUnknown::Unasked(detail));
assert_eq!(unasked.exit_code(), 1, "{unasked}");
}
#[test]
fn nul_records_are_preserved_exactly() {
let parsed = split_nul_paths(b"pkg/a b\0pkg/\n weird \0pkg/caf\xc3\xa9.rs\0")
.expect("valid utf-8 records");
assert_eq!(parsed, ["pkg/a b", "pkg/\n weird ", "pkg/caf\u{e9}.rs"]);
assert_eq!(split_nul_paths(b"").expect("empty"), Vec::<String>::new());
}
#[test]
fn a_non_utf8_record_is_refused_rather_than_lossily_rewritten() {
assert_eq!(split_nul_paths(b"pkg/\xff.bin\0"), None);
}
}
#[cfg(test)]
mod base_tree_reads {
use std::path::Path;
use std::process::Command;
use super::super::base_tree::base_workspace_in_scratch as in_scratch;
use super::{Commit, Git};
use crate::cli::Outcome;
fn git(dir: &Path, args: &[&str]) -> String {
let global = dir.join("..").join("gitconfig");
std::fs::write(&global, "").expect("global config");
let out = Command::new("git")
.args([
"-c",
"user.name=t",
"-c",
"user.email=t@t",
"-c",
"commit.gpgSign=false",
])
.args(args)
.current_dir(dir)
.env("GIT_CONFIG_GLOBAL", &global)
.env("GIT_CONFIG_NOSYSTEM", "1")
.output()
.expect("git");
assert!(
out.status.success(),
"git {args:?}: {}",
String::from_utf8_lossy(&out.stderr)
);
String::from_utf8(out.stdout)
.expect("utf-8")
.trim()
.to_owned()
}
#[test]
fn a_blob_a_partial_clone_lacks_is_unverified_and_not_fetched() {
let (read, before, after) = in_scratch(&[], |dir: &Path| {
let source = dir.join("source");
std::fs::create_dir_all(source.join("app/src")).expect("source");
git(&source, &["init", "-q"]);
std::fs::write(
source.join("Cargo.toml"),
"[workspace]\nmembers = [\"app\"]\n",
)
.expect("root");
let manifest = "[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
std::fs::write(source.join("app/Cargo.toml"), manifest).expect("app");
std::fs::write(source.join("app/src/lib.rs"), "").expect("lib");
git(&source, &["add", "-A"]);
git(&source, &["commit", "-qm", "base"]);
std::fs::write(
source.join("app/Cargo.toml"),
format!("{manifest}# changed\n"),
)
.expect("app");
git(&source, &["commit", "-qam", "head"]);
git(&source, &["config", "uploadpack.allowFilter", "true"]);
let url = format!("file://{}", source.display());
git(dir, &["clone", "-q", "--filter=blob:none", &url, "clone"]);
let clone = dir.join("clone");
let base = git(&clone, &["rev-parse", "HEAD^"]);
(clone, base)
});
let err = read.expect_err("unverified");
assert!(
err.to_string().contains("is not in this partial clone"),
"{err}"
);
assert_eq!(after, before);
}
fn one_member_repo(dir: &Path) -> std::path::PathBuf {
let repo = dir.join("repo");
std::fs::create_dir_all(repo.join("app/src")).expect("repo");
git(&repo, &["init", "-q"]);
std::fs::write(
repo.join("Cargo.toml"),
"[workspace]\nmembers = [\"app\"]\n",
)
.expect("root");
std::fs::write(
repo.join("app/Cargo.toml"),
"[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
)
.expect("app");
std::fs::write(repo.join("app/src/lib.rs"), "").expect("lib");
git(&repo, &["add", "-A"]);
repo
}
#[test]
fn a_submodule_holding_a_member_is_unverified() {
let (read, _, _) = in_scratch(&[("app", "app"), ("vendor/lib", "lib")], |dir: &Path| {
let repo = one_member_repo(dir);
let commit = "1111111111111111111111111111111111111111";
git(
&repo,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{commit},vendor/lib"),
],
);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`vendor/lib`"), "{err}");
assert!(err.to_string().contains("is a submodule"), "{err}");
}
fn write_crate(dir: &Path, name: &str) {
std::fs::create_dir_all(dir.join("src")).expect("crate");
std::fs::write(
dir.join("Cargo.toml"),
format!("[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"),
)
.expect("manifest");
std::fs::write(dir.join("src/lib.rs"), "").expect("lib");
}
fn base_repo(
dir: &Path,
root: &str,
build: impl FnOnce(&Path),
) -> (std::path::PathBuf, String) {
let repo = dir.join("repo");
std::fs::create_dir_all(&repo).expect("repo");
git(&repo, &["init", "-q"]);
std::fs::write(repo.join("Cargo.toml"), root).expect("root");
build(&repo);
git(&repo, &["add", "-A"]);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
}
fn names(read: Result<Option<oakum::plan::Workspace>, super::CliError>) -> Vec<String> {
let workspace = read.expect("read").expect("a workspace");
let mut names: Vec<String> = workspace.packages().map(|p| p.id().name.clone()).collect();
names.sort();
names
}
#[cfg(unix)]
#[test]
fn nested_links_and_a_link_to_the_root_read_as_a_checkout_does() {
use std::os::unix::fs::symlink;
let (read, _, _) = in_scratch(&[], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = [\"crates/*\"]\n", |repo| {
write_crate(&repo.join("real/a"), "a");
write_crate(&repo.join("other/c"), "c");
symlink("../other/c", repo.join("real/c")).expect("inner link");
symlink("real", repo.join("crates")).expect("outer link");
})
});
assert_eq!(names(read), ["a", "c"]);
let (read, _, _) = in_scratch(&[], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = [\"self/app\"]\n", |repo| {
write_crate(&repo.join("app"), "app");
symlink(".", repo.join("self")).expect("root link");
})
});
assert_eq!(names(read), ["app"]);
}
const LOCK_X_Y: &str = "version = 4\n\n[[package]]\nname = \"x\"\nversion = \"0.1.0\"\n\n[[package]]\nname = \"y\"\nversion = \"0.1.0\"\n";
#[cfg(unix)]
#[test]
fn a_config_link_leaving_the_tree_through_another_link_is_unverified() {
use std::os::unix::fs::symlink;
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
std::fs::create_dir_all(repo.join(".cargo")).expect(".cargo");
std::fs::create_dir_all(repo.join("d")).expect("d");
symlink("..", repo.join("d/a")).expect("d/a");
symlink("a/../../outside.toml", repo.join("d/b")).expect("d/b");
symlink("../d/b", repo.join(".cargo/config.toml")).expect("config");
git(&repo, &["add", "-A"]);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`.cargo/config.toml`"), "{err}");
}
#[cfg(unix)]
#[test]
fn a_config_leaving_the_tree_behind_a_directory_link_is_unverified() {
use std::os::unix::fs::symlink;
for target in ["../../outside", "/outside/config"] {
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
std::fs::create_dir_all(repo.join("conf")).expect("conf");
symlink(target, repo.join("conf/config")).expect("conf/config");
symlink("conf", repo.join(".cargo")).expect(".cargo");
git(&repo, &["add", "-A"]);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("unverified");
assert!(
err.to_string().contains("`.cargo/config`"),
"{target}: {err}"
);
}
}
#[cfg(unix)]
#[test]
fn a_lacked_blob_through_a_link_is_unread_not_absent() {
let _ = in_scratch(&[("app", "app")], |dir: &Path| {
let source = dir.join("source");
std::fs::create_dir_all(source.join("old")).expect("old");
git(&source, &["init", "-q", "-b", "main"]);
std::fs::write(source.join("old/Cargo.toml"), "[workspace]\n").expect("base");
std::os::unix::fs::symlink("old", source.join("rust")).expect("link");
std::fs::write(source.join(".aa"), "sorts first\n").expect("first");
std::fs::create_dir_all(source.join("sub")).expect("sub");
std::os::unix::fs::symlink("..", source.join("sub/up")).expect("up");
git(&source, &["add", "-A"]);
git(&source, &["commit", "-qm", "base"]);
std::fs::write(source.join("old/Cargo.toml"), "[workspace]\n# head\n").expect("head");
git(&source, &["commit", "-qam", "head"]);
git(&source, &["config", "uploadpack.allowFilter", "true"]);
let url = format!("file://{}", source.display());
git(dir, &["clone", "-q", "--filter=blob:none", &url, "clone"]);
let clone = dir.join("clone");
let base = Commit::from_log(&git(&clone, &["rev-parse", "HEAD^"])).expect("base");
let full = Git::at(&source).offline();
assert!(matches!(
full.followed_blob(&base, "sub/up/old/Cargo.toml")
.expect("full"),
super::Followed::File(_)
));
git(
&source,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{},vendor/sub", "1".repeat(40)),
],
);
git(&source, &["commit", "-qm", "submodule"]);
let tip = Commit::from_log(&git(&source, &["rev-parse", "HEAD"])).expect("tip");
assert_eq!(
full.blobs_or_absent(&tip, &["vendor/sub/Cargo.toml"])
.expect("into a submodule"),
[None]
);
for removed in [false, true] {
if removed {
git(&clone, &["remote", "remove", "origin"]);
}
let reader = Git::at(&clone).offline();
let err = reader
.followed_blob(&base, "rust/Cargo.toml")
.expect_err("through the link");
assert!(
err.to_string().contains("reaches `old/Cargo.toml`"),
"{err}"
);
let err = reader
.followed_blob(&base, "sub/up/old/Cargo.toml")
.expect_err("through a link to the root");
assert!(
err.to_string().contains("reaches `old/Cargo.toml`"),
"{err}"
);
let err = reader
.blobs_or_absent(&base, &["old/Cargo.toml"])
.expect_err("in place");
assert_eq!(err.class(), Outcome::Unverified, "{err}");
assert_eq!(
reader
.blobs_or_absent(&base, &["nope.toml"])
.expect("absent"),
[None]
);
}
let head = git(&clone, &["rev-parse", "HEAD"]);
(clone, head)
});
}
#[test]
fn a_roots_base_is_empty_and_a_missing_commit_is_unread() {
let _ = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
git(&repo, &["commit", "-qm", "root"]);
let head = git(&repo, &["rev-parse", "HEAD"]);
git(&repo, &["config", "extensions.partialclone", "origin"]);
let reader = Git::at(&repo).offline();
let root = Commit::from_log(&head).expect("root");
let base = reader.first_parent_or_empty(&root).expect("base");
assert_eq!(
reader
.blobs_or_absent(&base, &["Cargo.toml"])
.expect("read"),
[None]
);
let err = reader
.blobs_or_absent(&Commit("1".repeat(40)), &["Cargo.toml"])
.expect_err("a commit the repository lacks");
assert_eq!(err.class(), Outcome::Unverified, "{err}");
(repo, head)
});
}
#[test]
fn a_root_commits_parent_and_an_empty_workspace_have_no_base() {
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
git(&repo, &["commit", "-qm", "root"]);
let root = Commit::from_log(&git(&repo, &["rev-parse", "HEAD"])).expect("root");
let base = Git::at(&repo).first_parent_or_empty(&root).expect("base");
(repo, base.as_str().to_owned())
});
assert!(read.expect("read").is_none());
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
git(&repo, &["commit", "-qm", "root"]);
(repo, "1".repeat(40))
});
let err = read.expect_err("a commit the repository lacks");
assert_eq!(err.class(), Outcome::Unverified, "{err}");
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = []\n", |_| {})
});
assert!(read.expect("read").is_none());
}
#[test]
fn without_a_base_lock_a_glob_covered_submodule_is_unverified() {
let (read, _, _) = in_scratch(&[("crates/x", "x"), ("vendor/y", "y")], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = [\"vendor/*\"]\n", |repo| {
write_crate(&repo.join("vendor/y"), "y");
std::fs::create_dir_all(repo.join("vendor/x")).expect("submodule dir");
git(repo, &["add", "-A"]);
let commit = "1111111111111111111111111111111111111111";
git(
repo,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{commit},vendor/x"),
],
);
})
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`vendor/x`"), "{err}");
}
#[cfg(unix)]
#[test]
fn a_config_behind_two_links_is_written_whole() {
use std::os::unix::fs::symlink;
let (read, _, _) = in_scratch(&[("app", "app")], |dir: &Path| {
let repo = one_member_repo(dir);
std::fs::create_dir_all(repo.join("conf")).expect("conf");
std::fs::create_dir_all(repo.join("real")).expect("real");
std::fs::write(repo.join("real/settings"), "[registries\n").expect("settings");
symlink("../real/settings", repo.join("conf/config")).expect("conf/config");
symlink("conf", repo.join(".cargo")).expect(".cargo");
git(&repo, &["add", "-A"]);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("cargo refuses the config");
assert!(
err.to_string().contains("could not read the workspace"),
"{err}"
);
}
#[test]
fn a_new_member_beside_an_unrelated_submodule_reads() {
let (read, _, _) = in_scratch(&[("vendor/y", "y"), ("crates/new", "new")], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = [\"vendor/*\"]\n", |repo| {
write_crate(&repo.join("vendor/y"), "y");
std::fs::write(repo.join("Cargo.lock"), LOCK_X_Y).expect("lock");
std::fs::create_dir_all(repo.join("docs/theme")).expect("submodule dir");
git(repo, &["add", "-A"]);
let commit = "1111111111111111111111111111111111111111";
git(
repo,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{commit},docs/theme"),
],
);
})
});
assert_eq!(names(read), ["y"]);
}
#[test]
fn a_submodule_a_glob_covers_is_unverified_when_a_member_is_missing() {
let (read, _, _) = in_scratch(&[("crates/x", "x"), ("vendor/y", "y")], |dir: &Path| {
base_repo(dir, "[workspace]\nmembers = [\"vendor/*\"]\n", |repo| {
write_crate(&repo.join("vendor/y"), "y");
std::fs::write(repo.join("Cargo.lock"), LOCK_X_Y).expect("lock");
std::fs::create_dir_all(repo.join("vendor/x")).expect("submodule dir");
git(repo, &["add", "-A"]);
let commit = "1111111111111111111111111111111111111111";
git(
repo,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{commit},vendor/x"),
],
);
})
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`vendor/x`"), "{err}");
}
#[test]
fn a_workspace_in_a_submodule_at_the_base_is_unverified() {
let (read, _, _) = in_scratch(&[("vendor/lib", "lib")], |dir: &Path| {
let repo = dir.join("repo");
std::fs::create_dir_all(&repo).expect("repo");
git(&repo, &["init", "-q"]);
let commit = "1111111111111111111111111111111111111111";
git(
&repo,
&[
"update-index",
"--add",
"--cacheinfo",
&format!("160000,{commit},vendor/lib"),
],
);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`vendor/lib`"), "{err}");
}
#[cfg(unix)]
#[test]
fn a_toml_path_that_is_not_utf8_is_unverified() {
use std::os::unix::ffi::OsStrExt;
let (read, _, _) = in_scratch(&[], |dir: &Path| {
let repo = one_member_repo(dir);
let blob = git(&repo, &["hash-object", "-w", "app/Cargo.toml"]);
let mut info = format!("100644,{blob},").into_bytes();
info.extend_from_slice(b"app/\xff.toml");
let out = Command::new("git")
.args(["update-index", "--add", "--cacheinfo"])
.arg(std::ffi::OsStr::from_bytes(&info))
.current_dir(&repo)
.output()
.expect("git");
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("is not a UTF-8 path"), "{err}");
}
#[cfg(unix)]
#[test]
fn a_path_cargo_does_not_read_need_not_be_utf8() {
use std::os::unix::ffi::OsStrExt;
let (read, _, _) = in_scratch(&[], |dir: &Path| {
let repo = one_member_repo(dir);
let blob = git(&repo, &["hash-object", "-w", "app/src/lib.rs"]);
let mut info = format!("100644,{blob},").into_bytes();
info.extend_from_slice(b"junk\xff.bin");
let out = Command::new("git")
.args(["update-index", "--add", "--cacheinfo"])
.arg(std::ffi::OsStr::from_bytes(&info))
.current_dir(&repo)
.output()
.expect("git");
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
git(&repo, &["commit", "-qm", "base"]);
let base = git(&repo, &["rev-parse", "HEAD"]);
(repo, base)
});
let workspace = read.expect("read").expect("a workspace");
assert_eq!(workspace.packages().count(), 1);
}
#[cfg(unix)]
#[test]
fn a_link_a_partial_clone_lacks_is_unverified() {
let (read, before, after) = in_scratch(&[], |dir: &Path| {
let source = one_member_repo(dir);
std::fs::rename(source.join("Cargo.toml"), source.join("ws.toml")).expect("move");
std::os::unix::fs::symlink("ws.toml", source.join("Cargo.toml")).expect("link");
git(&source, &["add", "-A"]);
git(&source, &["commit", "-qm", "base"]);
std::fs::copy(source.join("ws.toml"), source.join("ws2.toml")).expect("copy");
std::fs::remove_file(source.join("Cargo.toml")).expect("unlink");
std::os::unix::fs::symlink("ws2.toml", source.join("Cargo.toml")).expect("relink");
git(&source, &["add", "-A"]);
git(&source, &["commit", "-qm", "head"]);
git(&source, &["config", "uploadpack.allowFilter", "true"]);
let url = format!("file://{}", source.display());
git(dir, &["clone", "-q", "--filter=blob:none", &url, "clone"]);
let clone = dir.join("clone");
let base = git(&clone, &["rev-parse", "HEAD^"]);
(clone, base)
});
let err = read.expect_err("unverified");
assert!(err.to_string().contains("`Cargo.toml`"), "{err}");
assert!(
err.to_string().contains("is not in this partial clone"),
"{err}"
);
assert_eq!(after, before);
}
}