The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | |
| 4 | import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 5 | |
| 6 | const TEMPLATE_DIRECTORIES = [".azuredevops", ".vsts", "docs", ""]; |
| 7 | const TEMPLATE_MAX_BYTES = 8_000; |
| 8 | const TREE_LIST_MAX_BYTES = 100_000; |
| 9 | const TRUNCATION_MARKER = "[truncated]"; |
| 10 | |
| 11 | type ExecuteGit = SourceControlHost.SourceControlHost["Service"]["git"]["execute"]; |
| 12 | |
| 13 | export const detect = Effect.fn("azureDevOpsChangeRequestTemplate.detect")( |
| 14 | function* (cwd: string, treeish: string, executeGit: ExecuteGit) { |
| 15 | // Inspect only the root and template directories; never recurse through the repository. |
| 16 | const root = yield* executeGit({ |
| 17 | operation: "azureDevOpsChangeRequestTemplate.listTemplates", |
| 18 | cwd, |
| 19 | args: ["ls-tree", "-z", "--full-tree", treeish], |
| 20 | maxOutputBytes: TREE_LIST_MAX_BYTES, |
| 21 | appendTruncationMarker: true, |
| 22 | }); |
| 23 | if (root.stdoutTruncated) { |
| 24 | return Option.none(); |
| 25 | } |
| 26 | |
| 27 | const rootEntries = root.stdout.split("\0"); |
| 28 | let objectId: string | undefined; |
| 29 | for (const directory of TEMPLATE_DIRECTORIES) { |
| 30 | let entries = rootEntries; |
| 31 | if (directory !== "") { |
| 32 | const trees = rootEntries.flatMap((record) => { |
| 33 | const entry = /^040000 tree ([0-9a-f]{40,64})\t(.+)$/.exec(record); |
| 34 | return entry?.[1] && entry[2]?.toLowerCase() === directory ? [entry[1]] : []; |
| 35 | }); |
| 36 | entries = []; |
| 37 | for (const tree of trees) { |
| 38 | const listing = yield* executeGit({ |
| 39 | operation: "azureDevOpsChangeRequestTemplate.listTemplates", |
| 40 | cwd, |
| 41 | args: ["ls-tree", "-z", "--full-tree", tree], |
| 42 | maxOutputBytes: TREE_LIST_MAX_BYTES, |
| 43 | appendTruncationMarker: true, |
| 44 | }); |
| 45 | if (listing.stdoutTruncated) { |
| 46 | return Option.none(); |
| 47 | } |
| 48 | entries.push(...listing.stdout.split("\0")); |
| 49 | } |
| 50 | } |
| 51 | // Only regular committed blobs qualify, so symlinks never reach the host filesystem. |
| 52 | objectId = entries |
| 53 | .map((record) => /^100(?:644|755) blob ([0-9a-f]{40,64})\t(.+)$/.exec(record)) |
| 54 | .find((entry) => /^pull_request_template\.(?:md|txt)$/i.test(entry?.[2] ?? ""))?.[1]; |
| 55 | if (objectId) { |
| 56 | break; |
| 57 | } |
| 58 | } |
| 59 | if (!objectId) { |
| 60 | return Option.none(); |
| 61 | } |
| 62 | |
| 63 | const blob = yield* executeGit({ |
| 64 | operation: "azureDevOpsChangeRequestTemplate.readTemplate", |
| 65 | cwd, |
| 66 | args: ["cat-file", "blob", objectId], |
| 67 | maxOutputBytes: TEMPLATE_MAX_BYTES, |
| 68 | appendTruncationMarker: true, |
| 69 | }); |
| 70 | const template = blob.stdout.trim(); |
| 71 | if (template.length === 0) { |
| 72 | return Option.none(); |
| 73 | } |
| 74 | return Option.some( |
| 75 | blob.stdoutTruncated && !template.endsWith(TRUNCATION_MARKER) |
| 76 | ? `${template}\n\n${TRUNCATION_MARKER}` |
| 77 | : template, |
| 78 | ); |
| 79 | }, |
| 80 | Effect.orElseSucceed(() => Option.none()), |
| 81 | ); |