Skip to content
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
22 changes: 22 additions & 0 deletions LibGit2Sharp.Tests/CommitFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using LibGit2Sharp.Core;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand Down Expand Up @@ -519,6 +520,27 @@ public void DirectlyAccessingAnUnknownTreeEntryOfTheCommitReturnsNull()
}
}

[Theory]
[InlineData(null, "x@example.com")]
[InlineData("", "x@example.com")]
[InlineData("X", null)]
[InlineData("X", "")]
public void CommitWithInvalidSignatureConfigThrows(string name, string email)
{
string repoPath = InitNewRepository();
string configPath = CreateConfigurationWithDummyUser(name, email);
var options = new RepositoryOptions { GlobalConfigurationLocation = configPath };

using (var repo = new Repository(repoPath, options))
{
Assert.Equal(name, repo.Config.GetValueOrDefault<string>("user.name"));
Assert.Equal(email, repo.Config.GetValueOrDefault<string>("user.email"));

Assert.Throws<LibGit2SharpException>(
() => repo.Commit("Initial egotistic commit", new CommitOptions { AllowEmptyCommit = true }));
}
}

[Fact]
public void CanCommitWithSignatureFromConfig()
{
Expand Down
8 changes: 1 addition & 7 deletions LibGit2Sharp.Tests/SignatureFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public void CreatingASignatureWithBadParamsThrows()
Assert.Throws<ArgumentNullException>(() => new Signature(null, "me@there.com", DateTimeOffset.Now));
Assert.Throws<ArgumentException>(() => new Signature(string.Empty, "me@there.com", DateTimeOffset.Now));
Assert.Throws<ArgumentNullException>(() => new Signature("Me", null, DateTimeOffset.Now));
}

[Fact]
public void CanCreateASignatureWithAnEmptyEmail()
{
var sig = new Signature("Me", string.Empty, DateTimeOffset.Now);
Assert.Equal(string.Empty, sig.Email);
Assert.Throws<ArgumentException>(() => new Signature("Me", string.Empty, DateTimeOffset.Now));
}
}
}
16 changes: 14 additions & 2 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,27 @@ private static RepositoryOptions BuildFakeRepositoryOptions(SelfCleaningDirector
/// <param name="signature">The signature to use for user.name and user.email</param>
/// <returns>The path to the configuration file</returns>
protected string CreateConfigurationWithDummyUser(Signature signature)
{
return CreateConfigurationWithDummyUser(signature.Name, signature.Email);
}

protected string CreateConfigurationWithDummyUser(string name, string email)
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
Directory.CreateDirectory(scd.DirectoryPath);
string configFilePath = Path.Combine(scd.DirectoryPath, "global-config");

using (Configuration config = new Configuration(configFilePath))
{
config.Set("user.name", signature.Name, ConfigurationLevel.Global);
config.Set("user.email", signature.Email, ConfigurationLevel.Global);
if (name != null)
{
config.Set("user.name", name, ConfigurationLevel.Global);
}

if (email != null)
{
config.Set("user.email", email, ConfigurationLevel.Global);
}
}

return configFilePath;
Expand Down
44 changes: 26 additions & 18 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public virtual ConfigurationEntry<T> Get<T>(string key, ConfigurationLevel level
/// <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)
{
Ensure.ArgumentNotNull(value, "value");
Ensure.ArgumentNotNullOrEmptyString(key, "key");

using (ConfigurationSafeHandle h = RetrieveConfigurationHandle(level, true, configHandle))
Expand Down Expand Up @@ -341,30 +342,37 @@ public virtual Signature BuildSignature(DateTimeOffset now)

internal Signature BuildSignature(DateTimeOffset now, bool shouldThrowIfNotFound)
{
var name = Get<string>("user.name");
var email = Get<string>("user.email");
const string userNameKey = "user.name";
var name = this.GetValueOrDefault<string>(userNameKey);
var normalizedName = NormalizeUserSetting(shouldThrowIfNotFound, userNameKey, name,
() => "unknown");

const string userEmailKey = "user.email";
var email = this.GetValueOrDefault<string>(userEmailKey);
var normalizedEmail = NormalizeUserSetting(shouldThrowIfNotFound, userEmailKey, email,
() => string.Format(
CultureInfo.InvariantCulture, "{0}@{1}", Environment.UserName, Environment.UserDomainName));

return new Signature(normalizedName, normalizedEmail, now);
}

if (shouldThrowIfNotFound)
private string NormalizeUserSetting(bool shouldThrowIfNotFound, string entryName, string currentValue, Func<string> defaultValue)
{
if (!string.IsNullOrEmpty(currentValue))
{
if (name == null || string.IsNullOrEmpty(name.Value))
{
throw new LibGit2SharpException(
"Can not find Name setting of the current user in Git configuration.");
}
return currentValue;
}

if (email == null || string.IsNullOrEmpty(email.Value))
{
throw new LibGit2SharpException(
"Can not find Email setting of the current user in Git configuration.");
}
string message = string.Format("Configuration value '{0}' is missing or invalid.", entryName);

if (shouldThrowIfNotFound)
{
throw new LibGit2SharpException(message);
}

var nameForSignature = name == null || string.IsNullOrEmpty(name.Value) ? "unknown" : name.Value;
var emailForSignature = email == null || string.IsNullOrEmpty(email.Value)
? string.Format("{0}@{1}", Environment.UserName, Environment.UserDomainName)
: email.Value;
Log.Write(LogLevel.Warning, message);

return new Signature(nameForSignature, emailForSignature, now);
return defaultValue();
}

private ConfigurationSafeHandle Snapshot()
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Signature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal Signature(IntPtr signaturePtr)
public Signature(string name, string email, DateTimeOffset when)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(email, "email");
Ensure.ArgumentNotNullOrEmptyString(email, "email");
Ensure.ArgumentDoesNotContainZeroByte(name, "name");
Ensure.ArgumentDoesNotContainZeroByte(email, "email");

Expand Down