Class Hunks
java.lang.Object
io.github.jbastgen.stemma.git.api.Hunks
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 ClassesModifier and TypeClassDescriptionstatic final recordOne added or removed line of a diff.static final recordA hunk header@@ -oldStart,oldCount +newStart,newCount @@; a missing count means one line. -
Method Summary
Modifier and TypeMethodDescriptionstatic List<Hunks.ChangeLine> changeLines(String hunksText) All added and removed lines of a diff, in order.static StringThe key of the hunk a header line describes, ornullfor any other line.static StringlineKey(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).The keys of the added and removed lines of one hunk of a diff.static List<Hunks.Hunk> The hunks ofFileDiff.hunks(), in order.static Hunks.HunkparseHeader(String line) The hunk a header line describes, ornullif the line is not a hunk header.static byte[]revertLines(byte[] newSide, byte[] oldSide, String hunksText, Set<String> keys) newSidewith the selected changes of the diffoldSide→newSidetaken back: a selected added line is removed, a selected removed line is put back fromoldSide.static byte[]stage(byte[] index, byte[] worktree, List<Hunks.Hunk> hunks, Set<String> selected) The index content after staging:indexwith the selected hunks ofunstagedDifftaken over fromworktree.static byte[]stageLines(byte[] index, byte[] worktree, String hunksText, Set<String> keys) The index content after staging single lines:indexwith 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:indexwith the selected hunks ofstagedDiffreverted to the content ofhead.
-
Method Details
-
parse
The hunks ofFileDiff.hunks(), in order. -
parseHeader
The hunk a header line describes, ornullif the line is not a hunk header. -
keyOf
The key of the hunk a header line describes, ornullfor any other line. -
stage
public static byte[] stage(byte[] index, byte[] worktree, List<Hunks.Hunk> hunks, Set<String> selected) The index content after staging:indexwith the selected hunks ofunstagedDifftaken over fromworktree.- Parameters:
hunks- the hunks of the diff index → working treeselected- 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:indexwith the selected hunks ofstagedDiffreverted to the content ofhead.- Parameters:
hunks- the hunks of the diff HEAD → indexselected- keys of the hunks to unstage
-
lineKey
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
All added and removed lines of a diff, in order. -
lineKeys
The keys of the added and removed lines of one hunk of a diff. -
stageLines
The index content after staging single lines:indexwith those changes of the diff index → working tree whose lines are selected; every other change stays out. Likegit add -pwith 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) newSidewith the selected changes of the diffoldSide→newSidetaken back: a selected added line is removed, a selected removed line is put back fromoldSide. 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
-