diff --git a/lib/src/bindings/attr.dart b/lib/src/bindings/attr.dart index 189875b..f1ede92 100644 --- a/lib/src/bindings/attr.dart +++ b/lib/src/bindings/attr.dart @@ -25,20 +25,27 @@ dynamic getAttribute({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } final attributeValue = libgit2.git_attr_value(out.value); if (attributeValue == git_attr_value_t.GIT_ATTR_VALUE_UNSPECIFIED) { + calloc.free(out); return null; } else if (attributeValue == git_attr_value_t.GIT_ATTR_VALUE_TRUE) { + calloc.free(out); return true; } else if (attributeValue == git_attr_value_t.GIT_ATTR_VALUE_FALSE) { + calloc.free(out); return false; } else if (attributeValue == git_attr_value_t.GIT_ATTR_VALUE_STRING) { - return out.value.cast().toDartString(); + final result = out.value.cast().toDartString(); + calloc.free(out); + return result; } else { + calloc.free(out); throw Exception('The attribute value from libgit2 is invalid'); } } diff --git a/lib/src/bindings/blame.dart b/lib/src/bindings/blame.dart index 6fbb7fc..6516c6e 100644 --- a/lib/src/bindings/blame.dart +++ b/lib/src/bindings/blame.dart @@ -11,7 +11,7 @@ import 'libgit2_bindings.dart'; Pointer file({ required Pointer repoPointer, required String path, - int? flags, + required int flags, int? minMatchCharacters, Oid? newestCommit, Oid? oldestCommit, @@ -27,12 +27,13 @@ Pointer file({ ); if (optionsError < 0) { + calloc.free(out); + calloc.free(pathC); + calloc.free(options); throw LibGit2Error(libgit2.git_error_last()); } - if (flags != null) { - options.ref.flags = flags; - } + options.ref.flags = flags; if (minMatchCharacters != null) { options.ref.min_match_characters = minMatchCharacters; @@ -56,7 +57,11 @@ Pointer file({ final error = libgit2.git_blame_file(out, repoPointer, pathC, options); + calloc.free(pathC); + calloc.free(options); + if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/blob.dart b/lib/src/bindings/blob.dart index 00232b2..0971d91 100644 --- a/lib/src/bindings/blob.dart +++ b/lib/src/bindings/blob.dart @@ -15,6 +15,7 @@ Pointer lookup({ final error = libgit2.git_blob_lookup(out, repoPointer, oidPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -30,8 +31,7 @@ Pointer id(Pointer blob) => libgit2.git_blob_id(blob); /// Searching for NUL bytes and looking for a reasonable ratio of printable to /// non-printable characters among the first 8000 bytes. bool isBinary(Pointer blob) { - final result = libgit2.git_blob_is_binary(blob); - return result == 1 ? true : false; + return libgit2.git_blob_is_binary(blob) == 1 ? true : false; } /// Get a read-only buffer with the raw content of a blob. @@ -40,8 +40,7 @@ bool isBinary(Pointer blob) { /// internally by the object and shall not be free'd. The pointer may be invalidated /// at a later time. String content(Pointer blob) { - final result = libgit2.git_blob_rawcontent(blob); - return result.cast().toDartString(); + return libgit2.git_blob_rawcontent(blob).cast().toDartString(); } /// Get the size in bytes of the contents of a blob. @@ -57,12 +56,17 @@ Pointer create({ }) { final out = calloc(); final bufferC = buffer.toNativeUtf8().cast(); - final error = - libgit2.git_blob_create_from_buffer(out, repoPointer, bufferC, len); + final error = libgit2.git_blob_create_from_buffer( + out, + repoPointer, + bufferC, + len, + ); calloc.free(bufferC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -79,12 +83,16 @@ Pointer createFromWorkdir({ }) { final out = calloc(); final relativePathC = relativePath.toNativeUtf8().cast(); - final error = - libgit2.git_blob_create_from_workdir(out, repoPointer, relativePathC); + final error = libgit2.git_blob_create_from_workdir( + out, + repoPointer, + relativePathC, + ); calloc.free(relativePathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -102,7 +110,10 @@ Pointer createFromDisk({ final pathC = path.toNativeUtf8().cast(); final error = libgit2.git_blob_create_from_disk(out, repoPointer, pathC); + calloc.free(pathC); + if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; diff --git a/lib/src/bindings/branch.dart b/lib/src/bindings/branch.dart index 6fc21ab..e60be71 100644 --- a/lib/src/bindings/branch.dart +++ b/lib/src/bindings/branch.dart @@ -21,6 +21,7 @@ List> list({ ); if (iteratorError < 0) { + libgit2.git_branch_iterator_free(iterator.value); throw LibGit2Error(libgit2.git_error_last()); } @@ -65,6 +66,7 @@ Pointer lookup({ calloc.free(branchNameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -101,6 +103,7 @@ Pointer create({ calloc.free(branchNameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -109,17 +112,12 @@ Pointer create({ /// Delete an existing branch reference. /// -/// Note that if the deletion succeeds, the reference object will not be valid anymore, -/// and will be freed. -/// /// Throws a [LibGit2Error] if error occured. void delete(Pointer branch) { final error = libgit2.git_branch_delete(branch); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); - } else { - reference_bindings.free(branch); } } @@ -136,15 +134,19 @@ void rename({ final out = calloc>(); final newBranchNameC = newBranchName.toNativeUtf8().cast(); final forceC = force ? 1 : 0; - final error = - libgit2.git_branch_move(out, branchPointer, newBranchNameC, forceC); + final error = libgit2.git_branch_move( + out, + branchPointer, + newBranchNameC, + forceC, + ); calloc.free(newBranchNameC); + reference_bindings.free(out.value); + calloc.free(out); if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); - } else { - reference_bindings.free(out.value); } } @@ -188,6 +190,7 @@ String name(Pointer ref) { final error = libgit2.git_branch_name(out, ref); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.value.cast().toDartString(); diff --git a/lib/src/bindings/checkout.dart b/lib/src/bindings/checkout.dart index a3353be..5082a0f 100644 --- a/lib/src/bindings/checkout.dart +++ b/lib/src/bindings/checkout.dart @@ -33,7 +33,6 @@ void head({ for (final p in pathPointers) { calloc.free(p); } - calloc.free(strArray); calloc.free(optsC); @@ -65,7 +64,6 @@ void index({ for (final p in pathPointers) { calloc.free(p); } - calloc.free(strArray); calloc.free(optsC); @@ -99,7 +97,6 @@ void tree({ for (final p in pathPointers) { calloc.free(p); } - calloc.free(strArray); calloc.free(optsC); diff --git a/lib/src/bindings/commit.dart b/lib/src/bindings/commit.dart index 8261ca0..0d9d73a 100644 --- a/lib/src/bindings/commit.dart +++ b/lib/src/bindings/commit.dart @@ -17,6 +17,7 @@ Pointer lookup({ final error = libgit2.git_commit_lookup(out, repoPointer, oidPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -40,6 +41,7 @@ Pointer lookupPrefix({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -68,6 +70,7 @@ Pointer> annotatedLookup({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -127,6 +130,7 @@ Pointer create({ calloc.free(parentsC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -139,20 +143,14 @@ Pointer create({ /// in that case UTF-8 is assumed. String messageEncoding(Pointer commit) { final result = libgit2.git_commit_message_encoding(commit); - - if (result == nullptr) { - return 'utf-8'; - } else { - return result.cast().toDartString(); - } + return result == nullptr ? 'utf-8' : result.cast().toDartString(); } /// Get the full message of a commit. /// /// The returned message will be slightly prettified by removing any potential leading newlines. String message(Pointer commit) { - final out = libgit2.git_commit_message(commit); - return out.cast().toDartString(); + return libgit2.git_commit_message(commit).cast().toDartString(); } /// Get the id of a commit. @@ -215,6 +213,8 @@ Pointer revertCommit({ libgit2.git_merge_options_init(opts, GIT_MERGE_OPTIONS_VERSION); if (optsError < 0) { + calloc.free(out); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -230,6 +230,7 @@ Pointer revertCommit({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/config.dart b/lib/src/bindings/config.dart index 50f57fa..cd577b0 100644 --- a/lib/src/bindings/config.dart +++ b/lib/src/bindings/config.dart @@ -15,6 +15,7 @@ Pointer newConfig() { final error = libgit2.git_config_new(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -32,6 +33,7 @@ Pointer open(String path) { calloc.free(pathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -50,6 +52,7 @@ Pointer openDefault() { final error = libgit2.git_config_open_default(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -74,9 +77,12 @@ String findGlobal() { final error = libgit2.git_config_find_global(out); if (error != 0) { + calloc.free(out); throw Error(); } else { - return out.ref.ptr.cast().toDartString(); + final result = out.ref.ptr.cast().toDartString(); + calloc.free(out); + return result; } } @@ -90,9 +96,12 @@ String findSystem() { final error = libgit2.git_config_find_system(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return out.ref.ptr.cast().toDartString(); + final result = out.ref.ptr.cast().toDartString(); + calloc.free(out); + return result; } } @@ -107,9 +116,12 @@ String findXdg() { final error = libgit2.git_config_find_xdg(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return out.ref.ptr.cast().toDartString(); + final result = out.ref.ptr.cast().toDartString(); + calloc.free(out); + return result; } } @@ -125,6 +137,7 @@ Pointer snapshot(Pointer config) { final error = libgit2.git_config_snapshot(out, config); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -145,6 +158,7 @@ Pointer getEntry({ calloc.free(name); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/credentials.dart b/lib/src/bindings/credentials.dart index 1ecb8cf..d7f0c9f 100644 --- a/lib/src/bindings/credentials.dart +++ b/lib/src/bindings/credentials.dart @@ -19,6 +19,7 @@ Pointer username(String username) { calloc.free(usernameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -36,13 +37,17 @@ Pointer userPass({ final usernameC = username.toNativeUtf8().cast(); final passwordC = password.toNativeUtf8().cast(); - final error = - libgit2.git_credential_userpass_plaintext_new(out, usernameC, passwordC); + final error = libgit2.git_credential_userpass_plaintext_new( + out, + usernameC, + passwordC, + ); calloc.free(usernameC); calloc.free(passwordC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -78,6 +83,7 @@ Pointer sshKey({ calloc.free(passPhraseC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -96,6 +102,7 @@ Pointer sshKeyFromAgent(String username) { calloc.free(usernameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -131,6 +138,7 @@ Pointer sshKeyFromMemory({ calloc.free(passPhraseC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/describe.dart b/lib/src/bindings/describe.dart index 8c06cae..81539e0 100644 --- a/lib/src/bindings/describe.dart +++ b/lib/src/bindings/describe.dart @@ -33,6 +33,7 @@ Pointer commit({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -68,6 +69,7 @@ Pointer workdir({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -91,6 +93,8 @@ String format({ ); if (optsError < 0) { + calloc.free(out); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -106,7 +110,10 @@ String format({ final error = libgit2.git_describe_format(out, describeResultPointer, opts); + calloc.free(opts); + if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); diff --git a/lib/src/bindings/diff.dart b/lib/src/bindings/diff.dart index 7d32c8d..7033552 100644 --- a/lib/src/bindings/diff.dart +++ b/lib/src/bindings/diff.dart @@ -150,6 +150,7 @@ Pointer parse(String content) { calloc.free(contentC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -173,8 +174,16 @@ void findSimilar({ required int renameLimit, }) { final opts = calloc(); - final optsError = - libgit2.git_diff_find_options_init(opts, GIT_DIFF_FIND_OPTIONS_VERSION); + final optsError = libgit2.git_diff_find_options_init( + opts, + GIT_DIFF_FIND_OPTIONS_VERSION, + ); + + if (optsError < 0) { + calloc.free(opts); + throw LibGit2Error(libgit2.git_error_last()); + } + opts.ref.flags = flags; opts.ref.rename_threshold = renameThreshold; opts.ref.copy_threshold = copyThreshold; @@ -182,17 +191,13 @@ void findSimilar({ opts.ref.break_rewrite_threshold = breakRewriteThreshold; opts.ref.rename_limit = renameLimit; - if (optsError < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - final error = libgit2.git_diff_find_similar(diffPointer, opts); + calloc.free(opts); + if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } - - calloc.free(opts); } /// Calculate the patch ID for the given patch. @@ -210,6 +215,7 @@ Pointer patchId(Pointer diff) { final error = libgit2.git_diff_patchid(out, diff, nullptr); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -239,8 +245,7 @@ Pointer getDeltaByIndex({ /// value into these letters for your own purposes. [GitDelta.untracked] will return /// a space (i.e. ' '). String statusChar(int status) { - final result = libgit2.git_diff_status_char(status); - return String.fromCharCode(result); + return String.fromCharCode(libgit2.git_diff_status_char(status)); } /// Accumulate diff statistics for all patches. @@ -251,6 +256,7 @@ Pointer stats(Pointer diff) { final error = libgit2.git_diff_get_stats(out, diff); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -281,6 +287,7 @@ String statsPrint({ final error = libgit2.git_diff_stats_to_buf(out, statsPointer, format, width); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); diff --git a/lib/src/bindings/graph.dart b/lib/src/bindings/graph.dart index 06d9f6a..e939d51 100644 --- a/lib/src/bindings/graph.dart +++ b/lib/src/bindings/graph.dart @@ -49,5 +49,8 @@ List aheadBehind({ upstreamPointer, ); - return [ahead.value, behind.value]; + final result = [ahead.value, behind.value]; + calloc.free(ahead); + calloc.free(behind); + return result; } diff --git a/lib/src/bindings/index.dart b/lib/src/bindings/index.dart index edd062e..583b274 100644 --- a/lib/src/bindings/index.dart +++ b/lib/src/bindings/index.dart @@ -57,6 +57,7 @@ Pointer writeTree(Pointer index) { final error = libgit2.git_index_write_tree(out, index); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -79,6 +80,7 @@ Pointer writeTreeTo({ final error = libgit2.git_index_write_tree_to(out, indexPointer, repoPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -210,9 +212,9 @@ void addAll({ required List pathspec, }) { var pathspecC = calloc(); - final List> pathPointers = + final pathPointers = pathspec.map((e) => e.toNativeUtf8().cast()).toList(); - final Pointer> strArray = calloc(pathspec.length); + final strArray = calloc>(pathspec.length); for (var i = 0; i < pathspec.length; i++) { strArray[i] = pathPointers[i]; @@ -277,9 +279,9 @@ void removeAll({ required List pathspec, }) { final pathspecC = calloc(); - final List> pathPointers = + final pathPointers = pathspec.map((e) => e.toNativeUtf8().cast()).toList(); - final Pointer> strArray = calloc(pathspec.length); + final strArray = calloc>(pathspec.length); for (var i = 0; i < pathspec.length; i++) { strArray[i] = pathPointers[i]; @@ -318,10 +320,13 @@ List>> conflictList( Pointer index, ) { final iterator = calloc>(); - final iteratorError = - libgit2.git_index_conflict_iterator_new(iterator, index); + final iteratorError = libgit2.git_index_conflict_iterator_new( + iterator, + index, + ); if (iteratorError < 0) { + calloc.free(iterator); throw LibGit2Error(libgit2.git_error_last()); } diff --git a/lib/src/bindings/mailmap.dart b/lib/src/bindings/mailmap.dart index ec46de5..e9199ac 100644 --- a/lib/src/bindings/mailmap.dart +++ b/lib/src/bindings/mailmap.dart @@ -15,6 +15,7 @@ Pointer init() { final error = libgit2.git_mailmap_new(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -33,6 +34,7 @@ Pointer fromBuffer(String buffer) { calloc.free(bufferC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -55,6 +57,7 @@ Pointer fromRepository(Pointer repo) { final error = libgit2.git_mailmap_from_repository(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -114,6 +117,7 @@ Pointer resolveSignature({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/merge.dart b/lib/src/bindings/merge.dart index c067abe..a589a0a 100644 --- a/lib/src/bindings/merge.dart +++ b/lib/src/bindings/merge.dart @@ -16,6 +16,7 @@ Pointer mergeBase({ final error = libgit2.git_merge_base(out, repoPointer, aPointer, bPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -45,6 +46,8 @@ List analysis({ var result = []; if (error < 0) { + calloc.free(analysisOut); + calloc.free(preferenceOut); throw LibGit2Error(libgit2.git_error_last()); } else { result.add(analysisOut.value); @@ -67,10 +70,28 @@ void merge({ required int theirHeadsLen, }) { final mergeOpts = calloc(); - libgit2.git_merge_options_init(mergeOpts, GIT_MERGE_OPTIONS_VERSION); + final mergeError = libgit2.git_merge_options_init( + mergeOpts, + GIT_MERGE_OPTIONS_VERSION, + ); + + if (mergeError < 0) { + calloc.free(mergeOpts); + throw LibGit2Error(libgit2.git_error_last()); + } final checkoutOpts = calloc(); - libgit2.git_checkout_options_init(checkoutOpts, GIT_CHECKOUT_OPTIONS_VERSION); + final checkoutError = libgit2.git_checkout_options_init( + checkoutOpts, + GIT_CHECKOUT_OPTIONS_VERSION, + ); + + if (checkoutError < 0) { + calloc.free(mergeOpts); + calloc.free(checkoutOpts); + throw LibGit2Error(libgit2.git_error_last()); + } + checkoutOpts.ref.checkout_strategy = git_checkout_strategy_t.GIT_CHECKOUT_SAFE | git_checkout_strategy_t.GIT_CHECKOUT_RECREATE_MISSING; @@ -83,6 +104,9 @@ void merge({ checkoutOpts, ); + calloc.free(mergeOpts); + calloc.free(checkoutOpts); + if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } @@ -110,6 +134,7 @@ String mergeFileFromIndex({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(length: out.ref.len); @@ -152,6 +177,7 @@ Pointer mergeCommits({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -194,6 +220,7 @@ Pointer mergeTrees({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -207,13 +234,24 @@ void cherryPick({ required Pointer repoPointer, required Pointer commitPointer, }) { - final opts = calloc(sizeOf()); - libgit2.git_cherrypick_options_init(opts, GIT_CHERRYPICK_OPTIONS_VERSION); + final opts = calloc(); + final optsError = libgit2.git_cherrypick_options_init( + opts, + GIT_CHERRYPICK_OPTIONS_VERSION, + ); + + if (optsError < 0) { + calloc.free(opts); + throw LibGit2Error(libgit2.git_error_last()); + } + opts.ref.checkout_opts.checkout_strategy = git_checkout_strategy_t.GIT_CHECKOUT_SAFE; final error = libgit2.git_cherrypick(repoPointer, commitPointer, opts); + calloc.free(opts); + if (error < 0) { throw LibGit2Error(libgit2.git_error_last()); } @@ -231,6 +269,7 @@ Pointer _initMergeOptions({ ); if (error < 0) { + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } diff --git a/lib/src/bindings/note.dart b/lib/src/bindings/note.dart index da82c74..b2146f6 100644 --- a/lib/src/bindings/note.dart +++ b/lib/src/bindings/note.dart @@ -15,6 +15,7 @@ List> list(Pointer repo) { final iteratorError = libgit2.git_note_iterator_new(iterator, repo, notesRef); if (iteratorError < 0) { + calloc.free(iterator); throw LibGit2Error(libgit2.git_error_last()); } @@ -32,6 +33,9 @@ List> list(Pointer repo) { calloc.free(noteId); if (error < 0) { + calloc.free(out); + calloc.free(annotatedId); + calloc.free(iterator); throw LibGit2Error(libgit2.git_error_last()); } else { result.add({'note': out.value, 'annotatedId': annotatedId}); @@ -64,6 +68,7 @@ Pointer lookup({ calloc.free(notesRefC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -101,6 +106,7 @@ Pointer create({ calloc.free(noteC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; diff --git a/lib/src/bindings/object.dart b/lib/src/bindings/object.dart index 4138b3e..e5e57e2 100644 --- a/lib/src/bindings/object.dart +++ b/lib/src/bindings/object.dart @@ -26,6 +26,7 @@ Pointer lookup({ final error = libgit2.git_object_lookup(out, repoPointer, oidPointer, type); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/odb.dart b/lib/src/bindings/odb.dart index 5eddc9e..584da77 100644 --- a/lib/src/bindings/odb.dart +++ b/lib/src/bindings/odb.dart @@ -17,6 +17,7 @@ Pointer create() { final error = libgit2.git_odb_new(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -65,6 +66,7 @@ Pointer existsPrefix({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -76,8 +78,7 @@ bool exists({ required Pointer odbPointer, required Pointer oidPointer, }) { - final result = libgit2.git_odb_exists(odbPointer, oidPointer); - return result == 1 ? true : false; + return libgit2.git_odb_exists(odbPointer, oidPointer) == 1 ? true : false; } /// List of objects in the database. @@ -100,11 +101,10 @@ int _forEachCb( /// Throws a [LibGit2Error] if error occured. List objects(Pointer odb) { const except = -1; - final payload = calloc>(); final cb = Pointer.fromFunction, Pointer)>( _forEachCb, except); - final error = libgit2.git_odb_foreach(odb, cb, payload.cast()); + final error = libgit2.git_odb_foreach(odb, cb, nullptr); if (error < 0) { _objects.clear(); @@ -133,6 +133,7 @@ Pointer read({ final error = libgit2.git_odb_read(out, odbPointer, oidPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -155,9 +156,7 @@ int objectType(Pointer object) { /// /// This is the uncompressed, raw data as read from the ODB, without the leading header. String objectData(Pointer object) { - final out = libgit2.git_odb_object_data(object); - - return out.cast().toDartString(); + return libgit2.git_odb_object_data(object).cast().toDartString(); } /// Return the size of an ODB object. @@ -192,6 +191,7 @@ Pointer write({ ); if (streamError < 0) { + libgit2.git_odb_stream_free(stream.value); throw LibGit2Error(libgit2.git_error_last()); } @@ -203,6 +203,7 @@ Pointer write({ ); if (writeError < 0) { + calloc.free(buffer); libgit2.git_odb_stream_free(stream.value); throw LibGit2Error(libgit2.git_error_last()); } @@ -217,6 +218,7 @@ Pointer write({ libgit2.git_odb_stream_free(stream.value); if (finalizeError < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -237,6 +239,7 @@ Pointer getBackend({ final error = libgit2.git_odb_get_backend(out, odbPointer, position); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/oid.dart b/lib/src/bindings/oid.dart index 9b25100..bb41e92 100644 --- a/lib/src/bindings/oid.dart +++ b/lib/src/bindings/oid.dart @@ -13,9 +13,11 @@ Pointer fromStrN(String hex) { final out = calloc(); final str = hex.toNativeUtf8().cast(); final error = libgit2.git_oid_fromstrn(out, str, hex.length); + calloc.free(str); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -29,9 +31,11 @@ Pointer fromSHA(String hex) { final out = calloc(); final str = hex.toNativeUtf8().cast(); final error = libgit2.git_oid_fromstr(out, str); + calloc.free(str); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -52,6 +56,7 @@ Pointer fromRaw(Array raw) { calloc.free(rawC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -64,13 +69,13 @@ Pointer fromRaw(Array raw) { String toSHA(Pointer id) { final out = calloc(40); final error = libgit2.git_oid_fmt(out, id); - final result = out.cast().toDartString(length: 40); - - calloc.free(out); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { + final result = out.cast().toDartString(length: 40); + calloc.free(out); return result; } } @@ -93,6 +98,7 @@ Pointer copy(Pointer src) { final error = libgit2.git_oid_cpy(out, src); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; diff --git a/lib/src/bindings/packbuilder.dart b/lib/src/bindings/packbuilder.dart index 3d7a219..ab593ff 100644 --- a/lib/src/bindings/packbuilder.dart +++ b/lib/src/bindings/packbuilder.dart @@ -12,6 +12,7 @@ Pointer init(Pointer repo) { final error = libgit2.git_packbuilder_new(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/patch.dart b/lib/src/bindings/patch.dart index eb755fe..c208a4c 100644 --- a/lib/src/bindings/patch.dart +++ b/lib/src/bindings/patch.dart @@ -43,13 +43,12 @@ Map fromBuffers({ opts, ); - final result = {}; - calloc.free(oldAsPathC); calloc.free(newAsPathC); calloc.free(opts); if (error < 0) { + calloc.free(out); calloc.free(oldBufferC); calloc.free(newBufferC); throw LibGit2Error(libgit2.git_error_last()); @@ -57,10 +56,7 @@ Map fromBuffers({ // Returning map with pointers to patch and buffers because patch object does not // have refenrece to underlying buffers or blobs. So if the buffer or blob is freed/removed // the patch text becomes corrupted. - result['patch'] = out.value; - result['a'] = oldBufferC; - result['b'] = newBufferC; - return result; + return {'patch': out.value, 'a': oldBufferC, 'b': newBufferC}; } } @@ -97,22 +93,18 @@ Map fromBlobs({ opts, ); - final result = {}; - calloc.free(oldAsPathC); calloc.free(newAsPathC); calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { // Returning map with pointers to patch and blobs because patch object does not // have reference to underlying blobs. So if the blob is freed/removed the patch // text becomes corrupted. - result['patch'] = out.value; - result['a'] = oldBlobPointer; - result['b'] = newBlobPointer; - return result; + return {'patch': out.value, 'a': oldBlobPointer, 'b': newBlobPointer}; } } @@ -152,23 +144,19 @@ Map fromBlobAndBuffer({ opts, ); - final result = {}; - calloc.free(oldAsPathC); calloc.free(bufferAsPathC); calloc.free(opts); if (error < 0) { + calloc.free(out); calloc.free(bufferC); throw LibGit2Error(libgit2.git_error_last()); } else { // Returning map with pointers to patch and buffers because patch object does not // have reference to underlying buffers or blobs. So if the buffer or blob is freed/removed // the patch text becomes corrupted. - result['patch'] = out.value; - result['a'] = oldBlobPointer; - result['b'] = bufferC; - return result; + return {'patch': out.value, 'a': oldBlobPointer, 'b': bufferC}; } } @@ -187,6 +175,7 @@ Pointer fromDiff({ final error = libgit2.git_patch_from_diff(out, diffPointer, index); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -217,14 +206,15 @@ Map hunk({ patchPointer, hunkIndex, ); - final result = {}; if (error < 0) { + calloc.free(out); + calloc.free(linesInHunk); throw LibGit2Error(libgit2.git_error_last()); } else { - result['hunk'] = out.value; - result['linesN'] = linesInHunk.value; - return result; + final linesN = linesInHunk.value; + calloc.free(linesInHunk); + return {'hunk': out.value, 'linesN': linesN}; } } @@ -245,6 +235,7 @@ Pointer lines({ ); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -259,6 +250,7 @@ String text(Pointer patch) { final error = libgit2.git_patch_to_buf(out, patch); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); @@ -302,15 +294,19 @@ Pointer _diffOptionsInit({ required int interhunkLines, }) { final opts = calloc(); - final optsError = - libgit2.git_diff_options_init(opts, GIT_DIFF_OPTIONS_VERSION); + final optsError = libgit2.git_diff_options_init( + opts, + GIT_DIFF_OPTIONS_VERSION, + ); + + if (optsError < 0) { + calloc.free(opts); + throw LibGit2Error(libgit2.git_error_last()); + } + opts.ref.flags = flags; opts.ref.context_lines = contextLines; opts.ref.interhunk_lines = interhunkLines; - if (optsError < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } else { - return opts; - } + return opts; } diff --git a/lib/src/bindings/rebase.dart b/lib/src/bindings/rebase.dart index ed26847..5d895a1 100644 --- a/lib/src/bindings/rebase.dart +++ b/lib/src/bindings/rebase.dart @@ -31,6 +31,8 @@ Pointer init({ ); if (optsError < 0) { + calloc.free(out); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -43,7 +45,10 @@ Pointer init({ opts, ); + calloc.free(opts); + if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -63,13 +68,14 @@ int operationsCount(Pointer rebase) { /// /// Throws a [LibGit2Error] if error occured. Pointer next(Pointer rebase) { - final operation = calloc>(); - final error = libgit2.git_rebase_next(operation, rebase); + final out = calloc>(); + final error = libgit2.git_rebase_next(out, rebase); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return operation.value; + return out.value; } } @@ -83,11 +89,11 @@ void commit({ required Pointer committerPointer, required String? message, }) { - final id = calloc(); + final out = calloc(); final messageC = message?.toNativeUtf8().cast() ?? nullptr; final error = libgit2.git_rebase_commit( - id, + out, rebasePointer, authorPointer ?? nullptr, committerPointer, @@ -95,6 +101,7 @@ void commit({ messageC, ); + calloc.free(out); calloc.free(messageC); if (error < 0) { diff --git a/lib/src/bindings/reference.dart b/lib/src/bindings/reference.dart index 8a54f5c..41ac3cf 100644 --- a/lib/src/bindings/reference.dart +++ b/lib/src/bindings/reference.dart @@ -41,6 +41,7 @@ Pointer resolve(Pointer ref) { final error = libgit2.git_reference_resolve(out, ref); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -65,6 +66,7 @@ Pointer lookup({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -88,6 +90,7 @@ Pointer lookupDWIM({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -96,10 +99,7 @@ Pointer lookupDWIM({ /// Get the full name of a reference. String name(Pointer ref) { - var result = calloc(); - result = libgit2.git_reference_name(ref); - - return result.cast().toDartString(); + return libgit2.git_reference_name(ref).cast().toDartString(); } /// Get the reference's short name. @@ -107,8 +107,7 @@ String name(Pointer ref) { /// This will transform the reference name into a name "human-readable" version. /// If no shortname is appropriate, it will return the full name. String shorthand(Pointer ref) { - final result = libgit2.git_reference_shorthand(ref); - return result.cast().toDartString(); + return libgit2.git_reference_shorthand(ref).cast().toDartString(); } /// Rename an existing reference. @@ -146,6 +145,7 @@ Pointer rename({ calloc.free(logMessageC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -195,26 +195,22 @@ bool hasLog({ /// Check if a reference is a local branch. bool isBranch(Pointer ref) { - final result = libgit2.git_reference_is_branch(ref); - return result == 1 ? true : false; + return libgit2.git_reference_is_branch(ref) == 1 ? true : false; } /// Check if a reference is a note. bool isNote(Pointer ref) { - final result = libgit2.git_reference_is_note(ref); - return result == 1 ? true : false; + return libgit2.git_reference_is_note(ref) == 1 ? true : false; } /// Check if a reference is a remote tracking branch. bool isRemote(Pointer ref) { - final result = libgit2.git_reference_is_remote(ref); - return result == 1 ? true : false; + return libgit2.git_reference_is_remote(ref) == 1 ? true : false; } /// Check if a reference is a tag. bool isTag(Pointer ref) { - final result = libgit2.git_reference_is_tag(ref); - return result == 1 ? true : false; + return libgit2.git_reference_is_tag(ref) == 1 ? true : false; } /// Create a new direct reference. @@ -263,6 +259,7 @@ Pointer createDirect({ calloc.free(logMessageC); if (error < 0) { + calloc.free(out); throw (LibGit2Error(libgit2.git_error_last())); } else { return out.value; @@ -317,6 +314,7 @@ Pointer createSymbolic({ calloc.free(logMessageC); if (error < 0) { + calloc.free(out); throw (LibGit2Error(libgit2.git_error_last())); } else { return out.value; @@ -365,6 +363,7 @@ Pointer setTarget({ calloc.free(logMessageC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -401,6 +400,7 @@ Pointer setTargetSymbolic({ calloc.free(logMessageC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -412,8 +412,9 @@ bool compare({ required Pointer ref1Pointer, required Pointer ref2Pointer, }) { - final result = libgit2.git_reference_cmp(ref1Pointer, ref2Pointer); - return result == 0 ? true : false; + return libgit2.git_reference_cmp(ref1Pointer, ref2Pointer) == 0 + ? true + : false; } /// Recursively peel reference until object of the specified type is found. @@ -432,6 +433,7 @@ Pointer peel({ final error = libgit2.git_reference_peel(out, refPointer, type); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/reflog.dart b/lib/src/bindings/reflog.dart index 73a9556..07f4b24 100644 --- a/lib/src/bindings/reflog.dart +++ b/lib/src/bindings/reflog.dart @@ -23,6 +23,7 @@ Pointer read({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -46,8 +47,7 @@ Pointer getByIndex({ /// Get the log message. String entryMessage(Pointer entry) { - final result = libgit2.git_reflog_entry_message(entry); - return result.cast().toDartString(); + return libgit2.git_reflog_entry_message(entry).cast().toDartString(); } /// Get the committer of this entry. diff --git a/lib/src/bindings/refspec.dart b/lib/src/bindings/refspec.dart index 5506a47..4d17a5d 100644 --- a/lib/src/bindings/refspec.dart +++ b/lib/src/bindings/refspec.dart @@ -68,6 +68,7 @@ String transform({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); @@ -90,6 +91,7 @@ String rTransform({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.ref.ptr.cast().toDartString(); diff --git a/lib/src/bindings/remote.dart b/lib/src/bindings/remote.dart index 61c6c8c..171992b 100644 --- a/lib/src/bindings/remote.dart +++ b/lib/src/bindings/remote.dart @@ -15,6 +15,7 @@ List list(Pointer repo) { final error = libgit2.git_remote_list(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { var result = []; @@ -42,6 +43,7 @@ Pointer lookup({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -65,6 +67,7 @@ Pointer create({ calloc.free(urlC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -97,6 +100,7 @@ Pointer createWithFetchSpec({ calloc.free(fetchC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -148,6 +152,7 @@ List rename({ calloc.free(newNameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { var result = []; @@ -374,11 +379,13 @@ List> lsRemotes(Pointer remote) { final size = calloc(); final error = libgit2.git_remote_ls(out, size, remote); - var result = >[]; - if (error < 0) { + calloc.free(out); + calloc.free(size); throw LibGit2Error(libgit2.git_error_last()); } else { + var result = >[]; + for (var i = 0; i < size.value; i++) { var remote = {}; Oid? loid; @@ -401,6 +408,9 @@ List> lsRemotes(Pointer remote) { result.add(remote); } + calloc.free(out); + calloc.free(size); + return result; } } @@ -441,6 +451,14 @@ void fetch({ ); if (optsError < 0) { + for (final p in refspecsPointers) { + calloc.free(p); + } + calloc.free(refspecsC); + calloc.free(strArray); + calloc.free(proxyOptions); + calloc.free(reflogMessageC); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -501,6 +519,13 @@ void push({ libgit2.git_push_options_init(opts, GIT_PUSH_OPTIONS_VERSION); if (optsError < 0) { + for (final p in refspecsPointers) { + calloc.free(p); + } + calloc.free(strArray); + calloc.free(refspecsC); + calloc.free(proxyOptions); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -555,6 +580,7 @@ void prune({ ); if (callbacksError < 0) { + calloc.free(callbacksOptions); throw LibGit2Error(libgit2.git_error_last()); } @@ -586,6 +612,7 @@ Pointer _proxyOptionsInit(String? proxyOption) { libgit2.git_proxy_options_init(proxyOptions, GIT_PROXY_OPTIONS_VERSION); if (proxyOptionsError < 0) { + calloc.free(proxyOptions); throw LibGit2Error(libgit2.git_error_last()); } diff --git a/lib/src/bindings/repository.dart b/lib/src/bindings/repository.dart index 495a904..f956c26 100644 --- a/lib/src/bindings/repository.dart +++ b/lib/src/bindings/repository.dart @@ -21,6 +21,7 @@ Pointer open(String path) { calloc.free(pathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -40,6 +41,7 @@ Pointer openBare(String barePath) { calloc.free(barePathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -72,11 +74,15 @@ String discover({ calloc.free(ceilingDirsC); if (error == git_error_code.GIT_ENOTFOUND) { + calloc.free(out); return ''; } else if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return out.ref.ptr.cast().toDartString(); + final result = out.ref.ptr.cast().toDartString(); + calloc.free(out); + return result; } } @@ -107,6 +113,14 @@ Pointer init({ ); if (optsError < 0) { + calloc.free(out); + calloc.free(pathC); + calloc.free(workdirPathC); + calloc.free(descriptionC); + calloc.free(templatePathC); + calloc.free(initialHeadC); + calloc.free(originUrlC); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -129,6 +143,7 @@ Pointer init({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -158,6 +173,11 @@ Pointer clone({ libgit2.git_clone_options_init(cloneOptions, GIT_CLONE_OPTIONS_VERSION); if (cloneOptionsError < 0) { + calloc.free(out); + calloc.free(urlC); + calloc.free(localPathC); + calloc.free(checkoutBranchC); + calloc.free(cloneOptions); throw LibGit2Error(libgit2.git_error_last()); } @@ -166,6 +186,12 @@ Pointer clone({ libgit2.git_fetch_options_init(fetchOptions, GIT_FETCH_OPTIONS_VERSION); if (fetchOptionsError < 0) { + calloc.free(out); + calloc.free(urlC); + calloc.free(localPathC); + calloc.free(checkoutBranchC); + calloc.free(cloneOptions); + calloc.free(fetchOptions); throw LibGit2Error(libgit2.git_error_last()); } @@ -204,6 +230,7 @@ Pointer clone({ RemoteCallbacks.reset(); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -213,8 +240,7 @@ Pointer clone({ /// Returns the path to the `.git` folder for normal repositories or the /// repository itself for bare repositories. String path(Pointer repo) { - final result = libgit2.git_repository_path(repo); - return result.cast().toDartString(); + return libgit2.git_repository_path(repo).cast().toDartString(); } /// Get the path of the shared common directory for this repository. @@ -223,8 +249,7 @@ String path(Pointer repo) { /// If the repository is a worktree, it is the parent repo's `.git` folder. /// Otherwise, it is the `.git` folder. String commonDir(Pointer repo) { - final result = libgit2.git_repository_commondir(repo); - return result.cast().toDartString(); + return libgit2.git_repository_commondir(repo).cast().toDartString(); } /// Get the currently active namespace for this repository. @@ -264,8 +289,7 @@ void setNamespace({ /// Check if a repository is bare or not. bool isBare(Pointer repo) { - final result = libgit2.git_repository_is_bare(repo); - return result == 1 ? true : false; + return libgit2.git_repository_is_bare(repo) == 1 ? true : false; } /// Check if a repository is empty. @@ -295,6 +319,7 @@ Pointer head(Pointer repo) { final error = libgit2.git_repository_head(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -383,6 +408,7 @@ Pointer config(Pointer repo) { final error = libgit2.git_repository_config(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -402,6 +428,7 @@ Pointer configSnapshot(Pointer repo) { final error = libgit2.git_repository_config_snapshot(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -421,6 +448,7 @@ Pointer index(Pointer repo) { final error = libgit2.git_repository_index(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -429,14 +457,12 @@ Pointer index(Pointer repo) { /// Determine if the repository was a shallow clone. bool isShallow(Pointer repo) { - final result = libgit2.git_repository_is_shallow(repo); - return result == 1 ? true : false; + return libgit2.git_repository_is_shallow(repo) == 1 ? true : false; } /// Check if a repository is a linked work tree. bool isWorktree(Pointer repo) { - final result = libgit2.git_repository_is_worktree(repo); - return result == 1 ? true : false; + return libgit2.git_repository_is_worktree(repo) == 1 ? true : false; } /// Retrieve git's prepared message. @@ -455,9 +481,12 @@ String message(Pointer repo) { final error = libgit2.git_repository_message(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return out.ref.ptr.cast().toDartString(); + final result = out.ref.ptr.cast().toDartString(); + calloc.free(out); + return result; } } @@ -479,6 +508,7 @@ Pointer odb(Pointer repo) { final error = libgit2.git_repository_odb(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -499,6 +529,7 @@ Pointer refdb(Pointer repo) { final error = libgit2.git_repository_refdb(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -646,6 +677,7 @@ Pointer wrapODB(Pointer odb) { final error = libgit2.git_repository_wrap_odb(out, odb); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/revparse.dart b/lib/src/bindings/revparse.dart index 8c53faf..3396b0a 100644 --- a/lib/src/bindings/revparse.dart +++ b/lib/src/bindings/revparse.dart @@ -22,6 +22,7 @@ Pointer revParse({ calloc.free(specC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -47,6 +48,7 @@ Pointer revParseSingle({ calloc.free(specC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -72,7 +74,6 @@ List revParseExt({ final objectOut = calloc>(); final referenceOut = calloc>(); final specC = spec.toNativeUtf8().cast(); - var result = []; final error = libgit2.git_revparse_ext( objectOut, @@ -84,8 +85,11 @@ List revParseExt({ calloc.free(specC); if (error < 0) { + calloc.free(objectOut); + calloc.free(referenceOut); throw LibGit2Error(libgit2.git_error_last()); } else { + var result = []; result.add(objectOut.value); if (referenceOut.value != nullptr) { result.add(referenceOut.value); diff --git a/lib/src/bindings/revwalk.dart b/lib/src/bindings/revwalk.dart index 825a0b7..782089e 100644 --- a/lib/src/bindings/revwalk.dart +++ b/lib/src/bindings/revwalk.dart @@ -22,6 +22,7 @@ Pointer create(Pointer repo) { final error = libgit2.git_revwalk_new(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/signature.dart b/lib/src/bindings/signature.dart index ea186b2..51220e6 100644 --- a/lib/src/bindings/signature.dart +++ b/lib/src/bindings/signature.dart @@ -25,6 +25,7 @@ Pointer create({ calloc.free(emailC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -44,6 +45,7 @@ Pointer now({required String name, required String email}) { calloc.free(emailC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -61,6 +63,7 @@ Pointer defaultSignature(Pointer repo) { final error = libgit2.git_signature_default(out, repo); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/stash.dart b/lib/src/bindings/stash.dart index ad14e88..f2ed065 100644 --- a/lib/src/bindings/stash.dart +++ b/lib/src/bindings/stash.dart @@ -26,7 +26,10 @@ Pointer stash({ flags, ); + calloc.free(messageC); + if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; @@ -44,12 +47,14 @@ void apply({ String? directory, List? paths, }) { - final options = - calloc(sizeOf()); + final options = calloc(); final optionsError = libgit2.git_stash_apply_options_init( - options, GIT_STASH_APPLY_OPTIONS_VERSION); + options, + GIT_STASH_APPLY_OPTIONS_VERSION, + ); if (optionsError < 0) { + calloc.free(options); throw LibGit2Error(libgit2.git_error_last()); } @@ -67,16 +72,16 @@ void apply({ final error = libgit2.git_stash_apply(repoPointer, index, options); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - for (final p in pathPointers) { calloc.free(p); } calloc.free(strArray); calloc.free(optsC); calloc.free(options); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// Remove a single stashed state from the stash list. @@ -97,12 +102,14 @@ void pop({ String? directory, List? paths, }) { - final options = - calloc(sizeOf()); + final options = calloc(); final optionsError = libgit2.git_stash_apply_options_init( - options, GIT_STASH_APPLY_OPTIONS_VERSION); + options, + GIT_STASH_APPLY_OPTIONS_VERSION, + ); if (optionsError < 0) { + calloc.free(options); throw LibGit2Error(libgit2.git_error_last()); } @@ -120,16 +127,16 @@ void pop({ final error = libgit2.git_stash_pop(repoPointer, index, options); - if (error < 0) { - throw LibGit2Error(libgit2.git_error_last()); - } - for (final p in pathPointers) { calloc.free(p); } calloc.free(strArray); calloc.free(optsC); calloc.free(options); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } } /// List of stashed states. diff --git a/lib/src/bindings/status.dart b/lib/src/bindings/status.dart index b6d7bbb..0cb75ff 100644 --- a/lib/src/bindings/status.dart +++ b/lib/src/bindings/status.dart @@ -13,6 +13,7 @@ Pointer listNew(Pointer repo) { final error = libgit2.git_status_list_new(out, repo, nullptr); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -68,6 +69,7 @@ int file({required Pointer repoPointer, required String path}) { calloc.free(pathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { final result = out.value; diff --git a/lib/src/bindings/submodule.dart b/lib/src/bindings/submodule.dart index 8850b73..f356db9 100644 --- a/lib/src/bindings/submodule.dart +++ b/lib/src/bindings/submodule.dart @@ -62,6 +62,7 @@ Pointer lookup({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -113,6 +114,7 @@ void update({ ); if (optionsError < 0) { + calloc.free(options); throw LibGit2Error(libgit2.git_error_last()); } @@ -144,6 +146,7 @@ Pointer open(Pointer submodule) { final error = libgit2.git_submodule_open(out, submodule); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -180,6 +183,7 @@ Pointer addSetup({ calloc.free(pathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -201,6 +205,8 @@ void clone({ ); if (optionsError < 0) { + calloc.free(out); + calloc.free(options); throw LibGit2Error(libgit2.git_error_last()); } @@ -254,9 +260,12 @@ int status({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { - return out.value; + final result = out.value; + calloc.free(out); + return result; } } @@ -296,8 +305,7 @@ void reload({ /// Get the name of submodule. String name(Pointer submodule) { - final result = libgit2.git_submodule_name(submodule); - return result.cast().toDartString(); + return libgit2.git_submodule_name(submodule).cast().toDartString(); } /// Get the path to the submodule. @@ -305,14 +313,12 @@ String name(Pointer submodule) { /// The path is almost always the same as the submodule name, but the two /// are actually not required to match. String path(Pointer submodule) { - final result = libgit2.git_submodule_path(submodule); - return result.cast().toDartString(); + return libgit2.git_submodule_path(submodule).cast().toDartString(); } /// Get the URL for the submodule. String url(Pointer submodule) { - final result = libgit2.git_submodule_url(submodule); - return result.cast().toDartString(); + return libgit2.git_submodule_url(submodule).cast().toDartString(); } /// Set the URL for the submodule in the configuration. diff --git a/lib/src/bindings/tag.dart b/lib/src/bindings/tag.dart index e274ec8..b02f843 100644 --- a/lib/src/bindings/tag.dart +++ b/lib/src/bindings/tag.dart @@ -14,6 +14,7 @@ List list(Pointer repo) { var result = []; if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { for (var i = 0; i < out.ref.count; i++) { @@ -35,6 +36,7 @@ Pointer lookup({ final error = libgit2.git_tag_lookup(out, repoPointer, oidPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -51,6 +53,7 @@ Pointer target(Pointer tag) { final error = libgit2.git_tag_target(out, tag); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -113,6 +116,7 @@ Pointer create({ calloc.free(messageC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; diff --git a/lib/src/bindings/tree.dart b/lib/src/bindings/tree.dart index b2c7df9..d71be43 100644 --- a/lib/src/bindings/tree.dart +++ b/lib/src/bindings/tree.dart @@ -18,6 +18,7 @@ Pointer lookup({ final error = libgit2.git_tree_lookup(out, repoPointer, oidPointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -86,6 +87,7 @@ Pointer getByPath({ calloc.free(pathC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; diff --git a/lib/src/bindings/treebuilder.dart b/lib/src/bindings/treebuilder.dart index 0c18b47..1829827 100644 --- a/lib/src/bindings/treebuilder.dart +++ b/lib/src/bindings/treebuilder.dart @@ -24,6 +24,7 @@ Pointer create({ final error = libgit2.git_treebuilder_new(out, repoPointer, sourcePointer); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -38,6 +39,7 @@ Pointer write(Pointer bld) { final error = libgit2.git_treebuilder_write(out, bld); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out; diff --git a/lib/src/bindings/worktree.dart b/lib/src/bindings/worktree.dart index 661817b..d80e131 100644 --- a/lib/src/bindings/worktree.dart +++ b/lib/src/bindings/worktree.dart @@ -28,6 +28,10 @@ Pointer create({ ); if (optsError < 0) { + calloc.free(out); + calloc.free(nameC); + calloc.free(pathC); + calloc.free(opts); throw LibGit2Error(libgit2.git_error_last()); } @@ -43,6 +47,7 @@ Pointer create({ calloc.free(opts); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -63,6 +68,7 @@ Pointer lookup({ calloc.free(nameC); if (error < 0) { + calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { return out.value; @@ -88,12 +94,12 @@ void prune(Pointer wt) { List list(Pointer repo) { final out = calloc(); final error = libgit2.git_worktree_list(out, repo); - final result = []; if (error < 0) { calloc.free(out); throw LibGit2Error(libgit2.git_error_last()); } else { + final result = []; for (var i = 0; i < out.ref.count; i++) { result.add(out.ref.strings[i].cast().toDartString()); } diff --git a/lib/src/blame.dart b/lib/src/blame.dart index 21e1e99..4dbb495 100644 --- a/lib/src/blame.dart +++ b/lib/src/blame.dart @@ -4,7 +4,6 @@ import 'package:ffi/ffi.dart'; import 'package:libgit2dart/libgit2dart.dart'; import 'bindings/libgit2_bindings.dart'; import 'bindings/blame.dart' as bindings; -import 'util.dart'; class Blame with IterableMixin { /// Initializes a new instance of the [Blame] class from @@ -44,14 +43,10 @@ class Blame with IterableMixin { int? minLine, int? maxLine, }) { - libgit2.git_libgit2_init(); - - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - _blamePointer = bindings.file( repoPointer: repo.pointer, path: path, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), minMatchCharacters: minMatchCharacters, newestCommit: newestCommit, oldestCommit: oldestCommit, diff --git a/lib/src/blob.dart b/lib/src/blob.dart index fc5e972..af352a7 100644 --- a/lib/src/blob.dart +++ b/lib/src/blob.dart @@ -87,14 +87,12 @@ class Blob { int contextLines = 3, int interhunkLines = 0, }) { - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - final result = patch_bindings.fromBlobs( oldBlobPointer: _blobPointer, oldAsPath: oldAsPath, newBlobPointer: newBlob?.pointer, newAsPath: newAsPath, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), contextLines: contextLines, interhunkLines: interhunkLines, ); @@ -115,14 +113,12 @@ class Blob { int contextLines = 3, int interhunkLines = 0, }) { - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - final result = patch_bindings.fromBlobAndBuffer( oldBlobPointer: _blobPointer, oldAsPath: oldAsPath, buffer: buffer, bufferAsPath: bufferAsPath, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), contextLines: contextLines, interhunkLines: interhunkLines, ); diff --git a/lib/src/branch.dart b/lib/src/branch.dart index 6324e67..65ebab4 100644 --- a/lib/src/branch.dart +++ b/lib/src/branch.dart @@ -52,15 +52,14 @@ class Branch { name: name, ), ); - late final GitBranch type; - ref.isBranch ? type = GitBranch.local : GitBranch.remote; - ref.free(); _branchPointer = bindings.lookup( repoPointer: repo.pointer, branchName: name, - branchType: type.value, + branchType: ref.isBranch ? GitBranch.local.value : GitBranch.remote.value, ); + + ref.free(); } late final Pointer _branchPointer; @@ -93,6 +92,7 @@ class Branch { static void delete({required Repository repo, required String name}) { final branch = Branch.lookup(repo: repo, name: name); bindings.delete(branch.pointer); + branch.free(); } /// Renames an existing local branch reference. diff --git a/lib/src/commit.dart b/lib/src/commit.dart index 619351c..279ba58 100644 --- a/lib/src/commit.dart +++ b/lib/src/commit.dart @@ -45,8 +45,6 @@ class Commit { String? updateRef, String? messageEncoding, }) { - final parentsPointers = parents.map((parent) => parent.pointer).toList(); - return Oid(bindings.create( repoPointer: repo.pointer, updateRef: updateRef, @@ -56,7 +54,7 @@ class Commit { message: message, treePointer: tree.pointer, parentCount: parents.length, - parents: parentsPointers, + parents: parents.map((e) => e.pointer).toList(), )); } @@ -101,9 +99,10 @@ class Commit { /// Get the tree pointed to by a commit. Tree get tree { - final repo = bindings.owner(_commitPointer); - final oid = bindings.tree(_commitPointer); - return Tree(tree_bindings.lookup(repoPointer: repo, oidPointer: oid)); + return Tree(tree_bindings.lookup( + repoPointer: bindings.owner(_commitPointer), + oidPointer: bindings.tree(_commitPointer), + )); } /// Releases memory allocated for commit object. diff --git a/lib/src/diff.dart b/lib/src/diff.dart index 8944fbe..2e62434 100644 --- a/lib/src/diff.dart +++ b/lib/src/diff.dart @@ -96,11 +96,9 @@ class Diff { int breakRewriteThreshold = 60, int renameLimit = 200, }) { - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - bindings.findSimilar( diffPointer: _diffPointer, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), renameThreshold: renameThreshold, copyThreshold: copyThreshold, renameFromRewriteThreshold: renameFromRewriteThreshold, @@ -229,11 +227,9 @@ class DiffStats { /// /// Throws a [LibGit2Error] if error occured. String print({required Set format, required int width}) { - final int formatInt = format.fold(0, (acc, e) => acc | e.value); - return bindings.statsPrint( statsPointer: _diffStatsPointer, - format: formatInt, + format: format.fold(0, (acc, e) => acc | e.value), width: width, ); } diff --git a/lib/src/index.dart b/lib/src/index.dart index 7aace5c..6da34fc 100644 --- a/lib/src/index.dart +++ b/lib/src/index.dart @@ -189,13 +189,10 @@ class Index with IterableMixin { int contextLines = 3, int interhunkLines = 0, }) { - final repo = bindings.owner(_indexPointer); - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - return Diff(diff_bindings.indexToWorkdir( - repoPointer: repo, + repoPointer: bindings.owner(_indexPointer), indexPointer: _indexPointer, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), contextLines: contextLines, interhunkLines: interhunkLines, )); @@ -210,14 +207,11 @@ class Index with IterableMixin { int contextLines = 3, int interhunkLines = 0, }) { - final repo = bindings.owner(_indexPointer); - final int flagsInt = flags.fold(0, (acc, e) => acc | e.value); - return Diff(diff_bindings.treeToIndex( - repoPointer: repo, + repoPointer: bindings.owner(_indexPointer), treePointer: tree.pointer, indexPointer: _indexPointer, - flags: flagsInt, + flags: flags.fold(0, (acc, e) => acc | e.value), contextLines: contextLines, interhunkLines: interhunkLines, )); @@ -255,8 +249,9 @@ class IndexEntry { /// Returns the UNIX file attributes of a index entry. GitFilemode get mode { - final modeInt = _indexEntryPointer.ref.mode; - return GitFilemode.values.singleWhere((mode) => modeInt == mode.value); + return GitFilemode.values.singleWhere( + (mode) => _indexEntryPointer.ref.mode == mode.value, + ); } /// Sets the UNIX file attributes of a index entry. diff --git a/lib/src/reference.dart b/lib/src/reference.dart index b08ebdc..b961084 100644 --- a/lib/src/reference.dart +++ b/lib/src/reference.dart @@ -241,8 +241,10 @@ class Reference { /// /// Throws a [LibGit2Error] if error occured. bool get hasLog { - final owner = bindings.owner(_refPointer); - return bindings.hasLog(repoPointer: owner, name: name); + return bindings.hasLog( + repoPointer: bindings.owner(_refPointer), + name: name, + ); } /// Returns a [RefLog] object. diff --git a/lib/src/reflog.dart b/lib/src/reflog.dart index 63d7b46..2e3ec27 100644 --- a/lib/src/reflog.dart +++ b/lib/src/reflog.dart @@ -9,9 +9,10 @@ class RefLog with IterableMixin { /// /// Throws a [LibGit2Error] if error occured. RefLog(Reference ref) { - final repo = ref.owner; - final name = ref.name; - _reflogPointer = bindings.read(repoPointer: repo.pointer, name: name); + _reflogPointer = bindings.read( + repoPointer: ref.owner.pointer, + name: ref.name, + ); } /// Pointer to memory address for allocated reflog object. diff --git a/lib/src/repository.dart b/lib/src/repository.dart index a22d0f1..212ff98 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -207,13 +207,10 @@ class Repository { /// /// Throws a [LibGit2Error] if error occured. void setHead(String target) { - late final Oid oid; - if (isValidShaHex(target)) { - oid = Oid.fromSHA(repo: this, sha: target); bindings.setHeadDetached( repoPointer: _repoPointer, - commitishPointer: oid.pointer, + commitishPointer: this[target].pointer, ); } else { bindings.setHead(repoPointer: _repoPointer, refname: target); @@ -450,11 +447,10 @@ class Repository { required String sha, Set sorting = const {GitSort.none}, }) { - final oid = Oid.fromSHA(repo: this, sha: sha); final walker = RevWalk(this); walker.sorting(sorting); - walker.push(oid); + walker.push(this[sha]); final result = walker.walk(); walker.free(); @@ -759,12 +755,10 @@ class Repository { /// /// Throws a [LibGit2Error] if error occured. Oid mergeBase({required String a, required String b}) { - final oidA = Oid.fromSHA(repo: this, sha: a); - final oidB = Oid.fromSHA(repo: this, sha: b); return Oid(merge_bindings.mergeBase( repoPointer: _repoPointer, - aPointer: oidA.pointer, - bPointer: oidB.pointer, + aPointer: this[a].pointer, + bPointer: this[b].pointer, )); } @@ -994,10 +988,9 @@ class Repository { /// /// Throws a [LibGit2Error] if error occured. void reset({required String target, required GitReset resetType}) { - final oid = Oid.fromSHA(repo: this, sha: target); final object = object_bindings.lookup( repoPointer: _repoPointer, - oidPointer: oid.pointer, + oidPointer: this[target].pointer, type: GitObject.any.value, ); @@ -1375,13 +1368,10 @@ class Repository { /// /// Throws a [LibGit2Error] if error occured. bool descendantOf({required String commitSHA, required String ancestorSHA}) { - final commit = Oid.fromSHA(repo: this, sha: commitSHA); - final ancestor = Oid.fromSHA(repo: this, sha: ancestorSHA); - return graph_bindings.descendantOf( repoPointer: _repoPointer, - commitPointer: commit.pointer, - ancestorPointer: ancestor.pointer, + commitPointer: this[commitSHA].pointer, + ancestorPointer: this[ancestorSHA].pointer, ); } @@ -1394,13 +1384,10 @@ class Repository { required String localSHA, required String upstreamSHA, }) { - final local = Oid.fromSHA(repo: this, sha: localSHA); - final upstream = Oid.fromSHA(repo: this, sha: upstreamSHA); - return graph_bindings.aheadBehind( repoPointer: _repoPointer, - localPointer: local.pointer, - upstreamPointer: upstream.pointer, + localPointer: this[localSHA].pointer, + upstreamPointer: this[upstreamSHA].pointer, ); } diff --git a/lib/src/revwalk.dart b/lib/src/revwalk.dart index 4ab5f69..d6a7a9b 100644 --- a/lib/src/revwalk.dart +++ b/lib/src/revwalk.dart @@ -17,10 +17,8 @@ class RevWalk { /// /// Default sorting is reverse chronological order (default in git). List walk() { - final repoPointer = bindings.repository(_revWalkPointer); - final pointers = bindings.walk( - repoPointer: repoPointer, + repoPointer: bindings.repository(_revWalkPointer), walkerPointer: _revWalkPointer, ); diff --git a/lib/src/submodule.dart b/lib/src/submodule.dart index 78a6811..33ee694 100644 --- a/lib/src/submodule.dart +++ b/lib/src/submodule.dart @@ -140,10 +140,8 @@ class Submodule { Set status({ GitSubmoduleIgnore ignore = GitSubmoduleIgnore.unspecified, }) { - final repo = bindings.owner(_submodulePointer); - final resultInt = bindings.status( - repoPointer: repo, + repoPointer: bindings.owner(_submodulePointer), name: name, ignore: ignore.value, ); @@ -192,8 +190,11 @@ class Submodule { /// /// Throws a [LibGit2Error] if error occured. set url(String url) { - final repo = bindings.owner(_submodulePointer); - bindings.setUrl(repoPointer: repo, name: name, url: url); + bindings.setUrl( + repoPointer: bindings.owner(_submodulePointer), + name: name, + url: url, + ); } /// Returns the branch for the submodule. @@ -206,8 +207,11 @@ class Submodule { /// /// Throws a [LibGit2Error] if error occured. set branch(String branch) { - final repo = bindings.owner(_submodulePointer); - bindings.setBranch(repoPointer: repo, name: name, branch: branch); + bindings.setBranch( + repoPointer: bindings.owner(_submodulePointer), + name: name, + branch: branch, + ); } /// Returns the [Oid] for the submodule in the current HEAD tree or @@ -266,8 +270,11 @@ class Submodule { /// /// Throws a [LibGit2Error] if error occured. set updateRule(GitSubmoduleUpdate rule) { - final repo = bindings.owner(_submodulePointer); - bindings.setUpdateRule(repoPointer: repo, name: name, update: rule.value); + bindings.setUpdateRule( + repoPointer: bindings.owner(_submodulePointer), + name: name, + update: rule.value, + ); } /// Releases memory allocated for submodule object. diff --git a/lib/src/tag.dart b/lib/src/tag.dart index 7402103..c015283 100644 --- a/lib/src/tag.dart +++ b/lib/src/tag.dart @@ -52,6 +52,7 @@ class Tag { oidPointer: target.pointer, type: targetType.value, ); + final result = bindings.create( repoPointer: repo.pointer, tagName: tagName, @@ -62,6 +63,7 @@ class Tag { ); object_bindings.free(object); + return Oid(result); }