8000 Refactor Signature creation from configuration · raiden2012/libgit2sharp@8a71afe · GitHub
[go: up one dir, main page]

Skip to content

Commit 8a71afe

Browse files
committed
Refactor Signature creation from configuration
1 parent 381428e commit 8a71afe

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

LibGit2Sharp/Configuration.cs

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -342,29 +342,35 @@ public virtual Signature BuildSignature(DateTimeOffset now)
342342

343343
internal Signature BuildSignature(DateTimeOffset now, bool shouldThrowIfNotFound)
344344
{
345-
var name = this.GetValueOrDefault<string>("user.name");
346-
var email = this.GetValueOrDefault<string>("user.email");
345+
const string userNameKey = "user.name";
346+
var name = this.GetValueOrDefault<string>(userNameKey);
347+
var normalizedName = NormalizeUserSetting(shouldThrowIfNotFound, userNameKey, name,
348+
() => "unknown");
349+
350+
const string userEmailKey = "user.email";
351+
var email = this.GetValueOrDefault<string>(userEmailKey);
352+
var normalizedEmail = NormalizeUserSetting(shouldThrowIfNotFound, userEmailKey, email,
353+
() => string.Format(
354+
CultureInfo.InvariantCulture, "{0}@{1}", Environment.UserName, Environment.UserDomainName));
355+
356+
return new Signature(normalizedName, normalizedEmail, now);
357+
}
347358

348-
if (shouldThrowIfNotFound && (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email)))
359+
private string NormalizeUserSetting(bool shouldThrowIfNotFound, string entryName, string currentValue, Func<string> defaultValue)
360+
{
361+
if (!string.IsNullOrEmpty(currentValue))
349362
{
350-
if (string.IsNullOrEmpty(name))
351-
{
352-
throw new LibGit2SharpException(
353-
"Cannot find Name setting of the current user in Git configuration.");
354-
}
363+
return currentValue;
364+
}
355365

356-
if (string.IsNullOrEmpty(email))
357-
{
358-
throw new LibGit2SharpException(
359-
"Cannot find Email setting of the current user in Git configuration.");
360-
}
366+
string message = string.Format("Configuration value '{0}' is missing or invalid.", entryName);
367+
368+
if (shouldThrowIfNotFound)
369+
{
370+
throw new LibGit2SharpException(message);
361371
}
362372

363-
return new Signature(
364-
!string.IsNullOrEmpty(name) ? name : "unknown",
365-
!string.IsNullOrEmpty(email) ? email : string.Format(
366-
CultureInfo.InvariantCulture, "{0}@{1}", Environment.UserName, Environment.UserDomainName),
367-
now);
373+
return defaultValue();
368374
}
369375

370376
private ConfigurationSafeHandle Snapshot()

0 commit comments

Comments
 (0)
0