in app/lib/server/processors/DockerJobExecutor.ts [753:817]
private applySimplePatch(originalContent: string, diff: string): string {
// Simple patch application - this is a basic implementation
// For production, consider using a proper diff/patch library like 'diff' or 'node-patch'
const originalLines = originalContent.split("\n");
const diffLines = diff.split("\n");
const result: string[] = [];
let originalIndex = 0;
let inHunk = false;
let hunkOriginalStart = 0;
let hunkOriginalLength = 0;
let hunkNewStart = 0;
let hunkNewLength = 0;
let processedInHunk = 0;
for (const line of diffLines) {
if (line.startsWith("@@")) {
// Parse hunk header
const match = line.match(/@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@/);
if (match) {
hunkOriginalStart = parseInt(match[1]) - 1; // Convert to 0-based
hunkOriginalLength = match[2] ? parseInt(match[2]) : 1;
hunkNewStart = parseInt(match[3]) - 1;
hunkNewLength = match[4] ? parseInt(match[4]) : 1;
// Copy lines before this hunk
while (originalIndex < hunkOriginalStart) {
result.push(originalLines[originalIndex]);
originalIndex++;
}
inHunk = true;
processedInHunk = 0;
}
} else if (inHunk) {
if (line.startsWith("-")) {
// Remove line - skip it in original
originalIndex++;
processedInHunk++;
} else if (line.startsWith("+")) {
// Add line
result.push(line.substring(1));
processedInHunk++;
} else if (line.startsWith(" ")) {
// Context line - keep it
result.push(line.substring(1));
originalIndex++;
processedInHunk++;
}
// Check if hunk is complete
if (processedInHunk >= Math.max(hunkOriginalLength, hunkNewLength)) {
inHunk = false;
}
}
}
// Copy remaining lines
while (originalIndex < originalLines.length) {
result.push(originalLines[originalIndex]);
originalIndex++;
}
return result.join("\n");
}