use std::ffi::OsStr;
use std::fmt::Display;
use std::path::Path;
use std::process::Output;
use std::process::{CommandArgs, CommandEnvs, ExitStatus, Stdio};
use std::sync::LazyLock;
use owo_colors::OwoColorize;
use prek_consts::env_vars::EnvVars;
use thiserror::Error;
use tracing::trace;
use crate::git::GIT;
static LOG_TRUNCATE_LIMIT: LazyLock<usize> = LazyLock::new(|| {
EnvVars::var(EnvVars::PREK_LOG_TRUNCATE_LIMIT)
.ok()
.and_then(|limit| limit.parse::<usize>().ok())
.filter(|limit| *limit > 0)
.unwrap_or(120)
});
#[derive(Debug, Error)]
pub enum Error {
#[error("Run command `{summary}` failed")]
Exec {
summary: String,
#[source]
cause: std::io::Error,
},
#[error("Command `{summary}` exited with an error:\n{error}")]
Status { summary: String, error: StatusError },
#[cfg(not(windows))]
#[error("Failed to open pty")]
Pty(#[from] prek_pty::Error),
#[error("Failed to setup subprocess for pty")]
PtySetup(#[from] std::io::Error),
}
#[derive(Debug)]
pub struct StatusError {
pub status: ExitStatus,
pub output: Option<Output>,
}
impl Display for StatusError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "\n{}\n{}", "[status]".red(), self.status)?;
if let Some(output) = &self.output {
let stdout = String::from_utf8_lossy(&output.stdout);
let stderr = String::from_utf8_lossy(&output.stderr);
let stdout = stdout
.split('\n')
.filter_map(|line| {
let line = line.trim();
if line.is_empty() { None } else { Some(line) }
})
.collect::<Vec<_>>();
let stderr = stderr
.split('\n')
.filter_map(|line| {
let line = line.trim();
if line.is_empty() { None } else { Some(line) }
})
.collect::<Vec<_>>();
if !stdout.is_empty() {
writeln!(f, "\n{}\n{}", "[stdout]".red(), stdout.join("\n"))?;
}
if !stderr.is_empty() {
writeln!(f, "\n{}\n{}", "[stderr]".red(), stderr.join("\n"))?;
}
}
Ok(())
}
}
pub struct Cmd {
pub inner: tokio::process::Command,
summary: String,
check_status: bool,
}
impl Cmd {
pub fn new(command: impl AsRef<OsStr>, summary: impl Into<String>) -> Self {
let inner = tokio::process::Command::new(command);
Self {
summary: summary.into(),
inner,
check_status: true,
}
}
}
impl Cmd {
pub fn stdout_to_stderr(&mut self) -> &mut Self {
self.inner.stdout(std::io::stderr());
self
}
pub fn check(&mut self, checked: bool) -> &mut Self {
self.check_status = checked;
self
}
}
impl Cmd {
pub async fn run(&mut self) -> Result<(), Error> {
self.status().await?;
Ok(())
}
pub fn spawn(&mut self) -> Result<tokio::process::Child, Error> {
self.log_command();
self.inner.spawn().map_err(|cause| Error::Exec {
summary: self.summary.clone(),
cause,
})
}
pub async fn output(&mut self) -> Result<Output, Error> {
self.log_command();
let output = self.inner.output().await.map_err(|cause| Error::Exec {
summary: self.summary.clone(),
cause,
})?;
self.maybe_check_output(&output)?;
Ok(output)
}
#[cfg(windows)]
pub async fn pty_output(&mut self) -> Result<Output, Error> {
self.output().await
}
#[cfg(not(windows))]
pub async fn pty_output(&mut self) -> Result<Output, Error> {
if !*crate::run::USE_COLOR {
return self.output().await;
}
self.pty_output_inner().await
}
#[cfg(not(windows))]
async fn pty_output_inner(&mut self) -> Result<Output, Error> {
use tokio::io::AsyncReadExt;
let (mut pty, pts) = prek_pty::open()?;
let (_, stdout, stderr) = pts.setup_subprocess()?;
self.inner.stdin(Stdio::null());
self.inner.stdout(stdout);
self.inner.stderr(stderr);
let mut child = self.spawn()?;
let mut stdout = Vec::new();
let mut buffer = [0u8; 4096];
let status = loop {
tokio::select! {
read_result = pty.read(&mut buffer) => {
match read_result {
Ok(0) => {
break child.wait().await?;
}
Ok(n) => {
stdout.extend_from_slice(&buffer[..n]);
}
Err(e) => {
if let Ok(Some(status)) = child.try_wait() {
break status;
}
return Err(Error::PtySetup(e));
}
}
}
status = child.wait() => {
let status = status?;
drain_ready_pty(&mut pty, &mut stdout, &mut buffer).await?;
break status;
}
}
};
child.stdin.take();
child.stdout.take();
child.stderr.take();
let output = Output {
status,
stdout,
stderr: Vec::new(),
};
self.maybe_check_output(&output)?;
Ok(output)
}
pub async fn status(&mut self) -> Result<ExitStatus, Error> {
self.log_command();
let status = self.inner.status().await.map_err(|cause| Error::Exec {
summary: self.summary.clone(),
cause,
})?;
self.maybe_check_status(status)?;
Ok(status)
}
}
#[cfg(not(windows))]
async fn drain_ready_pty(
pty: &mut prek_pty::Pty,
stdout: &mut Vec<u8>,
buffer: &mut [u8; 4096],
) -> Result<(), Error> {
use tokio::io::AsyncReadExt;
use tokio::time::{Duration, timeout};
loop {
match timeout(Duration::from_millis(20), pty.read(buffer)).await {
Ok(Ok(0)) => return Ok(()),
Ok(Ok(n)) => stdout.extend_from_slice(&buffer[..n]),
Err(_) => return Ok(()),
Ok(Err(err)) if err.kind() == std::io::ErrorKind::WouldBlock => return Ok(()),
Ok(Err(err)) => return Err(Error::PtySetup(err)),
}
}
}
impl Cmd {
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
self.inner.arg(arg);
self
}
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
self.inner.args(args);
self
}
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.inner.env(key, val);
self
}
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
V: AsRef<OsStr>,
{
self.inner.envs(vars);
self
}
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self {
self.inner.env_remove(key);
self
}
pub fn env_clear(&mut self) -> &mut Self {
self.inner.env_clear();
self
}
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
self.inner.current_dir(dir);
self
}
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.inner.stdin(cfg);
self
}
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.inner.stdout(cfg);
self
}
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.inner.stderr(cfg);
self
}
pub fn get_program(&self) -> &OsStr {
self.inner.as_std().get_program()
}
pub fn get_args(&self) -> CommandArgs<'_> {
self.inner.as_std().get_args()
}
pub fn get_envs(&self) -> CommandEnvs<'_> {
self.inner.as_std().get_envs()
}
pub fn get_current_dir(&self) -> Option<&Path> {
self.inner.as_std().get_current_dir()
}
pub fn remove_git_envs(&mut self) -> &mut Self {
for (key, _) in crate::git::GIT_ENV_TO_REMOVE.iter() {
self.inner.env_remove(key);
}
self
}
}
impl Cmd {
pub fn check_status(&self, status: ExitStatus) -> Result<(), Error> {
if status.success() {
Ok(())
} else {
Err(Error::Status {
summary: self.summary.clone(),
error: StatusError {
status,
output: None,
},
})
}
}
pub fn check_output(&self, output: &Output) -> Result<(), Error> {
if output.status.success() {
Ok(())
} else {
Err(Error::Status {
summary: self.summary.clone(),
error: StatusError {
status: output.status,
output: Some(output.clone()),
},
})
}
}
pub fn maybe_check_status(&self, status: ExitStatus) -> Result<(), Error> {
if self.check_status {
self.check_status(status)?;
}
Ok(())
}
pub fn maybe_check_output(&self, output: &Output) -> Result<(), Error> {
if self.check_status {
self.check_output(output)?;
}
Ok(())
}
pub fn log_command(&self) {
trace!("Executing `{self}`");
}
}
fn skip_args(cmd: &OsStr, cur: &OsStr, next: Option<&&OsStr>) -> usize {
if GIT.as_ref().is_ok_and(|git| cmd == git) {
if cur == "-c" {
if let Some(flag) = next {
let flag = flag.as_encoded_bytes();
if flag.starts_with(b"core.useBuiltinFSMonitor")
|| flag.starts_with(b"protocol.version")
{
return 2;
}
}
} else if cur == "--no-ext-diff"
|| cur == "--no-textconv"
|| cur == "--ignore-submodules"
|| cur == "--no-color"
{
return 1;
}
}
0
}
impl Display for Cmd {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if let Some(cwd) = self.get_current_dir() {
write!(f, "cd {} && ", cwd.to_string_lossy())?;
}
let program = self.get_program();
let mut args = self.get_args().peekable();
write!(f, "{}", program.to_string_lossy())?;
if args.peek().is_some_and(|arg| *arg == program) {
args.next(); }
let mut len = 0;
while let Some(arg) = args.next() {
let skip = skip_args(program, arg, args.peek());
if skip > 0 {
for _ in 1..skip {
args.next();
}
continue;
}
write!(f, " {}", arg.to_string_lossy())?;
len += arg.len() + 1;
if len > *LOG_TRUNCATE_LIMIT {
write!(f, " [...]")?;
break;
}
}
Ok(())
}
}
#[cfg(all(test, not(windows)))]
mod tests {
use super::Cmd;
#[tokio::test]
async fn pty_output_captures_trailing_output_after_fast_exit() {
for _ in 0..20 {
let output = Cmd::new("/bin/sh", "pty trailing output test")
.arg("-c")
.arg("printf 'FINAL\\n'")
.check(false)
.pty_output_inner()
.await
.expect("pty command should succeed");
assert!(output.status.success());
let stdout = String::from_utf8_lossy(&output.stdout).replace("\r\n", "\n");
assert_eq!(stdout, "FINAL\n");
assert!(output.stderr.is_empty());
}
}
}