Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitConfig.setup.ts

47 lines2.0 KB

1// @effect-diagnostics nodeBuiltinImport:off - runs once at test setup, outside any Effect runtime.
2import * as NodeChildProcess from "node:child_process";
3import * as NodeFS from "node:fs";
4import * as NodePath from "node:path";
5import * as HostProcess from "@t3tools/shared/HostProcess";
6​
7// Git for Windows installs with core.autocrlf=true, which checks committed LF
8// files out as CRLF and breaks every byte-exact content assertion; a signing
9// key or a non-default init branch on the developer's machine breaks fixtures
10// the same way. Set as environment so each git child the driver spawns sees it
11// without touching the fixtures.
12const entries: ReadonlyArray<readonly [key: string, value: string]> = [
13 ["core.autocrlf", "false"],
14 ["core.filemode", "false"],
15 ["core.longpaths", "true"],
16 ["commit.gpgsign", "false"],
17 ["tag.gpgsign", "false"],
18 ["init.defaultBranch", "main"],
19];
20​
21const existing = Number(process.env.GIT_CONFIG_COUNT ?? "0");
22process.env.GIT_CONFIG_COUNT = String(existing + entries.length);
23entries.forEach(([key, value], index) => {
24 process.env[`GIT_CONFIG_KEY_${existing + index}`] = key;
25 process.env[`GIT_CONFIG_VALUE_${existing + index}`] = value;
26});
27​
28// Without Homebrew git, macOS resolves `git` to /usr/bin/git, an xcrun shim that costs
29// about 8 ms per spawn against 3 ms for the real binary. Git-heavy files such as
30// GitVcsDriverCore.test.ts and GitManager.test.ts run twice as fast with the real one.
31if (HostProcess.Platform.defaultValue() === "darwin") {
32 const gitOnPath = (process.env.PATH ?? "")
33 .split(NodePath.delimiter)
34 .map((dir) => NodePath.join(dir, "git"))
35 .find((candidate) => NodeFS.existsSync(candidate));
36 if (gitOnPath === "/usr/bin/git") {
37 try {
38 const realGit = NodeChildProcess.execFileSync("xcrun", ["-f", "git"], {
39 encoding: "utf8",
40 stdio: ["ignore", "pipe", "ignore"],
41 }).trim();
42 if (NodePath.isAbsolute(realGit)) {
43 process.env.PATH = `${NodePath.dirname(realGit)}${NodePath.delimiter}${process.env.PATH}`;
44 }
45 } catch {}
46 }