#![cfg(unix)]
use super::repo_read_config_scan::{ScopeEnvironment, content_key, entries_avoid_keys, file_avoids_keys, outer_scopes_avoid_keys};
use super::repo_read_config_text::{CONFIG_TEXT_LIMIT, parse_config_text};
use super::repo_read_diff::{PathFilter, diff_sorted, name_lines, parse_diff_filter, raw_lines, select};
use super::repo_read_index::{INTENT_TO_ADD, IndexEntry, parse_index};
use super::repo_read_layout::{RepoLayout, env_config_avoids, read_layout_with};
use super::repo_read_object::{ObjectKind, from_hex, name_length, to_hex};
use super::repo_read_odb::ObjectDatabase;
use super::repo_read_query_refs::{config_list, for_each_ref, remote_url, rev_parse, symbolic_ref};
use super::repo_read_tree::{FlatEntry, flatten_pair, flatten_tree_shared, parse_commit, subject_of};
use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use std::rc::Rc;
pub struct Query<'a> {
pub arguments: &'a [OsString],
pub directory: Option<&'a Path>,
pub variables: &'a [(OsString, OsString)],
pub input: Option<&'a [u8]>,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Answer {
pub stdout: Vec<u8>,
pub stderr: Vec<u8>,
pub code: i32,
}
pub(super) struct Parsed<'a> {
pub(super) command: &'a str,
pub(super) flags: Vec<&'a str>,
pub(super) operands: Vec<&'a [u8]>,
pub(super) specs: Vec<&'a [u8]>,
}
fn parse<'a>(arguments: &'a [OsString], start: &mut PathBuf) -> Option<Parsed<'a>> {
let mut index: usize = 0;
while index < arguments.len() {
let word: &OsStr = arguments[index].as_os_str();
if word == "-C" {
let target: &OsString = arguments.get(index + 1)?;
if !target.is_empty() {
*start = start.join(target);
}
if !std::fs::metadata(&*start).is_ok_and(|found| { return found.is_dir(); }) {
return None;
}
index += 2;
} else if word == "--no-literal-pathspecs" {
index += 1;
} else {
break;
}
}
let command: &str = arguments.get(index)?.to_str()?;
let mut parsed: Parsed<'a> = Parsed { command, flags: Vec::new(), operands: Vec::new(), specs: Vec::new() };
let mut after_separator: bool = false;
for argument in &arguments[index + 1..] {
let bytes: &[u8] = argument.as_encoded_bytes();
if after_separator {
parsed.specs.push(bytes);
} else if bytes == b"--" {
after_separator = true;
} else if bytes.starts_with(b"-") {
parsed.flags.push(argument.to_str()?);
} else {
parsed.operands.push(bytes);
}
}
return Some(parsed);
}
pub(super) fn flags_are(flags: &[&str], required: &[&str], allowed: &[&str]) -> bool {
return required.iter().all(|flag| { return flags.contains(flag); })
&& flags.iter().all(|flag| { return allowed.contains(flag) || required.contains(flag); })
&& flags.iter().enumerate().all(|(position, flag)| { return !flags[..position].contains(flag); });
}
fn environment(
base: Vec<(OsString, OsString)>,
variables: &[(OsString, OsString)],
) -> (Vec<(OsString, OsString)>, Option<OsString>, Option<OsString>) {
let mut all: Vec<(OsString, OsString)> = base
.into_iter()
.filter(|(name, _)| { return !variables.iter().any(|(added, _)| { return added == name; }); })
.collect();
all.extend(variables.iter().cloned());
let find = |wanted: &str| -> Option<OsString> {
return all.iter().rev().find(|(name, _)| { return name == wanted; }).map(|(_, value)| { return value.clone(); });
};
let index: Option<OsString> = find("GIT_INDEX_FILE");
let objects: Option<OsString> = find("GIT_OBJECT_DIRECTORY");
all.retain(|(name, _)| { return name != "GIT_INDEX_FILE" && name != "GIT_OBJECT_DIRECTORY"; });
return (all, index, objects);
}
fn content_config_is_plain(layout: &RepoLayout, scope: &ScopeEnvironment) -> Option<bool> {
let path: PathBuf = layout.common_dir.join("config");
let bytes: Vec<u8> = super::private_storage::read_regular_file(path.as_path(), CONFIG_TEXT_LIMIT as u64).ok()?;
let entries = parse_config_text(bytes.as_slice())?;
if !entries_avoid_keys(entries.as_slice(), path.as_path(), scope.home.as_deref(), 0, &content_key)? {
return Some(false);
}
if !file_avoids_keys(layout.git_dir.join("config.worktree").as_path(), scope.home.as_deref(), 0, &content_key)? {
return Some(false);
}
return outer_scopes_avoid_keys(scope, &content_key);
}
pub(super) struct Context {
pub(super) layout: RepoLayout,
index_path: PathBuf,
objects: Option<PathBuf>,
pub(super) scope: ScopeEnvironment,
pub(super) variables: Vec<(OsString, OsString)>,
}
impl Context {
pub(super) fn database(&self) -> Option<ObjectDatabase> {
return ObjectDatabase::open(self.layout.common_dir.as_path(), self.objects.as_deref(), self.layout.object_format);
}
fn index(&self) -> Option<Vec<IndexEntry>> {
let bytes: Vec<u8> = super::private_storage::read_regular_file(self.index_path.as_path(), 1 << 30).ok()?;
return parse_index(bytes.as_slice(), self.layout.object_format);
}
fn resolved_index(&self) -> Option<Vec<FlatEntry>> {
let entries: Vec<IndexEntry> = self.index()?;
if entries.iter().any(|entry| { return entry.stage != 0 || entry.extended & INTENT_TO_ADD != 0; }) {
return None;
}
return Some(entries.into_iter().map(|entry| { return FlatEntry { path: entry.path, mode: entry.mode, id: entry.id }; }).collect());
}
fn tree_of(&self, database: &ObjectDatabase, hex: &[u8]) -> Option<Vec<u8>> {
let name: Vec<u8> = from_hex(hex, self.layout.object_format)?;
let object = database.read(name.as_slice())?;
return match object.kind {
ObjectKind::Tree => Some(name),
ObjectKind::Commit => Some(parse_commit(object.bytes.as_slice(), self.layout.object_format)?.tree),
ObjectKind::Blob | ObjectKind::Tag => None,
};
}
fn zero(&self) -> String {
return "0".repeat(self.layout.object_format.hex_length());
}
}
fn list_files(context: &Context, parsed: &Parsed<'_>) -> Option<Answer> {
let unmerged: bool = parsed.flags.contains(&"--unmerged");
let required: [&str; 2] = [if unmerged { "--unmerged" } else { "--stage" }, "-z"];
if !flags_are(&parsed.flags, &required, &["--full-name"]) || !parsed.operands.is_empty() || !context.layout.prefix.is_empty() {
return None;
}
let filter: PathFilter = PathFilter::parse(parsed.specs.as_slice(), true)?;
let mut stdout: Vec<u8> = Vec::new();
for entry in context.index()? {
if (unmerged && entry.stage == 0) || !filter.matches(entry.path.as_slice()) {
continue;
}
stdout.extend_from_slice(format!("{:06o} {} {}\t", entry.mode, to_hex(entry.id.as_slice()), entry.stage).as_bytes());
stdout.extend_from_slice(entry.path.as_slice());
stdout.push(0);
}
return Some(Answer { stdout, stderr: Vec::new(), code: 0 });
}
fn diff_cached(context: &Context, parsed: &Parsed<'_>, porcelain: bool) -> Option<Answer> {
let raw: bool = parsed.flags.contains(&"--raw");
let mut required: Vec<&str> = vec!["--cached", "-z"];
required.push(if raw { "--raw" } else { "--name-only" });
if raw {
required.push("--no-abbrev");
}
let mut statuses: Option<Vec<u8>> = None;
let mut others: Vec<&str> = Vec::new();
for flag in &parsed.flags {
match flag.strip_prefix("--diff-filter=") {
Some(letters) if !porcelain && statuses.is_none() => statuses = Some(parse_diff_filter(letters)?),
Some(_) => return None,
_ => others.push(flag),
}
}
let allowed: &[&str] = if porcelain { &[] } else { &["--no-renames"] };
if (porcelain && raw) || !flags_are(&others, &required, allowed) || parsed.operands.len() != 1 || (porcelain && !parsed.specs.is_empty()) {
return None;
}
let filter: PathFilter = PathFilter::parse(parsed.specs.as_slice(), context.layout.prefix.is_empty())?;
let database: ObjectDatabase = context.database()?;
let tree: Vec<u8> = context.tree_of(&database, parsed.operands[0])?;
let old: Rc<Vec<FlatEntry>> = flatten_tree_shared(&database, tree.as_slice(), context.layout.object_format)?;
let changes = diff_sorted(old.as_slice(), context.resolved_index()?.as_slice())?;
if porcelain && changes.iter().any(|change| { return change.status == b'A'; }) && changes.iter().any(|change| { return change.status == b'D'; }) {
return None;
}
let kept = select(changes, &filter, statuses.as_deref());
let stdout: Vec<u8> = if raw { raw_lines(kept.as_slice(), context.zero().as_str()) } else { name_lines(kept.as_slice()) };
return Some(Answer { stdout, stderr: Vec::new(), code: 0 });
}
fn diff_tree(context: &Context, parsed: &Parsed<'_>) -> Option<Answer> {
let required: [&str; 7] = ["--root", "--no-commit-id", "-r", "-z", "-m", "--no-renames", "--no-abbrev"];
if !flags_are(&parsed.flags, &required, &[]) || parsed.operands.len() != 1 || !parsed.specs.is_empty() {
return None;
}
let database: ObjectDatabase = context.database()?;
let name: Vec<u8> = from_hex(parsed.operands[0], context.layout.object_format)?;
let object = database.read(name.as_slice())?;
if object.kind != ObjectKind::Commit {
return None;
}
let commit = parse_commit(object.bytes.as_slice(), context.layout.object_format)?;
if commit.parents.len() > 1 {
return None;
}
let new: Rc<Vec<FlatEntry>> = flatten_tree_shared(&database, commit.tree.as_slice(), context.layout.object_format)?;
let old: Rc<Vec<FlatEntry>> = match commit.parents.first() {
Some(parent) => {
let parent_object = database.read(parent.as_slice())?;
if parent_object.kind != ObjectKind::Commit {
return None;
}
let parent_tree = parse_commit(parent_object.bytes.as_slice(), context.layout.object_format)?.tree;
flatten_tree_shared(&database, parent_tree.as_slice(), context.layout.object_format)?
}
None => Rc::new(Vec::new()),
};
let changes = diff_sorted(old.as_slice(), new.as_slice())?;
return Some(Answer { stdout: raw_lines(changes.as_slice(), context.zero().as_str()), stderr: Vec::new(), code: 0 });
}
fn diff_tree_names(context: &Context, parsed: &Parsed<'_>) -> Option<Answer> {
if !parsed.specs.is_empty() {
return None;
}
let database: ObjectDatabase = context.database()?;
let (old, new): (Vec<u8>, Vec<u8>) = match parsed.operands.as_slice() {
[pair_old, pair_new] => {
if !flags_are(&parsed.flags, &["-r", "--name-only", "-z"], &["--no-renames"]) {
return None;
}
(context.tree_of(&database, pair_old)?, context.tree_of(&database, pair_new)?)
}
[single] => {
if !flags_are(&parsed.flags, &["--no-commit-id", "--name-only", "-r", "-z"], &[]) {
return None;
}
let name: Vec<u8> = from_hex(single, context.layout.object_format)?;
let object = database.read(name.as_slice())?;
if object.kind != ObjectKind::Commit {
return None;
}
let commit = parse_commit(object.bytes.as_slice(), context.layout.object_format)?;
let [parent] = commit.parents.as_slice() else { return None };
let parent_object = database.read(parent.as_slice())?;
if parent_object.kind != ObjectKind::Commit {
return None;
}
(parse_commit(parent_object.bytes.as_slice(), context.layout.object_format)?.tree, commit.tree)
}
_ => return None,
};
let (before, after): (Vec<FlatEntry>, Vec<FlatEntry>) = flatten_pair(&database, old.as_slice(), new.as_slice(), context.layout.object_format)?;
let changes = diff_sorted(before.as_slice(), after.as_slice())?;
return Some(Answer { stdout: name_lines(changes.as_slice()), stderr: Vec::new(), code: 0 });
}
fn show_subject(context: &Context, parsed: &Parsed<'_>) -> Option<Answer> {
if !flags_are(&parsed.flags, &["--no-patch", "--format=%s"], &[]) || parsed.operands.len() != 1 || !parsed.specs.is_empty() {
return None;
}
let database: ObjectDatabase = context.database()?;
let name: Vec<u8> = from_hex(parsed.operands[0], context.layout.object_format)?;
let object = database.read(name.as_slice())?;
if object.kind != ObjectKind::Commit {
return None;
}
let commit = parse_commit(object.bytes.as_slice(), context.layout.object_format)?;
let mut stdout: Vec<u8> = subject_of(commit.message.as_slice())?;
stdout.push(b'\n');
return Some(Answer { stdout, stderr: Vec::new(), code: 0 });
}
fn batch_objects(context: &Context, parsed: &Parsed<'_>, input: Option<&[u8]>) -> Option<Answer> {
if !flags_are(&parsed.flags, &["--batch"], &[]) || !parsed.operands.is_empty() || !parsed.specs.is_empty() {
return None;
}
let database: ObjectDatabase = context.database()?;
let mut stdout: Vec<u8> = Vec::new();
let text: &[u8] = input?.strip_suffix(b"\n")?;
for line in text.split(|byte| { return *byte == b'\n'; }) {
let name: Vec<u8> = from_hex(line, context.layout.object_format)?;
let object = database.read(name.as_slice())?;
stdout.extend_from_slice(format!("{} {} {}\n", to_hex(name.as_slice()), object.kind.word(), object.bytes.len()).as_bytes());
stdout.extend_from_slice(object.bytes.as_slice());
stdout.push(b'\n');
}
return Some(Answer { stdout, stderr: Vec::new(), code: 0 });
}
pub fn answer(query: &Query<'_>) -> Option<Answer> {
let mut base: Vec<(OsString, OsString)> = std::env::vars_os().collect();
base.extend(query.variables.iter().cloned());
let scope: ScopeEnvironment = ScopeEnvironment::from_variables(base.as_slice());
return answer_with(query, std::env::vars_os().collect(), &scope);
}
pub fn answer_with(query: &Query<'_>, base: Vec<(OsString, OsString)>, scope: &ScopeEnvironment) -> Option<Answer> {
let mut start: PathBuf = match query.directory {
Some(directory) => directory.to_path_buf(),
None => std::env::current_dir().ok()?,
};
let parsed: Parsed<'_> = parse(query.arguments, &mut start)?;
if !matches!(parsed.command, "ls-files" | "diff-index" | "diff" | "diff-tree" | "show" | "config" | "remote" | "rev-parse" | "for-each-ref" | "symbolic-ref" | "cat-file") {
return None;
}
let (variables, index_variable, objects_variable) = environment(base, query.variables);
let layout: RepoLayout = read_layout_with(start.as_path(), variables.as_slice(), scope)?;
let index_path: PathBuf = match index_variable {
Some(file) => start.join(file),
None => layout.git_dir.join("index"),
};
let objects: Option<PathBuf> = objects_variable.map(|directory| { return start.join(directory); });
let content_command: bool = matches!(parsed.command, "ls-files" | "diff-index" | "diff" | "diff-tree" | "show");
if content_command && !content_config_is_plain(&layout, scope)? {
return None;
}
if content_command && !env_config_avoids(variables.as_slice(), &|key: &str| { return content_key(key.to_ascii_lowercase().as_str()); }) {
return None;
}
let context: Context = Context { layout, index_path, objects, scope: scope.clone(), variables };
if name_length(context.layout.object_format) == 0 {
return None;
}
return match parsed.command {
"ls-files" => list_files(&context, &parsed),
"diff-index" => diff_cached(&context, &parsed, false),
"diff" => diff_cached(&context, &parsed, true),
"diff-tree" if parsed.flags.contains(&"--root") => diff_tree(&context, &parsed),
"diff-tree" => diff_tree_names(&context, &parsed),
"show" => show_subject(&context, &parsed),
"config" => config_list(&context, &parsed),
"remote" => remote_url(&context, &parsed),
"for-each-ref" => for_each_ref(&context, &parsed),
"rev-parse" => rev_parse(&context, &parsed),
"symbolic-ref" => symbolic_ref(&context, &parsed),
"cat-file" => batch_objects(&context, &parsed, query.input),
_ => None,
};
}
#[cfg(test)]
#[path = "repo_read_query_tests.rs"]
pub(crate) mod tests;
#[cfg(test)]
#[path = "repo_read_pair_tests.rs"]
mod pair_tests;