private async applyDiffToFile()

in app/lib/server/processors/DockerJobExecutor.ts [628:679]


  private async applyDiffToFile(
    filePath: string,
    diff: string,
    isNewFile: boolean
  ): Promise<void> {
    const { promises: fs } = await import("fs");

    if (!diff) {
      console.warn(`No diff content for ${filePath}`);
      return;
    }

    console.log(`Applying diff to ${filePath}:`, {
      isNewFile,
      diffLength: diff.length,
      diffPreview: diff.substring(0, 200) + (diff.length > 200 ? "..." : ""),
    });

    if (isNewFile) {
      // For new files, extract content from diff
      const content = this.extractContentFromDiff(diff);
      console.log(
        `Extracted content for new file (${content.length} chars):`,
        content.substring(0, 100)
      );
      await fs.writeFile(filePath, content, "utf8");
    } else {
      // For existing files, apply patch
      try {
        const currentContent = await fs.readFile(filePath, "utf8");
        console.log(
          `Current file content (${currentContent.length} chars):`,
          currentContent.substring(0, 100)
        );
        const patchedContent = this.applySimplePatch(currentContent, diff);
        console.log(
          `Patched content (${patchedContent.length} chars):`,
          patchedContent.substring(0, 100)
        );
        await fs.writeFile(filePath, patchedContent, "utf8");
      } catch (error) {
        console.error(`Failed to apply patch to ${filePath}:`, error);
        // Fallback: try to extract content from diff
        const content = this.extractContentFromDiff(diff);
        console.log(
          `Fallback: extracted content (${content.length} chars):`,
          content.substring(0, 100)
        );
        await fs.writeFile(filePath, content, "utf8");
      }
    }
  }