@@ -743,12 +743,8 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
743743 }
744744
745745 /// <summary>
746- /// Builds a <see cref="Signature"/> based on current configuration.
747- /// <para>
748- /// Name is populated from the user.name setting, and is "unknown" if unspecified.
749- /// Email is populated from the user.email setting, and is built from
750- /// <see cref="Environment.UserName"/> and <see cref="Environment.UserDomainName"/> if unspecified.
751- /// </para>
746+ /// Builds a <see cref="Signature"/> based on current configuration. If it is not found or
747+ /// some configuration is missing, <code>null</code> is returned.
752748 /// <para>
753749 /// The same escalation logic than in git.git will be used when looking for the key in the config files:
754750 /// - local: the Git file in the current repository
@@ -758,51 +754,30 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
758754 /// </para>
759755 /// </summary>
760756 /// <param name="now">The timestamp to use for the <see cref="Signature"/>.</param>
761- /// <returns>The signature.</returns>
757+ /// <returns>The signature or null if no user identity can be found in the configuration .</returns>
762758 public virtual Signature BuildSignature ( DateTimeOffset now )
763759 {
764- return BuildSignature ( now , false ) ;
765- }
766-
767- internal Signature BuildSignature ( DateTimeOffset now , bool shouldThrowIfNotFound )
768- {
769- const string userNameKey = "user.name" ;
770- var name = this . GetValueOrDefault < string > ( userNameKey ) ;
771- var normalizedName = NormalizeUserSetting ( shouldThrowIfNotFound ,
772- userNameKey ,
773- name ,
774- ( ) => "unknown" ) ;
775-
776- const string userEmailKey = "user.email" ;
777- var email = this . GetValueOrDefault < string > ( userEmailKey ) ;
778- var normalizedEmail = NormalizeUserSetting ( shouldThrowIfNotFound ,
779- userEmailKey ,
780- email ,
781- ( ) => string . Format ( CultureInfo . InvariantCulture ,
782- "{0}@{1}" ,
783- Environment . UserName ,
784- Environment . UserDomainName ) ) ;
760+ var name = this . GetValueOrDefault < string > ( "user.name" ) ;
761+ var email = this . GetValueOrDefault < string > ( "user.email" ) ;
785762
786- return new Signature ( normalizedName , normalizedEmail , now ) ;
787- }
788-
789- private string NormalizeUserSetting ( bool shouldThrowIfNotFound , string entryName , string currentValue , Func < string > defaultValue )
790- {
791- if ( ! string . IsNullOrEmpty ( currentValue ) )
763+ if ( string . IsNullOrEmpty ( name ) || string . IsNullOrEmpty ( email ) )
792764 {
793- return currentValue ;
765+ return null ;
794766 }
795767
796- string message = string . Format ( "Configuration value '{0}' is missing or invalid." , entryName ) ;
768+ return new Signature ( name , email , now ) ;
769+ }
797770
798- if ( shouldThrowIfNotFound )
771+ internal Signature BuildSignatureOrThrow ( DateTimeOffset now )
772+ {
773+ var signature = BuildSignature ( now ) ;
774+ if ( signature == null )
799775 {
800- throw new LibGit2SharpException ( message ) ;
776+ throw new LibGit2SharpException ( "This overload requires 'user.name' and 'user.email' to be set. " +
777+ "Use a different overload or set those variables in the configuation" ) ;
801778 }
802779
803- Log . Write ( LogLevel . Warning , message ) ;
804-
805- return defaultValue ( ) ;
780+ return signature ;
806781 }
807782
808783 private ConfigurationSafeHandle Snapshot ( )
0 commit comments