refactor(reference): remove duplication

This commit is contained in:
Aleksey Kulikov 2021-08-09 11:56:37 +03:00
parent a474f06917
commit 1f0201d259
6 changed files with 147 additions and 208 deletions

View file

@ -1,6 +1,5 @@
import 'dart:ffi';
import 'odb.dart';
import 'oid.dart';
import 'reference.dart';
import 'bindings/libgit2_bindings.dart';
import 'bindings/repository.dart' as bindings;
@ -22,9 +21,11 @@ class Repository {
_repoPointer = bindings.open(path);
}
/// Pointer to memory address for allocated repository object.
late final Pointer<git_repository> _repoPointer;
/// Pointer to memory address for allocated repository object.
Pointer<git_repository> get pointer => _repoPointer;
/// Returns path to the `.git` folder for normal repositories
/// or path to the repository itself for bare repositories.
String get path => bindings.path(_repoPointer);
@ -157,101 +158,9 @@ class Repository {
libgit2.git_libgit2_shutdown();
}
/// 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.runtimeType == Oid) {
oid = target as Oid;
isDirect = true;
} else if (isValidShaHex(target as String)) {
if (target.length == 40) {
oid = Oid.fromSHA(target);
} else {
final shortOid = Oid.fromSHAn(target);
final odb = this.odb;
oid = Oid(odb.existsPrefix(shortOid.pointer, target.length));
odb.free();
}
isDirect = true;
} else {
isDirect = false;
}
if (isDirect) {
return Reference.createDirect(
repo: _repoPointer,
name: name,
oid: oid.pointer,
force: force,
logMessage: logMessage,
);
} else {
return Reference.createSymbolic(
repo: _repoPointer,
name: name,
target: target as String,
force: force,
logMessage: logMessage,
);
}
}
/// Returns [Reference] object pointing to repository head.
Reference get head => Reference(bindings.head(_repoPointer));
/// Returns [Reference] object by lookingup a [name] in repository.
///
/// Throws a [LibGit2Error] if error occured.
Reference getReference(String name) => Reference.lookup(_repoPointer, name);
/// Returns [Reference] object by lookingup a short [name] in repository.
///
/// Throws a [LibGit2Error] if error occured.
Reference getReferenceDWIM(String name) =>
Reference.lookupDWIM(_repoPointer, name);
/// Checks if a reflog exists for the specified reference [name].
///
/// Throws a [LibGit2Error] if error occured.
bool referenceHasLog(String name) => Reference.hasLog(_repoPointer, name);
/// Returns a map with all the references names and corresponding SHA hashes
/// that can be found in a repository.
Map<String, String> get references {
var refMap = <String, String>{};
final refList = Reference.list(_repoPointer);
for (var ref in refList) {
final r = getReference(ref);
refMap[ref] = r.target.sha;
r.free();
}
return refMap;
}
/// Returns [Odb] for this repository.
///
/// ODB Object must be freed once it's no longer being used.