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