Class Hunks

java.lang.Object
io.github.jbastgen.stemma.git.api.Hunks

public final class Hunks extends Object
Hunks of a FileDiff and the arithmetic of staging single ones. Works on the hunk headers and on the file contents as bytes, so every encoding and line ending survives untouched.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static final record 
    One added or removed line of a diff.
    static final record 
    A hunk header @@ -oldStart,oldCount +newStart,newCount @@; a missing count means one line.
  • Method Summary

    Modifier and Type
    Method
    Description
    changeLines(String hunksText)
    All added and removed lines of a diff, in order.
    static String
    keyOf(String headerLine)
    The key of the hunk a header line describes, or null for any other line.
    static String
    lineKey(char type, int number)
    The key of an added line (+n, n = its number in the new file) or a removed line (-n, its number in the old file).
    static List<String>
    lineKeys(String hunksText, String hunkKey)
    The keys of the added and removed lines of one hunk of a diff.
    parse(String hunks)
    The hunks of FileDiff.hunks(), in order.
    static Hunks.Hunk
    The hunk a header line describes, or null if the line is not a hunk header.
    static byte[]
    revertLines(byte[] newSide, byte[] oldSide, String hunksText, Set<String> keys)
    newSide with the selected changes of the diff oldSide → newSide taken back: a selected added line is removed, a selected removed line is put back from oldSide.
    static byte[]
    stage(byte[] index, byte[] worktree, List<Hunks.Hunk> hunks, Set<String> selected)
    The index content after staging: index with the selected hunks of unstagedDiff taken over from worktree.
    static byte[]
    stageLines(byte[] index, byte[] worktree, String hunksText, Set<String> keys)
    The index content after staging single lines: index with those changes of the diff index → working tree whose lines are selected; every other change stays out.
    static byte[]
    unstage(byte[] index, byte[] head, List<Hunks.Hunk> hunks, Set<String> selected)
    The index content after unstaging: index with the selected hunks of stagedDiff reverted to the content of head.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • parse

      public static List<Hunks.Hunk> parse(String hunks)
      The hunks of FileDiff.hunks(), in order.
    • parseHeader

      public static Hunks.Hunk parseHeader(String line)
      The hunk a header line describes, or null if the line is not a hunk header.
    • keyOf

      public static String keyOf(String headerLine)
      The key of the hunk a header line describes, or null for any other line.
    • stage

      public static byte[] stage(byte[] index, byte[] worktree, List<Hunks.Hunk> hunks, Set<String> selected)
      The index content after staging: index with the selected hunks of unstagedDiff taken over from worktree.
      Parameters:
      hunks - the hunks of the diff index → working tree
      selected - keys of the hunks to stage
    • unstage

      public static byte[] unstage(byte[] index, byte[] head, List<Hunks.Hunk> hunks, Set<String> selected)
      The index content after unstaging: index with the selected hunks of stagedDiff reverted to the content of head.
      Parameters:
      hunks - the hunks of the diff HEAD → index
      selected - keys of the hunks to unstage
    • lineKey

      public static String lineKey(char type, int number)
      The key of an added line (+n, n = its number in the new file) or a removed line (-n, its number in the old file).
    • changeLines

      public static List<Hunks.ChangeLine> changeLines(String hunksText)
      All added and removed lines of a diff, in order.
    • lineKeys

      public static List<String> lineKeys(String hunksText, String hunkKey)
      The keys of the added and removed lines of one hunk of a diff.
    • stageLines

      public static byte[] stageLines(byte[] index, byte[] worktree, String hunksText, Set<String> keys)
      The index content after staging single lines: index with those changes of the diff index → working tree whose lines are selected; every other change stays out. Like git add -p with the lines edited: a removed line that is selected is removed, an added line that is selected is inserted where it is in the working tree.
      Parameters:
      hunksText - the diff index → working tree (FileDiff.hunks())
      keys - the keys of the lines to stage
      Throws:
      GitException - if a key is not in the diff, or the result would have a line without a line feed in the middle
    • revertLines

      public static byte[] revertLines(byte[] newSide, byte[] oldSide, String hunksText, Set<String> keys)
      newSide with the selected changes of the diff oldSide → newSide taken back: a selected added line is removed, a selected removed line is put back from oldSide. Unstaging lines (newSide = the index, oldSide = HEAD) and discarding lines (newSide = the working tree, oldSide = the index) are this.
      Parameters:
      keys - the keys of the lines to take back