Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ jobs:
- uses: Swatinem/rust-cache@v2
with:
save-if: ${{ github.ref == 'refs/heads/main' }}
# rust-cache keeps the duckdb build-script output but prunes the
# library it downloaded, so a cache hit skips the download and the
# test binaries fail to link with `-lduckdb`. Cache the library too.
cache-directories: target/duckdb-download
# Bumped with cache-directories so restores carry the library.
key: duckdb-download
# rust-cache prunes the library under target/duckdb-download before it
# saves, but keeps the libduckdb-sys build-script output that links
# against it. Drop that output so the build script downloads the library
# again; a cache miss has nothing to drop.
- name: restore downloaded DuckDB library
run: rm -rf target/debug/build/libduckdb-sys-* target/debug/.fingerprint/libduckdb-sys-*
- uses: taiki-e/install-action@v2
with:
tool: cargo-nextest
Expand Down
21 changes: 11 additions & 10 deletions crates/opensymphony-memory/src/code_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::opensymphony_gateway_schema::{
CodeSymbolProvenance,
},
};
use crate::opensymphony_workspace::git_program;
use duckdb::OptionalExt;
use url::Url;
use std::sync::{Mutex, OnceLock};
Expand Down Expand Up @@ -1363,7 +1364,7 @@ fn unindexed_code_repo_summary(config: &MemoryConfig) -> CodeRepoSummary {
}

fn configured_code_repo_id(config: &MemoryConfig) -> String {
let remote = Command::new("git")
let remote = Command::new(git_program())
.args(["-C"])
.arg(&config.repo_root)
.args(["remote", "get-url", "origin"])
Expand Down Expand Up @@ -3170,7 +3171,7 @@ fn workspace_git_bytes<const N: usize>(
workspace_path: &Path,
args: [&str; N],
) -> Result<Vec<u8>, CodeGraphProjectionError> {
let output = Command::new("git")
let output = Command::new(git_program())
.arg("-C")
.arg(workspace_path)
.args(args)
Expand Down Expand Up @@ -3931,7 +3932,7 @@ pub fn code_index_target(
}

pub fn code_index_repository_is_git(config: &MemoryConfig) -> bool {
Command::new("git")
Command::new(git_program())
.args(["-C"])
.arg(&config.repo_root)
.args(["rev-parse", "--git-dir"])
Expand Down Expand Up @@ -5035,7 +5036,7 @@ fn validate_code_index_branch(
"configured target branch is invalid".to_string(),
));
}
let branch_check = Command::new("git")
let branch_check = Command::new(git_program())
.args(["check-ref-format", "--branch", branch])
.output()
.map_err(|source| CodeGraphProjectionError::Memory(MemoryError::ResolvePath {
Expand All @@ -5058,7 +5059,7 @@ fn git_target_commit(
format!("refs/remotes/origin/{branch}"),
format!("refs/heads/{branch}"),
] {
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["rev-parse", "--verify", "--quiet"])
Expand Down Expand Up @@ -5087,7 +5088,7 @@ fn git_tree_paths(
} else {
format!("{directory}/")
};
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["ls-tree", "-z"])
Expand Down Expand Up @@ -5139,7 +5140,7 @@ fn git_tree_paths(
}

fn git_repo_prefix(root: &Path) -> Result<PathBuf, CodeGraphProjectionError> {
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["rev-parse", "--show-prefix"])
Expand All @@ -5162,7 +5163,7 @@ fn git_changed_tree_paths(
head: &str,
repo_prefix: &Path,
) -> Result<BTreeSet<String>, CodeGraphProjectionError> {
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["diff", "--name-only", "-z", base, head, "--", "."])
Expand Down Expand Up @@ -5197,7 +5198,7 @@ fn git_blob(
path: &Path,
) -> Result<Vec<u8>, CodeGraphProjectionError> {
let object = format!("{}:{}", commit, repo_prefix.join(path).to_string_lossy());
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["cat-file", "blob"])
Expand All @@ -5223,7 +5224,7 @@ fn git_blob_size(
path: &Path,
) -> Result<u64, CodeGraphProjectionError> {
let object = format!("{}:{}", commit, repo_prefix.join(path).to_string_lossy());
let output = Command::new("git")
let output = Command::new(git_program())
.args(["-C"])
.arg(root)
.args(["cat-file", "-s"])
Expand Down
81 changes: 81 additions & 0 deletions crates/opensymphony-workspace/src/git_program.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Resolves the Git executable that checkout and memory code spawn.
//!
//! On macOS, `/usr/bin/git` is an `xcrun` trampoline: every invocation looks
//! up the active developer directory before it execs the real binary, which
//! adds roughly 10 ms to each Git process. Checkout verification and code
//! indexing start thousands of short Git processes, so the trampoline cost
//! dominates their wall time. When `git` on `PATH` is that trampoline, the
//! real binary is resolved once with `xcrun --find git`, the same lookup the
//! trampoline performs, and used directly. Every other platform and PATH
//! layout keeps spawning `git` through `PATH`.

use std::{ffi::OsStr, sync::OnceLock};

/// Returns the program to pass to `Command::new` for Git.
pub fn git_program() -> &'static OsStr {
static PROGRAM: OnceLock<std::ffi::OsString> = OnceLock::new();
PROGRAM.get_or_init(resolve_git_program).as_os_str()
}

#[cfg(target_os = "macos")]
fn resolve_git_program() -> std::ffi::OsString {
use std::path::{Path, PathBuf};

const XCRUN_TRAMPOLINE: &str = "/usr/bin/git";

fn first_git_on_path() -> Option<PathBuf> {
let path = std::env::var_os("PATH")?;
std::env::split_paths(&path)
.map(|directory| directory.join("git"))
.find(|candidate| candidate.is_file())
}

let default = std::ffi::OsString::from("git");
let Some(on_path) = first_git_on_path() else {
return default;
};
if on_path.canonicalize().ok().as_deref() != Some(Path::new(XCRUN_TRAMPOLINE)) {
return default;
}
let Ok(output) = std::process::Command::new("/usr/bin/xcrun")
.args(["--find", "git"])
.stdin(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.output()
else {
return default;
};
if !output.status.success() {
return default;
}
let Ok(resolved) = String::from_utf8(output.stdout) else {
return default;
};
let resolved = PathBuf::from(resolved.trim());
if resolved.is_absolute() && resolved.is_file() && resolved != Path::new(XCRUN_TRAMPOLINE) {
tracing::debug!(git = %resolved.display(), "resolved Git behind the xcrun trampoline");
resolved.into_os_string()
} else {
default
}
}

#[cfg(not(target_os = "macos"))]
fn resolve_git_program() -> std::ffi::OsString {
std::ffi::OsString::from("git")
}

#[cfg(test)]
mod tests {
use super::git_program;

#[test]
fn git_program_runs_git() {
let output = std::process::Command::new(git_program())
.arg("--version")
.output()
.expect("spawn the resolved Git program");
assert!(output.status.success());
assert!(String::from_utf8_lossy(&output.stdout).starts_with("git version"));
}
}
2 changes: 2 additions & 0 deletions crates/opensymphony-workspace/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod environment;
mod error;
mod git_program;
mod manager;
mod models;
mod paths;
Expand All @@ -10,6 +11,7 @@ pub(crate) use sensitive_fields::{normalize_secret_field_name, runtime_field_is_
pub use environment::environment_variable_names_equal;
pub(crate) use environment::{has_environment_name_collision, insert_environment_value};
pub use error::{WorkspaceError, WorkspaceOwnershipConflictDetails};
pub use git_program::git_program;
pub use manager::{
WorkspaceManager, compose_parent_continuation_prompt, compose_parent_prompt,
compose_terminal_prompt,
Expand Down
Loading
Loading