mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 12:49:08 -04:00
feat(commit): add ability to create commit with different number of parents
This commit is contained in:
parent
c90561ed8f
commit
94b4116adf
15 changed files with 217 additions and 260 deletions
|
@ -58,19 +58,19 @@ Pointer<git_oid> create(
|
|||
final messageEncodingC =
|
||||
messageEncoding?.toNativeUtf8().cast<Int8>() ?? nullptr;
|
||||
final messageC = message.toNativeUtf8().cast<Int8>();
|
||||
late final Pointer<Pointer<git_commit>> parentsC;
|
||||
Pointer<Pointer<git_commit>> parentsC =
|
||||
calloc.call<Pointer<git_commit>>(parentCount);
|
||||
|
||||
if (parents.isNotEmpty) {
|
||||
parentsC = calloc(parentCount);
|
||||
|
||||
for (var i = 0; i < parentCount; i++) {
|
||||
final oid = oid_bindings.fromSHA(parents[i]);
|
||||
final commit = lookup(repo, oid);
|
||||
parentsC[i] = commit;
|
||||
var commit = calloc<IntPtr>();
|
||||
commit = lookup(repo, oid).cast();
|
||||
parentsC[i] = commit.cast();
|
||||
}
|
||||
} else {
|
||||
throw UnimplementedError(
|
||||
'Writing commit without parents is not implemented');
|
||||
final commit = calloc<IntPtr>();
|
||||
parentsC[0] = commit.cast();
|
||||
}
|
||||
|
||||
final error = libgit2.git_commit_create(
|
||||
|
@ -89,6 +89,9 @@ Pointer<git_oid> create(
|
|||
calloc.free(updateRefC);
|
||||
calloc.free(messageEncodingC);
|
||||
calloc.free(messageC);
|
||||
for (var i = 0; i < parentCount; i++) {
|
||||
free(parentsC[i]);
|
||||
}
|
||||
calloc.free(parentsC);
|
||||
|
||||
if (error < 0) {
|
||||
|
@ -134,7 +137,7 @@ int parentCount(Pointer<git_commit> commit) =>
|
|||
Pointer<git_oid> parentId(Pointer<git_commit> commit, int n) {
|
||||
final parentOid = libgit2.git_commit_parent_id(commit, n);
|
||||
|
||||
if (parentOid is int && parentOid as int < 0) {
|
||||
if (parentOid == nullptr) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return parentOid;
|
||||
|
@ -163,8 +166,5 @@ Pointer<git_oid> tree(Pointer<git_commit> commit) {
|
|||
Pointer<git_repository> owner(Pointer<git_commit> commit) =>
|
||||
libgit2.git_commit_owner(commit);
|
||||
|
||||
/// Close an open commit.
|
||||
///
|
||||
/// IMPORTANT: It is necessary to call this method when you stop using a commit.
|
||||
/// Failure to do so will cause a memory leak.
|
||||
/// Close an open commit to release memory.
|
||||
void free(Pointer<git_commit> commit) => libgit2.git_commit_free(commit);
|
||||
|
|
|
@ -38,6 +38,26 @@ Pointer<git_oid> fromSHA(String hex) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Copy an already raw oid into a git_oid structure.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_oid> fromRaw(Array<Uint8> raw) {
|
||||
final out = calloc<git_oid>();
|
||||
var rawC = calloc<Uint8>(20);
|
||||
for (var i = 0; i < 20; i++) {
|
||||
rawC[i] = raw[i];
|
||||
}
|
||||
final error = libgit2.git_oid_fromraw(out, rawC);
|
||||
|
||||
calloc.free(rawC);
|
||||
|
||||
if (error < 0) {
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/// Format a git_oid into a hex string.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
import 'dart:ffi';
|
||||
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/commit.dart' as bindings;
|
||||
import 'bindings/oid.dart' as oid_bindings;
|
||||
import 'bindings/tree.dart' as tree_bindings;
|
||||
import 'repository.dart';
|
||||
import 'oid.dart';
|
||||
import 'signature.dart';
|
||||
|
@ -38,30 +39,13 @@ class Commit {
|
|||
required Signature author,
|
||||
required Signature commiter,
|
||||
required String treeSHA,
|
||||
required List<String> parentsSHA,
|
||||
required List<String> parents,
|
||||
String? updateRef,
|
||||
String? messageEncoding,
|
||||
}) {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
final parentCount = parentsSHA.length;
|
||||
late final Tree tree;
|
||||
|
||||
if (treeSHA.length == 40) {
|
||||
final treeOid = Oid.fromSHA(treeSHA);
|
||||
tree = Tree.lookup(
|
||||
repo.pointer,
|
||||
treeOid.pointer,
|
||||
);
|
||||
} else {
|
||||
final odb = repo.odb;
|
||||
final treeOid = Oid.fromShortSHA(treeSHA, odb);
|
||||
tree = Tree.lookup(
|
||||
repo.pointer,
|
||||
treeOid.pointer,
|
||||
);
|
||||
odb.free();
|
||||
}
|
||||
final treeOid = oid_bindings.fromStrN(treeSHA);
|
||||
final tree =
|
||||
Tree(tree_bindings.lookupPrefix(repo.pointer, treeOid, treeSHA.length));
|
||||
|
||||
final result = Oid(bindings.create(
|
||||
repo.pointer,
|
||||
|
@ -71,12 +55,11 @@ class Commit {
|
|||
messageEncoding,
|
||||
message,
|
||||
tree.pointer,
|
||||
parentCount,
|
||||
parentsSHA,
|
||||
parents.length,
|
||||
parents,
|
||||
));
|
||||
|
||||
tree.free();
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
@ -105,18 +88,13 @@ class Commit {
|
|||
/// Returns list of parent commits.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
List<Commit> get parents {
|
||||
var parents = <Commit>[];
|
||||
List<Oid> get parents {
|
||||
var parents = <Oid>[];
|
||||
final parentCount = bindings.parentCount(_commitPointer);
|
||||
|
||||
for (var i = 0; i < parentCount; i++) {
|
||||
final parentOid = bindings.parentId(_commitPointer, i);
|
||||
|
||||
if (parentOid != nullptr) {
|
||||
final owner = bindings.owner(_commitPointer);
|
||||
final commit = bindings.lookup(owner, parentOid);
|
||||
parents.add(Commit(commit));
|
||||
}
|
||||
parents.add(Oid(parentOid));
|
||||
}
|
||||
|
||||
return parents;
|
||||
|
@ -128,6 +106,5 @@ class Commit {
|
|||
/// Releases memory allocated for commit object.
|
||||
void free() {
|
||||
bindings.free(_commitPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -142,6 +142,5 @@ class Config {
|
|||
/// Releases memory allocated for config object.
|
||||
void free() {
|
||||
bindings.free(_configPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,10 +3,9 @@ import 'package:ffi/ffi.dart';
|
|||
import 'package:libgit2dart/src/tree.dart';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/index.dart' as bindings;
|
||||
import 'bindings/repository.dart' as repo_bindings;
|
||||
import 'odb.dart';
|
||||
import 'oid.dart';
|
||||
import 'enums.dart';
|
||||
import 'repository.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class Index {
|
||||
|
@ -101,15 +100,9 @@ class Index {
|
|||
} else if (target is Tree) {
|
||||
tree = target;
|
||||
} else if (isValidShaHex(target as String)) {
|
||||
if (target.length == 40) {
|
||||
oid = Oid.fromSHA(target);
|
||||
tree = Tree.lookup(bindings.owner(_indexPointer), oid.pointer);
|
||||
} else {
|
||||
final odb = Odb(repo_bindings.odb(bindings.owner(_indexPointer)));
|
||||
oid = Oid.fromShortSHA(target, odb);
|
||||
odb.free();
|
||||
tree = Tree.lookup(bindings.owner(_indexPointer), oid.pointer);
|
||||
}
|
||||
final repo = Repository(bindings.owner(_indexPointer));
|
||||
oid = Oid.fromSHA(repo, target);
|
||||
tree = Tree.lookup(repo.pointer, oid.pointer);
|
||||
} else {
|
||||
throw ArgumentError.value(
|
||||
'$target should be either Oid object, SHA hex string or Tree object');
|
||||
|
@ -146,7 +139,6 @@ class Index {
|
|||
/// Releases memory allocated for index object.
|
||||
void free() {
|
||||
bindings.free(_indexPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -158,7 +150,7 @@ class IndexEntry {
|
|||
late final Pointer<git_index_entry> _indexEntryPointer;
|
||||
|
||||
/// Unique identity of the index entry.
|
||||
Oid get id => Oid.fromSHA(sha);
|
||||
Oid get id => Oid.fromRaw(_indexEntryPointer.ref.id);
|
||||
|
||||
set id(Oid oid) => _indexEntryPointer.ref.id = oid.pointer.ref;
|
||||
|
||||
|
|
|
@ -10,9 +10,11 @@ class Odb {
|
|||
libgit2.git_libgit2_init();
|
||||
}
|
||||
|
||||
/// Pointer to memory address for allocated oid object.
|
||||
late final Pointer<git_odb> _odbPointer;
|
||||
|
||||
/// Pointer to memory address for allocated oid object.
|
||||
Pointer<git_odb> get pointer => _odbPointer;
|
||||
|
||||
/// Determine if an object can be found in the object database by an abbreviated object ID.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
|
@ -26,6 +28,5 @@ class Odb {
|
|||
/// Releases memory allocated for odb object.
|
||||
void free() {
|
||||
bindings.free(_odbPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'dart:ffi';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/oid.dart' as bindings;
|
||||
import 'odb.dart';
|
||||
import 'repository.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class Oid {
|
||||
|
@ -11,24 +11,26 @@ class Oid {
|
|||
libgit2.git_libgit2_init();
|
||||
}
|
||||
|
||||
/// Initializes a new instance of [Oid] class from provided
|
||||
/// hexadecimal [sha] string.
|
||||
/// Initializes a new instance of [Oid] class by determining if an object can be found
|
||||
/// in the ODB of [repository] with provided hexadecimal [sha] string that is 40 characters
|
||||
/// long or shorter.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Oid.fromSHA(String sha) {
|
||||
Oid.fromSHA(Repository repository, String sha) {
|
||||
libgit2.git_libgit2_init();
|
||||
|
||||
_oidPointer = bindings.fromSHA(sha);
|
||||
if (sha.length == 40) {
|
||||
_oidPointer = bindings.fromSHA(sha);
|
||||
} else {
|
||||
final odb = repository.odb;
|
||||
_oidPointer = odb.existsPrefix(bindings.fromStrN(sha), sha.length);
|
||||
odb.free();
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes a new instance of [Oid] class by determining if an object can be found
|
||||
/// in the object database of repository with provided hexadecimal [sha] string that is
|
||||
/// lesser than 40 characters long and [Odb] object.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Oid.fromShortSHA(String sha, Odb odb) {
|
||||
/// Initializes a new instance of [Oid] class from provided raw git_oid.
|
||||
Oid.fromRaw(git_oid raw) {
|
||||
libgit2.git_libgit2_init();
|
||||
_oidPointer = odb.existsPrefix(bindings.fromStrN(sha), sha.length);
|
||||
_oidPointer = bindings.fromRaw(raw.id);
|
||||
}
|
||||
|
||||
late final Pointer<git_oid> _oidPointer;
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
import 'dart:ffi';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/reference.dart' as bindings;
|
||||
import 'bindings/repository.dart' as repo_bindings;
|
||||
import 'odb.dart';
|
||||
import 'oid.dart';
|
||||
import 'reflog.dart';
|
||||
import 'enums.dart';
|
||||
import 'repository.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class References {
|
||||
|
@ -139,13 +138,8 @@ class Reference {
|
|||
late final Oid oid;
|
||||
|
||||
if (isValidShaHex(target)) {
|
||||
if (target.length == 40) {
|
||||
oid = Oid.fromSHA(target);
|
||||
} else {
|
||||
final odb = Odb(repo_bindings.odb(owner));
|
||||
oid = Oid.fromShortSHA(target, odb);
|
||||
odb.free();
|
||||
}
|
||||
final repo = Repository(bindings.owner(_refPointer));
|
||||
oid = Oid.fromSHA(repo, target);
|
||||
} else {
|
||||
final ref = Reference(
|
||||
_repoPointer,
|
||||
|
@ -233,6 +227,5 @@ class Reference {
|
|||
/// Releases memory allocated for reference object.
|
||||
void free() {
|
||||
bindings.free(_refPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,6 @@ class RefLog {
|
|||
/// Releases memory allocated for reflog object.
|
||||
void free() {
|
||||
bindings.free(_reflogPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,14 @@ import 'bindings/repository.dart' as bindings;
|
|||
import 'util.dart';
|
||||
|
||||
class Repository {
|
||||
/// Initializes a new instance of the [Repository] class from provided
|
||||
/// pointer to repository object in memory.
|
||||
///
|
||||
/// Should be freed with `free()` to release allocated memory.
|
||||
Repository(this._repoPointer) {
|
||||
libgit2.git_libgit2_init();
|
||||
}
|
||||
|
||||
/// Initializes a new instance of the [Repository] class by creating a new
|
||||
/// Git repository in the given folder.
|
||||
///
|
||||
|
@ -119,13 +127,7 @@ class Repository {
|
|||
late final Oid oid;
|
||||
|
||||
if (isValidShaHex(target)) {
|
||||
if (target.length == 40) {
|
||||
oid = Oid.fromSHA(target);
|
||||
} else {
|
||||
final odb = this.odb;
|
||||
oid = Oid.fromShortSHA(target, odb);
|
||||
odb.free();
|
||||
}
|
||||
oid = Oid.fromSHA(this, target);
|
||||
bindings.setHeadDetached(_repoPointer, oid.pointer);
|
||||
} else {
|
||||
bindings.setHead(_repoPointer, target);
|
||||
|
@ -208,7 +210,6 @@ class Repository {
|
|||
/// Releases memory allocated for repository object.
|
||||
void free() {
|
||||
bindings.free(_repoPointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
|
||||
/// Returns the configuration file for this repository.
|
||||
|
@ -269,7 +270,7 @@ class Repository {
|
|||
oid = target as Oid;
|
||||
isDirect = true;
|
||||
} else if (isValidShaHex(target as String)) {
|
||||
oid = _getOid(target);
|
||||
oid = Oid.fromSHA(this, target);
|
||||
isDirect = true;
|
||||
} else {
|
||||
isDirect = false;
|
||||
|
@ -313,24 +314,10 @@ class Repository {
|
|||
late final Oid oid;
|
||||
|
||||
if (isValidShaHex(sha)) {
|
||||
oid = _getOid(sha);
|
||||
oid = Oid.fromSHA(this, sha);
|
||||
} else {
|
||||
throw ArgumentError.value('$sha is not a valid sha hex string');
|
||||
}
|
||||
return Commit.lookup(_repoPointer, oid.pointer);
|
||||
}
|
||||
|
||||
/// Returns [Oid] for provided sha hex that is 40 characters long or less.
|
||||
Oid _getOid(String sha) {
|
||||
late final Oid oid;
|
||||
|
||||
if (sha.length == 40) {
|
||||
oid = Oid.fromSHA(sha);
|
||||
} else {
|
||||
final odb = this.odb;
|
||||
oid = Oid.fromShortSHA(sha, odb);
|
||||
odb.free();
|
||||
}
|
||||
return oid;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -68,6 +68,5 @@ class Signature {
|
|||
/// Releases memory allocated for signature object.
|
||||
void free() {
|
||||
bindings.free(_signaturePointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -27,6 +27,5 @@ class Tree {
|
|||
/// Releases memory allocated for tree object.
|
||||
void free() {
|
||||
bindings.free(_treePointer);
|
||||
libgit2.git_libgit2_shutdown();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue