mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 12:19:09 -04:00
feat(revwalk): add ability to limit number of commits to walk (#65)
This commit is contained in:
parent
a708d54b0a
commit
48e2240c73
3 changed files with 30 additions and 4 deletions
|
@ -145,11 +145,12 @@ void pushRange({
|
|||
List<Pointer<git_commit>> walk({
|
||||
required Pointer<git_repository> repoPointer,
|
||||
required Pointer<git_revwalk> walkerPointer,
|
||||
required int limit,
|
||||
}) {
|
||||
final result = <Pointer<git_commit>>[];
|
||||
var error = 0;
|
||||
|
||||
while (error == 0) {
|
||||
void next() {
|
||||
final oid = calloc<git_oid>();
|
||||
error = libgit2.git_revwalk_next(oid, walkerPointer);
|
||||
if (error == 0) {
|
||||
|
@ -158,10 +159,21 @@ List<Pointer<git_commit>> walk({
|
|||
oidPointer: oid,
|
||||
);
|
||||
result.add(commit);
|
||||
calloc.free(oid);
|
||||
} else {
|
||||
break;
|
||||
calloc.free(oid);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (limit == 0) {
|
||||
while (error == 0) {
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
for (var i = 0; i < limit; i++) {
|
||||
next();
|
||||
}
|
||||
calloc.free(oid);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -19,11 +19,15 @@ class RevWalk {
|
|||
|
||||
/// Returns the list of commits from the revision walk.
|
||||
///
|
||||
/// [limit] is optional number of commits to walk (by default walks through
|
||||
/// all of the commits pushed onto the walker).
|
||||
///
|
||||
/// Default sorting is reverse chronological order (default in git).
|
||||
List<Commit> walk() {
|
||||
List<Commit> walk({int limit = 0}) {
|
||||
final pointers = bindings.walk(
|
||||
repoPointer: bindings.repository(_revWalkPointer),
|
||||
walkerPointer: _revWalkPointer,
|
||||
limit: limit,
|
||||
);
|
||||
|
||||
return pointers.map((e) => Commit(e)).toList();
|
||||
|
|
|
@ -50,6 +50,16 @@ void main() {
|
|||
}
|
||||
});
|
||||
|
||||
test('walks only number of commits provided with limit', () {
|
||||
final walker = RevWalk(repo);
|
||||
|
||||
walker.push(repo[log.first]);
|
||||
final commits = walker.walk(limit: 1);
|
||||
|
||||
expect(commits.length, 1);
|
||||
expect(commits[0].oid.sha, log[0]);
|
||||
});
|
||||
|
||||
test('returns list of commits with reverse sorting', () {
|
||||
final walker = RevWalk(repo);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue