@@ -90,27 +90,27 @@ public Configuration(string globalConfigurationFileLocation = null, string syste
90
90
}
91
91
92
92
/// <summary>
93
- /// Determines if there is a local repository level Git configuration file .
93
+ /// Determines if a Git configuration file specific to the current interactive user has been found .
94
94
/// </summary>
95
- private bool HasLocalConfig
95
+ public virtual bool HasGlobalConfig
96
96
{
97
- get { return localHandle != null ; }
97
+ get { return HasConfig ( ConfigurationLevel . Global ) ; }
98
98
}
99
99
100
100
/// <summary>
101
- /// Determines if a Git configuration file specific to the current interactive user has been found.
101
+ /// Determines if a system-wide Git configuration file has been found.
102
102
/// </summary>
103
- public virtual bool HasGlobalConfig
103
+ public virtual bool HasSystemConfig
104
104
{
105
- get { return globalConfigPath != null ; }
105
+ get { return HasConfig ( ConfigurationLevel . System ) ; }
106
106
}
107
107
108
108
/// <summary>
109
- /// Determines if a system-wide Git configuration file has been found.
109
+ /// Determines which configuration file has been found.
110
110
/// </summary>
111
- public virtual bool HasSystemConfig
111
+ public virtual bool HasConfig ( ConfigurationLevel level )
112
112
{
113
- get { return systemConfigPath != null ; }
113
+ return RetrieveConfigurationHandle ( level , false ) != null ;
114
114
}
115
115
116
116
#region IDisposable Members
@@ -136,7 +136,7 @@ public virtual void Unset(string key, ConfigurationLevel level = ConfigurationLe
136
136
{
137
137
Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
138
138
139
- ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level ) ;
139
+ ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) ;
140
140
141
141
bool success = Proxy . git_config_delete ( h , key ) ;
142
142
@@ -412,7 +412,7 @@ public virtual void Set<T>(string key, T value, ConfigurationLevel level = Confi
412
412
{
413
413
Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
414
414
415
- ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level ) ;
415
+ ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) ;
416
416
417
417
if ( ! configurationTypedUpdater . ContainsKey ( typeof ( T ) ) )
418
418
{
@@ -423,42 +423,23 @@ public virtual void Set<T>(string key, T value, ConfigurationLevel level = Confi
423
423
Save ( ) ;
424
424
}
425
425
426
- private ConfigurationSafeHandle RetrieveConfigurationHandle ( ConfigurationLevel level )
426
+ private ConfigurationSafeHandle RetrieveConfigurationHandle ( ConfigurationLevel level , bool throwIfStoreHasNotBeenFound )
427
427
{
428
- if ( level == ConfigurationLevel . Local && ! HasLocalConfig )
428
+ Func < Configuration , ConfigurationSafeHandle > handleRetriever ;
429
+ if ( ! configurationHandleRetriever . TryGetValue ( level , out handleRetriever ) )
429
430
{
430
- throw new LibGit2SharpException ( "No local configuration file has been found. You must use ConfigurationLevel.Global when accessing configuration outside of repository." ) ;
431
+ throw new ArgumentException (
432
+ string . Format ( CultureInfo . InvariantCulture , "Configuration level has an unexpected value ('{0}')." ,
433
+ level ) , "level" ) ;
431
434
}
432
435
433
- if ( level == ConfigurationLevel . Global && ! HasGlobalConfig )
434
- {
435
- throw new LibGit2SharpException ( "No global configuration file has been found." ) ;
436
- }
436
+ ConfigurationSafeHandle h = handleRetriever ( this ) ;
437
437
438
- if ( level == ConfigurationLevel . System && ! HasSystemConfig )
438
+ if ( h == null && throwIfStoreHasNotBeenFound )
439
439
{
440
- throw new LibGit2SharpException ( "No system configuration file has been found." ) ;
440
+ throw new LibGit2SharpException ( "No matching configuration file has been found." ) ;
441
441
}
442
442
443
- ConfigurationSafeHandle h ;
444
-
445
- switch ( level )
446
- {
447
- case ConfigurationLevel . Local :
448
- h = localHandle ;
449
- break ;
450
-
451
- case ConfigurationLevel . Global :
452
- h = globalHandle ;
453
- break ;
454
-
455
- case ConfigurationLevel . System :
456
- h = systemHandle ;
457
- break ;
458
-
459
- default :
460
- throw new ArgumentException ( string . Format ( CultureInfo . InvariantCulture , "Configuration level has an unexpected value ('{0}')." , level ) , "level" ) ;
461
- }
462
443
return h ;
463
444
}
464
445
@@ -467,14 +448,21 @@ private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Act
467
448
return ( key , val , handle ) => setter ( handle , key , ( T ) val ) ;
468
449
}
469
450
470
- private readonly IDictionary < Type , Action < string , object , ConfigurationSafeHandle > > configurationTypedUpdater = new Dictionary < Type , Action < string , object , ConfigurationSafeHandle > >
451
+ private readonly static IDictionary < Type , Action < string , object , ConfigurationSafeHandle > > configurationTypedUpdater = new Dictionary < Type , Action < string , object , ConfigurationSafeHandle > >
471
452
{
472
453
{ typeof ( int ) , GetUpdater < int > ( Proxy . git_config_set_int32 ) } ,
473
454
{ typeof ( long ) , GetUpdater < long > ( Proxy . git_config_set_int64 ) } ,
474
455
{ typeof ( bool ) , GetUpdater < bool > ( Proxy . git_config_set_bool ) } ,
475
456
{ typeof ( string ) , GetUpdater < string > ( Proxy . git_config_set_string ) } ,
476
457
} ;
477
458
459
+ private readonly static IDictionary < ConfigurationLevel , Func < Configuration , ConfigurationSafeHandle > > configurationHandleRetriever = new Dictionary < ConfigurationLevel , Func < Configuration , ConfigurationSafeHandle > >
460
+ {
461
+ { ConfigurationLevel . Local , cfg => cfg . localHandle } ,
462
+ { ConfigurationLevel . Global , cfg => cfg . globalHandle } ,
463
+ { ConfigurationLevel . System , cfg => cfg . systemHandle } ,
464
+ } ;
465
+
478
466
IEnumerator < ConfigurationEntry < string > > IEnumerable < ConfigurationEntry < String > > . GetEnumerator ( )
479
467
{
480
468
return BuildConfigEntries ( ) . Cast < ConfigurationEntry < string > > ( ) . GetEnumerator ( ) ;
0 commit comments