From 24dfd15584ddd264752bb3009ec8a583d1f551d6 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Wed, 11 Feb 2015 21:13:07 -0500 Subject: [PATCH] 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 --- LibGit2Sharp.Tests/CommitAncestorFixture.cs | 8 +-- LibGit2Sharp/CommitLog.cs | 42 ++------------- LibGit2Sharp/HistoryDivergence.cs | 2 +- LibGit2Sharp/IQueryableCommitLog.cs | 5 +- LibGit2Sharp/ObjectDatabase.cs | 60 ++++++++++++++++++++- 5 files changed, 72 insertions(+), 45 deletions(-) diff --git a/LibGit2Sharp.Tests/CommitAncestorFixture.cs b/LibGit2Sharp.Tests/CommitAncestorFixture.cs index 6ea6176b6..c752f7415 100644 --- a/LibGit2Sharp.Tests/CommitAncestorFixture.cs +++ b/LibGit2Sharp.Tests/CommitAncestorFixture.cs @@ -41,7 +41,7 @@ public void FindCommonAncestorForTwoCommits(string result, string sha1, string s var first = sha1 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup(sha1); var second = sha2 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup(sha2); - Commit ancestor = repo.Commits.FindMergeBase(first, second); + Commit ancestor = repo.ObjectDatabase.FindMergeBase(first, second); if (result == null) { @@ -71,7 +71,7 @@ public void FindCommonAncestorForCommitsAsEnumerable(string result, string[] sha { var commits = shas.Select(sha => sha == "-" ? CreateOrphanedCommit(repo) : repo.Lookup(sha)).ToArray(); - Commit ancestor = repo.Commits.FindMergeBase(commits, strategy); + Commit ancestor = repo.ObjectDatabase.FindMergeBase(commits, strategy); if (result == null) { @@ -96,7 +96,7 @@ public void FindCommonAncestorForTwoCommitsThrows(string sha1, string sha2) var first = repo.Lookup(sha1); var second = repo.Lookup(sha2); - Assert.Throws(() => repo.Commits.FindMergeBase(first, second)); + Assert.Throws(() => repo.ObjectDatabase.FindMergeBase(first, second)); } } @@ -112,7 +112,7 @@ public void FindCommonAncestorForCommitsAsEnumerableThrows(string[] shas, MergeB { var commits = shas.Select(sha => sha == "-" ? CreateOrphanedCommit(repo) : repo.Lookup(sha)).ToArray(); - Assert.Throws(() => repo.Commits.FindMergeBase(commits, strategy)); + Assert.Throws(() => repo.ObjectDatabase.FindMergeBase(commits, strategy)); } } diff --git a/LibGit2Sharp/CommitLog.cs b/LibGit2Sharp/CommitLog.cs index 1056437af..660f37094 100644 --- a/LibGit2Sharp/CommitLog.cs +++ b/LibGit2Sharp/CommitLog.cs @@ -87,12 +87,10 @@ public ICommitLog QueryBy(CommitFilter filter) /// The first . /// The second . /// The merge base or null if none found. + [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); } /// @@ -101,42 +99,10 @@ public Commit FindMergeBase(Commit first, Commit second) /// The s for which to find the merge base. /// The strategy to leverage in order to find the merge base. /// The merge base or null if none found. + [Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")] public Commit FindMergeBase(IEnumerable commits, MergeBaseFindingStrategy strategy) { - Ensure.ArgumentNotNull(commits, "commits"); - - ObjectId id; - List ids = new List(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(id); + return repo.ObjectDatabase.FindMergeBase(commits, strategy); } private class CommitEnumerator : IEnumerator diff --git a/LibGit2Sharp/HistoryDivergence.cs b/LibGit2Sharp/HistoryDivergence.cs index 27d84dd19..7c54b6bec 100644 --- a/LibGit2Sharp/HistoryDivergence.cs +++ b/LibGit2Sharp/HistoryDivergence.cs @@ -19,7 +19,7 @@ protected HistoryDivergence() internal HistoryDivergence(Repository repo, Commit one, Commit another) { - commonAncestor = new Lazy(() => repo.Commits.FindMergeBase(one, another)); + commonAncestor = new Lazy(() => repo.ObjectDatabase.FindMergeBase(one, another)); Tuple div = Proxy.git_graph_ahead_behind(repo.Handle, one, another); One = one; diff --git a/LibGit2Sharp/IQueryableCommitLog.cs b/LibGit2Sharp/IQueryableCommitLog.cs index 7642d4122..6935979ea 100644 --- a/LibGit2Sharp/IQueryableCommitLog.cs +++ b/LibGit2Sharp/IQueryableCommitLog.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace LibGit2Sharp { @@ -20,6 +21,7 @@ public interface IQueryableCommitLog : ICommitLog /// The first . /// The second . /// The merge base or null if none found. + [Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")] Commit FindMergeBase(Commit first, Commit second); /// @@ -28,6 +30,7 @@ public interface IQueryableCommitLog : ICommitLog /// The s for which to find the merge base. /// The strategy to leverage in order to find the merge base. /// The merge base or null if none found. + [Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")] Commit FindMergeBase(IEnumerable commits, MergeBaseFindingStrategy strategy); } } diff --git a/LibGit2Sharp/ObjectDatabase.cs b/LibGit2Sharp/ObjectDatabase.cs index 0d6f69f69..b4b1da006 100644 --- a/LibGit2Sharp/ObjectDatabase.cs +++ b/LibGit2Sharp/ObjectDatabase.cs @@ -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) @@ -435,5 +435,63 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another) } } } + + /// + /// Find the best possible merge base given two s. + /// + /// The first . + /// The second . + /// The merge base or null if none found. + public virtual Commit FindMergeBase(Commit first, Commit second) + { + Ensure.ArgumentNotNull(first, "first"); + Ensure.ArgumentNotNull(second, "second"); + + return FindMergeBase(new[] { first, second }, MergeBaseFindingStrategy.Standard); + } + + /// + /// Find the best possible merge base given two or more according to the . + /// + /// The s for which to find the merge base. + /// The strategy to leverage in order to find the merge base. + /// The merge base or null if none found. + public virtual Commit FindMergeBase(IEnumerable commits, MergeBaseFindingStrategy strategy) + { + Ensure.ArgumentNotNull(commits, "commits"); + + ObjectId id; + List ids = new List(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(id); + } } }