|
| 1 | +import path from "path" |
| 2 | +import z from "zod" |
| 3 | +import { Effect } from "effect" |
| 4 | +import { AppFileSystem } from "@opencode-ai/shared/filesystem" |
| 5 | +import { Flock } from "@opencode-ai/shared/util/flock" |
| 6 | +import { Git } from "@/git" |
| 7 | +import DESCRIPTION from "./repo_clone.txt" |
| 8 | +import * as Tool from "./tool" |
| 9 | +import { parseRepositoryReference, repositoryCachePath, sameRepositoryReference } from "@/util/repository" |
| 10 | + |
| 11 | +const parameters = z.object({ |
| 12 | + repository: z |
| 13 | + .string() |
| 14 | + .describe("Repository to clone, as a git URL, host/path reference, or GitHub owner/repo shorthand"), |
| 15 | + refresh: z.boolean().optional().describe("When true, fetches the latest remote state into the managed cache"), |
| 16 | +}) |
| 17 | + |
| 18 | +function statusForRepository(input: { reuse: boolean; refresh?: boolean }) { |
| 19 | + if (!input.reuse) return "cloned" as const |
| 20 | + if (input.refresh) return "refreshed" as const |
| 21 | + return "cached" as const |
| 22 | +} |
| 23 | + |
| 24 | +function resetTarget(input: { |
| 25 | + remoteHead: { code: number; stdout: string } |
| 26 | + branch: { code: number; stdout: string } |
| 27 | +}) { |
| 28 | + if (input.remoteHead.code === 0 && input.remoteHead.stdout) { |
| 29 | + return input.remoteHead.stdout.replace(/^refs\/remotes\//, "") |
| 30 | + } |
| 31 | + if (input.branch.code === 0 && input.branch.stdout) { |
| 32 | + return `origin/${input.branch.stdout}` |
| 33 | + } |
| 34 | + return "HEAD" |
| 35 | +} |
| 36 | + |
| 37 | +export const RepoCloneTool = Tool.define( |
| 38 | + "repo_clone", |
| 39 | + Effect.gen(function* () { |
| 40 | + const fs = yield* AppFileSystem.Service |
| 41 | + const git = yield* Git.Service |
| 42 | + |
| 43 | + return { |
| 44 | + description: DESCRIPTION, |
| 45 | + parameters, |
| 46 | + execute: (params: z.infer<typeof parameters>, ctx: Tool.Context) => |
| 47 | + Effect.gen(function* () { |
| 48 | + const reference = parseRepositoryReference(params.repository) |
| 49 | + if (!reference) throw new Error("Repository must be a git URL, host/path reference, or GitHub owner/repo shorthand") |
| 50 | + |
| 51 | + const repository = reference.label |
| 52 | + const remote = reference.remote |
| 53 | + const localPath = repositoryCachePath(reference) |
| 54 | + const cloneTarget = parseRepositoryReference(remote) ?? reference |
| 55 | + |
| 56 | + yield* ctx.ask({ |
| 57 | + permission: "repo_clone", |
| 58 | + patterns: [repository], |
| 59 | + always: [repository], |
| 60 | + metadata: { |
| 61 | + repository, |
| 62 | + remote, |
| 63 | + path: localPath, |
| 64 | + refresh: Boolean(params.refresh), |
| 65 | + }, |
| 66 | + }) |
| 67 | + |
| 68 | + return yield* Effect.acquireUseRelease( |
| 69 | + Effect.promise((signal) => Flock.acquire(`repo-clone:${localPath}`, { signal })), |
| 70 | + () => |
| 71 | + Effect.gen(function* () { |
| 72 | + yield* fs.ensureDir(path.dirname(localPath)).pipe(Effect.orDie) |
| 73 | + |
| 74 | + const exists = yield* fs.existsSafe(localPath) |
| 75 | + const hasGitDir = yield* fs.existsSafe(path.join(localPath, ".git")) |
| 76 | + const origin = hasGitDir |
| 77 | + ? yield* git.run(["config", "--get", "remote.origin.url"], { cwd: localPath }) |
| 78 | + : undefined |
| 79 | + const originReference = origin?.exitCode === 0 ? parseRepositoryReference(origin.text().trim()) : undefined |
| 80 | + const reuse = hasGitDir && Boolean(originReference && sameRepositoryReference(originReference, cloneTarget)) |
| 81 | + if (exists && !reuse) { |
| 82 | + yield* fs.remove(localPath, { recursive: true }).pipe(Effect.orDie) |
| 83 | + } |
| 84 | + |
| 85 | + const status = statusForRepository({ reuse, refresh: params.refresh }) |
| 86 | + |
| 87 | + if (status === "cloned") { |
| 88 | + const clone = yield* git.run(["clone", "--depth", "100", remote, localPath], { cwd: path.dirname(localPath) }) |
| 89 | + if (clone.exitCode !== 0) { |
| 90 | + throw new Error(clone.stderr.toString().trim() || clone.text().trim() || `Failed to clone ${repository}`) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + if (status === "refreshed") { |
| 95 | + const fetch = yield* git.run(["fetch", "--all", "--prune"], { cwd: localPath }) |
| 96 | + if (fetch.exitCode !== 0) { |
| 97 | + throw new Error(fetch.stderr.toString().trim() || fetch.text().trim() || `Failed to refresh ${repository}`) |
| 98 | + } |
| 99 | + |
| 100 | + const remoteHead = yield* git.run(["symbolic-ref", "refs/remotes/origin/HEAD"], { cwd: localPath }) |
| 101 | + const branch = yield* git.run(["symbolic-ref", "--quiet", "--short", "HEAD"], { cwd: localPath }) |
| 102 | + const target = resetTarget({ |
| 103 | + remoteHead: { code: remoteHead.exitCode, stdout: remoteHead.text().trim() }, |
| 104 | + branch: { code: branch.exitCode, stdout: branch.text().trim() }, |
| 105 | + }) |
| 106 | + |
| 107 | + const reset = yield* git.run(["reset", "--hard", target], { cwd: localPath }) |
| 108 | + if (reset.exitCode !== 0) { |
| 109 | + throw new Error(reset.stderr.toString().trim() || reset.text().trim() || `Failed to reset ${repository}`) |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + const head = yield* git.run(["rev-parse", "HEAD"], { cwd: localPath }) |
| 114 | + const branch = yield* git.branch(localPath) |
| 115 | + const headText = head.exitCode === 0 ? head.text().trim() : undefined |
| 116 | + |
| 117 | + return { |
| 118 | + title: repository, |
| 119 | + metadata: { |
| 120 | + repository, |
| 121 | + host: reference.host, |
| 122 | + remote, |
| 123 | + localPath, |
| 124 | + status, |
| 125 | + head: headText, |
| 126 | + branch, |
| 127 | + }, |
| 128 | + output: [ |
| 129 | + `Repository ready: ${repository}`, |
| 130 | + `Status: ${status}`, |
| 131 | + `Local path: ${localPath}`, |
| 132 | + ...(branch ? [`Branch: ${branch}`] : []), |
| 133 | + ...(headText ? [`HEAD: ${headText}`] : []), |
| 134 | + ].join("\n"), |
| 135 | + } |
| 136 | + }), |
| 137 | + (lock) => Effect.promise(() => lock.release()).pipe(Effect.ignore), |
| 138 | + ) |
| 139 | + }).pipe(Effect.orDie), |
| 140 | + } |
| 141 | + }), |
| 142 | +) |
0 commit comments