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 { quoteGitPatchPath } from "@t3tools/shared/gitPatchPath"; |
| 2 | import { structuredPatch } from "diff"; |
| 3 | |
| 4 | import type { AzureDevOpsChangeEntry } from "./azureDevOpsPullRequestJson.ts"; |
| 5 | |
| 6 | /** Pin the iteration to prevent pushes from renumbering files during pagination. */ |
| 7 | export interface AzureDevOpsDiffCursor { |
| 8 | readonly iterationId: number; |
| 9 | readonly fileIndex: number; |
| 10 | } |
| 11 | |
| 12 | const CURSOR_SEPARATOR = ":"; |
| 13 | |
| 14 | const CURSOR_COMPONENT = /^\d+$/; |
| 15 | |
| 16 | export function formatAzureDevOpsDiffCursor(cursor: AzureDevOpsDiffCursor): string { |
| 17 | return `${cursor.iterationId}${CURSOR_SEPARATOR}${cursor.fileIndex}`; |
| 18 | } |
| 19 | |
| 20 | export function parseAzureDevOpsDiffCursor( |
| 21 | raw: string | null | undefined, |
| 22 | ): AzureDevOpsDiffCursor | null { |
| 23 | if (raw === null || raw === undefined) return null; |
| 24 | const [iteration, file, ...rest] = raw.split(CURSOR_SEPARATOR); |
| 25 | if (rest.length > 0) return null; |
| 26 | if (iteration === undefined || file === undefined) return null; |
| 27 | if (!CURSOR_COMPONENT.test(iteration) || !CURSOR_COMPONENT.test(file)) return null; |
| 28 | const iterationId = Number(iteration); |
| 29 | const fileIndex = Number(file); |
| 30 | if (!Number.isSafeInteger(iterationId) || iterationId <= 0) return null; |
| 31 | if (!Number.isSafeInteger(fileIndex) || fileIndex < 0) return null; |
| 32 | return { iterationId, fileIndex }; |
| 33 | } |
| 34 | |
| 35 | export interface AzureDevOpsFileTexts { |
| 36 | readonly oldContents: string; |
| 37 | readonly newContents: string; |
| 38 | readonly binary: boolean; |
| 39 | } |
| 40 | |
| 41 | export interface AzureDevOpsFilePatch { |
| 42 | readonly section: string; |
| 43 | readonly truncated: boolean; |
| 44 | readonly abandoned: boolean; |
| 45 | readonly edits: number; |
| 46 | } |
| 47 | |
| 48 | /** Azure returns whole files; oversized files incur both reads before diffing. */ |
| 49 | const MAX_FILE_BYTES = 512 * 1024; |
| 50 | |
| 51 | const PATCH_CONTEXT_LINES = 3; |
| 52 | |
| 53 | /** Edit distance bounds quadratic work independently of machine speed. */ |
| 54 | export const MAX_FILE_DIFF_EDITS = 2_000; |
| 55 | |
| 56 | const MAX_FILE_DIFF_MILLIS = 2_000; |
| 57 | |
| 58 | export const MAX_DIFF_SLICE_EDITS = 6_000; |
| 59 | |
| 60 | export const MAX_DIFF_SLICE_BYTES = 256 * 1024; |
| 61 | |
| 62 | /** Header-only entries can evade the byte and edit budgets. */ |
| 63 | export const MAX_DIFF_SLICE_FILES = 300; |
| 64 | |
| 65 | const NO_NEWLINE_MARKER = "\\ No newline at end of file"; |
| 66 | |
| 67 | function contentLines(contents: string): ReadonlyArray<string> { |
| 68 | if (contents === "") return []; |
| 69 | const lines = contents.split("\n"); |
| 70 | if (lines.at(-1) === "") lines.pop(); |
| 71 | return lines; |
| 72 | } |
| 73 | |
| 74 | function isBinary(contents: string): boolean { |
| 75 | return contents.includes("\u0000"); |
| 76 | } |
| 77 | |
| 78 | export const byteLength = (contents: string) => Buffer.byteLength(contents, "utf8"); |
| 79 | |
| 80 | /** Git anchors empty ranges at the preceding line, which may be zero. */ |
| 81 | function hunkRange(start: number, lines: number): string { |
| 82 | if (lines === 0) return `${start - 1},0`; |
| 83 | return lines === 1 ? String(start) : `${start},${lines}`; |
| 84 | } |
| 85 | |
| 86 | /** Azure omits modes. Quote prefixed paths to preserve tabs/newlines. */ |
| 87 | function patchHeader(change: AzureDevOpsChangeEntry): string { |
| 88 | const oldSide = quoteGitPatchPath(`a/${change.oldPath}`); |
| 89 | const newSide = quoteGitPatchPath(`b/${change.path}`); |
| 90 | const lines = [`diff --git ${oldSide} ${newSide}`]; |
| 91 | if (change.changeKind === "new") lines.push("new file mode 100644"); |
| 92 | if (change.changeKind === "deleted") lines.push("deleted file mode 100644"); |
| 93 | if (change.changeKind === "rename-pure" || change.changeKind === "rename-changed") { |
| 94 | // Pierre needs this header to keep the rename. |
| 95 | lines.push( |
| 96 | `similarity index ${change.changeKind === "rename-pure" ? 100 : 0}%`, |
| 97 | `rename from ${quoteGitPatchPath(change.oldPath)}`, |
| 98 | `rename to ${quoteGitPatchPath(change.path)}`, |
| 99 | ); |
| 100 | } |
| 101 | lines.push( |
| 102 | `--- ${change.changeKind === "new" ? "/dev/null" : oldSide}`, |
| 103 | `+++ ${change.changeKind === "deleted" ? "/dev/null" : newSide}`, |
| 104 | ); |
| 105 | return lines.join("\n"); |
| 106 | } |
| 107 | |
| 108 | function replacementSection(header: string, texts: AzureDevOpsFileTexts): string { |
| 109 | const oldLines = contentLines(texts.oldContents); |
| 110 | const newLines = contentLines(texts.newContents); |
| 111 | const noNewline = (contents: string, lines: ReadonlyArray<string>) => |
| 112 | lines.length > 0 && !contents.endsWith("\n") ? [NO_NEWLINE_MARKER] : []; |
| 113 | return [ |
| 114 | header, |
| 115 | `@@ -${hunkRange(1, oldLines.length)} +${hunkRange(1, newLines.length)} @@`, |
| 116 | ...oldLines.map((line) => `-${line}`), |
| 117 | ...noNewline(texts.oldContents, oldLines), |
| 118 | ...newLines.map((line) => `+${line}`), |
| 119 | ...noNewline(texts.newContents, newLines), |
| 120 | "", |
| 121 | ].join("\n"); |
| 122 | } |
| 123 | |
| 124 | export function azureDevOpsFilePatch(input: { |
| 125 | readonly change: AzureDevOpsChangeEntry; |
| 126 | readonly texts: AzureDevOpsFileTexts; |
| 127 | }): AzureDevOpsFilePatch { |
| 128 | const header = patchHeader(input.change); |
| 129 | const { oldContents, newContents } = input.texts; |
| 130 | |
| 131 | if (input.texts.binary || isBinary(oldContents) || isBinary(newContents)) { |
| 132 | const oldSide = quoteGitPatchPath(`a/${input.change.oldPath}`); |
| 133 | const newSide = quoteGitPatchPath(`b/${input.change.path}`); |
| 134 | const binary = `Binary files ${oldSide} and ${newSide} differ`; |
| 135 | return { section: `${header}\n${binary}\n`, truncated: true, abandoned: false, edits: 0 }; |
| 136 | } |
| 137 | if (byteLength(oldContents) > MAX_FILE_BYTES || byteLength(newContents) > MAX_FILE_BYTES) { |
| 138 | return { section: `${header}\n`, truncated: true, abandoned: false, edits: 0 }; |
| 139 | } |
| 140 | |
| 141 | const created = oldContents === "" && newContents !== ""; |
| 142 | const deleted = newContents === "" && oldContents !== ""; |
| 143 | if (created || deleted) { |
| 144 | const contents = created ? newContents : oldContents; |
| 145 | const lines = contentLines(contents); |
| 146 | // Include per-line markers in the byte budget. |
| 147 | if (byteLength(contents) + lines.length > MAX_FILE_BYTES) { |
| 148 | return { section: `${header}\n`, truncated: true, abandoned: false, edits: lines.length }; |
| 149 | } |
| 150 | const section = replacementSection(header, input.texts); |
| 151 | if (byteLength(section) > MAX_FILE_BYTES) { |
| 152 | return { section: `${header}\n`, truncated: true, abandoned: false, edits: lines.length }; |
| 153 | } |
| 154 | return { section, truncated: false, abandoned: false, edits: lines.length }; |
| 155 | } |
| 156 | |
| 157 | const patch = structuredPatch( |
| 158 | `a/${input.change.oldPath}`, |
| 159 | `b/${input.change.path}`, |
| 160 | oldContents, |
| 161 | newContents, |
| 162 | undefined, |
| 163 | undefined, |
| 164 | { |
| 165 | context: PATCH_CONTEXT_LINES, |
| 166 | maxEditLength: MAX_FILE_DIFF_EDITS, |
| 167 | timeout: MAX_FILE_DIFF_MILLIS, |
| 168 | }, |
| 169 | ); |
| 170 | // A full replacement could bury a small change that hit the edit limit. |
| 171 | if (patch === undefined) { |
| 172 | return { |
| 173 | section: `${header}\n`, |
| 174 | truncated: true, |
| 175 | abandoned: true, |
| 176 | edits: MAX_FILE_DIFF_EDITS, |
| 177 | }; |
| 178 | } |
| 179 | |
| 180 | let edits = 0; |
| 181 | const hunks = patch.hunks.map((hunk) => { |
| 182 | for (const line of hunk.lines) { |
| 183 | if (line.startsWith("+") || line.startsWith("-")) edits += 1; |
| 184 | } |
| 185 | return [ |
| 186 | `@@ -${hunkRange(hunk.oldStart, hunk.oldLines)} +${hunkRange(hunk.newStart, hunk.newLines)} @@`, |
| 187 | ...hunk.lines, |
| 188 | ].join("\n"); |
| 189 | }); |
| 190 | const section = hunks.length === 0 ? `${header}\n` : `${header}\n${hunks.join("\n")}\n`; |
| 191 | // Long lines can exceed the byte budget even within the edit budget. |
| 192 | if (byteLength(section) > MAX_FILE_BYTES) { |
| 193 | return { section: `${header}\n`, truncated: true, abandoned: false, edits }; |
| 194 | } |
| 195 | return { section, truncated: false, abandoned: false, edits }; |
| 196 | } |
| 197 | |
| 198 | export function azureDevOpsUnreadableFilePatch( |
| 199 | change: AzureDevOpsChangeEntry, |
| 200 | ): AzureDevOpsFilePatch { |
| 201 | return { section: `${patchHeader(change)}\n`, truncated: true, abandoned: false, edits: 0 }; |
| 202 | } |