feat(repository)!: add more aliases for api methods

BREAKING CHANGE: Make repository entry point for most operations
This commit is contained in:
Aleksey Kulikov 2021-10-11 20:06:36 +03:00
parent 9205a3ad82
commit 3a0fa75929
51 changed files with 1380 additions and 1062 deletions

View file

@ -4,134 +4,131 @@ import 'bindings/libgit2_bindings.dart';
import 'bindings/branch.dart' as bindings;
import 'bindings/reference.dart' as reference_bindings;
class Branches {
/// Initializes a new instance of the [Branches] class
/// from provided [Repository] object.
Branches(Repository repo) {
_repoPointer = repo.pointer;
}
/// Pointer to memory address for allocated repository object.
late final Pointer<git_repository> _repoPointer;
/// Returns a list of all branches that can be found in a repository.
class Branch {
/// Initializes a new instance of [Branch] class from provided pointer to
/// branch object in memory.
///
/// Throws a [LibGit2Error] if error occured.
List<String> list() {
return bindings.list(
repoPointer: _repoPointer,
flags: GitBranch.all.value,
);
}
/// Returns a list of local branches that can be found in a repository.
///
/// Throws a [LibGit2Error] if error occured.
List<String> get local {
return bindings.list(
repoPointer: _repoPointer,
flags: GitBranch.local.value,
);
}
/// Returns a list of remote branches that can be found in a repository.
///
/// Throws a [LibGit2Error] if error occured.
List<String> get remote {
return bindings.list(
repoPointer: _repoPointer,
flags: GitBranch.remote.value,
);
}
/// Lookups a branch by its name in a repository.
///
/// The generated reference must be freed. The branch name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
Branch operator [](String branchName) {
final ref = Reference(
reference_bindings.lookupDWIM(
repoPointer: _repoPointer,
name: branchName,
),
);
late final GitBranch type;
ref.isBranch ? type = GitBranch.local : GitBranch.remote;
ref.free();
return Branch(bindings.lookup(
repoPointer: _repoPointer,
branchName: branchName,
branchType: type.value,
));
}
/// Should be freed with [free] to release allocated memory when no longer
/// needed.
Branch(this._branchPointer);
/// Creates a new branch pointing at a [target] commit.
///
/// A new direct reference will be created pointing to this target commit.
/// If [force] is true and a reference already exists with the given name, it'll be replaced.
///
/// The returned reference must be freed.
/// Should be freed with [free] to release allocated memory when no longer
/// needed.
///
/// The branch name will be checked for validity.
///
/// Throws a [LibGit2Error] if error occured.
Reference create({
Branch.create({
required Repository repo,
required String name,
required Commit target,
bool force = false,
}) {
return Reference(bindings.create(
repoPointer: _repoPointer,
_branchPointer = bindings.create(
repoPointer: repo.pointer,
branchName: name,
targetPointer: target.pointer,
force: force,
));
);
}
}
class Branch {
/// Initializes a new instance of [Branch] class from provided pointer to
/// branch object in memory.
/// Lookups a branch by its [name] in a [repo]sitory.
///
/// Should be freed with `free()` to release allocated memory.
const Branch(this._branchPointer);
/// The branch name will be checked for validity.
///
/// Should be freed with [free] to release allocated memory when no longer
/// needed.
///
/// Throws a [LibGit2Error] if error occured.
Branch.lookup({required Repository repo, required String name}) {
final ref = Reference(
reference_bindings.lookupDWIM(
repoPointer: repo.pointer,
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,
);
}
late final Pointer<git_reference> _branchPointer;
/// Pointer to memory address for allocated branch object.
final Pointer<git_reference> _branchPointer;
Pointer<git_reference> get pointer => _branchPointer;
/// Returns the OID pointed to by a branch.
/// Returns a list of branches that can be found in a [repo]sitory for provided [type].
/// Default is all branches (local and remote).
///
/// Throws an exception if error occured.
Oid get target => Oid(reference_bindings.target(_branchPointer));
/// IMPORTANT: Branches must be freed manually when no longer needed to prevent
/// memory leak.
///
/// Throws a [LibGit2Error] if error occured.
static List<Branch> list({
required Repository repo,
GitBranch type = GitBranch.all,
}) {
final pointers = bindings.list(
repoPointer: repo.pointer,
flags: type.value,
);
final result = <Branch>[];
for (var pointer in pointers) {
result.add(Branch(pointer));
}
return result;
}
/// Deletes 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() => bindings.delete(_branchPointer);
static void delete({required Repository repo, required String name}) {
final branch = Branch.lookup(repo: repo, name: name);
bindings.delete(branch.pointer);
}
/// Renames an existing local branch reference.
///
/// The new branch name will be checked for validity.
///
/// Note that if the move succeeds, the old reference object will not be valid anymore,
/// and will be freed immediately.
///
/// If [force] is true, existing branch will be overwritten.
///
/// Throws a [LibGit2Error] if error occured.
Branch rename({required String newName, bool force = false}) {
return Branch(bindings.rename(
branchPointer: _branchPointer,
static void rename({
required Repository repo,
required String oldName,
required String newName,
bool force = false,
}) {
final branch = Branch.lookup(repo: repo, name: oldName);
bindings.rename(
branchPointer: branch.pointer,
newBranchName: newName,
force: force,
));
);
branch.free();
}
/// Returns the OID pointed to by a branch.
///
/// Throws an exception if error occured.
Oid get target => Oid(reference_bindings.target(_branchPointer));
/// Checks if HEAD points to the given branch.
///
/// Throws a [LibGit2Error] if error occured.
@ -155,4 +152,9 @@ class Branch {
/// Releases memory allocated for branch object.
void free() => bindings.free(_branchPointer);
@override
String toString() {
return 'Branch{name: $name, target: $target}';
}
}