From 9563097c3f51fd13111de1cd30062e8cd387ed48 Mon Sep 17 00:00:00 2001 From: Kayzel Date: Sun, 1 Nov 2026 22:33:08 +0100 Subject: [PATCH] perf(server): Windows git reads no longer start a console host --- apps/server/src/vcs/GitVcsDriverCore.ts | 5 +++- packages/shared/src/shell.test.ts | 31 +++++++++++++++++++ packages/shared/src/shell.ts | 40 +++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 1 deletion(-) diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 25a757104db32..11e5f39cbe684 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -37,7 +37,7 @@ import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { compactTraceAttributes } from "@t3tools/shared/observability"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; -import { resolveSpawnCommand } from "@t3tools/shared/shell"; +import { resolveSpawnCommand, runsGitWithoutConsole } from "@t3tools/shared/shell"; import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile"; import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings"; import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts"; @@ -994,6 +994,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* cwd: commandInput.cwd, env: spawnEnv, forceKillAfter: input.forceKillAfterMs, + ...(runsGitWithoutConsole(hostPlatform, executable, commandInput.args) + ? { detached: true, windowsHide: true } + : {}), }), ) .pipe( diff --git a/packages/shared/src/shell.test.ts b/packages/shared/src/shell.test.ts index e3af0855ebfac..757c9b38abda0 100644 --- a/packages/shared/src/shell.test.ts +++ b/packages/shared/src/shell.test.ts @@ -32,6 +32,7 @@ import { resolveCommandPath, resolveSpawnCommand, resolveWindowsEnvironment, + runsGitWithoutConsole, SpawnExecutableResolution, WindowsShellEnvironment, withPathDirectoryListings, @@ -561,6 +562,36 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => { ); }); +describe("runsGitWithoutConsole", () => { + const git = "C:\\Program Files\\Git\\ucrt64\\bin\\git.exe"; + + it.each([ + [["rev-parse", "--git-path", "index"], true], + [["--git-dir", "C:\\repo\\.git", "symbolic-ref", "refs/remotes/origin/HEAD"], true], + [["remote", "get-url", "--", "origin"], true], + [["config", "--get", "branch.main.pushRemote"], true], + [["symbolic-ref", "--quiet", "HEAD"], true], + [["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"], true], + [["rev-parse", "HEAD"], false], + [["symbolic-ref", "HEAD", "refs/heads/x"], false], + [["symbolic-ref", "-qd", "refs/remotes/origin/HEAD"], false], + [["config", "--get", "--edit"], false], + [["-c", "core.pager=x", "config", "--get", "user.name"], false], + ])("Git for Windows %j without a console: %s", (args, expected) => { + expect(runsGitWithoutConsole("win32", git, args)).toBe(expected); + }); + + it.each([ + ["win32", "C:\\Program Files\\Git\\cmd\\git.exe"], + ["win32", "C:\\Users\\x\\scoop\\shims\\git.exe"], + ["linux", git], + ] as const)("keeps the console on %s for %s", (platform, executable) => { + expect(runsGitWithoutConsole(platform, executable, ["rev-parse", "--show-toplevel"])).toBe( + false, + ); + }); +}); + describe("preferGitForWindowsBinary", () => { const files = (...paths: Array) => diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.ts index 28b666ce05531..ad30c152a07c0 100644 --- a/packages/shared/src/shell.ts +++ b/packages/shared/src/shell.ts @@ -827,6 +827,46 @@ export interface SpawnCommandOptions extends CommandAvailabilityOptions { readonly bypassNpmShim?: boolean; } +// `*` is one argument that is not an option. These forms read refs or config +// only, so git starts no hook, helper, pager, editor or lazy fetch for them. +const CONSOLE_FREE_GIT_READS = [ + "rev-parse --git-path index", + "rev-parse --git-common-dir", + "rev-parse --show-toplevel", + "rev-parse --symbolic-full-name HEAD", + "rev-parse --abbrev-ref --symbolic-full-name @{upstream}", + "symbolic-ref --quiet HEAD", + "symbolic-ref *", + "remote", + "remote get-url *", + "remote get-url -- *", + "config --get *", +].map((form) => form.split(" ")); + +export function runsGitWithoutConsole( + platform: NodeJS.Platform, + executable: string, + args: ReadonlyArray, +): boolean { + if (platform !== "win32") return false; + const { basename, dirname } = NodePath.win32; + const bin = dirname(executable); + const build = basename(dirname(bin)).toLowerCase(); + if (basename(executable).toLowerCase() !== "git.exe" || basename(bin).toLowerCase() !== "bin") { + return false; + } + if (!GIT_FOR_WINDOWS_BUILDS.some((candidate) => candidate === build)) return false; + const rest = + args[0] === "--git-dir" && args[1] !== undefined && !args[1].startsWith("-") + ? args.slice(2) + : args; + return CONSOLE_FREE_GIT_READS.some( + (form) => + form.length === rest.length && + form.every((part, i) => part === rest[i] || (part === "*" && !rest[i]!.startsWith("-"))), + ); +} + export const resolveSpawnCommand = Effect.fnUntraced(function* ( command: string, args: ReadonlyArray,