refactor(reference): simplify api

This commit is contained in:
Aleksey Kulikov 2021-09-23 17:05:28 +03:00
parent 466f960c7b
commit 3c1a6b4ab4
6 changed files with 113 additions and 191 deletions

View file

@ -32,9 +32,7 @@ class Repository {
/// pointer to repository object in memory.
///
/// Should be freed with `free()` to release allocated memory.
Repository(this._repoPointer) {
libgit2.git_libgit2_init();
}
Repository(this._repoPointer);
/// Initializes a new instance of the [Repository] class by creating a new
/// Git repository in the given folder.
@ -254,66 +252,11 @@ class Repository {
/// Returns [Reference] object pointing to repository head.
///
/// Must be freed once it's no longer being used.
Reference get head => Reference(_repoPointer, bindings.head(_repoPointer));
Reference get head => Reference(bindings.head(_repoPointer));
/// Returns [References] object.
References get references => References(this);
/// Creates a new reference.
///
/// The reference will be created in the repository and written to the disk.
/// The generated [Reference] object must be freed by the user.
///
/// Valid reference names must follow one of two patterns:
///
/// Top-level names must contain only capital letters and underscores, and must begin and end
/// with a letter. (e.g. "HEAD", "ORIG_HEAD").
/// Names prefixed with "refs/" can be almost anything. You must avoid the characters
/// '~', '^', ':', '\', '?', '[', and '*', and the sequences ".." and "@{" which have
/// special meaning to revparse.
/// Throws a [LibGit2Error] if a reference already exists with the given name
/// unless force is true, in which case it will be overwritten.
///
/// The message for the reflog will be ignored if the reference does not belong in the
/// standard set (HEAD, branches and remote-tracking branches) and it does not have a reflog.
Reference createReference({
required String name,
required Object target,
bool force = false,
String? logMessage,
}) {
late final Oid oid;
late final bool isDirect;
if (target is Oid) {
oid = target;
isDirect = true;
} else if (isValidShaHex(target as String)) {
oid = Oid.fromSHA(this, target);
isDirect = true;
} else {
isDirect = false;
}
if (isDirect) {
return Reference.createDirect(
repo: this,
name: name,
oid: oid.pointer,
force: force,
logMessage: logMessage,
);
} else {
return Reference.createSymbolic(
repo: this,
name: name,
target: target as String,
force: force,
logMessage: logMessage,
);
}
}
/// Returns [Index] file for this repository.
///
/// Must be freed once it's no longer being used.