diff --git a/lib/src/bindings/merge.dart b/lib/src/bindings/merge.dart index 6bf16a8..d1dd1dc 100644 --- a/lib/src/bindings/merge.dart +++ b/lib/src/bindings/merge.dart @@ -67,11 +67,11 @@ void merge( int theirHeadsLen, ) { final mergeOpts = calloc(sizeOf()); - libgit2.git_merge_options_init(mergeOpts, 1); + libgit2.git_merge_options_init(mergeOpts, GIT_MERGE_OPTIONS_VERSION); final checkoutOpts = calloc(sizeOf()); - libgit2.git_checkout_options_init(checkoutOpts, 1); + libgit2.git_checkout_options_init(checkoutOpts, GIT_CHECKOUT_OPTIONS_VERSION); checkoutOpts.ref.checkout_strategy = git_checkout_strategy_t.GIT_CHECKOUT_SAFE + git_checkout_strategy_t.GIT_CHECKOUT_RECREATE_MISSING; @@ -166,3 +166,19 @@ Pointer mergeTrees( return out.value; } } + +/// Cherry-pick the given commit, producing changes in the index and working directory. +/// +/// Throws a [LibGit2Error] if error occured. +void cherryPick(Pointer repo, Pointer commit) { + final opts = calloc(sizeOf()); + libgit2.git_cherrypick_options_init(opts, GIT_CHERRYPICK_OPTIONS_VERSION); + opts.ref.checkout_opts.checkout_strategy = + git_checkout_strategy_t.GIT_CHECKOUT_SAFE; + + final error = libgit2.git_cherrypick(repo, commit, opts); + + if (error < 0) { + throw LibGit2Error(libgit2.git_error_last()); + } +} diff --git a/lib/src/repository.dart b/lib/src/repository.dart index e48cf71..b36180a 100644 --- a/lib/src/repository.dart +++ b/lib/src/repository.dart @@ -608,4 +608,14 @@ class Repository { return Index(result); } + + /// Cherry-picks the given commit, producing changes in the index and working directory. + /// + /// Any changes are staged for commit and any conflicts are written to the index. Callers + /// should inspect the repository's index after this completes, resolve any conflicts and + /// prepare a commit. + /// + /// Throws a [LibGit2Error] if error occured. + void cherryPick(Commit commit) => + merge_bindings.cherryPick(_repoPointer, commit.pointer); } diff --git a/test/merge_test.dart b/test/merge_test.dart index 22f58bd..1fff0e1 100644 --- a/test/merge_test.dart +++ b/test/merge_test.dart @@ -242,5 +242,15 @@ void main() { theirCommit.free(); }); }); + + test('successfully cherry-picks commit', () { + final cherry = repo['5aecfa0fb97eadaac050ccb99f03c3fb65460ad4'] as Commit; + repo.cherryPick(cherry); + expect(repo.state, GitRepositoryState.cherrypick.value); + final index = repo.index; + expect(index.conflicts, isEmpty); + + index.free(); + }); }); }