mirror of
https://github.com/SkinnyMind/libgit2dart.git
synced 2025-05-05 04:39:07 -04:00
refactor(merge)!: move merge related methods into Merge class (#41)
This commit is contained in:
parent
e7c18c35e2
commit
570c696269
8 changed files with 393 additions and 348 deletions
|
@ -8,7 +8,6 @@ import 'package:libgit2dart/src/bindings/checkout.dart' as checkout_bindings;
|
|||
import 'package:libgit2dart/src/bindings/describe.dart' as describe_bindings;
|
||||
import 'package:libgit2dart/src/bindings/graph.dart' as graph_bindings;
|
||||
import 'package:libgit2dart/src/bindings/libgit2_bindings.dart';
|
||||
import 'package:libgit2dart/src/bindings/merge.dart' as merge_bindings;
|
||||
import 'package:libgit2dart/src/bindings/object.dart' as object_bindings;
|
||||
import 'package:libgit2dart/src/bindings/repository.dart' as bindings;
|
||||
import 'package:libgit2dart/src/bindings/reset.dart' as reset_bindings;
|
||||
|
@ -553,279 +552,6 @@ class Repository {
|
|||
}
|
||||
}
|
||||
|
||||
/// Finds a merge base between [commits].
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
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.mergeBaseOctopus(
|
||||
repoPointer: _repoPointer,
|
||||
commits: commits.map((e) => e.pointer.ref).toList(),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Analyzes the given branch's [theirHead] oid and determines the
|
||||
/// opportunities for merging them into [ourRef] reference (default is
|
||||
/// 'HEAD').
|
||||
///
|
||||
/// Returns list with analysis result and preference for fast forward merge
|
||||
/// values respectively.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
List mergeAnalysis({
|
||||
required Oid theirHead,
|
||||
String ourRef = 'HEAD',
|
||||
}) {
|
||||
final ref = Reference.lookup(repo: this, name: ourRef);
|
||||
final head = AnnotatedCommit.lookup(
|
||||
repo: this,
|
||||
oid: theirHead,
|
||||
);
|
||||
final analysisInt = merge_bindings.analysis(
|
||||
repoPointer: _repoPointer,
|
||||
ourRefPointer: ref.pointer,
|
||||
theirHeadPointer: head.pointer,
|
||||
theirHeadsLen: 1,
|
||||
);
|
||||
|
||||
final analysisSet = GitMergeAnalysis.values
|
||||
.where((e) => analysisInt[0] & e.value == e.value)
|
||||
.toSet();
|
||||
final mergePreference = GitMergePreference.values.singleWhere(
|
||||
(e) => analysisInt[1] == e.value,
|
||||
);
|
||||
|
||||
head.free();
|
||||
ref.free();
|
||||
|
||||
return <Object>[analysisSet, mergePreference];
|
||||
}
|
||||
|
||||
/// Merges the given [commit] into HEAD, writing the results into the
|
||||
/// 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.
|
||||
///
|
||||
/// [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({
|
||||
required AnnotatedCommit commit,
|
||||
GitMergeFileFavor favor = GitMergeFileFavor.normal,
|
||||
Set<GitMergeFlag> mergeFlags = const {GitMergeFlag.findRenames},
|
||||
Set<GitMergeFileFlag> fileFlags = const {GitMergeFileFlag.defaults},
|
||||
}) {
|
||||
merge_bindings.merge(
|
||||
repoPointer: _repoPointer,
|
||||
theirHeadPointer: commit.pointer,
|
||||
theirHeadsLen: 1,
|
||||
favor: favor.value,
|
||||
mergeFlags: mergeFlags.fold(0, (acc, e) => acc | e.value),
|
||||
fileFlags: fileFlags.fold(0, (acc, e) => acc | e.value),
|
||||
);
|
||||
}
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
String mergeFileFromIndex({
|
||||
required IndexEntry? ancestor,
|
||||
required IndexEntry ours,
|
||||
required IndexEntry theirs,
|
||||
}) {
|
||||
return merge_bindings.mergeFileFromIndex(
|
||||
repoPointer: _repoPointer,
|
||||
ancestorPointer: ancestor?.pointer,
|
||||
oursPointer: ours.pointer,
|
||||
theirsPointer: theirs.pointer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Merges two commits, producing an index that reflects the result of the
|
||||
/// merge. The index may be written as-is to the working directory or checked
|
||||
/// out. If the index is to be converted to a tree, the caller should resolve
|
||||
/// any conflicts that arose as part of the merge.
|
||||
///
|
||||
/// [ourCommit] is the commit that reflects the destination tree.
|
||||
///
|
||||
/// [theirCommit] is the commit to merge into [ourCommit].
|
||||
///
|
||||
/// [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].
|
||||
///
|
||||
/// **IMPORTANT**: returned index should be freed to release allocated memory.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Index mergeCommits({
|
||||
required Commit ourCommit,
|
||||
required Commit theirCommit,
|
||||
GitMergeFileFavor favor = GitMergeFileFavor.normal,
|
||||
Set<GitMergeFlag> mergeFlags = const {GitMergeFlag.findRenames},
|
||||
Set<GitMergeFileFlag> fileFlags = const {GitMergeFileFlag.defaults},
|
||||
}) {
|
||||
return Index(
|
||||
merge_bindings.mergeCommits(
|
||||
repoPointer: _repoPointer,
|
||||
ourCommitPointer: ourCommit.pointer,
|
||||
theirCommitPointer: theirCommit.pointer,
|
||||
favor: favor.value,
|
||||
mergeFlags: mergeFlags.fold(0, (acc, e) => acc | e.value),
|
||||
fileFlags: fileFlags.fold(0, (acc, e) => acc | e.value),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Merges two trees, producing an index that reflects the result of the
|
||||
/// merge. The index may be written as-is to the working directory or checked
|
||||
/// out. If the index is to be converted to a tree, the caller should resolve
|
||||
/// any conflicts that arose as part of the merge.
|
||||
///
|
||||
/// [ancestorTree] is the common ancestor between the trees, or null if none.
|
||||
///
|
||||
/// [ourTree] is the tree that reflects the destination tree.
|
||||
///
|
||||
/// [theirTree] is the tree to merge into [ourTree].
|
||||
///
|
||||
/// [favor] is one of the [GitMergeFileFavor] flags for handling conflicting
|
||||
/// content. Defaults to [GitMergeFileFavor.normal], recording conflict to
|
||||
/// the 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].
|
||||
///
|
||||
/// **IMPORTANT**: returned index should be freed to release allocated memory.
|
||||
///
|
||||
/// Throws a [LibGit2Error] if error occured.
|
||||
Index mergeTrees({
|
||||
Tree? ancestorTree,
|
||||
required Tree ourTree,
|
||||
required Tree theirTree,
|
||||
GitMergeFileFavor favor = GitMergeFileFavor.normal,
|
||||
List<GitMergeFlag> mergeFlags = const [GitMergeFlag.findRenames],
|
||||
List<GitMergeFileFlag> fileFlags = const [GitMergeFileFlag.defaults],
|
||||
}) {
|
||||
return Index(
|
||||
merge_bindings.mergeTrees(
|
||||
repoPointer: _repoPointer,
|
||||
ancestorTreePointer: ancestorTree?.pointer ?? nullptr,
|
||||
ourTreePointer: ourTree.pointer,
|
||||
theirTreePointer: theirTree.pointer,
|
||||
favor: favor.value,
|
||||
mergeFlags: mergeFlags.fold(0, (acc, element) => acc | element.value),
|
||||
fileFlags: fileFlags.fold(0, (acc, element) => acc | element.value),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Cherry-picks the provided [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: _repoPointer,
|
||||
commitPointer: commit.pointer,
|
||||
);
|
||||
}
|
||||
|
||||
/// Checkouts the provided [target] using the given strategy, and updates the
|
||||
/// HEAD.
|
||||
///
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue