refactor!: use IterableMixin where possible

This commit is contained in:
Aleksey Kulikov 2021-09-20 13:03:43 +03:00
parent 6845286af2
commit cf677e488a
8 changed files with 100 additions and 57 deletions

View file

@ -1,3 +1,4 @@
import 'dart:collection';
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:libgit2dart/libgit2dart.dart';
@ -10,7 +11,7 @@ import 'git_types.dart';
import 'repository.dart';
import 'util.dart';
class Index {
class Index with IterableMixin<IndexEntry> {
/// Initializes a new instance of [Index] class from provided
/// pointer to index object in memory.
///
@ -36,10 +37,7 @@ class Index {
}
/// Checks whether entry at provided [path] is in the git index or not.
bool contains(String path) => bindings.find(_indexPointer, path);
/// Returns the count of entries currently in the index.
int get count => bindings.entryCount(_indexPointer);
bool find(String path) => bindings.find(_indexPointer, path);
/// Checks if the index contains entries representing file conflicts.
bool get hasConflicts => bindings.hasConflicts(_indexPointer);
@ -232,6 +230,9 @@ class Index {
/// Releases memory allocated for index object.
void free() => bindings.free(_indexPointer);
@override
Iterator<IndexEntry> get iterator => _IndexIterator(_indexPointer);
}
class IndexEntry {
@ -307,3 +308,28 @@ class ConflictEntry {
String toString() =>
'ConflictEntry{ancestor: $ancestor, our: $our, their: $their}';
}
class _IndexIterator implements Iterator<IndexEntry> {
_IndexIterator(this._indexPointer) {
count = bindings.entryCount(_indexPointer);
}
final Pointer<git_index> _indexPointer;
IndexEntry? _currentEntry;
int _index = 0;
late final int count;
@override
IndexEntry get current => _currentEntry!;
@override
bool moveNext() {
if (_index == count) {
return false;
} else {
_currentEntry = IndexEntry(bindings.getByIndex(_indexPointer, _index));
_index++;
return true;
}
}
}

View file

@ -1,3 +1,4 @@
import 'dart:collection';
import 'dart:ffi';
import 'bindings/libgit2_bindings.dart';
import 'bindings/reflog.dart' as bindings;
@ -5,7 +6,7 @@ import 'reference.dart';
import 'signature.dart';
import 'util.dart';
class RefLog {
class RefLog with IterableMixin<RefLogEntry> {
/// Initializes a new instance of [RefLog] class from provided [Reference].
///
/// Throws a [LibGit2Error] if error occured.
@ -20,20 +21,6 @@ class RefLog {
/// Pointer to memory address for allocated reflog object.
late final Pointer<git_reflog> _reflogPointer;
/// Returns a list with entries of reference log.
List<RefLogEntry> get entries {
var log = <RefLogEntry>[];
for (var i = 0; i < count; i++) {
log.add(RefLogEntry(bindings.getByIndex(_reflogPointer, i)));
}
return log;
}
/// Returns the number of log entries in a reflog.
int get count => bindings.entryCount(_reflogPointer);
/// Lookup an entry by its index.
///
/// Requesting the reflog entry with an index of 0 (zero) will return
@ -44,6 +31,9 @@ class RefLog {
/// Releases memory allocated for reflog object.
void free() => bindings.free(_reflogPointer);
@override
Iterator<RefLogEntry> get iterator => _RefLogIterator(_reflogPointer);
}
class RefLogEntry {
@ -62,3 +52,30 @@ class RefLogEntry {
@override
String toString() => 'ReflogEntry{message: $message}';
}
class _RefLogIterator implements Iterator<RefLogEntry> {
_RefLogIterator(this._reflogPointer) {
_count = bindings.entryCount(_reflogPointer);
}
/// Pointer to memory address for allocated reflog object.
final Pointer<git_reflog> _reflogPointer;
RefLogEntry? _currentEntry;
int _index = 0;
late final int _count;
@override
RefLogEntry get current => _currentEntry!;
@override
bool moveNext() {
if (_index == _count) {
return false;
} else {
_currentEntry = RefLogEntry(bindings.getByIndex(_reflogPointer, _index));
_index++;
return true;
}
}
}

View file

@ -1,15 +1,14 @@
import 'dart:ffi';
import 'package:libgit2dart/libgit2dart.dart';
import 'bindings/libgit2_bindings.dart';
import 'bindings/tag.dart' as bindings;
import 'bindings/object.dart' as object_bindings;
import 'blob.dart';
import 'commit.dart';
import 'oid.dart';
import 'repository.dart';
import 'signature.dart';
import 'git_types.dart';
import 'tree.dart';
import 'util.dart';
class Tag {

View file

@ -49,8 +49,7 @@ class Tree {
///
/// If string [value] is provided, lookup is done by entry filename.
///
/// If provided string [value] is a path to file, lookup is done by path. In that case
/// returned object should be freed explicitly.
/// If provided string [value] is a path to file, lookup is done by path.
TreeEntry operator [](Object value) {
if (value is int) {
return TreeEntry(bindings.getByIndex(_treePointer, value));