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
5 changes: 4 additions & 1 deletion apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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(
Expand Down
31 changes: 31 additions & 0 deletions packages/shared/src/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
resolveCommandPath,
resolveSpawnCommand,
resolveWindowsEnvironment,
runsGitWithoutConsole,
SpawnExecutableResolution,
WindowsShellEnvironment,
withPathDirectoryListings,
Expand Down Expand Up @@ -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>) =>
Expand Down
40 changes: 40 additions & 0 deletions packages/shared/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
Loading