8000 Add XDG configuration store · Saaman/libgit2sharp@b7d44cf · GitHub
[go: up one dir, main page]

Skip to content

Commit b7d44cf

Browse files
yorahnulltoken
authored andcommitted
Add XDG configuration store
1 parent bbf0b7f commit b7d44cf

File tree

7 files changed

+44
-4
lines changed

7 files changed

+44
-4
lines changed

LibGit2Sharp.Tests/ConfigurationFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ public void CanGetAnEntryFromASpecificStore()
345345
repo.Config.Set("Woot.this-rocks", "local");
346346

347347
Assert.Equal("global", repo.Config.Get<string>("Woot.this-rocks", ConfigurationLevel.Global).Value);
348+
Assert.Equal("xdg", repo.Config.Get<string>("Woot.this-rocks", ConfigurationLevel.XDG).Value);
348349
Assert.Equal("system", repo.Config.Get<string>("Woot.this-rocks", ConfigurationLevel.System).Value);
349350
Assert.Equal("local", repo.Config.Get<string>("Woot.this-rocks", ConfigurationLevel.Local).Value);
350351
}
@@ -381,6 +382,11 @@ private RepositoryOptions BuildFakeConfigs(SelfCleaningDirectory scd)
381382
.AppendFormat("this-rocks = system{0}", Environment.NewLine);
382383
File.WriteAllText(options.SystemConfigurationLocation, sb.ToString());
383384

385+
sb = new StringBuilder()
386+
.AppendFormat("[Woot]{0}", Environment.NewLine)
387+
.AppendFormat("this-rocks = xdg{0}", Environment.NewLine);
388+
File.WriteAllText(options.XDGConfigurationLocation, sb.ToString());
389+
384390
return options;
385391
}
386392

@@ -390,11 +396,13 @@ private RepositoryOptions BuildFakeRepositoryOptions(SelfCleaningDirectory scd)
390396
Directory.CreateDirectory(confs);
391397

392398
string globalLocation = Path.Combine(confs, "my-global-config");
399+
string xdgLocation = Path.Combine(confs, "my-xdg-config");
393400
string systemLocation = Path.Combine(confs, "my-system-config");
394401

395402
return new RepositoryOptions
396403
{
397404
GlobalConfigurationLocation = globalLocation,
405+
XDGConfigurationLocation = xdgLocation,
398406
SystemConfigurationLocation = systemLocation,
399407
};
400408
}

LibGit2Sharp.Tests/RepositoryOptionsFixture.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ private string MeanwhileInAnotherDimensionAnEvilMastermindIsAtWork(string workin
146146
public void CanProvideDifferentConfigurationFilesToARepository()
147147
{
148148
string globalLocation = Path.Combine(newWorkdir, "my-global-config");
149+
string xdgLocation = Path.Combine(newWorkdir, "my-xdg-config");
149150
string systemLocation = Path.Combine(newWorkdir, "my-system-config");
150151

151152
const string name = "Adam 'aroben' Roben";
@@ -158,9 +159,11 @@ public void CanProvideDifferentConfigurationFilesToARepository()
158159

159160
File.WriteAllText(globalLocation, sb.ToString());
160161
File.WriteAllText(systemLocation, string.Empty);
162+
File.WriteAllText(xdgLocation, string.Empty);
161163

162164
var options = new RepositoryOptions {
163165
GlobalConfigurationLocation = globalLocation,
166+
XDGConfigurationLocation = xdgLocation,
164167
SystemConfigurationLocation = systemLocation,
165168
};
166169

@@ -170,6 +173,7 @@ public void CanProvideDifferentConfigurationFilesToARepository()
170173
Assert.Equal(name, repo.Config.Get<string>("user.name").Value);
171174
Assert.Equal(email, repo.Config.Get<string>("user.email").Value);
172175

176+
repo.Config.Set("xdg.setting", "https://twitter.com/libgit2sharp", ConfigurationLevel.XDG);
173177
repo.Config.Set("help.link", "https://twitter.com/xpaulbettsx/status/205761932626636800", ConfigurationLevel.System);
174178
}
175179

LibGit2Sharp/Configuration.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class Configuration : IDisposable,
1717
IEnumerable<ConfigurationEntry<string>>
1818
{
1919
private readonly FilePath globalConfigPath;
20+
private readonly FilePath xdgConfigPath;
2021
private readonly FilePath systemConfigPath;
2122

2223
private readonly Repository repository;
@@ -29,11 +30,13 @@ public class Configuration : IDisposable,
2930
protected Configuration()
3031
{ }
3132

32-
internal Configuration(Repository repository, string globalConfigurationFileLocation, string systemConfigurationFileLocation)
33+
internal Configuration(Repository repository, string globalConfigurationFileLocation,
34+
string xdgConfigurationFileLocation, string systemConfigurationFileLocation)
3335
{
3436
this.repository = repository;
3537

3638
globalConfigPath = globalConfigurationFileLocation ?? Proxy.git_config_find_global();
39+
xdgConfigPath = xdgConfigurationFileLocation ?? Proxy.git_config_find_xdg();
3740
systemConfigPath = systemConfigurationFileLocation ?? Proxy.git_config_find_system();
3841

3942
Init();
@@ -60,6 +63,11 @@ private void Init()
6063
Proxy.git_config_add_file_ondisk(configHandle, globalConfigPath, ConfigurationLevel.Global);
6164
}
6265

66+
if (xdgConfigPath != null)
67+
{
68+
Proxy.git_config_add_file_ondisk(configHandle, xdgConfigPath, ConfigurationLevel.XDG);
69+
}
70+
6371
if (systemConfigPath != null)
6472
{
6573
Proxy.git_config_add_file_ondisk(configHandle, systemConfigPath, ConfigurationLevel.System);
@@ -70,9 +78,10 @@ private void Init()
7078
/// Access configuration values without a repository. Generally you want to access configuration via an instance of <see cref = "Repository" /> instead.
7179
/// </summary>
7280
/// <param name="globalConfigurationFileLocation">Path to a Global configuration file. If null, the default path for a global configuration file will be probed.</param>
81+
/// <param name="xdgConfigurationFileLocation">Path to a XDG configuration file. If null, the default path for a XDG configuration file will be probed.</param>
7382
/// <param name="systemConfigurationFileLocation">Path to a System configuration file. If null, the default path for a system configuration file will be probed.</param>
74-
public Configuration(string globalConfigurationFileLocation = null, string systemConfigurationFileLocation = null)
75-
: this(null, globalConfigurationFileLocation, systemConfigurationFileLocation)
83+
public Configuration(string globalConfigurationFileLocation = null, string xdgConfigurationFileLocation = null, string systemConfigurationFileLocation = null)
84+
: this(null, globalConfigurationFileLocation, xdgConfigurationFileLocation, systemConfigurationFileLocation)
7685
{
7786
}
7887

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ internal static extern int git_commit_create(
228228
[DllImport(libgit2)]
229229
internal static extern int git_config_find_system(byte[] system_config_path, UIntPtr length);
230230

231+
[DllImport(libgit2)]
232+
internal static extern int git_config_find_xdg(byte[] xdg_config_path, UIntPtr length);
233+
231234
[DllImport(libgit2)]
232235
internal static extern void git_config_free(IntPtr cfg);
233236

LibGit2Sharp/Core/Proxy.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,11 @@ public static string git_config_find_system()
353353
return ConvertPath(NativeMethods.git_config_find_system);
354354
}
355355

356+
public static string git_config_find_xdg()
357+
{
358+
return ConvertPath(NativeMethods.git_config_find_xdg);
359+
}
360+
356361
public static void git_config_free(IntPtr config)
357362
{
358363
NativeMethods.git_config_free(config);

LibGit2Sharp/Repository.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public Repository(string path, RepositoryOptions options = null)
5757
Func<Index> indexBuilder = () => new Index(this);
5858

5959
string configurationGlobalFilePath = null;
60+
string configurationXDGFilePath = null;
6061
string configurationSystemFilePath = null;
6162

6263
if (options != null)
@@ -82,6 +83,7 @@ public Repository(string path, RepositoryOptions options = null)
8283
}
8384

8485
configurationGlobalFilePath = options.GlobalConfigurationLocation;
86+
configurationXDGFilePath = options.XDGConfigurationLocation;
8587
configurationSystemFilePath = options.SystemConfigurationLocation;
8688
}
8789

@@ -95,7 +97,7 @@ public Repository(string path, RepositoryOptions options = null)
9597
branches = new BranchCollection(this);
9698
tags = new TagCollection(this);
9799
info = new Lazy<RepositoryInformation>(() => new RepositoryInformation(this, isBare));
98-
config = new Lazy<Configuration>(() => RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationSystemFilePath)));
100+
config = new Lazy<Configuration>(() => RegisterForCleanup(new Configuration(this, configurationGlobalFilePath, configurationXDGFilePath, configurationSystemFilePath)));
99101
remotes = new Lazy<RemoteCollection>(() => new RemoteCollection(this));
100102
odb = new Lazy<ObjectDatabase>(() => new ObjectDatabase(this));
101103
diff = new Diff(this);

LibGit2Sharp/RepositoryOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ public class RepositoryOptions
3535
/// </summary>
3636
public string GlobalConfigurationLocation { get; set; }
3737

38+
/// <summary>
39+
/// Overrides the probed location of the XDG configuration file of a repository.
40+
/// <para>
41+
/// The path has either to lead to an existing valid configuration file,
42+
/// or to a non existent configuration file which will be eventually created.
43+
/// </para>
44+
/// </summary>
45+
public string XDGConfigurationLocation { get; set; }
46+
3847
/// <summary>
3948
/// Overrides the probed location of the System configuration file of a repository.
4049
/// <para>

0 commit comments

Comments
 (0)
0