Skip to content
This repository was archived by the owner on Feb 3, 2023. It is now read-only.

Commit 30e6427

Browse files
committed
add merge has conflicts
1 parent e62364d commit 30e6427

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,15 @@ internal static extern int git_merge(
657657
ref GitMergeOpts merge_opts,
658658
ref GitCheckoutOpts checkout_opts);
659659

660+
[DllImport(libgit2)]
661+
internal static extern int git_merge_trees(
662+
out IndexSafeHandle index,
663+
RepositorySafeHandle repo,
664+
GitObjectSafeHandle ancestor_tree,
665+
GitObjectSafeHandle our_tree,
666+
GitObjectSafeHandle their_tree,
667+
ref GitMergeOpts merge_opts);
668+
660669
[DllImport(libgit2)]
661670
internal static extern int git_merge_analysis(
662671
out GitMergeAnalysis status_out,

LibGit2Sharp/Core/Proxy.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,22 @@ public static void git_index_clear(Index index)
10121012

10131013
#region git_merge_
10141014

1015+
public static IndexSafeHandle git_merge_trees(RepositorySafeHandle repo, GitObjectSafeHandle ancestorTree, GitObjectSafeHandle ourTree, GitObjectSafeHandle theirTree)
1016+
{
1017+
using (ThreadAffinity())
1018+
{
1019+
IndexSafeHandle index;
1020+
GitMergeOpts opts = new GitMergeOpts { Version = 1 };
1021+
int res = NativeMethods.git_merge_trees(out index, repo, ancestorTree, ourTree, theirTree, ref opts);
1022+
if (res != (int) GitErrorCode.Ok || index == null)
1023+
{
1024+
return null;
1025+
}
1026+
1027+
return index;
1028+
}
1029+
}
1030+
10151031
public static ObjectId git_merge_base_many(RepositorySafeHandle repo, GitOid[] commitIds)
10161032
{
10171033
using (ThreadAffinity())

LibGit2Sharp/IRepository.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,16 @@ public interface IRepository : IDisposable
196196
/// <returns>The result of the revert.</returns>
197197
RevertResult Revert(Commit commit, Signature reverter, RevertOptions options);
198198

199+
/// <summary>
200+
/// Returns whether merging <paramref name="theirTree"/> into <paramref name="ourTree"/>
201+
/// would result in merge conflicts.
202+
/// </summary>
203+
/// <param name="ourTree">The base tree to merge into.</param>
204+
/// <param name="theirTree">The tree to merge into <paramref name="ourTree"/>.</param>
205+
/// <param name="ancestorTree">The common ancestor of the two trees, or null if none exists.</param>
206+
/// <returns>Whether a merge would result in conflicts</returns>
207+
bool MergeHasConflicts(Tree ourTree, Tree theirTree, Tree ancestorTree);
208+
199209
/// <summary>
200210
/// Merge changes from commit into the branch pointed at by HEAD..
201211
/// </summary>

LibGit2Sharp/Repository.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,25 @@ public static string Version
974974
get { return GlobalSettings.Version.ToString(); }
975975
}
976976

977+
/// <summary>
978+
/// Returns whether merging <paramref name="theirTree"/> into <paramref name="ourTree"/>
979+
/// would result in merge conflicts.
980+
/// </summary>
981+
/// <param name="ourTree">The base tree to merge into.</param>
982+
/// <param name="theirTree">The tree to merge into <paramref name="ourTree"/>.</param>
983+
/// <param name="ancestorTree">The common ancestor of the two trees, or null if none exists.</param>
984+
/// <returns>Whether a merge would result in conflicts</returns>
985+
public bool MergeHasConflicts(Tree ourTree, Tree theirTree, Tree ancestorTree)
986+
{
987+
using (var ourHandle = Proxy.git_object_peel(Handle, ourTree.Id, GitObjectType.Tree, true))
988+
using (var theirHandle = Proxy.git_object_peel(Handle, theirTree.Id, GitObjectType.Tree, true))
989+
using (var ancestorHandle = Proxy.git_object_peel(Handle, ancestorTree.Id, GitObjectType.Tree, false))
990+
using (var indexHandle = Proxy.git_merge_trees(Handle, ourHandle, theirHandle, ancestorHandle))
991+
{
992+
return Proxy.git_index_has_conflicts(indexHandle);
993+
}
994+
}
995+
977996
/// <summary>
978997
/// Merges changes from commit into the branch pointed at by HEAD.
979998
/// </summary>

0 commit comments

Comments
 (0)