Skip to content

Commit 70d0c3b

Browse files
committed
Merge pull request #1093 from libgit2/ntk/overloads
Remove extension methods
2 parents 0e3af9b + 025b273 commit 70d0c3b

27 files changed

+1010
-1150
lines changed

LibGit2Sharp.Tests/ArchiveFixture.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ public void ArchivingANullTreeOrCommitThrows()
6868
string path = SandboxBareTestRepo();
6969
using (var repo = new Repository(path))
7070
{
71-
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Commit)null, null));
72-
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Tree)null, null));
71+
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Commit), default(ArchiverBase)));
72+
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Commit), default(string)));
73+
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Tree), default(ArchiverBase)));
74+
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Tree), default(string)));
7375
}
7476
}
7577

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,8 @@ public void RemovingABranchWithBadParamsThrows()
962962
using (var repo = new Repository(path))
963963
{
964964
Assert.Throws<ArgumentException>(() => repo.Branches.Remove(string.Empty));
965-
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(null));
965+
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(default(string)));
966+
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(default(Branch)));
966967
}
967968
}
968969

LibGit2Sharp.Tests/MetaFixture.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Reflection;
7+
using System.Runtime.CompilerServices;
68
using System.Text;
79
using LibGit2Sharp.Tests.TestHelpers;
810
using Xunit;
@@ -344,6 +346,38 @@ where p.IsOptional
344346

345347
Assert.Equal("", sb.ToString());
346348
}
349+
350+
[Fact]
351+
public void PublicExtensionMethodsShouldonlyTargetInterfacesOrEnums()
352+
{
353+
IEnumerable<string> mis =
354+
from m in GetInvalidPublicExtensionMethods()
355+
select m.DeclaringType + "." + m.Name;
356+
357+
var sb = new StringBuilder();
358+
359+
foreach (var method in mis.Distinct())
360+
{
361+
sb.AppendFormat("'{0}' is a public extension method that doesn't target an interface or an enum.{1}",
362+
method, Environment.NewLine);
363+
}
364+
365+
Assert.Equal("", sb.ToString());
366+
}
367+
368+
// Inspired from http://stackoverflow.com/a/299526
369+
370+
static IEnumerable<MethodInfo> GetInvalidPublicExtensionMethods()
371+
{
372+
var query = from type in (Assembly.GetAssembly(typeof(IRepository))).GetTypes()
373+
where type.IsSealed && !type.IsGenericType && !type.IsNested && type.IsPublic
374+
from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
375+
where method.IsDefined(typeof(ExtensionAttribute), false)
376+
let parameterType = method.GetParameters()[0].ParameterType
377+
where parameterType != null && !parameterType.IsInterface && !parameterType.IsEnum
378+
select method;
379+
return query;
380+
}
347381
}
348382

349383
internal static class TypeExtensions

LibGit2Sharp.Tests/ReferenceFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -829,15 +829,15 @@ public void CanIdentifyReferenceKind()
829829
string path = SandboxStandardTestRepo();
830830
using (var repo = new Repository(path))
831831
{
832-
Assert.True(repo.Refs["refs/heads/master"].IsLocalBranch());
833-
Assert.True(repo.Refs["refs/remotes/origin/master"].IsRemoteTrackingBranch());
834-
Assert.True(repo.Refs["refs/tags/lw"].IsTag());
832+
Assert.True(repo.Refs["refs/heads/master"].IsLocalBranch);
833+
Assert.True(repo.Refs["refs/remotes/origin/master"].IsRemoteTrackingBranch);
834+
Assert.True(repo.Refs["refs/tags/lw"].IsTag);
835835
}
836836

837837
path = SandboxBareTestRepo();
838838
using (var repo = new Repository(path))
839839
{
840-
Assert.True(repo.Refs["refs/notes/commits"].IsNote());
840+
Assert.True(repo.Refs["refs/notes/commits"].IsNote);
841841
}
842842
}
843843

@@ -871,7 +871,7 @@ public void CanQueryReachabilityAmongASubsetOfreferences()
871871
using (var repo = new Repository(path))
872872
{
873873
var result = repo.Refs.ReachableFrom(
874-
repo.Refs.Where(r => r.IsTag()),
874+
repo.Refs.Where(r => r.IsTag),
875875
new[] { repo.Lookup<Commit>("f8d44d7"), repo.Lookup<Commit>("6dcf9bf") });
876876

877877
var expected = new[]

LibGit2Sharp/Blob.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Text;
34
using LibGit2Sharp.Core;
45

56
namespace LibGit2Sharp
@@ -55,7 +56,64 @@ public virtual Stream GetContentStream()
5556
public virtual Stream GetContentStream(FilteringOptions filteringOptions)
5657
{
5758
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");
59+
5860
return Proxy.git_blob_filtered_content_stream(repo.Handle, Id, filteringOptions.HintPath, false);
5961
}
62+
63+
/// <summary>
64+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected from the byte order mark
65+
/// </summary>
66+
/// <returns>Blob content as text.</returns>
67+
public virtual string GetContentText()
68+
{
69+
return ReadToEnd(GetContentStream(), null);
70+
}
71+
72+
/// <summary>
73+
/// Gets the blob content decoded with the specified encoding,
74+
/// or according to byte order marks, or the specified encoding as a fallback
75+
/// </summary>
76+
/// <param name="encoding">The encoding of the text to use, if it cannot be detected</param>
77+
/// <returns>Blob content as text.</returns>
78+
public virtual string GetContentText(Encoding encoding)
79+
{
80+
Ensure.ArgumentNotNull(encoding, "encoding");
81+
82+
return ReadToEnd(GetContentStream(), encoding);
83+
}
84+
85+
/// <summary>
86+
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected
87+
/// </summary>
88+
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
89+
/// <returns>Blob content as text.</returns>
90+
public virtual string GetContentText(FilteringOptions filteringOptions)
91+
{
92+
return GetContentText(filteringOptions, null);
93+
}
94+
95+
/// <summary>
96+
/// Gets the blob content as it would be checked out to the
97+
/// working directory, decoded with the specified encoding,
98+
/// or according to byte order marks, with UTF8 as fallback,
99+
/// if <paramref name="encoding"/> is null.
100+
/// </summary>
101+
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
102+
/// <param name="encoding">The encoding of the text. (default: detected or UTF8)</param>
103+
/// <returns>Blob content as text.</returns>
104+
public virtual string GetContentText(FilteringOptions filteringOptions, Encoding encoding)
105+
{
106+
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");
107+
108+
return ReadToEnd(GetContentStream(filteringOptions), encoding);
109+
}
110+
111+
private static string ReadToEnd(Stream stream, Encoding encoding)
112+
{
113+
using (var reader = new StreamReader(stream, encoding ?? LaxUtf8Marshaler.Encoding, encoding == null))
114+
{
115+
return reader.ReadToEnd();
116+
}
117+
}
60118
}
61119
}

LibGit2Sharp/BlobExtensions.cs

Lines changed: 0 additions & 76 deletions
This file was deleted.

LibGit2Sharp/BranchCollection.cs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ public virtual Branch Add(string name, string committish)
117117
return Add(name, committish, false);
118118
}
119119

120+
/// <summary>
121+
/// Create a new local branch with the specified name
122+
/// </summary>
123+
/// <param name="name">The name of the branch.</param>
124+
/// <param name="commit">The target commit.</param>
125+
/// <returns>A new <see cref="Branch"/>.</returns>
126+
public virtual Branch Add(string name, Commit commit)
127+
{
128+
return Add(name, commit, false);
129+
}
130+
131+
/// <summary>
132+
/// Create a new local branch with the specified name
133+
/// </summary>
134+
/// <param name="name">The name of the branch.</param>
135+
/// <param name="commit">The target commit.</param>
136+
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
137+
/// <returns>A new <see cref="Branch"/>.</returns>
138+
public virtual Branch Add(string name, Commit commit, bool allowOverwrite)
139+
{
140+
Ensure.ArgumentNotNull(commit, "commit");
141+
142+
return Add(name, commit.Sha, allowOverwrite);
143+
}
144+
120145
/// <summary>
121146
/// Create a new local branch with the specified name
122147
/// </summary>
@@ -135,6 +160,35 @@ public virtual Branch Add(string name, string committish, bool allowOverwrite)
135160
return branch;
136161
}
137162

163+
/// <summary>
164+
/// Deletes the branch with the specified name.
165+
/// </summary>
166+
/// <param name="name">The name of the branch to delete.</param>
167+
public virtual void Remove(string name)
168+
{
169+
Remove(name, false);
170+
}
171+
172+
/// <summary>
173+
/// Deletes the branch with the specified name.
174+
/// </summary>
175+
/// <param name="name">The name of the branch to delete.</param>
176+
/// <param name="isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
177+
public virtual void Remove(string name, bool isRemote)
178+
{
179+
Ensure.ArgumentNotNullOrEmptyString(name, "name");
180+
181+
string branchName = isRemote ? Reference.RemoteTrackingBranchPrefix + name : name;
182+
183+
Branch branch = this[branchName];
184+
185+
if (branch == null)
186+
{
187+
return;
188+
}
189+
190+
Remove(branch);
191+
}
138192
/// <summary>
139193
/// Deletes the specified branch.
140194
/// </summary>
@@ -149,6 +203,39 @@ public virtual void Remove(Branch branch)
149203
}
150204
}
151205

206+
/// <summary>
207+
/// Rename an existing local branch, using the default reflog message
208+
/// </summary>
209+
/// <param name="currentName">The current branch name.</param>
210+
/// <param name="newName">The new name the existing branch should bear.</param>
211+
/// <returns>A new <see cref="Branch"/>.</returns>
212+
public virtual Branch Rename(string currentName, string newName)
213+
{
214+
return Rename(currentName, newName, false);
215+
}
216+
217+
/// <summary>
218+
/// Rename an existing local branch, using the default reflog message
219+
/// </summary>
220+
/// <param name="currentName">The current branch name.</param>
221+
/// <param name="newName">The new name the existing branch should bear.</param>
222+
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
223+
/// <returns>A new <see cref="Branch"/>.</returns>
224+
public virtual Branch Rename(string currentName, string newName, bool allowOverwrite)
225+
{
226+
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
227+
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");
228+
229+
Branch branch = this[currentName];
230+
231+
if (branch == null)
232+
{
233+
throw new LibGit2SharpException("No branch named '{0}' exists in the repository.");
234+
}
235+
236+
return Rename(branch, newName, allowOverwrite);
237+
}
238+
152239
/// <summary>
153240
/// Rename an existing local branch
154241
/// </summary>

0 commit comments

Comments
 (0)