Skip to content

Don't build a fake signature #1171

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 1 commit into from
Aug 17, 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
57 changes: 16 additions & 41 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -743,12 +743,8 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
}

/// <summary>
/// Builds a <see cref="Signature"/> based on current configuration.
/// <para>
/// Name is populated from the user.name setting, and is "unknown" if unspecified.
/// Email is populated from the user.email setting, and is built from
/// <see cref="Environment.UserName"/> and <see cref="Environment.UserDomainName"/> if unspecified.
/// </para>
/// Builds a <see cref="Signature"/> based on current configuration. If it is not found or
/// some configuration is missing, <code>null</code> is returned.
/// <para>
/// The same escalation logic than in git.git will be used when looking for the key in the config files:
/// - local: the Git file in the current repository
Expand All @@ -758,51 +754,30 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
/// </para>
/// </summary>
/// <param name="now">The timestamp to use for the <see cref="Signature"/>.</param>
/// <returns>The signature.</returns>
/// <returns>The signature or null if no user identity can be found in the configuration.</returns>
public virtual Signature BuildSignature(DateTimeOffset now)
{
return BuildSignature(now, false);
}

internal Signature BuildSignature(DateTimeOffset now, bool shouldThrowIfNotFound)
{
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));
var name = this.GetValueOrDefault<string>("user.name");
var email = this.GetValueOrDefault<string>("user.email");

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

private string NormalizeUserSetting(bool shouldThrowIfNotFound, string entryName, string currentValue, Func<string> defaultValue)
{
if (!string.IsNullOrEmpty(currentValue))
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(email))
{
return currentValue;
return null;
}

string message = string.Format("Configuration value '{0}' is missing or invalid.", entryName);
return new Signature(name, email, now);
}

if (shouldThrowIfNotFound)
internal Signature BuildSignatureOrThrow(DateTimeOffset now)
{
var signature = BuildSignature(now);
if (signature == null)
{
throw new LibGit2SharpException(message);
throw new LibGit2SharpException("This overload requires 'user.name' and 'user.email' to be set. " +
"Use a different overload or set those variables in the configuation");
}

Log.Write(LogLevel.Warning, message);

return defaultValue();
return signature;
}

private ConfigurationSafeHandle Snapshot()
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/NoteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ internal static string UnCanonicalizeName(string name)
/// <returns>The note which was just saved.</returns>
public virtual Note Add(ObjectId targetId, string message, string @namespace)
{
Signature author = repo.Config.BuildSignature(DateTimeOffset.Now, true);
Signature author = repo.Config.BuildSignatureOrThrow(DateTimeOffset.Now);

return Add(targetId, message, author, author, @namespace);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public virtual Note Add(ObjectId targetId, string message, Signature author, Sig
/// <param name="namespace">The namespace on which the note will be removed. It can be either a canonical namespace or an abbreviated namespace ('refs/notes/myNamespace' or just 'myNamespace').</param>
public virtual void Remove(ObjectId targetId, string @namespace)
{
Signature author = repo.Config.BuildSignature(DateTimeOffset.Now, true);
Signature author = repo.Config.BuildSignatureOrThrow(DateTimeOffset.Now);

Remove(targetId, author, author, @namespace);
}
Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/RepositoryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public static Commit Commit(this IRepository repository, string message)
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
public static Commit Commit(this IRepository repository, string message, CommitOptions options)
{
Signature author = repository.Config.BuildSignature(DateTimeOffset.Now, true);
Signature author = repository.Config.BuildSignatureOrThrow(DateTimeOffset.Now);

return repository.Commit(message, author, options);
}
Expand Down Expand Up @@ -270,7 +270,7 @@ public static Commit Commit(this IRepository repository, string message, Signatu
/// <returns>The generated <see cref="LibGit2Sharp.Commit"/>.</returns>
public static Commit Commit(this IRepository repository, string message, Signature author, CommitOptions options)
{
Signature committer = repository.Config.BuildSignature(DateTimeOffset.Now, true);
Signature committer = repository.Config.BuildSignatureOrThrow(DateTimeOffset.Now);

return repository.Commit(message, author, committer, options);
}
Expand Down