style: use "map/where" instead of "for" loop

This commit is contained in:
Aleksey Kulikov 2021-10-12 14:56:56 +03:00
parent 3a0fa75929
commit cfa5268af2
28 changed files with 193 additions and 323 deletions

View file

@ -19,15 +19,18 @@ void head({
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
final initOpts = final initOpts = initOptions(
initOptions(strategy: strategy, directory: directory, paths: paths); strategy: strategy,
directory: directory,
paths: paths,
);
final optsC = initOpts[0]; final optsC = initOpts[0];
final pathPointers = initOpts[1]; final pathPointers = initOpts[1];
final strArray = initOpts[2]; final strArray = initOpts[2];
final error = libgit2.git_checkout_head(repoPointer, optsC); final error = libgit2.git_checkout_head(repoPointer, optsC);
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
@ -48,15 +51,18 @@ void index({
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
final initOpts = final initOpts = initOptions(
initOptions(strategy: strategy, directory: directory, paths: paths); strategy: strategy,
directory: directory,
paths: paths,
);
final optsC = initOpts[0]; final optsC = initOpts[0];
final pathPointers = initOpts[1]; final pathPointers = initOpts[1];
final strArray = initOpts[2]; final strArray = initOpts[2];
final error = libgit2.git_checkout_index(repoPointer, nullptr, optsC); final error = libgit2.git_checkout_index(repoPointer, nullptr, optsC);
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
@ -90,7 +96,7 @@ void tree({
final error = libgit2.git_checkout_tree(repoPointer, treeishPointer, optsC); final error = libgit2.git_checkout_tree(repoPointer, treeishPointer, optsC);
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
@ -128,9 +134,5 @@ List<dynamic> initOptions({
optsC.ref.paths.count = paths.length; optsC.ref.paths.count = paths.length;
} }
var result = <dynamic>[]; return [optsC, pathPointers, strArray];
result.add(optsC);
result.add(pathPointers);
result.add(strArray);
return result;
} }

View file

@ -230,7 +230,7 @@ void addAll({
); );
calloc.free(pathspecC); calloc.free(pathspecC);
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);
@ -296,7 +296,7 @@ void removeAll({
); );
calloc.free(pathspecC); calloc.free(pathspecC);
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);

View file

@ -66,14 +66,13 @@ void merge({
required Pointer<Pointer<git_annotated_commit>> theirHeadsPointer, required Pointer<Pointer<git_annotated_commit>> theirHeadsPointer,
required int theirHeadsLen, required int theirHeadsLen,
}) { }) {
final mergeOpts = calloc<git_merge_options>(sizeOf<git_merge_options>()); final mergeOpts = calloc<git_merge_options>();
libgit2.git_merge_options_init(mergeOpts, GIT_MERGE_OPTIONS_VERSION); libgit2.git_merge_options_init(mergeOpts, GIT_MERGE_OPTIONS_VERSION);
final checkoutOpts = final checkoutOpts = calloc<git_checkout_options>();
calloc<git_checkout_options>(sizeOf<git_checkout_options>());
libgit2.git_checkout_options_init(checkoutOpts, GIT_CHECKOUT_OPTIONS_VERSION); libgit2.git_checkout_options_init(checkoutOpts, GIT_CHECKOUT_OPTIONS_VERSION);
checkoutOpts.ref.checkout_strategy = checkoutOpts.ref.checkout_strategy =
git_checkout_strategy_t.GIT_CHECKOUT_SAFE + git_checkout_strategy_t.GIT_CHECKOUT_SAFE |
git_checkout_strategy_t.GIT_CHECKOUT_RECREATE_MISSING; git_checkout_strategy_t.GIT_CHECKOUT_RECREATE_MISSING;
final error = libgit2.git_merge( final error = libgit2.git_merge(
@ -131,24 +130,26 @@ Pointer<git_index> mergeCommits({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required Pointer<git_commit> ourCommitPointer, required Pointer<git_commit> ourCommitPointer,
required Pointer<git_commit> theirCommitPointer, required Pointer<git_commit> theirCommitPointer,
required Map<String, int> opts, required int favor,
required int mergeFlags,
required int fileFlags,
}) { }) {
final out = calloc<Pointer<git_index>>(); final out = calloc<Pointer<git_index>>();
final optsC = calloc<git_merge_options>(sizeOf<git_merge_options>()); final opts = _initMergeOptions(
optsC.ref.file_favor = opts['favor']!; favor: favor,
optsC.ref.flags = opts['mergeFlags']!; mergeFlags: mergeFlags,
optsC.ref.file_flags = opts['fileFlags']!; fileFlags: fileFlags,
optsC.ref.version = GIT_MERGE_OPTIONS_VERSION; );
final error = libgit2.git_merge_commits( final error = libgit2.git_merge_commits(
out, out,
repoPointer, repoPointer,
ourCommitPointer, ourCommitPointer,
theirCommitPointer, theirCommitPointer,
optsC, opts,
); );
calloc.free(optsC); calloc.free(opts);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
@ -170,14 +171,16 @@ Pointer<git_index> mergeTrees({
required Pointer<git_tree> ancestorTreePointer, required Pointer<git_tree> ancestorTreePointer,
required Pointer<git_tree> ourTreePointer, required Pointer<git_tree> ourTreePointer,
required Pointer<git_tree> theirTreePointer, required Pointer<git_tree> theirTreePointer,
required Map<String, int> opts, required int favor,
required int mergeFlags,
required int fileFlags,
}) { }) {
final out = calloc<Pointer<git_index>>(); final out = calloc<Pointer<git_index>>();
final optsC = calloc<git_merge_options>(sizeOf<git_merge_options>()); final opts = _initMergeOptions(
optsC.ref.file_favor = opts['favor']!; favor: favor,
optsC.ref.flags = opts['mergeFlags']!; mergeFlags: mergeFlags,
optsC.ref.file_flags = opts['fileFlags']!; fileFlags: fileFlags,
optsC.ref.version = GIT_MERGE_OPTIONS_VERSION; );
final error = libgit2.git_merge_trees( final error = libgit2.git_merge_trees(
out, out,
@ -185,10 +188,10 @@ Pointer<git_index> mergeTrees({
ancestorTreePointer, ancestorTreePointer,
ourTreePointer, ourTreePointer,
theirTreePointer, theirTreePointer,
optsC, opts,
); );
calloc.free(optsC); calloc.free(opts);
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
@ -215,3 +218,25 @@ void cherryPick({
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
} }
Pointer<git_merge_options> _initMergeOptions({
required int favor,
required int mergeFlags,
required int fileFlags,
}) {
final opts = calloc<git_merge_options>();
final error = libgit2.git_merge_options_init(
opts,
GIT_MERGE_OPTIONS_VERSION,
);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
}
opts.ref.file_favor = favor;
opts.ref.flags = mergeFlags;
opts.ref.file_flags = fileFlags;
return opts;
}

View file

@ -17,9 +17,8 @@ List<String> list(Pointer<git_repository> repo) {
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
final count = out.ref.count;
var result = <String>[]; var result = <String>[];
for (var i = 0; i < count; i++) { for (var i = 0; i < out.ref.count; i++) {
result.add(out.ref.strings[i].cast<Utf8>().toDartString()); result.add(out.ref.strings[i].cast<Utf8>().toDartString());
} }
calloc.free(out); calloc.free(out);
@ -151,9 +150,8 @@ List<String> rename({
if (error < 0) { if (error < 0) {
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} else { } else {
final count = out.ref.count;
var result = <String>[]; var result = <String>[];
for (var i = 0; i < count; i++) { for (var i = 0; i < out.ref.count; i++) {
result.add(out.ref.strings[i].cast<Utf8>().toDartString()); result.add(out.ref.strings[i].cast<Utf8>().toDartString());
} }
calloc.free(out); calloc.free(out);
@ -249,8 +247,7 @@ List<String> fetchRefspecs(Pointer<git_remote> remote) {
libgit2.git_remote_get_fetch_refspecs(out, remote); libgit2.git_remote_get_fetch_refspecs(out, remote);
var result = <String>[]; var result = <String>[];
final count = out.ref.count; for (var i = 0; i < out.ref.count; i++) {
for (var i = 0; i < count; i++) {
result.add(out.ref.strings[i].cast<Utf8>().toDartString()); result.add(out.ref.strings[i].cast<Utf8>().toDartString());
} }
calloc.free(out); calloc.free(out);
@ -263,8 +260,7 @@ List<String> pushRefspecs(Pointer<git_remote> remote) {
libgit2.git_remote_get_push_refspecs(out, remote); libgit2.git_remote_get_push_refspecs(out, remote);
var result = <String>[]; var result = <String>[];
final count = out.ref.count; for (var i = 0; i < out.ref.count; i++) {
for (var i = 0; i < count; i++) {
result.add(out.ref.strings[i].cast<Utf8>().toDartString()); result.add(out.ref.strings[i].cast<Utf8>().toDartString());
} }
calloc.free(out); calloc.free(out);
@ -462,7 +458,7 @@ void fetch({
reflogMessageC, reflogMessageC,
); );
for (var p in refspecsPointers) { for (final p in refspecsPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);
@ -516,7 +512,7 @@ void push({
final error = libgit2.git_remote_push(remotePointer, refspecsC, opts); final error = libgit2.git_remote_push(remotePointer, refspecsC, opts);
for (var p in refspecsPointers) { for (final p in refspecsPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);

View file

@ -71,10 +71,9 @@ void apply({
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);
calloc.free(optsC); calloc.free(optsC);
calloc.free(options); calloc.free(options);
@ -125,10 +124,9 @@ void pop({
throw LibGit2Error(libgit2.git_error_last()); throw LibGit2Error(libgit2.git_error_last());
} }
for (var p in pathPointers) { for (final p in pathPointers) {
calloc.free(p); calloc.free(p);
} }
calloc.free(strArray); calloc.free(strArray);
calloc.free(optsC); calloc.free(optsC);
calloc.free(options); calloc.free(options);

View file

@ -46,8 +46,7 @@ class Blame with IterableMixin<BlameHunk> {
}) { }) {
libgit2.git_libgit2_init(); libgit2.git_libgit2_init();
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
_blamePointer = bindings.file( _blamePointer = bindings.file(
repoPointer: repo.pointer, repoPointer: repo.pointer,

View file

@ -87,8 +87,7 @@ class Blob {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
final result = patch_bindings.fromBlobs( final result = patch_bindings.fromBlobs(
oldBlobPointer: _blobPointer, oldBlobPointer: _blobPointer,
@ -116,8 +115,7 @@ class Blob {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
final result = patch_bindings.fromBlobAndBuffer( final result = patch_bindings.fromBlobAndBuffer(
oldBlobPointer: _blobPointer, oldBlobPointer: _blobPointer,

View file

@ -84,12 +84,7 @@ class Branch {
flags: type.value, flags: type.value,
); );
final result = <Branch>[]; return pointers.map((e) => Branch(e)).toList();
for (var pointer in pointers) {
result.add(Branch(pointer));
}
return result;
} }
/// Deletes an existing branch reference. /// Deletes an existing branch reference.

View file

@ -196,14 +196,9 @@ class ConfigEntry {
/// Returns which config file this was found in. /// Returns which config file this was found in.
GitConfigLevel get level { GitConfigLevel get level {
late GitConfigLevel result; return GitConfigLevel.values.singleWhere(
for (var level in GitConfigLevel.values) { (e) => _configEntryPointer.ref.level == e.value,
if (_configEntryPointer.ref.level == level.value) { );
result = level;
break;
}
}
return result;
} }
/// Releases memory allocated for config entry object. /// Releases memory allocated for config entry object.

View file

@ -96,8 +96,7 @@ class Diff {
int breakRewriteThreshold = 60, int breakRewriteThreshold = 60,
int renameLimit = 200, int renameLimit = 200,
}) { }) {
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
bindings.findSimilar( bindings.findSimilar(
diffPointer: _diffPointer, diffPointer: _diffPointer,
@ -136,14 +135,9 @@ class DiffDelta {
/// Returns type of change. /// Returns type of change.
GitDelta get status { GitDelta get status {
late final GitDelta status; return GitDelta.values.singleWhere(
for (var type in GitDelta.values) { (e) => _diffDeltaPointer.ref.status == e.value,
if (_diffDeltaPointer.ref.status == type.value) { );
status = type;
break;
}
}
return status;
} }
/// Looks up the single character abbreviation for a delta status code. /// Looks up the single character abbreviation for a delta status code.
@ -156,14 +150,9 @@ class DiffDelta {
/// Returns flags for the delta object. /// Returns flags for the delta object.
Set<GitDiffFlag> get flags { Set<GitDiffFlag> get flags {
var flags = <GitDiffFlag>{}; return GitDiffFlag.values
for (var flag in GitDiffFlag.values) { .where((e) => _diffDeltaPointer.ref.flags & e.value == e.value)
if (_diffDeltaPointer.ref.flags & flag.value == flag.value) { .toSet();
flags.add(flag);
}
}
return flags;
} }
/// Returns a similarity score for renamed or copied files between 0 and 100 /// Returns a similarity score for renamed or copied files between 0 and 100
@ -206,26 +195,14 @@ class DiffFile {
/// Returns flags for the diff file object. /// Returns flags for the diff file object.
Set<GitDiffFlag> get flags { Set<GitDiffFlag> get flags {
var flags = <GitDiffFlag>{}; return GitDiffFlag.values
for (var flag in GitDiffFlag.values) { .where((e) => _diffFile.flags & e.value == e.value)
if (_diffFile.flags & flag.value == flag.value) { .toSet();
flags.add(flag);
}
}
return flags;
} }
/// Returns one of the [GitFilemode] values. /// Returns one of the [GitFilemode] values.
GitFilemode get mode { GitFilemode get mode {
late final GitFilemode result; return GitFilemode.values.singleWhere((e) => _diffFile.mode == e.value);
for (var mode in GitFilemode.values) {
if (_diffFile.mode == mode.value) {
result = mode;
break;
}
}
return result;
} }
} }
@ -252,8 +229,7 @@ class DiffStats {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
String print({required Set<GitDiffStats> format, required int width}) { String print({required Set<GitDiffStats> format, required int width}) {
final int formatInt = final int formatInt = format.fold(0, (acc, e) => acc | e.value);
format.fold(0, (previousValue, e) => previousValue | e.value);
return bindings.statsPrint( return bindings.statsPrint(
statsPointer: _diffStatsPointer, statsPointer: _diffStatsPointer,
@ -335,15 +311,9 @@ class DiffLine {
/// Returns type of the line. /// Returns type of the line.
GitDiffLine get origin { GitDiffLine get origin {
final originInt = _diffLinePointer.ref.origin; return GitDiffLine.values.singleWhere(
late final GitDiffLine result; (e) => _diffLinePointer.ref.origin == e.value,
for (var type in GitDiffLine.values) { );
if (originInt == type.value) {
result = type;
break;
}
}
return result;
} }
/// Returns line number in old file or -1 for added line. /// Returns line number in old file or -1 for added line.

View file

@ -3,16 +3,10 @@ import 'util.dart';
class Features { class Features {
/// Returns list of compile time options for libgit2. /// Returns list of compile time options for libgit2.
static List<GitFeature> get list { static Set<GitFeature> get list {
var result = <GitFeature>[];
final featuresInt = libgit2.git_libgit2_features(); final featuresInt = libgit2.git_libgit2_features();
return GitFeature.values
for (var feature in GitFeature.values) { .where((e) => featuresInt & e.value == e.value)
if (featuresInt & feature.value == feature.value) { .toSet();
result.add(feature);
}
}
return result;
} }
} }

View file

@ -55,7 +55,7 @@ class Index with IterableMixin<IndexEntry> {
final conflicts = bindings.conflictList(_indexPointer); final conflicts = bindings.conflictList(_indexPointer);
var result = <String, ConflictEntry>{}; var result = <String, ConflictEntry>{};
for (var entry in conflicts) { for (final entry in conflicts) {
IndexEntry? ancestor, our, their; IndexEntry? ancestor, our, their;
String path; String path;
@ -190,8 +190,7 @@ class Index with IterableMixin<IndexEntry> {
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final repo = bindings.owner(_indexPointer); final repo = bindings.owner(_indexPointer);
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
return Diff(diff_bindings.indexToWorkdir( return Diff(diff_bindings.indexToWorkdir(
repoPointer: repo, repoPointer: repo,
@ -212,8 +211,7 @@ class Index with IterableMixin<IndexEntry> {
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final repo = bindings.owner(_indexPointer); final repo = bindings.owner(_indexPointer);
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
return Diff(diff_bindings.treeToIndex( return Diff(diff_bindings.treeToIndex(
repoPointer: repo, repoPointer: repo,

View file

@ -82,15 +82,12 @@ class Note {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
static List<Note> list(Repository repo) { static List<Note> list(Repository repo) {
final notesPointers = bindings.list(repo.pointer); final notesPointers = bindings.list(repo.pointer);
var result = <Note>[]; return notesPointers
for (var note in notesPointers) { .map((e) => Note(
result.add(Note( e['note'] as Pointer<git_note>,
note['note'] as Pointer<git_note>, e['annotatedId'] as Pointer<git_oid>,
note['annotatedId'] as Pointer<git_oid>, ))
)); .toList();
}
return result;
} }
/// Returns the note object's [Oid]. /// Returns the note object's [Oid].

View file

@ -109,15 +109,8 @@ class OdbObject {
/// Returns the type of an ODB object. /// Returns the type of an ODB object.
GitObject get type { GitObject get type {
late GitObject result;
final typeInt = bindings.objectType(_odbObjectPointer); final typeInt = bindings.objectType(_odbObjectPointer);
for (var type in GitObject.values) { return GitObject.values.singleWhere((e) => typeInt == e.value);
if (typeInt == type.value) {
result = type;
break;
}
}
return result;
} }
/// Returns the data of an ODB object. /// Returns the data of an ODB object.

View file

@ -31,8 +31,7 @@ class Patch {
}) { }) {
libgit2.git_libgit2_init(); libgit2.git_libgit2_init();
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
var result = <String, dynamic>{}; var result = <String, dynamic>{};
if (a is Blob || a == null) { if (a is Blob || a == null) {

View file

@ -121,14 +121,9 @@ class RebaseOperation {
/// Returns the type of rebase operation. /// Returns the type of rebase operation.
GitRebaseOperation get type { GitRebaseOperation get type {
late final GitRebaseOperation result; return GitRebaseOperation.values.singleWhere(
for (var operation in GitRebaseOperation.values) { (e) => _rebaseOperationPointer.ref.type == e.value,
if (_rebaseOperationPointer.ref.type == operation.value) { );
result = operation;
break;
}
}
return result;
} }
/// The commit ID being cherry-picked. This will be populated for /// The commit ID being cherry-picked. This will be populated for

View file

@ -42,7 +42,7 @@ class Repository {
}) { }) {
libgit2.git_libgit2_init(); libgit2.git_libgit2_init();
int flagsInt = flags.fold(0, (previousValue, e) => previousValue | e.value); int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
if (bare) { if (bare) {
flagsInt |= GitRepositoryInit.bare.value; flagsInt |= GitRepositoryInit.bare.value;
@ -717,15 +717,12 @@ class Repository {
.toDartString(); .toDartString();
} }
var statuses = <GitStatus>{};
// Skipping GitStatus.current because entry that is in the list can't be without changes // Skipping GitStatus.current because entry that is in the list can't be without changes
// but `&` on `0` value falsly adds it to the set of flags // but `&` on `0` value falsly adds it to the set of flags
for (var status in GitStatus.values.skip(1)) { result[path] = GitStatus.values
if (entry.ref.status & status.value == status.value) { .skip(1)
statuses.add(status); .where((e) => entry.ref.status & e.value == e.value)
} .toSet();
}
result[path] = statuses;
} }
status_bindings.listFree(list); status_bindings.listFree(list);
@ -747,20 +744,16 @@ class Repository {
path: path, path: path,
); );
var statuses = <GitStatus>{};
if (statusInt == GitStatus.current.value) { if (statusInt == GitStatus.current.value) {
statuses.add(GitStatus.current); return {GitStatus.current};
} else { } else {
// Skipping GitStatus.current because `&` on `0` value falsly adds it to the set of flags // Skipping GitStatus.current because `&` on `0` value falsly adds it to the set of flags
for (var status in GitStatus.values.skip(1)) { return GitStatus.values
if (statusInt & status.value == status.value) { .skip(1)
statuses.add(status); .where((e) => statusInt & e.value == e.value)
.toSet();
} }
} }
}
return statuses;
}
/// Finds a merge base between two commits. /// Finds a merge base between two commits.
/// ///
@ -782,7 +775,7 @@ class Repository {
/// respectively. /// respectively.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
List<Set<dynamic>> mergeAnalysis({ List mergeAnalysis({
required Oid theirHead, required Oid theirHead,
String ourRef = 'HEAD', String ourRef = 'HEAD',
}) { }) {
@ -791,29 +784,24 @@ class Repository {
repoPointer: _repoPointer, repoPointer: _repoPointer,
oidPointer: theirHead.pointer, oidPointer: theirHead.pointer,
); );
var result = <Set<dynamic>>[];
var analysisSet = <GitMergeAnalysis>{};
final analysisInt = merge_bindings.analysis( final analysisInt = merge_bindings.analysis(
repoPointer: _repoPointer, repoPointer: _repoPointer,
ourRefPointer: ref.pointer, ourRefPointer: ref.pointer,
theirHeadPointer: head, theirHeadPointer: head,
theirHeadsLen: 1, theirHeadsLen: 1,
); );
for (var analysis in GitMergeAnalysis.values) {
if (analysisInt[0] & analysis.value == analysis.value) { final analysisSet = GitMergeAnalysis.values
analysisSet.add(analysis); .where((e) => analysisInt[0] & e.value == e.value)
} .toSet();
} final mergePreference = GitMergePreference.values.singleWhere(
result.add(analysisSet); (e) => analysisInt[1] == e.value,
result.add(
{GitMergePreference.values.singleWhere((e) => analysisInt[1] == e.value)},
); );
commit_bindings.annotatedFree(head.value); commit_bindings.annotatedFree(head.value);
ref.free(); ref.free();
return result; return [analysisSet, mergePreference];
} }
/// Merges the given commit(s) oid into HEAD, writing the results into the working directory. /// Merges the given commit(s) oid into HEAD, writing the results into the working directory.
@ -870,21 +858,14 @@ class Repository {
Set<GitMergeFlag> mergeFlags = const {GitMergeFlag.findRenames}, Set<GitMergeFlag> mergeFlags = const {GitMergeFlag.findRenames},
Set<GitMergeFileFlag> fileFlags = const {GitMergeFileFlag.defaults}, Set<GitMergeFileFlag> fileFlags = const {GitMergeFileFlag.defaults},
}) { }) {
var opts = <String, int>{}; return Index(merge_bindings.mergeCommits(
opts['favor'] = favor.value;
opts['mergeFlags'] =
mergeFlags.fold(0, (previousValue, e) => previousValue | e.value);
opts['fileFlags'] =
fileFlags.fold(0, (previousValue, e) => previousValue | e.value);
final result = merge_bindings.mergeCommits(
repoPointer: _repoPointer, repoPointer: _repoPointer,
ourCommitPointer: ourCommit.pointer, ourCommitPointer: ourCommit.pointer,
theirCommitPointer: theirCommit.pointer, theirCommitPointer: theirCommit.pointer,
opts: opts, favor: favor.value,
); mergeFlags: mergeFlags.fold(0, (acc, e) => acc | e.value),
fileFlags: fileFlags.fold(0, (acc, e) => acc | e.value),
return Index(result); ));
} }
/// Reverts the given commit against the given "our" commit, producing an index that /// Reverts the given commit against the given "our" commit, producing an index that
@ -924,26 +905,15 @@ class Repository {
List<GitMergeFlag> mergeFlags = const [GitMergeFlag.findRenames], List<GitMergeFlag> mergeFlags = const [GitMergeFlag.findRenames],
List<GitMergeFileFlag> fileFlags = const [GitMergeFileFlag.defaults], List<GitMergeFileFlag> fileFlags = const [GitMergeFileFlag.defaults],
}) { }) {
var opts = <String, int>{}; return Index(merge_bindings.mergeTrees(
opts['favor'] = favor.value;
opts['mergeFlags'] = mergeFlags.fold(
0,
(previousValue, element) => previousValue + element.value,
);
opts['fileFlags'] = fileFlags.fold(
0,
(previousValue, element) => previousValue + element.value,
);
final result = merge_bindings.mergeTrees(
repoPointer: _repoPointer, repoPointer: _repoPointer,
ancestorTreePointer: ancestorTree.pointer, ancestorTreePointer: ancestorTree.pointer,
ourTreePointer: ourTree.pointer, ourTreePointer: ourTree.pointer,
theirTreePointer: theirTree.pointer, theirTreePointer: theirTree.pointer,
opts: opts, favor: favor.value,
); mergeFlags: mergeFlags.fold(0, (acc, element) => acc | element.value),
fileFlags: fileFlags.fold(0, (acc, element) => acc | element.value),
return Index(result); ));
} }
/// Cherry-picks the provided commit, producing changes in the index and working directory. /// Cherry-picks the provided commit, producing changes in the index and working directory.
@ -980,8 +950,7 @@ class Repository {
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
final int strat = final int strat = strategy.fold(0, (acc, e) => acc | e.value);
strategy.fold(0, (previousValue, e) => previousValue | e.value);
if (refName == null) { if (refName == null) {
checkout_bindings.index( checkout_bindings.index(
@ -1057,8 +1026,7 @@ class Repository {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final int flagsInt = final int flagsInt = flags.fold(0, (acc, e) => acc | e.value);
flags.fold(0, (previousValue, e) => previousValue | e.value);
if (a is Tree && b is Tree) { if (a is Tree && b is Tree) {
return Diff(diff_bindings.treeToTree( return Diff(diff_bindings.treeToTree(
@ -1173,15 +1141,11 @@ class Repository {
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
int flags = reinstateIndex ? GitStashApply.reinstateIndex.value : 0;
final int strat =
strategy.fold(0, (previousValue, e) => previousValue | e.value);
stash_bindings.apply( stash_bindings.apply(
repoPointer: _repoPointer, repoPointer: _repoPointer,
index: index, index: index,
flags: flags, flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0,
strategy: strat, strategy: strategy.fold(0, (acc, e) => acc | e.value),
directory: directory, directory: directory,
paths: paths, paths: paths,
); );
@ -1210,15 +1174,11 @@ class Repository {
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
int flags = reinstateIndex ? GitStashApply.reinstateIndex.value : 0;
final int strat =
strategy.fold(0, (previousValue, e) => previousValue | e.value);
stash_bindings.pop( stash_bindings.pop(
repoPointer: _repoPointer, repoPointer: _repoPointer,
index: index, index: index,
flags: flags, flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0,
strategy: strat, strategy: strategy.fold(0, (acc, e) => acc | e.value),
directory: directory, directory: directory,
paths: paths, paths: paths,
); );
@ -1292,12 +1252,9 @@ class Repository {
required String name, required String name,
Set<GitAttributeCheck> flags = const {GitAttributeCheck.fileThenIndex}, Set<GitAttributeCheck> flags = const {GitAttributeCheck.fileThenIndex},
}) { }) {
final int flagsInt =
flags.fold(0, (previousValue, e) => previousValue | e.value);
return attr_bindings.getAttribute( return attr_bindings.getAttribute(
repoPointer: _repoPointer, repoPointer: _repoPointer,
flags: flagsInt, flags: flags.fold(0, (acc, e) => acc | e.value),
path: path, path: path,
name: name, name: name,
); );
@ -1532,10 +1489,13 @@ class Repository {
/// let libgit2 to autodetect number of CPUs. /// let libgit2 to autodetect number of CPUs.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
int pack( int pack({
{String? path, void Function(PackBuilder)? packDelegate, int? threads}) { String? path,
void Function(PackBuilder)? packDelegate,
int? threads,
}) {
void packAll(PackBuilder packbuilder) { void packAll(PackBuilder packbuilder) {
for (var object in odb.objects) { for (final object in odb.objects) {
packbuilder.add(object); packbuilder.add(object);
} }
} }

View file

@ -78,22 +78,15 @@ class RevSpec {
/// The right element of the revspec; must be freed by the user. /// The right element of the revspec; must be freed by the user.
Commit? get to { Commit? get to {
if (_revSpecPointer.ref.to == nullptr) { return _revSpecPointer.ref.to == nullptr
return null; ? null
} else { : Commit(_revSpecPointer.ref.to.cast());
return Commit(_revSpecPointer.ref.to.cast());
}
} }
/// The intent of the revspec. /// The intent of the revspec.
Set<GitRevSpec> get flags { Set<GitRevSpec> get flags {
final flagInt = _revSpecPointer.ref.flags; return GitRevSpec.values
var flags = <GitRevSpec>{}; .where((e) => _revSpecPointer.ref.flags & e.value == e.value)
for (var flag in GitRevSpec.values) { .toSet();
if (flagInt & flag.value == flag.value) {
flags.add(flag);
}
}
return flags;
} }
} }

View file

@ -18,17 +18,13 @@ class RevWalk {
/// Default sorting is reverse chronological order (default in git). /// Default sorting is reverse chronological order (default in git).
List<Commit> walk() { List<Commit> walk() {
final repoPointer = bindings.repository(_revWalkPointer); final repoPointer = bindings.repository(_revWalkPointer);
var result = <Commit>[];
final commits = bindings.walk( final pointers = bindings.walk(
repoPointer: repoPointer, repoPointer: repoPointer,
walkerPointer: _revWalkPointer, walkerPointer: _revWalkPointer,
); );
for (var commit in commits) {
result.add(Commit(commit));
}
return result; return pointers.map((e) => Commit(e)).toList();
} }
/// Changes the sorting mode when iterating through the repository's contents. /// Changes the sorting mode when iterating through the repository's contents.
@ -37,11 +33,10 @@ class RevWalk {
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void sorting(Set<GitSort> sorting) { void sorting(Set<GitSort> sorting) {
final int sort = sorting.fold( bindings.sorting(
0, walkerPointer: _revWalkPointer,
(previousValue, e) => previousValue | e.value, sortMode: sorting.fold(0, (acc, e) => acc | e.value),
); );
bindings.sorting(walkerPointer: _revWalkPointer, sortMode: sort);
} }
/// Adds a new root for the traversal. /// Adds a new root for the traversal.

View file

@ -141,20 +141,16 @@ class Submodule {
GitSubmoduleIgnore ignore = GitSubmoduleIgnore.unspecified, GitSubmoduleIgnore ignore = GitSubmoduleIgnore.unspecified,
}) { }) {
final repo = bindings.owner(_submodulePointer); final repo = bindings.owner(_submodulePointer);
final resultInt = bindings.status( final resultInt = bindings.status(
repoPointer: repo, repoPointer: repo,
name: name, name: name,
ignore: ignore.value, ignore: ignore.value,
); );
var result = <GitSubmoduleStatus>{}; return GitSubmoduleStatus.values
for (var status in GitSubmoduleStatus.values) { .where((e) => resultInt & e.value == e.value)
if (resultInt & status.value == status.value) { .toSet();
result.add(status);
}
}
return result;
} }
/// Copies submodule remote info into submodule repo. /// Copies submodule remote info into submodule repo.
@ -242,17 +238,8 @@ class Submodule {
/// Returns the ignore rule that will be used for the submodule. /// Returns the ignore rule that will be used for the submodule.
GitSubmoduleIgnore get ignore { GitSubmoduleIgnore get ignore {
late GitSubmoduleIgnore result;
final ruleInt = bindings.ignore(_submodulePointer); final ruleInt = bindings.ignore(_submodulePointer);
return GitSubmoduleIgnore.values.singleWhere((e) => ruleInt == e.value);
for (var rule in GitSubmoduleIgnore.values) {
if (ruleInt == rule.value) {
result = rule;
break;
}
}
return result;
} }
/// Sets the ignore rule for the submodule in the configuration. /// Sets the ignore rule for the submodule in the configuration.
@ -269,17 +256,8 @@ class Submodule {
/// ///
/// This value controls the behavior of the `git submodule update` command. /// This value controls the behavior of the `git submodule update` command.
GitSubmoduleUpdate get updateRule { GitSubmoduleUpdate get updateRule {
late GitSubmoduleUpdate result;
final ruleInt = bindings.updateRule(_submodulePointer); final ruleInt = bindings.updateRule(_submodulePointer);
return GitSubmoduleUpdate.values.singleWhere((e) => ruleInt == e.value);
for (var rule in GitSubmoduleUpdate.values) {
if (ruleInt == rule.value) {
result = rule;
break;
}
}
return result;
} }
/// Sets the update rule for the submodule in the configuration. /// Sets the update rule for the submodule in the configuration.

View file

@ -83,14 +83,10 @@ class Tree {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final repo = bindings.owner(_treePointer);
final int flagsInt =
flags.fold(0, (previousValue, e) => previousValue | e.value);
return Diff(diff_bindings.treeToWorkdir( return Diff(diff_bindings.treeToWorkdir(
repoPointer: repo, repoPointer: bindings.owner(_treePointer),
treePointer: _treePointer, treePointer: _treePointer,
flags: flagsInt, flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines, contextLines: contextLines,
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
)); ));
@ -105,15 +101,11 @@ class Tree {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final repo = bindings.owner(_treePointer);
final int flagsInt =
flags.fold(0, (previousValue, e) => previousValue | e.value);
return Diff(diff_bindings.treeToIndex( return Diff(diff_bindings.treeToIndex(
repoPointer: repo, repoPointer: bindings.owner(_treePointer),
treePointer: _treePointer, treePointer: _treePointer,
indexPointer: index.pointer, indexPointer: index.pointer,
flags: flagsInt, flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines, contextLines: contextLines,
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
)); ));
@ -128,15 +120,11 @@ class Tree {
int contextLines = 3, int contextLines = 3,
int interhunkLines = 0, int interhunkLines = 0,
}) { }) {
final repo = bindings.owner(_treePointer);
final int flagsInt =
flags.fold(0, (previousValue, e) => previousValue | e.value);
return Diff(diff_bindings.treeToTree( return Diff(diff_bindings.treeToTree(
repoPointer: repo, repoPointer: bindings.owner(_treePointer),
oldTreePointer: _treePointer, oldTreePointer: _treePointer,
newTreePointer: tree.pointer, newTreePointer: tree.pointer,
flags: flagsInt, flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines, contextLines: contextLines,
interhunkLines: interhunkLines, interhunkLines: interhunkLines,
)); ));

View file

@ -313,7 +313,7 @@ index e69de29..c217c63 100644
expect(patches.length, 8); expect(patches.length, 8);
expect(patches.first.delta.status, GitDelta.deleted); expect(patches.first.delta.status, GitDelta.deleted);
for (var p in patches) { for (final p in patches) {
p.free(); p.free();
} }
diff.free(); diff.free();

View file

@ -6,7 +6,7 @@ void main() {
test('returns list of compile time options for libgit2', () { test('returns list of compile time options for libgit2', () {
expect( expect(
Features.list, Features.list,
[GitFeature.threads, GitFeature.https, GitFeature.ssh, GitFeature.nsec], {GitFeature.threads, GitFeature.https, GitFeature.ssh, GitFeature.nsec},
); );
}); });
}); });

View file

@ -148,7 +148,7 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
final mailmap = Mailmap.fromBuffer(testMailmap); final mailmap = Mailmap.fromBuffer(testMailmap);
expect(mailmap, isA<Mailmap>()); expect(mailmap, isA<Mailmap>());
for (var entry in testResolve) { for (final entry in testResolve) {
expect( expect(
mailmap.resolve(name: entry['name']!, email: entry['email']!), mailmap.resolve(name: entry['name']!, email: entry['email']!),
[entry['realName'], entry['realEmail']], [entry['realName'], entry['realEmail']],
@ -162,7 +162,7 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
final mailmap = Mailmap.fromRepository(repo); final mailmap = Mailmap.fromRepository(repo);
expect(mailmap, isA<Mailmap>()); expect(mailmap, isA<Mailmap>());
for (var entry in testResolve) { for (final entry in testResolve) {
expect( expect(
mailmap.resolve(name: entry['name']!, email: entry['email']!), mailmap.resolve(name: entry['name']!, email: entry['email']!),
[entry['realName'], entry['realEmail']], [entry['realName'], entry['realEmail']],
@ -175,7 +175,7 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
test('successfully resolves names and emails when mailmap is empty', () { test('successfully resolves names and emails when mailmap is empty', () {
final mailmap = Mailmap.empty(); final mailmap = Mailmap.empty();
for (var entry in testResolve) { for (final entry in testResolve) {
expect( expect(
mailmap.resolve(name: entry['name']!, email: entry['email']!), mailmap.resolve(name: entry['name']!, email: entry['email']!),
[entry['name'], entry['email']], [entry['name'], entry['email']],
@ -188,7 +188,7 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
test('successfully adds entries and resolves them', () { test('successfully adds entries and resolves them', () {
final mailmap = Mailmap.empty(); final mailmap = Mailmap.empty();
for (var entry in testEntries) { for (final entry in testEntries) {
mailmap.addEntry( mailmap.addEntry(
realName: entry['realName'], realName: entry['realName'],
realEmail: entry['realEmail'], realEmail: entry['realEmail'],
@ -197,7 +197,7 @@ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
); );
} }
for (var entry in testResolve) { for (final entry in testResolve) {
expect( expect(
mailmap.resolve(name: entry['name']!, email: entry['email']!), mailmap.resolve(name: entry['name']!, email: entry['email']!),
[entry['realName'], entry['realEmail']], [entry['realName'], entry['realEmail']],

View file

@ -27,8 +27,10 @@ void main() {
); );
final result = repo.mergeAnalysis(theirHead: commit.id); final result = repo.mergeAnalysis(theirHead: commit.id);
expect(result[0], {GitMergeAnalysis.upToDate}); expect(result, [
expect(result[1], {GitMergePreference.none}); {GitMergeAnalysis.upToDate},
GitMergePreference.none,
]);
expect(repo.status, isEmpty); expect(repo.status, isEmpty);
commit.free(); commit.free();

View file

@ -87,9 +87,9 @@ void main() {
test('successfully packs with provided packDelegate', () { test('successfully packs with provided packDelegate', () {
void packDelegate(PackBuilder packBuilder) { void packDelegate(PackBuilder packBuilder) {
final branches = repo.branches; final branches = repo.branches;
for (var branch in branches) { for (final branch in branches) {
final ref = repo.lookupReference('refs/heads/${branch.name}'); final ref = repo.lookupReference('refs/heads/${branch.name}');
for (var commit in repo.log(sha: ref.target.sha)) { for (final commit in repo.log(sha: ref.target.sha)) {
packBuilder.addRecursively(commit.id); packBuilder.addRecursively(commit.id);
commit.free(); commit.free();
} }

View file

@ -45,7 +45,7 @@ void main() {
expect(commits[i].id.sha, log[i]); expect(commits[i].id.sha, log[i]);
} }
for (var c in commits) { for (final c in commits) {
c.free(); c.free();
} }
}); });

View file

@ -36,7 +36,7 @@ void main() {
expect(commits[i].id.sha, log[i]); expect(commits[i].id.sha, log[i]);
} }
for (var c in commits) { for (final c in commits) {
c.free(); c.free();
} }
walker.free(); walker.free();
@ -54,7 +54,7 @@ void main() {
expect(commits[i].id.sha, log.reversed.toList()[i]); expect(commits[i].id.sha, log.reversed.toList()[i]);
} }
for (var c in commits) { for (final c in commits) {
c.free(); c.free();
} }
walker.free(); walker.free();
@ -77,10 +77,10 @@ void main() {
expect(reverseSortedCommits[i].id.sha, log.reversed.toList()[i]); expect(reverseSortedCommits[i].id.sha, log.reversed.toList()[i]);
} }
for (var c in timeSortedCommits) { for (final c in timeSortedCommits) {
c.free(); c.free();
} }
for (var c in reverseSortedCommits) { for (final c in reverseSortedCommits) {
c.free(); c.free();
} }
walker.free(); walker.free();
@ -97,7 +97,7 @@ void main() {
expect(commits.length, 2); expect(commits.length, 2);
for (var c in commits) { for (final c in commits) {
c.free(); c.free();
} }
walker.free(); walker.free();
@ -129,7 +129,7 @@ void main() {
expect(commits.length, 3); expect(commits.length, 3);
} }
for (var c in commits) { for (final c in commits) {
c.free(); c.free();
} }
walker.free(); walker.free();