Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move FindMergeBase() overloads to ObjectDatabase #957

Merged
merged 1 commit into from
Feb 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Move FindMergeBase() overloads to ObjectDatabase
Per isssues #864 and #951, the overloads of FindMergeBase() have been moved
from CommitLog to ObjectDatabase.

The methods on ICommitLog and CommitLog have been deprecated, and calls to
the CommitLog methods have been changed to call the ObjectDatabase versions
instead.

The relevant tests have been updated as well.

Fixes #864 #951
  • Loading branch information
bording committed Feb 12, 2015
commit 24dfd15584ddd264752bb3009ec8a583d1f551d6
8 changes: 4 additions & 4 deletions LibGit2Sharp.Tests/CommitAncestorFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 41,7 @@ public void FindCommonAncestorForTwoCommits(string result, string sha1, string s
var first = sha1 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha1);
var second = sha2 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha2);

Commit ancestor = repo.Commits.FindMergeBase(first, second);
Commit ancestor = repo.ObjectDatabase.FindMergeBase(first, second);

if (result == null)
{
Expand Down Expand Up @@ -71,7 71,7 @@ public void FindCommonAncestorForCommitsAsEnumerable(string result, string[] sha
{
var commits = shas.Select(sha => sha == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha)).ToArray();

Commit ancestor = repo.Commits.FindMergeBase(commits, strategy);
Commit ancestor = repo.ObjectDatabase.FindMergeBase(commits, strategy);

if (result == null)
{
Expand All @@ -96,7 96,7 @@ public void FindCommonAncestorForTwoCommitsThrows(string sha1, string sha2)
var first = repo.Lookup<Commit>(sha1);
var second = repo.Lookup<Commit>(sha2);

Assert.Throws<ArgumentNullException>(() => repo.Commits.FindMergeBase(first, second));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.FindMergeBase(first, second));
}
}

Expand All @@ -112,7 112,7 @@ public void FindCommonAncestorForCommitsAsEnumerableThrows(string[] shas, MergeB
{
var commits = shas.Select(sha => sha == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha)).ToArray();

Assert.Throws<ArgumentException>(() => repo.Commits.FindMergeBase(commits, strategy));
Assert.Throws<ArgumentException>(() => repo.ObjectDatabase.FindMergeBase(commits, strategy));
}
}

Expand Down
42 changes: 4 additions & 38 deletions LibGit2Sharp/CommitLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 87,10 @@ public ICommitLog QueryBy(CommitFilter filter)
/// <param name="first">The first <see cref="Commit"/>.</param>
/// <param name="second">The second <see cref="Commit"/>.</param>
/// <returns>The merge base or null if none found.</returns>
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
public Commit FindMergeBase(Commit first, Commit second)
{
Ensure.ArgumentNotNull(first, "first");
Ensure.ArgumentNotNull(second, "second");

return FindMergeBase(new[] { first, second }, MergeBaseFindingStrategy.Standard);
return repo.ObjectDatabase.FindMergeBase(first, second);
}

/// <summary>
Expand All @@ -101,42 99,10 @@ public Commit FindMergeBase(Commit first, Commit second)
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
/// <returns>The merge base or null if none found.</returns>
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
public Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy)
{
Ensure.ArgumentNotNull(commits, "commits");

ObjectId id;
List<GitOid> ids = new List<GitOid>(8);
int count = 0;

foreach (var commit in commits)
{
if (commit == null)
{
throw new ArgumentException("Enumerable contains null at position: " count.ToString(CultureInfo.InvariantCulture), "commits");
}
ids.Add(commit.Id.Oid);
count ;
}

if (count < 2)
{
throw new ArgumentException("The enumerable must contains at least two commits.", "commits");
}

switch (strategy)
{
case MergeBaseFindingStrategy.Standard:
id = Proxy.git_merge_base_many(repo.Handle, ids.ToArray());
break;
case MergeBaseFindingStrategy.Octopus:
id = Proxy.git_merge_base_octopus(repo.Handle, ids.ToArray());
break;
default:
throw new ArgumentException("", "strategy");
}

return id == null ? null : repo.Lookup<Commit>(id);
return repo.ObjectDatabase.FindMergeBase(commits, strategy);
}

private class CommitEnumerator : IEnumerator<Commit>
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/HistoryDivergence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 19,7 @@ protected HistoryDivergence()

internal HistoryDivergence(Repository repo, Commit one, Commit another)
{
commonAncestor = new Lazy<Commit>(() => repo.Commits.FindMergeBase(one, another));
commonAncestor = new Lazy<Commit>(() => repo.ObjectDatabase.FindMergeBase(one, another));
Tuple<int?, int?> div = Proxy.git_graph_ahead_behind(repo.Handle, one, another);

One = one;
Expand Down
5 changes: 4 additions & 1 deletion LibGit2Sharp/IQueryableCommitLog.cs
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace LibGit2Sharp
{
Expand All @@ -20,6 21,7 @@ public interface IQueryableCommitLog : ICommitLog
/// <param name="first">The first <see cref="Commit"/>.</param>
/// <param name="second">The second <see cref="Commit"/>.</param>
/// <returns>The merge base or null if none found.</returns>
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
Commit FindMergeBase(Commit first, Commit second);

/// <summary>
Expand All @@ -28,6 30,7 @@ public interface IQueryableCommitLog : ICommitLog
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
/// <returns>The merge base or null if none found.</returns>
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy);
}
}
60 changes: 59 additions & 1 deletion LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 422,7 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
using (var ourHandle = Proxy.git_object_peel(repo.Handle, one.Id, GitObjectType.Tree, true))
using (var theirHandle = Proxy.git_object_peel(repo.Handle, another.Id, GitObjectType.Tree, true))
{
var ancestorCommit = repo.Commits.FindMergeBase(one, another);
var ancestorCommit = FindMergeBase(one, another);

var ancestorHandle = ancestorCommit != null
? Proxy.git_object_peel(repo.Handle, ancestorCommit.Id, GitObjectType.Tree, false)
Expand All @@ -435,5 435,63 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
}
}
}

/// <summary>
/// Find the best possible merge base given two <see cref="Commit"/>s.
/// </summary>
/// <param name="first">The first <see cref="Commit"/>.</param>
/// <param name="second">The second <see cref="Commit"/>.</param>
/// <returns>The merge base or null if none found.</returns>
public virtual Commit FindMergeBase(Commit first, Commit second)
{
Ensure.ArgumentNotNull(first, "first");
Ensure.ArgumentNotNull(second, "second");

return FindMergeBase(new[] { first, second }, MergeBaseFindingStrategy.Standard);
}

/// <summary>
/// Find the best possible merge base given two or more <see cref="Commit"/> according to the <see cref="MergeBaseFindingStrategy"/>.
/// </summary>
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
/// <returns>The merge base or null if none found.</returns>
public virtual Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy)
{
Ensure.ArgumentNotNull(commits, "commits");

ObjectId id;
List<GitOid> ids = new List<GitOid>(8);
int count = 0;

foreach (var commit in commits)
{
if (commit == null)
{
throw new ArgumentException("Enumerable contains null at position: " count.ToString(CultureInfo.InvariantCulture), "commits");
}
ids.Add(commit.Id.Oid);
count ;
}

if (count < 2)
{
throw new ArgumentException("The enumerable must contains at least two commits.", "commits");
}

switch (strategy)
{
case MergeBaseFindingStrategy.Standard:
id = Proxy.git_merge_base_many(repo.Handle, ids.ToArray());
break;
case MergeBaseFindingStrategy.Octopus:
id = Proxy.git_merge_base_octopus(repo.Handle, ids.ToArray());
break;
default:
throw new ArgumentException("", "strategy");
}

return id == null ? null : repo.Lookup<Commit>(id);
}
}
}