use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::process::Command;
use serde::{Deserialize, Serialize};
use crate::diagnostics::{DiagnosticPath, DiagnosticSource};
use crate::ir::{
ExecutionEngine, ExecutionMode, ExecutionOrigin, ExtractionInput, ExtractionMode,
InputFingerprint, KernelProvenance, ParserProvenance, Provenance, ProvenanceActivity,
SourceSpan, TargetReference,
};
use crate::paths::ResolvedRepositoryPaths;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RepositoryObservation {
pub declared: Option<String>,
pub observed: Option<String>,
pub dirty: Option<bool>,
}
pub fn observe_repository(
repository: &ResolvedRepositoryPaths,
declared: Option<&str>,
) -> RepositoryObservation {
let mut result = RepositoryObservation {
declared: declared.map(str::to_owned),
observed: None,
dirty: None,
};
if !repository.path.join(".git").exists() {
return result;
}
let Some(root) = git(repository, &["rev-parse", "--show-toplevel"]) else {
return result;
};
let Ok(root) = String::from_utf8(root) else {
return result;
};
if fs::canonicalize(root.trim_end_matches('\n')).ok().as_ref() != Some(&repository.path) {
return result;
}
result.observed = git(repository, &["rev-parse", "--verify", "HEAD^{commit}"])
.and_then(|bytes| String::from_utf8(bytes).ok())
.map(|value| value.trim_end().to_owned())
.filter(|value| !value.is_empty() && value.bytes().all(|byte| byte.is_ascii_hexdigit()));
if !can_observe_dirty_state(repository) {
return result;
}
result.dirty = git(
repository,
&[
"status",
"--porcelain=v1",
"-z",
"--untracked-files=all",
"--ignore-submodules=all",
],
)
.map(|bytes| !bytes.is_empty());
result
}
fn git(repository: &ResolvedRepositoryPaths, args: &[&str]) -> Option<Vec<u8>> {
git_command(repository, args)
.output()
.ok()
.filter(|output| output.status.success())
.map(|output| output.stdout)
}
fn can_observe_dirty_state(repository: &ResolvedRepositoryPaths) -> bool {
let Ok(filters) = git_command(
repository,
&[
"config",
"--includes",
"--null",
"--name-only",
"--get-regexp",
"^filter\\.",
],
)
.output() else {
return false;
};
if filters.status.code() != Some(1) || !filters.stdout.is_empty() || !filters.stderr.is_empty()
{
return false;
}
let Some(index) = git(repository, &["ls-files", "--stage", "-z"]) else {
return false;
};
!index
.split(|byte| *byte == 0)
.any(|entry| entry.starts_with(b"160000 "))
}
fn git_command(repository: &ResolvedRepositoryPaths, args: &[&str]) -> Command {
let mut command = Command::new("git");
command
.arg("--no-optional-locks")
.arg("-C")
.arg(&repository.path)
.args([
"-c",
"core.fsmonitor=false",
"-c",
"core.untrackedCache=false",
])
.args(args)
.env("GIT_NO_LAZY_FETCH", "1")
.env("GIT_CONFIG_NOSYSTEM", "1")
.env(
"GIT_CONFIG_GLOBAL",
if cfg!(windows) { "NUL" } else { "/dev/null" },
);
for name in [
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_COMMON_DIR",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_CONFIG",
"GIT_CONFIG_COUNT",
"GIT_CONFIG_PARAMETERS",
] {
command.env_remove(name);
}
command
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BuiltinExtractor {
Rust,
Julia,
Python,
R,
}
impl BuiltinExtractor {
pub const fn as_str(self) -> &'static str {
match self {
Self::Rust => "rust",
Self::Julia => "julia",
Self::Python => "python",
Self::R => "r",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtractionObservation {
pub target: TargetReference,
pub extractor: BuiltinExtractor,
pub capabilities: BTreeSet<String>,
pub parsers: BTreeMap<String, ParserProvenance>,
pub inputs: BTreeMap<String, BTreeMap<DiagnosticPath, ExtractionInput>>,
}
impl ExtractionObservation {
pub fn into_provenance(
self,
source: Option<DiagnosticSource>,
span: Option<SourceSpan>,
) -> Provenance {
let mut tools = BTreeMap::from([
("diplodocus".into(), env!("CARGO_PKG_VERSION").into()),
(
self.extractor.as_str().into(),
env!("CARGO_PKG_VERSION").into(),
),
]);
tools.extend(
self.parsers
.iter()
.map(|(name, parser)| (name.clone(), parser.version.clone())),
);
Provenance {
source,
span,
tools,
activity: ProvenanceActivity::Extraction {
target: self.target,
mode: if self.extractor == BuiltinExtractor::Rust {
ExtractionMode::Compiler
} else {
ExtractionMode::Static
},
capabilities: self.capabilities,
parsers: self.parsers,
inputs: self.inputs,
},
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExecutionObservation {
pub engine: ExecutionEngine,
pub kernel: KernelProvenance,
pub origin: ExecutionOrigin,
pub tools: BTreeMap<String, String>,
pub declared_environment_inputs: Vec<InputFingerprint>,
}
impl ExecutionObservation {
pub fn into_provenance(
mut self,
source: Option<DiagnosticSource>,
span: Option<SourceSpan>,
) -> Provenance {
self.declared_environment_inputs.sort_by(|left, right| {
(&left.source.repository, &left.source.path)
.cmp(&(&right.source.repository, &right.source.path))
});
Provenance {
source,
span,
tools: self.tools,
activity: ProvenanceActivity::Execution {
mode: ExecutionMode::Execute,
engine: self.engine,
kernel: self.kernel,
origin: self.origin,
declared_environment_inputs: self.declared_environment_inputs,
},
}
}
}