Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions apps/server/src/git/Layers/GitCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,24 @@ function writeTextFile(
});
}

function removePath(
targetPath: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.remove(targetPath, { recursive: true, force: true });
});
}

function makeDirectory(
dirPath: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.makeDirectory(dirPath, { recursive: true });
});
}

/** Run a raw git command for test setup (not under test). */
function git(
cwd: string,
Expand Down Expand Up @@ -299,6 +317,21 @@ it.layer(TestLayer)("git integration", (it) => {
}),
);

it.effect("returns isRepo: false for deleted directories", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const deletedDir = path.join(tmp, "deleted-repo");
yield* makeDirectory(deletedDir);
yield* removePath(deletedDir);

const result = yield* (yield* GitCore).listBranches({ cwd: deletedDir });

expect(result.isRepo).toBe(false);
expect(result.hasOriginRemote).toBe(false);
expect(result.branches).toEqual([]);
}),
);

it.effect("returns the current branch with current: true", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
Expand Down Expand Up @@ -1626,6 +1659,37 @@ it.layer(TestLayer)("git integration", (it) => {
}),
);

it.effect("returns a non-repo status for deleted directories", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
const deletedDir = path.join(tmp, "deleted-repo");
yield* makeDirectory(deletedDir);
yield* removePath(deletedDir);
const core = yield* GitCore;

const status = yield* core.statusDetails(deletedDir);
const localStatus = yield* core.statusDetailsLocal(deletedDir);

expect(status).toEqual({
isRepo: false,
hasOriginRemote: false,
isDefaultBranch: false,
branch: null,
upstreamRef: null,
hasWorkingTreeChanges: false,
workingTree: {
files: [],
insertions: 0,
deletions: 0,
},
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
});
expect(localStatus).toEqual(status);
}),
);

it.effect("computes ahead count against base branch when no upstream is configured", () =>
Effect.gen(function* () {
const tmp = yield* makeTmpDir();
Expand Down
44 changes: 42 additions & 2 deletions apps/server/src/git/Layers/GitCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
type ExecuteGitProgress,
type GitCommitOptions,
type GitCoreShape,
type GitStatusDetails,
type ExecuteGitInput,
type ExecuteGitResult,
} from "../Services/GitCore.ts";
Expand Down Expand Up @@ -59,6 +60,18 @@ const STATUS_UPSTREAM_REFRESH_FAILURE_COOLDOWN = Duration.seconds(5);
const STATUS_UPSTREAM_REFRESH_CACHE_CAPACITY = 2_048;
const DEFAULT_BASE_BRANCH_CANDIDATES = ["main", "master"] as const;
const GIT_LIST_BRANCHES_DEFAULT_LIMIT = 100;
const NON_REPOSITORY_STATUS_DETAILS = {
isRepo: false,
hasOriginRemote: false,
isDefaultBranch: false,
branch: null,
upstreamRef: null,
hasWorkingTreeChanges: false,
workingTree: { files: [], insertions: 0, deletions: 0 },
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
} satisfies GitStatusDetails;

type TraceTailState = {
processedChars: number;
Expand Down Expand Up @@ -359,6 +372,16 @@ function quoteGitCommand(args: ReadonlyArray<string>): string {
return `git ${args.join(" ")}`;
}

function isMissingGitCwdError(error: GitCommandError): boolean {
const normalized = `${error.detail}\n${error.message}`.toLowerCase();
return (
normalized.includes("no such file or directory") ||
normalized.includes("notfound: filesystem.access") ||
normalized.includes("enoent") ||
normalized.includes("not a directory")
);
}

function toGitCommandError(
input: Pick<ExecuteGitInput, "operation" | "cwd" | "args">,
detail: string,
Expand Down Expand Up @@ -1190,7 +1213,11 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
{
allowNonZeroExit: true,
},
);
).pipe(Effect.catchIf(isMissingGitCwdError, () => Effect.succeed(null)));

if (statusResult === null) {
return NON_REPOSITORY_STATUS_DETAILS;
Comment thread
juliusmarminge marked this conversation as resolved.
}

if (statusResult.code !== 0) {
const stderr = statusResult.stderr.trim();
Expand Down Expand Up @@ -1322,7 +1349,10 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
);

const statusDetails: GitCoreShape["statusDetails"] = Effect.fn("statusDetails")(function* (cwd) {
yield* refreshStatusUpstreamIfStale(cwd).pipe(Effect.ignoreCause({ log: true }));
yield* refreshStatusUpstreamIfStale(cwd).pipe(
Effect.catchIf(isMissingGitCwdError, () => Effect.void),
Effect.ignoreCause({ log: true }),
);
return yield* readStatusDetailsLocal(cwd);
});

Expand Down Expand Up @@ -1719,6 +1749,16 @@ export const makeGitCore = Effect.fn("makeGitCore")(function* (options?: {
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) {
Expand Down
47 changes: 47 additions & 0 deletions apps/server/src/git/Layers/GitManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ function makeTempDir(
});
}

function removePath(
targetPath: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.remove(targetPath, { recursive: true, force: true });
});
}

function makeDirectory(
dirPath: string,
): Effect.Effect<void, PlatformError.PlatformError, FileSystem.FileSystem> {
return Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
yield* fileSystem.makeDirectory(dirPath, { recursive: true });
});
}

function runGit(
cwd: string,
args: readonly string[],
Expand Down Expand Up @@ -720,6 +738,35 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
}),
);

it.effect("status returns an explicit non-repo result for deleted directories", () =>
Effect.gen(function* () {
const rootDir = yield* makeTempDir("t3code-git-manager-missing-dir-");
const cwd = path.join(rootDir, "deleted-repo");
yield* makeDirectory(cwd);
yield* removePath(cwd);
const { manager } = yield* makeManager();

const status = yield* manager.status({ cwd });

expect(status).toEqual({
isRepo: false,
hasOriginRemote: false,
isDefaultBranch: false,
branch: null,
hasWorkingTreeChanges: false,
workingTree: {
files: [],
insertions: 0,
deletions: 0,
},
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
pr: null,
});
}),
);

it.effect("status briefly caches repeated lookups for the same cwd", () =>
Effect.gen(function* () {
const repoDir = yield* makeTempDir("t3code-git-manager-");
Expand Down
Loading