8000 Move FindMergeBase() overloads to ObjectDatabase · GiTechLab/libgit2sharp@24dfd15 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24dfd15

Browse files
committed
Move FindMergeBase() overloads to ObjectDatabase
Per isssues libgit2#864 and libgit2#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 libgit2#864 libgit2#951
1 parent 12b1078 commit 24dfd15

File tree

5 files changed

+72
-45
lines changed

5 files changed

+72
-45
lines changed

LibGit2Sharp.Tests/CommitAncestorFixture.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void FindCommonAncestorForTwoCommits(string result, string sha1, string s
4141
var first = sha1 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha1);
4242
var second = sha2 == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha2);
4343

44-
Commit ancestor = repo.Commits.FindMergeBase(first, second);
44+
Commit ancestor = repo.ObjectDatabase.FindMergeBase(first, second);
4545

4646
if (result == null)
4747
{
@@ -71,7 +71,7 @@ public void FindCommonAncestorForCommitsAsEnumerable(string result, string[] sha
7171
{
7272
var commits = shas.Select(sha => sha == "-" ? CreateOrphanedCommit(repo) : repo.Lookup<Commit>(sha)).ToArray();
7373

74-
Commit ancestor = repo.Commits.FindMergeBase(commits, strategy);
74+
Commit ancestor = repo.ObjectDatabase.FindMergeBase(commits, strategy);
7575

7676
if (result == null)
7777
{
@@ -96,7 +96,7 @@ public void FindCommonAncestorForTwoCommitsThrows(string sha1, string sha2)
9696
var first = repo.Lookup<Commit>(sha1);
9797
var second = repo.Lookup<Commit>(sha2);
9898

99-
Assert.Throws<ArgumentNullException>(() => repo.Commits.FindMergeBase(first, second));
99+
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.FindMergeBase(first, second));
100100
}
101101
}
102102

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

115-
Assert.Throws<ArgumentException>(() => repo.Commits.FindMergeBase(commits, strategy));
115+
Assert.Throws<ArgumentException>(() => repo.ObjectDatabase.FindMergeBase(commits, strategy));
116116
}
117117
}
118118

LibGit2Sharp/CommitLog.cs

Lines changed: 4 additions & 38 deletions
< 6D47 td data-grid-cell-id="diff-611ccf265d8940f68ca41e27db105a8309be5b3b503e703ad9964c53ec871c84-96-94-1" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">94
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,10 @@ public ICommitLog QueryBy(CommitFilter filter)
8787
/// <param name="first">The first <see cref="Commit"/>.</param>
8888
/// <param name="second">The second <see cref="Commit"/>.</param>
8989
/// <returns>The merge base or null if none found.</returns>
90+
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
9091
public Commit FindMergeBase(Commit first, Commit second)
9192
{
92-
Ensure.ArgumentNotNull(first, "first");
93-
Ensure.ArgumentNotNull(second, "second");
94-
95-
return FindMergeBase(new[] { first, second }, MergeBaseFindingStrategy.Standard);
93+
return repo.ObjectDatabase.FindMergeBase(first, second);
96
}
9795

9896
/// <summary>
@@ -101,42 +99,10 @@ public Commit FindMergeBase(Commit first, Commit second)
10199
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
102100
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
103101
/// <returns>The merge base or null if none found.</returns>
102+
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
104103
public Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy)
105104
{
106-
Ensure.ArgumentNotNull(commits, "commits");
107-
108-
ObjectId id;
109-
List<GitOid> ids = new List<GitOid>(8);
110-
int count = 0;
111-
112-
foreach (var commit in commits)
113-
{
114-
if (commit == null)
115-
{
116-
throw new ArgumentException("Enumerable contains null at position: " + count.ToString(CultureInfo.InvariantCulture), "commits");
117-
}
118-
ids.Add(commit.Id.Oid);
119-
count++;
120-
}
121-
122-
if (count < 2)
123-
{
124-
throw new ArgumentException("The enumerable must contains at least two commits.", "commits");
125-
}
126-
127-
switch (strategy)
128-
{
129-
case MergeBaseFindingStrategy.Standard:
130-
id = Proxy.git_merge_base_many(repo.Handle, ids.ToArray());
131-
break;
132-
case MergeBaseFindingStrategy.Octopus:
133-
id = Proxy.git_merge_base_octopus(repo.Handle, ids.ToArray());
134-
break;
135-
default:
136-
throw new ArgumentException("", "strategy");
137-
}
138-
139-
return id == null ? null : repo.Lookup<Commit>(id);
105+
return repo.ObjectDatabase.FindMergeBase(commits, strategy);
140106
}
141107

142108
private class CommitEnumerator : IEnumerator<Commit>

LibGit2Sharp/HistoryDivergence.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected HistoryDivergence()
1919

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

2525
One = one;

LibGit2Sharp/IQueryableCommitLog.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23

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

2527
/// <summary>
@@ -28,6 +30,7 @@ public interface IQueryableCommitLog : ICommitLog
2830
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
2931
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
3032
/// <returns>The merge base or null if none found.</returns>
33+
[Obsolete("This method will be removed in the next release. Please use ObjectDatabase.FindMergeBase() instead.")]
3134
Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy);
3235
}
3336
}

LibGit2Sharp/ObjectDatabase.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
422422
using (var ourHandle = Proxy.git_object_peel(repo.Handle, one.Id, GitObjectType.Tree, true))
423423
using (var theirHandle = Proxy.git_object_peel(repo.Handle, another.Id, GitObjectType.Tree, true))
424424
{
425-
var ancestorCommit = repo.Commits.FindMergeBase(one, another);
425+
var ancestorCommit = FindMergeBase(one, another);
426426

427427
var ancestorHandle = ancestorCommit != null
428428
? Proxy.git_object_peel(repo.Handle, ancestorCommit.Id, GitObjectType.Tree, false)
@@ -435,5 +435,63 @@ public virtual bool CanMergeWithoutConflict(Commit one, Commit another)
435435
}
436436
}
437437
}
438+
439+
/// <summary>
440+
/// Find the best possible merge base given two <see cref="Commit"/>s.
441+
/// </summary>
442+
/// <param name="first">The first <see cref="Commit"/>.</param>
443+
/// <param name="second">The second <see cref="Commit"/>.</param>
444+
/// <returns>The merge base or null if none found.</returns>
445+
public virtual Commit FindMergeBase(Commit first, Commit second)
446+
{
447+
Ensure.ArgumentNotNull(first, "first");
448+
Ensure.ArgumentNotNull(second, "second");
449+
450+
return FindMergeBase(new[] { first, second }, MergeBaseFindingStrategy.Standard);
451+
}
452+
453+
/// <summary>
454+
/// Find the best possible merge base given two or more <see cref="Commit"/> according to the <see cref="MergeBaseFindingStrategy"/>.
455+
/// </summary>
456+
/// <param name="commits">The <see cref="Commit"/>s for which to find the merge base.</param>
457+
/// <param name="strategy">The strategy to leverage in order to find the merge base.</param>
458+
/// <returns>The merge base or null if none found.</returns>
459+
public virtual Commit FindMergeBase(IEnumerable<Commit> commits, MergeBaseFindingStrategy strategy)
460+
{
461+
Ensure.ArgumentNotNull(commits, "commits");
462+
463+
ObjectId id;
464+
List<GitOid> ids = new List<GitOid>(8);
465+
int count = 0;
466+
467+
foreach (var commit in commits)
468+
{
469+
if (commit == null)
470+
{
471+
throw new ArgumentException("Enumerable contains null at position: " + count.ToString(CultureInfo.InvariantCulture), "commits");
472+
}
473+
ids.Add(commit.Id.Oid);
474+
count++;
475+
}
476+
477+
if (count < 2)
478+
{
479+
throw new ArgumentException("The enumerable must contains at least two commits.", "commits");
480+
}
481+
482+
switch (strategy)
483+
{
484+
case MergeBaseFindingStrategy.Standard:
485+
id = Proxy.git_merge_base_many(repo.Handle, ids.ToArray());
486+
break;
487+
case MergeBaseFindingStrategy.Octopus:
488+
id = Proxy.git_merge_base_octopus(repo.Handle, ids.ToArray());
489+
break;
490+
default:
491+
throw new ArgumentException("", "strategy");
492+
}
493+
494+
return id == null ? null : repo.Lookup<Commit>(id);
495+
}
438496
}
439497
}

0 commit comments

Comments
 (0)
0