refactor(stash)!: extract implementation of api

This commit is contained in:
Aleksey Kulikov 2021-10-13 11:53:24 +03:00
parent 20ca75639d
commit 23787adc3a
4 changed files with 126 additions and 47 deletions

View file

@ -10,7 +10,7 @@ import '../util.dart';
/// Save the local modifications to a new stash. /// Save the local modifications to a new stash.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Pointer<git_oid> stash({ Pointer<git_oid> save({
required Pointer<git_repository> repoPointer, required Pointer<git_repository> repoPointer,
required Pointer<git_signature> stasherPointer, required Pointer<git_signature> stasherPointer,
String? message, String? message,

View file

@ -10,7 +10,6 @@ import 'bindings/commit.dart' as commit_bindings;
import 'bindings/checkout.dart' as checkout_bindings; import 'bindings/checkout.dart' as checkout_bindings;
import 'bindings/reset.dart' as reset_bindings; import 'bindings/reset.dart' as reset_bindings;
import 'bindings/diff.dart' as diff_bindings; import 'bindings/diff.dart' as diff_bindings;
import 'bindings/stash.dart' as stash_bindings;
import 'bindings/attr.dart' as attr_bindings; import 'bindings/attr.dart' as attr_bindings;
import 'bindings/graph.dart' as graph_bindings; import 'bindings/graph.dart' as graph_bindings;
import 'bindings/describe.dart' as describe_bindings; import 'bindings/describe.dart' as describe_bindings;
@ -1098,33 +1097,33 @@ class Repository {
); );
} }
/// Returns list of all the stashed states, first being the most recent.
List<Stash> get stashes => Stash.list(this);
/// Saves the local modifications to a new stash. /// Saves the local modifications to a new stash.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
Oid stash({ Oid createStash({
required Signature stasher, required Signature stasher,
String? message, String? message,
bool keepIndex = false, bool keepIndex = false,
bool includeUntracked = false, bool includeUntracked = false,
bool includeIgnored = false, bool includeIgnored = false,
}) { }) {
int flags = 0; return Stash.create(
if (keepIndex) flags |= GitStash.keepIndex.value; repo: this,
if (includeUntracked) flags |= GitStash.includeUntracked.value; stasher: stasher,
if (includeIgnored) flags |= GitStash.includeIgnored.value;
return Oid(stash_bindings.stash(
repoPointer: _repoPointer,
stasherPointer: stasher.pointer,
message: message, message: message,
flags: flags, keepIndex: keepIndex,
)); includeUntracked: includeUntracked,
includeIgnored: includeIgnored,
);
} }
/// Applies a single stashed state from the stash list. /// Applies a single stashed state from the stash list.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void stashApply({ void applyStash({
int index = 0, int index = 0,
bool reinstateIndex = false, bool reinstateIndex = false,
Set<GitCheckout> strategy = const { Set<GitCheckout> strategy = const {
@ -1134,11 +1133,11 @@ class Repository {
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
stash_bindings.apply( Stash.apply(
repoPointer: _repoPointer, repo: this,
index: index, index: index,
flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0, reinstateIndex: reinstateIndex,
strategy: strategy.fold(0, (acc, e) => acc | e.value), strategy: strategy,
directory: directory, directory: directory,
paths: paths, paths: paths,
); );
@ -1147,17 +1146,15 @@ class Repository {
/// Removes a single stashed state from the stash list. /// Removes a single stashed state from the stash list.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void stashDrop([int index = 0]) { void dropStash([int index = 0]) {
stash_bindings.drop( Stash.drop(repo: this, index: index);
repoPointer: _repoPointer,
index: index,
);
} }
/// Applies a single stashed state from the stash list and remove it from the list if successful. /// Applies a single stashed state from the stash list and remove it from
/// the list if successful.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.
void stashPop({ void popStash({
int index = 0, int index = 0,
bool reinstateIndex = false, bool reinstateIndex = false,
Set<GitCheckout> strategy = const { Set<GitCheckout> strategy = const {
@ -1167,21 +1164,16 @@ class Repository {
String? directory, String? directory,
List<String>? paths, List<String>? paths,
}) { }) {
stash_bindings.pop( Stash.pop(
repoPointer: _repoPointer, repo: this,
index: index, index: index,
flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0, reinstateIndex: reinstateIndex,
strategy: strategy.fold(0, (acc, e) => acc | e.value), strategy: strategy,
directory: directory, directory: directory,
paths: paths, paths: paths,
); );
} }
/// Returns list of all the stashed states, first being the most recent.
List<Stash> get stashList {
return stash_bindings.list(_repoPointer);
}
/// Returns a list of the configured remotes for a repository. /// Returns a list of the configured remotes for a repository.
/// ///
/// Throws a [LibGit2Error] if error occured. /// Throws a [LibGit2Error] if error occured.

View file

@ -1,4 +1,5 @@
import 'package:libgit2dart/libgit2dart.dart'; import 'package:libgit2dart/libgit2dart.dart';
import 'bindings/stash.dart' as bindings;
class Stash { class Stash {
/// Initializes a new instance of [Stash] class. /// Initializes a new instance of [Stash] class.
@ -17,6 +18,92 @@ class Stash {
/// The commit oid of the stashed state. /// The commit oid of the stashed state.
final Oid oid; final Oid oid;
/// Returns list of all the stashed states, first being the most recent.
static List<Stash> list(Repository repo) => bindings.list(repo.pointer);
/// Saves the local modifications to a new stash.
///
/// Throws a [LibGit2Error] if error occured.
static Oid create({
required Repository repo,
required Signature stasher,
String? message,
bool keepIndex = false,
bool includeUntracked = false,
bool includeIgnored = false,
}) {
int flags = 0;
if (keepIndex) flags |= GitStash.keepIndex.value;
if (includeUntracked) flags |= GitStash.includeUntracked.value;
if (includeIgnored) flags |= GitStash.includeIgnored.value;
return Oid(bindings.save(
repoPointer: repo.pointer,
stasherPointer: stasher.pointer,
message: message,
flags: flags,
));
}
/// Applies a single stashed state from the stash list.
///
/// Throws a [LibGit2Error] if error occured.
static void apply({
required Repository repo,
int index = 0,
bool reinstateIndex = false,
Set<GitCheckout> strategy = const {
GitCheckout.safe,
GitCheckout.recreateMissing
},
String? directory,
List<String>? paths,
}) {
bindings.apply(
repoPointer: repo.pointer,
index: index,
flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0,
strategy: strategy.fold(0, (acc, e) => acc | e.value),
directory: directory,
paths: paths,
);
}
/// Removes a single stashed state from the stash list.
///
/// Throws a [LibGit2Error] if error occured.
static void drop({required Repository repo, int index = 0}) {
bindings.drop(
repoPointer: repo.pointer,
index: index,
);
}
/// Applies a single stashed state from the stash list and remove it from
/// the list if successful.
///
/// Throws a [LibGit2Error] if error occured.
static void pop({
required Repository repo,
int index = 0,
bool reinstateIndex = false,
Set<GitCheckout> strategy = const {
GitCheckout.safe,
GitCheckout.recreateMissing
},
String? directory,
List<String>? paths,
}) {
bindings.pop(
repoPointer: repo.pointer,
index: index,
flags: reinstateIndex ? GitStashApply.reinstateIndex.value : 0,
strategy: strategy.fold(0, (acc, e) => acc | e.value),
directory: directory,
paths: paths,
);
}
@override @override
String toString() { String toString() {
return 'Stash{index: $index, message: $message, sha: ${oid.sha}}'; return 'Stash{index: $index, message: $message, sha: ${oid.sha}}';

View file

@ -30,7 +30,7 @@ void main() {
mode: FileMode.append, mode: FileMode.append,
); );
repo.stash(stasher: stasher, includeUntracked: true); repo.createStash(stasher: stasher, includeUntracked: true);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
}); });
@ -40,10 +40,10 @@ void main() {
mode: FileMode.append, mode: FileMode.append,
); );
repo.stash(stasher: stasher); repo.createStash(stasher: stasher);
expect(repo.status.isEmpty, true); expect(repo.status.isEmpty, true);
repo.stashApply(); repo.applyStash();
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
}); });
@ -53,10 +53,10 @@ void main() {
mode: FileMode.append, mode: FileMode.append,
); );
repo.stash(stasher: stasher); repo.createStash(stasher: stasher);
final stash = repo.stashList.first; final stash = repo.stashes.first;
repo.stashDrop(stash.index); repo.dropStash(stash.index);
expect(() => repo.stashApply(), throwsA(isA<LibGit2Error>())); expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>()));
}); });
test('successfully pops from stash', () { test('successfully pops from stash', () {
@ -65,10 +65,10 @@ void main() {
mode: FileMode.append, mode: FileMode.append,
); );
repo.stash(stasher: stasher); repo.createStash(stasher: stasher);
repo.stashPop(); repo.popStash();
expect(repo.status, contains('file')); expect(repo.status, contains('file'));
expect(() => repo.stashApply(), throwsA(isA<LibGit2Error>())); expect(() => repo.applyStash(), throwsA(isA<LibGit2Error>()));
}); });
test('returns list of stashes', () { test('returns list of stashes', () {
@ -77,11 +77,11 @@ void main() {
mode: FileMode.append, mode: FileMode.append,
); );
repo.stash(stasher: stasher, message: 'WIP'); repo.createStash(stasher: stasher, message: 'WIP');
final stash = repo.stashList.first; final stash = repo.stashes.first;
expect(repo.stashList.length, 1); expect(repo.stashes.length, 1);
expect(stash.index, 0); expect(stash.index, 0);
expect(stash.message, 'On master: WIP'); expect(stash.message, 'On master: WIP');
}); });