@@ -21,9 +21,7 @@ public class Configuration : IDisposable,
21
21
22
22
private readonly Repository repository ;
23
23
24
- private ConfigurationSafeHandle systemHandle ;
25
- private ConfigurationSafeHandle globalHandle ;
26
- private ConfigurationSafeHandle localHandle ;
24
+ private ConfigurationSafeHandle configHandle ;
27
25
28
26
/// <summary>
29
27
/// Needed for mocking purposes.
@@ -43,39 +41,28 @@ internal Configuration(Repository repository, string globalConfigurationFileLoca
43
41
44
42
private void Init ( )
45
43
{
44
+ configHandle = Proxy . git_config_new ( ) ;
45
+
46
46
if ( repository != null )
47
47
{
48
48
//TODO: push back this logic into libgit2.
49
49
// As stated by @carlosmn "having a helper function to load the defaults and then allowing you
50
50
// to modify it before giving it to git_repository_open_ext() would be a good addition, I think."
51
51
// -- Agreed :)
52
-
53
- localHandle = Proxy . git_config_new ( ) ;
54
-
55
52
string repoConfigLocation = Path . Combine ( repository . Info . Path , "config" ) ;
56
- Proxy . git_config_add_file_ondisk ( localHandle , repoConfigLocation , ( uint ) ConfigurationLevel . Local ) ;
53
+ Proxy . git_config_add_file_ondisk ( configHandle , repoConfigLocation , ConfigurationLevel . Local ) ;
57
54
58
- if ( globalConfigPath != null )
59
- {
60
- Proxy . git_config_add_file_ondisk ( localHandle , globalConfigPath , ( uint ) ConfigurationLevel . Global ) ;
61
- }
62
-
63
- if ( systemConfigPath != null )
64
- {
65
- Proxy . git_config_add_file_ondisk ( localHandle , systemConfigPath , ( uint ) ConfigurationLevel . System ) ;
66
- }
67
-
68
- Proxy . git_repository_set_config ( repository . Handle , localHandle ) ;
55
+ Proxy . git_repository_set_config ( repository . Handle , configHandle ) ;
69
56
}
70
57
71
58
if ( globalConfigPath != null )
72
59
{
73
- globalHandle = Proxy . git_config_open_ondisk ( globalConfigPath ) ;
60
+ Proxy . git_config_add_file_ondisk ( configHandle , globalConfigPath , ConfigurationLevel . Global ) ;
74
61
}
75
62
76
63
if ( systemConfigPath != null )
77
64
{
78
- systemHandle = Proxy . git_config_open_ondisk ( systemConfigPath ) ;
65
+ Proxy . git_config_add_file_ondisk ( configHandle , systemConfigPath , ConfigurationLevel . System ) ;
79
66
}
80
67
}
81
68
@@ -112,7 +99,10 @@ public virtual bool HasSystemConfig
112
99
/// </summary>
113
100
public virtual bool HasConfig ( ConfigurationLevel level )
114
101
{
115
- return RetrieveConfigurationHandle ( level , false ) != null ;
102
+ using ( ConfigurationSafeHandle handle = RetrieveConfigurationHandle ( level , false ) )
103
+ {
104
+ return handle != null ;
105
+ }
116
106
}
117
107
118
108
#region IDisposable Members
@@ -138,13 +128,9 @@ public virtual void Unset(string key, ConfigurationLevel level = ConfigurationLe
138
128
{
139
129
Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
140
130
141
- ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) ;
142
-
143
- bool success = Proxy . git_config_delete ( h , key ) ;
144
-
145
- if ( success )
131
+ using ( ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) )
146
132
{
147
- Save ( ) ;
133
+ Proxy . git_config_delete ( h , key ) ;
148
134
}
149
135
}
150
136
@@ -153,9 +139,7 @@ public virtual void Unset(string key, ConfigurationLevel level = ConfigurationLe
153
139
/// </summary>
154
140
protected virtual void Dispose ( bool disposing )
155
141
{
156
- localHandle . SafeDispose ( ) ;
157
- globalHandle . SafeDispose ( ) ;
158
- systemHandle . SafeDispose ( ) ;
142
+ configHandle . SafeDispose ( ) ;
159
143
}
160
144
161
145
/// <summary>
@@ -182,20 +166,43 @@ public ConfigurationEntry<T> Get<T>(string key)
182
166
{
183
167
Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
184
168
185
- ConfigurationSafeHandle handle = ( localHandle ?? globalHandle ) ?? systemHandle ;
186
-
187
- if ( handle == null )
188
- {
189
- throw new LibGit2SharpException ( "Could not find a local, global or system level configuration." ) ;
190
- }
191
-
192
- return Proxy . git_config_get_entry < T > ( handle , key ) ;
169
+ return Proxy . git_config_get_entry < T > ( configHandle , key ) ;
193
170
}
194
171
195
- private void Save ( )
172
+ /// <summary>
173
+ /// Get a configuration value for a key. Keys are in the form 'section.name'.
174
+ /// <para>
175
+ /// For example in order to get the value for this in a .git\config file:
176
+ ///
177
+ /// <code>
178
+ /// [core]
179
+ /// bare = true
180
+ /// </code>
181
+ ///
182
+ /// You would call:
183
+ ///
184
+ /// <code>
185
+ /// bool isBare = repo.Config.Get<bool>("core.bare").Value;
186
+ /// </code>
187
+ /// </para>
188
+ /// </summary>
189
+ /// <typeparam name = "T">The configuration value type</typeparam>
190
+ /// <param name = "key">The key</param>
191
+ /// <param name = "level">The configuration file into which the key should be searched for</param>
192
+ /// <returns>The <see cref="ConfigurationEntry{T}"/>, or null if not set</returns>
193
+ public ConfigurationEntry < T > Get < T > ( string key , ConfigurationLevel level )
196
194
{
197
- Dispose ( true ) ;
198
- Init ( ) ;
195
+ Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
196
+
197
+ using ( ConfigurationSafeHandle handle = RetrieveConfigurationHandle ( level , false ) )
198
+ {
199
+ if ( handle == null )
200
+ {
201
+ return null ;
202
+ }
203
+
204
+ return Proxy . git_config_get_entry < T > ( handle , key ) ;
205
+ }
199
206
}
200
207
201
208
/// <summary>
@@ -219,35 +226,31 @@ public virtual void Set<T>(string key, T value, ConfigurationLevel level = Confi
219
226
{
220
227
Ensure . ArgumentNotNullOrEmptyString ( key , "key" ) ;
221
228
222
- ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) ;
223
-
224
- if ( ! configurationTypedUpdater . ContainsKey ( typeof ( T ) ) )
229
+ using ( ConfigurationSafeHandle h = RetrieveConfigurationHandle ( level , true ) )
225
230
{
226
- throw new ArgumentException ( string . Format ( CultureInfo . InvariantCulture , "Generic Argument of type '{0}' is not supported." , typeof ( T ) . FullName ) ) ;
227
- }
231
+ if ( ! configurationTypedUpdater . ContainsKey ( typeof ( T ) ) )
232
+ {
233
+ throw new ArgumentException ( string . Format ( CultureInfo . InvariantCulture , "Generic Argument of type '{0}' is not supported." , typeof ( T ) . FullName ) ) ;
234
+ }
228
235
229
- configurationTypedUpdater [ typeof ( T ) ] ( key , value , h ) ;
230
- Save ( ) ;
236
+ configurationTypedUpdater [ typeof ( T ) ] ( key , value , h ) ;
237
+ }
231
238
}
232
239
233
240
private ConfigurationSafeHandle RetrieveConfigurationHandle ( ConfigurationLevel level , bool throwIfStoreHasNotBeenFound )
234
241
{
235
- Func < Configuration , ConfigurationSafeHandle > handleRetriever ;
236
- if ( ! configurationHandleRetriever . TryGetValue ( level , out handleRetriever ) )
242
+ ConfigurationSafeHandle handle = null ;
243
+ if ( configHandle != null )
237
244
{
238
- throw new ArgumentException (
239
- string . Format ( CultureInfo . InvariantCulture , "Configuration level has an unexpected value ('{0}')." ,
240
- level ) , "level" ) ;
245
+ handle = Proxy . git_config_open_level ( configHandle , level ) ;
241
246
}
242
247
243
- ConfigurationSafeHandle h = handleRetriever ( this ) ;
244
-
245
- if ( h == null && throwIfStoreHasNotBeenFound )
248
+ if ( handle == null && throwIfStoreHasNotBeenFound )
246
249
{
247
250
throw new LibGit2SharpException ( "No matching configuration file has been found." ) ;
248
251
}
249
252
250
- return h ;
253
+ return handle ;
251
254
}
252
255
253
256
private static Action < string , object , ConfigurationSafeHandle > GetUpdater < T > ( Action < ConfigurationSafeHandle , string , T > setter )
@@ -263,13 +266,6 @@ private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Act
263
266
{ typeof ( string ) , GetUpdater < string > ( Proxy . git_config_set_string ) } ,
264
267
} ;
265
268
266
- private readonly static IDictionary < ConfigurationLevel , Func < Configuration , ConfigurationSafeHandle > > configurationHandleRetriever = new Dictionary < ConfigurationLevel , Func < Configuration , ConfigurationSafeHandle > >
267
- {
268
- { ConfigurationLevel . Local , cfg => cfg . localHandle } ,
269
- { ConfigurationLevel . Global , cfg => cfg . globalHandle } ,
270
- { ConfigurationLevel . System , cfg => cfg . systemHandle } ,
271
- } ;
272
-
273
269
IEnumerator < ConfigurationEntry < string > > IEnumerable < ConfigurationEntry < String > > . GetEnumerator ( )
274
270
{
275
271
return BuildConfigEntries ( ) . Cast < ConfigurationEntry < string > > ( ) . GetEnumerator ( ) ;
@@ -288,7 +284,7 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
288
284
289
285
private ICollection < ConfigurationEntry > BuildConfigEntries ( )
290
286
{
291
- return Proxy . git_config_foreach ( localHandle , entryPtr =>
287
+ return Proxy . git_config_foreach ( configHandle , entryPtr =>
292
288
{
293
289
var entry = ( GitConfigEntry ) Marshal . PtrToStructure ( entryPtr , typeof ( GitConfigEntry ) ) ;
294
290
0 commit comments