Class PatchApplier

java.lang.Object
org.machanism.machai.gw.tools.PatchApplier

public class PatchApplier extends Object
Utility to apply unified diff patches to text files.
Since:
1.2.0
Author:
Viktor Tovstyi
  • Constructor Details

    • PatchApplier

      public PatchApplier()
  • Method Details

    • applyPatch

      public static void applyPatch(File file, List<String> patchLines, Charset charset) throws IOException
      Applies a unified or simplified diff patch to a file. Supports both standard unified diff headers (e.g. "@@ -1,5 +1,6 @@") and simplified search-and-replace headers (e.g. "@@").
      Parameters:
      file - The target file to patch.
      patchLines - List of lines from the patch file.
      charset - Charset for reading/writing the file.
      Throws:
      IOException - if file operations fail or patch cannot be applied.
    • validatePatchResult

      private static void validatePatchResult(List<String> original, List<String> modified, String fileName) throws IOException
      Asserts structural sanity on the patched code structure before writing to disk.
      Throws:
      IOException
    • findHunkStart

      private static int findHunkStart(List<String> fileLines, List<String> hunkLines, int expectedStart)
    • matchLines

      private static boolean matchLines(List<String> fileLines, List<String> expectedLines, int startOffset)
    • indexOfLine

      private static int indexOfLine(List<String> lines, String expectedLine, int startIndex)