in app/lib/server/processors/ApiJobExecutor.ts [755:825]
private extractContentFromDiff(diff: string): string {
// Extract content from diff format
// This handles unified diff format and extracts added lines
if (!diff || diff.trim() === "") {
console.warn("Empty diff provided, returning empty content");
return "";
}
const lines = diff.split("\n");
const content: string[] = [];
let foundContent = false;
console.log(`Extracting content from diff with ${lines.length} lines`);
for (const line of lines) {
if (line.startsWith("+") && !line.startsWith("+++")) {
// Add line (remove the + prefix)
content.push(line.substring(1));
foundContent = true;
} else if (
!line.startsWith("-") &&
!line.startsWith("@@") &&
!line.startsWith("index") &&
!line.startsWith("diff") &&
!line.startsWith("+++") &&
!line.startsWith("---")
) {
// Context line (unchanged)
if (line.trim() !== "") {
content.push(line);
foundContent = true;
}
}
}
const result = content.join("\n");
console.log(
`Extracted ${content.length} lines, found content: ${foundContent}, result length: ${result.length}`
);
// If no content was found in the diff, it might be a simple text replacement
// Try to extract everything after the diff headers
if (!foundContent && diff.includes("@@")) {
const hunkStart = diff.indexOf("@@");
const secondHunkStart = diff.indexOf("@@", hunkStart + 2);
if (secondHunkStart !== -1) {
const hunkContent = diff.substring(
secondHunkStart + diff.substring(secondHunkStart).indexOf("\n") + 1
);
const hunkLines = hunkContent.split("\n");
const extractedContent: string[] = [];
for (const line of hunkLines) {
if (line.startsWith("+")) {
extractedContent.push(line.substring(1));
} else if (line.startsWith(" ")) {
extractedContent.push(line.substring(1));
}
}
if (extractedContent.length > 0) {
console.log(
`Fallback extraction found ${extractedContent.length} lines`
);
return extractedContent.join("\n");
}
}
}
return result;
}