From c3300acd4d13d9abdf8cb044580cee0380dca04f Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 11:28:33 -0700 Subject: [PATCH] Refactor git core into pluggable VCS driver - Move low-level git service into `GitVcsDriverCore` - Update server wiring and tests to use the new driver --- apps/server/src/git/Layers/GitManager.ts | 3 +- apps/server/src/git/Services/GitCore.ts | 288 ------- apps/server/src/vcs/GitVcsDriver.ts | 191 ++++- .../GitVcsDriverCore.test.ts} | 633 ++++++++------- .../GitCore.ts => vcs/GitVcsDriverCore.ts} | 722 +++++++++--------- 5 files changed, 931 insertions(+), 906 deletions(-) delete mode 100644 apps/server/src/git/Services/GitCore.ts rename apps/server/src/{git/Layers/GitCore.test.ts => vcs/GitVcsDriverCore.test.ts} (77%) rename apps/server/src/{git/Layers/GitCore.ts => vcs/GitVcsDriverCore.ts} (76%) diff --git a/apps/server/src/git/Layers/GitManager.ts b/apps/server/src/git/Layers/GitManager.ts index a25cb6856b..d8b0f77760 100644 --- a/apps/server/src/git/Layers/GitManager.ts +++ b/apps/server/src/git/Layers/GitManager.ts @@ -38,7 +38,6 @@ import { type GitManagerShape, type GitRunStackedActionOptions, } from "../Services/GitManager.ts"; -import type { GitStatusDetails } from "../Services/GitCore.ts"; import { GitHubCli, type GitHubPullRequestSummary } from "../Services/GitHubCli.ts"; import { TextGeneration } from "../Services/TextGeneration.ts"; import { ProjectSetupScriptRunner } from "../../project/Services/ProjectSetupScriptRunner.ts"; @@ -49,7 +48,7 @@ import { decodeGitHubPullRequestListJson, formatGitHubJsonDecodeError, } from "../githubPullRequests.ts"; -import { GitVcsDriver } from "../../vcs/GitVcsDriver.ts"; +import { GitVcsDriver, type GitStatusDetails } from "../../vcs/GitVcsDriver.ts"; const COMMIT_TIMEOUT_MS = 10 * 60_000; const MAX_PROGRESS_TEXT_LENGTH = 500; diff --git a/apps/server/src/git/Services/GitCore.ts b/apps/server/src/git/Services/GitCore.ts deleted file mode 100644 index 232cbe29d7..0000000000 --- a/apps/server/src/git/Services/GitCore.ts +++ /dev/null @@ -1,288 +0,0 @@ -/** - * GitCore - Effect service contract for low-level Git operations. - * - * Wraps core repository primitives used by higher-level orchestration - * services and WebSocket routes. - * - * @module GitCore - */ -import { Context } from "effect"; -import type { Effect } from "effect"; -import type { - GitCheckoutInput, - GitCheckoutResult, - GitCreateBranchInput, - GitCreateBranchResult, - GitCreateWorktreeInput, - GitCreateWorktreeResult, - GitInitInput, - GitListBranchesInput, - GitListBranchesResult, - GitPullResult, - GitRemoveWorktreeInput, - GitStatusInput, - GitStatusResult, -} from "@t3tools/contracts"; - -import type { GitCommandError } from "@t3tools/contracts"; - -export interface ExecuteGitInput { - readonly operation: string; - readonly cwd: string; - readonly args: ReadonlyArray; - readonly stdin?: string; - readonly env?: NodeJS.ProcessEnv; - readonly allowNonZeroExit?: boolean; - readonly timeoutMs?: number; - readonly maxOutputBytes?: number; - readonly truncateOutputAtMaxBytes?: boolean; - readonly progress?: ExecuteGitProgress; -} - -export interface ExecuteGitResult { - readonly code: number; - readonly stdout: string; - readonly stderr: string; - readonly stdoutTruncated: boolean; - readonly stderrTruncated: boolean; -} - -export interface GitStatusDetails extends Omit { - upstreamRef: string | null; -} - -export interface GitPreparedCommitContext { - stagedSummary: string; - stagedPatch: string; -} - -export interface ExecuteGitProgress { - readonly onStdoutLine?: (line: string) => Effect.Effect; - readonly onStderrLine?: (line: string) => Effect.Effect; - readonly onHookStarted?: (hookName: string) => Effect.Effect; - readonly onHookFinished?: (input: { - hookName: string; - exitCode: number | null; - durationMs: number | null; - }) => Effect.Effect; -} - -export interface GitCommitProgress { - readonly onOutputLine?: (input: { - stream: "stdout" | "stderr"; - text: string; - }) => Effect.Effect; - readonly onHookStarted?: (hookName: string) => Effect.Effect; - readonly onHookFinished?: (input: { - hookName: string; - exitCode: number | null; - durationMs: number | null; - }) => Effect.Effect; -} - -export interface GitCommitOptions { - readonly timeoutMs?: number; - readonly progress?: GitCommitProgress; -} - -export interface GitPushResult { - status: "pushed" | "skipped_up_to_date"; - branch: string; - upstreamBranch?: string | undefined; - setUpstream?: boolean | undefined; -} - -export interface GitRangeContext { - commitSummary: string; - diffSummary: string; - diffPatch: string; -} - -export interface GitRenameBranchInput { - cwd: string; - oldBranch: string; - newBranch: string; -} - -export interface GitRenameBranchResult { - branch: string; -} - -export interface GitFetchPullRequestBranchInput { - cwd: string; - prNumber: number; - branch: string; -} - -export interface GitEnsureRemoteInput { - cwd: string; - preferredName: string; - url: string; -} - -export interface GitFetchRemoteBranchInput { - cwd: string; - remoteName: string; - remoteBranch: string; - localBranch: string; -} - -export interface GitSetBranchUpstreamInput { - cwd: string; - branch: string; - remoteName: string; - remoteBranch: string; -} - -/** - * GitCoreShape - Service API for low-level Git repository interactions. - */ -export interface GitCoreShape { - /** - * Execute a raw Git command. - */ - readonly execute: (input: ExecuteGitInput) => Effect.Effect; - - /** - * Read Git status for a repository. - */ - readonly status: (input: GitStatusInput) => Effect.Effect; - - /** - * Read detailed working tree / branch status for a repository. - */ - readonly statusDetails: (cwd: string) => Effect.Effect; - - /** - * Read detailed working tree / branch status without refreshing remote tracking refs. - */ - readonly statusDetailsLocal: (cwd: string) => Effect.Effect; - - /** - * Build staged change context for commit generation. - */ - readonly prepareCommitContext: ( - cwd: string, - filePaths?: readonly string[], - ) => Effect.Effect; - - /** - * Create a commit with provided subject/body. - */ - readonly commit: ( - cwd: string, - subject: string, - body: string, - options?: GitCommitOptions, - ) => Effect.Effect<{ commitSha: string }, GitCommandError>; - - /** - * Push current branch, setting upstream if needed. - */ - readonly pushCurrentBranch: ( - cwd: string, - fallbackBranch: string | null, - ) => Effect.Effect; - - /** - * Collect commit/diff context between base branch and current HEAD. - */ - readonly readRangeContext: ( - cwd: string, - baseBranch: string, - ) => Effect.Effect; - - /** - * Read a Git config value from the local repository. - */ - readonly readConfigValue: ( - cwd: string, - key: string, - ) => Effect.Effect; - - /** - * List local + remote branches and branch metadata. - */ - readonly listBranches: ( - input: GitListBranchesInput, - ) => Effect.Effect; - - /** - * Pull current branch from upstream using fast-forward only. - */ - readonly pullCurrentBranch: (cwd: string) => Effect.Effect; - - /** - * Create a worktree and branch from a base branch. - */ - readonly createWorktree: ( - input: GitCreateWorktreeInput, - ) => Effect.Effect; - - /** - * Materialize a GitHub pull request head as a local branch without switching checkout. - */ - readonly fetchPullRequestBranch: ( - input: GitFetchPullRequestBranchInput, - ) => Effect.Effect; - - /** - * Ensure a named remote exists for the provided URL, returning the reused or created remote name. - */ - readonly ensureRemote: (input: GitEnsureRemoteInput) => Effect.Effect; - - /** - * Fetch a remote branch into a local branch without checkout. - */ - readonly fetchRemoteBranch: ( - input: GitFetchRemoteBranchInput, - ) => Effect.Effect; - - /** - * Set the upstream tracking branch for a local branch. - */ - readonly setBranchUpstream: ( - input: GitSetBranchUpstreamInput, - ) => Effect.Effect; - - /** - * Remove an existing worktree. - */ - readonly removeWorktree: (input: GitRemoveWorktreeInput) => Effect.Effect; - - /** - * Rename an existing local branch. - */ - readonly renameBranch: ( - input: GitRenameBranchInput, - ) => Effect.Effect; - - /** - * Create a local branch. - */ - readonly createBranch: ( - input: GitCreateBranchInput, - ) => Effect.Effect; - - /** - * Checkout an existing branch and refresh its upstream metadata in background. - */ - readonly checkoutBranch: ( - input: GitCheckoutInput, - ) => Effect.Effect; - - /** - * Initialize a repository in the provided directory. - */ - readonly initRepo: (input: GitInitInput) => Effect.Effect; - - /** - * List local branch names (short format). - */ - readonly listLocalBranchNames: (cwd: string) => Effect.Effect; -} - -/** - * GitCore - Service tag for low-level Git repository operations. - */ -export class GitCore extends Context.Service()("t3/git/Services/GitCore") {} diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index 7df9c13581..9c692780a1 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -1,12 +1,191 @@ import { Context, DateTime, Effect, Layer } from "effect"; -import { VcsProcessExitError } from "@t3tools/contracts"; -import { makeGitCore } from "../git/Layers/GitCore.ts"; -import type { GitCoreShape } from "../git/Services/GitCore.ts"; +import { + GitCommandError, + VcsProcessExitError, + type GitCheckoutInput, + type GitCheckoutResult, + type GitCreateBranchInput, + type GitCreateBranchResult, + type GitCreateWorktreeInput, + type GitCreateWorktreeResult, + type GitInitInput, + type GitListBranchesInput, + type GitListBranchesResult, + type GitPullResult, + type GitRemoveWorktreeInput, + type GitStatusInput, + type GitStatusResult, +} from "@t3tools/contracts"; +import { makeGitVcsDriverCore } from "./GitVcsDriverCore.ts"; import { VcsDriver, type VcsDriverShape } from "./VcsDriver.ts"; import { VcsProcess, type VcsProcessShape } from "./VcsProcess.ts"; -export interface GitVcsDriverShape extends GitCoreShape {} +export interface ExecuteGitInput { + readonly operation: string; + readonly cwd: string; + readonly args: ReadonlyArray; + readonly stdin?: string; + readonly env?: NodeJS.ProcessEnv; + readonly allowNonZeroExit?: boolean; + readonly timeoutMs?: number; + readonly maxOutputBytes?: number; + readonly truncateOutputAtMaxBytes?: boolean; + readonly progress?: ExecuteGitProgress; +} + +export interface ExecuteGitResult { + readonly code: number; + readonly stdout: string; + readonly stderr: string; + readonly stdoutTruncated: boolean; + readonly stderrTruncated: boolean; +} + +export interface GitStatusDetails extends Omit { + upstreamRef: string | null; +} + +export interface GitPreparedCommitContext { + stagedSummary: string; + stagedPatch: string; +} + +export interface ExecuteGitProgress { + readonly onStdoutLine?: (line: string) => Effect.Effect; + readonly onStderrLine?: (line: string) => Effect.Effect; + readonly onHookStarted?: (hookName: string) => Effect.Effect; + readonly onHookFinished?: (input: { + hookName: string; + exitCode: number | null; + durationMs: number | null; + }) => Effect.Effect; +} + +export interface GitCommitProgress { + readonly onOutputLine?: (input: { + stream: "stdout" | "stderr"; + text: string; + }) => Effect.Effect; + readonly onHookStarted?: (hookName: string) => Effect.Effect; + readonly onHookFinished?: (input: { + hookName: string; + exitCode: number | null; + durationMs: number | null; + }) => Effect.Effect; +} + +export interface GitCommitOptions { + readonly timeoutMs?: number; + readonly progress?: GitCommitProgress; +} + +export interface GitPushResult { + status: "pushed" | "skipped_up_to_date"; + branch: string; + upstreamBranch?: string | undefined; + setUpstream?: boolean | undefined; +} + +export interface GitRangeContext { + commitSummary: string; + diffSummary: string; + diffPatch: string; +} + +export interface GitRenameBranchInput { + cwd: string; + oldBranch: string; + newBranch: string; +} + +export interface GitRenameBranchResult { + branch: string; +} + +export interface GitFetchPullRequestBranchInput { + cwd: string; + prNumber: number; + branch: string; +} + +export interface GitEnsureRemoteInput { + cwd: string; + preferredName: string; + url: string; +} + +export interface GitFetchRemoteBranchInput { + cwd: string; + remoteName: string; + remoteBranch: string; + localBranch: string; +} + +export interface GitSetBranchUpstreamInput { + cwd: string; + branch: string; + remoteName: string; + remoteBranch: string; +} + +export interface GitVcsDriverShape { + readonly execute: (input: ExecuteGitInput) => Effect.Effect; + readonly status: (input: GitStatusInput) => Effect.Effect; + readonly statusDetails: (cwd: string) => Effect.Effect; + readonly statusDetailsLocal: (cwd: string) => Effect.Effect; + readonly prepareCommitContext: ( + cwd: string, + filePaths?: readonly string[], + ) => Effect.Effect; + readonly commit: ( + cwd: string, + subject: string, + body: string, + options?: GitCommitOptions, + ) => Effect.Effect<{ commitSha: string }, GitCommandError>; + readonly pushCurrentBranch: ( + cwd: string, + fallbackBranch: string | null, + ) => Effect.Effect; + readonly readRangeContext: ( + cwd: string, + baseBranch: string, + ) => Effect.Effect; + readonly readConfigValue: ( + cwd: string, + key: string, + ) => Effect.Effect; + readonly listBranches: ( + input: GitListBranchesInput, + ) => Effect.Effect; + readonly pullCurrentBranch: (cwd: string) => Effect.Effect; + readonly createWorktree: ( + input: GitCreateWorktreeInput, + ) => Effect.Effect; + readonly fetchPullRequestBranch: ( + input: GitFetchPullRequestBranchInput, + ) => Effect.Effect; + readonly ensureRemote: (input: GitEnsureRemoteInput) => Effect.Effect; + readonly fetchRemoteBranch: ( + input: GitFetchRemoteBranchInput, + ) => Effect.Effect; + readonly setBranchUpstream: ( + input: GitSetBranchUpstreamInput, + ) => Effect.Effect; + readonly removeWorktree: (input: GitRemoveWorktreeInput) => Effect.Effect; + readonly renameBranch: ( + input: GitRenameBranchInput, + ) => Effect.Effect; + readonly createBranch: ( + input: GitCreateBranchInput, + ) => Effect.Effect; + readonly checkoutBranch: ( + input: GitCheckoutInput, + ) => Effect.Effect; + readonly initRepo: (input: GitInitInput) => Effect.Effect; + readonly listLocalBranchNames: (cwd: string) => Effect.Effect; +} export class GitVcsDriver extends Context.Service()( "t3/vcs/GitVcsDriver", @@ -259,8 +438,8 @@ export const makeVcsDriver = Effect.fn("makeGitVcsDriver")(function* () { }); export const make = Effect.fn("makeGitVcsDriverService")(function* () { - const legacyGit = yield* makeGitCore(); - return GitVcsDriver.of(legacyGit); + const git = yield* makeGitVcsDriverCore(); + return GitVcsDriver.of(git); }); export const vcsLayer = Layer.effect(VcsDriver, makeVcsDriver()); diff --git a/apps/server/src/git/Layers/GitCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts similarity index 77% rename from apps/server/src/git/Layers/GitCore.test.ts rename to apps/server/src/vcs/GitVcsDriverCore.test.ts index 2f2c8bd130..044a8dba0f 100644 --- a/apps/server/src/git/Layers/GitCore.test.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts @@ -6,20 +6,20 @@ import { it } from "@effect/vitest"; import { Effect, FileSystem, Layer, PlatformError, Scope } from "effect"; import { describe, expect, vi } from "vitest"; -import { GitCoreLive, makeGitCore } from "./GitCore.ts"; -import { GitCore, type GitCoreShape } from "../Services/GitCore.ts"; +import { makeGitVcsDriverCore } from "./GitVcsDriverCore.ts"; +import * as GitVcsDriver from "./GitVcsDriver.ts"; import { GitCommandError } from "@t3tools/contracts"; -import { type ProcessRunResult, runProcess } from "../../processRunner.ts"; -import { ServerConfig } from "../../config.ts"; +import { type ProcessRunResult, runProcess } from "../processRunner.ts"; +import { ServerConfig } from "../config.ts"; // ── Helpers ── const ServerConfigLayer = ServerConfig.layerTest(process.cwd(), { prefix: "t3-git-core-test-" }); -const GitCoreTestLayer = GitCoreLive.pipe( +const GitVcsDriverTestLayer = GitVcsDriver.layer.pipe( Layer.provide(ServerConfigLayer), Layer.provide(NodeServices.layer), ); -const TestLayer = Layer.mergeAll(NodeServices.layer, GitCoreTestLayer); +const TestLayer = Layer.mergeAll(NodeServices.layer, GitVcsDriverTestLayer); function makeTmpDir( prefix = "git-test-", @@ -63,11 +63,11 @@ function git( cwd: string, args: ReadonlyArray, env?: NodeJS.ProcessEnv, -): Effect.Effect { +): Effect.Effect { return Effect.gen(function* () { - const gitCore = yield* GitCore; - const result = yield* gitCore.execute({ - operation: "GitCore.test.git", + const gitVcsDriver = yield* GitVcsDriver.GitVcsDriver; + const result = yield* gitVcsDriver.execute({ + operation: "GitVcsDriver.test.git", cwd, args, ...(env ? { env } : {}), @@ -82,7 +82,7 @@ function configureRemote( remoteName: string, remotePath: string, fetchNamespace: string, -): Effect.Effect { +): Effect.Effect { return Effect.gen(function* () { yield* git(cwd, ["config", `remote.${remoteName}.url`, remotePath]); return yield* git(cwd, [ @@ -119,8 +119,8 @@ function runShellCommand(input: { }); } -const makeIsolatedGitCore = (executeOverride: GitCoreShape["execute"]) => - makeGitCore({ executeOverride }).pipe( +const makeIsolatedGitVcsDriver = (executeOverride: GitVcsDriver.GitVcsDriverShape["execute"]) => + makeGitVcsDriverCore({ executeOverride }).pipe( Effect.provide(Layer.provideMerge(ServerConfigLayer, NodeServices.layer)), ); @@ -130,10 +130,10 @@ function initRepoWithCommit( ): Effect.Effect< { initialBranch: string }, GitCommandError | PlatformError.PlatformError, - GitCore | FileSystem.FileSystem + GitVcsDriver.GitVcsDriver | FileSystem.FileSystem > { return Effect.gen(function* () { - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* core.initRepo({ cwd }); yield* git(cwd, ["config", "user.email", "test@test.com"]); yield* git(cwd, ["config", "user.name", "Test"]); @@ -154,7 +154,7 @@ function commitWithDate( ): Effect.Effect< void, GitCommandError | PlatformError.PlatformError, - GitCore | FileSystem.FileSystem + GitVcsDriver.GitVcsDriver | FileSystem.FileSystem > { return Effect.gen(function* () { yield* writeTextFile(path.join(cwd, fileName), fileContents); @@ -199,7 +199,7 @@ it.layer(TestLayer)("git integration", (it) => { it.effect("creates a valid git repo", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); - yield* (yield* GitCore).initRepo({ cwd: tmp }); + yield* (yield* GitVcsDriver.GitVcsDriver).initRepo({ cwd: tmp }); expect(existsSync(path.join(tmp, ".git"))).toBe(true); }), ); @@ -208,7 +208,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.isRepo).toBe(true); expect(result.hasOriginRemote).toBe(false); expect(result.branches.length).toBeGreaterThanOrEqual(1); @@ -222,7 +222,7 @@ it.layer(TestLayer)("git integration", (it) => { it.effect("returns isRepo: false for non-git directory", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.isRepo).toBe(false); expect(result.hasOriginRemote).toBe(false); expect(result.branches).toEqual([]); @@ -236,7 +236,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* makeDirectory(deletedDir); yield* removePath(deletedDir); - const result = yield* (yield* GitCore).listBranches({ cwd: deletedDir }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: deletedDir }); expect(result.isRepo).toBe(false); expect(result.hasOriginRemote).toBe(false); @@ -248,7 +248,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const current = result.branches.find((b) => b.current); expect(current).toBeDefined(); expect(current!.current).toBe(true); @@ -261,7 +261,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); yield* git(tmp, ["checkout", "--detach", "HEAD"]); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches.some((branch) => branch.name.startsWith("("))).toBe(false); expect(result.branches.some((branch) => branch.current)).toBe(false); }), @@ -271,12 +271,18 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "older-branch" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "older-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "older-branch", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "older-branch", + }); yield* commitWithDate( tmp, "older.txt", @@ -285,9 +291,18 @@ it.layer(TestLayer)("git integration", (it) => { "older branch change", ); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: initialBranch }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "newer-branch" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "newer-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: initialBranch, + }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "newer-branch", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "newer-branch", + }); yield* commitWithDate( tmp, "newer.txt", @@ -297,9 +312,12 @@ it.layer(TestLayer)("git integration", (it) => { ); // Switch away to show current branch is pinned, then remaining branches are recency-sorted. - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "older-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "older-branch", + }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("older-branch"); expect(result.branches[1]!.name).toBe("newer-branch"); }), @@ -310,17 +328,23 @@ it.layer(TestLayer)("git integration", (it) => { const tmp = yield* makeTmpDir(); const remote = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; yield* git(remote, ["init", "--bare"]); yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); yield* git(tmp, ["remote", "set-head", "origin", defaultBranch]); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "current-branch" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "current-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "current-branch", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "current-branch", + }); yield* commitWithDate( tmp, "current.txt", @@ -329,9 +353,18 @@ it.layer(TestLayer)("git integration", (it) => { "current change", ); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "newer-branch" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "newer-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: defaultBranch, + }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "newer-branch", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "newer-branch", + }); yield* commitWithDate( tmp, "newer.txt", @@ -340,9 +373,12 @@ it.layer(TestLayer)("git integration", (it) => { "newer change", ); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "current-branch" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "current-branch", + }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches[0]!.name).toBe("current-branch"); expect(result.branches[1]!.name).toBe(defaultBranch); expect(result.branches[2]!.name).toBe("newer-branch"); @@ -353,10 +389,10 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-a" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-b" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature-a" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature-b" }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const names = result.branches.map((b) => b.name); expect(names).toContain("feature-a"); expect(names).toContain("feature-b"); @@ -367,11 +403,14 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-a" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-b" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-c" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature-a" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature-b" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature-c" }); - const firstPage = yield* (yield* GitCore).listBranches({ cwd: tmp, limit: 2 }); + const firstPage = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + limit: 2, + }); expect(firstPage.totalCount).toBe(4); expect(firstPage.nextCursor).toBe(2); expect(firstPage.branches.map((branch) => branch.name)).toEqual([ @@ -379,7 +418,7 @@ it.layer(TestLayer)("git integration", (it) => { "feature-a", ]); - const secondPage = yield* (yield* GitCore).listBranches({ + const secondPage = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp, cursor: firstPage.nextCursor ?? 0, limit: 2, @@ -403,7 +442,7 @@ it.layer(TestLayer)("git integration", (it) => { "copilot/rewrite-cli-in-rust", ] as const; for (const branchName of createdBranchNames) { - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: branchName }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: branchName }); } yield* git(tmp, ["config", "column.ui", "always"]); @@ -420,9 +459,9 @@ it.layer(TestLayer)("git integration", (it) => { ), ).toBe(true); - const realGitCore = yield* GitCore; - const core = yield* makeIsolatedGitCore((input) => - realGitCore.execute( + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; + const core = yield* makeIsolatedGitVcsDriver((input) => + realGitVcsDriver.execute( input.args[0] === "branch" ? { ...input, @@ -455,7 +494,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches.every((b) => b.isDefault === false)).toBe(true); }), ); @@ -467,23 +506,29 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", defaultBranch]); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/local-only" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/local-only", + }); const remoteOnlyBranch = "feature/remote-only"; - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: defaultBranch, + }); yield* git(tmp, ["checkout", "-b", remoteOnlyBranch]); yield* git(tmp, ["push", "-u", "origin", remoteOnlyBranch]); yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const firstRemoteIndex = result.branches.findIndex((branch) => branch.isRemote); expect(result.hasOriginRemote).toBe(true); @@ -515,9 +560,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; yield* git(tmp, ["remote", "add", remoteName, remote]); yield* git(tmp, ["push", "-u", remoteName, defaultBranch]); @@ -528,7 +573,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["checkout", defaultBranch]); yield* git(tmp, ["branch", "-D", remoteOnlyBranch]); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const remoteBranch = result.branches.find( (branch) => branch.name === `${remoteName}/${remoteOnlyBranch}`, ); @@ -551,7 +596,10 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", initialBranch]); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/demo" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/demo", + }); yield* git(tmp, ["push", "-u", "origin", "feature/demo"]); yield* git(tmp, ["checkout", "-b", "feature/remote-only"]); @@ -559,7 +607,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["checkout", initialBranch]); yield* git(tmp, ["branch", "-D", "feature/remote-only"]); - const result = yield* (yield* GitCore).listBranches({ + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp, query: "feature/", limit: 10, @@ -582,11 +630,11 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "feature" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "feature" }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const current = result.branches.find((b) => b.current); expect(current!.name).toBe("feature"); }), @@ -603,20 +651,29 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); yield* git(source, ["push", "-u", "origin", defaultBranch]); const featureBranch = "feature-behind"; - yield* (yield* GitCore).createBranch({ cwd: source, branch: featureBranch }); - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: featureBranch }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: source, + branch: featureBranch, + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: source, + branch: featureBranch, + }); yield* writeTextFile(path.join(source, "feature.txt"), "feature base\n"); yield* git(source, ["add", "feature.txt"]); yield* git(source, ["commit", "-m", "feature base"]); yield* git(source, ["push", "-u", "origin", featureBranch]); - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: defaultBranch }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: source, + branch: defaultBranch, + }); yield* git(clone, ["clone", remote, "."]); yield* git(clone, ["config", "user.email", "test@test.com"]); @@ -627,8 +684,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(clone, ["commit", "-m", "remote feature update"]); yield* git(clone, ["push", "origin", featureBranch]); - yield* (yield* GitCore).checkoutBranch({ cwd: source, branch: featureBranch }); - const core = yield* GitCore; + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: source, + branch: featureBranch, + }); + const core = yield* GitVcsDriver.GitVcsDriver; yield* Effect.promise(() => vi.waitFor( async () => { @@ -653,9 +713,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); yield* git(source, ["push", "-u", "origin", defaultBranch]); @@ -668,9 +728,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(source, ["push", "-u", "origin", featureBranch]); yield* git(source, ["checkout", defaultBranch]); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let refreshFetchAttempts = 0; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (input.args[0] === "--git-dir" && input.args[2] === "fetch") { refreshFetchAttempts += 1; return Effect.fail( @@ -682,7 +742,7 @@ it.layer(TestLayer)("git integration", (it) => { }), ); } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); yield* core.checkoutBranch({ cwd: source, branch: featureBranch }); const status = yield* core.statusDetails(source); @@ -700,9 +760,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); yield* git(source, ["push", "-u", "origin", defaultBranch]); @@ -714,9 +774,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(source, ["push", "-u", "origin", featureBranch]); yield* git(source, ["checkout", defaultBranch]); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let refreshFetchAttempts = 0; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (input.args[0] === "--git-dir" && input.args[2] === "fetch") { refreshFetchAttempts += 1; return Effect.succeed({ @@ -727,7 +787,7 @@ it.layer(TestLayer)("git integration", (it) => { stderrTruncated: false, }); } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); yield* core.checkoutBranch({ cwd: source, branch: featureBranch }); yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 50))); @@ -750,7 +810,7 @@ it.layer(TestLayer)("git integration", (it) => { }); let fetchCount = 0; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if ( input.args[0] === "rev-parse" && input.args[1] === "--abbrev-ref" && @@ -780,7 +840,7 @@ it.layer(TestLayer)("git integration", (it) => { ]); return ok(); } - if (input.operation === "GitCore.statusDetails.status") { + if (input.operation === "GitVcsDriver.statusDetails.status") { return ok( input.cwd === "/repo/worktrees/pr-123" ? "# branch.head feature/pr-123\n# branch.upstream origin/feature/pr-123\n# branch.ab +0 -0\n" @@ -788,12 +848,12 @@ it.layer(TestLayer)("git integration", (it) => { ); } if ( - input.operation === "GitCore.statusDetails.unstagedNumstat" || - input.operation === "GitCore.statusDetails.stagedNumstat" + input.operation === "GitVcsDriver.statusDetails.unstagedNumstat" || + input.operation === "GitVcsDriver.statusDetails.stagedNumstat" ) { return ok(); } - if (input.operation === "GitCore.statusDetails.defaultRef") { + if (input.operation === "GitVcsDriver.statusDetails.defaultRef") { return ok("refs/remotes/origin/main\n"); } return Effect.fail( @@ -826,7 +886,7 @@ it.layer(TestLayer)("git integration", (it) => { }); let fetchCount = 0; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if ( input.args[0] === "rev-parse" && input.args[1] === "--abbrev-ref" && @@ -856,7 +916,7 @@ it.layer(TestLayer)("git integration", (it) => { }), ); } - if (input.operation === "GitCore.statusDetails.status") { + if (input.operation === "GitVcsDriver.statusDetails.status") { return ok( input.cwd === "/repo/worktrees/pr-123" ? "# branch.head feature/pr-123\n# branch.upstream origin/feature/pr-123\n# branch.ab +0 -0\n" @@ -864,12 +924,12 @@ it.layer(TestLayer)("git integration", (it) => { ); } if ( - input.operation === "GitCore.statusDetails.unstagedNumstat" || - input.operation === "GitCore.statusDetails.stagedNumstat" + input.operation === "GitVcsDriver.statusDetails.unstagedNumstat" || + input.operation === "GitVcsDriver.statusDetails.stagedNumstat" ) { return ok(); } - if (input.operation === "GitCore.statusDetails.defaultRef") { + if (input.operation === "GitVcsDriver.statusDetails.defaultRef") { return ok("refs/remotes/origin/main\n"); } return Effect.fail( @@ -893,7 +953,7 @@ it.layer(TestLayer)("git integration", (it) => { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); const result = yield* Effect.result( - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "nonexistent" }), + (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "nonexistent" }), ); expect(result._tag).toBe("Failure"); }), @@ -906,16 +966,19 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); yield* git(source, ["push", "-u", "origin", defaultBranch]); - yield* (yield* GitCore).createBranch({ cwd: source, branch: "feature" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: source, branch: "feature" }); const checkoutResult = yield* Effect.result( - (yield* GitCore).checkoutBranch({ cwd: source, branch: "origin/feature" }), + (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: source, + branch: "origin/feature", + }), ); expect(checkoutResult._tag).toBe("Failure"); expect(yield* git(source, ["branch", "--show-current"])).toBe(defaultBranch); @@ -935,9 +998,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(prefixRemote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* configureRemote(source, prefixRemoteName, prefixRemote, prefixFetchNamespace); yield* configureRemote(source, remoteName, remote, remoteName); yield* git(source, ["push", "-u", remoteName, defaultBranch]); @@ -950,16 +1013,16 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(source, ["checkout", defaultBranch]); yield* git(source, ["branch", "-D", featureBranch]); - const checkoutResult = yield* (yield* GitCore).checkoutBranch({ + const checkoutResult = yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: source, branch: `${remoteName}/${featureBranch}`, }); expect(checkoutResult.branch).toBe("upstream/feature"); expect(yield* git(source, ["branch", "--show-current"])).toBe("upstream/feature"); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let fetchArgs: readonly string[] | null = null; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (input.args[0] === "--git-dir" && input.args[2] === "fetch") { fetchArgs = [...input.args]; return Effect.succeed({ @@ -970,7 +1033,7 @@ it.layer(TestLayer)("git integration", (it) => { stderrTruncated: false, }); } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); const status = yield* core.statusDetails(source); @@ -996,7 +1059,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const defaultBranch = (yield* (yield* GitCore).listBranches({ + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: source, })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); @@ -1006,12 +1069,12 @@ it.layer(TestLayer)("git integration", (it) => { // would attempt to create an already-existing local branch. yield* git(source, ["branch", "--unset-upstream"]); - yield* (yield* GitCore).checkoutBranch({ + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: source, branch: `origin/${defaultBranch}`, }); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const status = yield* core.statusDetails(source); expect(status.branch).toBeNull(); }), @@ -1021,7 +1084,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "other" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "other" }); // Create a conflicting change: modify README on current branch yield* writeTextFile(path.join(tmp, "README.md"), "modified\n"); @@ -1029,23 +1092,26 @@ it.layer(TestLayer)("git integration", (it) => { // First, checkout other branch cleanly yield* git(tmp, ["stash"]); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "other" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "other" }); yield* writeTextFile(path.join(tmp, "README.md"), "other content\n"); yield* git(tmp, ["add", "."]); yield* git(tmp, ["commit", "-m", "other change"]); // Go back to default branch - const defaultBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => !b.current, - )!.name; - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: defaultBranch }); + const defaultBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => !b.current)!.name; + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: defaultBranch, + }); // Make uncommitted changes to the same file yield* writeTextFile(path.join(tmp, "README.md"), "conflicting local\n"); // Checkout should fail due to uncommitted changes const result = yield* Effect.result( - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "other" }), + (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "other" }), ); expect(result._tag).toBe("Failure"); }), @@ -1059,9 +1125,9 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "new-feature" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "new-feature" }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches.some((b) => b.name === "new-feature")).toBe(true); }), ); @@ -1070,9 +1136,9 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "dupe" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "dupe" }); const result = yield* Effect.result( - (yield* GitCore).createBranch({ cwd: tmp, branch: "dupe" }), + (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "dupe" }), ); expect(result._tag).toBe("Failure"); }), @@ -1086,10 +1152,16 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/old-name" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/old-name" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/old-name", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "feature/old-name", + }); - const renamed = yield* (yield* GitCore).renameBranch({ + const renamed = yield* (yield* GitVcsDriver.GitVcsDriver).renameBranch({ cwd: tmp, oldBranch: "feature/old-name", newBranch: "feature/new-name", @@ -1097,7 +1169,7 @@ it.layer(TestLayer)("git integration", (it) => { expect(renamed.branch).toBe("feature/new-name"); - const branches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const branches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(branches.branches.some((branch) => branch.name === "feature/old-name")).toBe(false); const current = branches.branches.find((branch) => branch.current); expect(current?.name).toBe("feature/new-name"); @@ -1108,11 +1180,11 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const current = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!; + const current = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!; - const renamed = yield* (yield* GitCore).renameBranch({ + const renamed = yield* (yield* GitVcsDriver.GitVcsDriver).renameBranch({ cwd: tmp, oldBranch: current.name, newBranch: current.name, @@ -1126,18 +1198,27 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/tmp-working" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "t3code/tmp-working" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "t3code/feat/session", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "t3code/tmp-working", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "t3code/tmp-working", + }); - const renamed = yield* (yield* GitCore).renameBranch({ + const renamed = yield* (yield* GitVcsDriver.GitVcsDriver).renameBranch({ cwd: tmp, oldBranch: "t3code/tmp-working", newBranch: "t3code/feat/session", }); expect(renamed.branch).toBe("t3code/feat/session-1"); - const branches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const branches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(branches.branches.some((branch) => branch.name === "t3code/feat/session")).toBe( true, ); @@ -1153,12 +1234,24 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/feat/session-1" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "t3code/tmp-working" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "t3code/tmp-working" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "t3code/feat/session", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "t3code/feat/session-1", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "t3code/tmp-working", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "t3code/tmp-working", + }); - const renamed = yield* (yield* GitCore).renameBranch({ + const renamed = yield* (yield* GitVcsDriver.GitVcsDriver).renameBranch({ cwd: tmp, oldBranch: "t3code/tmp-working", newBranch: "t3code/feat/session", @@ -1172,16 +1265,22 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/old-name" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/old-name" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/old-name", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "feature/old-name", + }); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let renameArgs: ReadonlyArray | null = null; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (input.args[0] === "branch" && input.args[1] === "-m") { renameArgs = [...input.args]; } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); const renamed = yield* core.renameBranch({ @@ -1205,11 +1304,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); const wtPath = path.join(tmp, "worktree-out"); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; - const result = yield* (yield* GitCore).createWorktree({ + const result = yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "wt-branch", @@ -1222,7 +1321,7 @@ it.layer(TestLayer)("git integration", (it) => { expect(existsSync(path.join(wtPath, "README.md"))).toBe(true); // Clean up worktree before tmp dir disposal - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); }), ); @@ -1232,11 +1331,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); const wtPath = path.join(tmp, "wt-check-dir"); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; - yield* (yield* GitCore).createWorktree({ + yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "wt-check", @@ -1247,7 +1346,7 @@ it.layer(TestLayer)("git integration", (it) => { const branchOutput = yield* git(wtPath, ["branch", "--show-current"]); expect(branchOutput).toBe("wt-check"); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); }), ); @@ -1255,10 +1354,13 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/existing-worktree" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/existing-worktree", + }); const wtPath = path.join(tmp, "wt-existing"); - const result = yield* (yield* GitCore).createWorktree({ + const result = yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: "feature/existing-worktree", path: wtPath, @@ -1269,7 +1371,7 @@ it.layer(TestLayer)("git integration", (it) => { const branchOutput = yield* git(wtPath, ["branch", "--show-current"]); expect(branchOutput).toBe("feature/existing-worktree"); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); }), ); @@ -1277,15 +1379,15 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "existing" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "existing" }); const wtPath = path.join(tmp, "wt-conflict"); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; const result = yield* Effect.result( - (yield* GitCore).createWorktree({ + (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "existing", @@ -1302,11 +1404,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); const wtPath = path.join(tmp, "wt-list-dir"); - const mainBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const mainBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; - yield* (yield* GitCore).createWorktree({ + yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: mainBranch, newBranch: "wt-list", @@ -1314,17 +1416,17 @@ it.layer(TestLayer)("git integration", (it) => { }); // listGitBranches from the worktree should show wt-list as current - const wtBranches = yield* (yield* GitCore).listBranches({ cwd: wtPath }); + const wtBranches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: wtPath }); expect(wtBranches.isRepo).toBe(true); const wtCurrent = wtBranches.branches.find((b) => b.current); expect(wtCurrent!.name).toBe("wt-list"); // Main repo should still show the original branch as current - const mainBranches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const mainBranches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const mainCurrent = mainBranches.branches.find((b) => b.current); expect(mainCurrent!.name).toBe(mainBranch); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); }), ); @@ -1334,11 +1436,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); const wtPath = path.join(tmp, "wt-remove-dir"); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; - yield* (yield* GitCore).createWorktree({ + yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "wt-remove", @@ -1346,7 +1448,7 @@ it.layer(TestLayer)("git integration", (it) => { }); expect(existsSync(wtPath)).toBe(true); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); expect(existsSync(wtPath)).toBe(false); }), ); @@ -1357,11 +1459,11 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); const wtPath = path.join(tmp, "wt-dirty-dir"); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; - yield* (yield* GitCore).createWorktree({ + yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "wt-dirty", @@ -1372,12 +1474,16 @@ it.layer(TestLayer)("git integration", (it) => { yield* writeTextFile(path.join(wtPath, "README.md"), "dirty change\n"); const failedRemove = yield* Effect.result( - (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }), + (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }), ); expect(failedRemove._tag).toBe("Failure"); expect(existsSync(wtPath)).toBe(true); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath, force: true }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ + cwd: tmp, + path: wtPath, + force: true, + }); expect(existsSync(wtPath)).toBe(false); }), ); @@ -1390,10 +1496,16 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature-login" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature-login" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature-login", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "feature-login", + }); - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const current = result.branches.find((b) => b.current); expect(current!.name).toBe("feature-login"); }), @@ -1408,12 +1520,12 @@ it.layer(TestLayer)("git integration", (it) => { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const currentBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (b) => b.current, - )!.name; + const currentBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((b) => b.current)!.name; const wtPath = path.join(tmp, "my-worktree"); - const result = yield* (yield* GitCore).createWorktree({ + const result = yield* (yield* GitVcsDriver.GitVcsDriver).createWorktree({ cwd: tmp, branch: currentBranch, newBranch: "feature-wt", @@ -1424,7 +1536,7 @@ it.layer(TestLayer)("git integration", (it) => { expect(existsSync(result.worktree.path)).toBe(true); // Main repo still on original branch - const mainBranches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const mainBranches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const mainCurrent = mainBranches.branches.find((b) => b.current); expect(mainCurrent!.name).toBe(currentBranch); @@ -1432,7 +1544,7 @@ it.layer(TestLayer)("git integration", (it) => { const wtBranch = yield* git(wtPath, ["branch", "--show-current"]); expect(wtBranch).toBe("feature-wt"); - yield* (yield* GitCore).removeWorktree({ cwd: tmp, path: wtPath }); + yield* (yield* GitVcsDriver.GitVcsDriver).removeWorktree({ cwd: tmp, path: wtPath }); }), ); }); @@ -1454,7 +1566,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["push", "origin", "HEAD:refs/pull/55/head"]); yield* git(tmp, ["checkout", initialBranch]); - yield* (yield* GitCore).fetchPullRequestBranch({ + yield* (yield* GitVcsDriver.GitVcsDriver).fetchPullRequestBranch({ cwd: tmp, prNumber: 55, branch: "feature/pr-fetch", @@ -1475,22 +1587,22 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "branch-a" }); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "branch-b" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "branch-a" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "branch-b" }); // Simulate switching to thread A's branch - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-a" }); - let branches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "branch-a" }); + let branches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a"); // Simulate switching to thread B's branch - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-b" }); - branches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "branch-b" }); + branches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(branches.branches.find((b) => b.current)!.name).toBe("branch-b"); // Switch back to thread A - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "branch-a" }); - branches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "branch-a" }); + branches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(branches.branches.find((b) => b.current)!.name).toBe("branch-a"); }), ); @@ -1503,40 +1615,43 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "diverged" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ cwd: tmp, branch: "diverged" }); // Make diverged branch have different file content - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "diverged" }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "diverged" }); yield* writeTextFile(path.join(tmp, "README.md"), "diverged content\n"); yield* git(tmp, ["add", "."]); yield* git(tmp, ["commit", "-m", "diverge"]); // Actually, let's just get back to the initial branch explicitly - const allBranches = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const allBranches = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); const initialBranch = allBranches.branches.find((b) => b.name !== "diverged")!.name; - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: initialBranch }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: initialBranch, + }); // Make local uncommitted changes to the same file yield* writeTextFile(path.join(tmp, "README.md"), "local uncommitted\n"); // Attempt checkout should fail const failedCheckout = yield* Effect.result( - (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "diverged" }), + (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ cwd: tmp, branch: "diverged" }), ); expect(failedCheckout._tag).toBe("Failure"); // Current branch should still be the initial one - const result = yield* (yield* GitCore).listBranches({ cwd: tmp }); + const result = yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: tmp }); expect(result.branches.find((b) => b.current)!.name).toBe(initialBranch); }), ); }); - describe("GitCore", () => { + describe("GitVcsDriver", () => { it.effect("supports branch lifecycle operations through the service API", () => Effect.gen(function* () { const tmp = yield* makeTmpDir(); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* core.initRepo({ cwd: tmp }); yield* git(tmp, ["config", "user.email", "test@test.com"]); @@ -1563,7 +1678,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* git(tmp, ["remote", "add", "origin", "git@github.com:pingdotgg/t3code.git"]); @@ -1582,7 +1697,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const clean = yield* core.status({ cwd: tmp }); expect(clean.hasWorkingTreeChanges).toBe(false); @@ -1600,7 +1715,7 @@ it.layer(TestLayer)("git integration", (it) => { const deletedDir = path.join(tmp, "deleted-repo"); yield* makeDirectory(deletedDir); yield* removePath(deletedDir); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const status = yield* core.statusDetails(deletedDir); const localStatus = yield* core.statusDetailsLocal(deletedDir); @@ -1629,7 +1744,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* core.createBranch({ cwd: tmp, branch: "feature/no-upstream-ahead" }); yield* core.checkoutBranch({ cwd: tmp, branch: "feature/no-upstream-ahead" }); @@ -1654,7 +1769,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const initialBranch = (yield* (yield* GitCore).listBranches({ + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: source, })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); @@ -1668,7 +1783,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(source, ["commit", "-m", "feature commit"]); yield* git(source, ["branch", "-D", initialBranch]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const details = yield* core.statusDetails(source); expect(details.branch).toBe("feature/remote-base-only"); expect(details.hasUpstream).toBe(false); @@ -1687,7 +1802,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const initialBranch = (yield* (yield* GitCore).listBranches({ + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: source, })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", remoteName, remote]); @@ -1706,7 +1821,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(source, ["commit", "-m", "feature commit"]); yield* git(source, ["branch", "-D", initialBranch]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const details = yield* core.statusDetails(source); expect(details.branch).toBe("feature/non-origin-merge-base"); expect(details.hasUpstream).toBe(false); @@ -1719,7 +1834,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* core.createBranch({ cwd: tmp, branch: "feature/no-upstream-no-ahead" }); yield* core.checkoutBranch({ cwd: tmp, branch: "feature/no-upstream-no-ahead" }); @@ -1745,7 +1860,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["checkout", "-b", "feature/no-base"]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); expect(pushed.setUpstream).toBe(true); @@ -1770,7 +1885,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["remote", "add", "fork", remote]); yield* git(tmp, ["checkout", "-b", "feature/fork-only"]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); expect(pushed.setUpstream).toBe(true); @@ -1790,9 +1905,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(tmp); - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; yield* git(tmp, ["remote", "add", "origin", remote]); yield* git(tmp, ["push", "-u", "origin", initialBranch]); @@ -1803,7 +1918,7 @@ it.layer(TestLayer)("git integration", (it) => { const featureBranch = "feature/publish-no-upstream"; yield* git(tmp, ["checkout", "-b", featureBranch]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); expect(pushed.setUpstream).toBe(true); @@ -1826,9 +1941,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(fork, ["init", "--bare"]); yield* initRepoWithCommit(tmp); - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: tmp })).branches.find( - (branch) => branch.current, - )!.name; + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: tmp, + })).branches.find((branch) => branch.current)!.name; yield* git(tmp, ["remote", "add", "origin", origin]); yield* git(tmp, ["remote", "add", "fork", fork]); yield* git(tmp, ["push", "-u", "origin", initialBranch]); @@ -1840,7 +1955,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["add", "feature.txt"]); yield* git(tmp, ["commit", "-m", "feature commit"]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); expect(pushed.setUpstream).toBe(true); @@ -1883,7 +1998,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["add", "fork.txt"]); yield* git(tmp, ["commit", "-m", "update reviewed PR branch"]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); @@ -1928,7 +2043,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(tmp, ["add", "feature.txt"]); yield* git(tmp, ["commit", "-m", "feature update"]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pushed = yield* core.pushCurrentBranch(tmp, null); expect(pushed.status).toBe("pushed"); expect(pushed.setUpstream).toBe(false); @@ -1946,7 +2061,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const missingWorktreePath = path.join(tmp, "missing-worktree"); const removeResult = yield* Effect.result( @@ -1973,7 +2088,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const initialBranch = (yield* (yield* GitCore).listBranches({ + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ cwd: source, })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); @@ -1994,7 +2109,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(clone, ["commit", "-m", "remote update"]); yield* git(clone, ["push", "origin", initialBranch]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const details = yield* core.statusDetails(source); expect(details.branch).toBe(initialBranch); expect(details.aheadCount).toBe(0); @@ -2006,7 +2121,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* writeTextFile(path.join(tmp, "README.md"), "new content\n"); const context = yield* core.prepareCommitContext(tmp); @@ -2024,7 +2139,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* writeTextFile(path.join(tmp, "a.txt"), "file a\n"); yield* writeTextFile(path.join(tmp, "b.txt"), "file b\n"); @@ -2047,7 +2162,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* writeTextFile(path.join(tmp, "a.txt"), "file a\n"); yield* writeTextFile(path.join(tmp, "b.txt"), "file b\n"); @@ -2063,7 +2178,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* writeTextFile(path.join(tmp, "README.md"), buildLargeText()); @@ -2078,7 +2193,7 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); const { initialBranch } = yield* initRepoWithCommit(tmp); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; yield* core.createBranch({ cwd: tmp, branch: "feature/large-range-context" }); yield* core.checkoutBranch({ cwd: tmp, branch: "feature/large-range-context" }); @@ -2100,11 +2215,17 @@ it.layer(TestLayer)("git integration", (it) => { yield* initRepoWithCommit(tmp); yield* git(remote, ["init", "--bare"]); yield* git(tmp, ["remote", "add", "origin", remote]); - yield* (yield* GitCore).createBranch({ cwd: tmp, branch: "feature/core-push" }); - yield* (yield* GitCore).checkoutBranch({ cwd: tmp, branch: "feature/core-push" }); + yield* (yield* GitVcsDriver.GitVcsDriver).createBranch({ + cwd: tmp, + branch: "feature/core-push", + }); + yield* (yield* GitVcsDriver.GitVcsDriver).checkoutBranch({ + cwd: tmp, + branch: "feature/core-push", + }); yield* writeTextFile(path.join(tmp, "feature.txt"), "push me\n"); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const context = yield* core.prepareCommitContext(tmp); expect(context).not.toBeNull(); yield* core.commit(tmp, "Add feature file", ""); @@ -2129,9 +2250,9 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* initRepoWithCommit(source); - const initialBranch = (yield* (yield* GitCore).listBranches({ cwd: source })).branches.find( - (branch) => branch.current, - )!.name; + const initialBranch = (yield* (yield* GitVcsDriver.GitVcsDriver).listBranches({ + cwd: source, + })).branches.find((branch) => branch.current)!.name; yield* git(source, ["remote", "add", "origin", remote]); yield* git(source, ["push", "-u", "origin", initialBranch]); @@ -2143,7 +2264,7 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(clone, ["commit", "-m", "remote update"]); yield* git(clone, ["push", "origin", initialBranch]); - const core = yield* GitCore; + const core = yield* GitVcsDriver.GitVcsDriver; const pulled = yield* core.pullCurrentBranch(source); expect(pulled.status).toBe("pulled"); expect((yield* core.statusDetails(source)).behindCount).toBe(0); @@ -2157,7 +2278,9 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const result = yield* Effect.result((yield* GitCore).pullCurrentBranch(tmp)); + const result = yield* Effect.result( + (yield* GitVcsDriver.GitVcsDriver).pullCurrentBranch(tmp), + ); expect(result._tag).toBe("Failure"); if (result._tag === "Failure") { expect(result.failure.message.toLowerCase()).toContain("no upstream"); @@ -2169,9 +2292,9 @@ it.layer(TestLayer)("git integration", (it) => { Effect.gen(function* () { const tmp = yield* makeTmpDir(); yield* initRepoWithCommit(tmp); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let didFailRecency = false; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (!didFailRecency && input.args[0] === "for-each-ref") { didFailRecency = true; return Effect.fail( @@ -2183,7 +2306,7 @@ it.layer(TestLayer)("git integration", (it) => { }), ); } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); const result = yield* core.listBranches({ cwd: tmp }); @@ -2203,10 +2326,10 @@ it.layer(TestLayer)("git integration", (it) => { yield* git(remote, ["init", "--bare"]); yield* git(tmp, ["remote", "add", "origin", remote]); - const realGitCore = yield* GitCore; + const realGitVcsDriver = yield* GitVcsDriver.GitVcsDriver; let didFailRemoteBranches = false; let didFailRemoteNames = false; - const core = yield* makeIsolatedGitCore((input) => { + const core = yield* makeIsolatedGitVcsDriver((input) => { if (input.args.join(" ") === "branch --no-color --no-column --remotes") { didFailRemoteBranches = true; return Effect.fail( @@ -2229,7 +2352,7 @@ it.layer(TestLayer)("git integration", (it) => { }), ); } - return realGitCore.execute(input); + return realGitVcsDriver.execute(input); }); const result = yield* core.listBranches({ cwd: tmp }); diff --git a/apps/server/src/git/Layers/GitCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts similarity index 76% rename from apps/server/src/git/Layers/GitCore.ts rename to apps/server/src/vcs/GitVcsDriverCore.ts index 2fac4b9adf..fa18d2f15e 100644 --- a/apps/server/src/git/Layers/GitCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -5,7 +5,6 @@ import { Effect, Exit, FileSystem, - Layer, Option, Path, PlatformError, @@ -20,23 +19,22 @@ import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process"; import { GitCommandError, type GitBranch } from "@t3tools/contracts"; import { dedupeRemoteBranchesWithLocalMatches } from "@t3tools/shared/git"; -import { compactTraceAttributes } from "../../observability/Attributes.ts"; -import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../../observability/Metrics.ts"; -import { - GitCore, - type ExecuteGitProgress, - type GitCommitOptions, - type GitCoreShape, - type GitStatusDetails, - type ExecuteGitInput, - type ExecuteGitResult, -} from "../Services/GitCore.ts"; +import { compactTraceAttributes } from "../observability/Attributes.ts"; +import { gitCommandDuration, gitCommandsTotal, withMetrics } from "../observability/Metrics.ts"; +import type { + ExecuteGitProgress, + GitCommitOptions, + GitVcsDriverShape, + GitStatusDetails, + ExecuteGitInput, + ExecuteGitResult, +} from "./GitVcsDriver.ts"; import { parseRemoteNames, parseRemoteNamesInGitOrder, parseRemoteRefWithRemoteNames, -} from "../remoteRefs.ts"; -import { ServerConfig } from "../../config.ts"; +} from "../git/remoteRefs.ts"; +import { ServerConfig } from "../config.ts"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; const DEFAULT_TIMEOUT_MS = 30_000; @@ -411,7 +409,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* ( const traceRecord = decodeJsonResult(Trace2Record)(trimmedLine); if (Result.isFailure(traceRecord)) { yield* Effect.logDebug( - `GitCore.trace2: failed to parse trace line for ${quoteGitCommand(input.args)} in ${input.cwd}`, + `GitVcsDriver.trace2: failed to parse trace line for ${quoteGitCommand(input.args)} in ${input.cwd}`, traceRecord.failure, ); return; @@ -604,14 +602,14 @@ const collectOutput = Effect.fn("collectOutput")(function* ( }; }); -export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { - executeOverride?: GitCoreShape["execute"]; +export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* (options?: { + executeOverride?: GitVcsDriverShape["execute"]; }) { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const { worktreesDir } = yield* ServerConfig; - let executeRaw: GitCoreShape["execute"]; + let executeRaw: GitVcsDriverShape["execute"]; if (options?.executeOverride) { executeRaw = options.executeOverride; @@ -718,7 +716,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }); } - const execute: GitCoreShape["execute"] = (input) => + const execute: GitVcsDriverShape["execute"] = (input) => executeRaw(input).pipe( withMetrics({ counter: gitCommandsTotal, @@ -812,7 +810,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const branchExists = (cwd: string, branch: string): Effect.Effect => executeGit( - "GitCore.branchExists", + "GitVcsDriver.branchExists", cwd, ["show-ref", "--verify", "--quiet", `refs/heads/${branch}`], { @@ -839,7 +837,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { } return yield* createGitCommandError( - "GitCore.renameBranch", + "GitVcsDriver.renameBranch", cwd, ["branch", "-m", "--", desiredBranch], `Could not find an available branch name for '${desiredBranch}'.`, @@ -848,7 +846,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const resolveCurrentUpstream = Effect.fn("resolveCurrentUpstream")(function* (cwd: string) { const upstreamRef = yield* runGitStdout( - "GitCore.resolveCurrentUpstream", + "GitVcsDriver.resolveCurrentUpstream", cwd, ["rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{upstream}"], true, @@ -858,7 +856,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { return null; } - const remoteNames = yield* runGitStdout("GitCore.listRemoteNames", cwd, ["remote"]).pipe( + const remoteNames = yield* runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe( Effect.map(parseRemoteNames), Effect.catch(() => Effect.succeed>([])), ); @@ -875,7 +873,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const fetchCwd = path.basename(gitCommonDir) === ".git" ? path.dirname(gitCommonDir) : gitCommonDir; return executeGit( - "GitCore.fetchRemoteForStatus", + "GitVcsDriver.fetchRemoteForStatus", fetchCwd, ["--git-dir", gitCommonDir, "fetch", "--quiet", "--no-tags", remoteName], { @@ -886,7 +884,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }; const resolveGitCommonDir = Effect.fn("resolveGitCommonDir")(function* (cwd: string) { - const gitCommonDir = yield* runGitStdout("GitCore.resolveGitCommonDir", cwd, [ + const gitCommonDir = yield* runGitStdout("GitVcsDriver.resolveGitCommonDir", cwd, [ "rev-parse", "--git-common-dir", ]).pipe(Effect.map((stdout) => stdout.trim())); @@ -929,7 +927,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { remoteName: string, ): Effect.Effect => executeGit( - "GitCore.resolveDefaultBranchName", + "GitVcsDriver.resolveDefaultBranchName", cwd, ["symbolic-ref", `refs/remotes/${remoteName}/HEAD`], { allowNonZeroExit: true }, @@ -948,7 +946,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { branch: string, ): Effect.Effect => executeGit( - "GitCore.remoteBranchExists", + "GitVcsDriver.remoteBranchExists", cwd, ["show-ref", "--verify", "--quiet", `refs/remotes/${remoteName}/${branch}`], { @@ -957,12 +955,12 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { ).pipe(Effect.map((result) => result.code === 0)); const originRemoteExists = (cwd: string): Effect.Effect => - executeGit("GitCore.originRemoteExists", cwd, ["remote", "get-url", "origin"], { + executeGit("GitVcsDriver.originRemoteExists", cwd, ["remote", "get-url", "origin"], { allowNonZeroExit: true, }).pipe(Effect.map((result) => result.code === 0)); const listRemoteNames = (cwd: string): Effect.Effect, GitCommandError> => - runGitStdout("GitCore.listRemoteNames", cwd, ["remote"]).pipe( + runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe( Effect.map(parseRemoteNamesInGitOrder), ); @@ -976,7 +974,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { return firstRemote; } return yield* createGitCommandError( - "GitCore.resolvePrimaryRemoteName", + "GitVcsDriver.resolvePrimaryRemoteName", cwd, ["remote"], "No git remote is configured for this repository.", @@ -988,7 +986,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { branch: string, ) { const branchPushRemote = yield* runGitStdout( - "GitCore.resolvePushRemoteName.branchPushRemote", + "GitVcsDriver.resolvePushRemoteName.branchPushRemote", cwd, ["config", "--get", `branch.${branch}.pushRemote`], true, @@ -998,7 +996,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { } const pushDefaultRemote = yield* runGitStdout( - "GitCore.resolvePushRemoteName.remotePushDefault", + "GitVcsDriver.resolvePushRemoteName.remotePushDefault", cwd, ["config", "--get", "remote.pushDefault"], true, @@ -1010,37 +1008,45 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { return yield* resolvePrimaryRemoteName(cwd).pipe(Effect.catch(() => Effect.succeed(null))); }); - const ensureRemote: GitCoreShape["ensureRemote"] = Effect.fn("ensureRemote")(function* (input) { - const preferredName = sanitizeRemoteName(input.preferredName); - const normalizedTargetUrl = normalizeRemoteUrl(input.url); - const remoteFetchUrls = yield* runGitStdout("GitCore.ensureRemote.listRemoteUrls", input.cwd, [ - "remote", - "-v", - ]).pipe(Effect.map((stdout) => parseRemoteFetchUrls(stdout))); + const ensureRemote: GitVcsDriverShape["ensureRemote"] = Effect.fn("ensureRemote")( + function* (input) { + const preferredName = sanitizeRemoteName(input.preferredName); + const normalizedTargetUrl = normalizeRemoteUrl(input.url); + const remoteFetchUrls = yield* runGitStdout( + "GitVcsDriver.ensureRemote.listRemoteUrls", + input.cwd, + ["remote", "-v"], + ).pipe(Effect.map((stdout) => parseRemoteFetchUrls(stdout))); - for (const [remoteName, remoteUrl] of remoteFetchUrls.entries()) { - if (normalizeRemoteUrl(remoteUrl) === normalizedTargetUrl) { - return remoteName; + for (const [remoteName, remoteUrl] of remoteFetchUrls.entries()) { + if (normalizeRemoteUrl(remoteUrl) === normalizedTargetUrl) { + return remoteName; + } } - } - let remoteName = preferredName; - let suffix = 1; - while (remoteFetchUrls.has(remoteName)) { - remoteName = `${preferredName}-${suffix}`; - suffix += 1; - } + let remoteName = preferredName; + let suffix = 1; + while (remoteFetchUrls.has(remoteName)) { + remoteName = `${preferredName}-${suffix}`; + suffix += 1; + } - yield* runGit("GitCore.ensureRemote.add", input.cwd, ["remote", "add", remoteName, input.url]); - return remoteName; - }); + yield* runGit("GitVcsDriver.ensureRemote.add", input.cwd, [ + "remote", + "add", + remoteName, + input.url, + ]); + return remoteName; + }, + ); const resolveBaseBranchForNoUpstream = Effect.fn("resolveBaseBranchForNoUpstream")(function* ( cwd: string, branch: string, ) { const configuredBaseBranch = yield* runGitStdout( - "GitCore.resolveBaseBranchForNoUpstream.config", + "GitVcsDriver.resolveBaseBranchForNoUpstream.config", cwd, ["config", "--get", `branch.${branch}.gh-merge-base`], true, @@ -1098,7 +1104,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { } const result = yield* executeGit( - "GitCore.computeAheadCountAgainstBase", + "GitVcsDriver.computeAheadCountAgainstBase", cwd, ["rev-list", "--count", `${baseBranch}..HEAD`], { allowNonZeroExit: true }, @@ -1113,7 +1119,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const readBranchRecency = Effect.fn("readBranchRecency")(function* (cwd: string) { const branchRecency = yield* executeGit( - "GitCore.readBranchRecency", + "GitVcsDriver.readBranchRecency", cwd, [ "for-each-ref", @@ -1149,7 +1155,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const readStatusDetailsLocal = Effect.fn("readStatusDetailsLocal")(function* (cwd: string) { const statusResult = yield* executeGit( - "GitCore.statusDetails.status", + "GitVcsDriver.statusDetails.status", cwd, ["status", "--porcelain=2", "--branch"], { @@ -1164,7 +1170,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { if (statusResult.code !== 0) { const stderr = statusResult.stderr.trim(); return yield* createGitCommandError( - "GitCore.statusDetails.status", + "GitVcsDriver.statusDetails.status", cwd, ["status", "--porcelain=2", "--branch"], stderr || "git status failed", @@ -1174,14 +1180,14 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const [unstagedNumstatStdout, stagedNumstatStdout, defaultRefResult, hasOriginRemote] = yield* Effect.all( [ - runGitStdout("GitCore.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]), - runGitStdout("GitCore.statusDetails.stagedNumstat", cwd, [ + runGitStdout("GitVcsDriver.statusDetails.unstagedNumstat", cwd, ["diff", "--numstat"]), + runGitStdout("GitVcsDriver.statusDetails.stagedNumstat", cwd, [ "diff", "--cached", "--numstat", ]), executeGit( - "GitCore.statusDetails.defaultRef", + "GitVcsDriver.statusDetails.defaultRef", cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"], { @@ -1284,21 +1290,23 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }; }); - const statusDetailsLocal: GitCoreShape["statusDetailsLocal"] = Effect.fn("statusDetailsLocal")( + const statusDetailsLocal: GitVcsDriverShape["statusDetailsLocal"] = Effect.fn( + "statusDetailsLocal", + )(function* (cwd) { + return yield* readStatusDetailsLocal(cwd); + }); + + const statusDetails: GitVcsDriverShape["statusDetails"] = Effect.fn("statusDetails")( function* (cwd) { + yield* refreshStatusUpstreamIfStale(cwd).pipe( + Effect.catchIf(isMissingGitCwdError, () => Effect.void), + Effect.ignoreCause({ log: true }), + ); return yield* readStatusDetailsLocal(cwd); }, ); - const statusDetails: GitCoreShape["statusDetails"] = Effect.fn("statusDetails")(function* (cwd) { - yield* refreshStatusUpstreamIfStale(cwd).pipe( - Effect.catchIf(isMissingGitCwdError, () => Effect.void), - Effect.ignoreCause({ log: true }), - ); - return yield* readStatusDetailsLocal(cwd); - }); - - const status: GitCoreShape["status"] = (input) => + const status: GitVcsDriverShape["status"] = (input) => statusDetails(input.cwd).pipe( Effect.map((details) => ({ isRepo: details.isRepo, @@ -1314,34 +1322,34 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { })), ); - const prepareCommitContext: GitCoreShape["prepareCommitContext"] = Effect.fn( + const prepareCommitContext: GitVcsDriverShape["prepareCommitContext"] = Effect.fn( "prepareCommitContext", )(function* (cwd, filePaths) { if (filePaths && filePaths.length > 0) { - yield* runGit("GitCore.prepareCommitContext.reset", cwd, ["reset"]).pipe( + yield* runGit("GitVcsDriver.prepareCommitContext.reset", cwd, ["reset"]).pipe( Effect.catch(() => Effect.void), ); - yield* runGit("GitCore.prepareCommitContext.addSelected", cwd, [ + yield* runGit("GitVcsDriver.prepareCommitContext.addSelected", cwd, [ "add", "-A", "--", ...filePaths, ]); } else { - yield* runGit("GitCore.prepareCommitContext.addAll", cwd, ["add", "-A"]); + yield* runGit("GitVcsDriver.prepareCommitContext.addAll", cwd, ["add", "-A"]); } - const stagedSummary = yield* runGitStdout("GitCore.prepareCommitContext.stagedSummary", cwd, [ - "diff", - "--cached", - "--name-status", - ]).pipe(Effect.map((stdout) => stdout.trim())); + const stagedSummary = yield* runGitStdout( + "GitVcsDriver.prepareCommitContext.stagedSummary", + cwd, + ["diff", "--cached", "--name-status"], + ).pipe(Effect.map((stdout) => stdout.trim())); if (stagedSummary.length === 0) { return null; } const stagedPatch = yield* runGitStdoutWithOptions( - "GitCore.prepareCommitContext.stagedPatch", + "GitVcsDriver.prepareCommitContext.stagedPatch", cwd, ["diff", "--cached", "--patch", "--minimal"], { @@ -1356,7 +1364,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }; }); - const commit: GitCoreShape["commit"] = Effect.fn("commit")(function* ( + const commit: GitVcsDriverShape["commit"] = Effect.fn("commit")(function* ( cwd, subject, body, @@ -1377,11 +1385,11 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { onStderrLine: (line: string) => options.progress?.onOutputLine?.({ stream: "stderr", text: line }) ?? Effect.void, }; - yield* executeGit("GitCore.commit.commit", cwd, args, { + yield* executeGit("GitVcsDriver.commit.commit", cwd, args, { ...(options?.timeoutMs !== undefined ? { timeoutMs: options.timeoutMs } : {}), ...(progress ? { progress } : {}), }).pipe(Effect.asVoid); - const commitSha = yield* runGitStdout("GitCore.commit.revParseHead", cwd, [ + const commitSha = yield* runGitStdout("GitVcsDriver.commit.revParseHead", cwd, [ "rev-parse", "HEAD", ]).pipe(Effect.map((stdout) => stdout.trim())); @@ -1389,13 +1397,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { return { commitSha }; }); - const pushCurrentBranch: GitCoreShape["pushCurrentBranch"] = Effect.fn("pushCurrentBranch")( + const pushCurrentBranch: GitVcsDriverShape["pushCurrentBranch"] = Effect.fn("pushCurrentBranch")( function* (cwd, fallbackBranch) { const details = yield* statusDetails(cwd); const branch = details.branch ?? fallbackBranch; if (!branch) { return yield* createGitCommandError( - "GitCore.pushCurrentBranch", + "GitVcsDriver.pushCurrentBranch", cwd, ["push"], "Cannot push from detached HEAD.", @@ -1442,13 +1450,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const publishRemoteName = yield* resolvePushRemoteName(cwd, branch); if (!publishRemoteName) { return yield* createGitCommandError( - "GitCore.pushCurrentBranch", + "GitVcsDriver.pushCurrentBranch", cwd, ["push"], "Cannot push because no git remote is configured for this repository.", ); } - yield* runGit("GitCore.pushCurrentBranch.pushWithUpstream", cwd, [ + yield* runGit("GitVcsDriver.pushCurrentBranch.pushWithUpstream", cwd, [ "push", "-u", publishRemoteName, @@ -1466,7 +1474,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { Effect.catch(() => Effect.succeed(null)), ); if (currentUpstream) { - yield* runGit("GitCore.pushCurrentBranch.pushUpstream", cwd, [ + yield* runGit("GitVcsDriver.pushCurrentBranch.pushUpstream", cwd, [ "push", currentUpstream.remoteName, `HEAD:${currentUpstream.upstreamBranch}`, @@ -1479,7 +1487,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }; } - yield* runGit("GitCore.pushCurrentBranch.push", cwd, ["push"]); + yield* runGit("GitVcsDriver.pushCurrentBranch.push", cwd, ["push"]); return { status: "pushed" as const, branch, @@ -1489,13 +1497,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const pullCurrentBranch: GitCoreShape["pullCurrentBranch"] = Effect.fn("pullCurrentBranch")( + const pullCurrentBranch: GitVcsDriverShape["pullCurrentBranch"] = Effect.fn("pullCurrentBranch")( function* (cwd) { const details = yield* statusDetails(cwd); const branch = details.branch; if (!branch) { return yield* createGitCommandError( - "GitCore.pullCurrentBranch", + "GitVcsDriver.pullCurrentBranch", cwd, ["pull", "--ff-only"], "Cannot pull from detached HEAD.", @@ -1503,24 +1511,24 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { } if (!details.hasUpstream) { return yield* createGitCommandError( - "GitCore.pullCurrentBranch", + "GitVcsDriver.pullCurrentBranch", cwd, ["pull", "--ff-only"], "Current branch has no upstream configured. Push with upstream first.", ); } const beforeSha = yield* runGitStdout( - "GitCore.pullCurrentBranch.beforeSha", + "GitVcsDriver.pullCurrentBranch.beforeSha", cwd, ["rev-parse", "HEAD"], true, ).pipe(Effect.map((stdout) => stdout.trim())); - yield* executeGit("GitCore.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], { + yield* executeGit("GitVcsDriver.pullCurrentBranch.pull", cwd, ["pull", "--ff-only"], { timeoutMs: 30_000, fallbackErrorMessage: "git pull failed", }); const afterSha = yield* runGitStdout( - "GitCore.pullCurrentBranch.afterSha", + "GitVcsDriver.pullCurrentBranch.afterSha", cwd, ["rev-parse", "HEAD"], true, @@ -1535,13 +1543,13 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const readRangeContext: GitCoreShape["readRangeContext"] = Effect.fn("readRangeContext")( + const readRangeContext: GitVcsDriverShape["readRangeContext"] = Effect.fn("readRangeContext")( function* (cwd, baseBranch) { const range = `${baseBranch}..HEAD`; const [commitSummary, diffSummary, diffPatch] = yield* Effect.all( [ runGitStdoutWithOptions( - "GitCore.readRangeContext.log", + "GitVcsDriver.readRangeContext.log", cwd, ["log", "--oneline", range], { @@ -1550,7 +1558,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ), runGitStdoutWithOptions( - "GitCore.readRangeContext.diffStat", + "GitVcsDriver.readRangeContext.diffStat", cwd, ["diff", "--stat", range], { @@ -1559,7 +1567,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ), runGitStdoutWithOptions( - "GitCore.readRangeContext.diffPatch", + "GitVcsDriver.readRangeContext.diffPatch", cwd, ["diff", "--patch", "--minimal", range], { @@ -1579,228 +1587,230 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const readConfigValue: GitCoreShape["readConfigValue"] = (cwd, key) => - runGitStdout("GitCore.readConfigValue", cwd, ["config", "--get", key], true).pipe( + const readConfigValue: GitVcsDriverShape["readConfigValue"] = (cwd, key) => + runGitStdout("GitVcsDriver.readConfigValue", cwd, ["config", "--get", key], true).pipe( Effect.map((stdout) => stdout.trim()), Effect.map((trimmed) => (trimmed.length > 0 ? trimmed : null)), ); - const listBranches: GitCoreShape["listBranches"] = Effect.fn("listBranches")(function* (input) { - const branchRecencyPromise = readBranchRecency(input.cwd).pipe( - Effect.catch(() => Effect.succeed(new Map())), - ); - const localBranchResult = yield* executeGit( - "GitCore.listBranches.branchNoColor", - input.cwd, - ["branch", "--no-color", "--no-column"], - { - timeoutMs: 10_000, - allowNonZeroExit: true, - }, - ).pipe( - Effect.catchIf(isMissingGitCwdError, () => - Effect.succeed({ - code: 128, - stdout: "", - stderr: "fatal: not a git repository", - stdoutTruncated: false, - stderrTruncated: false, - }), - ), - ); - - if (localBranchResult.code !== 0) { - const stderr = localBranchResult.stderr.trim(); - if (stderr.toLowerCase().includes("not a git repository")) { - return { - branches: [], - isRepo: false, - hasOriginRemote: false, - nextCursor: null, - totalCount: 0, - }; - } - return yield* createGitCommandError( - "GitCore.listBranches", + const listBranches: GitVcsDriverShape["listBranches"] = Effect.fn("listBranches")( + function* (input) { + const branchRecencyPromise = readBranchRecency(input.cwd).pipe( + Effect.catch(() => Effect.succeed(new Map())), + ); + const localBranchResult = yield* executeGit( + "GitVcsDriver.listBranches.branchNoColor", input.cwd, ["branch", "--no-color", "--no-column"], - stderr || "git branch failed", - ); - } - - const remoteBranchResultEffect = executeGit( - "GitCore.listBranches.remoteBranches", - input.cwd, - ["branch", "--no-color", "--no-column", "--remotes"], - { - timeoutMs: 10_000, - allowNonZeroExit: true, - }, - ).pipe( - Effect.catch((error) => - Effect.logWarning( - `GitCore.listBranches: remote branch lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote branch list.`, - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), - ), - ); - - const remoteNamesResultEffect = executeGit( - "GitCore.listBranches.remoteNames", - input.cwd, - ["remote"], - { - timeoutMs: 5_000, - allowNonZeroExit: true, - }, - ).pipe( - Effect.catch((error) => - Effect.logWarning( - `GitCore.listBranches: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`, - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), - ), - ); - - const [defaultRef, worktreeList, remoteBranchResult, remoteNamesResult, branchLastCommit] = - yield* Effect.all( - [ - executeGit( - "GitCore.listBranches.defaultRef", - input.cwd, - ["symbolic-ref", "refs/remotes/origin/HEAD"], - { - timeoutMs: 5_000, - allowNonZeroExit: true, - }, - ), - executeGit( - "GitCore.listBranches.worktreeList", - input.cwd, - ["worktree", "list", "--porcelain"], - { - timeoutMs: 5_000, - allowNonZeroExit: true, - }, - ), - remoteBranchResultEffect, - remoteNamesResultEffect, - branchRecencyPromise, - ], - { concurrency: "unbounded" }, + { + timeoutMs: 10_000, + allowNonZeroExit: true, + }, + ).pipe( + Effect.catchIf(isMissingGitCwdError, () => + Effect.succeed({ + code: 128, + stdout: "", + stderr: "fatal: not a git repository", + stdoutTruncated: false, + stderrTruncated: false, + }), + ), ); - const remoteNames = - remoteNamesResult.code === 0 ? parseRemoteNames(remoteNamesResult.stdout) : []; - if (remoteBranchResult.code !== 0 && remoteBranchResult.stderr.trim().length > 0) { - yield* Effect.logWarning( - `GitCore.listBranches: remote branch lookup returned code ${remoteBranchResult.code} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote branch list.`, - ); - } - if (remoteNamesResult.code !== 0 && remoteNamesResult.stderr.trim().length > 0) { - yield* Effect.logWarning( - `GitCore.listBranches: remote name lookup returned code ${remoteNamesResult.code} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`, - ); - } + if (localBranchResult.code !== 0) { + const stderr = localBranchResult.stderr.trim(); + if (stderr.toLowerCase().includes("not a git repository")) { + return { + branches: [], + isRepo: false, + hasOriginRemote: false, + nextCursor: null, + totalCount: 0, + }; + } + return yield* createGitCommandError( + "GitVcsDriver.listBranches", + input.cwd, + ["branch", "--no-color", "--no-column"], + stderr || "git branch failed", + ); + } - const defaultBranch = - defaultRef.code === 0 - ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") - : null; + const remoteBranchResultEffect = executeGit( + "GitVcsDriver.listBranches.remoteBranches", + input.cwd, + ["branch", "--no-color", "--no-column", "--remotes"], + { + timeoutMs: 10_000, + allowNonZeroExit: true, + }, + ).pipe( + Effect.catch((error) => + Effect.logWarning( + `GitVcsDriver.listBranches: remote branch lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote branch list.`, + ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), + ), + ); - const worktreeMap = new Map(); - if (worktreeList.code === 0) { - let currentPath: string | null = null; - for (const line of worktreeList.stdout.split("\n")) { - if (line.startsWith("worktree ")) { - const candidatePath = line.slice("worktree ".length); - const exists = yield* fileSystem.stat(candidatePath).pipe( - Effect.map(() => true), - Effect.catch(() => Effect.succeed(false)), - ); - currentPath = exists ? candidatePath : null; - } else if (line.startsWith("branch refs/heads/") && currentPath) { - worktreeMap.set(line.slice("branch refs/heads/".length), currentPath); - } else if (line === "") { - currentPath = null; + const remoteNamesResultEffect = executeGit( + "GitVcsDriver.listBranches.remoteNames", + input.cwd, + ["remote"], + { + timeoutMs: 5_000, + allowNonZeroExit: true, + }, + ).pipe( + Effect.catch((error) => + Effect.logWarning( + `GitVcsDriver.listBranches: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`, + ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), + ), + ); + + const [defaultRef, worktreeList, remoteBranchResult, remoteNamesResult, branchLastCommit] = + yield* Effect.all( + [ + executeGit( + "GitVcsDriver.listBranches.defaultRef", + input.cwd, + ["symbolic-ref", "refs/remotes/origin/HEAD"], + { + timeoutMs: 5_000, + allowNonZeroExit: true, + }, + ), + executeGit( + "GitVcsDriver.listBranches.worktreeList", + input.cwd, + ["worktree", "list", "--porcelain"], + { + timeoutMs: 5_000, + allowNonZeroExit: true, + }, + ), + remoteBranchResultEffect, + remoteNamesResultEffect, + branchRecencyPromise, + ], + { concurrency: "unbounded" }, + ); + + const remoteNames = + remoteNamesResult.code === 0 ? parseRemoteNames(remoteNamesResult.stdout) : []; + if (remoteBranchResult.code !== 0 && remoteBranchResult.stderr.trim().length > 0) { + yield* Effect.logWarning( + `GitVcsDriver.listBranches: remote branch lookup returned code ${remoteBranchResult.code} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote branch list.`, + ); + } + if (remoteNamesResult.code !== 0 && remoteNamesResult.stderr.trim().length > 0) { + yield* Effect.logWarning( + `GitVcsDriver.listBranches: remote name lookup returned code ${remoteNamesResult.code} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`, + ); + } + + const defaultBranch = + defaultRef.code === 0 + ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") + : null; + + const worktreeMap = new Map(); + if (worktreeList.code === 0) { + let currentPath: string | null = null; + for (const line of worktreeList.stdout.split("\n")) { + if (line.startsWith("worktree ")) { + const candidatePath = line.slice("worktree ".length); + const exists = yield* fileSystem.stat(candidatePath).pipe( + Effect.map(() => true), + Effect.catch(() => Effect.succeed(false)), + ); + currentPath = exists ? candidatePath : null; + } else if (line.startsWith("branch refs/heads/") && currentPath) { + worktreeMap.set(line.slice("branch refs/heads/".length), currentPath); + } else if (line === "") { + currentPath = null; + } } } - } - const localBranches = localBranchResult.stdout - .split("\n") - .map(parseBranchLine) - .filter((branch): branch is { name: string; current: boolean } => branch !== null) - .map((branch) => ({ - name: branch.name, - current: branch.current, - isRemote: false, - isDefault: branch.name === defaultBranch, - worktreePath: worktreeMap.get(branch.name) ?? null, - })) - .toSorted((a, b) => { - const aPriority = a.current ? 0 : a.isDefault ? 1 : 2; - const bPriority = b.current ? 0 : b.isDefault ? 1 : 2; - if (aPriority !== bPriority) return aPriority - bPriority; + const localBranches = localBranchResult.stdout + .split("\n") + .map(parseBranchLine) + .filter((branch): branch is { name: string; current: boolean } => branch !== null) + .map((branch) => ({ + name: branch.name, + current: branch.current, + isRemote: false, + isDefault: branch.name === defaultBranch, + worktreePath: worktreeMap.get(branch.name) ?? null, + })) + .toSorted((a, b) => { + const aPriority = a.current ? 0 : a.isDefault ? 1 : 2; + const bPriority = b.current ? 0 : b.isDefault ? 1 : 2; + if (aPriority !== bPriority) return aPriority - bPriority; - const aLastCommit = branchLastCommit.get(a.name) ?? 0; - const bLastCommit = branchLastCommit.get(b.name) ?? 0; - if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; - return a.name.localeCompare(b.name); + const aLastCommit = branchLastCommit.get(a.name) ?? 0; + const bLastCommit = branchLastCommit.get(b.name) ?? 0; + if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; + return a.name.localeCompare(b.name); + }); + + const remoteBranches = + remoteBranchResult.code === 0 + ? remoteBranchResult.stdout + .split("\n") + .map(parseBranchLine) + .filter((branch): branch is { name: string; current: boolean } => branch !== null) + .map((branch) => { + const parsedRemoteRef = parseRemoteRefWithRemoteNames(branch.name, remoteNames); + const remoteBranch: { + name: string; + current: boolean; + isRemote: boolean; + remoteName?: string; + isDefault: boolean; + worktreePath: string | null; + } = { + name: branch.name, + current: false, + isRemote: true, + isDefault: false, + worktreePath: null, + }; + if (parsedRemoteRef) { + remoteBranch.remoteName = parsedRemoteRef.remoteName; + } + return remoteBranch; + }) + .toSorted((a, b) => { + const aLastCommit = branchLastCommit.get(a.name) ?? 0; + const bLastCommit = branchLastCommit.get(b.name) ?? 0; + if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; + return a.name.localeCompare(b.name); + }) + : []; + + const branches = paginateBranches({ + branches: filterBranchesForListQuery( + dedupeRemoteBranchesWithLocalMatches([...localBranches, ...remoteBranches]), + input.query, + ), + cursor: input.cursor, + limit: input.limit, }); - const remoteBranches = - remoteBranchResult.code === 0 - ? remoteBranchResult.stdout - .split("\n") - .map(parseBranchLine) - .filter((branch): branch is { name: string; current: boolean } => branch !== null) - .map((branch) => { - const parsedRemoteRef = parseRemoteRefWithRemoteNames(branch.name, remoteNames); - const remoteBranch: { - name: string; - current: boolean; - isRemote: boolean; - remoteName?: string; - isDefault: boolean; - worktreePath: string | null; - } = { - name: branch.name, - current: false, - isRemote: true, - isDefault: false, - worktreePath: null, - }; - if (parsedRemoteRef) { - remoteBranch.remoteName = parsedRemoteRef.remoteName; - } - return remoteBranch; - }) - .toSorted((a, b) => { - const aLastCommit = branchLastCommit.get(a.name) ?? 0; - const bLastCommit = branchLastCommit.get(b.name) ?? 0; - if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; - return a.name.localeCompare(b.name); - }) - : []; + return { + branches: [...branches.branches], + isRepo: true, + hasOriginRemote: remoteNames.includes("origin"), + nextCursor: branches.nextCursor, + totalCount: branches.totalCount, + }; + }, + ); - const branches = paginateBranches({ - branches: filterBranchesForListQuery( - dedupeRemoteBranchesWithLocalMatches([...localBranches, ...remoteBranches]), - input.query, - ), - cursor: input.cursor, - limit: input.limit, - }); - - return { - branches: [...branches.branches], - isRepo: true, - hasOriginRemote: remoteNames.includes("origin"), - nextCursor: branches.nextCursor, - totalCount: branches.totalCount, - }; - }); - - const createWorktree: GitCoreShape["createWorktree"] = Effect.fn("createWorktree")( + const createWorktree: GitVcsDriverShape["createWorktree"] = Effect.fn("createWorktree")( function* (input) { const targetBranch = input.newBranch ?? input.branch; const sanitizedBranch = targetBranch.replace(/\//g, "-"); @@ -1810,7 +1820,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { ? ["worktree", "add", "-b", input.newBranch, worktreePath, input.branch] : ["worktree", "add", worktreePath, input.branch]; - yield* executeGit("GitCore.createWorktree", input.cwd, args, { + yield* executeGit("GitVcsDriver.createWorktree", input.cwd, args, { fallbackErrorMessage: "git worktree add failed", }); @@ -1823,12 +1833,12 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const fetchPullRequestBranch: GitCoreShape["fetchPullRequestBranch"] = Effect.fn( + const fetchPullRequestBranch: GitVcsDriverShape["fetchPullRequestBranch"] = Effect.fn( "fetchPullRequestBranch", )(function* (input) { const remoteName = yield* resolvePrimaryRemoteName(input.cwd); yield* executeGit( - "GitCore.fetchPullRequestBranch", + "GitVcsDriver.fetchPullRequestBranch", input.cwd, [ "fetch", @@ -1843,9 +1853,9 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { ); }); - const fetchRemoteBranch: GitCoreShape["fetchRemoteBranch"] = Effect.fn("fetchRemoteBranch")( + const fetchRemoteBranch: GitVcsDriverShape["fetchRemoteBranch"] = Effect.fn("fetchRemoteBranch")( function* (input) { - yield* runGit("GitCore.fetchRemoteBranch.fetch", input.cwd, [ + yield* runGit("GitVcsDriver.fetchRemoteBranch.fetch", input.cwd, [ "fetch", "--quiet", "--no-tags", @@ -1856,7 +1866,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const localBranchAlreadyExists = yield* branchExists(input.cwd, input.localBranch); const targetRef = `${input.remoteName}/${input.remoteBranch}`; yield* runGit( - "GitCore.fetchRemoteBranch.materialize", + "GitVcsDriver.fetchRemoteBranch.materialize", input.cwd, localBranchAlreadyExists ? ["branch", "--force", input.localBranch, targetRef] @@ -1865,28 +1875,28 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const setBranchUpstream: GitCoreShape["setBranchUpstream"] = (input) => - runGit("GitCore.setBranchUpstream", input.cwd, [ + const setBranchUpstream: GitVcsDriverShape["setBranchUpstream"] = (input) => + runGit("GitVcsDriver.setBranchUpstream", input.cwd, [ "branch", "--set-upstream-to", `${input.remoteName}/${input.remoteBranch}`, input.branch, ]); - const removeWorktree: GitCoreShape["removeWorktree"] = Effect.fn("removeWorktree")( + const removeWorktree: GitVcsDriverShape["removeWorktree"] = Effect.fn("removeWorktree")( function* (input) { const args = ["worktree", "remove"]; if (input.force) { args.push("--force"); } args.push(input.path); - yield* executeGit("GitCore.removeWorktree", input.cwd, args, { + yield* executeGit("GitVcsDriver.removeWorktree", input.cwd, args, { timeoutMs: 15_000, fallbackErrorMessage: "git worktree remove failed", }).pipe( Effect.mapError((error) => createGitCommandError( - "GitCore.removeWorktree", + "GitVcsDriver.removeWorktree", input.cwd, args, `${commandLabel(args)} failed (cwd: ${input.cwd}): ${error.message}`, @@ -1897,31 +1907,33 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const renameBranch: GitCoreShape["renameBranch"] = Effect.fn("renameBranch")(function* (input) { - if (input.oldBranch === input.newBranch) { - return { branch: input.newBranch }; - } - const targetBranch = yield* resolveAvailableBranchName(input.cwd, input.newBranch); + const renameBranch: GitVcsDriverShape["renameBranch"] = Effect.fn("renameBranch")( + function* (input) { + if (input.oldBranch === input.newBranch) { + return { branch: input.newBranch }; + } + const targetBranch = yield* resolveAvailableBranchName(input.cwd, input.newBranch); - yield* executeGit( - "GitCore.renameBranch", - input.cwd, - ["branch", "-m", "--", input.oldBranch, targetBranch], - { - timeoutMs: 10_000, - fallbackErrorMessage: "git branch rename failed", - }, - ); + yield* executeGit( + "GitVcsDriver.renameBranch", + input.cwd, + ["branch", "-m", "--", input.oldBranch, targetBranch], + { + timeoutMs: 10_000, + fallbackErrorMessage: "git branch rename failed", + }, + ); - return { branch: targetBranch }; - }); + return { branch: targetBranch }; + }, + ); - const checkoutBranch: GitCoreShape["checkoutBranch"] = Effect.fn("checkoutBranch")( + const checkoutBranch: GitVcsDriverShape["checkoutBranch"] = Effect.fn("checkoutBranch")( function* (input) { const [localInputExists, remoteExists] = yield* Effect.all( [ executeGit( - "GitCore.checkoutBranch.localInputExists", + "GitVcsDriver.checkoutBranch.localInputExists", input.cwd, ["show-ref", "--verify", "--quiet", `refs/heads/${input.branch}`], { @@ -1930,7 +1942,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ).pipe(Effect.map((result) => result.code === 0)), executeGit( - "GitCore.checkoutBranch.remoteExists", + "GitVcsDriver.checkoutBranch.remoteExists", input.cwd, ["show-ref", "--verify", "--quiet", `refs/remotes/${input.branch}`], { @@ -1944,7 +1956,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const localTrackingBranch = remoteExists ? yield* executeGit( - "GitCore.checkoutBranch.localTrackingBranch", + "GitVcsDriver.checkoutBranch.localTrackingBranch", input.cwd, ["for-each-ref", "--format=%(refname:short)\t%(upstream:short)", "refs/heads"], { @@ -1964,7 +1976,7 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { const localTrackedBranchTargetExists = remoteExists && localTrackedBranchCandidate ? yield* executeGit( - "GitCore.checkoutBranch.localTrackedBranchTargetExists", + "GitVcsDriver.checkoutBranch.localTrackedBranchTargetExists", input.cwd, ["show-ref", "--verify", "--quiet", `refs/heads/${localTrackedBranchCandidate}`], { @@ -1984,12 +1996,12 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { ? ["checkout", localTrackingBranch] : ["checkout", input.branch]; - yield* executeGit("GitCore.checkoutBranch.checkout", input.cwd, checkoutArgs, { + yield* executeGit("GitVcsDriver.checkoutBranch.checkout", input.cwd, checkoutArgs, { timeoutMs: 10_000, fallbackErrorMessage: "git checkout failed", }); - const branch = yield* runGitStdout("GitCore.checkoutBranch.currentBranch", input.cwd, [ + const branch = yield* runGitStdout("GitVcsDriver.checkoutBranch.currentBranch", input.cwd, [ "branch", "--show-current", ]).pipe(Effect.map((stdout) => stdout.trim() || null)); @@ -1998,26 +2010,28 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { }, ); - const createBranch: GitCoreShape["createBranch"] = Effect.fn("createBranch")(function* (input) { - yield* executeGit("GitCore.createBranch", input.cwd, ["branch", input.branch], { - timeoutMs: 10_000, - fallbackErrorMessage: "git branch create failed", - }); - if (input.checkout) { - yield* checkoutBranch({ cwd: input.cwd, branch: input.branch }); - } + const createBranch: GitVcsDriverShape["createBranch"] = Effect.fn("createBranch")( + function* (input) { + yield* executeGit("GitVcsDriver.createBranch", input.cwd, ["branch", input.branch], { + timeoutMs: 10_000, + fallbackErrorMessage: "git branch create failed", + }); + if (input.checkout) { + yield* checkoutBranch({ cwd: input.cwd, branch: input.branch }); + } - return { branch: input.branch }; - }); + return { branch: input.branch }; + }, + ); - const initRepo: GitCoreShape["initRepo"] = (input) => - executeGit("GitCore.initRepo", input.cwd, ["init"], { + const initRepo: GitVcsDriverShape["initRepo"] = (input) => + executeGit("GitVcsDriver.initRepo", input.cwd, ["init"], { timeoutMs: 10_000, fallbackErrorMessage: "git init failed", }).pipe(Effect.asVoid); - const listLocalBranchNames: GitCoreShape["listLocalBranchNames"] = (cwd) => - runGitStdout("GitCore.listLocalBranchNames", cwd, [ + const listLocalBranchNames: GitVcsDriverShape["listLocalBranchNames"] = (cwd) => + runGitStdout("GitVcsDriver.listLocalBranchNames", cwd, [ "branch", "--list", "--no-column", @@ -2054,7 +2068,5 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: { checkoutBranch, initRepo, listLocalBranchNames, - } satisfies GitCoreShape; + } satisfies GitVcsDriverShape; }); - -export const GitCoreLive = Layer.effect(GitCore, makeGitCore());