mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-04 20:29:08 -04:00
feat(merge)!: add more bindings and API methods (#24)
This commit is contained in:
parent
561986ebfd
commit
9791b6324c
5 changed files with 394 additions and 41 deletions
|
@ -24,6 +24,66 @@ Pointer<git_oid> mergeBase({
|
|||
}
|
||||
}
|
||||
|
||||
/// Find a merge base given a list of commits.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_oid> mergeBaseMany({
|
||||
required Pointer<git_repository> repoPointer,
|
||||
required List<git_oid> commits,
|
||||
}) {
|
||||
final out = calloc<git_oid>();
|
||||
final commitsC = calloc<git_oid>(commits.length * 20);
|
||||
for (var i = 0; i < commits.length; i++) {
|
||||
commitsC[i].id = commits[i].id;
|
||||
}
|
||||
|
||||
final error = libgit2.git_merge_base_many(
|
||||
out,
|
||||
repoPointer,
|
||||
commits.length,
|
||||
commitsC,
|
||||
);
|
||||
|
||||
calloc.free(commitsC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/// Find a merge base in preparation for an octopus merge.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Pointer<git_oid> mergeBaseOctopus({
|
||||
required Pointer<git_repository> repoPointer,
|
||||
required List<git_oid> commits,
|
||||
}) {
|
||||
final out = calloc<git_oid>();
|
||||
final commitsC = calloc<git_oid>(commits.length * 20);
|
||||
for (var i = 0; i < commits.length; i++) {
|
||||
commitsC[i].id = commits[i].id;
|
||||
}
|
||||
|
||||
final error = libgit2.git_merge_base_octopus(
|
||||
out,
|
||||
repoPointer,
|
||||
commits.length,
|
||||
commitsC,
|
||||
);
|
||||
|
||||
calloc.free(commitsC);
|
||||
|
||||
if (error < 0) {
|
||||
calloc.free(out);
|
||||
throw LibGit2Error(libgit2.git_error_last());
|
||||
} else {
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
/// Analyzes the given branch(es) and determines the opportunities for merging
|
||||
/// them into a reference.
|
||||
List<int> analysis({
|
||||
|
@ -59,9 +119,15 @@ void merge({
|
|||
required Pointer<git_repository> repoPointer,
|
||||
required Pointer<Pointer<git_annotated_commit>> theirHeadsPointer,
|
||||
required int theirHeadsLen,
|
||||
required int favor,
|
||||
required int mergeFlags,
|
||||
required int fileFlags,
|
||||
}) {
|
||||
final mergeOpts = calloc<git_merge_options>();
|
||||
libgit2.git_merge_options_init(mergeOpts, GIT_MERGE_OPTIONS_VERSION);
|
||||
final mergeOpts = _initMergeOptions(
|
||||
favor: favor,
|
||||
mergeFlags: mergeFlags,
|
||||
fileFlags: fileFlags,
|
||||
);
|
||||
|
||||
final checkoutOpts = calloc<git_checkout_options>();
|
||||
libgit2.git_checkout_options_init(checkoutOpts, GIT_CHECKOUT_OPTIONS_VERSION);
|
||||
|
@ -82,6 +148,63 @@ void merge({
|
|||
calloc.free(checkoutOpts);
|
||||
}
|
||||
|
||||
/// Merge two files as they exist in the in-memory data structures, using the
|
||||
/// given common ancestor as the baseline, producing a string that reflects the
|
||||
/// merge result.
|
||||
///
|
||||
/// Note that this function does not reference a repository and any
|
||||
/// configuration must be passed.
|
||||
String mergeFile({
|
||||
required String ancestor,
|
||||
required String ancestorLabel,
|
||||
required String ours,
|
||||
required String oursLabel,
|
||||
required String theirs,
|
||||
required String theirsLabel,
|
||||
required int favor,
|
||||
required int flags,
|
||||
}) {
|
||||
final out = calloc<git_merge_file_result>();
|
||||
final ancestorC = calloc<git_merge_file_input>();
|
||||
final oursC = calloc<git_merge_file_input>();
|
||||
final theirsC = calloc<git_merge_file_input>();
|
||||
libgit2.git_merge_file_input_init(ancestorC, GIT_MERGE_FILE_INPUT_VERSION);
|
||||
libgit2.git_merge_file_input_init(oursC, GIT_MERGE_FILE_INPUT_VERSION);
|
||||
libgit2.git_merge_file_input_init(theirsC, GIT_MERGE_FILE_INPUT_VERSION);
|
||||
ancestorC.ref.ptr = ancestor.toNativeUtf8().cast<Int8>();
|
||||
ancestorC.ref.size = ancestor.length;
|
||||
oursC.ref.ptr = ours.toNativeUtf8().cast<Int8>();
|
||||
oursC.ref.size = ours.length;
|
||||
theirsC.ref.ptr = theirs.toNativeUtf8().cast<Int8>();
|
||||
theirsC.ref.size = theirs.length;
|
||||
|
||||
final opts = calloc<git_merge_file_options>();
|
||||
libgit2.git_merge_file_options_init(opts, GIT_MERGE_FILE_OPTIONS_VERSION);
|
||||
opts.ref.favor = favor;
|
||||
opts.ref.flags = flags;
|
||||
if (ancestorLabel.isNotEmpty) {
|
||||
opts.ref.ancestor_label = ancestorLabel.toNativeUtf8().cast<Int8>();
|
||||
}
|
||||
if (oursLabel.isNotEmpty) {
|
||||
opts.ref.our_label = oursLabel.toNativeUtf8().cast<Int8>();
|
||||
}
|
||||
if (theirsLabel.isNotEmpty) {
|
||||
opts.ref.their_label = theirsLabel.toNativeUtf8().cast<Int8>();
|
||||
}
|
||||
|
||||
libgit2.git_merge_file(out, ancestorC, oursC, theirsC, opts);
|
||||
|
||||
calloc.free(ancestorC);
|
||||
calloc.free(oursC);
|
||||
calloc.free(theirsC);
|
||||
calloc.free(opts);
|
||||
|
||||
final result = out.ref.ptr.cast<Utf8>().toDartString(length: out.ref.len);
|
||||
calloc.free(out);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// Merge two files as they exist in the index, using the given common ancestor
|
||||
/// as the baseline, producing a string that reflects the merge result
|
||||
/// containing possible conflicts.
|
||||
|
|
|
@ -949,15 +949,34 @@ class Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/// Finds a merge base between two commits [a] and [b].
|
||||
/// Finds a merge base between [commits].
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Oid mergeBase({required Oid a, required Oid b}) {
|
||||
Oid mergeBase(List<Oid> commits) {
|
||||
return commits.length == 2
|
||||
? Oid(
|
||||
merge_bindings.mergeBase(
|
||||
repoPointer: _repoPointer,
|
||||
aPointer: commits[0].pointer,
|
||||
bPointer: commits[1].pointer,
|
||||
),
|
||||
)
|
||||
: Oid(
|
||||
merge_bindings.mergeBaseMany(
|
||||
repoPointer: _repoPointer,
|
||||
commits: commits.map((e) => e.pointer.ref).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Finds a merge base in preparation for an octopus merge.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Oid mergeBaseOctopus(List<Oid> commits) {
|
||||
return Oid(
|
||||
merge_bindings.mergeBase(
|
||||
merge_bindings.mergeBaseOctopus(
|
||||
repoPointer: _repoPointer,
|
||||
aPointer: a.pointer,
|
||||
bPointer: b.pointer,
|
||||
commits: commits.map((e) => e.pointer.ref).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -1004,8 +1023,24 @@ class Repository {
|
|||
/// are written to the index. Callers should inspect the repository's index
|
||||
/// after this completes, resolve any conflicts and prepare a commit.
|
||||
///
|
||||
/// [favor] is one of the [GitMergeFileFavor] flags for handling conflicting
|
||||
/// content. Defaults to [GitMergeFileFavor.normal], recording conflict to t
|
||||
/// he index.
|
||||
///
|
||||
/// [mergeFlags] is a combination of [GitMergeFlag] flags. Defaults to
|
||||
/// [GitMergeFlag.findRenames] enabling the ability to merge between a
|
||||
/// modified and renamed file.
|
||||
///
|
||||
/// [fileFlags] is a combination of [GitMergeFileFlag] flags. Defaults to
|
||||
/// [GitMergeFileFlag.defaults].
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
void merge(Oid oid) {
|
||||
void merge({
|
||||
required Oid oid,
|
||||
GitMergeFileFavor favor = GitMergeFileFavor.normal,
|
||||
Set<GitMergeFlag> mergeFlags = const {GitMergeFlag.findRenames},
|
||||
Set<GitMergeFileFlag> fileFlags = const {GitMergeFileFlag.defaults},
|
||||
}) {
|
||||
final theirHead = AnnotatedCommit.lookup(
|
||||
repo: this,
|
||||
oid: oid,
|
||||
|
@ -1015,11 +1050,64 @@ class Repository {
|
|||
repoPointer: _repoPointer,
|
||||
theirHeadsPointer: theirHead.pointer,
|
||||
theirHeadsLen: 1,
|
||||
favor: favor.value,
|
||||
mergeFlags: mergeFlags.fold(0, (acc, e) => acc | e.value),
|
||||
fileFlags: fileFlags.fold(0, (acc, e) => acc | e.value),
|
||||
);
|
||||
|
||||
theirHead.free();
|
||||
}
|
||||
|
||||
/// Merges two files as they exist in the in-memory data structures, using the
|
||||
/// given common ancestor as the baseline, producing a string that reflects
|
||||
/// the merge result.
|
||||
///
|
||||
/// Note that this function does not reference a repository and configuration
|
||||
/// must be passed as [favor] and [flags].
|
||||
///
|
||||
/// [ancestor] is the contents of the ancestor file.
|
||||
///
|
||||
/// [ancestorLabel] is optional label for the ancestor file side of the
|
||||
/// conflict which will be prepended to labels in diff3-format merge files.
|
||||
/// Defaults to "file.txt".
|
||||
///
|
||||
/// [ours] is the contents of the file in "our" side.
|
||||
///
|
||||
/// [oursLabel] is optional label for our file side of the conflict which
|
||||
/// will be prepended to labels in merge files. Defaults to "file.txt".
|
||||
///
|
||||
/// [theirs] is the contents of the file in "their" side.
|
||||
///
|
||||
/// [theirsLabel] is optional label for their file side of the conflict which
|
||||
/// will be prepended to labels in merge files. Defaults to "file.txt".
|
||||
///
|
||||
/// [favor] is one of the [GitMergeFileFavor] flags for handling conflicting
|
||||
/// content. Defaults to [GitMergeFileFavor.normal].
|
||||
///
|
||||
/// [flags] is a combination of [GitMergeFileFlag] flags. Defaults to
|
||||
/// [GitMergeFileFlag.defaults].
|
||||
String mergeFile({
|
||||
required String ancestor,
|
||||
String ancestorLabel = '',
|
||||
required String ours,
|
||||
String oursLabel = '',
|
||||
required String theirs,
|
||||
String theirsLabel = '',
|
||||
GitMergeFileFavor favor = GitMergeFileFavor.normal,
|
||||
Set<GitMergeFileFlag> flags = const {GitMergeFileFlag.defaults},
|
||||
}) {
|
||||
return merge_bindings.mergeFile(
|
||||
ancestor: ancestor,
|
||||
ancestorLabel: ancestorLabel,
|
||||
ours: ours,
|
||||
oursLabel: oursLabel,
|
||||
theirs: theirs,
|
||||
theirsLabel: theirsLabel,
|
||||
favor: favor.value,
|
||||
flags: flags.fold(0, (acc, e) => acc | e.value),
|
||||
);
|
||||
}
|
||||
|
||||
/// Merges two files [ours] and [theirs] as they exist in the index, using the
|
||||
/// given common [ancestor] as the baseline, producing a string that reflects
|
||||
/// the merge result containing possible conflicts.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue