Skip to content

Commit a20ae6d

Browse files
authored
refactor(core): resolve real git binary once on macOS (anomalyco#58602)
1 parent 6436011 commit a20ae6d

4 files changed

Lines changed: 124 additions & 13 deletions

File tree

‎packages/core/src/git.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ const layer = Layer.effect(
339339
) {
340340
const result = yield* proc
341341
.run(
342-
ChildProcess.make(gitExecutable, repositoryArgs(repository, args), {
342+
ChildProcess.make(gitExecutable(), repositoryArgs(repository, args), {
343343
cwd: repository.worktree,
344344
env: options?.index ? { ...options.env, GIT_INDEX_FILE: options.index } : options?.env,
345345
extendEnv: true,
@@ -549,7 +549,7 @@ const layer = Layer.effect(
549549
const result = yield* proc
550550
.run(
551551
ChildProcess.make(
552-
gitExecutable,
552+
gitExecutable(),
553553
repositoryArgs(input.repository, ["check-ignore", "--no-index", "--stdin", "-z"]),
554554
{
555555
cwd: input.repository.worktree,
@@ -1227,7 +1227,7 @@ const layer = Layer.effect(
12271227
cwd = repository.worktree,
12281228
) {
12291229
const result = yield* proc
1230-
.run(ChildProcess.make(gitExecutable, args, { cwd, extendEnv: true, stdin: "ignore" }))
1230+
.run(ChildProcess.make(gitExecutable(), args, { cwd, extendEnv: true, stdin: "ignore" }))
12311231
.pipe(
12321232
Effect.mapError(
12331233
(cause) => new WorktreeError({ operation, directory: worktreeDirectory, message: cause.message, cause }),
@@ -1324,7 +1324,7 @@ function run(cwd: string, proc: AppProcess.Interface, args: string[]) {
13241324
function execute(cwd: string, proc: AppProcess.Interface, args: string[]) {
13251325
return proc
13261326
.run(
1327-
ChildProcess.make(gitExecutable, args, {
1327+
ChildProcess.make(gitExecutable(), args, {
13281328
cwd,
13291329
extendEnv: true,
13301330
stdin: "ignore",

‎packages/core/src/plugin/vcs/git.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const Plugin = define({
4444
init: (input) =>
4545
Effect.gen(function* () {
4646
const result = yield* processes.run(
47-
ChildProcess.make(gitExecutable, ["init"], { cwd: input.worktree, stdin: "ignore" }),
47+
ChildProcess.make(gitExecutable(), ["init"], { cwd: input.worktree, stdin: "ignore" }),
4848
)
4949
if (result.exitCode !== 0) return yield* Effect.fail(new Error("Git initialization failed"))
5050
}),
@@ -209,7 +209,7 @@ function makeGit(proc: AppProcess.Interface, env?: Record<string, string>) {
209209
const run = Effect.fnUntraced(
210210
function* (args: string[], opts: { cwd: string; maxOutputBytes?: number }) {
211211
const result = yield* proc.run(
212-
ChildProcess.make(gitExecutable, [...cfg, ...args], {
212+
ChildProcess.make(gitExecutable(), [...cfg, ...args], {
213213
cwd: opts.cwd,
214214
env,
215215
extendEnv: true,
Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,59 @@
1+
import { existsSync, realpathSync } from "node:fs"
12
import path from "path"
3+
import { lazy } from "./lazy.js"
24
import { which } from "./which.js"
35

4-
export function resolveGitExecutable(platform: NodeJS.Platform, resolved: string | null) {
5-
if (platform !== "win32" || !resolved) return "git"
6-
return path.resolve(resolved)
6+
const darwinShim = "/usr/bin/git"
7+
const xcodeSelectLink = "/var/db/xcode_select_link"
8+
9+
const defaultDeveloperDirs = [
10+
"/Applications/Xcode.app/Contents/Developer",
11+
"/Library/Developer/CommandLineTools",
12+
]
13+
14+
function readXcodeSelectLink(link: string) {
15+
if (!existsSync(link)) return null
16+
try {
17+
return realpathSync(link)
18+
} catch {
19+
return null
20+
}
721
}
822

9-
const resolved = process.platform === "win32" ? which("git") : null
23+
export function darwinDeveloperDirs(
24+
developerDir = process.env.DEVELOPER_DIR?.trim(),
25+
readLink: (link: string) => string | null = readXcodeSelectLink,
26+
) {
27+
if (developerDir) return [developerDir]
28+
const selected = readLink(xcodeSelectLink)
29+
return selected ? [selected, ...defaultDeveloperDirs] : defaultDeveloperDirs
30+
}
31+
32+
export function resolveGitExecutable(
33+
platform: NodeJS.Platform,
34+
resolved: string | null,
35+
options?: {
36+
developerDirs?: readonly string[]
37+
exists?: (file: string) => boolean
38+
},
39+
) {
40+
if (platform === "win32") return resolved ? path.resolve(resolved) : "git"
41+
if (platform !== "darwin" || !resolved) return "git"
42+
const full = path.posix.resolve(resolved)
43+
if (full !== darwinShim) return full
44+
const exists = options?.exists ?? existsSync
45+
const dirs = options?.developerDirs ?? darwinDeveloperDirs()
46+
return (
47+
dirs
48+
.filter((dir) => Boolean(dir) && path.posix.isAbsolute(dir))
49+
.map((dir) => path.posix.join(dir, "usr/bin/git"))
50+
.find((candidate) => candidate !== darwinShim && exists(candidate)) ?? "git"
51+
)
52+
}
1053

11-
export const gitExecutable = resolveGitExecutable(process.platform, resolved)
54+
export const gitExecutable = lazy(() =>
55+
resolveGitExecutable(
56+
process.platform,
57+
process.platform === "win32" || process.platform === "darwin" ? which("git") : null,
58+
),
59+
)

‎packages/core/test/util/git-executable.test.ts‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,76 @@
11
import { describe, expect, test } from "bun:test"
22
import path from "path"
3-
import { resolveGitExecutable } from "@opencode/core/util/git-executable"
3+
import { darwinDeveloperDirs, resolveGitExecutable } from "@opencode/core/util/git-executable"
44

55
describe("Git executable", () => {
66
test("uses an absolute executable on Windows", () => {
77
expect(resolveGitExecutable("win32", "bin/git.exe")).toBe(path.resolve("bin/git.exe"))
88
})
99

10-
test("leaves lookup to the process outside Windows", () => {
10+
test("uses a non-shim PATH executable on macOS without probing developer directories", () => {
11+
let calls = 0
12+
const resolved = resolveGitExecutable("darwin", "/opt/homebrew/bin/git", {
13+
developerDirs: ["/Library/Developer/CommandLineTools"],
14+
exists: () => {
15+
calls += 1
16+
return true
17+
},
18+
})
19+
expect(resolved).toBe("/opt/homebrew/bin/git")
20+
expect(calls).toBe(0)
21+
})
22+
23+
test("resolves the Apple shim on macOS from developer directories", () => {
24+
const resolved = resolveGitExecutable("darwin", "/usr/bin/git", {
25+
developerDirs: ["/Applications/Xcode.app/Contents/Developer", "/Library/Developer/CommandLineTools"],
26+
exists: (file) => file === "/Library/Developer/CommandLineTools/usr/bin/git",
27+
})
28+
expect(resolved).toBe("/Library/Developer/CommandLineTools/usr/bin/git")
29+
})
30+
31+
test("falls back to PATH lookup on macOS when no developer tools exist or candidate is invalid", () => {
32+
expect(
33+
resolveGitExecutable("darwin", "/usr/bin/git", {
34+
developerDirs: ["/Applications/Xcode.app/Contents/Developer", "/Library/Developer/CommandLineTools"],
35+
exists: () => false,
36+
}),
37+
).toBe("git")
38+
expect(
39+
resolveGitExecutable("darwin", "/usr/bin/git", {
40+
developerDirs: ["relative/dir"],
41+
exists: () => true,
42+
}),
43+
).toBe("git")
44+
expect(
45+
resolveGitExecutable("darwin", "/usr/bin/git", {
46+
developerDirs: ["/"],
47+
exists: () => true,
48+
}),
49+
).toBe("git")
50+
expect(
51+
resolveGitExecutable("darwin", null, {
52+
developerDirs: ["/Library/Developer/CommandLineTools"],
53+
exists: () => true,
54+
}),
55+
).toBe("git")
56+
})
57+
58+
test("orders macOS developer directory candidates by DEVELOPER_DIR then xcode-select link", () => {
59+
expect(darwinDeveloperDirs("/Custom/Developer", () => "/Library/Developer/CommandLineTools")).toEqual([
60+
"/Custom/Developer",
61+
])
62+
expect(darwinDeveloperDirs("", () => "/Applications/Xcode-beta.app/Contents/Developer")).toEqual([
63+
"/Applications/Xcode-beta.app/Contents/Developer",
64+
"/Applications/Xcode.app/Contents/Developer",
65+
"/Library/Developer/CommandLineTools",
66+
])
67+
expect(darwinDeveloperDirs(undefined, () => null)).toEqual([
68+
"/Applications/Xcode.app/Contents/Developer",
69+
"/Library/Developer/CommandLineTools",
70+
])
71+
})
72+
73+
test("leaves lookup to the process on Linux", () => {
1174
expect(resolveGitExecutable("linux", "/usr/bin/git")).toBe("git")
1275
})
1376

0 commit comments

Comments
 (0)