Skip to content

Commit cedaa0d

Browse files
authored
perf(server): Windows git reads no longer start a console host (pingdotgg#19288)
1 parent 693ac5f commit cedaa0d

3 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
3636
import * as HostProcess from "@t3tools/shared/HostProcess";
3737
import { compactTraceAttributes } from "@t3tools/shared/observability";
3838
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
39-
import { resolveSpawnCommand } from "@t3tools/shared/shell";
39+
import { resolveSpawnCommand, runsGitWithoutConsole } from "@t3tools/shared/shell";
4040
import { parseT3ProjectFile } from "@t3tools/shared/t3ProjectFile";
4141
import { resolveProjectFileBackedSetting } from "@t3tools/shared/projectSettings";
4242
import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts";
@@ -993,6 +993,9 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
993993
cwd: commandInput.cwd,
994994
env: spawnEnv,
995995
forceKillAfter: input.forceKillAfterMs,
996+
...(runsGitWithoutConsole(hostPlatform, executable, commandInput.args)
997+
? { detached: true, windowsHide: true }
998+
: {}),
996999
}),
9971000
)
9981001
.pipe(

‎packages/shared/src/shell.test.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
resolveCommandPath,
3333
resolveSpawnCommand,
3434
resolveWindowsEnvironment,
35+
runsGitWithoutConsole,
3536
SpawnExecutableResolution,
3637
WindowsShellEnvironment,
3738
withPathDirectoryListings,
@@ -561,6 +562,36 @@ effectIt.layer(NodeServices.layer)("resolveCommandPath", (it) => {
561562
);
562563
});
563564

565+
describe("runsGitWithoutConsole", () => {
566+
const git = "C:\\Program Files\\Git\\ucrt64\\bin\\git.exe";
567+
568+
it.each([
569+
[["rev-parse", "--git-path", "index"], true],
570+
[["--git-dir", "C:\\repo\\.git", "symbolic-ref", "refs/remotes/origin/HEAD"], true],
571+
[["remote", "get-url", "--", "origin"], true],
572+
[["config", "--get", "branch.main.pushRemote"], true],
573+
[["symbolic-ref", "--quiet", "HEAD"], true],
574+
[["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"], true],
575+
[["rev-parse", "HEAD"], false],
576+
[["symbolic-ref", "HEAD", "refs/heads/x"], false],
577+
[["symbolic-ref", "-qd", "refs/remotes/origin/HEAD"], false],
578+
[["config", "--get", "--edit"], false],
579+
[["-c", "core.pager=x", "config", "--get", "user.name"], false],
580+
])("Git for Windows %j without a console: %s", (args, expected) => {
581+
expect(runsGitWithoutConsole("win32", git, args)).toBe(expected);
582+
});
583+
584+
it.each([
585+
["win32", "C:\\Program Files\\Git\\cmd\\git.exe"],
586+
["win32", "C:\\Users\\x\\scoop\\shims\\git.exe"],
587+
["linux", git],
588+
] as const)("keeps the console on %s for %s", (platform, executable) => {
589+
expect(runsGitWithoutConsole(platform, executable, ["rev-parse", "--show-toplevel"])).toBe(
590+
false,
591+
);
592+
});
593+
});
594+
564595
describe("preferGitForWindowsBinary", () => {
565596
const files =
566597
(...paths: Array<string>) =>

‎packages/shared/src/shell.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,46 @@ export interface SpawnCommandOptions extends CommandAvailabilityOptions {
827827
readonly bypassNpmShim?: boolean;
828828
}
829829

830+
// `*` is one argument that is not an option. These forms read refs or config
831+
// only, so git starts no hook, helper, pager, editor or lazy fetch for them.
832+
const CONSOLE_FREE_GIT_READS = [
833+
"rev-parse --git-path index",
834+
"rev-parse --git-common-dir",
835+
"rev-parse --show-toplevel",
836+
"rev-parse --symbolic-full-name HEAD",
837+
"rev-parse --abbrev-ref --symbolic-full-name @{upstream}",
838+
"symbolic-ref --quiet HEAD",
839+
"symbolic-ref *",
840+
"remote",
841+
"remote get-url *",
842+
"remote get-url -- *",
843+
"config --get *",
844+
].map((form) => form.split(" "));
845+
846+
export function runsGitWithoutConsole(
847+
platform: NodeJS.Platform,
848+
executable: string,
849+
args: ReadonlyArray<string>,
850+
): boolean {
851+
if (platform !== "win32") return false;
852+
const { basename, dirname } = NodePath.win32;
853+
const bin = dirname(executable);
854+
const build = basename(dirname(bin)).toLowerCase();
855+
if (basename(executable).toLowerCase() !== "git.exe" || basename(bin).toLowerCase() !== "bin") {
856+
return false;
857+
}
858+
if (!GIT_FOR_WINDOWS_BUILDS.some((candidate) => candidate === build)) return false;
859+
const rest =
860+
args[0] === "--git-dir" && args[1] !== undefined && !args[1].startsWith("-")
861+
? args.slice(2)
862+
: args;
863+
return CONSOLE_FREE_GIT_READS.some(
864+
(form) =>
865+
form.length === rest.length &&
866+
form.every((part, i) => part === rest[i] || (part === "*" && !rest[i]!.startsWith("-"))),
867+
);
868+
}
869+
830870
export const resolveSpawnCommand = Effect.fnUntraced(function* (
831871
command: string,
832872
args: ReadonlyArray<string>,

0 commit comments

Comments
 (0)