use std::error::Error;
use std::ffi::OsString;
use std::fmt::{self, Display, Formatter};
use std::io::{self, Read};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
use std::time::{Duration, Instant};
use crate::domain::{CommitBaseline, RepositoryRoot};
use crate::git::{GitCommand, OutputCompleteness};
const MAX_STDOUT_BYTES: usize = 8 * 1024 * 1024;
const MAX_STDERR_BYTES: usize = 64 * 1024;
const MAX_COMMAND_DURATION: Duration = Duration::from_secs(30);
const CHILD_POLL_INTERVAL: Duration = Duration::from_millis(2);
const INITIAL_CAPTURE_CAPACITY: usize = 64 * 1024;
const READ_BUFFER_BYTES: usize = 16 * 1024;
type ReaderResult = io::Result<CommandStream>;
type ReaderHandle = thread::JoinHandle<ReaderResult>;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum CommandStatus {
Succeeded(Option<i32>),
Failed(Option<i32>),
}
impl CommandStatus {
const fn from_parts(success: bool, code: Option<i32>) -> Self {
if success {
Self::Succeeded(code)
} else {
Self::Failed(code)
}
}
const fn succeeded(self) -> bool {
matches!(self, Self::Succeeded(_))
}
const fn code(self) -> Option<i32> {
match self {
Self::Succeeded(code) | Self::Failed(code) => code,
}
}
}
impl From<ExitStatus> for CommandStatus {
fn from(status: ExitStatus) -> Self {
Self::from_parts(status.success(), status.code())
}
}
#[derive(Clone, Debug)]
pub enum CommandStream {
Complete(Vec<u8>),
Truncated(Vec<u8>),
}
impl CommandStream {
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
match self {
Self::Complete(bytes) | Self::Truncated(bytes) => bytes,
}
}
#[must_use]
pub const fn is_truncated(&self) -> bool {
matches!(self, Self::Truncated(_))
}
}
#[derive(Debug)]
pub struct CommandOutput {
status: CommandStatus,
stdout: CommandStream,
stderr: CommandStream,
}
impl CommandOutput {
#[must_use]
pub fn new(status: ExitStatus, stdout: CommandStream, stderr: CommandStream) -> Self {
Self {
status: status.into(),
stdout,
stderr,
}
}
#[cfg(test)]
pub(crate) fn for_test(
success: bool,
code: Option<i32>,
stdout: Vec<u8>,
stderr: Vec<u8>,
) -> Self {
Self {
status: CommandStatus::from_parts(success, code),
stdout: CommandStream::Complete(stdout),
stderr: CommandStream::Complete(stderr),
}
}
#[must_use]
pub const fn success(&self) -> bool {
self.status.succeeded()
}
#[must_use]
pub const fn code(&self) -> Option<i32> {
self.status.code()
}
#[must_use]
pub fn stdout(&self) -> &[u8] {
self.stdout.as_bytes()
}
#[must_use]
pub fn stderr(&self) -> &[u8] {
self.stderr.as_bytes()
}
#[must_use]
pub const fn stdout_truncated(&self) -> bool {
self.stdout.is_truncated()
}
#[must_use]
pub const fn stderr_truncated(&self) -> bool {
self.stderr.is_truncated()
}
}
#[non_exhaustive]
#[derive(Debug)]
pub enum GitError {
Io {
operation: &'static str,
source: io::Error,
},
CommandFailed {
operation: &'static str,
code: Option<i32>,
stderr: String,
},
OutputLimit {
operation: &'static str,
},
TimedOut {
operation: &'static str,
},
Parse {
context: &'static str,
detail: String,
},
Unsupported(String),
}
impl GitError {
#[must_use]
pub fn parse(context: &'static str, detail: impl Into<String>) -> Self {
Self::Parse {
context,
detail: detail.into(),
}
}
}
impl Display for GitError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Io { operation, .. } => write!(formatter, "could not {operation}"),
Self::CommandFailed {
operation,
code,
stderr,
} => {
write!(formatter, "could not {operation} (exit {code:?})")?;
if !stderr.trim().is_empty() {
write!(formatter, ": {}", stderr.trim())?;
}
Ok(())
}
Self::OutputLimit { operation } => {
write!(formatter, "{operation} exceeded the output limit")
}
Self::TimedOut { operation } => {
write!(
formatter,
"{operation} exceeded the {} second time limit",
MAX_COMMAND_DURATION.as_secs()
)
}
Self::Parse { context, detail } => {
write!(formatter, "could not parse {context}: {detail}")
}
Self::Unsupported(detail) => formatter.write_str(detail),
}
}
}
impl Error for GitError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
Self::Io { source, .. } => Some(source),
Self::CommandFailed { .. }
| Self::OutputLimit { .. }
| Self::TimedOut { .. }
| Self::Parse { .. }
| Self::Unsupported(_) => None,
}
}
}
pub trait GitRunner: Send + Sync + 'static {
fn run(
&self,
root: Option<&RepositoryRoot>,
command: &GitCommand,
) -> Result<CommandOutput, GitError>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SystemGitRunner;
impl GitRunner for SystemGitRunner {
fn run(
&self,
root: Option<&RepositoryRoot>,
command: &GitCommand,
) -> Result<CommandOutput, GitError> {
let mut process = build_process(root, command);
process.stdout(Stdio::piped()).stderr(Stdio::piped());
let mut child = process.spawn().map_err(|source| GitError::Io {
operation: command.kind(),
source,
})?;
let stdout = child.stdout.take().ok_or_else(|| GitError::Io {
operation: command.kind(),
source: io::Error::other("Git stdout pipe was unavailable"),
})?;
let stderr = child.stderr.take().ok_or_else(|| GitError::Io {
operation: command.kind(),
source: io::Error::other("Git stderr pipe was unavailable"),
})?;
let exceeded = Arc::new(AtomicBool::new(false));
let stdout_exceeded = Arc::clone(&exceeded);
let stderr_exceeded = Arc::clone(&exceeded);
let stdout_reader = match spawn_reader(
"chronogit-git-stdout",
stdout,
MAX_STDOUT_BYTES,
stdout_exceeded,
) {
Ok(reader) => reader,
Err(source) => {
stop_child(&mut child);
return Err(GitError::Io {
operation: command.kind(),
source,
});
}
};
let stderr_reader = match spawn_reader(
"chronogit-git-stderr",
stderr,
MAX_STDERR_BYTES,
stderr_exceeded,
) {
Ok(reader) => reader,
Err(source) => {
stop_child(&mut child);
let _ignored = stdout_reader.join();
return Err(GitError::Io {
operation: command.kind(),
source,
});
}
};
let status = wait_with_limit(&mut child, &exceeded, command.kind(), MAX_COMMAND_DURATION);
let stdout = join_reader(stdout_reader, command.kind())?;
let stderr = join_reader(stderr_reader, command.kind())?;
Ok(CommandOutput::new(status?, stdout, stderr))
}
}
fn build_process(root: Option<&RepositoryRoot>, command: &GitCommand) -> Command {
let mut process = Command::new("git");
process
.arg("--no-pager")
.arg("--literal-pathspecs")
.arg("-c")
.arg("color.ui=false")
.arg("-c")
.arg("core.quotePath=false")
.arg("-c")
.arg("core.fsmonitor=false")
.env("GIT_OPTIONAL_LOCKS", "0")
.env("GIT_TERMINAL_PROMPT", "0")
.env("GIT_PAGER", "cat")
.env("PAGER", "cat");
if let Some(root) = root {
process.arg("-C").arg(root.as_path());
}
match command {
GitCommand::LocalBranches => {
process.args([
"for-each-ref",
"--sort=refname",
"--format=%(HEAD)%00%(refname)",
"refs/heads/",
]);
}
GitCommand::SwitchBranch { branch } => {
process
.args([
"-c",
"core.hooksPath=/dev/null",
"switch",
"--no-guess",
"--no-recurse-submodules",
"--",
])
.arg(branch.as_os_str());
}
GitCommand::Discover { start } => {
process
.arg("-C")
.arg(start)
.args(["rev-parse", "--show-toplevel"]);
}
GitCommand::IsBare => {
process.args(["rev-parse", "--is-bare-repository"]);
}
GitCommand::HasHead => {
process.args(["rev-parse", "--verify", "--quiet", "HEAD"]);
}
GitCommand::Status => {
process.args(["status", "--porcelain=v2", "-z", "--untracked-files=all"]);
}
GitCommand::WorktreeDiff { path } => {
process.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--find-renames",
"--find-copies",
"--",
]);
process.arg(path.to_os_string());
}
GitCommand::UntrackedDiff { path } => {
process.args([
"diff",
"--no-index",
"--no-ext-diff",
"--no-textconv",
"--",
"/dev/null",
]);
process.arg(path.to_os_string());
}
GitCommand::Commits { page } => {
process
.arg("log")
.arg("-z")
.arg("--date=iso-strict")
.arg("--topo-order")
.arg("--format=%H%x00%P%x00%an%x00%aI%x00%s")
.arg(format!("--skip={}", page.skip()))
.arg(format!("--max-count={}", page.limit()));
}
GitCommand::RepositoryFiles => {
process.args([
"ls-files",
"-z",
"--cached",
"--others",
"--exclude-standard",
"--",
]);
}
GitCommand::Grep { query } => {
process.args([
"grep",
"--untracked",
"--exclude-standard",
"-z",
"-n",
"-I",
"-F",
"-e",
]);
process.arg(query).arg("--");
}
GitCommand::FileHistory { path, limit } => {
process
.arg("log")
.arg("-z")
.arg("--date=iso-strict")
.arg("--format=%H%x00%P%x00%an%x00%aI%x00%s")
.arg(format!("--max-count={limit}"))
.arg("--")
.arg(path.to_os_string());
}
GitCommand::RevisionFile { revision, path } => {
let mut object = OsString::from(revision.as_str());
object.push(":");
object.push(path.to_os_string());
process.args(["show", "--format="]).arg(object);
}
GitCommand::CommitMessage { commit } => {
process.args(["show", "-s", "--format=%B", commit.as_str()]);
}
GitCommand::ChangedFiles { commit, baseline } => match baseline {
CommitBaseline::EmptyTree => {
process.args([
"diff-tree",
"--root",
"--no-commit-id",
"--name-status",
"-r",
"-z",
"--find-renames",
"--find-copies",
commit.as_str(),
"--",
]);
}
CommitBaseline::FirstParent(parent) => {
process.args([
"diff",
"--name-status",
"-z",
"--find-renames",
"--find-copies",
parent.as_str(),
commit.as_str(),
"--",
]);
}
},
GitCommand::CommitDiff {
commit,
baseline,
path,
} => {
match baseline {
CommitBaseline::EmptyTree => {
process.args([
"show",
"--format=",
"--no-ext-diff",
"--no-textconv",
"--find-renames",
"--find-copies",
commit.as_str(),
"--",
]);
}
CommitBaseline::FirstParent(parent) => {
process.args([
"diff",
"--no-ext-diff",
"--no-textconv",
"--find-renames",
"--find-copies",
parent.as_str(),
commit.as_str(),
"--",
]);
}
}
process.arg(path.to_os_string());
}
GitCommand::TreeEntries { treeish } => {
process.args(["ls-tree", "-z", treeish.as_str()]);
}
}
process
}
fn read_limited<R: Read>(mut reader: R, limit: usize, exceeded: &AtomicBool) -> ReaderResult {
let mut stored = Vec::with_capacity(limit.min(INITIAL_CAPTURE_CAPACITY));
let mut buffer = [0_u8; READ_BUFFER_BYTES];
let mut completeness = OutputCompleteness::Complete;
loop {
let read = reader.read(&mut buffer)?;
if read == 0 {
break;
}
let remaining = limit.saturating_sub(stored.len());
let keep = remaining.min(read);
stored.extend_from_slice(&buffer[..keep]);
if keep < read {
completeness = OutputCompleteness::Truncated;
exceeded.store(true, Ordering::Release);
}
}
Ok(match completeness {
OutputCompleteness::Complete => CommandStream::Complete(stored),
OutputCompleteness::Truncated => CommandStream::Truncated(stored),
})
}
fn spawn_reader<R: Read + Send + 'static>(
name: &'static str,
reader: R,
limit: usize,
exceeded: Arc<AtomicBool>,
) -> io::Result<ReaderHandle> {
thread::Builder::new()
.name(name.to_owned())
.spawn(move || read_limited(reader, limit, &exceeded))
}
fn stop_child(child: &mut std::process::Child) {
let _ignored = child.kill();
let _ignored = child.wait();
}
fn wait_with_limit(
child: &mut std::process::Child,
exceeded: &AtomicBool,
operation: &'static str,
timeout: Duration,
) -> Result<ExitStatus, GitError> {
let started = Instant::now();
loop {
if exceeded.load(Ordering::Acquire) {
let _ignored = child.kill();
return child
.wait()
.map_err(|source| GitError::Io { operation, source });
}
if started.elapsed() >= timeout {
let _ignored = child.kill();
child
.wait()
.map_err(|source| GitError::Io { operation, source })?;
return Err(GitError::TimedOut { operation });
}
if let Some(status) = child
.try_wait()
.map_err(|source| GitError::Io { operation, source })?
{
return Ok(status);
}
thread::sleep(CHILD_POLL_INTERVAL);
}
}
fn join_reader(handle: ReaderHandle, operation: &'static str) -> Result<CommandStream, GitError> {
handle
.join()
.map_err(|_| GitError::Io {
operation,
source: io::Error::other("Git output reader panicked"),
})?
.map_err(|source| GitError::Io { operation, source })
}
#[cfg(test)]
mod tests {
use std::process::Command;
use std::sync::atomic::AtomicBool;
use std::time::Duration;
use super::{GitError, wait_with_limit};
#[cfg(unix)]
#[test]
fn wait_with_limit_terminates_a_slow_child() {
let mut child = Command::new("sleep")
.arg("10")
.spawn()
.unwrap_or_else(|error| panic!("failed to start sleep fixture: {error}"));
let exceeded = AtomicBool::new(false);
let result = wait_with_limit(
&mut child,
&exceeded,
"run timeout fixture",
Duration::from_millis(10),
);
assert!(matches!(result, Err(GitError::TimedOut { .. })));
}
}