The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { GitCommandError } from "@t3tools/contracts"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Option from "effect/Option"; |
| 4 | |
| 5 | import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 6 | |
| 7 | const TEMPLATE_MAX_BYTES = 8_000; |
| 8 | const TREE_LIST_MAX_BYTES = 100_000; |
| 9 | const TRUNCATION_MARKER = "[truncated]"; |
| 10 | |
| 11 | const TEMPLATE_PATHS = [ |
| 12 | ".github/pull_request_template.md", |
| 13 | ".github/PULL_REQUEST_TEMPLATE.md", |
| 14 | ".github/pull_request_template.txt", |
| 15 | ".github/PULL_REQUEST_TEMPLATE.txt", |
| 16 | "pull_request_template.md", |
| 17 | "PULL_REQUEST_TEMPLATE.md", |
| 18 | "pull_request_template.txt", |
| 19 | "PULL_REQUEST_TEMPLATE.txt", |
| 20 | "docs/pull_request_template.md", |
| 21 | "docs/PULL_REQUEST_TEMPLATE.md", |
| 22 | "docs/pull_request_template.txt", |
| 23 | "docs/PULL_REQUEST_TEMPLATE.txt", |
| 24 | ] as const; |
| 25 | |
| 26 | const TEMPLATE_DIRECTORIES = [ |
| 27 | ".github/PULL_REQUEST_TEMPLATE", |
| 28 | ".github/pull_request_template", |
| 29 | "PULL_REQUEST_TEMPLATE", |
| 30 | "pull_request_template", |
| 31 | "docs/PULL_REQUEST_TEMPLATE", |
| 32 | "docs/pull_request_template", |
| 33 | ] as const; |
| 34 | |
| 35 | type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"]; |
| 36 | |
| 37 | interface TemplateTreeEntry { |
| 38 | readonly objectId: string; |
| 39 | readonly path: string; |
| 40 | } |
| 41 | |
| 42 | function parseTemplateTreeEntries(output: string): ReadonlyArray<TemplateTreeEntry> { |
| 43 | const entries: TemplateTreeEntry[] = []; |
| 44 | for (const record of output.split("\0")) { |
| 45 | if (record.length === 0) { |
| 46 | continue; |
| 47 | } |
| 48 | |
| 49 | const separator = record.indexOf("\t"); |
| 50 | if (separator < 0) { |
| 51 | continue; |
| 52 | } |
| 53 | |
| 54 | const [mode, type, objectId] = record.slice(0, separator).split(" "); |
| 55 | if ( |
| 56 | type !== "blob" || |
| 57 | (mode !== "100644" && mode !== "100755") || |
| 58 | !objectId || |
| 59 | !/^[0-9a-f]{40,64}$/.test(objectId) |
| 60 | ) { |
| 61 | continue; |
| 62 | } |
| 63 | |
| 64 | entries.push({ objectId, path: record.slice(separator + 1) }); |
| 65 | } |
| 66 | return entries; |
| 67 | } |
| 68 | |
| 69 | function readTemplateBlob(input: { |
| 70 | readonly cwd: string; |
| 71 | readonly executeGit: ExecuteGit; |
| 72 | readonly entry: TemplateTreeEntry; |
| 73 | }): Effect.Effect<Option.Option<string>, GitCommandError> { |
| 74 | return input |
| 75 | .executeGit({ |
| 76 | operation: "gitHubChangeRequestTemplate.readTemplateBlob", |
| 77 | cwd: input.cwd, |
| 78 | args: ["cat-file", "blob", input.entry.objectId], |
| 79 | maxOutputBytes: TEMPLATE_MAX_BYTES, |
| 80 | appendTruncationMarker: true, |
| 81 | }) |
| 82 | .pipe( |
| 83 | Effect.map((result) => { |
| 84 | const template = result.stdout.trim(); |
| 85 | if (template.length === 0) { |
| 86 | return Option.none(); |
| 87 | } |
| 88 | return Option.some( |
| 89 | result.stdoutTruncated && !template.endsWith(TRUNCATION_MARKER) |
| 90 | ? `${template}\n\n${TRUNCATION_MARKER}` |
| 91 | : template, |
| 92 | ); |
| 93 | }), |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | type DirectoryTemplateResult = |
| 98 | | { readonly _tag: "None" } |
| 99 | | { readonly _tag: "Ambiguous" } |
| 100 | | { readonly _tag: "Template"; readonly template: string }; |
| 101 | |
| 102 | const readTemplateDirectory = Effect.fn("gitHubChangeRequestTemplate.readTemplateDirectory")( |
| 103 | function* (input: { |
| 104 | readonly cwd: string; |
| 105 | readonly executeGit: ExecuteGit; |
| 106 | readonly entries: ReadonlyArray<TemplateTreeEntry>; |
| 107 | readonly directory: string; |
| 108 | }): Effect.fn.Return<DirectoryTemplateResult, GitCommandError> { |
| 109 | const prefix = `${input.directory}/`; |
| 110 | const candidates = input.entries.filter((entry) => { |
| 111 | if (!entry.path.startsWith(prefix)) { |
| 112 | return false; |
| 113 | } |
| 114 | const relativePath = entry.path.slice(prefix.length); |
| 115 | return !relativePath.includes("/") && /\.(?:md|txt)$/i.test(relativePath); |
| 116 | }); |
| 117 | |
| 118 | const templates: string[] = []; |
| 119 | for (const entry of candidates) { |
| 120 | const template = yield* readTemplateBlob({ ...input, entry }); |
| 121 | if (Option.isSome(template)) { |
| 122 | templates.push(template.value); |
| 123 | if (templates.length > 1) { |
| 124 | return { _tag: "Ambiguous" } as const; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | return templates[0] |
| 130 | ? ({ _tag: "Template", template: templates[0] } as const) |
| 131 | : ({ _tag: "None" } as const); |
| 132 | }, |
| 133 | ); |
| 134 | |
| 135 | export const detect = Effect.fn("gitHubChangeRequestTemplate.detect")(function* ( |
| 136 | cwd: string, |
| 137 | treeish: string, |
| 138 | executeGit: ExecuteGit, |
| 139 | ) { |
| 140 | return yield* Effect.gen(function* () { |
| 141 | // Read committed regular blobs; worktree paths can become symlinks between validation and open. |
| 142 | // ls-tree does not support case-insensitive pathspecs; grep discovers only matching names. |
| 143 | const paths = yield* executeGit({ |
| 144 | operation: "gitHubChangeRequestTemplate.findTemplatePaths", |
| 145 | cwd, |
| 146 | args: [ |
| 147 | "grep", |
| 148 | "-G", |
| 149 | "--no-color", |
| 150 | "-l", |
| 151 | "-z", |
| 152 | "--full-name", |
| 153 | "--no-recurse-submodules", |
| 154 | "-e", |
| 155 | "^", |
| 156 | treeish, |
| 157 | "--", |
| 158 | ...TEMPLATE_PATHS.map((path) => `:(top,icase,literal)${path}`), |
| 159 | ], |
| 160 | maxOutputBytes: TREE_LIST_MAX_BYTES, |
| 161 | }).pipe( |
| 162 | Effect.map((result) => |
| 163 | result.stdoutTruncated |
| 164 | ? [] |
| 165 | : result.stdout |
| 166 | .split("\0") |
| 167 | .filter((path) => path.startsWith(`${treeish}:`)) |
| 168 | .map((path) => path.slice(treeish.length + 1)), |
| 169 | ), |
| 170 | // grep exits with status 1 when no nonempty templates match. |
| 171 | Effect.orElseSucceed(() => []), |
| 172 | ); |
| 173 | const result = yield* executeGit({ |
| 174 | operation: "gitHubChangeRequestTemplate.listTemplates", |
| 175 | cwd, |
| 176 | args: [ |
| 177 | "ls-tree", |
| 178 | "-r", |
| 179 | "-z", |
| 180 | "--full-tree", |
| 181 | treeish, |
| 182 | "--", |
| 183 | ...new Set([...TEMPLATE_PATHS, ...paths]), |
| 184 | ...TEMPLATE_DIRECTORIES, |
| 185 | ], |
| 186 | maxOutputBytes: TREE_LIST_MAX_BYTES, |
| 187 | appendTruncationMarker: true, |
| 188 | }); |
| 189 | if (result.stdoutTruncated) { |
| 190 | return Option.none(); |
| 191 | } |
| 192 | |
| 193 | const entries = parseTemplateTreeEntries(result.stdout); |
| 194 | const candidates = [".github/", "", "docs/"].flatMap((location) => { |
| 195 | const paths = TEMPLATE_PATHS.filter( |
| 196 | (path) => path.slice(0, path.lastIndexOf("/") + 1) === location, |
| 197 | ); |
| 198 | return [ |
| 199 | ...paths.flatMap((path) => entries.filter((entry) => entry.path === path)), |
| 200 | ...entries.filter( |
| 201 | (entry) => |
| 202 | !paths.some((path) => path === entry.path) && |
| 203 | paths.some((path) => path.toLowerCase() === entry.path.toLowerCase()), |
| 204 | ), |
| 205 | ]; |
| 206 | }); |
| 207 | for (const entry of candidates) { |
| 208 | const template = yield* readTemplateBlob({ cwd, executeGit, entry }); |
| 209 | if (Option.isSome(template)) { |
| 210 | return template; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | for (const directory of TEMPLATE_DIRECTORIES) { |
| 215 | const directoryTemplate = yield* readTemplateDirectory({ |
| 216 | cwd, |
| 217 | executeGit, |
| 218 | entries, |
| 219 | directory, |
| 220 | }); |
| 221 | if (directoryTemplate._tag === "Template") { |
| 222 | return Option.some(directoryTemplate.template); |
| 223 | } |
| 224 | if (directoryTemplate._tag === "Ambiguous") { |
| 225 | return Option.none(); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | return Option.none(); |
| 230 | }).pipe(Effect.orElseSucceed(() => Option.none())); |
| 231 | }); |