Merge local-only Git transport base

This commit is contained in:
Chris Bookholt
2026-06-30 11:59:24 -07:00
16 changed files with 1044 additions and 30 deletions
+156 -1
View File
@@ -440,7 +440,7 @@ async fn run_git_command(
argv.extend(args.iter().map(|arg| (*arg).to_string()));
runner
.run(
WorkspaceCommand::new(argv)
WorkspaceCommand::local_only_git(argv)
.cwd(cwd.to_path_buf())
.env("GIT_OPTIONAL_LOCKS", "0"),
)
@@ -474,10 +474,85 @@ mod tests {
use super::*;
use crate::workspace_command::WorkspaceCommand;
use pretty_assertions::assert_eq;
#[cfg(unix)]
use std::fs;
use std::future::Future;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::pin::Pin;
#[cfg(unix)]
use std::process::Command as ProcessCommand;
use std::sync::Mutex;
#[cfg(unix)]
#[tokio::test]
async fn branch_diff_stats_do_not_lazy_fetch_promisor_objects() {
let temp_dir = tempfile::tempdir().expect("create temp dir");
let source = temp_dir.path().join("source");
let clone = temp_dir.path().join("clone");
fs::create_dir(&source).expect("create source repository");
run_git(&source, &["init", "-q", "--initial-branch=main"]);
run_git(&source, &["config", "uploadpack.allowFilter", "true"]);
fs::write(source.join("data.txt"), "before\n").expect("write initial blob");
run_git(&source, &["add", "data.txt"]);
commit_all(&source, "initial");
let base_sha = run_git_stdout(&source, &["rev-parse", "HEAD"]);
let base_blob = run_git_stdout(&source, &["rev-parse", "HEAD:data.txt"]);
fs::write(source.join("data.txt"), "after\n").expect("write current blob");
commit_all(&source, "current");
let source_url = format!("file://{}", source.display());
run_git(
temp_dir.path(),
&[
"-c",
"protocol.file.allow=always",
"clone",
"-q",
"--no-local",
"--filter=blob:none",
"--no-checkout",
&source_url,
clone.to_str().expect("clone path"),
],
);
run_git(&clone, &["checkout", "-q", "main"]);
let missing = run_git_stdout(
&clone,
&["rev-list", "--objects", "--all", "--missing=print"],
);
assert!(
missing.lines().any(|line| line == format!("?{base_blob}")),
"expected historical blob {base_blob} to remain missing:\n{missing}"
);
run_git(
&clone,
&["update-ref", "refs/remotes/origin/main", &base_sha],
);
let helper = temp_dir.path().join("transport-helper.sh");
fs::write(&helper, "#!/bin/sh\nprintf ran >\"$0.ran\"\nexit 1\n")
.expect("write transport helper");
let mut permissions = fs::metadata(&helper)
.expect("read transport helper metadata")
.permissions();
permissions.set_mode(/*mode*/ 0o755);
fs::set_permissions(&helper, permissions).expect("make transport helper executable");
let helper_url = format!("ext::{}", helper.display());
run_git(&clone, &["config", "remote.origin.url", &helper_url]);
run_git(&clone, &["config", "protocol.ext.allow", "always"]);
let stats = branch_diff_stats_to_default_branch(&LocalRunner, &clone).await;
assert_eq!(
(stats, helper.with_extension("sh.ran").exists()),
(None, false),
"local-only branch stats must fail without invoking the promisor transport"
);
}
#[tokio::test]
async fn branch_diff_stats_prefers_remote_default_ref_over_stale_local_branch() {
let runner = FakeRunner::new(vec![
@@ -880,4 +955,84 @@ mod tests {
})
}
}
#[cfg(unix)]
fn run_git(cwd: &Path, args: &[&str]) -> std::process::Output {
let output = ProcessCommand::new("git")
.args(args)
.current_dir(cwd)
.output()
.expect("run Git command");
assert!(
output.status.success(),
"git {args:?} failed\nstdout:\n{}\nstderr:\n{}",
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr)
);
output
}
#[cfg(unix)]
fn run_git_stdout(cwd: &Path, args: &[&str]) -> String {
String::from_utf8(run_git(cwd, args).stdout)
.expect("Git output should be UTF-8")
.trim()
.to_string()
}
#[cfg(unix)]
fn commit_all(cwd: &Path, message: &str) {
run_git(
cwd,
&[
"-c",
"user.name=Codex Test",
"-c",
"user.email=codex@example.com",
"commit",
"-qam",
message,
],
);
}
#[cfg(unix)]
struct LocalRunner;
#[cfg(unix)]
impl WorkspaceCommandExecutor for LocalRunner {
fn run(
&self,
command: WorkspaceCommand,
) -> Pin<
Box<
dyn Future<Output = Result<WorkspaceCommandOutput, WorkspaceCommandError>>
+ Send
+ '_,
>,
> {
Box::pin(async move {
let mut process = ProcessCommand::new(&command.argv[0]);
process
.args(&command.argv[1..])
.current_dir(command.cwd.expect("test command cwd"));
for (key, value) in command.env {
match value {
Some(value) => {
process.env(key, value);
}
None => {
process.env_remove(key);
}
}
}
let output = process.output().expect("run test command");
Ok(WorkspaceCommandOutput {
exit_code: output.status.code().expect("test command exit code"),
stdout: String::from_utf8(output.stdout).expect("utf8 stdout"),
stderr: String::from_utf8(output.stderr).expect("utf8 stderr"),
})
})
}
}
}
+21 -4
View File
@@ -34,7 +34,7 @@ struct WorkspaceFsmonitorProbeRunner<'a> {
impl FsmonitorProbeRunner for WorkspaceFsmonitorProbeRunner<'_> {
async fn run_probe(&mut self, args: &[&str]) -> Option<Vec<u8>> {
let argv = ["git"].into_iter().chain(args.iter().copied());
let command = WorkspaceCommand::new(argv).cwd(self.cwd.to_path_buf());
let command = WorkspaceCommand::local_only_git(argv).cwd(self.cwd.to_path_buf());
match self.runner.run(command).await {
Ok(output) if output.success() => Some(output.stdout.into_bytes()),
_ => None,
@@ -238,7 +238,7 @@ async fn run_git_command(
]
.into_iter()
.chain(args.iter().copied());
let mut command = WorkspaceCommand::new(argv)
let mut command = WorkspaceCommand::local_only_git(argv)
.cwd(cwd.to_path_buf())
.timeout(DIFF_COMMAND_TIMEOUT)
.disable_output_cap();
@@ -764,7 +764,8 @@ mod tests {
}
fn filter_override_env(driver: &str) -> HashMap<String, Option<String>> {
HashMap::from([
let mut env = expected_local_only_git_env();
env.extend([
("GIT_CONFIG_COUNT".to_string(), Some("3".to_string())),
(
"GIT_CONFIG_KEY_0".to_string(),
@@ -781,6 +782,14 @@ mod tests {
Some(format!("{driver}.required")),
),
("GIT_CONFIG_VALUE_2".to_string(), Some("false".to_string())),
]);
env
}
fn expected_local_only_git_env() -> HashMap<String, Option<String>> {
HashMap::from([
("GIT_ALLOW_PROTOCOL".to_string(), Some(String::new())),
("GIT_NO_LAZY_FETCH".to_string(), Some("1".to_string())),
])
}
@@ -833,11 +842,19 @@ mod tests {
command.argv.get(1).map(String::as_str),
Some("config" | "version")
) {
assert_eq!(command.env, HashMap::new());
assert_eq!(command.env, expected_local_only_git_env());
assert_eq!(command.timeout, Duration::from_secs(/*secs*/ 5));
assert_eq!(command.output_bytes_cap, 64 * 1024);
assert_eq!(command.disable_output_cap, false);
} else {
assert_eq!(
command.env.get("GIT_ALLOW_PROTOCOL"),
Some(&Some(String::new()))
);
assert_eq!(
command.env.get("GIT_NO_LAZY_FETCH"),
Some(&Some("1".to_string()))
);
assert_eq!(command.timeout, DIFF_COMMAND_TIMEOUT);
assert_eq!(command.disable_output_cap, true);
}
+9
View File
@@ -62,6 +62,15 @@ impl WorkspaceCommand {
}
}
/// Creates a Git command that is restricted to already-local repository data.
pub(crate) fn local_only_git(argv: impl IntoIterator<Item = impl Into<String>>) -> Self {
let mut command = Self::new(argv);
for (key, value) in codex_git_utils::local_only_git_env() {
command = command.env(key, value);
}
command
}
/// Sets the command working directory.
pub(crate) fn cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.cwd = Some(cwd.into());