8000 Introduce ObjectDatabase.CanMergeWithoutConflict() · GiTechLab/libgit2sharp@25d802f · GitHub
[go: up one dir, main page]

Skip to content

Commit 25d802f

Browse files
joshveranulltoken
authored andcommitted
Introduce ObjectDatabase.CanMergeWithoutConflict()
1 parent b924152 commit 25d802f

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

LibGit2Sharp/Core/NativeMethods.cs

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

681+
[DllImport(libgit2)]
682+
internal static extern int git_merge_trees(
683+
out IndexSafeHandle index,
684+
RepositorySafeHandle repo,
685+
GitObjectSafeHandle ancestor_tree,
686+
GitObjectSafeHandle our_tree,
687+
GitObjectSafeHandle their_tree,
688+
ref GitMergeOpts merge_opts);
689+
681690
[DllImport(libgit2)]
682691
internal static extern int git_merge_analysis(
683692
out GitMergeAnalysis status_out,

LibGit2Sharp/Core/Proxy.cs

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

10751075
#region git_merge_
10761076

1077+
public static IndexSafeHandle git_merge_trees(RepositorySafeHandle repo, GitObjectSafeHandle ancestorTree, GitObjectSafeHandle ourTree, GitObjectSafeHandle theirTree)
1078+
{
1079+
using (ThreadAffinity())
1080+
{
1081+
IndexSafeHandle index;
1082+
GitMergeOpts opts = new GitMergeOpts { Version = 1 };
1083+
int res = NativeMethods.git_merge_trees(out index, repo, ancestorTree, ourTree, theirTree, ref opts);
1084+
if (res != (int) GitErrorCode.Ok || index == null)
1085+
{
1086+
return null;
1087+
}
1088+
1089+
return index;
1090+
}
1091+
}
1092+
10771093
public static ObjectId git_merge_base_many(RepositorySafeHandle repo, GitOid[] commitIds)
10781094
{
10791095
using (ThreadAffinity())

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,34 @@ public virtual string ShortenObjectId(GitObject gitObject, int? minLength = null
406406

407407
return gitObject.Sha.Substring(0, minLength.Value);
408408
}
409+
410+
/// <summary>
411+
/// Returns whether merging <paramref name="one"/> into <paramref name="another"/>
412+
/// would result in merge conflicts.
413+
/// </summary>
414+
/// <param name="one">The commit wrapping the base tree to merge into.</param>
415+
/// <param name="another">The commit wrapping the tree to merge into <paramref name="one"/>.</param>
416+
/// <returns>True if the merge does not result in a conflict, false otherwise.</returns>
417+
public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
418+
{
419+
Ensure.ArgumentNotNull(one, "one");
420+
Ensure.ArgumentNotNull(another, "another");
421+
422+
using (var ourHandle = Proxy.git_object_peel(repo.Handle, one.Id, GitObjectType.Tree, true))
423+
using (var theirHandle = Proxy.git_object_peel(repo.Handle, another.Id, GitObjectType.Tree, true))
424+
{
425+
var ancestorCommit = repo.Commits.FindMergeBase(one, another);
426+
427+
var ancestorHandle = ancestorCommit != null
428+
? Proxy.git_object_peel(repo.Handle, ancestorCommit.Id, GitObjectType.Tree, false)
429+
: new NullGitObjectSafeHandle();
430+
431+
using (ancestorHandle)
432+
using (var indexHandle = Proxy.git_merge_trees(repo.Handle, ancestorHandle, ourHandle, theirHandle))
433+
{
434+
return !Proxy.git_index_has_conflicts(indexHandle);
435+
}
436+
}
437+
}
409438
}
410439
}

0 commit comments

Comments
 (0)
0