TypeScript
perf(server): Windows git reads no longer start a console host (#19288)
Kayzelcedaa0de48e6
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 94404b488b1..d1199f39c65 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -36,7 +36,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";@@ -993,6 +993,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.tsindex 5f268ffa507..16dc69ab14c 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<string>) =>diff --git a/packages/shared/src/shell.ts b/packages/shared/src/shell.tsindex 28b666ce055..ad30c152a07 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<string>,+): 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<string>,