/** * @fileoverview Patch Generator for Security Fixes * @module @nahisaho/musubix-security/remediation/patch-generator * * Generates unified diff patches for security fixes that can be * applied using standard patch tools. */ import type { Fix } from '../types/index.js'; /** * Generated patch */ export interface Patch { /** Patch ID */ id: string; /** Fix reference */ fixId: string; /** Vulnerability reference */ vulnerabilityId: string; /** Patch format */ format: PatchFormat; /** Patch content */ content: string; /** Files affected */ files: PatchFile[]; /** Patch metadata */ metadata: PatchMetadata; /** Generated timestamp */ generatedAt: Date; } /** * Patch format */ export type PatchFormat = 'unified' | 'context' | 'git' | 'json'; /** * Patch file entry */ export interface PatchFile { /** Original file path */ originalPath: string; /** Modified file path */ modifiedPath: string; /** Hunks in this file */ hunks: PatchHunk[]; /** File mode changes */ mode?: { old: string; new: string; }; } /** * Patch hunk */ export interface PatchHunk { /** Original line start */ originalStart: number; /** Original line count */ originalCount: number; /** Modified line start */ modifiedStart: number; /** Modified line count */ modifiedCount: number; /** Hunk lines */ lines: PatchLine[]; } /** * Patch line */ export interface PatchLine { /** Line type */ type: 'context' | 'addition' | 'deletion'; /** Line content */ content: string; } /** * Patch metadata */ export interface PatchMetadata { /** Author */ author?: string; /** Date */ date: Date; /** Description */ description: string; /** Vulnerability severity */ severity?: string; /** CWE references */ cwes?: string[]; /** OWASP references */ owasp?: string[]; /** Tags */ tags?: string[]; } /** * Patch generation options */ export interface PatchGenerationOptions { /** Patch format */ format?: PatchFormat; /** Context lines */ contextLines?: number; /** Include metadata header */ includeMetadata?: boolean; /** Author name */ author?: string; /** Base path for files */ basePath?: string; } /** * Patch application result */ export interface PatchApplicationResult { /** Whether application succeeded */ success: boolean; /** Files modified */ filesModified: string[]; /** Hunks applied */ hunksApplied: number; /** Hunks failed */ hunksFailed: number; /** Errors */ errors: string[]; /** Warnings */ warnings: string[]; } /** * Patch generator options */ export interface PatchGeneratorOptions { /** Default format */ defaultFormat?: PatchFormat; /** Default context lines */ defaultContextLines?: number; /** Include severity in patches */ includeSeverity?: boolean; /** Include remediation info */ includeRemediation?: boolean; } /** * Generator for security fix patches * * @example * ```typescript * const generator = createPatchGenerator(); * const patch = generator.generatePatch(fix, fileContents); * console.log(patch.content); // Unified diff * ``` */ export declare class PatchGenerator { private options; constructor(options?: PatchGeneratorOptions); /** * Generate a patch for a fix */ generatePatch(fix: Fix, fileContents: Map, options?: PatchGenerationOptions): Patch; /** * Generate patches for multiple fixes */ generateBatchPatches(fixes: Fix[], fileContents: Map, options?: PatchGenerationOptions): Patch[]; /** * Generate a combined patch for all fixes */ generateCombinedPatch(fixes: Fix[], fileContents: Map, options?: PatchGenerationOptions): Patch; /** * Parse a patch back into structured format */ parsePatch(patchContent: string, format?: PatchFormat): PatchFile[]; /** * Validate a patch can be applied */ validatePatch(patch: Patch, fileContents: Map): { valid: boolean; errors: string[]; }; /** * Apply a patch to file contents */ applyPatch(patch: Patch, fileContents: Map): PatchApplicationResult; /** * Generate reverse patch */ generateReversePatch(patch: Patch): Patch; private groupEditsByFile; private generatePatchFile; private generateHunks; private formatPatch; private formatUnifiedPatch; private formatGitPatch; private formatJsonPatch; private formatContextPatch; private parseUnifiedPatch; private parseGitPatch; private parseJsonPatch; private applyHunk; } /** * Create a patch generator */ export declare function createPatchGenerator(options?: PatchGeneratorOptions): PatchGenerator; /** * Generate a quick patch for a fix */ export declare function generateQuickPatch(fix: Fix, fileContents: Map): string; //# sourceMappingURL=patch-generator.d.ts.map