feat(commit): add ability to pass options to revert(...) and revertTo(...) API methods (#67)

- select parent to revert to for merge commits
- merge options
- checkout options
This commit is contained in:
Aleksey Kulikov 2022-06-17 12:43:54 +03:00 committed by GitHub
parent 75687c469c
commit d113af44b5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 216 additions and 3 deletions

View file

@ -384,8 +384,48 @@ Pointer<git_tree> tree(Pointer<git_commit> commit) {
void revert({
required Pointer<git_repository> repoPointer,
required Pointer<git_commit> commitPointer,
required int mainline,
int? mergeFavor,
int? mergeFlags,
int? mergeFileFlags,
int? checkoutStrategy,
String? checkoutDirectory,
List<String>? checkoutPaths,
}) {
final error = libgit2.git_revert(repoPointer, commitPointer, nullptr);
final opts = calloc<git_revert_options>();
libgit2.git_revert_options_init(opts, GIT_REVERT_OPTIONS_VERSION);
opts.ref.mainline = mainline;
if (mergeFavor != null) opts.ref.merge_opts.file_favor = mergeFavor;
if (mergeFlags != null) opts.ref.merge_opts.flags = mergeFlags;
if (mergeFileFlags != null) opts.ref.merge_opts.file_flags = mergeFileFlags;
if (checkoutStrategy != null) {
opts.ref.checkout_opts.checkout_strategy = checkoutStrategy;
}
if (checkoutDirectory != null) {
opts.ref.checkout_opts.target_directory = checkoutDirectory.toChar();
}
var pathPointers = <Pointer<Char>>[];
Pointer<Pointer<Char>> strArray = nullptr;
if (checkoutPaths != null) {
pathPointers = checkoutPaths.map((e) => e.toChar()).toList();
strArray = calloc(checkoutPaths.length);
for (var i = 0; i < checkoutPaths.length; i++) {
strArray[i] = pathPointers[i];
}
opts.ref.checkout_opts.paths.strings = strArray;
opts.ref.checkout_opts.paths.count = checkoutPaths.length;
}
final error = libgit2.git_revert(repoPointer, commitPointer, opts);
for (final p in pathPointers) {
calloc.free(p);
}
calloc.free(strArray);
calloc.free(opts);
if (error < 0) {
throw LibGit2Error(libgit2.git_error_last());
@ -403,11 +443,18 @@ Pointer<git_index> revertCommit({
required Pointer<git_commit> revertCommitPointer,
required Pointer<git_commit> ourCommitPointer,
required int mainline,
int? mergeFavor,
int? mergeFlags,
int? mergeFileFlags,
}) {
final out = calloc<Pointer<git_index>>();
final opts = calloc<git_merge_options>();
libgit2.git_merge_options_init(opts, GIT_MERGE_OPTIONS_VERSION);
if (mergeFavor != null) opts.ref.file_favor = mergeFavor;
if (mergeFlags != null) opts.ref.flags = mergeFlags;
if (mergeFileFlags != null) opts.ref.file_flags = mergeFileFlags;
final error = libgit2.git_revert_commit(
out,
repoPointer,

View file

@ -189,23 +189,64 @@ class Commit extends Equatable {
/// Reverts commit, producing changes in the index and working directory.
///
/// [mainline] is parent of the commit if it is a merge (i.e. 1, 2, etc.).
///
/// [mergeFavor] is one of the optional [GitMergeFileFavor] flags for
/// handling conflicting content.
///
/// [mergeFlags] is optional combination of [GitMergeFlag] flags.
///
/// [mergeFileFlags] is optional combination of [GitMergeFileFlag] flags.
///
/// [checkoutStrategy] is optional combination of [GitCheckout] flags.
///
/// [checkoutDirectory] is optional alternative checkout path to workdir.
///
/// [checkoutPaths] is optional list of files to checkout (by default all
/// paths are processed).
///
/// Throws a [LibGit2Error] if error occured.
void revert() {
void revert({
int mainline = 0,
GitMergeFileFavor? mergeFavor,
Set<GitMergeFlag>? mergeFlags,
Set<GitMergeFileFlag>? mergeFileFlags,
Set<GitCheckout>? checkoutStrategy,
String? checkoutDirectory,
List<String>? checkoutPaths,
}) {
bindings.revert(
repoPointer: bindings.owner(_commitPointer),
commitPointer: _commitPointer,
mainline: mainline,
mergeFavor: mergeFavor?.value,
mergeFlags: mergeFlags?.fold(0, (acc, e) => acc! | e.value),
mergeFileFlags: mergeFileFlags?.fold(0, (acc, e) => acc! | e.value),
checkoutStrategy: checkoutStrategy?.fold(0, (acc, e) => acc! | e.value),
checkoutDirectory: checkoutDirectory,
checkoutPaths: checkoutPaths,
);
}
/// 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).
/// [mainline] is parent of the commit if it is a merge (i.e. 1, 2, etc.).
///
/// [mergeFavor] is one of the optional [GitMergeFileFavor] flags for
/// handling conflicting content.
///
/// [mergeFlags] is optional combination of [GitMergeFlag] flags.
///
/// [mergeFileFlags] is optional combination of [GitMergeFileFlag] flags.
///
/// Throws a [LibGit2Error] if error occured.
Index revertTo({
required Commit commit,
int mainline = 0,
GitMergeFileFavor? mergeFavor,
Set<GitMergeFlag>? mergeFlags,
Set<GitMergeFileFlag>? mergeFileFlags,
}) {
return Index(
bindings.revertCommit(
@ -213,6 +254,9 @@ class Commit extends Equatable {
revertCommitPointer: _commitPointer,
ourCommitPointer: commit.pointer,
mainline: mainline,
mergeFavor: mergeFavor?.value,
mergeFlags: mergeFlags?.fold(0, (acc, e) => acc! | e.value),
mergeFileFlags: mergeFileFlags?.fold(0, (acc, e) => acc! | e.value),
),
);
}