Skip to content

Commit 67fa534

Browse files
committed
Simplify Configuration.Get() usage
Contains two changes: 1. Remove the firstKeyPart,secondKeyPart overload. Having a three-part overload is useful because the middle part is often dynamic, but it never is for two-part keys. 2. Make defaultValue optional, and update tests/usage accordingly.
1 parent 163bb04 commit 67fa534

File tree

5 files changed

+28
-62
lines changed

5 files changed

+28
-62
lines changed

LibGit2Sharp.Tests/CommitFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,8 @@ public void CanCommitWithSignatureFromConfig()
491491
AssertBlobContent(repo.Head[relativeFilepath], "nulltoken\n");
492492
AssertBlobContent(commit[relativeFilepath], "nulltoken\n");
493493

494-
var name = repo.Config.Get<string>("user.name", null);
495-
var email = repo.Config.Get<string>("user.email", null);
494+
var name = repo.Config.Get<string>("user.name");
495+
var email = repo.Config.Get<string>("user.email");
496496
Assert.Equal(commit.Author.Name, name);
497497
Assert.Equal(commit.Author.Email, email);
498498
Assert.Equal(commit.Committer.Name, name);

LibGit2Sharp.Tests/ConfigurationFixture.cs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ public void CanUnsetAnEntryFromTheLocalConfiguration()
5050
var path = BuildTemporaryCloneOfTestRepo(StandardTestRepoPath);
5151
using (var repo = new Repository(path.RepositoryPath))
5252
{
53-
Assert.False(repo.Config.Get<bool>("unittests.boolsetting", false));
53+
Assert.False(repo.Config.Get<bool>("unittests.boolsetting"));
5454

5555
repo.Config.Set("unittests.boolsetting", true);
56-
Assert.True(repo.Config.Get<bool>("unittests.boolsetting", false));
56+
Assert.True(repo.Config.Get<bool>("unittests.boolsetting"));
5757

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

60-
Assert.False(repo.Config.Get<bool>("unittests.boolsetting", false));
60+
Assert.False(repo.Config.Get<bool>("unittests.boolsetting"));
6161
}
6262
}
6363

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

107-
Assert.NotNull(repo.Config.Get<string>("user.name", null));
107+
Assert.NotNull(repo.Config.Get<string>("user.name"));
108108
}
109109
}
110110

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

@@ -123,8 +123,7 @@ public void CanReadBooleanValue()
123123
{
124124
using (var repo = new Repository(StandardTestRepoPath))
125125
{
126-
Assert.True(repo.Config.Get<bool>("core.ignorecase", false));
127-
Assert.True(repo.Config.Get<bool>("core", "ignorecase", false));
126+
Assert.True(repo.Config.Get<bool>("core.ignorecase"));
128127
}
129128
}
130129

@@ -133,8 +132,7 @@ public void CanReadIntValue()
133132
{
134133
using (var repo = new Repository(StandardTestRepoPath))
135134
{
136-
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting", 42));
137-
Assert.Equal(2, repo.Config.Get<int>("unittests", "intsetting", 42));
135+
Assert.Equal(2, repo.Config.Get<int>("unittests.intsetting"));
138136
}
139137
}
140138

@@ -143,8 +141,7 @@ public void CanReadLongValue()
143141
{
144142
using (var repo = new Repository(StandardTestRepoPath))
145143
{
146-
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting", 42));
147-
Assert.Equal(15234, repo.Config.Get<long>("unittests", "longsetting", 42));
144+
Assert.Equal(15234, repo.Config.Get<long>("unittests.longsetting"));
148145
}
149146
}
150147

@@ -153,8 +150,8 @@ public void CanReadStringValue()
153150
{
154151
using (var repo = new Repository(StandardTestRepoPath))
155152
{
156-
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch", null));
157-
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote", "origin", "fetch", null));
153+
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote.origin.fetch"));
154+
Assert.Equal("+refs/heads/*:refs/remotes/origin/*", repo.Config.Get<string>("remote", "origin", "fetch"));
158155
}
159156
}
160157

@@ -200,7 +197,7 @@ public void CanSetGlobalStringValue()
200197
{
201198
InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available");
202199

203-
var existing = repo.Config.Get<string>("user.name", null);
200+
var existing = repo.Config.Get<string>("user.name");
204201
Assert.NotNull(existing);
205202

206203
try
@@ -223,7 +220,7 @@ public void CanSetGlobalStringValueWithoutRepo()
223220
{
224221
InconclusiveIf(() => !config.HasGlobalConfig, "No Git global configuration available");
225222

226-
var existing = config.Get<string>("user.name", null);
223+
var existing = config.Get<string>("user.name");
227224
Assert.NotNull(existing);
228225

229226
try
@@ -311,8 +308,8 @@ public void ReadingUnsupportedTypeThrows()
311308
{
312309
using (var repo = new Repository(StandardTestRepoPath))
313310
{
314-
Assert.Throws<ArgumentException>(() => repo.Config.Get<short>("unittests.setting", 42));
315-
Assert.Throws<ArgumentException>(() => repo.Config.Get<Configuration>("unittests.setting", null));
311+
Assert.Throws<ArgumentException>(() => repo.Config.Get<short>("unittests.setting"));
312+
Assert.Throws<ArgumentException>(() => repo.Config.Get<Configuration>("unittests.setting"));
316313
}
317314
}
318315

@@ -321,10 +318,10 @@ public void ReadingValueThatDoesntExistReturnsDefault()
321318
{
322319
using (var repo = new Repository(StandardTestRepoPath))
323320
{
324-
Assert.Null(repo.Config.Get<string>("unittests.ghostsetting", null));
325-
Assert.Equal(0, repo.Config.Get<int>("unittests.ghostsetting", 0));
326-
Assert.Equal(0L, repo.Config.Get<long>("unittests.ghostsetting", 0L));
327-
Assert.False(repo.Config.Get<bool>("unittests.ghostsetting", false));
321+
Assert.Null(repo.Config.Get<string>("unittests.ghostsetting"));
322+
Assert.Equal(0, repo.Config.Get<int>("unittests.ghostsetting"));
323+
Assert.Equal(0L, repo.Config.Get<long>("unittests.ghostsetting"));
324+
Assert.False(repo.Config.Get<bool>("unittests.ghostsetting"));
328325
Assert.Equal("42", repo.Config.Get("unittests.ghostsetting", "42"));
329326
Assert.Equal(42, repo.Config.Get("unittests.ghostsetting", 42));
330327
Assert.Equal(42L, repo.Config.Get("unittests.ghostsetting", 42L));

LibGit2Sharp.Tests/RepositoryOptionsFixture.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ public void CanProvideDifferentConfigurationFilesToARepository()
166166
using (var repo = new Repository(BareTestRepoPath, options))
167167
{
168168
Assert.True(repo.Config.HasGlobalConfig);
169-
Assert.Equal(name, repo.Config.Get<string>("user", "name", null));
170-
Assert.Equal(email, repo.Config.Get<string>("user", "email", null));
169+
Assert.Equal(name, repo.Config.Get<string>("user.name"));
170+
Assert.Equal(email, repo.Config.Get<string>("user.email"));
171171

172172
repo.Config.Set("help.link", "https://twitter.com/xpaulbettsx/status/205761932626636800", ConfigurationLevel.System);
173173
}

LibGit2Sharp/Configuration.cs

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ protected virtual void Dispose(bool disposing)
187187
/// <param name = "key">The key</param>
188188
/// <param name = "defaultValue">The default value</param>
189189
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
190-
public virtual T Get<T>(string key, T defaultValue)
190+
public virtual T Get<T>(string key, T defaultValue = default(T))
191191
{
192192
Ensure.ArgumentNotNullOrEmptyString(key, "key");
193193

@@ -205,36 +205,6 @@ public virtual T Get<T>(string key, T defaultValue)
205205
return (T)configurationTypedRetriever[typeof(T)](key, defaultValue, handle);
206206
}
207207

208-
/// <summary>
209-
/// Get a configuration value for a key. Keys are in the form 'section.name'.
210-
/// <para>
211-
/// For example in order to get the value for this in a .git\config file:
212-
///
213-
/// <code>
214-
/// [core]
215-
/// bare = true
216-
/// </code>
217-
///
218-
/// You would call:
219-
///
220-
/// <code>
221-
/// bool isBare = repo.Config.Get&lt;bool&gt;("core", "bare", false);
222-
/// </code>
223-
/// </para>
224-
/// </summary>
225-
/// <typeparam name = "T">The configuration value type</typeparam>
226-
/// <param name = "firstKeyPart">The first key part</param>
227-
/// <param name = "secondKeyPart">The second key part</param>
228-
/// <param name = "defaultValue">The default value</param>
229-
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
230-
public virtual T Get<T>(string firstKeyPart, string secondKeyPart, T defaultValue)
231-
{
232-
Ensure.ArgumentNotNull(firstKeyPart, "firstKeyPart");
233-
Ensure.ArgumentNotNull(secondKeyPart, "secondKeyPart");
234-
235-
return Get(new[] { firstKeyPart, secondKeyPart }, defaultValue);
236-
}
237-
238208
/// <summary>
239209
/// Get a configuration value for the given key parts.
240210
/// <para>
@@ -258,7 +228,7 @@ public virtual T Get<T>(string firstKeyPart, string secondKeyPart, T defaultValu
258228
/// <param name = "thirdKeyPart">The third key part</param>
259229
/// <param name = "defaultValue">The default value</param>
260230
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
261-
public virtual T Get<T>(string firstKeyPart, string secondKeyPart, string thirdKeyPart, T defaultValue)
231+
public virtual T Get<T>(string firstKeyPart, string secondKeyPart, string thirdKeyPart, T defaultValue = default(T))
262232
{
263233
Ensure.ArgumentNotNull(firstKeyPart, "firstKeyPart");
264234
Ensure.ArgumentNotNull(secondKeyPart, "secondKeyPart");
@@ -288,7 +258,7 @@ public virtual T Get<T>(string firstKeyPart, string secondKeyPart, string thirdK
288258
/// <param name = "keyParts">The key parts</param>
289259
/// <param name = "defaultValue">The default value</param>
290260
/// <returns>The configuration value, or <c>defaultValue</c> if not set</returns>
291-
public virtual T Get<T>(string[] keyParts, T defaultValue)
261+
public virtual T Get<T>(string[] keyParts, T defaultValue = default(T))
292262
{
293263
Ensure.ArgumentNotNull(keyParts, "keyParts");
294264

@@ -316,7 +286,7 @@ private void Save()
316286
/// </summary>
317287
/// <typeparam name = "T">The configuration value type</typeparam>
318288
/// <param name = "key">The key parts</param>
319-
/// <param name = "value">The default value</param>
289+
/// <param name = "value">The value</param>
320290
/// <param name = "level">The configuration file which should be considered as the target of this operation</param>
321291
public virtual void Set<T>(string key, T value, ConfigurationLevel level = ConfigurationLevel.Local)
322292
{

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using LibGit2Sharp.Core;
32

43
namespace LibGit2Sharp
54
{
@@ -147,8 +146,8 @@ public static Commit Commit(this IRepository repository, string message, Signatu
147146

148147
private static Signature BuildSignatureFromGlobalConfiguration(IRepository repository, DateTimeOffset now)
149148
{
150-
var name = repository.Config.Get<string>("user.name", null);
151-
var email = repository.Config.Get<string>("user.email", null);
149+
var name = repository.Config.Get<string>("user.name");
150+
var email = repository.Config.Get<string>("user.email");
152151

153152
if ((name == null) || (email == null))
154153
{

0 commit comments

Comments
 (0)