Add JJ VCS driver support
- Detect and route jj repositories alongside git - Thread VCS kind through status and UI labels - Add jj driver contract coverage and shared freshness helper
This commit is contained in:
1 parent
128b432bee
commit
c31becadc7
30 files changed
+838
-44
No files matched your search
@@ -331,6 +331,7 @@ export const makeOrchestrationIntegrationHarness = (
|
||||
refreshLocalStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: false,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
|
||||
@@ -704,6 +704,7 @@ export const makeGitManager = Effect.fn("makeGitManager")(function* () {
|
||||
: null;
|
||||
|
||||
return {
|
||||
kind: details.isRepo ? ("git" as const) : ("unknown" as const),
|
||||
isRepo: details.isRepo,
|
||||
...(hostingProvider ? { sourceControlProvider: hostingProvider } : {}),
|
||||
hasPrimaryRemote: details.hasOriginRemote,
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Context, Effect, Layer } from "effect";
|
||||
import {
|
||||
GitManagerError,
|
||||
GitCommandError,
|
||||
type VcsDriverKind,
|
||||
type VcsSwitchRefInput,
|
||||
type VcsSwitchRefResult,
|
||||
type VcsCreateRefInput,
|
||||
@@ -29,6 +30,7 @@ import {
|
||||
import { GitManager, type GitRunStackedActionOptions } from "./GitManager.ts";
|
||||
import { GitVcsDriver } from "../vcs/GitVcsDriver.ts";
|
||||
import { VcsDriverRegistry } from "../vcs/VcsDriverRegistry.ts";
|
||||
import { mergeGitStatusParts } from "@t3tools/shared/git";
|
||||
|
||||
export interface GitWorkflowServiceShape {
|
||||
readonly status: (
|
||||
@@ -92,6 +94,18 @@ const unsupportedGitCommand = (operation: string, cwd: string, detail: string) =
|
||||
detail,
|
||||
});
|
||||
|
||||
const emptyWorkingTree = { files: [], insertions: 0, deletions: 0 } as const;
|
||||
|
||||
const nonGitLocalStatus = (kind: VcsDriverKind, isRepo: boolean): VcsStatusLocalResult => ({
|
||||
kind,
|
||||
isRepo,
|
||||
hasPrimaryRemote: false,
|
||||
isDefaultRef: false,
|
||||
refName: null,
|
||||
hasWorkingTreeChanges: false,
|
||||
workingTree: emptyWorkingTree,
|
||||
});
|
||||
|
||||
export const make = Effect.fn("makeGitWorkflowService")(function* () {
|
||||
const registry = yield* VcsDriverRegistry;
|
||||
const git = yield* GitVcsDriver;
|
||||
@@ -153,10 +167,60 @@ export const make = Effect.fn("makeGitWorkflowService")(function* () {
|
||||
(input: Input) =>
|
||||
ensureGit(operation, input.cwd).pipe(Effect.andThen(run(input)));
|
||||
|
||||
const localStatus: GitWorkflowServiceShape["localStatus"] = Effect.fn(
|
||||
"GitWorkflowService.localStatus",
|
||||
)(function* (input) {
|
||||
const handle = yield* registry
|
||||
.detect({ cwd: input.cwd })
|
||||
.pipe(
|
||||
Effect.mapError((error) =>
|
||||
unsupportedGitWorkflow(
|
||||
"GitWorkflowService.localStatus",
|
||||
input.cwd,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (!handle) {
|
||||
return nonGitLocalStatus("unknown", false);
|
||||
}
|
||||
if (handle.kind === "git") {
|
||||
return yield* gitManager.localStatus(input);
|
||||
}
|
||||
return nonGitLocalStatus(handle.kind, true);
|
||||
});
|
||||
|
||||
const remoteStatus: GitWorkflowServiceShape["remoteStatus"] = Effect.fn(
|
||||
"GitWorkflowService.remoteStatus",
|
||||
)(function* (input) {
|
||||
const handle = yield* registry
|
||||
.detect({ cwd: input.cwd })
|
||||
.pipe(
|
||||
Effect.mapError((error) =>
|
||||
unsupportedGitWorkflow(
|
||||
"GitWorkflowService.remoteStatus",
|
||||
input.cwd,
|
||||
error instanceof Error ? error.message : String(error),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (handle?.kind === "git") {
|
||||
return yield* gitManager.remoteStatus(input);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
const status: GitWorkflowServiceShape["status"] = Effect.fn("GitWorkflowService.status")(
|
||||
function* (input) {
|
||||
const [local, remote] = yield* Effect.all([localStatus(input), remoteStatus(input)]);
|
||||
return mergeGitStatusParts(local, remote);
|
||||
},
|
||||
);
|
||||
|
||||
return GitWorkflowService.of({
|
||||
status: routeGitManager("GitWorkflowService.status", gitManager.status),
|
||||
localStatus: routeGitManager("GitWorkflowService.localStatus", gitManager.localStatus),
|
||||
remoteStatus: routeGitManager("GitWorkflowService.remoteStatus", gitManager.remoteStatus),
|
||||
status,
|
||||
localStatus,
|
||||
remoteStatus,
|
||||
invalidateLocalStatus: gitManager.invalidateLocalStatus,
|
||||
invalidateRemoteStatus: gitManager.invalidateRemoteStatus,
|
||||
invalidateStatus: gitManager.invalidateStatus,
|
||||
|
||||
@@ -291,6 +291,7 @@ describe("CheckpointReactor", () => {
|
||||
}).pipe(
|
||||
Effect.as({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: false,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
|
||||
@@ -240,6 +240,7 @@ describe("ProviderCommandReactor", () => {
|
||||
const refreshStatus = vi.fn((_: string) =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git" as const,
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "renamed-branch",
|
||||
|
||||
@@ -2373,6 +2373,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
@@ -2389,6 +2390,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
status: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
@@ -2643,6 +2645,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
@@ -2664,6 +2667,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
statusCalls += 1;
|
||||
return {
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
@@ -2720,6 +2724,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "feature/demo",
|
||||
@@ -2741,6 +2746,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
statusCalls += 1;
|
||||
return {
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "feature/demo",
|
||||
@@ -2792,6 +2798,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: true,
|
||||
refName: "main",
|
||||
@@ -2835,6 +2842,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
localStatus: () =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git",
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "feature/demo",
|
||||
@@ -2911,6 +2919,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
Effect.andThen(
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git" as const,
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "feature/demo",
|
||||
@@ -3577,6 +3586,7 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
const refreshStatus = vi.fn((_: string) =>
|
||||
Effect.succeed({
|
||||
isRepo: true,
|
||||
kind: "git" as const,
|
||||
hasPrimaryRemote: true,
|
||||
isDefaultRef: false,
|
||||
refName: "t3code/bootstrap-refName",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Context, DateTime, Effect, Layer, Option } from "effect";
|
||||
import { Context, Effect, Layer } from "effect";
|
||||
import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import {
|
||||
@@ -20,6 +20,7 @@ import {
|
||||
} from "@t3tools/contracts";
|
||||
import { makeGitVcsDriverCore } from "./GitVcsDriverCore.ts";
|
||||
import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts";
|
||||
import { nowFreshness } from "./VcsFreshness.ts";
|
||||
import { VcsProcess, type VcsProcessShape } from "./VcsProcess.ts";
|
||||
|
||||
export interface ExecuteGitInput {
|
||||
@@ -209,15 +210,6 @@ const WORKSPACE_GIT_HARDENED_CONFIG_ARGS = [
|
||||
"core.untrackedCache=false",
|
||||
] as const;
|
||||
|
||||
const nowFreshness = Effect.fn("GitVcsDriver.nowFreshness")(function* () {
|
||||
const now = yield* DateTime.now;
|
||||
return {
|
||||
source: "live-local" as const,
|
||||
observedAt: now,
|
||||
expiresAt: Option.none(),
|
||||
};
|
||||
});
|
||||
|
||||
function splitNullSeparatedPaths(input: string, truncated: boolean): string[] {
|
||||
const parts = input.split("\0");
|
||||
if (parts.length === 0) return [];
|
||||
|
||||
@@ -1314,6 +1314,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
|
||||
const status: GitVcsDriverShape["status"] = (input) =>
|
||||
statusDetails(input.cwd).pipe(
|
||||
Effect.map((details) => ({
|
||||
kind: "git" as const,
|
||||
isRepo: details.isRepo,
|
||||
hasPrimaryRemote: details.hasOriginRemote,
|
||||
isDefaultRef: details.isDefaultBranch,
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
|
||||
import * as NodeServices from "@effect/platform-node/NodeServices";
|
||||
import { assert, it } from "@effect/vitest";
|
||||
import { Effect, FileSystem, Layer, Path, PlatformError } from "effect";
|
||||
import { ChildProcessSpawner } from "effect/unstable/process";
|
||||
import { describe } from "vitest";
|
||||
|
||||
import type { VcsError } from "@t3tools/contracts";
|
||||
import type { VcsProcessInput, VcsProcessOutput } from "./VcsProcess.ts";
|
||||
import { VcsProcess, layer as VcsProcessLayer } from "./VcsProcess.ts";
|
||||
import * as JjVcsDriver from "./JjVcsDriver.ts";
|
||||
import { runVcsDriverContractSuite } from "./testing/VcsDriverContractHarness.ts";
|
||||
|
||||
const JjContractLayer = JjVcsDriver.vcsLayer.pipe(
|
||||
Layer.provideMerge(VcsProcessLayer),
|
||||
Layer.provideMerge(NodeServices.layer),
|
||||
);
|
||||
|
||||
const commandCalls = (calls: ReadonlyArray<VcsProcessInput>) =>
|
||||
calls.map((call) => [call.command].concat(call.args));
|
||||
|
||||
const processOutput = (stdout: string, exitCode = 0, stderr = ""): VcsProcessOutput => ({
|
||||
exitCode: ChildProcessSpawner.ExitCode(exitCode),
|
||||
stdout,
|
||||
stderr,
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
});
|
||||
|
||||
const hasJj = spawnSync("jj", ["--version"], { stdio: "ignore" }).status === 0;
|
||||
|
||||
const runJj = (cwd: string, args: ReadonlyArray<string>) =>
|
||||
Effect.gen(function* () {
|
||||
const process = yield* VcsProcess;
|
||||
yield* process.run({
|
||||
operation: "JjVcsDriver.contract.jj",
|
||||
command: "jj",
|
||||
cwd,
|
||||
args,
|
||||
timeoutMs: 10_000,
|
||||
});
|
||||
});
|
||||
|
||||
type JjContractError = PlatformError.PlatformError | VcsError;
|
||||
|
||||
if (hasJj) {
|
||||
runVcsDriverContractSuite<VcsProcess, JjContractError>({
|
||||
name: "JJ",
|
||||
kind: "jj",
|
||||
layer: JjContractLayer,
|
||||
fixture: {
|
||||
createRepo: (cwd) => runJj(cwd, ["git", "init", "--colocate"]),
|
||||
writeFile: (cwd, relativePath, contents) =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
const absolutePath = path.join(cwd, relativePath);
|
||||
yield* fileSystem.makeDirectory(path.dirname(absolutePath), { recursive: true });
|
||||
yield* fileSystem.writeFileString(absolutePath, contents);
|
||||
}),
|
||||
ignorePath: (cwd, pattern) =>
|
||||
Effect.gen(function* () {
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const path = yield* Path.Path;
|
||||
yield* fileSystem.writeFileString(path.join(cwd, ".gitignore"), `${pattern}\n`);
|
||||
}),
|
||||
},
|
||||
});
|
||||
} else {
|
||||
it("skips the JJ VCS driver contract when jj is not installed", () => {
|
||||
assert.isFalse(hasJj);
|
||||
});
|
||||
}
|
||||
|
||||
describe("JjVcsDriver", () => {
|
||||
it.effect("detects repository identity with jj root", () => {
|
||||
const calls: VcsProcessInput[] = [];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const driver = yield* JjVcsDriver.makeVcsDriverShape();
|
||||
const identity = yield* driver.detectRepository("/repo/src");
|
||||
|
||||
assert.equal(identity?.kind, "jj");
|
||||
assert.equal(identity?.rootPath, "/repo");
|
||||
assert.equal(identity?.metadataPath, "/repo/.jj");
|
||||
assert.deepStrictEqual(commandCalls(calls), [["jj", "--no-pager", "root"]]);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
calls.push(input);
|
||||
return processOutput("/repo\n");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("lists workspace files using jj file list", () => {
|
||||
let observedInput: VcsProcessInput | null = null;
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const driver = yield* JjVcsDriver.makeVcsDriverShape();
|
||||
const result = yield* driver.listWorkspaceFiles("/repo");
|
||||
|
||||
assert.deepStrictEqual(result.paths, ["README.md", "src/index.ts"]);
|
||||
assert.deepStrictEqual(observedInput?.args, ["--no-pager", "file", "list"]);
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
observedInput = input;
|
||||
return processOutput("README.md\nsrc/index.ts\n");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("filters paths with the git ignore oracle", () => {
|
||||
const calls: VcsProcessInput[] = [];
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const driver = yield* JjVcsDriver.makeVcsDriverShape();
|
||||
const result = yield* driver.filterIgnoredPaths("/repo", [
|
||||
"keep.ts",
|
||||
"debug.log",
|
||||
"src/index.ts",
|
||||
]);
|
||||
|
||||
assert.deepStrictEqual(result, ["keep.ts", "src/index.ts"]);
|
||||
assert.equal(calls[0]?.command, "git");
|
||||
assert.deepStrictEqual(calls[0]?.args.slice(-2), ["init", "--bare"]);
|
||||
assert.equal(calls[1]?.command, "git");
|
||||
assert.deepStrictEqual(calls[1]?.args.slice(-4), [
|
||||
"check-ignore",
|
||||
"--no-index",
|
||||
"-z",
|
||||
"--stdin",
|
||||
]);
|
||||
assert.equal(calls[1]?.stdin, "keep.ts\0debug.log\0src/index.ts\0");
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
calls.push(input);
|
||||
if (input.command === "git" && input.args.includes("check-ignore")) {
|
||||
return processOutput("debug.log\0");
|
||||
}
|
||||
return processOutput("");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
it.effect("forwards execute env to the VCS process", () => {
|
||||
let observedEnv: NodeJS.ProcessEnv | undefined;
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const driver = yield* JjVcsDriver.makeVcsDriverShape();
|
||||
|
||||
yield* driver.execute({
|
||||
operation: "JjVcsDriver.test.env",
|
||||
cwd: "/repo",
|
||||
args: ["status"],
|
||||
env: {
|
||||
JJ_CONFIG: "/tmp/t3-jj-config.toml",
|
||||
},
|
||||
});
|
||||
|
||||
assert.deepStrictEqual(observedEnv, {
|
||||
JJ_CONFIG: "/tmp/t3-jj-config.toml",
|
||||
});
|
||||
}).pipe(
|
||||
Effect.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
observedEnv = input.env;
|
||||
return processOutput("");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,319 @@
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { Effect, Layer } from "effect";
|
||||
|
||||
import { VcsOutputDecodeError, VcsProcessExitError } from "@t3tools/contracts";
|
||||
import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts";
|
||||
import { nowFreshness } from "./VcsFreshness.ts";
|
||||
import { VcsProcess, type VcsProcessShape } from "./VcsProcess.ts";
|
||||
|
||||
const WORKSPACE_FILES_MAX_OUTPUT_BYTES = 16 * 1024 * 1024;
|
||||
const CHECK_IGNORE_MAX_STDIN_BYTES = 256 * 1024;
|
||||
|
||||
function splitNullSeparatedPaths(input: string, truncated: boolean): string[] {
|
||||
const parts = input.split("\0");
|
||||
if (parts.length === 0) return [];
|
||||
|
||||
if (truncated && parts[parts.length - 1]?.length) {
|
||||
parts.pop();
|
||||
}
|
||||
|
||||
return parts.filter((value) => value.length > 0);
|
||||
}
|
||||
|
||||
function splitLineSeparatedPaths(input: string, truncated: boolean): string[] {
|
||||
const lines = input.split(/\r?\n/g);
|
||||
if (truncated && lines[lines.length - 1]?.length) {
|
||||
lines.pop();
|
||||
}
|
||||
|
||||
return lines.map((line) => line.trim()).filter((line) => line.length > 0);
|
||||
}
|
||||
|
||||
function chunkPathsForCheckIgnore(relativePaths: ReadonlyArray<string>): string[][] {
|
||||
const chunks: string[][] = [];
|
||||
let chunk: string[] = [];
|
||||
let chunkBytes = 0;
|
||||
|
||||
for (const relativePath of relativePaths) {
|
||||
const relativePathBytes = Buffer.byteLength(relativePath) + 1;
|
||||
if (chunk.length > 0 && chunkBytes + relativePathBytes > CHECK_IGNORE_MAX_STDIN_BYTES) {
|
||||
chunks.push(chunk);
|
||||
chunk = [];
|
||||
chunkBytes = 0;
|
||||
}
|
||||
|
||||
chunk.push(relativePath);
|
||||
chunkBytes += relativePathBytes;
|
||||
|
||||
if (chunkBytes >= CHECK_IGNORE_MAX_STDIN_BYTES) {
|
||||
chunks.push(chunk);
|
||||
chunk = [];
|
||||
chunkBytes = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (chunk.length > 0) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
return chunks;
|
||||
}
|
||||
|
||||
const processCommand = (
|
||||
process: VcsProcessShape,
|
||||
command: string,
|
||||
operation: string,
|
||||
cwd: string,
|
||||
args: ReadonlyArray<string>,
|
||||
options?: {
|
||||
readonly stdin?: string;
|
||||
readonly env?: NodeJS.ProcessEnv;
|
||||
readonly allowNonZeroExit?: boolean;
|
||||
readonly timeoutMs?: number;
|
||||
readonly maxOutputBytes?: number;
|
||||
readonly truncateOutputAtMaxBytes?: boolean;
|
||||
},
|
||||
) =>
|
||||
process.run({
|
||||
operation,
|
||||
command,
|
||||
args,
|
||||
cwd,
|
||||
...(options?.stdin !== undefined ? { stdin: options.stdin } : {}),
|
||||
...(options?.env !== undefined ? { env: options.env } : {}),
|
||||
...(options?.allowNonZeroExit !== undefined
|
||||
? { allowNonZeroExit: options.allowNonZeroExit }
|
||||
: {}),
|
||||
...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}),
|
||||
...(options?.maxOutputBytes !== undefined ? { maxOutputBytes: options.maxOutputBytes } : {}),
|
||||
...(options?.truncateOutputAtMaxBytes !== undefined
|
||||
? { truncateOutputAtMaxBytes: options.truncateOutputAtMaxBytes }
|
||||
: {}),
|
||||
});
|
||||
|
||||
const jjCommand = (
|
||||
process: VcsProcessShape,
|
||||
operation: string,
|
||||
cwd: string,
|
||||
args: ReadonlyArray<string>,
|
||||
options?: {
|
||||
readonly stdin?: string;
|
||||
readonly env?: NodeJS.ProcessEnv;
|
||||
readonly allowNonZeroExit?: boolean;
|
||||
readonly timeoutMs?: number;
|
||||
readonly maxOutputBytes?: number;
|
||||
readonly truncateOutputAtMaxBytes?: boolean;
|
||||
},
|
||||
) => processCommand(process, "jj", operation, cwd, ["--no-pager", ...args], options);
|
||||
|
||||
const gitCommand = (
|
||||
process: VcsProcessShape,
|
||||
operation: string,
|
||||
cwd: string,
|
||||
args: ReadonlyArray<string>,
|
||||
options?: Parameters<typeof processCommand>[5],
|
||||
) => processCommand(process, "git", operation, cwd, args, options);
|
||||
|
||||
function tempDirError(operation: string, cwd: string, detail: string, cause: unknown) {
|
||||
return new VcsOutputDecodeError({
|
||||
operation,
|
||||
command: "git check-ignore",
|
||||
cwd,
|
||||
detail,
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
const makeTempGitDir = (operation: string, cwd: string) =>
|
||||
Effect.tryPromise({
|
||||
try: () => mkdtemp(join(tmpdir(), "t3-jj-check-ignore-")),
|
||||
catch: (cause) => tempDirError(operation, cwd, "failed to create temp git dir", cause),
|
||||
});
|
||||
|
||||
const removeTempGitDir = (gitDir: string) =>
|
||||
Effect.promise(() => rm(gitDir, { force: true, recursive: true })).pipe(Effect.ignore);
|
||||
|
||||
export const makeVcsDriverShape = Effect.fn("makeJjVcsDriverShape")(function* () {
|
||||
const process = yield* VcsProcess;
|
||||
const capabilities = {
|
||||
kind: "jj" as const,
|
||||
supportsWorktrees: true,
|
||||
supportsBookmarks: true,
|
||||
supportsAtomicSnapshot: true,
|
||||
supportsPushDefaultRemote: false,
|
||||
};
|
||||
|
||||
const isInsideWorkTree: VcsDriverShape["isInsideWorkTree"] = (cwd) =>
|
||||
jjCommand(process, "JjVcsDriver.isInsideWorkTree", cwd, ["root"], {
|
||||
allowNonZeroExit: true,
|
||||
timeoutMs: 5_000,
|
||||
maxOutputBytes: 4_096,
|
||||
}).pipe(
|
||||
Effect.map((result) => result.exitCode === 0 && result.stdout.trim().length > 0),
|
||||
Effect.catch(() => Effect.succeed(false)),
|
||||
);
|
||||
|
||||
const execute: VcsDriverShape["execute"] = (input) =>
|
||||
jjCommand(process, input.operation, input.cwd, input.args, {
|
||||
...(input.stdin !== undefined ? { stdin: input.stdin } : {}),
|
||||
...(input.env !== undefined ? { env: input.env } : {}),
|
||||
...(input.allowNonZeroExit !== undefined ? { allowNonZeroExit: input.allowNonZeroExit } : {}),
|
||||
...(input.timeoutMs !== undefined ? { timeoutMs: input.timeoutMs } : {}),
|
||||
...(input.maxOutputBytes !== undefined ? { maxOutputBytes: input.maxOutputBytes } : {}),
|
||||
...(input.truncateOutputAtMaxBytes !== undefined
|
||||
? { truncateOutputAtMaxBytes: input.truncateOutputAtMaxBytes }
|
||||
: {}),
|
||||
});
|
||||
|
||||
const detectRepository: VcsDriverShape["detectRepository"] = Effect.fn("detectRepository")(
|
||||
function* (cwd) {
|
||||
const root = yield* jjCommand(process, "JjVcsDriver.detectRepository.root", cwd, ["root"], {
|
||||
allowNonZeroExit: true,
|
||||
timeoutMs: 5_000,
|
||||
maxOutputBytes: 4_096,
|
||||
}).pipe(Effect.catch(() => Effect.succeed(null)));
|
||||
if (!root || root.exitCode !== 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const rootPath = root.stdout.trim();
|
||||
if (rootPath.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
kind: "jj" as const,
|
||||
rootPath,
|
||||
metadataPath: `${rootPath.replace(/[\\/]$/g, "")}/.jj`,
|
||||
freshness: yield* nowFreshness(),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
const listWorkspaceFiles: VcsDriverShape["listWorkspaceFiles"] = (cwd) =>
|
||||
jjCommand(process, "JjVcsDriver.listWorkspaceFiles", cwd, ["file", "list"], {
|
||||
allowNonZeroExit: true,
|
||||
timeoutMs: 20_000,
|
||||
maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
|
||||
truncateOutputAtMaxBytes: true,
|
||||
}).pipe(
|
||||
Effect.flatMap((result) =>
|
||||
result.exitCode === 0
|
||||
? Effect.gen(function* () {
|
||||
return {
|
||||
paths: splitLineSeparatedPaths(result.stdout, result.stdoutTruncated),
|
||||
truncated: result.stdoutTruncated,
|
||||
freshness: yield* nowFreshness(),
|
||||
};
|
||||
})
|
||||
: Effect.fail(
|
||||
new VcsProcessExitError({
|
||||
operation: "JjVcsDriver.listWorkspaceFiles",
|
||||
command: "jj file list",
|
||||
cwd,
|
||||
exitCode: result.exitCode,
|
||||
detail: result.stderr.trim() || "jj file list failed",
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const filterIgnoredPaths: VcsDriverShape["filterIgnoredPaths"] = Effect.fn("filterIgnoredPaths")(
|
||||
function* (cwd, relativePaths) {
|
||||
if (relativePaths.length === 0) {
|
||||
return relativePaths;
|
||||
}
|
||||
|
||||
const operation = "JjVcsDriver.filterIgnoredPaths";
|
||||
const ignoredPaths = new Set<string>();
|
||||
yield* Effect.acquireUseRelease(
|
||||
makeTempGitDir(operation, cwd),
|
||||
Effect.fn("JjVcsDriver.filterIgnoredPaths.withTempGitDir")(function* (gitDir) {
|
||||
const initResult = yield* gitCommand(process, operation, cwd, [
|
||||
"--git-dir",
|
||||
gitDir,
|
||||
"init",
|
||||
"--bare",
|
||||
]);
|
||||
if (initResult.exitCode !== 0) {
|
||||
return yield* new VcsProcessExitError({
|
||||
operation,
|
||||
command: "git init --bare",
|
||||
cwd,
|
||||
exitCode: initResult.exitCode,
|
||||
detail: initResult.stderr.trim() || "git init --bare failed",
|
||||
});
|
||||
}
|
||||
|
||||
for (const chunk of chunkPathsForCheckIgnore(relativePaths)) {
|
||||
const result = yield* gitCommand(
|
||||
process,
|
||||
operation,
|
||||
cwd,
|
||||
[
|
||||
"--git-dir",
|
||||
gitDir,
|
||||
"--work-tree",
|
||||
cwd,
|
||||
"check-ignore",
|
||||
"--no-index",
|
||||
"-z",
|
||||
"--stdin",
|
||||
],
|
||||
{
|
||||
stdin: `${chunk.join("\0")}\0`,
|
||||
allowNonZeroExit: true,
|
||||
timeoutMs: 20_000,
|
||||
maxOutputBytes: WORKSPACE_FILES_MAX_OUTPUT_BYTES,
|
||||
truncateOutputAtMaxBytes: true,
|
||||
},
|
||||
);
|
||||
|
||||
if (result.exitCode !== 0 && result.exitCode !== 1) {
|
||||
return yield* new VcsProcessExitError({
|
||||
operation,
|
||||
command: "git check-ignore",
|
||||
cwd,
|
||||
exitCode: result.exitCode,
|
||||
detail: result.stderr.trim() || "git check-ignore failed",
|
||||
});
|
||||
}
|
||||
|
||||
for (const ignoredPath of splitNullSeparatedPaths(
|
||||
result.stdout,
|
||||
result.stdoutTruncated,
|
||||
)) {
|
||||
ignoredPaths.add(ignoredPath);
|
||||
}
|
||||
}
|
||||
}),
|
||||
removeTempGitDir,
|
||||
);
|
||||
|
||||
if (ignoredPaths.size === 0) {
|
||||
return relativePaths;
|
||||
}
|
||||
|
||||
return relativePaths.filter((relativePath) => !ignoredPaths.has(relativePath));
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
capabilities,
|
||||
execute,
|
||||
detectRepository,
|
||||
isInsideWorkTree,
|
||||
listWorkspaceFiles,
|
||||
filterIgnoredPaths,
|
||||
} satisfies VcsDriverShape;
|
||||
});
|
||||
|
||||
export const makeVcsDriver = Effect.fn("makeJjVcsDriver")(function* () {
|
||||
const driver = yield* makeVcsDriverShape();
|
||||
return VcsDriver.of(driver);
|
||||
});
|
||||
|
||||
export const vcsLayer = Layer.effect(VcsDriver, makeVcsDriver());
|
||||
@@ -7,10 +7,13 @@ import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "./VcsPr
|
||||
import { VcsProjectConfig } from "./VcsProjectConfig.ts";
|
||||
import { VcsDriverRegistry, make as makeVcsDriverRegistry } from "./VcsDriverRegistry.ts";
|
||||
|
||||
const processOutput = (stdout: string): VcsProcessOutput => ({
|
||||
exitCode: ChildProcessSpawner.ExitCode(0),
|
||||
const commandCalls = (calls: ReadonlyArray<VcsProcessInput>) =>
|
||||
calls.map((call) => [call.command].concat(call.args));
|
||||
|
||||
const processOutput = (stdout: string, exitCode = 0, stderr = ""): VcsProcessOutput => ({
|
||||
exitCode: ChildProcessSpawner.ExitCode(exitCode),
|
||||
stdout,
|
||||
stderr: "",
|
||||
stderr,
|
||||
stdoutTruncated: false,
|
||||
stderrTruncated: false,
|
||||
});
|
||||
@@ -62,4 +65,76 @@ describe("VcsDriverRegistry", () => {
|
||||
);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("resolves an explicitly requested jj repository", () => {
|
||||
const calls: VcsProcessInput[] = [];
|
||||
const layer = Layer.effect(VcsDriverRegistry, makeVcsDriverRegistry()).pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(VcsProjectConfig)({
|
||||
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
calls.push(input);
|
||||
const command = input.args.join(" ");
|
||||
if (input.command === "jj" && command === "--no-pager root") {
|
||||
return processOutput("/repo\n");
|
||||
}
|
||||
return processOutput("", 1, "not found");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const registry = yield* VcsDriverRegistry;
|
||||
const handle = yield* registry.resolve({ cwd: "/repo", requestedKind: "jj" });
|
||||
|
||||
assert.equal(handle.kind, "jj");
|
||||
assert.equal(handle.repository.rootPath, "/repo");
|
||||
assert.deepStrictEqual(commandCalls(calls), [["jj", "--no-pager", "root"]]);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
|
||||
it.effect("auto-detects jj after git misses", () => {
|
||||
const calls: VcsProcessInput[] = [];
|
||||
const layer = Layer.effect(VcsDriverRegistry, makeVcsDriverRegistry()).pipe(
|
||||
Layer.provide(
|
||||
Layer.mock(VcsProjectConfig)({
|
||||
resolveKind: (input) => Effect.succeed(input.requestedKind ?? "auto"),
|
||||
}),
|
||||
),
|
||||
Layer.provide(
|
||||
Layer.mock(VcsProcess)({
|
||||
run: (input) =>
|
||||
Effect.sync(() => {
|
||||
calls.push(input);
|
||||
const command = input.args.join(" ");
|
||||
if (input.command === "git" && command === "rev-parse --is-inside-work-tree") {
|
||||
return processOutput("", 1, "not a git repository");
|
||||
}
|
||||
if (input.command === "jj" && command === "--no-pager root") {
|
||||
return processOutput("/repo\n");
|
||||
}
|
||||
return processOutput("");
|
||||
}),
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
return Effect.gen(function* () {
|
||||
const registry = yield* VcsDriverRegistry;
|
||||
const handle = yield* registry.resolve({ cwd: "/repo" });
|
||||
|
||||
assert.equal(handle.kind, "jj");
|
||||
assert.equal(handle.repository.rootPath, "/repo");
|
||||
assert.deepStrictEqual(commandCalls(calls), [
|
||||
["git", "rev-parse", "--is-inside-work-tree"],
|
||||
["jj", "--no-pager", "root"],
|
||||
]);
|
||||
}).pipe(Effect.provide(layer));
|
||||
});
|
||||
});
|
||||
@@ -3,6 +3,7 @@ import { Cache, Context, Duration, Effect, Exit, Layer } from "effect";
|
||||
import type { VcsDriverKind, VcsError, VcsRepositoryIdentity } from "@t3tools/contracts";
|
||||
import { VcsUnsupportedOperationError } from "@t3tools/contracts";
|
||||
import * as GitVcsDriver from "./GitVcsDriver.ts";
|
||||
import * as JjVcsDriver from "./JjVcsDriver.ts";
|
||||
import * as VcsProjectConfig from "./VcsProjectConfig.ts";
|
||||
import type { VcsDriverShape } from "./VcsDriver.ts";
|
||||
|
||||
@@ -65,8 +66,10 @@ function parseDetectionCacheKey(key: string): {
|
||||
export const make = Effect.fn("makeVcsDriverRegistry")(function* () {
|
||||
const projectConfig = yield* VcsProjectConfig.VcsProjectConfig;
|
||||
const git = yield* GitVcsDriver.makeVcsDriverShape();
|
||||
const jj = yield* JjVcsDriver.makeVcsDriverShape();
|
||||
const drivers: Partial<Record<VcsDriverKind, VcsDriverShape>> = {
|
||||
git,
|
||||
jj,
|
||||
};
|
||||
|
||||
const detectWithDriver = Effect.fn("VcsDriverRegistry.detectWithDriver")(function* (
|
||||
@@ -103,7 +106,12 @@ export const make = Effect.fn("makeVcsDriverRegistry")(function* () {
|
||||
return yield* detectWithDriver(requestedKind, driver, input.cwd);
|
||||
}
|
||||
|
||||
return yield* detectWithDriver("git", git, input.cwd);
|
||||
const gitDetected = yield* detectWithDriver("git", git, input.cwd);
|
||||
if (gitDetected) {
|
||||
return gitDetected;
|
||||
}
|
||||
|
||||
return yield* detectWithDriver("jj", jj, input.cwd);
|
||||
});
|
||||
|
||||
const detectionCache = yield* Cache.makeWith<string, VcsDriverHandle | null, VcsError>(
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { DateTime, Effect, Option } from "effect";
|
||||
|
||||
export const nowFreshness = Effect.fn("VcsFreshness.nowFreshness")(function* () {
|
||||
const now = yield* DateTime.now;
|
||||
return {
|
||||
source: "live-local" as const,
|
||||
observedAt: now,
|
||||
expiresAt: Option.none(),
|
||||
};
|
||||
});
|
||||
@@ -16,6 +16,7 @@ import {
|
||||
import { GitWorkflowService, type GitWorkflowServiceShape } from "../git/GitWorkflowService.ts";
|
||||
|
||||
const baseLocalStatus: VcsStatusLocalResult = {
|
||||
kind: "git",
|
||||
isRepo: true,
|
||||
sourceControlProvider: {
|
||||
kind: "github",
|
||||
|
||||
@@ -56,9 +56,11 @@ export function runVcsDriverContractSuite<R, E>(input: VcsDriverContractSuiteInp
|
||||
|
||||
yield* input.fixture.createRepo(cwd);
|
||||
yield* input.fixture.writeFile(cwd, "src/index.ts", "export const value = 1;\n");
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
const realCwd = yield* fileSystem.realPath(cwd);
|
||||
const identity = yield* driver.detectRepository(cwd);
|
||||
assert.equal(identity?.kind, input.kind);
|
||||
assert.isTrue(identity?.rootPath.endsWith(cwd));
|
||||
assert.isTrue(identity?.rootPath.endsWith(realCwd));
|
||||
assert.equal(identity?.freshness.source, "live-local");
|
||||
assert.isTrue(DateTime.isDateTime(identity?.freshness.observedAt));
|
||||
assert.isTrue(Option.isNone(identity?.freshness.expiresAt ?? Option.none()));
|
||||
|
||||
Reference in new issue
Block a user