mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
section 11: Phase 11 — Diff sources: contract and git reader
This commit is contained in:
1 parent
df1891a52d
commit
11c23da67e
7 files changed
+750
-7
No files matched your search
@@ -115,3 +115,156 @@ export function splitDiffHunks(patch: string): DiffHunk[] {
|
||||
function toHunk(lines: string[]): DiffHunk {
|
||||
return { header: lines[0]!, text: lines.join("\n") };
|
||||
}
|
||||
|
||||
export interface UnifiedFileDiff {
|
||||
file: string;
|
||||
status: "added" | "deleted" | "modified";
|
||||
patch: string;
|
||||
additions: number;
|
||||
deletions: number;
|
||||
}
|
||||
|
||||
export function splitUnifiedDiffByFile(diff: string): UnifiedFileDiff[] {
|
||||
const blocks: string[][] = [];
|
||||
|
||||
for (const line of diff.split("\n")) {
|
||||
if (line.startsWith("diff --git ")) {
|
||||
blocks.push([line]);
|
||||
} else if (blocks.length > 0) {
|
||||
blocks[blocks.length - 1]!.push(line);
|
||||
}
|
||||
}
|
||||
|
||||
return blocks.map((lines) => toUnifiedFileDiff(lines));
|
||||
}
|
||||
|
||||
function toUnifiedFileDiff(lines: string[]): UnifiedFileDiff {
|
||||
const header = lines[0]!;
|
||||
const newPath = lines.find((line) => line.startsWith("+++ "));
|
||||
const oldPath = lines.find((line) => line.startsWith("--- "));
|
||||
const renameTo = lines.find((line) => line.startsWith("rename to "));
|
||||
let file = "";
|
||||
let status: UnifiedFileDiff["status"] = "modified";
|
||||
let additions = 0;
|
||||
let deletions = 0;
|
||||
|
||||
if (newPath && newPath.slice(4) !== "/dev/null") {
|
||||
file = parseDiffPathToken(newPath.slice(4), "b/");
|
||||
} else if (oldPath && oldPath.slice(4) !== "/dev/null") {
|
||||
file = parseDiffPathToken(oldPath.slice(4), "a/");
|
||||
} else if (renameTo) {
|
||||
file = parseDiffPathToken(renameTo.slice("rename to ".length), "");
|
||||
} else {
|
||||
file = newPathFromDiffHeader(header);
|
||||
}
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith("new file mode")) {
|
||||
status = "added";
|
||||
} else if (line.startsWith("deleted file mode")) {
|
||||
status = "deleted";
|
||||
}
|
||||
|
||||
if (line.startsWith("+++") || line.startsWith("---")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (line.startsWith("+")) {
|
||||
additions++;
|
||||
} else if (line.startsWith("-")) {
|
||||
deletions++;
|
||||
}
|
||||
}
|
||||
|
||||
return { file, status, patch: lines.join("\n"), additions, deletions };
|
||||
}
|
||||
|
||||
function stripDiffPathPrefix(path: string, prefix: string): string {
|
||||
return path.startsWith(prefix) ? path.slice(prefix.length) : path;
|
||||
}
|
||||
|
||||
const GIT_C_ESCAPES: Record<string, number> = {
|
||||
a: 0x07,
|
||||
b: 0x08,
|
||||
f: 0x0c,
|
||||
n: 0x0a,
|
||||
r: 0x0d,
|
||||
t: 0x09,
|
||||
v: 0x0b,
|
||||
"\\": 0x5c,
|
||||
'"': 0x22,
|
||||
};
|
||||
|
||||
function parseDiffPathToken(raw: string, prefix: string): string {
|
||||
const token = raw.startsWith('"') ? decodeGitQuotedPath(raw) : raw.split("\t")[0]!;
|
||||
return stripDiffPathPrefix(token, prefix);
|
||||
}
|
||||
|
||||
function decodeGitQuotedPath(raw: string): string {
|
||||
const bytes: number[] = [];
|
||||
const encoder = new TextEncoder();
|
||||
let index = 1;
|
||||
|
||||
while (index < raw.length) {
|
||||
const char = raw[index]!;
|
||||
if (char === '"') {
|
||||
break;
|
||||
}
|
||||
if (char === "\\") {
|
||||
const octal = /^[0-7]{1,3}/.exec(raw.slice(index + 1));
|
||||
if (octal) {
|
||||
bytes.push(parseInt(octal[0]!, 8) & 0xff);
|
||||
index += 1 + octal[0]!.length;
|
||||
continue;
|
||||
}
|
||||
const next = raw[index + 1];
|
||||
if (next === undefined) {
|
||||
break;
|
||||
}
|
||||
const escaped = GIT_C_ESCAPES[next];
|
||||
bytes.push(...(escaped === undefined ? encoder.encode(next) : [escaped]));
|
||||
index += 2;
|
||||
continue;
|
||||
}
|
||||
bytes.push(...encoder.encode(char));
|
||||
index += 1;
|
||||
}
|
||||
|
||||
return new TextDecoder().decode(new Uint8Array(bytes));
|
||||
}
|
||||
|
||||
function newPathFromDiffHeader(header: string): string {
|
||||
const rest = header.slice("diff --git ".length);
|
||||
|
||||
if (rest.startsWith('"')) {
|
||||
const end = findClosingQuote(rest);
|
||||
if (end >= 0) {
|
||||
let index = end + 1;
|
||||
while (rest[index] === " ") {
|
||||
index += 1;
|
||||
}
|
||||
return parseDiffPathToken(rest.slice(index), "b/");
|
||||
}
|
||||
}
|
||||
|
||||
const quoted = rest.indexOf(' "');
|
||||
if (quoted >= 0) {
|
||||
return parseDiffPathToken(rest.slice(quoted + 1), "b/");
|
||||
}
|
||||
|
||||
const index = rest.lastIndexOf(" b/");
|
||||
return index >= 0 ? parseDiffPathToken(rest.slice(index + 1), "b/") : "";
|
||||
}
|
||||
|
||||
function findClosingQuote(raw: string): number {
|
||||
for (let index = 1; index < raw.length; index += 1) {
|
||||
if (raw[index] === "\\") {
|
||||
index += 1;
|
||||
continue;
|
||||
}
|
||||
if (raw[index] === '"') {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
Reference in new issue
Block a user