Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = Object.freeze<GitStatusDetails>({
isRepo: false,
hasOriginRemote: false,
isDefaultBranch: false,
branch: null,
upstreamRef: null,
hasWorkingTreeChanges: false,
workingTree: { files: [], insertions: 0, deletions: 0 },
hasUpstream: false,
aheadCount: 0,
behindCount: 0,
});

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;
}

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
99 changes: 99 additions & 0 deletions apps/server/src/git/Layers/GitHubCli.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,105 @@ layer("GitHubCliLive", (it) => {
}),
);

it.effect("trims pull request fields decoded from gh json", () =>
Effect.gen(function* () {
mockedRunProcess.mockResolvedValueOnce({
stdout: JSON.stringify({
number: 42,
title: " Add PR thread creation \n",
url: " https://github.com/pingdotgg/codething-mvp/pull/42 ",
baseRefName: " main ",
headRefName: "\tfeature/pr-threads\t",
state: "OPEN",
mergedAt: null,
isCrossRepository: true,
headRepository: {
nameWithOwner: " octocat/codething-mvp ",
},
headRepositoryOwner: {
login: " octocat ",
},
}),
stderr: "",
code: 0,
signal: null,
timedOut: false,
});

const result = yield* Effect.gen(function* () {
const gh = yield* GitHubCli;
return yield* gh.getPullRequest({
cwd: "/repo",
reference: "#42",
});
});

assert.deepStrictEqual(result, {
number: 42,
title: "Add PR thread creation",
url: "https://github.com/pingdotgg/codething-mvp/pull/42",
baseRefName: "main",
headRefName: "feature/pr-threads",
state: "open",
isCrossRepository: true,
headRepositoryNameWithOwner: "octocat/codething-mvp",
headRepositoryOwnerLogin: "octocat",
});
}),
);

it.effect("skips invalid entries when parsing pr lists", () =>
Effect.gen(function* () {
mockedRunProcess.mockResolvedValueOnce({
stdout: JSON.stringify([
{
number: 0,
title: "invalid",
url: "https://github.com/pingdotgg/codething-mvp/pull/0",
baseRefName: "main",
headRefName: "feature/invalid",
},
{
number: 43,
title: " Valid PR ",
url: " https://github.com/pingdotgg/codething-mvp/pull/43 ",
baseRefName: " main ",
headRefName: " feature/pr-list ",
headRepository: {
nameWithOwner: " ",
},
headRepositoryOwner: {
login: " ",
},
},
]),
stderr: "",
code: 0,
signal: null,
timedOut: false,
});

const result = yield* Effect.gen(function* () {
const gh = yield* GitHubCli;
return yield* gh.listOpenPullRequests({
cwd: "/repo",
headSelector: "feature/pr-list",
});
});

assert.deepStrictEqual(result, [
{
number: 43,
title: "Valid PR",
url: "https://github.com/pingdotgg/codething-mvp/pull/43",
baseRefName: "main",
headRefName: "feature/pr-list",
state: "open",
},
]);
}),
);

it.effect("reads repository clone URLs", () =>
Effect.gen(function* () {
mockedRunProcess.mockResolvedValueOnce({
Expand Down
Loading
Loading