Skip to content

Random Refactoring #232

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

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,8 @@ public void CanCommitWithSignatureFromConfig()
AssertBlobContent(repo.Head[relativeFilepath], "nulltoken\n");
AssertBlobContent(commit[relativeFilepath], "nulltoken\n");

var name = repo.Config.Get<string>("user.name", null);
var email = repo.Config.Get<string>("user.email", null);
var name = repo.Config.Get<string>("user.name");
var email = repo.Config.Get<string>("user.email");
Assert.Equal(commit.Author.Name, name);
Assert.Equal(commit.Author.Email, email);
Assert.Equal(commit.Committer.Name, name);
Expand Down
46 changes: 26 additions & 20 deletions LibGit2Sharp.Tests/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void CanUnsetAnEntryFromTheLocalConfiguration()
var path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
using (var repo = new Repository(path.RepositoryPath))
{
Assert.False(repo.Config.Get<bool>("unittests.boolsetting", false));
Assert.False(repo.Config.Get<bool>("unittests.boolsetting"));

repo.Config.Set("unittests.boolsetting", true);
Assert.True(repo.Config.Get<bool>("unittests.boolsetting", false));
Assert.True(repo.Config.Get<bool>("unittests.boolsetting"));

repo.Config.Unset("unittests.boolsetting");

Assert.False(repo.Config.Get<bool>("unittests.boolsetting", false));
Assert.False(repo.Config.Get<bool>("unittests.boolsetting"));
}
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public void CanGetGlobalStringValue()
{
InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available");

Assert.NotNull(repo.Config.Get<string>("user.name", null));
Assert.NotNull(repo.Config.Get<string>("user.name"));
}
}

Expand All @@ -114,7 +114,7 @@ public void CanGetGlobalStringValueWithoutRepo()
using (var config = new Configuration())
{
InconclusiveIf(() => !config.HasGlobalConfig, "No Git global configuration available");
Assert.NotNull(config.Get<string>("user.name", null));
Assert.NotNull(config.Get<string>("user.name"));
}
}

Expand All @@ -123,8 +123,8 @@ public void CanReadBooleanValue()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.True(repo.Config.Get<bool>("core.ignorecase", false));
Assert.True(repo.Config.Get<bool>("core", "ignorecase", false));
Assert.True(repo.Config.Get<bool>("core.ignorecase"));
Assert.Equal(true, repo.Config.Get<bool?>("core.ignorecase"));
}
}

Expand All @@ -133,8 +133,8 @@ public void CanReadIntValue()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting", 42));
Assert.Equal(2, repo.Config.Get<int>("unittests", "intsetting", 42));
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting"));
Assert.Equal(2, repo.Config.Get<int?>("unittests.intsetting"));
}
}

Expand All @@ -143,8 +143,8 @@ public void CanReadLongValue()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting", 42));
Assert.Equal(15234, repo.Config.Get<long>("unittests", "longsetting", 42));
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting"));
Assert.Equal(15234, repo.Config.Get<long?>("unittests.longsetting"));
}
}

Expand All @@ -153,7 +153,7 @@ public void CanReadStringValue()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch", null));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch"));
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote", "origin", "fetch", null));
}
}
Expand Down Expand Up @@ -200,7 +200,7 @@ public void CanSetGlobalStringValue()
{
InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available");

var existing = repo.Config.Get<string>("user.name", null);
var existing = repo.Config.Get<string>("user.name");
Assert.NotNull(existing);

try
Expand All @@ -223,7 +223,7 @@ public void CanSetGlobalStringValueWithoutRepo()
{
InconclusiveIf(() => !config.HasGlobalConfig, "No Git global configuration available");

var existing = config.Get<string>("user.name", null);
var existing = config.Get<string>("user.name");
Assert.NotNull(existing);

try
Expand Down Expand Up @@ -311,8 +311,8 @@ public void ReadingUnsupportedTypeThrows()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Throws<ArgumentException>(() => repo.Config.Get<short>("unittests.setting", 42));
Assert.Throws<ArgumentException>(() => repo.Config.Get<Configuration>("unittests.setting", null));
Assert.Throws<ArgumentException>(() => repo.Config.Get<short>("unittests.setting"));
Assert.Throws<ArgumentException>(() => repo.Config.Get<Configuration>("unittests.setting"));
}
}

Expand All @@ -321,14 +321,20 @@ public void ReadingValueThatDoesntExistReturnsDefault()
{
using (var repo = new Repository(StandardTestRepoPath))
{
Assert.Null(repo.Config.Get<string>("unittests.ghostsetting", null));
Assert.Equal(0, repo.Config.Get<int>("unittests.ghostsetting", 0));
Assert.Equal(0L, repo.Config.Get<long>("unittests.ghostsetting", 0L));
Assert.False(repo.Config.Get<bool>("unittests.ghostsetting", false));
Assert.Null(repo.Config.Get<string>("unittests.ghostsetting"));
Assert.Equal(0, repo.Config.Get<int>("unittests.ghostsetting"));
Assert.Null(repo.Config.Get<int?>("unittests.ghostsetting"));
Assert.Equal(0L, repo.Config.Get<long>("unittests.ghostsetting"));
Assert.Null(repo.Config.Get<long?>("unittests.ghostsetting"));
Assert.False(repo.Config.Get<bool>("unittests.ghostsetting"));
Assert.Null(repo.Config.Get<bool?>("unittests.ghostsetting"));
Assert.Equal("42", repo.Config.Get("unittests.ghostsetting", "42"));
Assert.Equal(42, repo.Config.Get("unittests.ghostsetting", 42));
Assert.Equal(42, repo.Config.Get<int?>("unittests.ghostsetting", 42));
Assert.Equal(42L, repo.Config.Get("unittests.ghostsetting", 42L));
Assert.Equal(42L, repo.Config.Get<long?>("unittests.ghostsetting", 42L));
Assert.True(repo.Config.Get("unittests.ghostsetting", true));
Assert.Equal(true, repo.Config.Get<bool?>("unittests.ghostsetting", true));
}
}

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/RepositoryOptionsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ public void CanProvideDifferentConfigurationFilesToARepository()
using (var repo = new Repository(BareTestRepoPath, options))
{
Assert.True(repo.Config.HasGlobalConfig);
Assert.Equal(name, repo.Config.Get<string>("user", "name", null));
Assert.Equal(email, repo.Config.Get<string>("user", "email", null));
Assert.Equal(name, repo.Config.Get<string>("user.name"));
Assert.Equal(email, repo.Config.Get<string>("user.email"));

repo.Config.Set("help.link", "https://twitter.com/xpaulbettsx/status/205761932626636800", ConfigurationLevel.System);
}
Expand Down
29 changes: 1 addition & 28 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,39 +89,12 @@ private Branch BuildFromReferenceName(string canonicalName)
/// <returns>An <see cref = "IEnumerator{T}" /> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<Branch> GetEnumerator()
{
return new BranchNameEnumerable(repo.Handle, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE)
return Proxy.git_branch_foreach(repo.Handle, GitBranchType.GIT_BRANCH_LOCAL | GitBranchType.GIT_BRANCH_REMOTE, (b, t) => Utf8Marshaler.FromNative(b))
.Select(n => this[n])
.OrderBy(b => b.CanonicalName, StringComparer.Ordinal)
.GetEnumerator();
}

private class BranchNameEnumerable : IEnumerable<string>
{
private readonly List<string> list = new List<string>();

public BranchNameEnumerable(RepositorySafeHandle handle, GitBranchType gitBranchType)
{
Proxy.git_branch_foreach(handle, gitBranchType, Callback);
}

private int Callback(IntPtr branchName, GitBranchType branchType, IntPtr payload)
{
string name = Utf8Marshaler.FromNative(branchName);
list.Add(name);
return 0;
}

public IEnumerator<string> GetEnumerator()
{
return list.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}

/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
Expand Down
19 changes: 10 additions & 9 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ protected virtual void Dispose(bool disposing)
/// <param name = "key">The key</param>
/// <param name = "defaultValue">The default value</param>
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
public virtual T Get<T>(string key, T defaultValue)
public virtual T Get<T>(string key, T defaultValue = default(T))
{
Ensure.ArgumentNotNullOrEmptyString(key, "key");

Expand Down Expand Up @@ -227,6 +227,7 @@ public virtual T Get<T>(string key, T defaultValue)
/// <param name = "secondKeyPart">The second key part</param>
/// <param name = "defaultValue">The default value</param>
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
[Obsolete("This method will be removed in the next release. Please use a different overload instead.")]
public virtual T Get<T>(string firstKeyPart, string secondKeyPart, T defaultValue)
{
Ensure.ArgumentNotNull(firstKeyPart, "firstKeyPart");
Expand Down Expand Up @@ -288,7 +289,7 @@ public virtual T Get<T>(string firstKeyPart, string secondKeyPart, string thirdK
/// <param name = "keyParts">The key parts</param>
/// <param name = "defaultValue">The default value</param>
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
public virtual T Get<T>(string[] keyParts, T defaultValue)
public virtual T Get<T>(string[] keyParts, T defaultValue = default(T))
{
Ensure.ArgumentNotNull(keyParts, "keyParts");

Expand Down Expand Up @@ -316,7 +317,7 @@ private void Save()
/// </summary>
/// <typeparam name = "T">The configuration value type</typeparam>
/// <param name = "key">The key parts</param>
/// <param name = "value">The default value</param>
/// <param name = "value">The value</param>
/// <param name = "level">The configuration file which should be considered as the target of this operation</param>
public virtual void Set<T>(string key, T value, ConfigurationLevel level = ConfigurationLevel.Local)
{
Expand Down Expand Up @@ -389,8 +390,11 @@ private static Func<string, object, ConfigurationSafeHandle, object> GetRetrieve
private readonly IDictionary<Type, Func<string, object, ConfigurationSafeHandle, object>> configurationTypedRetriever = new Dictionary<Type, Func<string, object, ConfigurationSafeHandle, object>>
{
{ typeof(int), GetRetriever(Proxy.git_config_get_int32) },
{ typeof(int?), GetRetriever(Proxy.git_config_get_int32) },
{ typeof(long), GetRetriever(Proxy.git_config_get_int64) },
{ typeof(long?), GetRetriever(Proxy.git_config_get_int64) },
{ typeof(bool), GetRetriever(Proxy.git_config_get_bool) },
{ typeof(bool?), GetRetriever(Proxy.git_config_get_bool) },
{ typeof(string), GetRetriever(Proxy.git_config_get_string) },
};

Expand All @@ -409,14 +413,11 @@ private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Act

IEnumerator<ConfigurationEntry> IEnumerable<ConfigurationEntry>.GetEnumerator()
{
var values = new List<ConfigurationEntry>();
Proxy.git_config_foreach(LocalHandle, (namePtr, valuePtr, _) => {
return Proxy.git_config_foreach(LocalHandle, (namePtr, valuePtr) => {
var name = Utf8Marshaler.FromNative(namePtr);
var value = Utf8Marshaler.FromNative(valuePtr);
values.Add(new ConfigurationEntry(name, value));
return 0;
});
return values.GetEnumerator();
return new ConfigurationEntry(name, value);
}).GetEnumerator();
}

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/LambdaEqualityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal class LambdaEqualityHelper<T>
{
private readonly Func<T, object>[] equalityContributorAccessors;

public LambdaEqualityHelper(Func<T, object>[] equalityContributorAccessors)
public LambdaEqualityHelper(params Func<T, object>[] equalityContributorAccessors)
{
this.equalityContributorAccessors = equalityContributorAccessors;
}
Expand Down
2 changes: 2 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.InteropServices;
using LibGit2Sharp.Core.Handles;

// ReSharper disable InconsistentNaming
namespace LibGit2Sharp.Core
{
internal static class NativeMethods
Expand Down Expand Up @@ -790,3 +791,4 @@ internal static extern int git_treebuilder_insert(
internal static extern void git_treebuilder_free(IntPtr bld);
}
}
// ReSharper restore InconsistentNaming
Loading