@@ -743,12 +743,8 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
743
743
}
744
744
745
745
/// <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.
752
748
/// <para>
753
749
/// The same escalation logic than in git.git will be used when looking for the key in the config files:
754
750
/// - local: the Git file in the current repository
@@ -758,51 +754,30 @@ private static ConfigurationEntry<string> BuildConfigEntry(IntPtr entryPtr)
758
754
/// </para>
759
755
/// </summary>
760
756
/// <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>
762
758
public virtual Signature BuildSignature ( DateTimeOffset now )
763
759
{
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" ) ;
785
762
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 ) )
792
764
{
793
- return currentValue ;
765
+ return null ;
794
766
}
795
767
796
- string message = string . Format ( "Configuration value '{0}' is missing or invalid." , entryName ) ;
768
+ return new Signature ( name , email , now ) ;
769
+ }
797
770
798
- if ( shouldThrowIfNotFound )
771
+ internal Signature BuildSignatureOrThrow ( DateTimeOffset now )
772
+ {
773
+ var signature = BuildSignature ( now ) ;
774
+ if ( signature == null )
799
775
{
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" ) ;
801
778
}
802
779
803
- Log . Write ( LogLevel . Warning , message ) ;
804
-
805
- return defaultValue ( ) ;
780
+ return signature ;
806
781
}
807
782
808
783
private ConfigurationSafeHandle Snapshot ( )
0 commit comments