refactor!: use class names instead of aliases from Repository in tests (#37)

BREAKING CHANGE: move API methods related to diffing into Diff class
This commit is contained in:
Aleksey Kulikov 2022-01-25 12:05:34 +03:00 committed by GitHub
parent 3e1ece4e6f
commit 08cbe8a17f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 943 additions and 834 deletions

View file

@ -66,7 +66,7 @@ Pointer<git_diff> indexToWorkdir({
/// Create a diff between a tree and repository index. /// Create a diff between a tree and repository index.
Pointer<git_diff> treeToIndex({ Pointer<git_diff> treeToIndex({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required Pointer<git_tree> treePointer, required Pointer<git_tree>? treePointer,
required Pointer<git_index> indexPointer, required Pointer<git_index> indexPointer,
required int flags, required int flags,
required int contextLines, required int contextLines,
@ -82,7 +82,7 @@ Pointer<git_diff> treeToIndex({
libgit2.git_diff_tree_to_index( libgit2.git_diff_tree_to_index(
out, out,
repoPointer, repoPointer,
treePointer, treePointer ?? nullptr,
indexPointer, indexPointer,
opts, opts,
); );
@ -97,7 +97,7 @@ Pointer<git_diff> treeToIndex({
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_diff> treeToWorkdir({ Pointer<git_diff> treeToWorkdir({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required Pointer<git_tree> treePointer, required Pointer<git_tree>? treePointer,
required int flags, required int flags,
required int contextLines, required int contextLines,
required int interhunkLines, required int interhunkLines,
@ -112,7 +112,7 @@ Pointer<git_diff> treeToWorkdir({
final error = libgit2.git_diff_tree_to_workdir( final error = libgit2.git_diff_tree_to_workdir(
out, out,
repoPointer, repoPointer,
treePointer, treePointer ?? nullptr,
opts, opts,
); );
@ -170,8 +170,8 @@ Pointer<git_diff> treeToWorkdirWithIndex({
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_diff> treeToTree({ Pointer<git_diff> treeToTree({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required Pointer<git_tree> oldTreePointer, required Pointer<git_tree>? oldTreePointer,
required Pointer<git_tree> newTreePointer, required Pointer<git_tree>? newTreePointer,
required int flags, required int flags,
required int contextLines, required int contextLines,
required int interhunkLines, required int interhunkLines,
@ -186,8 +186,8 @@ Pointer<git_diff> treeToTree({
final error = libgit2.git_diff_tree_to_tree( final error = libgit2.git_diff_tree_to_tree(
out, out,
repoPointer, repoPointer,
oldTreePointer, oldTreePointer ?? nullptr,
newTreePointer, newTreePointer ?? nullptr,
opts, opts,
); );

View file

@ -41,7 +41,8 @@ class Branch {
); );
} }
/// Lookups a branch by its [name] and [type] in a [repo]sitory. /// Lookups a branch by its [name] and [type] in a [repo]sitory. Lookups in
/// local branches by default.
/// ///
/// The branch name will be checked for validity. /// The branch name will be checked for validity.
/// ///

View file

@ -178,6 +178,38 @@ class Commit {
); );
} }
/// Reverts commit, producing changes in the index and working directory.
///
/// Throws a [LibGit2Error] if error occured.
void revert() {
bindings.revert(
repoPointer: bindings.owner(_commitPointer),
commitPointer: _commitPointer,
);
}
/// Reverts commit against provided [commit], producing an index that
/// reflects the result of the revert.
///
/// [mainline] is parent of the commit if it is a merge (i.e. 1, 2).
///
/// **IMPORTANT**: produced index should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Index revertTo({
required Commit commit,
int mainline = 0,
}) {
return Index(
bindings.revertCommit(
repoPointer: bindings.owner(_commitPointer),
revertCommitPointer: _commitPointer,
ourCommitPointer: commit.pointer,
mainline: mainline,
),
);
}
/// Wncoding for the message of a commit, as a string representing a standard /// Wncoding for the message of a commit, as a string representing a standard
/// encoding name. /// encoding name.
String get messageEncoding => bindings.messageEncoding(_commitPointer); String get messageEncoding => bindings.messageEncoding(_commitPointer);

View file

@ -14,6 +14,118 @@ class Diff {
/// **IMPORTANT**: Should be freed to release allocated memory. /// **IMPORTANT**: Should be freed to release allocated memory.
Diff(this._diffPointer); Diff(this._diffPointer);
/// Creates a diff between the [repo]sitory [index] and the workdir directory.
///
/// This matches the `git diff` command.
///
/// [repo] is the repository containing index.
///
/// [index] is the index to diff from.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Diff.indexToWorkdir({
required Repository repo,
required Index index,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
_diffPointer = bindings.indexToWorkdir(
repoPointer: repo.pointer,
indexPointer: index.pointer,
flags: flags.fold(0, (int acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
);
}
/// Creates a diff between a [tree] and [repo]sitory [index].
///
/// This is equivalent to `git diff --cached <treeish>` or if you pass the
/// HEAD tree, then like `git diff --cached`.
///
/// [repo] is the repository containing the tree and index.
///
/// [tree] is the [Tree] object to diff from or null for empty tree.
///
/// [index] is the index to diff with.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
Diff.treeToIndex({
required Repository repo,
required Tree? tree,
required Index index,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
_diffPointer = bindings.treeToIndex(
repoPointer: repo.pointer,
treePointer: tree?.pointer,
indexPointer: index.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
);
}
/// Creates a diff between a [tree] and the working directory.
///
/// This is not the same as `git diff <treeish>` or
/// `git diff-index <treeish>`. Those commands use information from the
/// index, whereas this method strictly returns the differences between the
/// tree and the files in the working directory, regardless of the state of
/// the index. Use [Diff.treeToWorkdirWithIndex] to emulate those commands.
///
/// To see difference between this and [Diff.treeToWorkdirWithIndex],
/// consider the example of a staged file deletion where the file has then
/// been put back into the working directory and further modified. The
/// tree-to-workdir diff for that file is 'modified', but `git diff` would
/// show status 'deleted' since there is a staged delete.
///
/// [repo] is the repository containing the tree.
///
/// [tree] is the [Tree] object to diff from or null for empty tree.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
Diff.treeToWorkdir({
required Repository repo,
required Tree? tree,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
_diffPointer = bindings.treeToWorkdir(
repoPointer: repo.pointer,
treePointer: tree?.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
);
}
/// Creates a diff between a [tree] and the working directory using index /// Creates a diff between a [tree] and the working directory using index
/// data to account for staged deletes, tracked files, etc. /// data to account for staged deletes, tracked files, etc.
/// ///
@ -52,6 +164,83 @@ class Diff {
); );
} }
/// Creates a diff with the difference between two [Tree] objects.
///
/// This is equivalent to `git diff <old-tree> <new-tree>`.
///
/// [repo] is the repository containing the trees.
///
/// [oldTree] is the [Tree] object to diff from, or null for empty tree.
///
/// [newTree] is the [Tree] object to diff to, or null for empty tree.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured or [ArgumentError] if both trees
/// are null.
Diff.treeToTree({
required Repository repo,
required Tree? oldTree,
required Tree? newTree,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
if (oldTree == null && newTree == null) {
throw ArgumentError('Both trees cannot be null');
}
_diffPointer = bindings.treeToTree(
repoPointer: repo.pointer,
oldTreePointer: oldTree?.pointer,
newTreePointer: newTree?.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
);
}
/// Creates a diff with the difference between two [Index] objects.
///
/// [repo] is the repository containing the indexes.
///
/// [oldIndex] is the [Index] object to diff from.
///
/// [newIndex] is the [Index] object to diff to.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured.
Diff.indexToIndex({
required Repository repo,
required Index oldIndex,
required Index newIndex,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
_diffPointer = bindings.indexToIndex(
repoPointer: repo.pointer,
oldIndexPointer: oldIndex.pointer,
newIndexPointer: newIndex.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
);
}
/// Reads the [content]s of a git patch file into a git diff object. /// Reads the [content]s of a git patch file into a git diff object.
/// ///
/// The diff object produced is similar to the one that would be produced if /// The diff object produced is similar to the one that would be produced if
@ -138,6 +327,77 @@ class Diff {
); );
} }
/// Applies the diff to the [repo]sitory, making changes in the provided
/// [location].
///
/// [repo] is the repository to apply to.
///
/// [hunkIndex] is optional index of the hunk to apply.
///
/// [location] is the location to apply (workdir, index or both).
/// Defaults to workdir.
///
/// Throws a [LibGit2Error] if error occured.
void apply({
required Repository repo,
int? hunkIndex,
GitApplyLocation location = GitApplyLocation.workdir,
}) {
bindings.apply(
repoPointer: repo.pointer,
diffPointer: _diffPointer,
hunkIndex: hunkIndex,
location: location.value,
);
}
/// Checks if the diff will apply to provided [location].
///
/// [repo] is the repository to apply to.
///
/// [hunkIndex] is optional index of the hunk to apply.
///
/// [location] is the location to apply (workdir, index or both).
/// Defaults to workdir.
bool applies({
required Repository repo,
int? hunkIndex,
GitApplyLocation location = GitApplyLocation.workdir,
}) {
return bindings.apply(
repoPointer: repo.pointer,
diffPointer: _diffPointer,
hunkIndex: hunkIndex,
location: location.value,
check: true,
);
}
/// Applies the diff to the [tree], and returns the resulting image as an
/// index.
///
/// [repo] is the repository to apply to.
///
/// [tree] is the tree to apply the diff to.
///
/// [hunkIndex] is optional index of the hunk to apply.
///
/// Throws a [LibGit2Error] if error occured.
Index applyToTree({
required Repository repo,
required Tree tree,
int? hunkIndex,
}) {
return Index(
bindings.applyToTree(
repoPointer: repo.pointer,
diffPointer: _diffPointer,
treePointer: tree.pointer,
hunkIndex: hunkIndex,
),
);
}
/// Transforms a diff marking file renames, copies, etc. /// Transforms a diff marking file renames, copies, etc.
/// ///
/// This modifies a diff in place, replacing old entries that look like /// This modifies a diff in place, replacing old entries that look like

View file

@ -3,7 +3,6 @@ import 'dart:ffi';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'package:libgit2dart/libgit2dart.dart'; import 'package:libgit2dart/libgit2dart.dart';
import 'package:libgit2dart/src/bindings/diff.dart' as diff_bindings;
import 'package:libgit2dart/src/bindings/index.dart' as bindings; import 'package:libgit2dart/src/bindings/index.dart' as bindings;
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
@ -300,91 +299,6 @@ class Index with IterableMixin<IndexEntry> {
void removeAll(List<String> path) => void removeAll(List<String> path) =>
bindings.removeAll(indexPointer: _indexPointer, pathspec: path); bindings.removeAll(indexPointer: _indexPointer, pathspec: path);
/// Creates a diff with the difference between two index objects.
///
/// [index] is the [Index] object to diff to.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured.
Diff diffToIndex({
required Index index,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.indexToIndex(
repoPointer: bindings.owner(_indexPointer),
oldIndexPointer: _indexPointer,
newIndexPointer: index.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Creates a diff between the repository index and the workdir directory.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
Diff diffToWorkdir({
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.indexToWorkdir(
repoPointer: bindings.owner(_indexPointer),
indexPointer: _indexPointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Creates a diff between a tree and repository index.
///
/// [tree] is the [Tree] object to diff from.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
Diff diffToTree({
required Tree tree,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.treeToIndex(
repoPointer: bindings.owner(_indexPointer),
treePointer: tree.pointer,
indexPointer: _indexPointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Releases memory allocated for index object. /// Releases memory allocated for index object.
void free() => bindings.free(_indexPointer); void free() => bindings.free(_indexPointer);

View file

@ -7,7 +7,6 @@ import 'package:libgit2dart/src/bindings/attr.dart' as attr_bindings;
import 'package:libgit2dart/src/bindings/checkout.dart' as checkout_bindings; import 'package:libgit2dart/src/bindings/checkout.dart' as checkout_bindings;
import 'package:libgit2dart/src/bindings/commit.dart' as commit_bindings; import 'package:libgit2dart/src/bindings/commit.dart' as commit_bindings;
import 'package:libgit2dart/src/bindings/describe.dart' as describe_bindings; import 'package:libgit2dart/src/bindings/describe.dart' as describe_bindings;
import 'package:libgit2dart/src/bindings/diff.dart' as diff_bindings;
import 'package:libgit2dart/src/bindings/graph.dart' as graph_bindings; import 'package:libgit2dart/src/bindings/graph.dart' as graph_bindings;
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/bindings/merge.dart' as merge_bindings; import 'package:libgit2dart/src/bindings/merge.dart' as merge_bindings;
@ -1410,82 +1409,6 @@ class Repository {
); );
} }
/// Returns a [Diff] with changes between the trees, tree and index, tree and
/// workdir or index and workdir.
///
/// If [b] is null, by default the [a] tree compared to working directory. If
/// [cached] is set to true the [a] tree compared to index/staging area.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// **IMPORTANT**: Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
Diff diff({
Tree? a,
Tree? b,
bool cached = false,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
final flagsInt = flags.fold(0, (int acc, e) => acc | e.value);
if (a is Tree && b is Tree) {
return Diff(
diff_bindings.treeToTree(
repoPointer: _repoPointer,
oldTreePointer: a.pointer,
newTreePointer: b.pointer,
flags: flagsInt,
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
} else if (a is Tree && b == null) {
if (cached) {
return Diff(
diff_bindings.treeToIndex(
repoPointer: _repoPointer,
treePointer: a.pointer,
indexPointer: index.pointer,
flags: flagsInt,
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
} else {
return Diff(
diff_bindings.treeToWorkdir(
repoPointer: _repoPointer,
treePointer: a.pointer,
flags: flagsInt,
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
} else if (a == null && b == null) {
return Diff(
diff_bindings.indexToWorkdir(
repoPointer: _repoPointer,
indexPointer: index.pointer,
flags: flagsInt,
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
} else {
throw ArgumentError.notNull('a');
}
}
/// Returns a [Patch] with changes between the blobs. /// Returns a [Patch] with changes between the blobs.
/// ///
/// [a] is the blob for old side of diff. /// [a] is the blob for old side of diff.
@ -1519,65 +1442,6 @@ class Repository {
return a.diff(newBlob: b, oldAsPath: aPath, newAsPath: bPath); return a.diff(newBlob: b, oldAsPath: aPath, newAsPath: bPath);
} }
/// Applies the [diff] to the repository, making changes in the provided
/// [location] (directly in the working directory (default), the index or
/// both).
///
/// [hunkIndex] is optional index of the hunk to apply.
///
/// Throws a [LibGit2Error] if error occured.
void apply({
required Diff diff,
int? hunkIndex,
GitApplyLocation location = GitApplyLocation.workdir,
}) {
diff_bindings.apply(
repoPointer: _repoPointer,
diffPointer: diff.pointer,
hunkIndex: hunkIndex,
location: location.value,
);
}
/// Applies the [diff] to the [tree], and returns the resulting image as an
/// index.
///
/// [hunkIndex] is optional index of the hunk to apply.
///
/// Throws a [LibGit2Error] if error occured.
Index applyToTree({
required Diff diff,
required Tree tree,
int? hunkIndex,
}) {
return Index(
diff_bindings.applyToTree(
repoPointer: _repoPointer,
diffPointer: diff.pointer,
treePointer: tree.pointer,
hunkIndex: hunkIndex,
),
);
}
/// Checks if the [diff] will apply to provided [location] (the working
/// directory (default), the index or both).
///
/// [hunkIndex] is optional index of the hunk to apply.
bool applies({
required Diff diff,
int? hunkIndex,
GitApplyLocation location = GitApplyLocation.workdir,
}) {
return diff_bindings.apply(
repoPointer: _repoPointer,
diffPointer: diff.pointer,
hunkIndex: hunkIndex,
location: location.value,
check: true,
);
}
/// Returns list of all the stashed states, first being the most recent. /// Returns list of all the stashed states, first being the most recent.
List<Stash> get stashes => Stash.list(this); List<Stash> get stashes => Stash.list(this);
@ -1812,13 +1676,6 @@ class Repository {
); );
} }
/// List of notes for repository.
///
/// **IMPORTANT**: Notes Should be freed to release allocated memory.
///
/// Throws a [LibGit2Error] if error occured.
List<Note> get notes => Note.list(this);
/// Lookups the note for an [annotatedOid]. /// Lookups the note for an [annotatedOid].
/// ///
/// [annotatedOid] is the [Oid] of the git object to read the note from. /// [annotatedOid] is the [Oid] of the git object to read the note from.
@ -2075,7 +1932,7 @@ class Repository {
required String submodule, required String submodule,
bool overwrite = false, bool overwrite = false,
}) { }) {
Submodule.init(repo: this, submodule: submodule, overwrite: overwrite); Submodule.init(repo: this, name: submodule, overwrite: overwrite);
} }
/// Updates a submodule. This will clone a missing submodule and checkout the /// Updates a submodule. This will clone a missing submodule and checkout the
@ -2096,7 +1953,7 @@ class Repository {
}) { }) {
Submodule.update( Submodule.update(
repo: this, repo: this,
submodule: submodule, name: submodule,
init: init, init: init,
callbacks: callbacks, callbacks: callbacks,
); );

View file

@ -14,8 +14,7 @@ class RevParse {
/// point to an intermediate reference. When such expressions are being /// point to an intermediate reference. When such expressions are being
/// passed in, reference_out will be valued as well. /// passed in, reference_out will be valued as well.
/// ///
/// **IMPORTANT**: The returned object and reference should be freed to /// **IMPORTANT**: Should be freed to release allocated memory.
/// release allocated memory.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
RevParse.ext({required Repository repo, required String spec}) { RevParse.ext({required Repository repo, required String spec}) {
@ -41,7 +40,7 @@ class RevParse {
/// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions /// See `man gitrevisions`, or https://git-scm.com/docs/git-rev-parse.html#_specifying_revisions
/// for information on the syntax accepted. /// for information on the syntax accepted.
/// ///
/// The returned object should be released when no longer needed. /// **IMPORTANT**: Should be freed to release allocated memory.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
static Commit single({required Repository repo, required String spec}) { static Commit single({required Repository repo, required String spec}) {

View file

@ -65,12 +65,12 @@ class Submodule {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
static void init({ static void init({
required Repository repo, required Repository repo,
required String submodule, required String name,
bool overwrite = false, bool overwrite = false,
}) { }) {
final submodulePointer = bindings.lookup( final submodulePointer = bindings.lookup(
repoPointer: repo.pointer, repoPointer: repo.pointer,
name: submodule, name: name,
); );
bindings.init(submodulePointer: submodulePointer, overwrite: overwrite); bindings.init(submodulePointer: submodulePointer, overwrite: overwrite);
@ -91,13 +91,13 @@ class Submodule {
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
static void update({ static void update({
required Repository repo, required Repository repo,
required String submodule, required String name,
bool init = false, bool init = false,
Callbacks callbacks = const Callbacks(), Callbacks callbacks = const Callbacks(),
}) { }) {
final submodulePointer = bindings.lookup( final submodulePointer = bindings.lookup(
repoPointer: repo.pointer, repoPointer: repo.pointer,
name: submodule, name: name,
); );
bindings.update( bindings.update(

View file

@ -1,7 +1,6 @@
import 'dart:ffi'; import 'dart:ffi';
import 'package:libgit2dart/libgit2dart.dart'; import 'package:libgit2dart/libgit2dart.dart';
import 'package:libgit2dart/src/bindings/diff.dart' as diff_bindings;
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart'; import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
import 'package:libgit2dart/src/bindings/tree.dart' as bindings; import 'package:libgit2dart/src/bindings/tree.dart' as bindings;
@ -90,93 +89,6 @@ class Tree {
/// Number of entries listed in a tree. /// Number of entries listed in a tree.
int get length => bindings.entryCount(_treePointer); int get length => bindings.entryCount(_treePointer);
/// Creates a diff between a tree and the working directory.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured.
Diff diffToWorkdir({
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.treeToWorkdir(
repoPointer: bindings.owner(_treePointer),
treePointer: _treePointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Creates a diff between a tree and repository index.
///
/// [index] is the [Index] object to diff to.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
Diff diffToIndex({
required Index index,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.treeToIndex(
repoPointer: bindings.owner(_treePointer),
treePointer: _treePointer,
indexPointer: index.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Creates a diff with the difference between two tree objects.
///
/// [tree] is the [Tree] object to diff to.
///
/// [flags] is a combination of [GitDiff] flags. Defaults to [GitDiff.normal].
///
/// [contextLines] is the number of unchanged lines that define the boundary
/// of a hunk (and to display before and after). Defaults to 3.
///
/// [interhunkLines] is the maximum number of unchanged lines between hunk
/// boundaries before the hunks will be merged into one. Defaults to 0.
///
/// Throws a [LibGit2Error] if error occured.
Diff diffToTree({
required Tree tree,
Set<GitDiff> flags = const {GitDiff.normal},
int contextLines = 3,
int interhunkLines = 0,
}) {
return Diff(
diff_bindings.treeToTree(
repoPointer: bindings.owner(_treePointer),
oldTreePointer: _treePointer,
newTreePointer: tree.pointer,
flags: flags.fold(0, (acc, e) => acc | e.value),
contextLines: contextLines,
interhunkLines: interhunkLines,
),
);
}
/// Releases memory allocated for tree object. /// Releases memory allocated for tree object.
void free() => bindings.free(_treePointer); void free() => bindings.free(_treePointer);

View file

@ -60,7 +60,8 @@ void main() {
group('Blame', () { group('Blame', () {
test('returns the blame for provided file', () { test('returns the blame for provided file', () {
final blame = repo.blame( final blame = Blame.file(
repo: repo,
path: 'feature_file', path: 'feature_file',
oldestCommit: repo['f17d0d4'], oldestCommit: repo['f17d0d4'],
); );
@ -86,11 +87,14 @@ void main() {
}); });
test('throws when provided file path is invalid', () { test('throws when provided file path is invalid', () {
expect(() => repo.blame(path: 'invalid'), throwsA(isA<LibGit2Error>())); expect(
() => Blame.file(repo: repo, path: 'invalid'),
throwsA(isA<LibGit2Error>()),
);
}); });
test('returns blame for buffer', () { test('returns blame for buffer', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
expect(blame.length, 2); expect(blame.length, 2);
final bufferBlame = Blame.buffer(reference: blame, buffer: ' '); final bufferBlame = Blame.buffer(reference: blame, buffer: ' ');
@ -106,7 +110,7 @@ void main() {
}); });
test('throws when trying to get blame for empty buffer', () { test('throws when trying to get blame for empty buffer', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
expect( expect(
() => Blame.buffer(reference: blame, buffer: ''), () => Blame.buffer(reference: blame, buffer: ''),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
@ -115,7 +119,8 @@ void main() {
}); });
test('returns the blame for provided file with minMatchCharacters set', () { test('returns the blame for provided file with minMatchCharacters set', () {
final blame = repo.blame( final blame = Blame.file(
repo: repo,
path: 'feature_file', path: 'feature_file',
minMatchCharacters: 1, minMatchCharacters: 1,
flags: {GitBlameFlag.trackCopiesSameFile}, flags: {GitBlameFlag.trackCopiesSameFile},
@ -127,7 +132,7 @@ void main() {
}); });
test('returns the blame for provided line', () { test('returns the blame for provided line', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
final hunk = blame.forLine(1); final hunk = blame.forLine(1);
@ -148,21 +153,22 @@ void main() {
}); });
test('throws when provided index for hunk is invalid', () { test('throws when provided index for hunk is invalid', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
expect(() => blame[10], throwsA(isA<RangeError>())); expect(() => blame[10], throwsA(isA<RangeError>()));
blame.free(); blame.free();
}); });
test('throws when provided line number for hunk is invalid', () { test('throws when provided line number for hunk is invalid', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
expect(() => blame.forLine(10), throwsA(isA<RangeError>())); expect(() => blame.forLine(10), throwsA(isA<RangeError>()));
blame.free(); blame.free();
}); });
test('returns the blame for provided file with newestCommit argument', () { test('returns the blame for provided file with newestCommit argument', () {
final blame = repo.blame( final blame = Blame.file(
repo: repo,
path: 'feature_file', path: 'feature_file',
newestCommit: repo['fc38877'], newestCommit: repo['fc38877'],
flags: {GitBlameFlag.ignoreWhitespace}, flags: {GitBlameFlag.ignoreWhitespace},
@ -190,7 +196,8 @@ void main() {
test('returns the blame for provided file with minLine and maxLine set', test('returns the blame for provided file with minLine and maxLine set',
() { () {
final blame = repo.blame( final blame = Blame.file(
repo: repo,
path: 'feature_file', path: 'feature_file',
minLine: 1, minLine: 1,
maxLine: 1, maxLine: 1,
@ -217,7 +224,7 @@ void main() {
}); });
test('returns string representation of BlameHunk object', () { test('returns string representation of BlameHunk object', () {
final blame = repo.blame(path: 'feature_file'); final blame = Blame.file(repo: repo, path: 'feature_file');
expect(blame.toString(), contains('BlameHunk{')); expect(blame.toString(), contains('BlameHunk{'));
blame.free(); blame.free();
}); });

View file

@ -26,20 +26,20 @@ void main() {
group('Blob', () { group('Blob', () {
test('lookups blob with provided oid', () { test('lookups blob with provided oid', () {
final blob = repo.lookupBlob(repo[blobSHA]); final blob = Blob.lookup(repo: repo, oid: repo[blobSHA]);
expect(blob, isA<Blob>()); expect(blob, isA<Blob>());
blob.free(); blob.free();
}); });
test('throws when trying to lookup with invalid oid', () { test('throws when trying to lookup with invalid oid', () {
expect( expect(
() => repo.lookupBlob(repo['0' * 40]), () => Blob.lookup(repo: repo, oid: repo['0' * 40]),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('returns correct values', () { test('returns correct values', () {
final blob = repo.lookupBlob(repo[blobSHA]); final blob = Blob.lookup(repo: repo, oid: repo[blobSHA]);
expect(blob.oid.sha, blobSHA); expect(blob.oid.sha, blobSHA);
expect(blob.isBinary, false); expect(blob.isBinary, false);
@ -50,8 +50,8 @@ void main() {
}); });
test('creates new blob with provided content', () { test('creates new blob with provided content', () {
final oid = repo.createBlob(newBlobContent); final oid = Blob.create(repo: repo, content: newBlobContent);
final newBlob = repo.lookupBlob(oid); final newBlob = Blob.lookup(repo: repo, oid: oid);
expect(newBlob.oid.sha, '18fdaeef018e57a92bcad2d4a35b577f34089af6'); expect(newBlob.oid.sha, '18fdaeef018e57a92bcad2d4a35b577f34089af6');
expect(newBlob.isBinary, false); expect(newBlob.isBinary, false);
@ -70,8 +70,11 @@ void main() {
}); });
test('creates new blob from file at provided relative path', () { test('creates new blob from file at provided relative path', () {
final oid = repo.createBlobFromWorkdir('feature_file'); final oid = Blob.createFromWorkdir(
final newBlob = repo.lookupBlob(oid); repo: repo,
relativePath: 'feature_file',
);
final newBlob = Blob.lookup(repo: repo, oid: oid);
expect(newBlob.oid.sha, blobSHA); expect(newBlob.oid.sha, blobSHA);
expect(newBlob.isBinary, false); expect(newBlob.isBinary, false);
@ -83,7 +86,10 @@ void main() {
test('throws when creating new blob from invalid path', () { test('throws when creating new blob from invalid path', () {
expect( expect(
() => repo.createBlobFromWorkdir('invalid/path.txt'), () => Blob.createFromWorkdir(
repo: repo,
relativePath: 'invalid/path.txt',
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -92,8 +98,8 @@ void main() {
final outsideFile = File( final outsideFile = File(
p.join(Directory.current.absolute.path, 'test', 'blob_test.dart'), p.join(Directory.current.absolute.path, 'test', 'blob_test.dart'),
); );
final oid = repo.createBlobFromDisk(outsideFile.path); final oid = Blob.createFromDisk(repo: repo, path: outsideFile.path);
final newBlob = repo.lookupBlob(oid); final newBlob = Blob.lookup(repo: repo, oid: oid);
expect(newBlob, isA<Blob>()); expect(newBlob, isA<Blob>());
expect(newBlob.isBinary, false); expect(newBlob.isBinary, false);
@ -103,13 +109,13 @@ void main() {
test('throws when trying to create from invalid path', () { test('throws when trying to create from invalid path', () {
expect( expect(
() => repo.createBlobFromDisk('invalid.file'), () => Blob.createFromDisk(repo: repo, path: 'invalid.file'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('duplicates blob', () { test('duplicates blob', () {
final blob = repo.lookupBlob(repo[blobSHA]); final blob = Blob.lookup(repo: repo, oid: repo[blobSHA]);
final dupBlob = blob.duplicate(); final dupBlob = blob.duplicate();
expect(blob.oid.sha, dupBlob.oid.sha); expect(blob.oid.sha, dupBlob.oid.sha);
@ -119,8 +125,8 @@ void main() {
}); });
test('filters content of a blob', () { test('filters content of a blob', () {
final blobOid = repo.createBlob('clrf\nclrf\n'); final blobOid = Blob.create(repo: repo, content: 'clrf\nclrf\n');
final blob = repo.lookupBlob(blobOid); final blob = Blob.lookup(repo: repo, oid: blobOid);
expect(blob.filterContent(asPath: 'file.crlf'), 'clrf\r\nclrf\r\n'); expect(blob.filterContent(asPath: 'file.crlf'), 'clrf\r\nclrf\r\n');
@ -130,11 +136,12 @@ void main() {
test('filters content of a blob with provided commit for attributes', () { test('filters content of a blob with provided commit for attributes', () {
repo.checkout(target: 'refs/tags/v0.2'); repo.checkout(target: 'refs/tags/v0.2');
final blobOid = repo.createBlob('clrf\nclrf\n'); final blobOid = Blob.create(repo: repo, content: 'clrf\nclrf\n');
final blob = repo.lookupBlob(blobOid); final blob = Blob.lookup(repo: repo, oid: blobOid);
final commit = repo.lookupCommit( final commit = Commit.lookup(
repo['d2f3abc9324a22a9f80fec2c131ec43c93430618'], repo: repo,
oid: repo['d2f3abc9324a22a9f80fec2c131ec43c93430618'],
); );
expect( expect(
@ -176,31 +183,26 @@ index e69de29..0000000
+++ /dev/null +++ /dev/null
"""; """;
test('creates patch with changes between blobs', () { test('creates patch with changes between blobs', () {
final a = repo.lookupBlob( final oldBlob = Blob.lookup(repo: repo, oid: repo['e69de29']);
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'], final newBlob = Blob.lookup(repo: repo, oid: repo['9c78c21']);
);
final b = repo.lookupBlob( final patch = oldBlob.diff(
repo['9c78c21d6680a7ffebc76f7ac68cacc11d8f48bc'], newBlob: newBlob,
); oldAsPath: path,
final patch = repo.diffBlobs( newAsPath: path,
a: a,
b: b,
aPath: path,
bPath: path,
); );
expect(patch.text, blobPatch); expect(patch.text, blobPatch);
patch.free(); patch.free();
a.free(); oldBlob.free();
b.free(); newBlob.free();
}); });
test('creates patch with changes between blobs from one blob (delete)', test('creates patch with changes between blobs from one blob (delete)',
() { () {
final blob = repo.lookupBlob( final blob = Blob.lookup(repo: repo, oid: repo['e69de29']);
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'],
);
final patch = blob.diff( final patch = blob.diff(
newBlob: null, newBlob: null,
oldAsPath: path, oldAsPath: path,
@ -214,9 +216,7 @@ index e69de29..0000000
}); });
test('creates patch with changes between blob and buffer', () { test('creates patch with changes between blob and buffer', () {
final blob = repo.lookupBlob( final blob = Blob.lookup(repo: repo, oid: repo['e69de29']);
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'],
);
final patch = blob.diffToBuffer( final patch = blob.diffToBuffer(
buffer: 'Feature edit\n', buffer: 'Feature edit\n',
@ -229,25 +229,19 @@ index e69de29..0000000
}); });
test('creates patch with changes between blob and buffer (delete)', () { test('creates patch with changes between blob and buffer (delete)', () {
final a = repo.lookupBlob( final blob = Blob.lookup(repo: repo, oid: repo['e69de29']);
repo['e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'],
); final patch = blob.diffToBuffer(buffer: null, oldAsPath: path);
final patch = Patch.create(
a: a,
b: null,
aPath: path,
bPath: path,
);
expect(patch.text, blobPatchDelete); expect(patch.text, blobPatchDelete);
patch.free(); patch.free();
a.free(); blob.free();
}); });
}); });
test('returns string representation of Blob object', () { test('returns string representation of Blob object', () {
final blob = repo.lookupBlob(repo[blobSHA]); final blob = Blob.lookup(repo: repo, oid: repo[blobSHA]);
expect(blob.toString(), contains('Blob{')); expect(blob.toString(), contains('Blob{'));
blob.free(); blob.free();
}); });

View file

@ -28,7 +28,7 @@ void main() {
group('Branch', () { group('Branch', () {
test('returns a list of all branches', () { test('returns a list of all branches', () {
const branchesExpected = ['feature', 'master', 'origin/master']; const branchesExpected = ['feature', 'master', 'origin/master'];
final branches = repo.branches; final branches = Branch.list(repo: repo);
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
@ -38,7 +38,7 @@ void main() {
test('returns a list of local branches', () { test('returns a list of local branches', () {
const branchesExpected = ['feature', 'master']; const branchesExpected = ['feature', 'master'];
final branches = repo.branchesLocal; final branches = Branch.list(repo: repo, type: GitBranch.local);
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
@ -48,7 +48,7 @@ void main() {
test('returns a list of remote branches', () { test('returns a list of remote branches', () {
const branchesExpected = ['origin/master']; const branchesExpected = ['origin/master'];
final branches = repo.branchesRemote; final branches = Branch.list(repo: repo, type: GitBranch.remote);
for (var i = 0; i < branches.length; i++) { for (var i = 0; i < branches.length; i++) {
expect(branches[i].name, branchesExpected[i]); expect(branches[i].name, branchesExpected[i]);
@ -57,31 +57,37 @@ void main() {
}); });
test('throws when trying to return list and error occurs', () { test('throws when trying to return list and error occurs', () {
final nullRepo = Repository(nullptr); expect(
expect(() => Branch.list(repo: nullRepo), throwsA(isA<LibGit2Error>())); () => Branch.list(repo: Repository(nullptr)),
throwsA(isA<LibGit2Error>()),
);
}); });
test('returns a branch with provided name', () { test('returns a branch with provided name', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.target.sha, lastCommit.sha); expect(branch.target.sha, lastCommit.sha);
branch.free(); branch.free();
}); });
test('throws when provided name not found', () { test('throws when provided name not found', () {
expect( expect(
() => repo.lookupBranch(name: 'invalid'), () => Branch.lookup(repo: repo, name: 'invalid'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect( expect(
() => repo.lookupBranch(name: 'origin/invalid', type: GitBranch.remote), () => Branch.lookup(
repo: repo,
name: 'origin/invalid',
type: GitBranch.remote,
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('checks if branch is current head', () { test('checks if branch is current head', () {
final masterBranch = repo.lookupBranch(name: 'master'); final masterBranch = Branch.lookup(repo: repo, name: 'master');
final featureBranch = repo.lookupBranch(name: 'feature'); final featureBranch = Branch.lookup(repo: repo, name: 'feature');
expect(masterBranch.isHead, true); expect(masterBranch.isHead, true);
expect(featureBranch.isHead, false); expect(featureBranch.isHead, false);
@ -99,8 +105,8 @@ void main() {
}); });
test('checks if branch is checked out', () { test('checks if branch is checked out', () {
final masterBranch = repo.lookupBranch(name: 'master'); final masterBranch = Branch.lookup(repo: repo, name: 'master');
final featureBranch = repo.lookupBranch(name: 'feature'); final featureBranch = Branch.lookup(repo: repo, name: 'feature');
expect(masterBranch.isCheckedOut, true); expect(masterBranch.isCheckedOut, true);
expect(featureBranch.isCheckedOut, false); expect(featureBranch.isCheckedOut, false);
@ -118,7 +124,7 @@ void main() {
}); });
test('returns name', () { test('returns name', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.name, 'master'); expect(branch.name, 'master');
branch.free(); branch.free();
}); });
@ -129,7 +135,7 @@ void main() {
}); });
test('returns remote name of a remote-tracking branch', () { test('returns remote name of a remote-tracking branch', () {
final branch = repo.branchesRemote.first; final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(branch.remoteName, 'origin'); expect(branch.remoteName, 'origin');
branch.free(); branch.free();
}); });
@ -137,12 +143,12 @@ void main() {
test( test(
'throws when getting remote name of a remote-tracking branch and ' 'throws when getting remote name of a remote-tracking branch and '
'error occurs', () { 'error occurs', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(() => branch.remoteName, throwsA(isA<LibGit2Error>())); expect(() => branch.remoteName, throwsA(isA<LibGit2Error>()));
}); });
test('returns upstream of a local branch', () { test('returns upstream of a local branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
final upstream = branch.upstream; final upstream = branch.upstream;
expect(upstream.isRemote, true); expect(upstream.isRemote, true);
@ -153,17 +159,18 @@ void main() {
}); });
test('throws when trying to get upstream of a remote branch', () { test('throws when trying to get upstream of a remote branch', () {
final branch = repo.branchesRemote.first; final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstream, throwsA(isA<LibGit2Error>())); expect(() => branch.upstream, throwsA(isA<LibGit2Error>()));
branch.free(); branch.free();
}); });
test('sets upstream of a branch', () { test('sets upstream of a branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
var upstream = branch.upstream; var upstream = branch.upstream;
expect(upstream.name, 'refs/remotes/origin/master'); expect(upstream.name, 'refs/remotes/origin/master');
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/remotes/origin/new', name: 'refs/remotes/origin/new',
target: 'refs/heads/master', target: 'refs/heads/master',
); );
@ -178,7 +185,7 @@ void main() {
}); });
test('unsets upstream of a branch', () { test('unsets upstream of a branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
final upstream = branch.upstream; final upstream = branch.upstream;
expect(upstream.name, 'refs/remotes/origin/master'); expect(upstream.name, 'refs/remotes/origin/master');
@ -190,7 +197,7 @@ void main() {
}); });
test('throws when trying to set upstream of a branch and error occurs', () { test('throws when trying to set upstream of a branch and error occurs', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect( expect(
() => branch.setUpstream('some/upstream'), () => branch.setUpstream('some/upstream'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
@ -199,50 +206,53 @@ void main() {
}); });
test('returns upstream name of a local branch', () { test('returns upstream name of a local branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamName, 'refs/remotes/origin/master'); expect(branch.upstreamName, 'refs/remotes/origin/master');
branch.free(); branch.free();
}); });
test('throws when trying to get upstream name of a branch and error occurs', test('throws when trying to get upstream name of a branch and error occurs',
() { () {
final branch = repo.lookupBranch(name: 'feature'); final branch = Branch.lookup(repo: repo, name: 'feature');
expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>())); expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>()));
branch.free(); branch.free();
}); });
test('returns upstream remote of a local branch', () { test('returns upstream remote of a local branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamRemote, 'origin'); expect(branch.upstreamRemote, 'origin');
branch.free(); branch.free();
}); });
test('throws when trying to get upstream remote of a remote branch', () { test('throws when trying to get upstream remote of a remote branch', () {
final branch = repo.branchesRemote.first; final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>())); expect(() => branch.upstreamName, throwsA(isA<LibGit2Error>()));
branch.free(); branch.free();
}); });
test('returns upstream merge of a local branch', () { test('returns upstream merge of a local branch', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.upstreamMerge, 'refs/heads/master'); expect(branch.upstreamMerge, 'refs/heads/master');
branch.free(); branch.free();
}); });
test('throws when trying to get upstream merge of a remote branch', () { test('throws when trying to get upstream merge of a remote branch', () {
final branch = repo.branchesRemote.first; final branch = Branch.list(repo: repo, type: GitBranch.remote).first;
expect(() => branch.upstreamMerge, throwsA(isA<LibGit2Error>())); expect(() => branch.upstreamMerge, throwsA(isA<LibGit2Error>()));
branch.free(); branch.free();
}); });
group('create()', () { group('create()', () {
test('creates branch', () { test('creates branch', () {
final commit = repo.lookupCommit(lastCommit); final commit = Commit.lookup(repo: repo, oid: lastCommit);
final branch = Branch.create(
repo: repo,
name: 'testing',
target: commit,
);
final branches = Branch.list(repo: repo);
final branch = repo.createBranch(name: 'testing', target: commit); expect(branches.length, 4);
final branches = repo.branches;
expect(repo.branches.length, 4);
expect(branch.target, lastCommit); expect(branch.target, lastCommit);
for (final branch in branches) { for (final branch in branches) {
@ -253,10 +263,10 @@ void main() {
}); });
test('throws when name already exists', () { test('throws when name already exists', () {
final commit = repo.lookupCommit(lastCommit); final commit = Commit.lookup(repo: repo, oid: lastCommit);
expect( expect(
() => repo.createBranch(name: 'feature', target: commit), () => Branch.create(repo: repo, name: 'feature', target: commit),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
@ -264,14 +274,14 @@ void main() {
}); });
test('creates branch with force flag when name already exists', () { test('creates branch with force flag when name already exists', () {
final commit = repo.lookupCommit(lastCommit); final commit = Commit.lookup(repo: repo, oid: lastCommit);
final branch = Branch.create(
final branch = repo.createBranch( repo: repo,
name: 'feature', name: 'feature',
target: commit, target: commit,
force: true, force: true,
); );
final localBranches = repo.branchesLocal; final localBranches = Branch.list(repo: repo, type: GitBranch.local);
expect(localBranches.length, 2); expect(localBranches.length, 2);
expect(branch.target, lastCommit); expect(branch.target, lastCommit);
@ -286,17 +296,17 @@ void main() {
group('delete()', () { group('delete()', () {
test('deletes branch', () { test('deletes branch', () {
repo.deleteBranch('feature'); Branch.delete(repo: repo, name: 'feature');
expect( expect(
() => repo.lookupBranch(name: 'feature'), () => Branch.lookup(repo: repo, name: 'feature'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('throws when trying to delete current HEAD', () { test('throws when trying to delete current HEAD', () {
expect( expect(
() => repo.deleteBranch('master'), () => Branch.delete(repo: repo, name: 'master'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -304,13 +314,13 @@ void main() {
group('rename()', () { group('rename()', () {
test('renames branch', () { test('renames branch', () {
repo.renameBranch(oldName: 'feature', newName: 'renamed'); Branch.rename(repo: repo, oldName: 'feature', newName: 'renamed');
final branch = repo.lookupBranch(name: 'renamed'); final branch = Branch.lookup(repo: repo, name: 'renamed');
final branches = repo.branches; final branches = Branch.list(repo: repo);
expect(branches.length, 3); expect(branches.length, 3);
expect( expect(
() => repo.lookupBranch(name: 'feature'), () => Branch.lookup(repo: repo, name: 'feature'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect(branch.target, featureCommit); expect(branch.target, featureCommit);
@ -323,18 +333,23 @@ void main() {
test('throws when name already exists', () { test('throws when name already exists', () {
expect( expect(
() => repo.renameBranch(oldName: 'feature', newName: 'master'), () => Branch.rename(
repo: repo,
oldName: 'feature',
newName: 'master',
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('renames branch with force flag when name already exists', () { test('renames branch with force flag when name already exists', () {
repo.renameBranch( Branch.rename(
repo: repo,
oldName: 'master', oldName: 'master',
newName: 'feature', newName: 'feature',
force: true, force: true,
); );
final branch = repo.lookupBranch(name: 'feature'); final branch = Branch.lookup(repo: repo, name: 'feature');
expect(branch.target, lastCommit); expect(branch.target, lastCommit);
@ -343,14 +358,18 @@ void main() {
test('throws when name is invalid', () { test('throws when name is invalid', () {
expect( expect(
() => repo.renameBranch(oldName: 'feature', newName: 'inv@{id'), () => Branch.rename(
repo: repo,
oldName: 'feature',
newName: 'inv@{id',
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
}); });
test('returns string representation of Branch object', () { test('returns string representation of Branch object', () {
final branch = repo.lookupBranch(name: 'master'); final branch = Branch.lookup(repo: repo, name: 'master');
expect(branch.toString(), contains('Branch{')); expect(branch.toString(), contains('Branch{'));
branch.free(); branch.free();
}); });

View file

@ -30,10 +30,7 @@ void main() {
time: 124, time: 124,
); );
tip = repo['821ed6e80627b8769d170a293862f9fc60825226']; tip = repo['821ed6e80627b8769d170a293862f9fc60825226'];
tree = Tree.lookup( tree = Tree.lookup(repo: repo, oid: repo['a8ae3dd']);
repo: repo,
oid: repo['a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f'],
);
}); });
tearDown(() { tearDown(() {
@ -46,14 +43,14 @@ void main() {
group('Commit', () { group('Commit', () {
test('lookups commit for provided oid', () { test('lookups commit for provided oid', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect(commit, isA<Commit>()); expect(commit, isA<Commit>());
commit.free(); commit.free();
}); });
test('throws when trying to lookup with invalid oid', () { test('throws when trying to lookup with invalid oid', () {
expect( expect(
() => repo.lookupCommit(repo['0' * 40]), () => Commit.lookup(repo: repo, oid: repo['0' * 40]),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -65,15 +62,14 @@ void main() {
}); });
test('reverts commit affecting index and workdir', () { test('reverts commit affecting index and workdir', () {
final commit = repo.lookupCommit( final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
repo['821ed6e80627b8769d170a293862f9fc60825226'],
);
final index = repo.index; final index = repo.index;
final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt')); final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt'));
expect(index.find('dir/dir_file.txt'), true); expect(index.find('dir/dir_file.txt'), true);
expect(file.existsSync(), true); expect(file.existsSync(), true);
repo.revert(commit); commit.revert();
expect(index.find('dir/dir_file.txt'), false); expect(index.find('dir/dir_file.txt'), false);
expect(file.existsSync(), false); expect(file.existsSync(), false);
@ -82,22 +78,18 @@ void main() {
}); });
test('throws when trying to revert and error occurs', () { test('throws when trying to revert and error occurs', () {
expect(() => repo.revert(Commit(nullptr)), throwsA(isA<LibGit2Error>())); expect(() => Commit(nullptr).revert(), throwsA(isA<LibGit2Error>()));
}); });
test('reverts commit to provided commit', () { test('reverts commit to provided commit', () {
final to = repo.lookupCommit( final to = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
repo['78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'], final from = Commit.lookup(repo: repo, oid: repo['821ed6e']);
);
final from = repo.lookupCommit(
repo['821ed6e80627b8769d170a293862f9fc60825226'],
);
final index = repo.index; final index = repo.index;
final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt')); final file = File(p.join(repo.workdir, 'dir', 'dir_file.txt'));
expect(index.find('dir/dir_file.txt'), true); expect(index.find('dir/dir_file.txt'), true);
expect(file.existsSync(), true); expect(file.existsSync(), true);
final revertIndex = repo.revertCommit(revertCommit: from, ourCommit: to); final revertIndex = from.revertTo(commit: to);
expect(revertIndex.find('dir/dir_file.txt'), false); expect(revertIndex.find('dir/dir_file.txt'), false);
expect(file.existsSync(), true); expect(file.existsSync(), true);
@ -110,17 +102,15 @@ void main() {
test('throws when trying to revert commit and error occurs', () { test('throws when trying to revert commit and error occurs', () {
final nullCommit = Commit(nullptr); final nullCommit = Commit(nullptr);
expect( expect(
() => repo.revertCommit( () => nullCommit.revertTo(commit: nullCommit),
revertCommit: nullCommit,
ourCommit: nullCommit,
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('creates commit', () { test('creates commit', () {
final parent = repo.lookupCommit(tip); final parent = Commit.lookup(repo: repo, oid: tip);
final oid = repo.createCommit( final oid = Commit.create(
repo: repo,
updateRef: 'HEAD', updateRef: 'HEAD',
message: message, message: message,
author: author, author: author,
@ -129,7 +119,7 @@ void main() {
parents: [parent], parents: [parent],
); );
final commit = repo.lookupCommit(oid); final commit = Commit.lookup(repo: repo, oid: oid);
expect(commit.oid, oid); expect(commit.oid, oid);
expect(commit.message, message); expect(commit.message, message);
@ -150,7 +140,7 @@ void main() {
}); });
test('writes commit into the buffer', () { test('writes commit into the buffer', () {
final parent = repo.lookupCommit(tip); final parent = Commit.lookup(repo: repo, oid: tip);
final commit = Commit.createBuffer( final commit = Commit.createBuffer(
repo: repo, repo: repo,
updateRef: 'HEAD', updateRef: 'HEAD',
@ -178,7 +168,8 @@ Some description.
}); });
test('creates commit without parents', () { test('creates commit without parents', () {
final oid = repo.createCommit( final oid = Commit.create(
repo: repo,
updateRef: 'refs/heads/new', updateRef: 'refs/heads/new',
message: message, message: message,
author: author, author: author,
@ -187,7 +178,7 @@ Some description.
parents: [], parents: [],
); );
final commit = repo.lookupCommit(oid); final commit = Commit.lookup(repo: repo, oid: oid);
expect(commit.oid, oid); expect(commit.oid, oid);
expect(commit.message, message); expect(commit.message, message);
@ -202,10 +193,8 @@ Some description.
}); });
test('creates commit with 2 parents', () { test('creates commit with 2 parents', () {
final parent1 = repo.lookupCommit(tip); final parent1 = Commit.lookup(repo: repo, oid: tip);
final parent2 = repo.lookupCommit( final parent2 = Commit.lookup(repo: repo, oid: repo['fc38877']);
repo['fc38877b2552ab554752d9a77e1f48f738cca79b'],
);
final oid = Commit.create( final oid = Commit.create(
updateRef: 'HEAD', updateRef: 'HEAD',
@ -217,7 +206,7 @@ Some description.
parents: [parent1, parent2], parents: [parent1, parent2],
); );
final commit = repo.lookupCommit(oid); final commit = Commit.lookup(repo: repo, oid: oid);
expect(commit.oid, oid); expect(commit.oid, oid);
expect(commit.message, message); expect(commit.message, message);
@ -236,11 +225,12 @@ Some description.
}); });
test('throws when trying to create commit and error occurs', () { test('throws when trying to create commit and error occurs', () {
final parent = repo.lookupCommit(tip); final parent = Commit.lookup(repo: repo, oid: tip);
final nullRepo = Repository(nullptr); final nullRepo = Repository(nullptr);
expect( expect(
() => nullRepo.createCommit( () => Commit.create(
repo: nullRepo,
updateRef: 'HEAD', updateRef: 'HEAD',
message: message, message: message,
author: author, author: author,
@ -256,7 +246,7 @@ Some description.
test('throws when trying to write commit into a buffer and error occurs', test('throws when trying to write commit into a buffer and error occurs',
() { () {
final parent = repo.lookupCommit(tip); final parent = Commit.lookup(repo: repo, oid: tip);
final nullRepo = Repository(nullptr); final nullRepo = Repository(nullptr);
expect( expect(
@ -277,15 +267,16 @@ Some description.
test('amends commit with default arguments', () { test('amends commit with default arguments', () {
final oldHead = repo.head; final oldHead = repo.head;
final commit = repo.lookupCommit(repo['821ed6e']); final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
expect(commit.oid, oldHead.target); expect(commit.oid, oldHead.target);
final amendedOid = repo.amendCommit( final amendedOid = Commit.amend(
repo: repo,
commit: commit, commit: commit,
message: 'amended commit\n', message: 'amended commit\n',
updateRef: 'HEAD', updateRef: 'HEAD',
); );
final amendedCommit = repo.lookupCommit(amendedOid); final amendedCommit = Commit.lookup(repo: repo, oid: amendedOid);
final newHead = repo.head; final newHead = repo.head;
expect(amendedCommit.oid, newHead.target); expect(amendedCommit.oid, newHead.target);
@ -303,10 +294,11 @@ Some description.
test('amends commit with provided arguments', () { test('amends commit with provided arguments', () {
final oldHead = repo.head; final oldHead = repo.head;
final commit = repo.lookupCommit(repo['821ed6e']); final commit = Commit.lookup(repo: repo, oid: repo['821ed6e']);
expect(commit.oid, oldHead.target); expect(commit.oid, oldHead.target);
final amendedOid = repo.amendCommit( final amendedOid = Commit.amend(
repo: repo,
commit: commit, commit: commit,
message: 'amended commit\n', message: 'amended commit\n',
updateRef: 'HEAD', updateRef: 'HEAD',
@ -314,7 +306,7 @@ Some description.
committer: committer, committer: committer,
tree: tree, tree: tree,
); );
final amendedCommit = repo.lookupCommit(amendedOid); final amendedCommit = Commit.lookup(repo: repo, oid: amendedOid);
final newHead = repo.head; final newHead = repo.head;
expect(amendedCommit.oid, newHead.target); expect(amendedCommit.oid, newHead.target);
@ -332,11 +324,12 @@ Some description.
test('amends commit that is not the tip of the branch', () { test('amends commit that is not the tip of the branch', () {
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(repo['78b8bf1']); final commit = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
expect(commit.oid, isNot(head.target)); expect(commit.oid, isNot(head.target));
expect( expect(
() => repo.amendCommit( () => Commit.amend(
repo: repo,
updateRef: null, updateRef: null,
commit: commit, commit: commit,
message: 'amended commit\n', message: 'amended commit\n',
@ -352,11 +345,12 @@ Some description.
'throws when trying to amend commit that is not the tip of the branch ' 'throws when trying to amend commit that is not the tip of the branch '
'with HEAD provided as update reference', () { 'with HEAD provided as update reference', () {
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(repo['78b8bf1']); final commit = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
expect(commit.oid, isNot(head.target)); expect(commit.oid, isNot(head.target));
expect( expect(
() => repo.amendCommit( () => Commit.amend(
repo: repo,
commit: commit, commit: commit,
message: 'amended commit\n', message: 'amended commit\n',
updateRef: 'HEAD', updateRef: 'HEAD',
@ -369,7 +363,7 @@ Some description.
}); });
test('creates an in-memory copy of a commit', () { test('creates an in-memory copy of a commit', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
final dupCommit = commit.duplicate(); final dupCommit = commit.duplicate();
expect(dupCommit.oid, commit.oid); expect(dupCommit.oid, commit.oid);
@ -379,7 +373,7 @@ Some description.
}); });
test('returns header field', () { test('returns header field', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect( expect(
commit.headerField('parent'), commit.headerField('parent'),
'78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8', '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8',
@ -388,7 +382,7 @@ Some description.
}); });
test('throws when header field not found', () { test('throws when header field not found', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect( expect(
() => commit.headerField('not-there'), () => commit.headerField('not-there'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
@ -397,7 +391,7 @@ Some description.
}); });
test('returns nth generation ancestor commit', () { test('returns nth generation ancestor commit', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
final ancestor = commit.nthGenAncestor(3); final ancestor = commit.nthGenAncestor(3);
expect(ancestor.oid.sha, 'f17d0d48eae3aa08cecf29128a35e310c97b3521'); expect(ancestor.oid.sha, 'f17d0d48eae3aa08cecf29128a35e310c97b3521');
@ -408,15 +402,13 @@ Some description.
test('throws when trying to get nth generation ancestor and none exists', test('throws when trying to get nth generation ancestor and none exists',
() { () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect(() => commit.nthGenAncestor(10), throwsA(isA<LibGit2Error>())); expect(() => commit.nthGenAncestor(10), throwsA(isA<LibGit2Error>()));
commit.free(); commit.free();
}); });
test('returns parent at specified position', () { test('returns parent at specified position', () {
final commit = repo.lookupCommit( final commit = Commit.lookup(repo: repo, oid: repo['78b8bf1']);
repo['78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'],
);
final firstParent = commit.parent(0); final firstParent = commit.parent(0);
final secondParent = commit.parent(1); final secondParent = commit.parent(1);
@ -429,13 +421,13 @@ Some description.
}); });
test('throws when trying to get the parent at invalid position', () { test('throws when trying to get the parent at invalid position', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect(() => commit.parent(10), throwsA(isA<LibGit2Error>())); expect(() => commit.parent(10), throwsA(isA<LibGit2Error>()));
commit.free(); commit.free();
}); });
test('returns string representation of Commit object', () { test('returns string representation of Commit object', () {
final commit = repo.lookupCommit(tip); final commit = Commit.lookup(repo: repo, oid: tip);
expect(commit.toString(), contains('Commit{')); expect(commit.toString(), contains('Commit{'));
commit.free(); commit.free();
}); });

View file

@ -120,7 +120,7 @@ index e69de29..c217c63 100644
group('Diff', () { group('Diff', () {
test('returns diff between index and workdir', () { test('returns diff between index and workdir', () {
final index = repo.index; final index = repo.index;
final diff = repo.diff(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
expect(diff.length, 8); expect(diff.length, 8);
for (var i = 0; i < diff.deltas.length; i++) { for (var i = 0; i < diff.deltas.length; i++) {
@ -134,34 +134,27 @@ index e69de29..c217c63 100644
test('returns diff between index and tree', () { test('returns diff between index and tree', () {
final index = repo.index; final index = repo.index;
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final tree = commit.tree; final tree = commit.tree;
final diff1 = index.diffToTree(tree: tree); final diff = Diff.treeToIndex(repo: repo, tree: tree, index: index);
final diff2 = tree.diffToIndex(index: index);
expect(diff1.length, 8); expect(diff.length, 8);
for (var i = 0; i < diff1.deltas.length; i++) { for (var i = 0; i < diff.deltas.length; i++) {
expect(diff1.deltas[i].newFile.path, indexToTree[i]); expect(diff.deltas[i].newFile.path, indexToTree[i]);
}
expect(diff2.length, 8);
for (var i = 0; i < diff2.deltas.length; i++) {
expect(diff2.deltas[i].newFile.path, indexToTree[i]);
} }
commit.free(); commit.free();
head.free(); head.free();
tree.free(); tree.free();
diff1.free(); diff.free();
diff2.free();
index.free(); index.free();
}); });
test('returns diff between tree and workdir', () { test('returns diff between tree and workdir', () {
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final tree = commit.tree; final tree = commit.tree;
final diff = repo.diff(a: tree); final diff = Diff.treeToWorkdir(repo: repo, tree: tree);
expect(diff.length, 9); expect(diff.length, 9);
for (var i = 0; i < diff.deltas.length; i++) { for (var i = 0; i < diff.deltas.length; i++) {
@ -176,65 +169,12 @@ index e69de29..c217c63 100644
test('throws when trying to diff between tree and workdir and error occurs', test('throws when trying to diff between tree and workdir and error occurs',
() { () {
final nullRepo = Repository(nullptr);
final nullTree = Tree(nullptr);
expect(() => nullRepo.diff(a: nullTree), throwsA(isA<LibGit2Error>()));
});
test('returns diff between tree and index', () {
final index = repo.index;
final head = repo.head;
final commit = repo.lookupCommit(head.target);
final tree = commit.tree;
final diff = repo.diff(a: tree, cached: true);
expect(diff.length, 8);
for (var i = 0; i < diff.deltas.length; i++) {
expect(diff.deltas[i].newFile.path, indexToTree[i]);
}
commit.free();
head.free();
tree.free();
diff.free();
index.free();
});
test('returns diff between tree and tree', () {
final head = repo.head;
final commit = repo.lookupCommit(head.target);
final tree1 = commit.tree;
final tree2 = repo.lookupTree(repo['b85d53c']);
final diff = repo.diff(a: tree1, b: tree2);
expect(diff.length, 10);
for (var i = 0; i < diff.deltas.length; i++) {
expect(diff.deltas[i].newFile.path, treeToTree[i]);
}
commit.free();
head.free();
tree1.free();
tree2.free();
diff.free();
});
test('throws when trying to diff between tree and tree and error occurs',
() {
final nullRepo = Repository(nullptr);
final nullTree = Tree(nullptr);
expect( expect(
() => nullRepo.diff(a: nullTree, b: nullTree), () => Diff.treeToWorkdir(repo: Repository(nullptr), tree: null),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('throws when trying to diff between null and tree', () {
final tree = repo.lookupTree(repo['b85d53c']);
expect(() => repo.diff(b: tree), throwsA(isA<ArgumentError>()));
tree.free();
});
test('returns diff between tree and workdir with index', () { test('returns diff between tree and workdir with index', () {
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = repo.lookupCommit(head.target);
@ -264,11 +204,54 @@ index e69de29..c217c63 100644
); );
}); });
test('returns diff between tree and tree', () {
final head = repo.head;
final commit = Commit.lookup(repo: repo, oid: head.target);
final tree1 = commit.tree;
final tree2 = Tree.lookup(repo: repo, oid: repo['b85d53c']);
final diff = Diff.treeToTree(repo: repo, oldTree: tree1, newTree: tree2);
expect(diff.length, 10);
for (var i = 0; i < diff.deltas.length; i++) {
expect(diff.deltas[i].newFile.path, treeToTree[i]);
}
commit.free();
head.free();
tree1.free();
tree2.free();
diff.free();
});
test('throws when trying to diff between tree and tree and error occurs',
() {
final nullTree = Tree(nullptr);
expect(
() => Diff.treeToTree(
repo: Repository(nullptr),
oldTree: nullTree,
newTree: nullTree,
),
throwsA(isA<LibGit2Error>()),
);
});
test('throws when trying to diff between two null trees', () {
expect(
() => Diff.treeToTree(repo: repo, oldTree: null, newTree: null),
throwsA(isA<ArgumentError>()),
);
});
test('returns diff between index and index', () { test('returns diff between index and index', () {
final index = repo.index; final index = repo.index;
final emptyIndex = Index.newInMemory(); final emptyIndex = Index.newInMemory();
final diff = index.diffToIndex(index: emptyIndex); final diff = Diff.indexToIndex(
repo: repo,
oldIndex: index,
newIndex: emptyIndex,
);
expect(diff.length, 12); expect(diff.length, 12);
for (var i = 0; i < diff.deltas.length; i++) { for (var i = 0; i < diff.deltas.length; i++) {
@ -284,7 +267,11 @@ index e69de29..c217c63 100644
final index = repo.index; final index = repo.index;
expect( expect(
() => index.diffToIndex(index: Index(nullptr)), () => Diff.indexToIndex(
repo: repo,
oldIndex: index,
newIndex: Index(nullptr),
),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
@ -293,13 +280,11 @@ index e69de29..c217c63 100644
test('merges diffs', () { test('merges diffs', () {
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final tree1 = commit.tree; final tree1 = commit.tree;
final tree2 = repo.lookupTree( final tree2 = Tree.lookup(repo: repo, oid: repo['b85d53c']);
repo['b85d53c9236e89aff2b62558adaa885fd1d6ff1c'], final diff1 = Diff.treeToTree(repo: repo, oldTree: tree1, newTree: tree2);
); final diff2 = Diff.treeToWorkdir(repo: repo, tree: tree1);
final diff1 = tree1.diffToTree(tree: tree2);
final diff2 = tree1.diffToWorkdir();
expect(diff1.length, 10); expect(diff1.length, 10);
expect(diff2.length, 9); expect(diff2.length, 9);
@ -330,28 +315,31 @@ index e69de29..c217c63 100644
group('apply', () { group('apply', () {
test('checks if diff can be applied to repository', () { test('checks if diff can be applied to repository', () {
final diff1 = repo.diff(); final index = repo.index;
final diff1 = Diff.indexToWorkdir(repo: repo, index: index);
expect( expect(
repo.applies(diff: diff1, location: GitApplyLocation.both), diff1.applies(repo: repo, location: GitApplyLocation.both),
false, false,
); );
final diff2 = Diff.parse(patchText); final diff2 = Diff.parse(patchText);
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect( expect(
repo.applies(diff: diff2, location: GitApplyLocation.both), diff2.applies(repo: repo, location: GitApplyLocation.both),
true, true,
); );
diff1.free(); diff1.free();
diff2.free(); diff2.free();
index.free();
}); });
test('checks if hunk with provided index can be applied to repository', test('checks if hunk with provided index can be applied to repository',
() { () {
final diff1 = repo.diff(); final index = repo.index;
final diff1 = Diff.indexToWorkdir(repo: repo, index: index);
expect( expect(
repo.applies(diff: diff1, location: GitApplyLocation.both), diff1.applies(repo: repo, location: GitApplyLocation.both),
false, false,
); );
@ -359,8 +347,8 @@ index e69de29..c217c63 100644
final hunk = diff2.patches.first.hunks.first; final hunk = diff2.patches.first.hunks.first;
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect( expect(
repo.applies( diff2.applies(
diff: diff2, repo: repo,
hunkIndex: hunk.index, hunkIndex: hunk.index,
location: GitApplyLocation.both, location: GitApplyLocation.both,
), ),
@ -369,6 +357,7 @@ index e69de29..c217c63 100644
diff1.free(); diff1.free();
diff2.free(); diff2.free();
index.free();
}); });
test('applies diff to repository', () { test('applies diff to repository', () {
@ -378,15 +367,17 @@ index e69de29..c217c63 100644
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(file.readAsStringSync(), ''); expect(file.readAsStringSync(), '');
repo.apply(diff: diff); diff.apply(repo: repo);
expect(file.readAsStringSync(), 'Modified content\n'); expect(file.readAsStringSync(), 'Modified content\n');
diff.free(); diff.free();
}); });
test('throws when trying to apply diff and error occurs', () { test('throws when trying to apply diff and error occurs', () {
final nullDiff = Diff(nullptr); expect(
expect(() => repo.apply(diff: nullDiff), throwsA(isA<LibGit2Error>())); () => Diff(nullptr).apply(repo: repo),
throwsA(isA<LibGit2Error>()),
);
}); });
test('creates patch from entry index in diff', () { test('creates patch from entry index in diff', () {
@ -408,7 +399,7 @@ index e69de29..c217c63 100644
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
expect(file.readAsStringSync(), ''); expect(file.readAsStringSync(), '');
repo.apply(diff: diff, hunkIndex: hunk.index); diff.apply(repo: repo, hunkIndex: hunk.index);
expect(file.readAsStringSync(), 'Modified content\n'); expect(file.readAsStringSync(), 'Modified content\n');
diff.free(); diff.free();
@ -419,15 +410,21 @@ index e69de29..c217c63 100644
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final tree = commit.tree; final tree = commit.tree;
final oldIndex = repo.index; final oldIndex = repo.index;
final oldBlob = repo.lookupBlob(oldIndex['subdir/modified_file'].oid); final oldBlob = Blob.lookup(
repo: repo,
oid: oldIndex['subdir/modified_file'].oid,
);
expect(oldBlob.content, ''); expect(oldBlob.content, '');
final newIndex = repo.applyToTree(diff: diff, tree: tree); final newIndex = diff.applyToTree(repo: repo, tree: tree);
final newBlob = repo.lookupBlob(newIndex['subdir/modified_file'].oid); final newBlob = Blob.lookup(
repo: repo,
oid: newIndex['subdir/modified_file'].oid,
);
expect(newBlob.content, 'Modified content\n'); expect(newBlob.content, 'Modified content\n');
oldBlob.free(); oldBlob.free();
@ -446,19 +443,25 @@ index e69de29..c217c63 100644
repo.checkout(target: 'HEAD', strategy: {GitCheckout.force}); repo.checkout(target: 'HEAD', strategy: {GitCheckout.force});
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final tree = commit.tree; final tree = commit.tree;
final oldIndex = repo.index; final oldIndex = repo.index;
final oldBlob = repo.lookupBlob(oldIndex['subdir/modified_file'].oid); final oldBlob = Blob.lookup(
repo: repo,
oid: oldIndex['subdir/modified_file'].oid,
);
expect(oldBlob.content, ''); expect(oldBlob.content, '');
final newIndex = repo.applyToTree( final newIndex = diff.applyToTree(
diff: diff, repo: repo,
tree: tree, tree: tree,
hunkIndex: hunk.index, hunkIndex: hunk.index,
); );
final newBlob = repo.lookupBlob(newIndex['subdir/modified_file'].oid); final newBlob = Blob.lookup(
repo: repo,
oid: newIndex['subdir/modified_file'].oid,
);
expect(newBlob.content, 'Modified content\n'); expect(newBlob.content, 'Modified content\n');
oldBlob.free(); oldBlob.free();
@ -474,7 +477,7 @@ index e69de29..c217c63 100644
test('throws when trying to apply diff to tree and error occurs', () { test('throws when trying to apply diff to tree and error occurs', () {
final diff = Diff.parse(patchText); final diff = Diff.parse(patchText);
expect( expect(
() => repo.applyToTree(diff: diff, tree: Tree(nullptr)), () => diff.applyToTree(repo: repo, tree: Tree(nullptr)),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -483,11 +486,15 @@ index e69de29..c217c63 100644
test('finds similar entries', () { test('finds similar entries', () {
final index = repo.index; final index = repo.index;
final head = repo.head; final head = repo.head;
final commit = repo.lookupCommit(head.target); final commit = Commit.lookup(repo: repo, oid: head.target);
final oldTree = commit.tree; final oldTree = commit.tree;
final newTree = repo.lookupTree(index.writeTree()); final newTree = Tree.lookup(repo: repo, oid: index.writeTree());
final diff = oldTree.diffToTree(tree: newTree); final diff = Diff.treeToTree(
repo: repo,
oldTree: oldTree,
newTree: newTree,
);
expect( expect(
diff.deltas.singleWhere((e) => e.newFile.path == 'staged_new').status, diff.deltas.singleWhere((e) => e.newFile.path == 'staged_new').status,
GitDelta.added, GitDelta.added,
@ -519,7 +526,7 @@ index e69de29..c217c63 100644
test('returns deltas', () { test('returns deltas', () {
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
expect(diff.deltas[0].numberOfFiles, 1); expect(diff.deltas[0].numberOfFiles, 1);
expect(diff.deltas[0].status, GitDelta.deleted); expect(diff.deltas[0].status, GitDelta.deleted);
@ -553,7 +560,7 @@ index e69de29..c217c63 100644
test('throws when trying to get delta with invalid index', () { test('throws when trying to get delta with invalid index', () {
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
expect(() => diff.deltas[-1], throwsA(isA<RangeError>())); expect(() => diff.deltas[-1], throwsA(isA<RangeError>()));
@ -563,7 +570,7 @@ index e69de29..c217c63 100644
test('returns patches', () { test('returns patches', () {
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
final patches = diff.patches; final patches = diff.patches;
expect(patches.length, 8); expect(patches.length, 8);
@ -578,7 +585,7 @@ index e69de29..c217c63 100644
test('returns stats', () { test('returns stats', () {
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
final stats = diff.stats; final stats = diff.stats;
expect(stats.insertions, 4); expect(stats.insertions, 4);

View file

@ -35,7 +35,7 @@ void main() {
group('Note', () { group('Note', () {
test('returns list of notes', () { test('returns list of notes', () {
final notes = repo.notes; final notes = Note.list(repo);
expect(notes.length, 2); expect(notes.length, 2);
@ -49,12 +49,12 @@ void main() {
test('throws when trying to get list of notes and error occurs', () { test('throws when trying to get list of notes and error occurs', () {
Directory(p.join(repo.path, 'refs', 'notes')).deleteSync(recursive: true); Directory(p.join(repo.path, 'refs', 'notes')).deleteSync(recursive: true);
expect(() => repo.notes, throwsA(isA<LibGit2Error>())); expect(() => Note.list(repo), throwsA(isA<LibGit2Error>()));
}); });
test('lookups note', () { test('lookups note', () {
final head = repo.head; final head = repo.head;
final note = repo.lookupNote(annotatedOid: head.target); final note = Note.lookup(repo: repo, annotatedOid: head.target);
expect(note.oid.sha, notesExpected[1]['oid']); expect(note.oid.sha, notesExpected[1]['oid']);
expect(note.message, notesExpected[1]['message']); expect(note.message, notesExpected[1]['message']);
@ -71,14 +71,15 @@ void main() {
time: 1234, time: 1234,
); );
final head = repo.head; final head = repo.head;
final noteOid = repo.createNote( final noteOid = Note.create(
repo: repo,
author: signature, author: signature,
committer: signature, committer: signature,
annotatedOid: head.target, annotatedOid: head.target,
note: 'New note for HEAD', note: 'New note for HEAD',
force: true, force: true,
); );
final noteBlob = repo.lookupBlob(noteOid); final noteBlob = Blob.lookup(repo: repo, oid: noteOid);
expect(noteOid.sha, 'ffd6e2ceaf91c00ea6d29e2e897f906da720529f'); expect(noteOid.sha, 'ffd6e2ceaf91c00ea6d29e2e897f906da720529f');
expect(noteBlob.content, 'New note for HEAD'); expect(noteBlob.content, 'New note for HEAD');
@ -90,7 +91,8 @@ void main() {
test('throws when trying to create note and error occurs', () { test('throws when trying to create note and error occurs', () {
expect( expect(
() => Repository(nullptr).createNote( () => Note.create(
repo: Repository(nullptr),
author: Signature(nullptr), author: Signature(nullptr),
committer: Signature(nullptr), committer: Signature(nullptr),
annotatedOid: repo['0' * 40], annotatedOid: repo['0' * 40],
@ -108,14 +110,15 @@ void main() {
); );
final head = repo.head; final head = repo.head;
repo.deleteNote( Note.delete(
repo: repo,
annotatedOid: repo.head.target, annotatedOid: repo.head.target,
author: signature, author: signature,
committer: signature, committer: signature,
); );
expect( expect(
() => repo.lookupNote(annotatedOid: head.target), () => Note.lookup(repo: repo, annotatedOid: head.target),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
@ -125,7 +128,8 @@ void main() {
test('throws when trying to delete note and error occurs', () { test('throws when trying to delete note and error occurs', () {
expect( expect(
() => Repository(nullptr).deleteNote( () => Note.delete(
repo: Repository(nullptr),
author: Signature(nullptr), author: Signature(nullptr),
committer: Signature(nullptr), committer: Signature(nullptr),
annotatedOid: repo['0' * 40], annotatedOid: repo['0' * 40],
@ -135,7 +139,7 @@ void main() {
}); });
test('returns string representation of Note object', () { test('returns string representation of Note object', () {
final note = repo.lookupNote(annotatedOid: repo['821ed6e']); final note = Note.lookup(repo: repo, annotatedOid: repo['821ed6e']);
expect(note.toString(), contains('Note{')); expect(note.toString(), contains('Note{'));
note.free(); note.free();
}); });

View file

@ -188,10 +188,14 @@ void main() {
test('packs with provided packDelegate', () { test('packs with provided packDelegate', () {
Directory(packDirPath).createSync(); Directory(packDirPath).createSync();
void packDelegate(PackBuilder packBuilder) { void packDelegate(PackBuilder packBuilder) {
final branches = repo.branchesLocal; final branches = repo.branchesLocal;
for (final branch in branches) { for (final branch in branches) {
final ref = repo.lookupReference('refs/heads/${branch.name}'); final ref = Reference.lookup(
repo: repo,
name: 'refs/heads/${branch.name}',
);
for (final commit in repo.log(oid: ref.target)) { for (final commit in repo.log(oid: ref.target)) {
packBuilder.addRecursively(commit.oid); packBuilder.addRecursively(commit.oid);
commit.free(); commit.free();

View file

@ -96,8 +96,8 @@ index e69de29..0000000
}); });
test('creates from blobs', () { test('creates from blobs', () {
final a = repo.lookupBlob(oldBlobOid); final a = Blob.lookup(repo: repo, oid: oldBlobOid);
final b = repo.lookupBlob(newBlobOid); final b = Blob.lookup(repo: repo, oid: newBlobOid);
final patch = Patch.create( final patch = Patch.create(
a: a, a: a,
b: b, b: b,
@ -111,7 +111,7 @@ index e69de29..0000000
}); });
test('creates from one blob (add)', () { test('creates from one blob (add)', () {
final b = repo.lookupBlob(newBlobOid); final b = Blob.lookup(repo: repo, oid: newBlobOid);
final patch = Patch.create( final patch = Patch.create(
a: null, a: null,
b: b, b: b,
@ -125,7 +125,7 @@ index e69de29..0000000
}); });
test('creates from one blob (delete)', () { test('creates from one blob (delete)', () {
final a = repo.lookupBlob(oldBlobOid); final a = Blob.lookup(repo: repo, oid: oldBlobOid);
final patch = Patch.create( final patch = Patch.create(
a: a, a: a,
b: null, b: null,
@ -139,7 +139,7 @@ index e69de29..0000000
}); });
test('creates from blob and buffer', () { test('creates from blob and buffer', () {
final a = repo.lookupBlob(oldBlobOid); final a = Blob.lookup(repo: repo, oid: oldBlobOid);
final patch = Patch.create( final patch = Patch.create(
a: a, a: a,
b: newBlob, b: newBlob,
@ -153,9 +153,8 @@ index e69de29..0000000
}); });
test('throws when argument is not Blob or String', () { test('throws when argument is not Blob or String', () {
final commit = repo.lookupCommit( final commit = Commit.lookup(repo: repo, oid: repo['fc38877']);
repo['fc38877b2552ab554752d9a77e1f48f738cca79b'],
);
expect( expect(
() => Patch.create( () => Patch.create(
a: commit, a: commit,

View file

@ -32,12 +32,12 @@ void main() {
email: 'author@email.com', email: 'author@email.com',
time: 1234, time: 1234,
); );
final master = repo.lookupReference('refs/heads/master'); final master = Reference.lookup(repo: repo, name: 'refs/heads/master');
final branchHead = AnnotatedCommit.fromReference( final branchHead = AnnotatedCommit.fromReference(
repo: repo, repo: repo,
reference: master, reference: master,
); );
final feature = repo.lookupReference('refs/heads/feature'); final feature = Reference.lookup(repo: repo, name: 'refs/heads/feature');
final ontoHead = AnnotatedCommit.fromReference( final ontoHead = AnnotatedCommit.fromReference(
repo: repo, repo: repo,
reference: feature, reference: feature,
@ -90,7 +90,7 @@ void main() {
email: 'author@email.com', email: 'author@email.com',
time: 1234, time: 1234,
); );
final feature = repo.lookupReference('refs/heads/feature'); final feature = Reference.lookup(repo: repo, name: 'refs/heads/feature');
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: feature.target); final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: feature.target);
final rebase = Rebase.init( final rebase = Rebase.init(
@ -136,9 +136,9 @@ void main() {
email: 'author@email.com', email: 'author@email.com',
time: 1234, time: 1234,
); );
final master = repo.lookupReference('refs/heads/master'); final master = Reference.lookup(repo: repo, name: 'refs/heads/master');
final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target); final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target);
final feature = repo.lookupReference('refs/heads/feature'); final feature = Reference.lookup(repo: repo, name: 'refs/heads/feature');
final upstream = AnnotatedCommit.lookup(repo: repo, oid: repo[shas[1]]); final upstream = AnnotatedCommit.lookup(repo: repo, oid: repo[shas[1]]);
repo.checkout(target: feature.name); repo.checkout(target: feature.name);
@ -189,9 +189,12 @@ void main() {
email: 'author@email.com', email: 'author@email.com',
time: 1234, time: 1234,
); );
final master = repo.lookupReference('refs/heads/master'); final master = Reference.lookup(repo: repo, name: 'refs/heads/master');
final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target); final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target);
final conflict = repo.lookupReference('refs/heads/conflict-branch'); final conflict = Reference.lookup(
repo: repo,
name: 'refs/heads/conflict-branch',
);
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target); final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(target: conflict.name); repo.checkout(target: conflict.name);
@ -226,9 +229,12 @@ void main() {
email: 'author@email.com', email: 'author@email.com',
time: 1234, time: 1234,
); );
final master = repo.lookupReference('refs/heads/master'); final master = Reference.lookup(repo: repo, name: 'refs/heads/master');
final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target); final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target);
final conflict = repo.lookupReference('refs/heads/conflict-branch'); final conflict = Reference.lookup(
repo: repo,
name: 'refs/heads/conflict-branch',
);
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target); final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(target: conflict.name); repo.checkout(target: conflict.name);
@ -252,9 +258,12 @@ void main() {
}); });
test('aborts rebase in progress', () { test('aborts rebase in progress', () {
final master = repo.lookupReference('refs/heads/master'); final master = Reference.lookup(repo: repo, name: 'refs/heads/master');
final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target); final branchHead = AnnotatedCommit.lookup(repo: repo, oid: master.target);
final conflict = repo.lookupReference('refs/heads/conflict-branch'); final conflict = Reference.lookup(
repo: repo,
name: 'refs/heads/conflict-branch',
);
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target); final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: conflict.target);
repo.checkout(target: conflict.name); repo.checkout(target: conflict.name);
@ -282,7 +291,7 @@ void main() {
}); });
test('opens an existing rebase', () { test('opens an existing rebase', () {
final feature = repo.lookupReference('refs/heads/feature'); final feature = Reference.lookup(repo: repo, name: 'refs/heads/feature');
final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: feature.target); final ontoHead = AnnotatedCommit.lookup(repo: repo, oid: feature.target);
final rebase = Rebase.init( final rebase = Rebase.init(

View file

@ -26,7 +26,7 @@ void main() {
group('Reference', () { group('Reference', () {
test('returns a list', () { test('returns a list', () {
expect( expect(
repo.references, Reference.list(repo),
[ [
'refs/heads/feature', 'refs/heads/feature',
'refs/heads/master', 'refs/heads/master',
@ -40,7 +40,7 @@ void main() {
test('throws when trying to get a list of references and error occurs', () { test('throws when trying to get a list of references and error occurs', () {
expect( expect(
() => Repository(nullptr).references, () => Reference.list(Repository(nullptr)),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -50,7 +50,7 @@ void main() {
expect(head.type, ReferenceType.direct); expect(head.type, ReferenceType.direct);
head.free(); head.free();
final ref = repo.lookupReference('HEAD'); final ref = Reference.lookup(repo: repo, name: 'HEAD');
expect(ref.type, ReferenceType.symbolic); expect(ref.type, ReferenceType.symbolic);
ref.free(); ref.free();
}); });
@ -62,7 +62,7 @@ void main() {
}); });
test('returns SHA hex of symbolic reference', () { test('returns SHA hex of symbolic reference', () {
final ref = repo.lookupReference('HEAD'); final ref = Reference.lookup(repo: repo, name: 'HEAD');
expect(ref.target.sha, lastCommit); expect(ref.target.sha, lastCommit);
ref.free(); ref.free();
}); });
@ -81,7 +81,8 @@ void main() {
}); });
test('returns the short name', () { test('returns the short name', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/remotes/origin/upstream', name: 'refs/remotes/origin/upstream',
target: repo[lastCommit], target: repo[lastCommit],
); );
@ -96,34 +97,37 @@ void main() {
}); });
test('checks if reference is a local branch', () { test('checks if reference is a local branch', () {
final ref = repo.lookupReference('refs/heads/feature'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/feature');
expect(ref.isBranch, true); expect(ref.isBranch, true);
ref.free(); ref.free();
}); });
test('checks if reference is a note', () { test('checks if reference is a note', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
expect(ref.isNote, false); expect(ref.isNote, false);
ref.free(); ref.free();
}); });
test('checks if reference is a remote branch', () { test('checks if reference is a remote branch', () {
final ref = repo.lookupReference('refs/remotes/origin/master'); final ref = Reference.lookup(
repo: repo,
name: 'refs/remotes/origin/master',
);
expect(ref.isRemote, true); expect(ref.isRemote, true);
ref.free(); ref.free();
}); });
test('checks if reference is a tag', () { test('checks if reference is a tag', () {
final ref = repo.lookupReference('refs/tags/v0.1'); final ref = Reference.lookup(repo: repo, name: 'refs/tags/v0.1');
expect(ref.isTag, true); expect(ref.isTag, true);
ref.free(); ref.free();
}); });
test('checks if reflog exists for the reference', () { test('checks if reflog exists for the reference', () {
var ref = repo.lookupReference('refs/heads/master'); var ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
expect(ref.hasLog, true); expect(ref.hasLog, true);
ref = repo.lookupReference('refs/tags/v0.1'); ref = Reference.lookup(repo: repo, name: 'refs/tags/v0.1');
expect(ref.hasLog, false); expect(ref.hasLog, false);
ref.free(); ref.free();
@ -132,7 +136,8 @@ void main() {
test('ensures updates to the reference will append to its log', () { test('ensures updates to the reference will append to its log', () {
Reference.ensureLog(repo: repo, refName: 'refs/tags/tag'); Reference.ensureLog(repo: repo, refName: 'refs/tags/tag');
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/tag', name: 'refs/tags/tag',
target: repo[lastCommit], target: repo[lastCommit],
); );
@ -157,7 +162,7 @@ void main() {
test('duplicates existing reference', () { test('duplicates existing reference', () {
expect(repo.references.length, 6); expect(repo.references.length, 6);
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
final duplicate = ref.duplicate(); final duplicate = ref.duplicate();
expect(repo.references.length, 6); expect(repo.references.length, 6);
@ -169,8 +174,9 @@ void main() {
group('create direct', () { group('create direct', () {
test('creates with oid as target', () { test('creates with oid as target', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
final refFromOid = repo.createReference( final refFromOid = Reference.create(
repo: repo,
name: 'refs/tags/from.oid', name: 'refs/tags/from.oid',
target: ref.target, target: ref.target,
); );
@ -183,7 +189,8 @@ void main() {
test('creates with log message', () { test('creates with log message', () {
repo.setIdentity(name: 'name', email: 'email'); repo.setIdentity(name: 'name', email: 'email');
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/heads/log.message', name: 'refs/heads/log.message',
target: repo[lastCommit], target: repo[lastCommit],
logMessage: 'log message', logMessage: 'log message',
@ -202,7 +209,8 @@ void main() {
test('throws if target is not valid', () { test('throws if target is not valid', () {
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/invalid', name: 'refs/tags/invalid',
target: '78b', target: '78b',
), ),
@ -210,7 +218,8 @@ void main() {
); );
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/invalid', name: 'refs/tags/invalid',
target: 0, target: 0,
), ),
@ -220,7 +229,8 @@ void main() {
test('throws if name is not valid', () { test('throws if name is not valid', () {
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/invalid~', name: 'refs/tags/invalid~',
target: repo[lastCommit], target: repo[lastCommit],
), ),
@ -229,12 +239,14 @@ void main() {
}); });
test('creates with force flag if name already exists', () { test('creates with force flag if name already exists', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: repo[lastCommit], target: repo[lastCommit],
); );
final forceRef = repo.createReference( final forceRef = Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: repo[lastCommit], target: repo[lastCommit],
force: true, force: true,
@ -247,13 +259,15 @@ void main() {
}); });
test('throws if name already exists', () { test('throws if name already exists', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: repo[lastCommit], target: repo[lastCommit],
); );
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: repo[lastCommit], target: repo[lastCommit],
), ),
@ -266,7 +280,8 @@ void main() {
group('create symbolic', () { group('create symbolic', () {
test('creates with valid target', () { test('creates with valid target', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/symbolic', name: 'refs/tags/symbolic',
target: 'refs/heads/master', target: 'refs/heads/master',
); );
@ -278,12 +293,14 @@ void main() {
}); });
test('creates with force flag if name already exists', () { test('creates with force flag if name already exists', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: 'refs/heads/master', target: 'refs/heads/master',
); );
final forceRef = repo.createReference( final forceRef = Reference.create(
repo: repo,
name: 'refs/tags/test', name: 'refs/tags/test',
target: 'refs/heads/master', target: 'refs/heads/master',
force: true, force: true,
@ -297,13 +314,15 @@ void main() {
}); });
test('throws if name already exists', () { test('throws if name already exists', () {
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'refs/tags/exists', name: 'refs/tags/exists',
target: 'refs/heads/master', target: 'refs/heads/master',
); );
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/exists', name: 'refs/tags/exists',
target: 'refs/heads/master', target: 'refs/heads/master',
), ),
@ -315,7 +334,8 @@ void main() {
test('throws if name is not valid', () { test('throws if name is not valid', () {
expect( expect(
() => repo.createReference( () => Reference.create(
repo: repo,
name: 'refs/tags/invalid~', name: 'refs/tags/invalid~',
target: 'refs/heads/master', target: 'refs/heads/master',
), ),
@ -325,7 +345,8 @@ void main() {
test('creates with log message', () { test('creates with log message', () {
repo.setIdentity(name: 'name', email: 'email'); repo.setIdentity(name: 'name', email: 'email');
final ref = repo.createReference( final ref = Reference.create(
repo: repo,
name: 'HEAD', name: 'HEAD',
target: 'refs/heads/feature', target: 'refs/heads/feature',
force: true, force: true,
@ -347,27 +368,27 @@ void main() {
test('deletes reference', () { test('deletes reference', () {
expect(repo.references, contains('refs/tags/v0.1')); expect(repo.references, contains('refs/tags/v0.1'));
repo.deleteReference('refs/tags/v0.1'); Reference.delete(repo: repo, name: 'refs/tags/v0.1');
expect(repo.references, isNot(contains('refs/tags/v0.1'))); expect(repo.references, isNot(contains('refs/tags/v0.1')));
}); });
group('finds', () { group('finds', () {
test('with provided name', () { test('with provided name', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
expect(ref.target.sha, lastCommit); expect(ref.target.sha, lastCommit);
ref.free(); ref.free();
}); });
test('throws when error occured', () { test('throws when error occured', () {
expect( expect(
() => repo.lookupReference('refs/heads/not/there'), () => Reference.lookup(repo: repo, name: 'refs/heads/not/there'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
}); });
test('returns log for reference', () { test('returns log for reference', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
final reflog = ref.log; final reflog = ref.log;
expect(reflog.last.message, 'commit (initial): init'); expect(reflog.last.message, 'commit (initial): init');
@ -377,7 +398,7 @@ void main() {
group('set target', () { group('set target', () {
test('sets direct reference with provided oid target', () { test('sets direct reference with provided oid target', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
ref.setTarget(target: repo[newCommit]); ref.setTarget(target: repo[newCommit]);
expect(ref.target.sha, newCommit); expect(ref.target.sha, newCommit);
@ -385,7 +406,7 @@ void main() {
}); });
test('sets symbolic target with provided reference name', () { test('sets symbolic target with provided reference name', () {
final ref = repo.lookupReference('HEAD'); final ref = Reference.lookup(repo: repo, name: 'HEAD');
expect(ref.target.sha, lastCommit); expect(ref.target.sha, lastCommit);
ref.setTarget(target: 'refs/heads/feature'); ref.setTarget(target: 'refs/heads/feature');
@ -395,7 +416,7 @@ void main() {
}); });
test('sets target with log message', () { test('sets target with log message', () {
final ref = repo.lookupReference('HEAD'); final ref = Reference.lookup(repo: repo, name: 'HEAD');
expect(ref.target.sha, lastCommit); expect(ref.target.sha, lastCommit);
repo.setIdentity(name: 'name', email: 'email'); repo.setIdentity(name: 'name', email: 'email');
@ -411,7 +432,7 @@ void main() {
}); });
test('throws on invalid target', () { test('throws on invalid target', () {
final ref = repo.lookupReference('HEAD'); final ref = Reference.lookup(repo: repo, name: 'HEAD');
expect( expect(
() => ref.setTarget(target: 'refs/heads/invalid~'), () => ref.setTarget(target: 'refs/heads/invalid~'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
@ -430,7 +451,8 @@ void main() {
group('rename', () { group('rename', () {
test('renames reference', () { test('renames reference', () {
repo.renameReference( Reference.rename(
repo: repo,
oldName: 'refs/tags/v0.1', oldName: 'refs/tags/v0.1',
newName: 'refs/tags/renamed', newName: 'refs/tags/renamed',
); );
@ -441,7 +463,8 @@ void main() {
test('throws on invalid name', () { test('throws on invalid name', () {
expect( expect(
() => repo.renameReference( () => Reference.rename(
repo: repo,
oldName: 'refs/tags/v0.1', oldName: 'refs/tags/v0.1',
newName: 'refs/tags/invalid~', newName: 'refs/tags/invalid~',
), ),
@ -451,7 +474,8 @@ void main() {
test('throws if name already exists', () { test('throws if name already exists', () {
expect( expect(
() => repo.renameReference( () => Reference.rename(
repo: repo,
oldName: 'refs/tags/v0.1', oldName: 'refs/tags/v0.1',
newName: 'refs/tags/v0.2', newName: 'refs/tags/v0.2',
), ),
@ -460,20 +484,24 @@ void main() {
}); });
test('renames with force flag set to true', () { test('renames with force flag set to true', () {
final ref1 = repo.lookupReference('refs/tags/v0.1'); final ref1 = Reference.lookup(repo: repo, name: 'refs/tags/v0.1');
final ref2 = repo.lookupReference('refs/tags/v0.2'); final ref2 = Reference.lookup(repo: repo, name: 'refs/tags/v0.2');
expect(ref1.target.sha, '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8'); expect(ref1.target.sha, '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8');
expect(ref2.target.sha, 'f0fdbf506397e9f58c59b88dfdd72778ec06cc0c'); expect(ref2.target.sha, 'f0fdbf506397e9f58c59b88dfdd72778ec06cc0c');
expect(repo.references.length, 6); expect(repo.references.length, 6);
repo.renameReference( Reference.rename(
repo: repo,
oldName: 'refs/tags/v0.1', oldName: 'refs/tags/v0.1',
newName: 'refs/tags/v0.2', newName: 'refs/tags/v0.2',
force: true, force: true,
); );
final updatedRef1 = repo.lookupReference('refs/tags/v0.2'); final updatedRef1 = Reference.lookup(
repo: repo,
name: 'refs/tags/v0.2',
);
expect( expect(
updatedRef1.target.sha, updatedRef1.target.sha,
'78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8', '78b8bf123e3952c970ae5c1ce0a3ea1d1336f6e8',
@ -487,9 +515,9 @@ void main() {
}); });
test('checks equality', () { test('checks equality', () {
final ref1 = repo.lookupReference('refs/heads/master'); final ref1 = Reference.lookup(repo: repo, name: 'refs/heads/master');
final ref2 = repo.lookupReference('refs/heads/master'); final ref2 = Reference.lookup(repo: repo, name: 'refs/heads/master');
final ref3 = repo.lookupReference('refs/heads/feature'); final ref3 = Reference.lookup(repo: repo, name: 'refs/heads/feature');
expect(ref1 == ref2, true); expect(ref1 == ref2, true);
expect(ref1 != ref2, false); expect(ref1 != ref2, false);
@ -502,8 +530,8 @@ void main() {
}); });
test('peels to non-tag object when no type is provided', () { test('peels to non-tag object when no type is provided', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
final commit = repo.lookupCommit(ref.target); final commit = Commit.lookup(repo: repo, oid: ref.target);
final peeled = ref.peel() as Commit; final peeled = ref.peel() as Commit;
expect(peeled.oid, commit.oid); expect(peeled.oid, commit.oid);
@ -514,14 +542,15 @@ void main() {
}); });
test('peels to object of provided type', () { test('peels to object of provided type', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
final blob = repo.lookupBlob(repo['9c78c21']); final blob = Blob.lookup(repo: repo, oid: repo['9c78c21']);
final blobRef = repo.createReference( final blobRef = Reference.create(
repo: repo,
name: 'refs/tags/blob', name: 'refs/tags/blob',
target: blob.oid, target: blob.oid,
); );
final tagRef = repo.lookupReference('refs/tags/v0.2'); final tagRef = Reference.lookup(repo: repo, name: 'refs/tags/v0.2');
final commit = repo.lookupCommit(ref.target); final commit = Commit.lookup(repo: repo, oid: ref.target);
final tree = commit.tree; final tree = commit.tree;
final peeledCommit = ref.peel(GitObject.commit) as Commit; final peeledCommit = ref.peel(GitObject.commit) as Commit;
@ -570,7 +599,7 @@ void main() {
}); });
test('returns string representation of Reference object', () { test('returns string representation of Reference object', () {
final ref = repo.lookupReference('refs/heads/master'); final ref = Reference.lookup(repo: repo, name: 'refs/heads/master');
expect(ref.toString(), contains('Reference{')); expect(ref.toString(), contains('Reference{'));
ref.free(); ref.free();
}); });

View file

@ -24,11 +24,11 @@ void main() {
group('Remote', () { group('Remote', () {
test('returns list of remotes', () { test('returns list of remotes', () {
expect(repo.remotes, ['origin']); expect(Remote.list(repo), ['origin']);
}); });
test('lookups remote for provided name', () { test('lookups remote for provided name', () {
final remote = repo.lookupRemote('origin'); final remote = Remote.lookup(repo: repo, name: 'origin');
expect(remote.name, remoteName); expect(remote.name, remoteName);
expect(remote.url, remoteUrl); expect(remote.url, remoteUrl);
@ -39,11 +39,18 @@ void main() {
}); });
test('throws when provided name for lookup is not found', () { test('throws when provided name for lookup is not found', () {
expect(() => repo.lookupRemote('upstream'), throwsA(isA<LibGit2Error>())); expect(
() => Remote.lookup(repo: repo, name: 'upstream'),
throwsA(isA<LibGit2Error>()),
);
}); });
test('creates without fetchspec', () { test('creates without fetchspec', () {
final remote = repo.createRemote(name: 'upstream', url: remoteUrl); final remote = Remote.create(
repo: repo,
name: 'upstream',
url: remoteUrl,
);
expect(repo.remotes.length, 2); expect(repo.remotes.length, 2);
expect(remote.name, 'upstream'); expect(remote.name, 'upstream');
@ -55,7 +62,8 @@ void main() {
test('creates with provided fetchspec', () { test('creates with provided fetchspec', () {
const spec = '+refs/*:refs/*'; const spec = '+refs/*:refs/*';
final remote = repo.createRemote( final remote = Remote.create(
repo: repo,
name: 'upstream', name: 'upstream',
url: remoteUrl, url: remoteUrl,
fetch: spec, fetch: spec,
@ -73,7 +81,8 @@ void main() {
test('throws when trying to create with fetchspec with invalid remote name', test('throws when trying to create with fetchspec with invalid remote name',
() { () {
expect( expect(
() => repo.createRemote( () => Remote.create(
repo: repo,
name: '', name: '',
url: '', url: '',
fetch: '', fetch: '',
@ -83,10 +92,14 @@ void main() {
}); });
test('deletes remote', () { test('deletes remote', () {
final remote = repo.createRemote(name: 'upstream', url: remoteUrl); final remote = Remote.create(
repo: repo,
name: 'upstream',
url: remoteUrl,
);
expect(repo.remotes.length, 2); expect(repo.remotes.length, 2);
repo.deleteRemote(remote.name); Remote.delete(repo: repo, name: remote.name);
expect(repo.remotes.length, 1); expect(repo.remotes.length, 1);
remote.free(); remote.free();
@ -94,19 +107,23 @@ void main() {
test('throws when trying to delete non existing remote', () { test('throws when trying to delete non existing remote', () {
expect( expect(
() => repo.deleteRemote('not/there'), () => Remote.delete(repo: repo, name: 'not/there'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('renames remote', () { test('renames remote', () {
final remote = repo.lookupRemote(remoteName); final remote = Remote.lookup(repo: repo, name: remoteName);
final problems = repo.renameRemote(oldName: remoteName, newName: 'new'); final problems = Remote.rename(
repo: repo,
oldName: remoteName,
newName: 'new',
);
expect(problems, isEmpty); expect(problems, isEmpty);
expect(remote.name, isNot('new')); expect(remote.name, isNot('new'));
final newRemote = repo.lookupRemote('new'); final newRemote = Remote.lookup(repo: repo, name: 'new');
expect(newRemote.name, 'new'); expect(newRemote.name, 'new');
newRemote.free(); newRemote.free();
@ -114,14 +131,15 @@ void main() {
}); });
test('returns list of non-default refspecs that cannot be renamed', () { test('returns list of non-default refspecs that cannot be renamed', () {
final remote = repo.createRemote( final remote = Remote.create(
repo: repo,
name: 'upstream', name: 'upstream',
url: remoteUrl, url: remoteUrl,
fetch: '+refs/*:refs/*', fetch: '+refs/*:refs/*',
); );
expect( expect(
repo.renameRemote(oldName: remote.name, newName: 'renamed'), Remote.rename(repo: repo, oldName: remote.name, newName: 'renamed'),
['+refs/*:refs/*'], ['+refs/*:refs/*'],
); );
@ -130,19 +148,19 @@ void main() {
test('throws when renaming with invalid names', () { test('throws when renaming with invalid names', () {
expect( expect(
() => repo.renameRemote(oldName: '', newName: ''), () => Remote.rename(repo: repo, oldName: '', newName: ''),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('sets url', () { test('sets url', () {
final remote = repo.lookupRemote(remoteName); final remote = Remote.lookup(repo: repo, name: remoteName);
expect(remote.url, remoteUrl); expect(remote.url, remoteUrl);
const newUrl = 'git://new/url.git'; const newUrl = 'git://new/url.git';
Remote.setUrl(repo: repo, remote: remoteName, url: newUrl); Remote.setUrl(repo: repo, remote: remoteName, url: newUrl);
final newRemote = repo.lookupRemote(remoteName); final newRemote = Remote.lookup(repo: repo, name: remoteName);
expect(newRemote.url, newUrl); expect(newRemote.url, newUrl);
newRemote.free(); newRemote.free();
@ -160,7 +178,7 @@ void main() {
const newUrl = 'git://new/url.git'; const newUrl = 'git://new/url.git';
Remote.setPushUrl(repo: repo, remote: remoteName, url: newUrl); Remote.setPushUrl(repo: repo, remote: remoteName, url: newUrl);
final remote = repo.lookupRemote(remoteName); final remote = Remote.lookup(repo: repo, name: remoteName);
expect(remote.pushUrl, newUrl); expect(remote.pushUrl, newUrl);
remote.free(); remote.free();
@ -174,7 +192,7 @@ void main() {
}); });
test('returns refspec', () { test('returns refspec', () {
final remote = repo.lookupRemote('origin'); final remote = Remote.lookup(repo: repo, name: 'origin');
expect(remote.refspecCount, 1); expect(remote.refspecCount, 1);
final refspec = remote.getRefspec(0); final refspec = remote.getRefspec(0);
@ -203,7 +221,7 @@ void main() {
test('throws when trying to transform refspec with invalid reference name', test('throws when trying to transform refspec with invalid reference name',
() { () {
final remote = repo.lookupRemote('origin'); final remote = Remote.lookup(repo: repo, name: 'origin');
final refspec = remote.getRefspec(0); final refspec = remote.getRefspec(0);
expect( expect(
@ -225,7 +243,7 @@ void main() {
remote: 'origin', remote: 'origin',
refspec: '+refs/test/*:refs/test/remotes/*', refspec: '+refs/test/*:refs/test/remotes/*',
); );
final remote = repo.lookupRemote('origin'); final remote = Remote.lookup(repo: repo, name: 'origin');
expect(remote.fetchRefspecs.length, 2); expect(remote.fetchRefspecs.length, 2);
expect( expect(
remote.fetchRefspecs, remote.fetchRefspecs,
@ -255,7 +273,7 @@ void main() {
remote: 'origin', remote: 'origin',
refspec: '+refs/test/*:refs/test/remotes/*', refspec: '+refs/test/*:refs/test/remotes/*',
); );
final remote = repo.lookupRemote('origin'); final remote = Remote.lookup(repo: repo, name: 'origin');
expect(remote.pushRefspecs.length, 1); expect(remote.pushRefspecs.length, 1);
expect(remote.pushRefspecs, ['+refs/test/*:refs/test/remotes/*']); expect(remote.pushRefspecs, ['+refs/test/*:refs/test/remotes/*']);
@ -279,7 +297,7 @@ void main() {
remote: 'libgit2', remote: 'libgit2',
url: 'https://github.com/libgit2/TestGitRepository', url: 'https://github.com/libgit2/TestGitRepository',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final refs = remote.ls(); final refs = remote.ls();
expect(refs.first['local'], false); expect(refs.first['local'], false);
@ -298,7 +316,7 @@ void main() {
"throws when trying to get remote repo's reference list with " "throws when trying to get remote repo's reference list with "
"invalid url", () { "invalid url", () {
Remote.setUrl(repo: repo, remote: 'libgit2', url: 'invalid'); Remote.setUrl(repo: repo, remote: 'libgit2', url: 'invalid');
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
expect(() => remote.ls(), throwsA(isA<LibGit2Error>())); expect(() => remote.ls(), throwsA(isA<LibGit2Error>()));
@ -318,7 +336,7 @@ void main() {
remote: 'libgit2', remote: 'libgit2',
refspec: '+refs/heads/*:refs/remotes/origin/*', refspec: '+refs/heads/*:refs/remotes/origin/*',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final stats = remote.fetch( final stats = remote.fetch(
refspecs: ['+refs/heads/*:refs/remotes/origin/*'], refspecs: ['+refs/heads/*:refs/remotes/origin/*'],
@ -351,7 +369,7 @@ void main() {
remote: 'libgit2', remote: 'libgit2',
refspec: '+refs/heads/*:refs/remotes/origin/*', refspec: '+refs/heads/*:refs/remotes/origin/*',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final stats = remote.fetch( final stats = remote.fetch(
refspecs: ['+refs/heads/*:refs/remotes/origin/*'], refspecs: ['+refs/heads/*:refs/remotes/origin/*'],
@ -385,7 +403,7 @@ void main() {
remote: 'libgit2', remote: 'libgit2',
refspec: '+refs/heads/*:refs/remotes/origin/*', refspec: '+refs/heads/*:refs/remotes/origin/*',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
expect( expect(
() => remote.fetch( () => remote.fetch(
@ -402,7 +420,7 @@ void main() {
test('throws when trying to fetch data with invalid url', () { test('throws when trying to fetch data with invalid url', () {
Remote.setUrl(repo: repo, remote: 'libgit2', url: 'https://wrong.url'); Remote.setUrl(repo: repo, remote: 'libgit2', url: 'https://wrong.url');
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
expect( expect(
() => remote.fetch(), () => remote.fetch(),
@ -420,7 +438,7 @@ void main() {
remote: 'libgit2', remote: 'libgit2',
url: 'https://github.com/libgit2/TestGitRepository', url: 'https://github.com/libgit2/TestGitRepository',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
TransferProgress? callbackStats; TransferProgress? callbackStats;
void tp(TransferProgress stats) => callbackStats = stats; void tp(TransferProgress stats) => callbackStats = stats;
@ -454,7 +472,7 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
remote: 'libgit2', remote: 'libgit2',
url: 'https://github.com/libgit2/TestGitRepository', url: 'https://github.com/libgit2/TestGitRepository',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final sidebandOutput = StringBuffer(); final sidebandOutput = StringBuffer();
void sideband(String message) { void sideband(String message) {
@ -479,7 +497,7 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
remote: 'libgit2', remote: 'libgit2',
url: 'https://github.com/libgit2/TestGitRepository', url: 'https://github.com/libgit2/TestGitRepository',
); );
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
final tipsExpected = [ final tipsExpected = [
{ {
'refname': 'refs/tags/annotated_tag', 'refname': 'refs/tags/annotated_tag',
@ -531,8 +549,8 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
); );
final originRepo = Repository.open(originDir.path); final originRepo = Repository.open(originDir.path);
repo.createRemote(name: 'local', url: originDir.path); Remote.create(repo: repo, name: 'local', url: originDir.path);
final remote = repo.lookupRemote('local'); final remote = Remote.lookup(repo: repo, name: 'local');
final updateRefOutput = <String, String>{}; final updateRefOutput = <String, String>{};
void updateRef(String refname, String message) { void updateRef(String refname, String message) {
@ -543,7 +561,7 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
remote.push(refspecs: ['refs/heads/master'], callbacks: callbacks); remote.push(refspecs: ['refs/heads/master'], callbacks: callbacks);
expect( expect(
originRepo.lookupCommit(originRepo.head.target).oid.sha, Commit.lookup(repo: originRepo, oid: originRepo.head.target).oid.sha,
'821ed6e80627b8769d170a293862f9fc60825226', '821ed6e80627b8769d170a293862f9fc60825226',
); );
expect(updateRefOutput, {'refs/heads/master': ''}); expect(updateRefOutput, {'refs/heads/master': ''});
@ -555,7 +573,7 @@ Total 69 (delta 0), reused 1 (delta 0), pack-reused 68
test('throws when trying to push to invalid url', () { test('throws when trying to push to invalid url', () {
Remote.setUrl(repo: repo, remote: 'libgit2', url: 'https://wrong.url'); Remote.setUrl(repo: repo, remote: 'libgit2', url: 'https://wrong.url');
final remote = repo.lookupRemote('libgit2'); final remote = Remote.lookup(repo: repo, name: 'libgit2');
expect( expect(
() => remote.push(refspecs: ['refs/heads/master']), () => remote.push(refspecs: ['refs/heads/master']),

View file

@ -42,7 +42,7 @@ void main() {
expect(contents, 'Feature edit\n'); expect(contents, 'Feature edit\n');
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
expect(diff.deltas, isEmpty); expect(diff.deltas, isEmpty);
index.free(); index.free();
@ -57,7 +57,7 @@ void main() {
expect(contents, 'Feature edit\n'); expect(contents, 'Feature edit\n');
final index = repo.index; final index = repo.index;
final diff = index.diffToWorkdir(); final diff = Diff.indexToWorkdir(repo: repo, index: index);
expect(diff.deltas.length, 1); expect(diff.deltas.length, 1);
index.free(); index.free();

View file

@ -24,13 +24,13 @@ void main() {
group('revParse', () { group('revParse', () {
test('.single() returns commit with different spec strings', () { test('.single() returns commit with different spec strings', () {
final headCommit = repo.revParseSingle('HEAD'); final headCommit = RevParse.single(repo: repo, spec: 'HEAD');
expect(headCommit.oid.sha, headSHA); expect(headCommit.oid.sha, headSHA);
final parentCommit = repo.revParseSingle('HEAD^'); final parentCommit = RevParse.single(repo: repo, spec: 'HEAD^');
expect(parentCommit.oid.sha, parentSHA); expect(parentCommit.oid.sha, parentSHA);
final initCommit = repo.revParseSingle('@{-1}'); final initCommit = RevParse.single(repo: repo, spec: '@{-1}');
expect(initCommit.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'); expect(initCommit.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4');
headCommit.free(); headCommit.free();
@ -40,15 +40,18 @@ void main() {
test('.single() throws when spec string not found or invalid', () { test('.single() throws when spec string not found or invalid', () {
expect( expect(
() => repo.revParseSingle('invalid'), () => RevParse.single(repo: repo, spec: 'invalid'),
throwsA(isA<LibGit2Error>()),
);
expect(
() => RevParse.single(repo: repo, spec: ''),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect(() => repo.revParseSingle(''), throwsA(isA<LibGit2Error>()));
}); });
test('.ext() returns commit and reference', () { test('.ext() returns commit and reference', () {
final masterRef = repo.lookupReference('refs/heads/master'); final masterRef = Reference.lookup(repo: repo, name: 'refs/heads/master');
var headParse = repo.revParseExt('master'); var headParse = RevParse.ext(repo: repo, spec: 'master');
expect(headParse.object.oid.sha, headSHA); expect(headParse.object.oid.sha, headSHA);
expect(headParse.reference, masterRef); expect(headParse.reference, masterRef);
@ -58,8 +61,11 @@ void main() {
headParse.object.free(); headParse.object.free();
headParse.reference?.free(); headParse.reference?.free();
final featureRef = repo.lookupReference('refs/heads/feature'); final featureRef = Reference.lookup(
headParse = repo.revParseExt('feature'); repo: repo,
name: 'refs/heads/feature',
);
headParse = RevParse.ext(repo: repo, spec: 'feature');
expect( expect(
headParse.object.oid.sha, headParse.object.oid.sha,
@ -73,7 +79,7 @@ void main() {
}); });
test('.ext() returns only commit when no intermidiate reference found', () { test('.ext() returns only commit when no intermidiate reference found', () {
final headParse = repo.revParseExt('HEAD^'); final headParse = RevParse.ext(repo: repo, spec: 'HEAD^');
expect(headParse.object.oid.sha, parentSHA); expect(headParse.object.oid.sha, parentSHA);
expect(headParse.reference, isNull); expect(headParse.reference, isNull);
@ -83,16 +89,19 @@ void main() {
test('.ext() throws when spec string not found or invalid', () { test('.ext() throws when spec string not found or invalid', () {
expect( expect(
() => repo.revParseExt('invalid'), () => RevParse.ext(repo: repo, spec: 'invalid'),
throwsA(isA<LibGit2Error>()),
);
expect(
() => RevParse.ext(repo: repo, spec: ''),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect(() => repo.revParseExt(''), throwsA(isA<LibGit2Error>()));
}); });
test( test(
'.range returns revspec with correct fields values based on ' '.range returns revspec with correct fields values based on '
'provided spec', () { 'provided spec', () {
var revspec = repo.revParse('master'); var revspec = RevParse.range(repo: repo, spec: 'master');
expect(revspec.from.oid.sha, headSHA); expect(revspec.from.oid.sha, headSHA);
expect(revspec.to, isNull); expect(revspec.to, isNull);
@ -101,7 +110,7 @@ void main() {
revspec.from.free(); revspec.from.free();
revspec = repo.revParse('HEAD^1..5aecfa'); revspec = RevParse.range(repo: repo, spec: 'HEAD^1..5aecfa');
expect(revspec.from.oid.sha, parentSHA); expect(revspec.from.oid.sha, parentSHA);
expect(revspec.to?.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'); expect(revspec.to?.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4');
@ -110,7 +119,7 @@ void main() {
revspec.from.free(); revspec.from.free();
revspec.to?.free(); revspec.to?.free();
revspec = repo.revParse('HEAD...feature'); revspec = RevParse.range(repo: repo, spec: 'HEAD...feature');
expect(revspec.from.oid.sha, headSHA); expect(revspec.from.oid.sha, headSHA);
expect(revspec.to?.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'); expect(revspec.to?.oid.sha, '5aecfa0fb97eadaac050ccb99f03c3fb65460ad4');
@ -126,11 +135,11 @@ void main() {
test('throws on invalid range spec', () { test('throws on invalid range spec', () {
expect( expect(
() => repo.revParse('invalid..5aecfa'), () => RevParse.range(repo: repo, spec: 'invalid..5aecfa'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect( expect(
() => repo.revParse('master.......5aecfa'), () => RevParse.range(repo: repo, spec: 'master.......5aecfa'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });

View file

@ -33,13 +33,13 @@ void main() {
test('saves changes to stash', () { test('saves changes to stash', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
}); });
test('throws when trying to save and error occurs', () { test('throws when trying to save and error occurs', () {
expect( expect(
() => Repository(nullptr).createStash(stasher: stasher), () => Stash.create(repo: Repository(nullptr), stasher: stasher),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -48,7 +48,8 @@ void main() {
final swpPath = File(p.join(repo.workdir, 'some.swp')); final swpPath = File(p.join(repo.workdir, 'some.swp'));
swpPath.writeAsStringSync('ignored'); swpPath.writeAsStringSync('ignored');
repo.createStash( Stash.create(
repo: repo,
stasher: stasher, stasher: stasher,
flags: {GitStash.includeUntracked, GitStash.includeIgnored}, flags: {GitStash.includeUntracked, GitStash.includeIgnored},
); );
@ -64,7 +65,7 @@ void main() {
final index = repo.index; final index = repo.index;
index.add('file'); index.add('file');
repo.createStash(stasher: stasher, flags: {GitStash.keepIndex}); Stash.create(repo: repo, stasher: stasher, flags: {GitStash.keepIndex});
expect(repo.status.isEmpty, false); expect(repo.status.isEmpty, false);
expect(repo.stashes.length, 1); expect(repo.stashes.length, 1);
@ -74,20 +75,20 @@ void main() {
test('applies changes from stash', () { test('applies changes from stash', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
repo.applyStash(); Stash.apply(repo: repo);
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
}); });
test('applies changes from stash with paths provided', () { test('applies changes from stash with paths provided', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
repo.applyStash(paths: ['file']); Stash.apply(repo: repo, paths: ['file']);
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
}); });
@ -97,11 +98,15 @@ void main() {
index.add('stash.this'); index.add('stash.this');
expect(index.find('stash.this'), true); expect(index.find('stash.this'), true);
repo.createStash(stasher: stasher, flags: {GitStash.includeUntracked}); Stash.create(
repo: repo,
stasher: stasher,
flags: {GitStash.includeUntracked},
);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
expect(index.find('stash.this'), false); expect(index.find('stash.this'), false);
repo.applyStash(reinstateIndex: true); Stash.apply(repo: repo, reinstateIndex: true);
expect(repo.status, contains('stash.this')); expect(repo.status, contains('stash.this'));
expect(index.find('stash.this'), true); expect(index.find('stash.this'), true);
@ -111,17 +116,20 @@ void main() {
test('throws when trying to apply with wrong index', () { test('throws when trying to apply with wrong index', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(() => repo.applyStash(index: 10), throwsA(isA<LibGit2Error>())); expect(
() => Stash.apply(repo: repo, index: 10),
throwsA(isA<LibGit2Error>()),
);
}); });
test('drops stash', () { test('drops stash', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
final stash = repo.stashes.first; final stash = repo.stashes.first;
repo.dropStash(index: stash.index); Stash.drop(repo: repo, index: stash.index);
expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>())); expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>()));
}); });
@ -129,29 +137,32 @@ void main() {
test('throws when trying to drop with wrong index', () { test('throws when trying to drop with wrong index', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(() => repo.dropStash(index: 10), throwsA(isA<LibGit2Error>())); expect(
() => Stash.drop(repo: repo, index: 10),
throwsA(isA<LibGit2Error>()),
);
}); });
test('pops from stash', () { test('pops from stash', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
repo.popStash(); Stash.pop(repo: repo);
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>())); expect(() => Stash.apply(repo: repo), throwsA(isA<LibGit2Error>()));
}); });
test('pops from stash with provided path', () { test('pops from stash with provided path', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
repo.popStash(paths: ['file']); Stash.pop(repo: repo, paths: ['file']);
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>())); expect(() => Stash.apply(repo: repo), throwsA(isA<LibGit2Error>()));
}); });
test('pops from stash including index changes', () { test('pops from stash including index changes', () {
@ -160,11 +171,15 @@ void main() {
index.add('stash.this'); index.add('stash.this');
expect(index.find('stash.this'), true); expect(index.find('stash.this'), true);
repo.createStash(stasher: stasher, flags: {GitStash.includeUntracked}); Stash.create(
repo: repo,
stasher: stasher,
flags: {GitStash.includeUntracked},
);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
expect(index.find('stash.this'), false); expect(index.find('stash.this'), false);
repo.popStash(reinstateIndex: true); Stash.pop(repo: repo, reinstateIndex: true);
expect(repo.status, contains('stash.this')); expect(repo.status, contains('stash.this'));
expect(index.find('stash.this'), true); expect(index.find('stash.this'), true);
@ -174,17 +189,20 @@ void main() {
test('throws when trying to pop with wrong index', () { test('throws when trying to pop with wrong index', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher); Stash.create(repo: repo, stasher: stasher);
expect(() => repo.popStash(index: 10), throwsA(isA<LibGit2Error>())); expect(
() => Stash.pop(repo: repo, index: 10),
throwsA(isA<LibGit2Error>()),
);
}); });
test('returns list of stashes', () { test('returns list of stashes', () {
File(filePath).writeAsStringSync('edit', mode: FileMode.append); File(filePath).writeAsStringSync('edit', mode: FileMode.append);
repo.createStash(stasher: stasher, message: 'WIP'); Stash.create(repo: repo, stasher: stasher, message: 'WIP');
final stash = repo.stashes.first; final stash = Stash.list(repo).first;
expect(repo.stashes.length, 1); expect(repo.stashes.length, 1);
expect(stash.index, 0); expect(stash.index, 0);
@ -194,7 +212,11 @@ void main() {
test('returns string representation of Stash object', () { test('returns string representation of Stash object', () {
File(p.join(repo.workdir, 'stash.this')).writeAsStringSync('stash'); File(p.join(repo.workdir, 'stash.this')).writeAsStringSync('stash');
repo.createStash(stasher: stasher, flags: {GitStash.includeUntracked}); Stash.create(
repo: repo,
stasher: stasher,
flags: {GitStash.includeUntracked},
);
expect(repo.stashes[0].toString(), contains('Stash{')); expect(repo.stashes[0].toString(), contains('Stash{'));
}); });

View file

@ -30,12 +30,12 @@ void main() {
group('Submodule', () { group('Submodule', () {
test('returns list of all submodules paths', () { test('returns list of all submodules paths', () {
expect(repo.submodules.length, 1); expect(Submodule.list(repo).length, 1);
expect(repo.submodules.first, testSubmodule); expect(repo.submodules.first, testSubmodule);
}); });
test('finds submodule with provided name/path', () { test('finds submodule with provided name/path', () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
expect(submodule.name, testSubmodule); expect(submodule.name, testSubmodule);
expect(submodule.path, testSubmodule); expect(submodule.path, testSubmodule);
@ -53,7 +53,7 @@ void main() {
test('throws when trying to lookup and submodule not found', () { test('throws when trying to lookup and submodule not found', () {
expect( expect(
() => repo.lookupSubmodule('not/there'), () => Submodule.lookup(repo: repo, name: 'not/there'),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -63,8 +63,8 @@ void main() {
p.join(repo.workdir, testSubmodule, 'master.txt'); p.join(repo.workdir, testSubmodule, 'master.txt');
expect(File(submoduleFilePath).existsSync(), false); expect(File(submoduleFilePath).existsSync(), false);
repo.initSubmodule(submodule: testSubmodule); Submodule.init(repo: repo, name: testSubmodule);
repo.updateSubmodule(submodule: testSubmodule); Submodule.update(repo: repo, name: testSubmodule);
expect(File(submoduleFilePath).existsSync(), true); expect(File(submoduleFilePath).existsSync(), true);
}); });
@ -74,22 +74,22 @@ void main() {
p.join(repo.workdir, testSubmodule, 'master.txt'); p.join(repo.workdir, testSubmodule, 'master.txt');
expect(File(submoduleFilePath).existsSync(), false); expect(File(submoduleFilePath).existsSync(), false);
repo.updateSubmodule(submodule: testSubmodule, init: true); Submodule.update(repo: repo, name: testSubmodule, init: true);
expect(File(submoduleFilePath).existsSync(), true); expect(File(submoduleFilePath).existsSync(), true);
}); });
test('throws when trying to update not initialized submodule', () { test('throws when trying to update not initialized submodule', () {
expect( expect(
() => repo.updateSubmodule(submodule: testSubmodule), () => Submodule.update(repo: repo, name: testSubmodule),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
test('opens repository for a submodule', () { test('opens repository for a submodule', () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
repo.initSubmodule(submodule: testSubmodule); Submodule.init(repo: repo, name: testSubmodule);
repo.updateSubmodule(submodule: testSubmodule); Submodule.update(repo: repo, name: testSubmodule);
final submoduleRepo = submodule.open(); final submoduleRepo = submodule.open();
final subHead = submoduleRepo.head; final subHead = submoduleRepo.head;
@ -107,13 +107,14 @@ void main() {
test('throws when trying to open repository for not initialized submodule', test('throws when trying to open repository for not initialized submodule',
() { () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
expect(() => submodule.open(), throwsA(isA<LibGit2Error>())); expect(() => submodule.open(), throwsA(isA<LibGit2Error>()));
submodule.free(); submodule.free();
}); });
test('adds submodule', () { test('adds submodule', () {
final submodule = repo.addSubmodule( final submodule = Submodule.add(
repo: repo,
url: submoduleUrl, url: submoduleUrl,
path: 'test', path: 'test',
); );
@ -129,7 +130,8 @@ void main() {
test('throws when trying to add submodule with wrong url', () { test('throws when trying to add submodule with wrong url', () {
expect( expect(
() => repo.addSubmodule( () => Submodule.add(
repo: repo,
url: 'https://wrong.url/', url: 'https://wrong.url/',
path: 'test', path: 'test',
), ),
@ -139,7 +141,8 @@ void main() {
test('throws when trying to add submodule and error occurs', () { test('throws when trying to add submodule and error occurs', () {
expect( expect(
() => Repository(nullptr).addSubmodule( () => Submodule.add(
repo: Repository(nullptr),
url: 'https://wrong.url/', url: 'https://wrong.url/',
path: 'test', path: 'test',
), ),
@ -148,7 +151,7 @@ void main() {
}); });
test('sets configuration values', () { test('sets configuration values', () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
expect(submodule.url, submoduleUrl); expect(submodule.url, submoduleUrl);
expect(submodule.branch, ''); expect(submodule.branch, '');
expect(submodule.ignore, GitSubmoduleIgnore.none); expect(submodule.ignore, GitSubmoduleIgnore.none);
@ -159,7 +162,10 @@ void main() {
submodule.ignore = GitSubmoduleIgnore.all; submodule.ignore = GitSubmoduleIgnore.all;
submodule.updateRule = GitSubmoduleUpdate.rebase; submodule.updateRule = GitSubmoduleUpdate.rebase;
final updatedSubmodule = repo.lookupSubmodule(testSubmodule); final updatedSubmodule = Submodule.lookup(
repo: repo,
name: testSubmodule,
);
expect(updatedSubmodule.url, 'updated'); expect(updatedSubmodule.url, 'updated');
expect(updatedSubmodule.branch, 'updated'); expect(updatedSubmodule.branch, 'updated');
expect(updatedSubmodule.ignore, GitSubmoduleIgnore.all); expect(updatedSubmodule.ignore, GitSubmoduleIgnore.all);
@ -170,8 +176,8 @@ void main() {
}); });
test('syncs', () { test('syncs', () {
repo.updateSubmodule(submodule: testSubmodule, init: true); Submodule.update(repo: repo, name: testSubmodule, init: true);
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
final submRepo = submodule.open(); final submRepo = submodule.open();
final repoConfig = repo.config; final repoConfig = repo.config;
final submRepoConfig = submRepo.config; final submRepoConfig = submRepo.config;
@ -182,7 +188,10 @@ void main() {
submodule.url = 'https://updated.com/'; submodule.url = 'https://updated.com/';
submodule.branch = 'updated'; submodule.branch = 'updated';
final updatedSubmodule = repo.lookupSubmodule(testSubmodule); final updatedSubmodule = Submodule.lookup(
repo: repo,
name: testSubmodule,
);
updatedSubmodule.sync(); updatedSubmodule.sync();
final updatedSubmRepo = updatedSubmodule.open(); final updatedSubmRepo = updatedSubmodule.open();
final updatedSubmRepoConfig = updatedSubmRepo.config; final updatedSubmRepoConfig = updatedSubmRepo.config;
@ -206,7 +215,7 @@ void main() {
}); });
test('reloads info', () { test('reloads info', () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
expect(submodule.url, submoduleUrl); expect(submodule.url, submoduleUrl);
submodule.url = 'updated'; submodule.url = 'updated';
@ -218,7 +227,7 @@ void main() {
}); });
test('returns status for a submodule', () { test('returns status for a submodule', () {
final submodule = repo.lookupSubmodule(testSubmodule); final submodule = Submodule.lookup(repo: repo, name: testSubmodule);
expect( expect(
submodule.status(), submodule.status(),
{ {
@ -229,7 +238,7 @@ void main() {
}, },
); );
repo.updateSubmodule(submodule: testSubmodule, init: true); Submodule.update(repo: repo, name: testSubmodule, init: true);
expect( expect(
submodule.status(), submodule.status(),
{ {

View file

@ -17,7 +17,7 @@ void main() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
repo = Repository.open(tmpDir.path); repo = Repository.open(tmpDir.path);
tagOid = repo['f0fdbf506397e9f58c59b88dfdd72778ec06cc0c']; tagOid = repo['f0fdbf506397e9f58c59b88dfdd72778ec06cc0c'];
tag = repo.lookupTag(tagOid); tag = Tag.lookup(repo: repo, oid: tagOid);
}); });
tearDown(() { tearDown(() {
@ -33,7 +33,7 @@ void main() {
test('throws when trying to lookup tag for invalid oid', () { test('throws when trying to lookup tag for invalid oid', () {
expect( expect(
() => repo.lookupTag(repo['0' * 40]), () => Tag.lookup(repo: repo, oid: repo['0' * 40]),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -84,7 +84,7 @@ void main() {
message: message, message: message,
); );
final newTag = repo.lookupTag(oid); final newTag = Tag.lookup(repo: repo, oid: oid);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target as Commit; final newTagTarget = newTag.target as Commit;
@ -111,7 +111,7 @@ void main() {
targetType: GitObject.commit, targetType: GitObject.commit,
); );
final newTag = repo.lookupReference('refs/tags/$tagName'); final newTag = Reference.lookup(repo: repo, name: 'refs/tags/$tagName');
expect(newTag.shorthand, tagName); expect(newTag.shorthand, tagName);
expect(newTag.target, target); expect(newTag.target, target);
@ -138,7 +138,7 @@ void main() {
message: message, message: message,
); );
final newTag = repo.lookupTag(oid); final newTag = Tag.lookup(repo: repo, oid: oid);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target as Tree; final newTagTarget = newTag.target as Tree;
@ -164,7 +164,7 @@ void main() {
targetType: GitObject.tree, targetType: GitObject.tree,
); );
final newTag = repo.lookupReference('refs/tags/$tagName'); final newTag = Reference.lookup(repo: repo, name: 'refs/tags/$tagName');
expect(newTag.shorthand, tagName); expect(newTag.shorthand, tagName);
expect(newTag.target, target); expect(newTag.target, target);
@ -191,7 +191,7 @@ void main() {
message: message, message: message,
); );
final newTag = repo.lookupTag(oid); final newTag = Tag.lookup(repo: repo, oid: oid);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target as Blob; final newTagTarget = newTag.target as Blob;
@ -217,7 +217,7 @@ void main() {
targetType: GitObject.blob, targetType: GitObject.blob,
); );
final newTag = repo.lookupReference('refs/tags/$tagName'); final newTag = Reference.lookup(repo: repo, name: 'refs/tags/$tagName');
expect(newTag.shorthand, tagName); expect(newTag.shorthand, tagName);
expect(newTag.target, target); expect(newTag.target, target);
@ -243,7 +243,7 @@ void main() {
message: message, message: message,
); );
final newTag = repo.lookupTag(oid); final newTag = Tag.lookup(repo: repo, oid: oid);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target as Tag; final newTagTarget = newTag.target as Tag;
@ -268,7 +268,7 @@ void main() {
targetType: GitObject.tag, targetType: GitObject.tag,
); );
final newTag = repo.lookupReference('refs/tags/$tagName'); final newTag = Reference.lookup(repo: repo, name: 'refs/tags/$tagName');
expect(newTag.shorthand, tagName); expect(newTag.shorthand, tagName);
expect(newTag.target, tag.oid); expect(newTag.target, tag.oid);
@ -302,7 +302,7 @@ void main() {
force: true, force: true,
); );
final newTag = repo.lookupTag(oid); final newTag = Tag.lookup(repo: repo, oid: oid);
final tagger = newTag.tagger; final tagger = newTag.tagger;
final newTagTarget = newTag.target as Commit; final newTagTarget = newTag.target as Commit;
@ -337,7 +337,7 @@ void main() {
force: true, force: true,
); );
final newTag = repo.lookupReference('refs/tags/$tagName'); final newTag = Reference.lookup(repo: repo, name: 'refs/tags/$tagName');
expect(newTag.shorthand, tagName); expect(newTag.shorthand, tagName);
expect(newTag.target, target); expect(newTag.target, target);
@ -408,14 +408,17 @@ void main() {
}); });
test('deletes tag', () { test('deletes tag', () {
expect(repo.tags, ['v0.1', 'v0.2']); expect(Tag.list(repo), ['v0.1', 'v0.2']);
repo.deleteTag('v0.2'); Tag.delete(repo: repo, name: 'v0.2');
expect(repo.tags, ['v0.1']); expect(Tag.list(repo), ['v0.1']);
}); });
test('throws when trying to delete non existing tag', () { test('throws when trying to delete non existing tag', () {
expect(() => repo.deleteTag('not.there'), throwsA(isA<LibGit2Error>())); expect(
() => Tag.delete(repo: repo, name: 'not.there'),
throwsA(isA<LibGit2Error>()),
);
}); });
}); });
} }

View file

@ -15,9 +15,7 @@ void main() {
setUp(() { setUp(() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
repo = Repository.open(tmpDir.path); repo = Repository.open(tmpDir.path);
tree = repo.lookupTree( tree = Tree.lookup(repo: repo, oid: repo['a8ae3dd']);
repo['a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f'],
);
}); });
tearDown(() { tearDown(() {
@ -34,7 +32,7 @@ void main() {
test('throws when looking up tree for invalid oid', () { test('throws when looking up tree for invalid oid', () {
expect( expect(
() => repo.lookupTree(repo['0' * 40]), () => Tree.lookup(repo: repo, oid: repo['0' * 40]),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
}); });
@ -79,7 +77,7 @@ void main() {
}); });
test('creates tree', () { test('creates tree', () {
final fileOid = repo.createBlob('blob content'); final fileOid = Blob.create(repo: repo, content: 'blob content');
final builder = TreeBuilder(repo: repo); final builder = TreeBuilder(repo: repo);
builder.add( builder.add(
@ -87,7 +85,7 @@ void main() {
oid: fileOid, oid: fileOid,
filemode: GitFilemode.blob, filemode: GitFilemode.blob,
); );
final newTree = repo.lookupTree(builder.write()); final newTree = Tree.lookup(repo: repo, oid: builder.write());
final entry = newTree['filename']; final entry = newTree['filename'];
expect(newTree.length, 1); expect(newTree.length, 1);

View file

@ -15,7 +15,7 @@ void main() {
setUp(() { setUp(() {
tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo'))); tmpDir = setupRepo(Directory(p.join('test', 'assets', 'test_repo')));
repo = Repository.open(tmpDir.path); repo = Repository.open(tmpDir.path);
tree = repo.lookupTree(repo['a8ae3dd59e6e1802c6f78e05e301bfd57c9f334f']); tree = Tree.lookup(repo: repo, oid: repo['a8ae3dd']);
}); });
tearDown(() { tearDown(() {

View file

@ -31,9 +31,10 @@ void main() {
group('Worktree', () { group('Worktree', () {
test('creates worktree at provided path', () { test('creates worktree at provided path', () {
expect(repo.worktrees, <String>[]); expect(Worktree.list(repo), <String>[]);
final worktree = repo.createWorktree( final worktree = Worktree.create(
repo: repo,
name: worktreeName, name: worktreeName,
path: worktreeDir.path, path: worktreeDir.path,
); );
@ -54,17 +55,22 @@ void main() {
}); });
test('creates worktree at provided path from provided reference', () { test('creates worktree at provided path from provided reference', () {
final head = repo.revParseSingle('HEAD'); final head = RevParse.single(repo: repo, spec: 'HEAD');
final worktreeBranch = repo.createBranch(name: 'v1', target: head); final worktreeBranch = Branch.create(
final ref = repo.lookupReference('refs/heads/v1'); repo: repo,
name: 'v1',
target: head,
);
final ref = Reference.lookup(repo: repo, name: 'refs/heads/v1');
expect(repo.worktrees, <String>[]); expect(repo.worktrees, <String>[]);
final worktree = repo.createWorktree( final worktree = Worktree.create(
repo: repo,
name: worktreeName, name: worktreeName,
path: worktreeDir.path, path: worktreeDir.path,
ref: ref, ref: ref,
); );
final branches = repo.branches; final branches = Branch.list(repo: repo);
expect(repo.worktrees, [worktreeName]); expect(repo.worktrees, [worktreeName]);
expect(branches.any((branch) => branch.name == 'v1'), true); expect(branches.any((branch) => branch.name == 'v1'), true);
@ -89,14 +95,16 @@ void main() {
test('throws when trying to create worktree with invalid name or path', () { test('throws when trying to create worktree with invalid name or path', () {
expect( expect(
() => repo.createWorktree( () => Worktree.create(
repo: repo,
name: '', name: '',
path: worktreeDir.path, path: worktreeDir.path,
), ),
throwsA(isA<LibGit2Error>()), throwsA(isA<LibGit2Error>()),
); );
expect( expect(
() => repo.createWorktree( () => Worktree.create(
repo: repo,
name: 'name', name: 'name',
path: '', path: '',
), ),
@ -105,11 +113,12 @@ void main() {
}); });
test('lookups worktree', () { test('lookups worktree', () {
final worktree = repo.createWorktree( final worktree = Worktree.create(
repo: repo,
name: worktreeName, name: worktreeName,
path: worktreeDir.path, path: worktreeDir.path,
); );
final lookedupWorktree = repo.lookupWorktree(worktreeName); final lookedupWorktree = Worktree.lookup(repo: repo, name: worktreeName);
expect(lookedupWorktree.name, worktreeName); expect(lookedupWorktree.name, worktreeName);
expect(lookedupWorktree.path, contains('worktree')); expect(lookedupWorktree.path, contains('worktree'));
@ -127,7 +136,8 @@ void main() {
}); });
test('locks and unlocks worktree', () { test('locks and unlocks worktree', () {
final worktree = repo.createWorktree( final worktree = Worktree.create(
repo: repo,
name: worktreeName, name: worktreeName,
path: worktreeDir.path, path: worktreeDir.path,
); );
@ -145,7 +155,8 @@ void main() {
test('prunes worktree', () { test('prunes worktree', () {
expect(repo.worktrees, <String>[]); expect(repo.worktrees, <String>[]);
final worktree = repo.createWorktree( final worktree = Worktree.create(
repo: repo,
name: worktreeName, name: worktreeName,
path: worktreeDir.path, path: worktreeDir.path,
); );