use std::collections::HashMap;
use std::ffi::{OsStr, OsString};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::sync::OnceLock;
use gitpoulpi_core::domain::NetworkAuth;
use gitpoulpi_core::Result;
const CONFIG_OVERRIDES: &[&str] = &[
"color.ui=false",
"core.quotepath=false",
"diff.mnemonicprefix=false",
"diff.noprefix=false",
"diff.srcprefix=a/",
"diff.dstprefix=b/",
"log.showsignature=false",
"status.relativepaths=false",
"i18n.logoutputencoding=UTF-8",
];
const CLEARED_ENV: &[&str] = &[
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
"GIT_COMMON_DIR",
"GIT_PREFIX",
"GIT_EXTERNAL_DIFF",
"GIT_PAGER",
"GIT_CONFIG_PARAMETERS",
"GIT_CONFIG_COUNT",
"GIT_LFS_SKIP_SMUDGE",
"GIT_LFS_PROGRESS",
];
const TRANSPORT_ALLOWLIST: &[&str] = &[
"protocol.allow=never",
"protocol.https.allow=always",
"protocol.http.allow=always",
"protocol.ssh.allow=always",
"protocol.git.allow=always",
"protocol.file.allow=user",
];
fn full_args(args: &[&str]) -> Vec<String> {
full_args_with(&[], args)
}
fn full_args_with(extra: &[String], args: &[&str]) -> Vec<String> {
let mut out = Vec::with_capacity(args.len() + CONFIG_OVERRIDES.len() * 2 + extra.len() + 1);
out.push("--no-pager".to_string());
let submodules = args
.iter()
.any(|a| *a == "submodule" || *a == "--recurse-submodules");
for kv in TRANSPORT_ALLOWLIST {
out.push("-c".to_string());
let kv = if submodules && *kv == "protocol.file.allow=user" {
"protocol.file.allow=always"
} else {
kv
};
out.push(kv.to_string());
}
for kv in CONFIG_OVERRIDES {
out.push("-c".to_string());
out.push((*kv).to_string());
}
out.extend(extra.iter().cloned());
out.extend(args.iter().map(|a| (*a).to_string()));
out
}
thread_local! {
static SPAWNS: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}
static TOTAL_SPAWNS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
fn count_spawn() {
SPAWNS.with(|c| c.set(c.get() + 1));
TOTAL_SPAWNS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
}
pub fn total_spawn_count() -> u64 {
TOTAL_SPAWNS.load(std::sync::atomic::Ordering::Relaxed)
}
#[cfg(all(test, feature = "lfs"))]
pub fn spawn_count() -> u64 {
SPAWNS.with(std::cell::Cell::get)
}
pub fn git_program() -> &'static OsStr {
static PROGRAM: OnceLock<OsString> = OnceLock::new();
PROGRAM.get_or_init(resolve_git_program)
}
fn resolve_git_program() -> OsString {
if let Some(p) = std::env::var_os("GITPOULPI_GIT").filter(|p| !p.is_empty()) {
return p;
}
#[cfg(windows)]
if let Some(real) = windows_unshimmed_git(std::env::var_os("PATH").as_deref()) {
return real.into_os_string();
}
OsString::from("git")
}
#[cfg_attr(not(windows), allow(dead_code))]
fn windows_unshimmed_git(path: Option<&OsStr>) -> Option<PathBuf> {
let path = path?;
let shim = std::env::split_paths(path)
.map(|d| d.join("git.exe"))
.find(|c| c.is_file())?;
let dir = shim.parent()?;
if !dir.file_name()?.eq_ignore_ascii_case("cmd") {
return None;
}
let real = dir.parent()?.join("mingw64").join("bin").join("git.exe");
real.is_file().then_some(real)
}
fn child_path() -> Option<&'static OsString> {
static PATH_VAR: OnceLock<Option<OsString>> = OnceLock::new();
PATH_VAR
.get_or_init(|| {
let prog = Path::new(git_program());
let bin = prog.parent()?;
if !bin.ends_with(Path::new("mingw64").join("bin")) {
return None;
}
let root = bin.parent()?.parent()?;
let mut paths = vec![bin.to_path_buf(), root.join("usr").join("bin")];
paths.extend(std::env::split_paths(&std::env::var_os("PATH")?));
std::env::join_paths(paths).ok()
})
.as_ref()
}
fn apply_git_env(cmd: &mut Command) {
if let Some(p) = child_path() {
cmd.env("PATH", p);
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CredentialMode {
#[default]
Helper,
Prompt,
None,
}
static CREDENTIAL_MODE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);
pub fn set_credential_mode(mode: CredentialMode) {
CREDENTIAL_MODE.store(mode as u8, std::sync::atomic::Ordering::Relaxed);
}
pub fn credential_mode() -> CredentialMode {
match CREDENTIAL_MODE.load(std::sync::atomic::Ordering::Relaxed) {
1 => CredentialMode::Prompt,
2 => CredentialMode::None,
_ => CredentialMode::Helper,
}
}
fn credential_settings(mode: CredentialMode) -> (&'static [&'static str], Option<&'static str>) {
match mode {
CredentialMode::Helper => (&[], None),
CredentialMode::Prompt => (&["-c", "credential.helper="], None),
CredentialMode::None => (&["-c", "credential.helper="], Some("0")),
}
}
static SSH_KEY: std::sync::RwLock<Option<String>> = std::sync::RwLock::new(None);
pub fn set_ssh_key(path: Option<String>) {
if let Ok(mut slot) = SSH_KEY.write() {
*slot = path.filter(|p| !p.trim().is_empty());
}
}
pub fn ssh_key_command(repo_root: &Path) -> Option<String> {
let key = SSH_KEY.read().ok()?.clone()?;
let user_env = |name: &str| std::env::var_os(name).is_some_and(|v| !v.is_empty());
if user_env("GIT_SSH_COMMAND") || user_env("GIT_SSH") {
return None;
}
let pinned = git(repo_root, &["config", "--get", "core.sshCommand"])
.output()
.ok()
.filter(|o| o.status.success())
.is_some_and(|o| !String::from_utf8_lossy(&o.stdout).trim().is_empty());
if pinned {
return None;
}
Some(gitpoulpi_core::repo_config::ssh_command_for_key(
&expand_home(&key),
))
}
static NETWORK_AUTH: std::sync::RwLock<Option<HashMap<PathBuf, NetworkAuth>>> =
std::sync::RwLock::new(None);
fn auth_key(repo_root: &Path) -> PathBuf {
std::fs::canonicalize(repo_root).unwrap_or_else(|_| repo_root.to_path_buf())
}
pub(crate) fn set_network_auth(repo_root: &Path, auth: Option<NetworkAuth>) {
let key = auth_key(repo_root);
if let Ok(mut slot) = NETWORK_AUTH.write() {
let map = slot.get_or_insert_with(HashMap::new);
match auth {
Some(auth) => {
map.insert(key, auth);
}
None => {
map.remove(&key);
}
}
}
}
fn network_auth_for(repo_root: &Path) -> Option<NetworkAuth> {
let slot = NETWORK_AUTH.read().ok()?;
let map = slot.as_ref().filter(|m| !m.is_empty())?;
map.get(&auth_key(repo_root)).cloned()
}
fn apply_network_auth(cmd: &mut Command, repo_root: &Path) -> Vec<String> {
let mut args = Vec::new();
let set = |args: &mut Vec<String>, kv: String| {
args.push("-c".to_string());
args.push(kv);
};
let Some(auth) = network_auth_for(repo_root) else {
let (credential_args, prompt) = credential_settings(credential_mode());
args.extend(credential_args.iter().map(|a| (*a).to_string()));
if let Some(prompt) = prompt {
cmd.env("GIT_TERMINAL_PROMPT", prompt);
}
if let Some(ssh) = ssh_key_command(repo_root) {
cmd.env("GIT_SSH_COMMAND", ssh);
}
return args;
};
if let Some(helpers) = &auth.credential_helpers {
set(&mut args, "credential.helper=".to_string());
for helper in helpers {
set(&mut args, format!("credential.helper={helper}"));
}
for (url, helper) in &auth.credential_url_helpers {
set(&mut args, format!("credential.{url}.helper={helper}"));
}
}
if let Some(user) = &auth.credential_username {
set(&mut args, format!("credential.username={user}"));
}
if auth.no_prompt {
set(&mut args, "core.askPass=".to_string());
cmd.env("GIT_TERMINAL_PROMPT", "0");
cmd.env_remove("GIT_ASKPASS");
cmd.env_remove("SSH_ASKPASS");
}
if auth.reset_extra_header {
set(&mut args, "http.extraHeader=".to_string());
}
if let Some(ssh) = &auth.ssh_command {
cmd.env("GIT_SSH_COMMAND", ssh);
}
for (name, value) in &auth.env {
cmd.env(name, value);
}
args
}
pub fn expand_home(path: &str) -> String {
let rest = path.strip_prefix("~/").or_else(|| path.strip_prefix("~\\"));
match (
rest,
std::env::var_os("HOME").or_else(|| std::env::var_os("USERPROFILE")),
) {
(Some(rest), Some(home)) => Path::new(&home).join(rest).display().to_string(),
_ => path.to_string(),
}
}
pub(crate) fn git(repo_root: &Path, args: &[&str]) -> Command {
count_spawn();
let mut cmd = Command::new(git_program());
apply_git_env(&mut cmd);
cmd.args(full_args(args).iter().map(OsStr::new));
cmd.current_dir(repo_root);
for key in CLEARED_ENV {
cmd.env_remove(key);
}
cmd
}
pub(crate) fn git_async(repo_root: &Path, args: &[&str]) -> tokio::process::Command {
count_spawn();
let mut cmd = tokio::process::Command::new(git_program());
if let Some(p) = child_path() {
cmd.env("PATH", p);
}
let network_args = apply_network_auth(cmd.as_std_mut(), repo_root);
cmd.stdin(Stdio::null());
cmd.args(full_args_with(&network_args, args).iter().map(OsStr::new));
cmd.current_dir(repo_root);
for key in CLEARED_ENV {
cmd.env_remove(key);
}
cmd
}
pub(crate) fn failed(args: &[&str], stderr: &str) -> gitpoulpi_core::Error {
failed_with(args, None, stderr)
}
pub(crate) fn failed_with(
args: &[&str],
exit_code: Option<i32>,
stderr: &str,
) -> gitpoulpi_core::Error {
classify_lfs_failure(stderr).unwrap_or_else(|| {
let message = gitpoulpi_core::redact(&format!("git {} failed: {}", args.join(" "), stderr));
gitpoulpi_core::Error::git(
message,
gitpoulpi_core::ErrorDetail::from_git(args, exit_code, stderr),
)
})
}
pub fn classify_lfs_failure(stderr: &str) -> Option<gitpoulpi_core::Error> {
const MISSING: &[&str] = &[
"git-lfs: command not found",
"git-lfs: not found",
"'lfs' is not a git command",
"'git-lfs' was not found on your path",
"cannot run git-lfs",
];
let lower = stderr.to_lowercase();
MISSING
.iter()
.any(|needle| lower.contains(needle))
.then_some(gitpoulpi_core::Error::LfsUnavailable {
reason: gitpoulpi_core::domain::LfsUnavailableReason::Missing,
})
}
pub(crate) fn spawn_failed(e: std::io::Error) -> gitpoulpi_core::Error {
gitpoulpi_core::Error::Backend(format!("failed to spawn git: {e}"))
}
fn utf8_stdout(stdout: Vec<u8>) -> Result<String> {
String::from_utf8(stdout)
.map_err(|e| gitpoulpi_core::Error::Backend(format!("git output was not utf-8: {e}")))
}
fn checked(args: &[&str], output: Output) -> Result<Output> {
if output.status.success() {
Ok(output)
} else {
Err(failed_with(
args,
output.status.code(),
&String::from_utf8_lossy(&output.stderr),
))
}
}
fn status_of(output: &Output) -> Result<StatusOutput> {
let stderr = String::from_utf8_lossy(&output.stderr).into_owned();
if !output.status.success() {
if let Some(e) = classify_lfs_failure(&stderr) {
return Err(e);
}
}
Ok(StatusOutput {
success: output.status.success(),
stderr,
exit_code: output.status.code(),
})
}
pub(crate) fn run_abort(repo_root: &Path, args: &[&str]) -> Result<StatusOutput> {
let english = [("LC_ALL", "C"), ("LANGUAGE", "C")];
let first = run_status_with_env(repo_root, args, &english)?;
if first.success || not_uptodate_paths(&first.stderr).is_empty() {
return Ok(first);
}
let _ = run_status_with_env(repo_root, &["update-index", "-q", "--refresh"], &english);
run_status_with_env(repo_root, args, &english)
}
pub(crate) fn not_uptodate_paths(stderr: &str) -> Vec<String> {
let mut out = Vec::new();
for line in stderr.lines() {
let Some(at) = line.find("Entry '") else {
continue;
};
let rest = &line[at + "Entry '".len()..];
if let Some(end) = rest.find("' not uptodate") {
let path = rest[..end].to_string();
if !out.contains(&path) {
out.push(path);
}
}
}
out
}
pub(crate) fn abort_failed(args: &[&str], out: &StatusOutput) -> gitpoulpi_core::Error {
let paths = not_uptodate_paths(&out.stderr);
if paths.is_empty() {
failed_with(args, out.exit_code, &out.stderr)
} else {
gitpoulpi_core::Error::ConflictRefused(
gitpoulpi_core::domain::ConflictRefusal::LocalChanges { paths },
)
}
}
fn with_env(mut cmd: Command, env: &[(&str, &str)]) -> Command {
for (key, value) in env {
cmd.env(key, value);
}
cmd
}
fn join_writer(writer: std::thread::JoinHandle<std::io::Result<()>>) -> Result<()> {
writer
.join()
.map_err(|_| gitpoulpi_core::Error::Backend("git stdin writer panicked".into()))?
.map_err(|e| gitpoulpi_core::Error::Backend(format!("failed to write git stdin: {e}")))
}
fn run_piped_stdin(
mut cmd: Command,
input: Vec<u8>,
) -> Result<(Output, std::thread::JoinHandle<std::io::Result<()>>)> {
let mut child = cmd
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
.map_err(spawn_failed)?;
let mut pipe = child.stdin.take().expect("stdin was piped");
let writer = std::thread::spawn(move || pipe.write_all(&input));
let output = child
.wait_with_output()
.map_err(|e| gitpoulpi_core::Error::Backend(format!("failed to wait for git: {e}")))?;
Ok((output, writer))
}
pub fn run(repo_root: &Path, args: &[&str]) -> Result<String> {
let output = git(repo_root, args).output().map_err(spawn_failed)?;
utf8_stdout(checked(args, output)?.stdout)
}
pub fn run_in_english(repo_root: &Path, args: &[&str]) -> Result<String> {
let output = git(repo_root, args)
.env("LC_ALL", "C")
.env("LANGUAGE", "C")
.output()
.map_err(spawn_failed)?;
utf8_stdout(checked(args, output)?.stdout)
}
#[cfg(feature = "submodule")]
pub fn run_network(repo_root: &Path, args: &[&str]) -> Result<String> {
count_spawn();
let mut cmd = Command::new(git_program());
apply_git_env(&mut cmd);
let network_args = apply_network_auth(&mut cmd, repo_root);
cmd.args(full_args_with(&network_args, args).iter().map(OsStr::new));
cmd.current_dir(repo_root);
for key in CLEARED_ENV {
cmd.env_remove(key);
}
cmd.stdin(Stdio::null());
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
unsafe {
cmd.pre_exec(|| {
libc::setsid();
Ok(())
});
}
}
let output = cmd.output().map_err(spawn_failed)?;
utf8_stdout(checked(args, output)?.stdout)
}
pub fn run_bytes(repo_root: &Path, args: &[&str]) -> Result<Vec<u8>> {
let output = git(repo_root, args).output().map_err(spawn_failed)?;
Ok(checked(args, output)?.stdout)
}
pub fn run_lenient_bytes(repo_root: &Path, args: &[&str]) -> Result<Vec<u8>> {
let output = git(repo_root, args).output().map_err(spawn_failed)?;
if output.status.code() == Some(1) {
return Ok(output.stdout);
}
Ok(checked(args, output)?.stdout)
}
pub fn run_with_stdin(repo_root: &Path, args: &[&str], stdin: &str) -> Result<String> {
run_with_env_and_stdin(repo_root, args, &[], stdin)
}
pub fn run_with_stdin_bytes(repo_root: &Path, args: &[&str], stdin: &[u8]) -> Result<String> {
let (output, writer) = run_piped_stdin(git(repo_root, args), stdin.to_vec())?;
let output = checked(args, output)?;
join_writer(writer)?;
utf8_stdout(output.stdout)
}
pub fn run_with_env_and_stdin(
repo_root: &Path,
args: &[&str],
env: &[(&str, &str)],
stdin: &str,
) -> Result<String> {
let cmd = with_env(git(repo_root, args), env);
let (output, writer) = run_piped_stdin(cmd, stdin.as_bytes().to_vec())?;
let output = checked(args, output)?;
join_writer(writer)?;
utf8_stdout(output.stdout)
}
pub struct StatusOutput {
pub success: bool,
pub stderr: String,
pub exit_code: Option<i32>,
}
pub fn run_status(repo_root: &Path, args: &[&str]) -> Result<StatusOutput> {
status_of(&git(repo_root, args).output().map_err(spawn_failed)?)
}
pub fn run_status_with_env(
repo_root: &Path,
args: &[&str],
env: &[(&str, &str)],
) -> Result<StatusOutput> {
let output = with_env(git(repo_root, args), env)
.output()
.map_err(spawn_failed)?;
status_of(&output)
}
async fn output_guarded(repo_root: &Path, args: &[&str]) -> Result<Output> {
let mut cmd = git_async(repo_root, args);
crate::tree_kill::captured(&mut cmd);
let cleanup = crate::cancel_cleanup::for_command(repo_root, args);
let (child, mut guard) = crate::tree_kill::spawn_guarded(&mut cmd).map_err(spawn_failed)?;
let output = child.wait_with_output().await.map_err(spawn_failed)?;
guard.disarm();
if let Some(mut cleanup) = cleanup {
cleanup.disarm();
}
Ok(output)
}
pub async fn run_async(repo_root: &Path, args: &[&str]) -> Result<String> {
let output = output_guarded(repo_root, args).await?;
utf8_stdout(checked(args, output)?.stdout)
}
pub struct FullOutput {
pub success: bool,
pub stdout: String,
pub stderr: String,
pub exit_code: Option<i32>,
}
pub async fn run_full_async(repo_root: &Path, args: &[&str]) -> Result<FullOutput> {
let output = output_guarded(repo_root, args).await?;
let status = status_of(&output)?;
Ok(FullOutput {
success: status.success,
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: status.stderr,
exit_code: status.exit_code,
})
}
pub(crate) async fn run_full_async_english(repo_root: &Path, args: &[&str]) -> Result<FullOutput> {
run_full_async_with(repo_root, args, false).await
}
fn verbose_ssh_command(cmd: &tokio::process::Command, repo_root: &Path) -> String {
let chosen = cmd
.as_std()
.get_envs()
.find(|(k, _)| *k == "GIT_SSH_COMMAND")
.and_then(|(_, v)| v.map(|v| v.to_string_lossy().into_owned()));
let base = chosen
.or_else(|| {
std::env::var("GIT_SSH_COMMAND")
.ok()
.filter(|v| !v.is_empty())
})
.or_else(|| {
git(repo_root, &["config", "--get", "core.sshCommand"])
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|v| !v.is_empty())
})
.or_else(|| {
std::env::var("GIT_SSH")
.ok()
.filter(|v| !v.is_empty())
.map(|v| gitpoulpi_core::repo_config::sh_quote_path(&v))
})
.unwrap_or_else(|| "ssh".to_string());
format!("{base} -v")
}
pub(crate) async fn run_full_async_with(
repo_root: &Path,
args: &[&str],
ssh_verbose: bool,
) -> Result<FullOutput> {
let mut cmd = git_async(repo_root, args);
cmd.env("LC_ALL", "C").env("LANGUAGE", "C");
if ssh_verbose {
let ssh = verbose_ssh_command(&cmd, repo_root);
cmd.env("GIT_SSH_COMMAND", ssh);
}
crate::tree_kill::captured(&mut cmd);
let (child, mut guard) = crate::tree_kill::spawn_guarded(&mut cmd).map_err(spawn_failed)?;
let output = child.wait_with_output().await.map_err(spawn_failed)?;
guard.disarm();
let status = status_of(&output)?;
Ok(FullOutput {
success: status.success,
stdout: String::from_utf8_lossy(&output.stdout).into_owned(),
stderr: status.stderr,
exit_code: status.exit_code,
})
}
pub async fn run_streaming_stderr<F>(
repo_root: &Path,
args: &[&str],
mut on_stderr: F,
) -> Result<()>
where
F: FnMut(&str) + Send,
{
use tokio::io::AsyncReadExt;
let mut cmd = git_async(repo_root, args);
cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
let (mut child, mut tree) = crate::tree_kill::spawn_guarded(&mut cmd).map_err(spawn_failed)?;
let mut stdout = child.stdout.take().expect("stdout was piped");
let drain_stdout = tokio::spawn(async move {
let mut sink = Vec::new();
let _ = stdout.read_to_end(&mut sink).await;
});
let mut stderr = child.stderr.take().expect("stderr was piped");
let mut buf = [0u8; 4096];
let mut tail = String::new();
loop {
match stderr.read(&mut buf).await {
Ok(0) | Err(_) => break,
Ok(n) => {
let chunk = String::from_utf8_lossy(&buf[..n]);
tail.push_str(&chunk);
if tail.len() > 8192 {
let cut = tail.len() - 4096;
tail = tail[tail
.char_indices()
.find(|(i, _)| *i >= cut)
.map_or(0, |(i, _)| i)..]
.to_string();
}
on_stderr(&chunk);
}
}
}
let status = child
.wait()
.await
.map_err(|e| gitpoulpi_core::Error::Backend(format!("failed to wait for git: {e}")))?;
let _ = drain_stdout.await;
tree.disarm();
if !status.success() {
return Err(failed_with(args, status.code(), tail.trim()));
}
Ok(())
}
pub async fn run_status_async(repo_root: &Path, args: &[&str]) -> Result<StatusOutput> {
let output = output_guarded(repo_root, args).await?;
status_of(&output)
}
pub fn run_lenient(repo_root: &Path, args: &[&str]) -> Result<(bool, String)> {
let output = git(repo_root, args).output().map_err(spawn_failed)?;
Ok((
output.status.success(),
String::from_utf8_lossy(&output.stdout).into_owned(),
))
}
pub fn run_bytes_with_stdin(repo_root: &Path, args: &[&str], stdin: &[u8]) -> Result<Vec<u8>> {
let (output, writer) = run_piped_stdin(git(repo_root, args), stdin.to_vec())?;
let output = checked(args, output)?;
join_writer(writer)?;
Ok(output.stdout)
}
pub fn run_z_fields(repo_root: &Path, args: &[&str]) -> Result<Vec<String>> {
let raw = run(repo_root, args)?;
Ok(raw
.split('\0')
.filter(|s| !s.is_empty())
.map(str::to_string)
.collect())
}
pub(crate) fn git_version() -> Option<(u32, u32)> {
static VERSION: OnceLock<Option<(u32, u32)>> = OnceLock::new();
*VERSION.get_or_init(|| {
run(Path::new("."), &["--version"])
.ok()
.and_then(|v| parse_git_version(&v))
})
}
pub(crate) fn parse_git_version(text: &str) -> Option<(u32, u32)> {
let mut parts = text.trim().strip_prefix("git version ")?.split('.');
Some((parts.next()?.parse().ok()?, parts.next()?.parse().ok()?))
}
pub(crate) fn git_at_least(major: u32, minor: u32) -> bool {
git_version().is_some_and(|v| v >= (major, minor))
}
pub(crate) struct BatchObject {
pub size: u64,
pub bytes: Option<Vec<u8>>,
}
pub(crate) fn cat_file_batch_capped(
repo_root: &Path,
oids: &[String],
cap: u64,
) -> Result<Vec<BatchObject>> {
use std::io::{BufRead, BufReader, Read};
let mut out = Vec::with_capacity(oids.len());
if oids.is_empty() {
return Ok(out);
}
let mut child = git(repo_root, &["cat-file", "--batch"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
.map_err(spawn_failed)?;
let mut stdin = child.stdin.take().expect("stdin was piped");
let mut stdout = BufReader::new(child.stdout.take().expect("stdout was piped"));
let io_err =
|e: std::io::Error| gitpoulpi_core::Error::Backend(format!("git cat-file --batch: {e}"));
for oid in oids {
writeln!(stdin, "{oid}").map_err(io_err)?;
stdin.flush().map_err(io_err)?;
let mut header = String::new();
stdout.read_line(&mut header).map_err(io_err)?;
let mut words = header.split_whitespace();
let size: u64 = match (words.next(), words.next(), words.next()) {
(Some(_), Some("blob"), Some(size)) => size.parse().map_err(|_| {
gitpoulpi_core::Error::Backend(format!("git cat-file gave `{}`", header.trim()))
})?,
_ => {
return Err(gitpoulpi_core::Error::Backend(format!(
"git cat-file --batch: no blob for {oid} (`{}`)",
header.trim()
)))
}
};
let bytes = if size <= cap {
let mut buf = vec![0u8; size as usize];
stdout.read_exact(&mut buf).map_err(io_err)?;
Some(buf)
} else {
std::io::copy(&mut (&mut stdout).take(size), &mut std::io::sink()).map_err(io_err)?;
None
};
let mut newline = [0u8; 1];
stdout.read_exact(&mut newline).map_err(io_err)?;
out.push(BatchObject { size, bytes });
}
drop(stdin);
let _ = child.wait();
Ok(out)
}
pub(crate) fn cat_file_batch_lenient(
repo_root: &Path,
specs: &[String],
cap: u64,
) -> Vec<Option<Vec<u8>>> {
use std::io::{BufRead, BufReader, Read};
let mut out: Vec<Option<Vec<u8>>> = Vec::with_capacity(specs.len());
if specs.is_empty() {
return out;
}
let Ok(mut child) = git(repo_root, &["cat-file", "--batch"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
.spawn()
else {
return vec![None; specs.len()];
};
let mut stdin = child.stdin.take().expect("stdin was piped");
let mut stdout = BufReader::new(child.stdout.take().expect("stdout was piped"));
let mut ask = |spec: &str| -> std::io::Result<Option<Vec<u8>>> {
writeln!(stdin, "{spec}")?;
stdin.flush()?;
let mut header = String::new();
stdout.read_line(&mut header)?;
let mut words = header.split_whitespace();
let (_, kind, size) = (words.next(), words.next(), words.next());
let (Some(kind), Some(size)) = (kind, size.and_then(|s| s.parse::<u64>().ok())) else {
return Ok(None);
};
let bytes = if kind == "blob" && size <= cap {
let mut buf = vec![0u8; size as usize];
stdout.read_exact(&mut buf)?;
Some(buf)
} else {
std::io::copy(&mut (&mut stdout).take(size), &mut std::io::sink())?;
None
};
let mut newline = [0u8; 1];
stdout.read_exact(&mut newline)?;
Ok(bytes)
};
for spec in specs {
match ask(spec) {
Ok(bytes) => out.push(bytes),
Err(_) => break,
}
}
drop(stdin);
let _ = child.wait();
out.resize(specs.len(), None);
out
}
pub(crate) fn run_merge_file(repo_root: &Path, args: &[&str]) -> Result<Vec<u8>> {
let output = git(repo_root, args).output().map_err(spawn_failed)?;
match output.status.code() {
Some(code) if (0..=127).contains(&code) => Ok(output.stdout),
_ => Err(failed(args, &String::from_utf8_lossy(&output.stderr))),
}
}
const READ_RETRY_PAUSE: std::time::Duration = std::time::Duration::from_millis(100);
pub(crate) fn read_with_retry<T>(op: impl FnMut() -> Result<T>) -> Result<T> {
retry_once(op, || std::thread::sleep(READ_RETRY_PAUSE))
}
fn retry_once<T>(mut op: impl FnMut() -> Result<T>, pause: impl FnOnce()) -> Result<T> {
match op() {
Ok(value) => Ok(value),
Err(_) => {
pause();
op()
}
}
}
#[cfg(test)]
mod retry_tests {
use super::*;
fn died_by_signal() -> gitpoulpi_core::Error {
failed_with(&["for-each-ref"], None, "")
}
#[test]
fn a_read_that_succeeds_runs_once() {
let mut calls = 0;
let mut paused = false;
let out = retry_once(
|| {
calls += 1;
Ok("ok")
},
|| paused = true,
);
assert_eq!(out.unwrap(), "ok");
assert_eq!(calls, 1);
assert!(!paused, "no pause without a failure");
}
#[test]
fn a_read_killed_once_is_retried_and_succeeds() {
let mut calls = 0;
let out = retry_once(
|| {
calls += 1;
if calls == 1 {
Err(died_by_signal())
} else {
Ok("ok")
}
},
|| {},
);
assert_eq!(out.unwrap(), "ok");
assert_eq!(calls, 2);
}
#[test]
fn a_read_that_fails_twice_returns_the_second_error_with_its_detail() {
let mut calls = 0;
let out: Result<()> = retry_once(
|| {
calls += 1;
Err(if calls == 1 {
failed_with(&["for-each-ref"], Some(128), "first")
} else {
failed_with(&["for-each-ref"], None, "free(): double free detected")
})
},
|| {},
);
let err = out.unwrap_err();
assert_eq!(calls, 2);
let detail = err.detail().expect("detail");
assert_eq!(detail.exit_code, None);
assert!(detail.stderr.contains("double free"), "{}", detail.stderr);
}
}
#[cfg(test)]
mod failure_detail_tests {
use super::*;
#[test]
fn the_transport_allowlist_blocks_remote_helpers() {
let dir = tempfile::tempdir().unwrap();
let marker = dir.path().join("ran");
let url = format!("ext::sh -c touch% {}", marker.display());
assert!(run(dir.path(), &["ls-remote", &url]).is_err());
assert!(!marker.exists(), "the helper command must not run");
assert!(full_args(&["fetch"])
.windows(2)
.any(|w| w == ["-c", "protocol.allow=never"]));
}
#[test]
fn a_failed_command_hides_credentials_everywhere() {
let err = failed_with(
&["push", "https://me:topsecret@host/r.git"],
Some(128),
"fatal: Authentication failed for 'https://me:topsecret@host/r.git/'\n",
);
let all = format!("{err} {err:?}");
assert!(!all.contains("topsecret"), "{all}");
let detail = err.detail().expect("detail");
assert_eq!(detail.exit_code, Some(128));
assert_eq!(detail.operation, "push");
assert!(detail.command.as_deref().unwrap().starts_with("git push"));
assert!(detail.stderr.contains("Authentication failed"));
}
}
#[cfg(test)]
mod lfs_classifier_tests {
use super::*;
use gitpoulpi_core::domain::LfsUnavailableReason;
use gitpoulpi_core::Error;
fn is_missing(stderr: &str) -> bool {
matches!(
classify_lfs_failure(stderr),
Some(Error::LfsUnavailable {
reason: LfsUnavailableReason::Missing
})
)
}
#[test]
fn the_recorded_missing_program_messages_are_recognized() {
assert!(is_missing(
"git-lfs filter-process: git-lfs: command not found\nfatal: a.bin: smudge filter lfs failed"
));
assert!(is_missing(
"git: 'lfs' is not a git command. See 'git --help'."
));
assert!(is_missing(
"\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. \
If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory."
));
}
#[test]
fn ordinary_failures_are_not_mistaken_for_a_missing_program() {
assert!(!is_missing("fatal: not a git repository"));
assert!(!is_missing(
"error: pathspec 'lfs' did not match any file(s) known to git"
));
assert!(!is_missing("remote: Git LFS: (0 of 1 files) 404 not found"));
}
#[test]
fn a_failed_command_carries_the_typed_error_or_the_plain_one() {
assert!(matches!(
failed(&["checkout", "x"], "git-lfs: command not found"),
Error::LfsUnavailable { .. }
));
assert!(
matches!(failed(&["checkout", "x"], "boom"), Error::Git { message, .. } if message.contains("checkout x failed: boom"))
);
}
}
#[cfg(test)]
mod resolve_tests {
use super::*;
#[test]
fn cmd_shim_resolves_to_the_mingw_binary_next_to_it() {
let root = tempfile::tempdir().unwrap();
let cmd = root.path().join("cmd");
let bin = root.path().join("mingw64").join("bin");
std::fs::create_dir_all(&cmd).unwrap();
std::fs::create_dir_all(&bin).unwrap();
std::fs::write(cmd.join("git.exe"), "").unwrap();
let path = std::env::join_paths([cmd.clone()]).unwrap();
assert_eq!(windows_unshimmed_git(Some(&path)), None);
std::fs::write(bin.join("git.exe"), "").unwrap();
assert_eq!(
windows_unshimmed_git(Some(&path)),
Some(bin.join("git.exe"))
);
let path = std::env::join_paths([bin]).unwrap();
assert_eq!(windows_unshimmed_git(Some(&path)), None);
}
}
#[cfg(test)]
mod stdin_writer_tests {
use super::*;
#[test]
fn a_stdin_the_child_never_reads_is_reported_not_swallowed() {
let dir = tempfile::tempdir().unwrap();
let input = vec![b'x'; 4 << 20];
assert!(run_bytes_with_stdin(dir.path(), &["--version"], &input).is_err());
assert!(run_with_stdin(dir.path(), &["--version"], &"x".repeat(4 << 20)).is_err());
}
}
#[cfg(test)]
mod network_auth_tests {
use super::*;
fn envs(cmd: &Command) -> Vec<(String, Option<String>)> {
cmd.get_envs()
.map(|(k, v)| {
(
k.to_string_lossy().into_owned(),
v.map(|v| v.to_string_lossy().into_owned()),
)
})
.collect()
}
#[test]
fn an_entry_replaces_the_global_mode_and_sits_before_the_subcommand() {
let dir = tempfile::tempdir().unwrap();
set_network_auth(
dir.path(),
Some(NetworkAuth {
credential_helpers: Some(vec!["!h".into()]),
credential_url_helpers: vec![("https://h".into(), "!u".into())],
credential_username: Some("u".into()),
no_prompt: true,
reset_extra_header: true,
ssh_command: Some("ssh -i k".into()),
env: vec![("GRANT".into(), "g1".into())],
}),
);
let mut cmd = Command::new("git");
let extra = apply_network_auth(&mut cmd, dir.path());
let all = full_args_with(&extra, &["fetch", "origin"]);
let flat = all.join(" ");
assert!(
flat.contains(
"-c credential.helper= -c credential.helper=!h \
-c credential.https://h.helper=!u -c credential.username=u \
-c core.askPass= -c http.extraHeader= fetch origin"
),
"{flat}"
);
assert!(
flat.starts_with("--no-pager -c protocol.allow=never"),
"{flat}"
);
let env = envs(&cmd);
let get = |name: &str| env.iter().find(|(k, _)| k == name).cloned();
assert_eq!(
get("GIT_SSH_COMMAND"),
Some(("GIT_SSH_COMMAND".into(), Some("ssh -i k".into())))
);
assert_eq!(get("GRANT").and_then(|e| e.1), Some("g1".into()));
assert_eq!(
get("GIT_TERMINAL_PROMPT").and_then(|e| e.1),
Some("0".into())
);
assert_eq!(get("GIT_ASKPASS"), Some(("GIT_ASKPASS".into(), None)));
assert_eq!(get("SSH_ASKPASS"), Some(("SSH_ASKPASS".into(), None)));
set_network_auth(dir.path(), None);
let mut cmd = Command::new("git");
let extra = apply_network_auth(&mut cmd, dir.path());
assert!(
!extra.iter().any(|a| a.starts_with("core.askPass")),
"an entry survived its removal: {extra:?}"
);
}
#[test]
fn entries_are_keyed_by_repository_root() {
let (a, b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
set_network_auth(
a.path(),
Some(NetworkAuth {
no_prompt: true,
..NetworkAuth::default()
}),
);
assert!(network_auth_for(a.path()).is_some());
assert!(network_auth_for(b.path()).is_none());
let dotted = a.path().join(".");
assert!(network_auth_for(&dotted).is_some());
set_network_auth(a.path(), None);
assert!(network_auth_for(a.path()).is_none());
}
}