diff --git a/LibGit2Sharp/Configuration.cs b/LibGit2Sharp/Configuration.cs index 69133fc96..3cce3610d 100644 --- a/LibGit2Sharp/Configuration.cs +++ b/LibGit2Sharp/Configuration.cs @@ -743,12 +743,8 @@ private static ConfigurationEntry BuildConfigEntry(IntPtr entryPtr) } /// - /// Builds a based on current configuration. - /// - /// 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 - /// and if unspecified. - /// + /// Builds a based on current configuration. If it is not found or + /// some configuration is missing, null is returned. /// /// 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 @@ -758,51 +754,30 @@ private static ConfigurationEntry BuildConfigEntry(IntPtr entryPtr) /// /// /// The timestamp to use for the . - /// The signature. + /// The signature or null if no user identity can be found in the configuration. 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(userNameKey); - var normalizedName = NormalizeUserSetting(shouldThrowIfNotFound, - userNameKey, - name, - () => "unknown"); - - const string userEmailKey = "user.email"; - var email = this.GetValueOrDefault(userEmailKey); - var normalizedEmail = NormalizeUserSetting(shouldThrowIfNotFound, - userEmailKey, - email, - () => string.Format(CultureInfo.InvariantCulture, - "{0}@{1}", - Environment.UserName, - Environment.UserDomainName)); + var name = this.GetValueOrDefault("user.name"); + var email = this.GetValueOrDefault("user.email"); - return new Signature(normalizedName, normalizedEmail, now); - } - - private string NormalizeUserSetting(bool shouldThrowIfNotFound, string entryName, string currentValue, Func 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() diff --git a/LibGit2Sharp/NoteCollection.cs b/LibGit2Sharp/NoteCollection.cs index 7d8c756d2..c722221ce 100644 --- a/LibGit2Sharp/NoteCollection.cs +++ b/LibGit2Sharp/NoteCollection.cs @@ -173,7 +173,7 @@ internal static string UnCanonicalizeName(string name) /// The note which was just saved. 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); } @@ -212,7 +212,7 @@ public virtual Note Add(ObjectId targetId, string message, Signature author, Sig /// 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'). 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); } diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs index 9c6446670..f0c88ae23 100644 --- a/LibGit2Sharp/RepositoryExtensions.cs +++ b/LibGit2Sharp/RepositoryExtensions.cs @@ -236,7 +236,7 @@ public static Commit Commit(this IRepository repository, string message) /// The generated . 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); } @@ -270,7 +270,7 @@ public static Commit Commit(this IRepository repository, string message, Signatu /// The generated . 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); }