ignore: revert 3 commits that broke dev branch (#18260)

This commit is contained in:
Aiden Cline
2026-03-19 11:15:07 -05:00
committed by GitHub
parent 0d7e62a532
commit aeece6166b
30 changed files with 232 additions and 1200 deletions

View File

@@ -1,107 +0,0 @@
import { $ } from "bun"
import { describe, expect, test } from "bun:test"
import fs from "fs/promises"
import path from "path"
import { ManagedRuntime } from "effect"
import { GitEffect } from "../../src/git/effect"
import { tmpdir } from "../fixture/fixture"
const weird = process.platform === "win32" ? "space file.txt" : "tab\tfile.txt"
async function withGit<T>(fn: (rt: ManagedRuntime.ManagedRuntime<GitEffect.Service, never>) => Promise<T>) {
const rt = ManagedRuntime.make(GitEffect.defaultLayer)
try {
return await fn(rt)
} finally {
await rt.dispose()
}
}
describe("Git", () => {
test("branch() returns current branch name", async () => {
await using tmp = await tmpdir({ git: true })
await withGit(async (rt) => {
const branch = await rt.runPromise(GitEffect.Service.use((git) => git.branch(tmp.path)))
expect(branch).toBeDefined()
expect(typeof branch).toBe("string")
})
})
test("branch() returns undefined for non-git directories", async () => {
await using tmp = await tmpdir()
await withGit(async (rt) => {
const branch = await rt.runPromise(GitEffect.Service.use((git) => git.branch(tmp.path)))
expect(branch).toBeUndefined()
})
})
test("defaultBranch() uses init.defaultBranch when available", async () => {
await using tmp = await tmpdir({ git: true })
await $`git branch -M trunk`.cwd(tmp.path).quiet()
await $`git config init.defaultBranch trunk`.cwd(tmp.path).quiet()
await withGit(async (rt) => {
const branch = await rt.runPromise(GitEffect.Service.use((git) => git.defaultBranch(tmp.path)))
expect(branch?.name).toBe("trunk")
expect(branch?.ref).toBe("trunk")
})
})
test("status() handles special filenames", async () => {
await using tmp = await tmpdir({ git: true })
const file = weird
await fs.writeFile(path.join(tmp.path, file), "hello\n", "utf-8")
await withGit(async (rt) => {
const status = await rt.runPromise(GitEffect.Service.use((git) => git.status(tmp.path)))
expect(status).toEqual(
expect.arrayContaining([
expect.objectContaining({
file,
status: "added",
}),
]),
)
})
})
test("diff(), stats(), and mergeBase() parse tracked changes", async () => {
await using tmp = await tmpdir({ git: true })
await $`git branch -M main`.cwd(tmp.path).quiet()
const file = weird
await fs.writeFile(path.join(tmp.path, file), "before\n", "utf-8")
await $`git add .`.cwd(tmp.path).quiet()
await $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet()
await $`git checkout -b feature/test`.cwd(tmp.path).quiet()
await fs.writeFile(path.join(tmp.path, file), "after\n", "utf-8")
await withGit(async (rt) => {
const [base, diff, stats] = await Promise.all([
rt.runPromise(GitEffect.Service.use((git) => git.mergeBase(tmp.path, "main"))),
rt.runPromise(GitEffect.Service.use((git) => git.diff(tmp.path, "HEAD"))),
rt.runPromise(GitEffect.Service.use((git) => git.stats(tmp.path, "HEAD"))),
])
expect(base).toBeTruthy()
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file,
status: "modified",
}),
]),
)
expect(stats).toEqual(
expect.arrayContaining([
expect.objectContaining({
file,
additions: 1,
deletions: 1,
}),
]),
)
})
})
})

View File

@@ -10,48 +10,45 @@ import { ProjectID } from "../../src/project/schema"
Log.init({ print: false })
const gitModule = await import("../../src/git")
const originalGit = gitModule.Git.run
const gitModule = await import("../../src/util/git")
const originalGit = gitModule.git
type Mode = "none" | "rev-list-fail" | "top-fail" | "common-dir-fail"
let mode: Mode = "none"
mock.module("../../src/git", () => ({
Git: {
...gitModule.Git,
run: (args: string[], opts: { cwd: string; env?: Record<string, string> }) => {
const cmd = ["git", ...args].join(" ")
if (
mode === "rev-list-fail" &&
cmd.includes("git rev-list") &&
cmd.includes("--max-parents=0") &&
cmd.includes("HEAD")
) {
return Promise.resolve({
exitCode: 128,
text: () => "",
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
if (mode === "top-fail" && cmd.includes("git rev-parse") && cmd.includes("--show-toplevel")) {
return Promise.resolve({
exitCode: 128,
text: () => "",
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
if (mode === "common-dir-fail" && cmd.includes("git rev-parse") && cmd.includes("--git-common-dir")) {
return Promise.resolve({
exitCode: 128,
text: () => "",
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
return originalGit(args, opts)
},
mock.module("../../src/util/git", () => ({
git: (args: string[], opts: { cwd: string; env?: Record<string, string> }) => {
const cmd = ["git", ...args].join(" ")
if (
mode === "rev-list-fail" &&
cmd.includes("git rev-list") &&
cmd.includes("--max-parents=0") &&
cmd.includes("HEAD")
) {
return Promise.resolve({
exitCode: 128,
text: () => Promise.resolve(""),
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
if (mode === "top-fail" && cmd.includes("git rev-parse") && cmd.includes("--show-toplevel")) {
return Promise.resolve({
exitCode: 128,
text: () => Promise.resolve(""),
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
if (mode === "common-dir-fail" && cmd.includes("git rev-parse") && cmd.includes("--git-common-dir")) {
return Promise.resolve({
exitCode: 128,
text: () => Promise.resolve(""),
stdout: Buffer.from(""),
stderr: Buffer.from("fatal"),
})
}
return originalGit(args, opts)
},
}))

View File

@@ -23,7 +23,7 @@ function withVcs(
) {
return withServices(
directory,
Layer.merge(FileWatcher.layer, Vcs.defaultLayer),
Layer.merge(FileWatcher.layer, Vcs.layer),
async (rt) => {
await rt.runPromise(FileWatcher.Service.use(() => Effect.void))
await rt.runPromise(Vcs.Service.use(() => Effect.void))
@@ -34,15 +34,7 @@ function withVcs(
)
}
function withVcsOnly(
directory: string,
body: (rt: ManagedRuntime.ManagedRuntime<Vcs.Service, never>) => Promise<void>,
) {
return withServices(directory, Vcs.defaultLayer, body)
}
type BranchEvent = { directory?: string; payload: { type: string; properties: { branch?: string } } }
const weird = process.platform === "win32" ? "space file.txt" : "tab\tfile.txt"
/** Wait for a Vcs.Event.BranchUpdated event on GlobalBus, with retry polling as fallback */
function nextBranchUpdate(directory: string, timeout = 10_000) {
@@ -129,104 +121,3 @@ describeVcs("Vcs", () => {
})
})
})
describe("Vcs diff", () => {
afterEach(() => Instance.disposeAll())
test("defaultBranch() falls back to main", async () => {
await using tmp = await tmpdir({ git: true })
await $`git branch -M main`.cwd(tmp.path).quiet()
await withVcsOnly(tmp.path, async (rt) => {
const branch = await rt.runPromise(Vcs.Service.use((s) => s.defaultBranch()))
expect(branch).toBe("main")
})
})
test("defaultBranch() uses init.defaultBranch when available", async () => {
await using tmp = await tmpdir({ git: true })
await $`git branch -M trunk`.cwd(tmp.path).quiet()
await $`git config init.defaultBranch trunk`.cwd(tmp.path).quiet()
await withVcsOnly(tmp.path, async (rt) => {
const branch = await rt.runPromise(Vcs.Service.use((s) => s.defaultBranch()))
expect(branch).toBe("trunk")
})
})
test("detects current branch from the active worktree", async () => {
await using tmp = await tmpdir({ git: true })
await using wt = await tmpdir()
await $`git branch -M main`.cwd(tmp.path).quiet()
const dir = path.join(wt.path, "feature")
await $`git worktree add -b feature/test ${dir} HEAD`.cwd(tmp.path).quiet()
await withVcsOnly(dir, async (rt) => {
const [branch, base] = await Promise.all([
rt.runPromise(Vcs.Service.use((s) => s.branch())),
rt.runPromise(Vcs.Service.use((s) => s.defaultBranch())),
])
expect(branch).toBe("feature/test")
expect(base).toBe("main")
})
})
test("diff('git') returns uncommitted changes", async () => {
await using tmp = await tmpdir({ git: true })
await fs.writeFile(path.join(tmp.path, "file.txt"), "original\n", "utf-8")
await $`git add .`.cwd(tmp.path).quiet()
await $`git commit --no-gpg-sign -m "add file"`.cwd(tmp.path).quiet()
await fs.writeFile(path.join(tmp.path, "file.txt"), "changed\n", "utf-8")
await withVcsOnly(tmp.path, async (rt) => {
const diff = await rt.runPromise(Vcs.Service.use((s) => s.diff("git")))
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: "file.txt",
status: "modified",
}),
]),
)
})
})
test("diff('git') handles special filenames", async () => {
await using tmp = await tmpdir({ git: true })
const file = weird
await fs.writeFile(path.join(tmp.path, file), "hello\n", "utf-8")
await withVcsOnly(tmp.path, async (rt) => {
const diff = await rt.runPromise(Vcs.Service.use((s) => s.diff("git")))
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file,
status: "added",
}),
]),
)
})
})
test("diff('branch') returns changes against default branch", async () => {
await using tmp = await tmpdir({ git: true })
await $`git branch -M main`.cwd(tmp.path).quiet()
await $`git checkout -b feature/test`.cwd(tmp.path).quiet()
await fs.writeFile(path.join(tmp.path, "branch.txt"), "hello\n", "utf-8")
await $`git add .`.cwd(tmp.path).quiet()
await $`git commit --no-gpg-sign -m "branch file"`.cwd(tmp.path).quiet()
await withVcsOnly(tmp.path, async (rt) => {
const diff = await rt.runPromise(Vcs.Service.use((s) => s.diff("branch")))
expect(diff).toEqual(
expect.arrayContaining([
expect.objectContaining({
file: "branch.txt",
status: "added",
}),
]),
)
})
})
})