Skip to content
Closed
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
2 changes: 1 addition & 1 deletion .github/scripts/ci-changes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const INERT = [

// Shared test setup and vendored mobile dependencies affect every package.
const RUNS_EVERYTHING = [
/^packages\/shared\/src\/(HostProcess|testing\/(longTempDir|providerEnv|gitEnv|gitRepositoryEnv))\.ts$/,
/^packages\/shared\/src\/(HostProcess|gitEnvironment|testing\/(longTempDir|providerEnv|gitEnv))\.ts$/,
/^apps\/mobile\/deps\//,
];

Expand Down
1 change: 1 addition & 0 deletions apps/server/scripts/verify-background-live.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalDate:off globalFetch:off globalConsole:off globalJsonInEffect:off - Host-side process verifier retains raw wire evidence and owns deadline timers.
import "@t3tools/shared/testing/gitEnv";
import * as NodeAssert from "node:assert/strict";
import * as NodeChildProcess from "node:child_process";
import * as NodeCrypto from "node:crypto";
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/project/RepositoryIdentityResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { RepositoryIdentity, SourceControlProviderError } from "@t3tools/co
import { parseGitRemoteHost } from "@t3tools/source-control-core/shared/definition";
import { repositoryPathFromRemoteUrl } from "@t3tools/source-control-core/server/SourceControlProvider";
import { canonicalRepositoryPath, normalizeGitRemoteUrl } from "@t3tools/shared/git";
import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment";
import * as Cache from "effect/Cache";
import * as Context from "effect/Context";
import * as Data from "effect/Data";
Expand All @@ -14,7 +15,7 @@ import * as Path from "effect/Path";

import * as ProcessRunner from "../processRunner.ts";
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
import { explicitBareRepositoryEnv, unsetInheritedRepositoryEnv } from "../vcs/VcsProcess.ts";
import { explicitBareRepositoryEnv } from "../vcs/VcsProcess.ts";
import * as HostProcess from "@t3tools/shared/HostProcess";

const DEFAULT_REPOSITORY_IDENTITY_CACHE_CAPACITY = 512;
Expand Down
3 changes: 2 additions & 1 deletion apps/server/src/project/worktreeContainer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment";
import * as HostProcess from "@t3tools/shared/HostProcess";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";

import * as ProcessRunner from "../processRunner.ts";
import { explicitBareRepositoryEnv, unsetInheritedRepositoryEnv } from "../vcs/VcsProcess.ts";
import { explicitBareRepositoryEnv } from "../vcs/VcsProcess.ts";

const parseGitfileTarget = (contents: string): string | undefined => {
const firstLine = contents.split(/\r?\n/u, 1)[0] ?? "";
Expand Down
7 changes: 2 additions & 5 deletions apps/server/src/vcs/GitVcsDriverCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
type VcsRef,
} from "@t3tools/contracts";
import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git";
import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { compactTraceAttributes } from "@t3tools/shared/observability";
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
Expand All @@ -47,11 +48,7 @@ import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observabil
import * as GitVcsDriver from "./GitVcsDriver.ts";
import { removeEmptyDirectory, type RemoveEmptyDirectoryError } from "./removeEmptyDirectory.ts";
import { VcsAutomaticFetchInterval } from "./VcsAutomaticFetchInterval.ts";
import {
explicitBareRepositoryEnv,
gitConfigEnv,
unsetInheritedRepositoryEnv,
} from "./VcsProcess.ts";
import { explicitBareRepositoryEnv, gitConfigEnv } from "./VcsProcess.ts";
import { makeWorktreeCheckoutCache } from "./worktreeCheckoutCache.ts";
import { removeLinkedWorktree } from "./worktreeRemoval.ts";
import { resolveWorktreesDirectory } from "../worktreesDirectory.ts";
Expand Down
24 changes: 1 addition & 23 deletions apps/server/src/vcs/VcsProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
VcsProcessStdinWriteError,
VcsProcessTimeoutError,
} from "@t3tools/contracts";
import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment";
import * as HostProcess from "@t3tools/shared/HostProcess";
import type { SourceControlProcessInput } from "@t3tools/source-control-core/server/SourceControlHost";

Expand Down Expand Up @@ -135,29 +136,6 @@ export const gitConfigEnv = (
export const explicitBareRepositoryEnv = (env: NodeJS.ProcessEnv, platform: NodeJS.Platform) =>
gitConfigEnv(env, "safe.bareRepository", "explicit", platform);

const REPOSITORY_SELECTORS = new Set([
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_COMMON_DIR",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
]);

/**
* Unsets repository selectors the server inherited, such as a Git hook's `GIT_DIR`, so Git finds
* the repository from cwd. Spread it before a command's own env, which may still set them.
*/
export const unsetInheritedRepositoryEnv = (
inherited: NodeJS.ProcessEnv,
platform: NodeJS.Platform,
): NodeJS.ProcessEnv =>
Object.fromEntries(
Object.keys(inherited)
.filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name))
.map((name) => [name, undefined]),
);

const classifyNonZeroExit = (
stderr: string,
classifyFailure: VcsProcessInput["classifyFailure"],
Expand Down
4 changes: 2 additions & 2 deletions packages/provider-testing/src/replayWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as Schema from "effect/Schema";
import * as ChildProcess from "effect/process/ChildProcess";
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
import * as HostProcess from "@t3tools/shared/HostProcess";
import { unsetInheritedGitRepositoryEnv } from "@t3tools/shared/testing/gitRepositoryEnv";
import { unsetInheritedRepositoryEnv } from "@t3tools/shared/gitEnvironment";

class ReplayFixtureGitCommandError extends Schema.TaggedError<ReplayFixtureGitCommandError>()(
"ReplayFixtureGitCommandError",
Expand All @@ -31,7 +31,7 @@ function runGit(
> {
return Effect.gen(function* () {
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
const env = unsetInheritedGitRepositoryEnv(process.env, yield* HostProcess.Platform);
const env = unsetInheritedRepositoryEnv(process.env, yield* HostProcess.Platform);
const exitCode = yield* spawner.exitCode(
ChildProcess.make("git", args, { cwd, env, extendEnv: true }),
);
Expand Down
10 changes: 7 additions & 3 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
"types": "./src/git.ts",
"import": "./src/git.ts"
},
"./gitEnvironment": {
"types": "./src/gitEnvironment.ts",
"import": "./src/gitEnvironment.ts"
},
"./sourceControl": {
"types": "./src/sourceControl.ts",
"import": "./src/sourceControl.ts"
Expand Down Expand Up @@ -375,9 +379,9 @@
"types": "./src/HostProcess.ts",
"import": "./src/HostProcess.ts"
},
"./testing/gitRepositoryEnv": {
"types": "./src/testing/gitRepositoryEnv.ts",
"import": "./src/testing/gitRepositoryEnv.ts"
"./testing/gitEnv": {
"types": "./src/testing/gitEnv.ts",
"import": "./src/testing/gitEnv.ts"
},
"./testing/longTempDir": {
"types": "./src/testing/longTempDir.ts",
Expand Down
22 changes: 22 additions & 0 deletions packages/shared/src/gitEnvironment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const REPOSITORY_SELECTORS = new Set([
"GIT_DIR",
"GIT_WORK_TREE",
"GIT_COMMON_DIR",
"GIT_INDEX_FILE",
"GIT_OBJECT_DIRECTORY",
"GIT_ALTERNATE_OBJECT_DIRECTORIES",
]);

/**
* Unsets repository selectors the caller inherited, such as a Git hook's `GIT_DIR`, so Git finds
* the repository from cwd. Spread it before a command's own env, which may still set them.
*/
export const unsetInheritedRepositoryEnv = (
inherited: NodeJS.ProcessEnv,
platform: NodeJS.Platform,
): NodeJS.ProcessEnv =>
Object.fromEntries(
Object.keys(inherited)
.filter((name) => REPOSITORY_SELECTORS.has(platform === "win32" ? name.toUpperCase() : name))
.map((name) => [name, undefined]),
);
6 changes: 3 additions & 3 deletions packages/shared/src/testing/gitEnv.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it, onTestFinished, vi } from "vite-plus/test";

import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";
import { unsetInheritedRepositoryEnv } from "../gitEnvironment.ts";

describe("git test setup", () => {
it("starts tests without the repository selectors git exported to the run", async () => {
Expand All @@ -21,7 +21,7 @@ describe("git test setup", () => {

it("unsets a selector under the spelling Windows passed it in", () => {
const inherited = { Git_Dir: "C:\\repo\\.git", GIT_EDITOR: "true", PATH: "C:\\bin" };
expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]);
expect(Object.keys(unsetInheritedGitRepositoryEnv(inherited, "linux"))).toEqual([]);
expect(Object.keys(unsetInheritedRepositoryEnv(inherited, "win32"))).toEqual(["Git_Dir"]);
expect(Object.keys(unsetInheritedRepositoryEnv(inherited, "linux"))).toEqual([]);
});
});
4 changes: 2 additions & 2 deletions packages/shared/src/testing/gitEnv.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as HostProcess from "../HostProcess.ts";
import { unsetInheritedGitRepositoryEnv } from "./gitRepositoryEnv.ts";
import { unsetInheritedRepositoryEnv } from "../gitEnvironment.ts";

// A test run started from `git bisect run`, `git rebase --exec` or a hook inherits GIT_DIR. Every git
// command a test or the code under test starts would then write into that repository.
for (const name of Object.keys(
unsetInheritedGitRepositoryEnv(process.env, HostProcess.Platform.defaultValue()),
unsetInheritedRepositoryEnv(process.env, HostProcess.Platform.defaultValue()),
)) {
delete process.env[name];
}
24 changes: 0 additions & 24 deletions packages/shared/src/testing/gitRepositoryEnv.ts

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/check-changed.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,10 @@ describe("check:changed", () => {
});

it.each([
"packages/shared/src/gitEnvironment.ts",
"packages/shared/src/testing/gitEnv.ts",
"packages/shared/src/testing/longTempDir.ts",
"packages/shared/src/testing/providerEnv.ts",
"packages/shared/src/testing/gitEnv.ts",
"packages/shared/src/testing/gitRepositoryEnv.ts",
])("tests every package when the shared test setup file %s changes", (file) => {
assert.strictEqual(decide([file], readWorkspace(), false).outputs.test_packages, "");
});
Expand Down
1 change: 1 addition & 0 deletions scripts/mobile-behavior/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* An isolated T3 server for one device run. Its only provider is `evalbot`, an Fx instance
* backed by `fake-fx.ts`, so every reply is scripted. State lives under the run's `--out`.
*/
import "@t3tools/shared/testing/gitEnv";
import * as NodeChildProcess from "node:child_process";
import * as NodeFS from "node:fs";
import * as NodeNet from "node:net";
Expand Down
1 change: 1 addition & 0 deletions scripts/mobile-showcase-environment.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @effect-diagnostics nodeBuiltinImport:off globalTimers:off globalDate:off - This host-side fixture creates an isolated local T3 environment.
import "@t3tools/shared/testing/gitEnv";
import * as NodeChildProcess from "node:child_process";
import * as NodeFSP from "node:fs/promises";
import * as NodePath from "node:path";
Expand Down
1 change: 1 addition & 0 deletions scripts/scenarios/shell.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import "@t3tools/shared/testing/gitEnv";
import * as Console from "effect/Console";
import * as Effect from "effect/Effect";
import * as Schema from "effect/Schema";
Expand Down
Loading