Skip to content

Commit 1e86842

Browse files
committed
Enumerate configuration values.
1 parent f0d7a61 commit 1e86842

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

LibGit2Sharp.Tests/ConfigurationFixture.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34
using System.Text;
45
using LibGit2Sharp.Tests.TestHelpers;
56
using Xunit;
@@ -157,6 +158,29 @@ public void CanReadStringValue()
157158
}
158159
}
159160

161+
[SkippableFact]
162+
public void CanEnumerateGlobalConfig()
163+
{
164+
using (var repo = new Repository(StandardTestRepoPath))
165+
{
166+
InconclusiveIf(() => !repo.Config.HasGlobalConfig, "No Git global configuration available");
167+
var entry = repo.Config.FirstOrDefault(e => e.Key == "user.name");
168+
Assert.NotNull(entry);
169+
Assert.NotNull(entry.Value);
170+
}
171+
}
172+
173+
[Fact]
174+
public void CanEnumerateLocalConfig()
175+
{
176+
using (var repo = new Repository(StandardTestRepoPath))
177+
{
178+
var entry = repo.Config.FirstOrDefault(e => e.Key == "core.ignorecase");
179+
Assert.NotNull(entry);
180+
Assert.Equal("true", entry.Value);
181+
}
182+
}
183+
160184
[Fact]
161185
public void CanSetBooleanValue()
162186
{

LibGit2Sharp/Configuration.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LibGit2Sharp
1010
/// <summary>
1111
/// Provides access to configuration variables for a repository.
1212
/// </summary>
13-
public class Configuration : IDisposable
13+
public class Configuration : IDisposable, IEnumerable<ConfigurationEntry>
1414
{
1515
private readonly FilePath globalConfigPath;
1616
private readonly FilePath systemConfigPath;
@@ -406,5 +406,22 @@ private static Action<string, object, ConfigurationSafeHandle> GetUpdater<T>(Act
406406
{ typeof(bool), GetUpdater<bool>(Proxy.git_config_set_bool) },
407407
{ typeof(string), GetUpdater<string>(Proxy.git_config_set_string) },
408408
};
409+
410+
IEnumerator<ConfigurationEntry> IEnumerable<ConfigurationEntry>.GetEnumerator()
411+
{
412+
var values = new List<ConfigurationEntry>();
413+
Proxy.git_config_foreach(LocalHandle, (namePtr, valuePtr, _) => {
414+
var name = Utf8Marshaler.FromNative(namePtr);
415+
var value = Utf8Marshaler.FromNative(valuePtr);
416+
values.Add(new ConfigurationEntry(name, value));
417+
return 0;
418+
});
419+
return values.GetEnumerator();
420+
}
421+
422+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
423+
{
424+
return ((IEnumerable<ConfigurationEntry>)this).GetEnumerator();
425+
}
409426
}
410427
}

LibGit2Sharp/ConfigurationEntry.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace LibGit2Sharp
2+
{
3+
/// <summary>
4+
/// An enumerated configuration entry.
5+
/// </summary>
6+
public class ConfigurationEntry
7+
{
8+
/// <summary>
9+
/// The option name.
10+
/// </summary>
11+
public string Key { get; private set; }
12+
13+
/// <summary>
14+
/// The option value.
15+
/// </summary>
16+
public string Value { get; private set; }
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="ConfigurationEntry"/> class with a given key and value
20+
/// </summary>
21+
/// <param name="key">The option name</param>
22+
/// <param name="value">The option value, as a string</param>
23+
public ConfigurationEntry(string key, string value)
24+
{
25+
Key = key;
26+
Value = value;
27+
}
28+
}
29+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,17 @@ internal static extern int git_config_set_string(
242242
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
243243
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string value);
244244

245+
internal delegate int config_foreach_callback(
246+
IntPtr var_name,
247+
IntPtr value,
248+
IntPtr payload);
249+
250+
[DllImport(libgit2)]
251+
internal static extern int git_config_foreach(
252+
ConfigurationSafeHandle cfg,
253+
config_foreach_callback callback,
254+
IntPtr payload);
255+
245256
[DllImport(libgit2)]
246257
internal static extern void git_diff_list_free(IntPtr diff);
247258

LibGit2Sharp/Core/Proxy.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,17 @@ public static void git_config_set_string(ConfigurationSafeHandle config, string
379379
}
380380
}
381381

382+
public static void git_config_foreach(
383+
ConfigurationSafeHandle config,
384+
NativeMethods.config_foreach_callback callback)
385+
{
386+
using (ThreadAffinity())
387+
{
388+
int res = NativeMethods.git_config_foreach(config, callback, IntPtr.Zero);
389+
Ensure.Success(res);
390+
}
391+
}
392+
382393
#endregion
383394

384395
#region git_diff_

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<Compile Include="Commit.cs" />
6666
<Compile Include="CommitLog.cs" />
6767
<Compile Include="Configuration.cs" />
68+
<Compile Include="ConfigurationEntry.cs" />
6869
<Compile Include="ContentChanges.cs" />
6970
<Compile Include="Core\Proxy.cs" />
7071
<Compile Include="TagCollectionExtensions.cs" />

0 commit comments

Comments
 (0)