Skip to content

Remove extension methods #1093

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

Merged
merged 15 commits into from
Jun 19, 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
6 changes: 4 additions & 2 deletions LibGit2Sharp.Tests/ArchiveFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ public void ArchivingANullTreeOrCommitThrows()
string path = SandboxBareTestRepo();
using (var repo = new Repository(path))
{
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Commit)null, null));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive((Tree)null, null));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Commit), default(ArchiverBase)));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Commit), default(string)));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Tree), default(ArchiverBase)));
Assert.Throws<ArgumentNullException>(() => repo.ObjectDatabase.Archive(default(Tree), default(string)));
}
}

Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -962,7 +962,8 @@ public void RemovingABranchWithBadParamsThrows()
using (var repo = new Repository(path))
{
Assert.Throws<ArgumentException>(() => repo.Branches.Remove(string.Empty));
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(null));
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(default(string)));
Assert.Throws<ArgumentNullException>(() => repo.Branches.Remove(default(Branch)));
}
}

Expand Down
34 changes: 34 additions & 0 deletions LibGit2Sharp.Tests/MetaFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
Expand Down Expand Up @@ -344,6 +346,38 @@ where p.IsOptional

Assert.Equal("", sb.ToString());
}

[Fact]
public void PublicExtensionMethodsShouldonlyTargetInterfacesOrEnums()
{
IEnumerable<string> mis =
from m in GetInvalidPublicExtensionMethods()
select m.DeclaringType + "." + m.Name;

var sb = new StringBuilder();

foreach (var method in mis.Distinct())
{
sb.AppendFormat("'{0}' is a public extension method that doesn't target an interface or an enum.{1}",
method, Environment.NewLine);
}

Assert.Equal("", sb.ToString());
}

// Inspired from http://stackoverflow.com/a/299526

static IEnumerable<MethodInfo> GetInvalidPublicExtensionMethods()
{
var query = from type in (Assembly.GetAssembly(typeof(IRepository))).GetTypes()
where type.IsSealed && !type.IsGenericType && !type.IsNested && type.IsPublic
from method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)
where method.IsDefined(typeof(ExtensionAttribute), false)
let parameterType = method.GetParameters()[0].ParameterType
where parameterType != null && !parameterType.IsInterface && !parameterType.IsEnum
select method;
return query;
}
}

internal static class TypeExtensions
Expand Down
10 changes: 5 additions & 5 deletions LibGit2Sharp.Tests/ReferenceFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -829,15 +829,15 @@ public void CanIdentifyReferenceKind()
string path = SandboxStandardTestRepo();
using (var repo = new Repository(path))
{
Assert.True(repo.Refs["refs/heads/master"].IsLocalBranch());
Assert.True(repo.Refs["refs/remotes/origin/master"].IsRemoteTrackingBranch());
Assert.True(repo.Refs["refs/tags/lw"].IsTag());
Assert.True(repo.Refs["refs/heads/master"].IsLocalBranch);
Assert.True(repo.Refs["refs/remotes/origin/master"].IsRemoteTrackingBranch);
Assert.True(repo.Refs["refs/tags/lw"].IsTag);
}

path = SandboxBareTestRepo();
using (var repo = new Repository(path))
{
Assert.True(repo.Refs["refs/notes/commits"].IsNote());
Assert.True(repo.Refs["refs/notes/commits"].IsNote);
}
}

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

var expected = new[]
Expand Down
58 changes: 58 additions & 0 deletions LibGit2Sharp/Blob.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
Expand Down Expand Up @@ -55,7 +56,64 @@ public virtual Stream GetContentStream()
public virtual Stream GetContentStream(FilteringOptions filteringOptions)
{
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");

return Proxy.git_blob_filtered_content_stream(repo.Handle, Id, filteringOptions.HintPath, false);
}

/// <summary>
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected from the byte order mark
/// </summary>
/// <returns>Blob content as text.</returns>
public virtual string GetContentText()
{
return ReadToEnd(GetContentStream(), null);
}

/// <summary>
/// Gets the blob content decoded with the specified encoding,
/// or according to byte order marks, or the specified encoding as a fallback
/// </summary>
/// <param name="encoding">The encoding of the text to use, if it cannot be detected</param>
/// <returns>Blob content as text.</returns>
public virtual string GetContentText(Encoding encoding)
{
Ensure.ArgumentNotNull(encoding, "encoding");

return ReadToEnd(GetContentStream(), encoding);
}

/// <summary>
/// Gets the blob content, decoded with UTF8 encoding if the encoding cannot be detected
/// </summary>
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
/// <returns>Blob content as text.</returns>
public virtual string GetContentText(FilteringOptions filteringOptions)
{
return GetContentText(filteringOptions, null);
}

/// <summary>
/// Gets the blob content as it would be checked out to the
/// working directory, decoded with the specified encoding,
/// or according to byte order marks, with UTF8 as fallback,
/// if <paramref name="encoding"/> is null.
/// </summary>
/// <param name="filteringOptions">Parameter controlling content filtering behavior</param>
/// <param name="encoding">The encoding of the text. (default: detected or UTF8)</param>
/// <returns>Blob content as text.</returns>
public virtual string GetContentText(FilteringOptions filteringOptions, Encoding encoding)
{
Ensure.ArgumentNotNull(filteringOptions, "filteringOptions");

return ReadToEnd(GetContentStream(filteringOptions), encoding);
}

private static string ReadToEnd(Stream stream, Encoding encoding)
{
using (var reader = new StreamReader(stream, encoding ?? LaxUtf8Marshaler.Encoding, encoding == null))
{
return reader.ReadToEnd();
}
}
}
}
76 changes: 0 additions & 76 deletions LibGit2Sharp/BlobExtensions.cs

This file was deleted.

87 changes: 87 additions & 0 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,31 @@ public virtual Branch Add(string name, string committish)
return Add(name, committish, false);
}

/// <summary>
/// Create a new local branch with the specified name
/// </summary>
/// <param name="name">The name of the branch.</param>
/// <param name="commit">The target commit.</param>
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Add(string name, Commit commit)
{
return Add(name, commit, false);
}

/// <summary>
/// Create a new local branch with the specified name
/// </summary>
/// <param name="name">The name of the branch.</param>
/// <param name="commit">The target commit.</param>
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Add(string name, Commit commit, bool allowOverwrite)
{
Ensure.ArgumentNotNull(commit, "commit");

return Add(name, commit.Sha, allowOverwrite);
}

/// <summary>
/// Create a new local branch with the specified name
/// </summary>
Expand All @@ -135,6 +160,35 @@ public virtual Branch Add(string name, string committish, bool allowOverwrite)
return branch;
}

/// <summary>
/// Deletes the branch with the specified name.
/// </summary>
/// <param name="name">The name of the branch to delete.</param>
public virtual void Remove(string name)
{
Remove(name, false);
}

/// <summary>
/// Deletes the branch with the specified name.
/// </summary>
/// <param name="name">The name of the branch to delete.</param>
/// <param name="isRemote">True if the provided <paramref name="name"/> is the name of a remote branch, false otherwise.</param>
public virtual void Remove(string name, bool isRemote)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");

string branchName = isRemote ? Reference.RemoteTrackingBranchPrefix + name : name;

Branch branch = this[branchName];

if (branch == null)
{
return;
}

Remove(branch);
}
/// <summary>
/// Deletes the specified branch.
/// </summary>
Expand All @@ -149,6 +203,39 @@ public virtual void Remove(Branch branch)
}
}

/// <summary>
/// Rename an existing local branch, using the default reflog message
/// </summary>
/// <param name="currentName">The current branch name.</param>
/// <param name="newName">The new name the existing branch should bear.</param>
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Rename(string currentName, string newName)
{
return Rename(currentName, newName, false);
}

/// <summary>
/// Rename an existing local branch, using the default reflog message
/// </summary>
/// <param name="currentName">The current branch name.</param>
/// <param name="newName">The new name the existing branch should bear.</param>
/// <param name="allowOverwrite">True to allow silent overwriting a potentially existing branch, false otherwise.</param>
/// <returns>A new <see cref="Branch"/>.</returns>
public virtual Branch Rename(string currentName, string newName, bool allowOverwrite)
{
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
Ensure.ArgumentNotNullOrEmptyString(newName, "newName");

Branch branch = this[currentName];

if (branch == null)
{
throw new LibGit2SharpException("No branch named '{0}' exists in the repository.");
}

return Rename(branch, newName, allowOverwrite);
}

/// <summary>
/// Rename an existing local branch
/// </summary>
Expand Down
Loading