mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
refactor(patch): initialize DiffHunk with all the information accessible immediately
This commit is contained in:
parent
28f08e308a
commit
77a34c3335
4 changed files with 167 additions and 149 deletions
|
@ -1,5 +1,6 @@
|
|||
import 'dart:ffi';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
import 'package:libgit2dart/libgit2dart.dart';
|
||||
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||
import 'package:libgit2dart/src/bindings/patch.dart' as bindings;
|
||||
|
@ -197,14 +198,49 @@ class Patch {
|
|||
final length = bindings.numHunks(_patchPointer);
|
||||
final hunks = <DiffHunk>[];
|
||||
|
||||
for (var i = 0; i < length; i++) {
|
||||
final hunk = bindings.hunk(patchPointer: _patchPointer, hunkIndex: i);
|
||||
for (var index = 0; index < length; index++) {
|
||||
final hunk = bindings.hunk(patchPointer: _patchPointer, hunkIndex: index);
|
||||
final hunkPointer = hunk['hunk']! as Pointer<git_diff_hunk>;
|
||||
final linesCount = hunk['linesN']! as int;
|
||||
|
||||
final lines = <DiffLine>[];
|
||||
for (var i = 0; i < linesCount; i++) {
|
||||
final linePointer = bindings.lines(
|
||||
patchPointer: _patchPointer,
|
||||
hunkIndex: index,
|
||||
lineOfHunk: i,
|
||||
);
|
||||
lines.add(
|
||||
DiffLine._(
|
||||
origin: GitDiffLine.values.singleWhere(
|
||||
(e) => linePointer.ref.origin == e.value,
|
||||
),
|
||||
oldLineNumber: linePointer.ref.old_lineno,
|
||||
newLineNumber: linePointer.ref.new_lineno,
|
||||
numLines: linePointer.ref.num_lines,
|
||||
contentOffset: linePointer.ref.content_offset,
|
||||
content: linePointer.ref.content
|
||||
.cast<Utf8>()
|
||||
.toDartString(length: linePointer.ref.content_len),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
final intHeader = <int>[];
|
||||
for (var i = 0; i < hunkPointer.ref.header_len; i++) {
|
||||
intHeader.add(hunkPointer.ref.header[i]);
|
||||
}
|
||||
|
||||
hunks.add(
|
||||
DiffHunk(
|
||||
_patchPointer,
|
||||
hunk['hunk']! as Pointer<git_diff_hunk>,
|
||||
hunk['linesN']! as int,
|
||||
i,
|
||||
DiffHunk._(
|
||||
linesCount: linesCount,
|
||||
index: index,
|
||||
oldStart: hunkPointer.ref.old_start,
|
||||
oldLines: hunkPointer.ref.old_lines,
|
||||
newStart: hunkPointer.ref.new_start,
|
||||
newLines: hunkPointer.ref.new_lines,
|
||||
header: String.fromCharCodes(intHeader),
|
||||
lines: lines,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -251,3 +287,83 @@ class PatchStats {
|
|||
'deletions: $deletions}';
|
||||
}
|
||||
}
|
||||
|
||||
class DiffHunk {
|
||||
const DiffHunk._({
|
||||
required this.linesCount,
|
||||
required this.index,
|
||||
required this.oldStart,
|
||||
required this.oldLines,
|
||||
required this.newStart,
|
||||
required this.newLines,
|
||||
required this.header,
|
||||
required this.lines,
|
||||
});
|
||||
|
||||
/// Number of total lines in this hunk.
|
||||
final int linesCount;
|
||||
|
||||
/// Index of this hunk in the patch.
|
||||
final int index;
|
||||
|
||||
/// Starting line number in 'old file'.
|
||||
final int oldStart;
|
||||
|
||||
/// Number of lines in 'old file'.
|
||||
final int oldLines;
|
||||
|
||||
/// Starting line number in 'new file'.
|
||||
final int newStart;
|
||||
|
||||
/// Number of lines in 'new file'.
|
||||
final int newLines;
|
||||
|
||||
/// Header of a hunk.
|
||||
final String header;
|
||||
|
||||
/// List of lines in a hunk of a patch.
|
||||
final List<DiffLine> lines;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'DiffHunk{linesCount: $linesCount, index: $index, '
|
||||
'oldStart: $oldStart, oldLines: $oldLines, newStart: $newStart, '
|
||||
'newLines: $newLines, header: $header}';
|
||||
}
|
||||
}
|
||||
|
||||
class DiffLine {
|
||||
const DiffLine._({
|
||||
required this.origin,
|
||||
required this.oldLineNumber,
|
||||
required this.newLineNumber,
|
||||
required this.numLines,
|
||||
required this.contentOffset,
|
||||
required this.content,
|
||||
});
|
||||
|
||||
/// Type of the line.
|
||||
final GitDiffLine origin;
|
||||
|
||||
/// Line number in old file or -1 for added line.
|
||||
final int oldLineNumber;
|
||||
|
||||
/// Line number in new file or -1 for deleted line.
|
||||
final int newLineNumber;
|
||||
|
||||
/// Number of newline characters in content.
|
||||
final int numLines;
|
||||
|
||||
/// Offset in the original file to the content.
|
||||
final int contentOffset;
|
||||
|
||||
/// Content of the diff line.
|
||||
final String content;
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'DiffLine{oldLineNumber: $oldLineNumber, '
|
||||
'newLineNumber: $newLineNumber, numLines: $numLines, '
|
||||
'contentOffset: $contentOffset, content: $content}';
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue