git.test.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { describe, expect } from "bun:test"
  2. import { $ } from "bun"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { Effect } from "effect"
  6. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  7. import { Git } from "@opencode-ai/core/git"
  8. import { AbsolutePath, RelativePath } from "@opencode-ai/core/schema"
  9. import { branch, commit, gitRemote } from "./fixture/git"
  10. import { tmpdir } from "./fixture/tmpdir"
  11. import { testEffect } from "./lib/effect"
  12. const it = testEffect(LayerNode.compile(Git.node))
  13. describe("Git", () => {
  14. it.live("clones a remote and reads checkout metadata", () =>
  15. withRemote((fixture) =>
  16. Effect.gen(function* () {
  17. const git = yield* Git.Service
  18. const target = AbsolutePath.make(path.join(fixture.root, "checkout"))
  19. const repository = yield* git.repo.clone({ remote: fixture.remote, directory: target })
  20. expect(yield* git.remote.get(repository)).toBe(fixture.remote)
  21. expect(yield* git.history.head(repository)).toBeString()
  22. expect(yield* git.history.branch(repository)).toBe("main")
  23. expect(yield* git.history.defaultRemoteBranch(repository)).toBe("main")
  24. expect(repository.worktree).toBe(target)
  25. expect(repository.gitDirectory).toBe(AbsolutePath.make(path.join(target, ".git")))
  26. expect(repository.commonDirectory).toBe(repository.gitDirectory)
  27. expect(yield* read(path.join(target, "README.md"))).toBe("one\n")
  28. }),
  29. ),
  30. )
  31. it.live("fetches, checks out, and resets remote changes", () =>
  32. withRemote((fixture) =>
  33. Effect.gen(function* () {
  34. const git = yield* Git.Service
  35. const target = AbsolutePath.make(path.join(fixture.root, "checkout"))
  36. const repository = yield* git.repo.clone({ remote: fixture.remote, directory: target })
  37. yield* Effect.promise(() => commit(fixture.source, "two\n", "second"))
  38. yield* git.sync.fetchRemotes(repository)
  39. yield* git.sync.resetHard(repository, "origin/main")
  40. expect(yield* read(path.join(target, "README.md"))).toBe("two\n")
  41. yield* Effect.promise(() => branch(fixture.source, "feature/docs", "feature\n"))
  42. yield* git.sync.fetchBranch(repository, { branch: "feature/docs" })
  43. yield* git.sync.checkoutRemoteBranch(repository, { branch: "feature/docs" })
  44. yield* git.sync.resetHard(repository, "origin/feature/docs")
  45. expect(yield* git.history.branch(repository)).toBe("feature/docs")
  46. expect(yield* read(path.join(target, "README.md"))).toBe("feature\n")
  47. }),
  48. ),
  49. )
  50. })
  51. function withRemote<A, E, R>(body: (fixture: Awaited<ReturnType<typeof gitRemote>>) => Effect.Effect<A, E, R>) {
  52. return Effect.acquireUseRelease(
  53. Effect.promise(async () => {
  54. const root = await tmpdir()
  55. return { root, fixture: await gitRemote(root.path) }
  56. }),
  57. (input) => body(input.fixture),
  58. (input) => Effect.promise(() => input.root[Symbol.asyncDispose]()),
  59. )
  60. }
  61. function read(file: string) {
  62. return Effect.promise(() => fs.readFile(file, "utf8")).pipe(Effect.map((content) => content.replace(/\r\n/g, "\n")))
  63. }
  64. async function initRepo(directory: string) {
  65. await $`git init`.cwd(directory).quiet()
  66. await $`git config core.fsmonitor false`.cwd(directory).quiet()
  67. await $`git config commit.gpgsign false`.cwd(directory).quiet()
  68. await $`git config user.email test@opencode.test`.cwd(directory).quiet()
  69. await $`git config user.name Test`.cwd(directory).quiet()
  70. await $`git commit --allow-empty -m root`.cwd(directory).quiet()
  71. }
  72. describe("Git worktrees", () => {
  73. it.live("creates, lists, and removes linked worktrees", () =>
  74. Effect.gen(function* () {
  75. const root = yield* Effect.acquireRelease(
  76. Effect.promise(() => tmpdir()),
  77. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  78. )
  79. yield* Effect.promise(() => initRepo(root.path))
  80. const directory = AbsolutePath.make(yield* Effect.promise(() => fs.realpath(root.path)))
  81. const worktree = AbsolutePath.make(`${root.path}-git-worktree`)
  82. yield* Effect.addFinalizer(() =>
  83. Effect.promise(() => fs.rm(worktree, { recursive: true, force: true })).pipe(Effect.ignore),
  84. )
  85. const git = yield* Git.Service
  86. const repo = yield* git.repo.discover(directory)
  87. if (!repo) throw new Error("Repository not found")
  88. yield* git.worktree.create({ repository: repo, directory: worktree })
  89. expect((yield* git.worktree.list(repo)).some((entry) => entry.directory.endsWith("-git-worktree"))).toBe(true)
  90. const linked = yield* git.repo.discover(worktree)
  91. expect(linked?.worktree).toBe(AbsolutePath.make(yield* Effect.promise(() => fs.realpath(worktree))))
  92. expect(linked?.commonDirectory).toBe(repo.commonDirectory)
  93. expect(linked?.gitDirectory).not.toBe(repo.gitDirectory)
  94. if (!linked) throw new Error("Linked worktree not found")
  95. yield* git.worktree.remove({ repository: linked, directory: worktree, force: false })
  96. expect((yield* git.worktree.list(repo)).some((entry) => entry.directory.endsWith("-git-worktree"))).toBe(false)
  97. }),
  98. )
  99. })
  100. describe("Git trees", () => {
  101. it.live("captures, compares, previews, and restores scoped trees", () =>
  102. Effect.gen(function* () {
  103. const root = yield* Effect.acquireRelease(
  104. Effect.promise(() => tmpdir()),
  105. (dir) => Effect.promise(() => dir[Symbol.asyncDispose]()),
  106. )
  107. yield* Effect.promise(async () => {
  108. await initRepo(root.path)
  109. await fs.mkdir(path.join(root.path, "scope"))
  110. await fs.writeFile(path.join(root.path, "scope", "tracked.txt"), "one\n")
  111. await fs.writeFile(path.join(root.path, "outside.txt"), "outside\n")
  112. await $`git add .`.cwd(root.path).quiet()
  113. await $`git commit -m initial`.cwd(root.path).quiet()
  114. })
  115. const git = yield* Git.Service
  116. const source = yield* git.repo.discover(AbsolutePath.make(root.path))
  117. if (!source) throw new Error("Repository not found")
  118. const storage = AbsolutePath.make(path.join(root.path, ".snapshot"))
  119. const repository = yield* git.repo.create({ worktree: source.worktree, gitDirectory: storage, seed: source })
  120. yield* git.index.refresh({ repository, scope: RelativePath.make("scope") })
  121. const before = yield* git.tree.write(repository)
  122. yield* Effect.promise(async () => {
  123. await fs.writeFile(path.join(root.path, "scope", "tracked.txt"), "two\n")
  124. await fs.writeFile(path.join(root.path, "scope", "added.txt"), "added\n")
  125. await fs.writeFile(path.join(root.path, "outside.txt"), "changed outside\n")
  126. })
  127. yield* git.index.refresh({ repository, scope: RelativePath.make("scope") })
  128. const after = yield* git.tree.write(repository)
  129. expect(yield* git.tree.files({ repository, from: before, to: after })).toEqual([
  130. RelativePath.make("scope/added.txt"),
  131. RelativePath.make("scope/tracked.txt"),
  132. ])
  133. const diffs = yield* git.tree.diff({ repository, from: before, to: after, context: 1 })
  134. expect(diffs.map((item) => [item.path, item.status])).toEqual([
  135. [RelativePath.make("scope/added.txt"), "added"],
  136. [RelativePath.make("scope/tracked.txt"), "modified"],
  137. ])
  138. const files = new Map([[RelativePath.make("scope/tracked.txt"), before]])
  139. const preview = yield* git.tree.preview({ repository, current: after, files, context: 1 })
  140. expect(preview).toHaveLength(1)
  141. expect(preview[0]?.path).toBe(RelativePath.make("scope/tracked.txt"))
  142. yield* git.tree.restore({ repository, files })
  143. expect(yield* read(path.join(root.path, "scope", "tracked.txt"))).toBe("one\n")
  144. expect(yield* read(path.join(root.path, "scope", "added.txt"))).toBe("added\n")
  145. expect(yield* read(path.join(root.path, "outside.txt"))).toBe("changed outside\n")
  146. }),
  147. )
  148. })