1
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
2
// Licensed under the MIT license.
3
3
using System ;
4
+ using System . Collections . Generic ;
4
5
using System . IO ;
5
6
using Microsoft . Git . CredentialManager . Interop . Native ;
6
7
using static Microsoft . Git . CredentialManager . Interop . Native . git_config_level_t ;
@@ -63,9 +64,8 @@ public string GetRepositoryPath(string path)
63
64
{
64
65
ThrowIfDisposed ( ) ;
65
66
66
- _trace . WriteLine ( $ "Discovering repository from path '{ path } '...") ;
67
-
68
67
var buf = new git_buf ( ) ;
68
+ _trace . WriteLine ( $ "Discovering repository from path '{ path } '...") ;
69
69
int error = git_repository_discover ( buf , path , true , null ) ;
70
70
71
71
try
@@ -91,6 +91,7 @@ public string GetRepositoryPath(string path)
91
91
92
92
protected override void ReleaseUnmanagedResources ( )
93
93
{
94
+ _trace . WriteLine ( "Shutting-down libgit2..." ) ;
94
95
git_libgit2_shutdown ( ) ;
95
96
base . ReleaseUnmanagedResources ( ) ;
96
97
}
@@ -114,6 +115,8 @@ internal unsafe LibGit2Configuration(ITrace trace, git_config* config)
114
115
_snapshot = snapshot ;
115
116
}
116
117
118
+ #region IGitConfiguration
119
+
117
120
public unsafe void Enumerate ( GitConfigurationEnumerationCallback cb )
118
121
{
119
122
ThrowIfDisposed ( ) ;
@@ -143,6 +146,47 @@ int native_cb(git_config_entry entry, void* payload)
143
146
}
144
147
}
145
148
149
+ public unsafe IGitConfiguration GetFilteredConfiguration ( GitConfigurationLevel level )
150
+ {
151
+ git_config * filteredConfig ;
152
+
153
+ _trace . WriteLine ( $ "Filtering default configuration set to '{ level . ToString ( ) } ' level...") ;
154
+
155
+ // Filter to the requested level
156
+ switch ( level )
157
+ {
158
+ case GitConfigurationLevel . ProgramData :
159
+ ThrowIfError ( git_config_open_level ( & filteredConfig , _config , GIT_CONFIG_LEVEL_PROGRAMDATA ) ,
160
+ nameof ( git_config_open_default ) ) ;
161
+ break ;
162
+
163
+ case GitConfigurationLevel . System :
164
+ ThrowIfError ( git_config_open_level ( & filteredConfig , _config , GIT_CONFIG_LEVEL_SYSTEM ) ,
165
+ nameof ( git_config_open_default ) ) ;
166
+ break ;
167
+
168
+ case GitConfigurationLevel . Xdg :
169
+ ThrowIfError ( git_config_open_level ( & filteredConfig , _config , GIT_CONFIG_LEVEL_XDG ) ,
170
+ nameof ( git_config_open_default ) ) ;
171
+ break ;
172
+
173
+ case GitConfigurationLevel . Global :
174
+ ThrowIfError ( git_config_open_level ( & filteredConfig , _config , GIT_CONFIG_LEVEL_GLOBAL ) ,
175
+ nameof ( git_config_open_default ) ) ;
176
+ break ;
177
+
178
+ case GitConfigurationLevel . Local :
179
+ ThrowIfError ( git_config_open_level ( & filteredConfig , _config , GIT_CONFIG_LEVEL_LOCAL ) ,
180
+ nameof ( git_config_open_default ) ) ;
181
+ break ;
182
+
183
+ default :
184
+ throw new ArgumentOutOfRangeException ( nameof ( level ) , level , null ) ;
185
+ }
186
+
187
+ return new LibGit2Configuration ( _trace , filteredConfig ) ;
188
+ }
189
+
146
190
public unsafe bool TryGetValue ( string name , out string value )
147
191
{
148
192
ThrowIfDisposed ( ) ;
@@ -167,6 +211,84 @@ public unsafe bool TryGetValue(string name, out string value)
167
211
return false ;
168
212
}
169
213
214
+ public unsafe void SetValue ( string name , string value )
215
+ {
216
+ _trace . WriteLine ( $ "Setting Git configuration entry '{ name } ' to '{ value } '...") ;
217
+ ThrowIfError ( git_config_set_string ( _config , name , value ) , nameof ( git_config_set_string ) ) ;
218
+ }
219
+
220
+ public unsafe void DeleteEntry ( string name )
221
+ {
222
+ _trace . WriteLine ( $ "Deleting Git configuration entry '{ name } '...") ;
223
+
224
+ int result = git_config_delete_entry ( _config , name ) ;
225
+ switch ( result )
226
+ {
227
+ case GIT_ENOTFOUND :
228
+ // Do nothing if asked to delete non-existent key
229
+ break ;
230
+
231
+ default :
232
+ ThrowIfError ( result , nameof ( git_config_delete_entry ) ) ;
233
+ break ;
234
+ }
235
+ }
236
+
237
+ public unsafe IEnumerable < string > GetMultivarValue ( string name , string regexp )
238
+ {
239
+ _trace . WriteLine ( $ "Reading Git configuration multivar '{ name } ' (regexp: '{ regexp } ')...") ;
240
+
241
+ var values = new List < string > ( ) ;
242
+
243
+ int value_callback ( git_config_entry entry , void * payload )
244
+ {
245
+ string value = entry . GetValue ( ) ;
246
+ _trace . WriteLine ( $ "Found multivar value '{ value } '.") ;
247
+ values . Add ( value ) ;
248
+ return 0 ;
249
+ }
250
+
251
+ int result = git_config_get_multivar_foreach ( _config , name , regexp , value_callback , ( void * ) IntPtr . Zero ) ;
252
+ switch ( result )
253
+ {
254
+ case GIT_ENOTFOUND :
255
+ // Do nothing if asked to enumerate non-existent multivar key
256
+ _trace . WriteLine ( "No entry found." ) ;
257
+ break ;
258
+
259
+ default :
260
+ ThrowIfError ( result , nameof ( git_config_get_multivar_foreach ) ) ;
261
+ break ;
262
+ }
263
+
264
+ return values ;
265
+ }
266
+
267
+ public unsafe void SetMultivarValue ( string name , string regexp , string value )
268
+ {
269
+ _trace . WriteLine ( $ "Setting Git configuration multivar '{ name } ' (regexp: '{ regexp } ') to '{ value } '...") ;
270
+ ThrowIfError ( git_config_set_multivar ( _config , name , regexp , value ) , nameof ( git_config_set_multivar ) ) ;
271
+ }
272
+
273
+ public unsafe void DeleteMultivarEntry ( string name , string regexp )
274
+ {
275
+ _trace . WriteLine ( $ "Deleting Git configuration multivar '{ name } ' (regexp: '{ regexp } ')...") ;
276
+
277
+ int result = git_config_delete_multivar ( _config , name , regexp ) ;
278
+ switch ( result )
279
+ {
280
+ case GIT_ENOTFOUND :
281
+ // Do nothing if asked to delete non-existent key
282
+ break ;
283
+
284
+ default :
285
+ ThrowIfError ( result , nameof ( git_config_delete_multivar ) ) ;
286
+ break ;
287
+ }
288
+ }
289
+
290
+ #endregion
291
+
170
292
protected override void ReleaseUnmanagedResources ( )
171
293
{
172
294
unsafe
0 commit comments