private async applyFileChanges()

in app/lib/server/processors/ApiJobExecutor.ts [626:702]


  private async applyFileChanges(
    repoPath: string,
    files: any[]
  ): Promise<void> {
    console.log(`Applying changes to ${files.length} files:`);

    for (const file of files) {
      console.log(`Processing file: ${file.filename} (status: ${file.status})`);

      // Use diff field if available, otherwise fall back to patch
      const diffContent = file.diff || file.patch;
      console.log(
        `File diff preview:`,
        diffContent ? diffContent.substring(0, 150) + "..." : "NO DIFF/PATCH"
      );
      console.log(`Available fields:`, Object.keys(file));

      const filePath = path.join(repoPath, file.filename);

      try {
        switch (file.status) {
          case "added":
            // Create new file
            console.log(`Creating new file: ${filePath}`);
            await fs.mkdir(path.dirname(filePath), { recursive: true });
            await this.applyDiffToFile(filePath, diffContent, true);
            break;

          case "modified":
            // Modify existing file
            console.log(`Modifying existing file: ${filePath}`);
            await this.applyDiffToFile(filePath, diffContent, false);
            break;

          case "deleted":
            // Delete file
            console.log(`Deleting file: ${filePath}`);
            try {
              await fs.unlink(filePath);
            } catch (err) {
              console.warn(`File already deleted or not found: ${filePath}`);
            }
            break;

          case "renamed":
            // Handle renamed files
            console.log(
              `Renaming file: ${file.oldFilename} -> ${file.filename}`
            );
            if (file.oldFilename) {
              const oldPath = path.join(repoPath, file.oldFilename);
              try {
                await fs.rename(oldPath, filePath);
                // Apply any changes to the renamed file
                if (diffContent) {
                  await this.applyDiffToFile(filePath, diffContent, false);
                }
              } catch (err) {
                console.warn(
                  `Rename failed, treating as new file: ${err.message}`
                );
                await this.applyDiffToFile(filePath, diffContent, true);
              }
            }
            break;

          default:
            console.warn(
              `Unknown file status: ${file.status} for ${file.filename}`
            );
        }
      } catch (error) {
        console.error(`Failed to apply changes to ${file.filename}:`, error);
        throw error;
      }
    }
  }