mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 12:49:08 -04:00
refactor: use classes instead of enums for git types
This commit is contained in:
parent
ebfc67ee0d
commit
da3bda077a
15 changed files with 135 additions and 77 deletions
|
@ -5,6 +5,8 @@ import 'libgit2_bindings.dart';
|
|||
import '../util.dart';
|
||||
|
||||
/// Get the type of a reference.
|
||||
///
|
||||
/// Either direct or symbolic.
|
||||
int referenceType(Pointer<git_reference> ref) =>
|
||||
libgit2.git_reference_type(ref);
|
||||
|
||||
|
|
|
@ -1,30 +0,0 @@
|
|||
enum ReferenceType { direct, symbolic }
|
||||
|
||||
enum GitFilemode { undreadable, tree, blob, blobExecutable, link, commit }
|
||||
|
||||
/// Flags to specify the sorting which a revwalk should perform.
|
||||
///
|
||||
/// [none] sort the output with the same default method from `git`: reverse
|
||||
/// chronological order. This is the default sorting for new walkers.
|
||||
///
|
||||
/// [topological] sort the repository contents in topological order (no parents before
|
||||
/// all of its children are shown); this sorting mode can be combined
|
||||
/// with time sorting to produce `git`'s `--date-order``.
|
||||
///
|
||||
/// [time] sort the repository contents by commit time;
|
||||
/// this sorting mode can be combined with topological sorting.
|
||||
///
|
||||
/// [reverse] Iterate through the repository contents in reverse
|
||||
/// order; this sorting mode can be combined with any of the above.
|
||||
enum GitSort { none, topological, time, reverse }
|
||||
|
||||
enum GitObject { commit, tree, blob, tag }
|
||||
|
||||
/// Revparse flags, indicate the intended behavior of the spec.
|
||||
///
|
||||
/// [single]: the spec targeted a single object.
|
||||
///
|
||||
/// [range]: the spec targeted a range of commits.
|
||||
///
|
||||
/// [mergeBase]: the spec used the '...' operator, which invokes special semantics.
|
||||
enum GitRevParse { single, range, mergeBase }
|
112
lib/src/git_types.dart
Normal file
112
lib/src/git_types.dart
Normal file
|
@ -0,0 +1,112 @@
|
|||
/// Basic type of any Git reference.
|
||||
class ReferenceType {
|
||||
const ReferenceType._(this._value);
|
||||
final int _value;
|
||||
|
||||
/// Invalid reference.
|
||||
static const invalid = ReferenceType._(0);
|
||||
|
||||
/// A reference that points at an object id.
|
||||
static const direct = ReferenceType._(1);
|
||||
|
||||
/// A reference that points at another reference.
|
||||
static const symbolic = ReferenceType._(2);
|
||||
|
||||
static const all = ReferenceType._(3);
|
||||
|
||||
int get value => _value;
|
||||
}
|
||||
|
||||
/// Valid modes for index and tree entries.
|
||||
class GitFilemode {
|
||||
const GitFilemode._(this._value);
|
||||
final int _value;
|
||||
|
||||
static const unreadable = GitFilemode._(0);
|
||||
|
||||
static const tree = GitFilemode._(16384);
|
||||
|
||||
static const blob = GitFilemode._(33188);
|
||||
|
||||
static const blobExecutable = GitFilemode._(33261);
|
||||
|
||||
static const link = GitFilemode._(40960);
|
||||
|
||||
static const commit = GitFilemode._(57344);
|
||||
|
||||
int get value => _value;
|
||||
}
|
||||
|
||||
/// Flags to specify the sorting which a revwalk should perform.
|
||||
class GitSort {
|
||||
const GitSort._(this._value);
|
||||
final int _value;
|
||||
|
||||
/// Sort the output with the same default method from `git`: reverse
|
||||
/// chronological order. This is the default sorting for new walkers.
|
||||
static const none = GitSort._(0);
|
||||
|
||||
/// Sort the repository contents in topological order (no parents before
|
||||
/// all of its children are shown); this sorting mode can be combined
|
||||
/// with time sorting to produce `git`'s `--date-order``.
|
||||
static const topological = GitSort._(1);
|
||||
|
||||
/// Sort the repository contents by commit time;
|
||||
/// this sorting mode can be combined with topological sorting.
|
||||
static const time = GitSort._(2);
|
||||
|
||||
/// Iterate through the repository contents in reverse order; this sorting mode
|
||||
/// can be combined with any of the above.
|
||||
static const reverse = GitSort._(4);
|
||||
|
||||
int get value => _value;
|
||||
}
|
||||
|
||||
/// Basic type (loose or packed) of any Git object.
|
||||
class GitObjectType {
|
||||
const GitObjectType._(this._value);
|
||||
final int _value;
|
||||
|
||||
/// Object can be any of the following.
|
||||
static const any = GitObjectType._(-2);
|
||||
|
||||
/// Object is invalid.
|
||||
static const invalid = GitObjectType._(-1);
|
||||
|
||||
/// A commit object.
|
||||
static const commit = GitObjectType._(1);
|
||||
|
||||
/// A tree (directory listing) object.
|
||||
static const tree = GitObjectType._(2);
|
||||
|
||||
/// A file revision object.
|
||||
static const blob = GitObjectType._(3);
|
||||
|
||||
/// An annotated tag object.
|
||||
static const tag = GitObjectType._(4);
|
||||
|
||||
/// A delta, base is given by an offset.
|
||||
static const offsetDelta = GitObjectType._(6);
|
||||
|
||||
/// A delta, base is given by object id.
|
||||
static const refDelta = GitObjectType._(7);
|
||||
|
||||
int get value => _value;
|
||||
}
|
||||
|
||||
/// Revparse flags, indicate the intended behavior of the spec.
|
||||
class GitRevParse {
|
||||
const GitRevParse._(this._value);
|
||||
final int _value;
|
||||
|
||||
/// The spec targeted a single object.
|
||||
static const single = GitRevParse._(1);
|
||||
|
||||
/// The spec targeted a range of commits.
|
||||
static const range = GitRevParse._(2);
|
||||
|
||||
/// The spec used the '...' operator, which invokes special semantics.
|
||||
static const mergeBase = GitRevParse._(4);
|
||||
|
||||
int get value => _value;
|
||||
}
|
|
@ -4,7 +4,7 @@ import 'package:libgit2dart/src/tree.dart';
|
|||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'bindings/index.dart' as bindings;
|
||||
import 'oid.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
import 'repository.dart';
|
||||
import 'util.dart';
|
||||
|
||||
|
@ -166,9 +166,7 @@ class IndexEntry {
|
|||
GitFilemode get mode => intToGitFilemode(_indexEntryPointer.ref.mode);
|
||||
|
||||
/// Sets the UNIX file attributes of a index entry.
|
||||
set mode(GitFilemode mode) {
|
||||
_indexEntryPointer.ref.mode = gitFilemodeToInt(mode);
|
||||
}
|
||||
set mode(GitFilemode mode) => _indexEntryPointer.ref.mode = mode.value;
|
||||
|
||||
String _oidToHex(git_oid oid) {
|
||||
var hex = StringBuffer();
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'bindings/libgit2_bindings.dart';
|
|||
import 'bindings/reference.dart' as bindings;
|
||||
import 'oid.dart';
|
||||
import 'reflog.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
import 'repository.dart';
|
||||
import 'util.dart';
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import 'reference.dart';
|
|||
import 'revwalk.dart';
|
||||
import 'revparse.dart';
|
||||
import 'blob.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
import 'signature.dart';
|
||||
import 'tag.dart';
|
||||
import 'util.dart';
|
||||
|
@ -415,7 +415,7 @@ class Repository {
|
|||
Oid createTag({
|
||||
required String tagName,
|
||||
required Oid target,
|
||||
required GitObject targetType,
|
||||
required GitObjectType targetType,
|
||||
required Signature tagger,
|
||||
required String message,
|
||||
bool force = false,
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'bindings/revparse.dart' as bindings;
|
|||
import 'commit.dart';
|
||||
import 'reference.dart';
|
||||
import 'repository.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
|
||||
class RevParse {
|
||||
/// Finds a single object and intermediate reference (if there is one) by a [spec] revision string.
|
||||
|
|
|
@ -4,7 +4,7 @@ import 'bindings/revwalk.dart' as bindings;
|
|||
import 'commit.dart';
|
||||
import 'oid.dart';
|
||||
import 'repository.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
|
||||
class RevWalk {
|
||||
/// Initializes a new instance of the [RevWalk] class.
|
||||
|
@ -36,13 +36,8 @@ class RevWalk {
|
|||
/// Changing the sorting mode resets the walker.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void sorting(GitSort sorting) {
|
||||
bindings.sorting(
|
||||
_revWalkPointer,
|
||||
// in libgit2 GIT_SORT_REVERSE flag is integer 4 so we are adding 1 to our enum index
|
||||
sorting == GitSort.reverse ? sorting.index + 1 : sorting.index,
|
||||
);
|
||||
}
|
||||
void sorting(GitSort sorting) =>
|
||||
bindings.sorting(_revWalkPointer, sorting.value);
|
||||
|
||||
/// Adds a new root for the traversal.
|
||||
///
|
||||
|
|
|
@ -7,7 +7,7 @@ import 'commit.dart';
|
|||
import 'oid.dart';
|
||||
import 'repository.dart';
|
||||
import 'signature.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
|
||||
class Tag {
|
||||
/// Initializes a new instance of [Tag] class from provided
|
||||
|
@ -39,16 +39,15 @@ class Tag {
|
|||
required Repository repository,
|
||||
required String tagName,
|
||||
required Oid target,
|
||||
required GitObject targetType,
|
||||
required GitObjectType targetType,
|
||||
required Signature tagger,
|
||||
required String message,
|
||||
bool force = false,
|
||||
}) {
|
||||
// add 1 to GitObject enum index to match libgit2
|
||||
final object = object_bindings.lookup(
|
||||
repository.pointer,
|
||||
target.pointer,
|
||||
targetType.index + 1,
|
||||
targetType.value,
|
||||
);
|
||||
final result = bindings.create(
|
||||
repository.pointer,
|
||||
|
|
|
@ -3,7 +3,7 @@ import 'bindings/libgit2_bindings.dart';
|
|||
import 'bindings/tree.dart' as bindings;
|
||||
import 'repository.dart';
|
||||
import 'oid.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class Tree {
|
||||
|
|
|
@ -3,9 +3,8 @@ import 'bindings/libgit2_bindings.dart';
|
|||
import 'bindings/treebuilder.dart' as bindings;
|
||||
import 'repository.dart';
|
||||
import 'oid.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
import 'tree.dart';
|
||||
import 'util.dart';
|
||||
|
||||
class TreeBuilder {
|
||||
/// Initializes a new instance of [TreeBuilder] class from provided
|
||||
|
@ -62,7 +61,7 @@ class TreeBuilder {
|
|||
_treeBuilderPointer,
|
||||
filename,
|
||||
id.pointer,
|
||||
gitFilemodeToInt(filemode),
|
||||
filemode.value,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import 'dart:io';
|
||||
import 'dart:ffi';
|
||||
import 'bindings/libgit2_bindings.dart';
|
||||
import 'enums.dart';
|
||||
import 'git_types.dart';
|
||||
|
||||
DynamicLibrary loadLibrary() {
|
||||
if (Platform.isLinux || Platform.isAndroid || Platform.isFuchsia) {
|
||||
|
@ -30,7 +30,7 @@ bool isValidShaHex(String str) {
|
|||
GitFilemode intToGitFilemode(int i) {
|
||||
switch (i) {
|
||||
case 0:
|
||||
return GitFilemode.undreadable;
|
||||
return GitFilemode.unreadable;
|
||||
case 16384:
|
||||
return GitFilemode.tree;
|
||||
case 33188:
|
||||
|
@ -42,23 +42,6 @@ GitFilemode intToGitFilemode(int i) {
|
|||
case 57344:
|
||||
return GitFilemode.commit;
|
||||
default:
|
||||
return GitFilemode.undreadable;
|
||||
}
|
||||
}
|
||||
|
||||
int gitFilemodeToInt(GitFilemode filemode) {
|
||||
switch (filemode) {
|
||||
case GitFilemode.undreadable:
|
||||
return 0;
|
||||
case GitFilemode.tree:
|
||||
return 16384;
|
||||
case GitFilemode.blob:
|
||||
return 33188;
|
||||
case GitFilemode.blobExecutable:
|
||||
return 33261;
|
||||
case GitFilemode.link:
|
||||
return 40960;
|
||||
case GitFilemode.commit:
|
||||
return 57344;
|
||||
return GitFilemode.unreadable;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue