Skip to content

Commit

Permalink
Make Configuration.BuildFrom() able to probe for the local configurat…
Browse files Browse the repository at this point in the history
…ion file
  • Loading branch information
nulltoken committed Jun 6, 2015
1 parent 5d9f53d commit ca4d15e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 10 deletions.
28 changes: 25 additions & 3 deletions LibGit2Sharp.Tests/ConfigurationFixture.cs
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand Down Expand Up @@ -367,8 +369,21 @@ public void CanTellIfASpecificStoreContainsAKey()
}
}

[Fact]
public void CanAccessConfigurationWithoutARepository()
public static IEnumerable<object[]> ConfigAccessors
{
get
{
return new List<object[]>
{
new[] { new Func<string, string>(p => Path.Combine(p, ".git", "config")) },
new[] { new Func<string, string>(p => Path.Combine(p, ".git")) },
new[] { new Func<string, string>(p => p) },
};
}
}

[Theory, PropertyData("ConfigAccessors")]
public void CanAccessConfigurationWithoutARepository(Func<string, string> localConfigurationPathProvider)
{
var path = SandboxStandardTestRepoGitDir();

Expand All @@ -381,11 +396,18 @@ public void CanAccessConfigurationWithoutARepository()
repo.Config.Set("my.key", "mouse", ConfigurationLevel.Global);
}

using (var config = Configuration.BuildFrom(Path.Combine(path, ".git", "config"), globalConfigPath))
using (var config = Configuration.BuildFrom(localConfigurationPathProvider(path), globalConfigPath))
{
Assert.Equal("local", config.Get<string>("my.key").Value);
Assert.Equal("mouse", config.Get<string>("my.key", ConfigurationLevel.Global).Value);
}
}

[Fact]
public void PassingANonExistingLocalConfigurationFileToBuildFromthrowss()
{
Assert.Throws<FileNotFoundException>(() => Configuration.BuildFrom(
Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())));
}
}
}
4 changes: 2 additions & 2 deletions LibGit2Sharp.Tests/TestHelpers/BaseFixture.cs
Expand Up @@ -336,8 +336,8 @@ protected string CreateConfigurationWithDummyUser(Signature signature)
protected string CreateConfigurationWithDummyUser(string name, string email)
{
SelfCleaningDirectory scd = BuildSelfCleaningDirectory();
Directory.CreateDirectory(scd.DirectoryPath);
string configFilePath = Path.Combine(scd.DirectoryPath, "fake-config");

string configFilePath = Touch(scd.DirectoryPath, "fake-config");

using (Configuration config = Configuration.BuildFrom(configFilePath))
{
Expand Down
59 changes: 54 additions & 5 deletions LibGit2Sharp/Configuration.cs
Expand Up @@ -31,7 +31,11 @@ protected Configuration()
internal Configuration(Repository repository, string repositoryConfigurationFileLocation, string globalConfigurationFileLocation,
string xdgConfigurationFileLocation, string systemConfigurationFileLocation)
{
repoConfigPath = repositoryConfigurationFileLocation;
if (repositoryConfigurationFileLocation != null)
{
repoConfigPath = NormalizeConfigPath(repositoryConfigurationFileLocation);
}

globalConfigPath = globalConfigurationFileLocation ?? Proxy.git_config_find_global();
xdgConfigPath = xdgConfigurationFileLocation ?? Proxy.git_config_find_xdg();
systemConfigPath = systemConfigurationFileLocation ?? Proxy.git_config_find_system();
Expand Down Expand Up @@ -75,13 +79,46 @@ private void Init(Repository repository)
}
}

private FilePath NormalizeConfigPath(FilePath path)
{
if (File.Exists(path.Native))
{
return path;
}

if (!Directory.Exists(path.Native))
{
throw new FileNotFoundException("Cannot find repository configuration file", path.Native);
}

var configPath = Path.Combine(path.Native, "config");

if (File.Exists(configPath))
{
return configPath;
}

var gitConfigPath = Path.Combine(path.Native, ".git", "config");

if (File.Exists(gitConfigPath))
{
return gitConfigPath;
}

throw new FileNotFoundException("Cannot find repository configuration file", path.Native);
}

/// <summary>
/// Access configuration values without a repository.
/// <para>
/// Generally you want to access configuration via an instance of <see cref="Repository"/> instead.
/// </para>
/// <para>
/// <paramref name="repositoryConfigurationFileLocation"/> can either contains a path to a file or a directory. In the latter case,
/// this can be the working directory, the .git directory or the directory containing a bare repository.
/// </para>
/// </summary>
/// <param name="repositoryConfigurationFileLocation">Path to a Repository configuration file.</param>
/// <param name="repositoryConfigurationFileLocation">Path to an existing Repository configuration file.</param>
/// <returns>An instance of <see cref="Configuration"/>.</returns>
public static Configuration BuildFrom(
string repositoryConfigurationFileLocation)
Expand All @@ -94,8 +131,12 @@ private void Init(Repository repository)
/// <para>
/// Generally you want to access configuration via an instance of <see cref="Repository"/> instead.
/// </para>
/// <para>
/// <paramref name="repositoryConfigurationFileLocation"/> can either contains a path to a file or a directory. In the latter case,
/// this can be the working directory, the .git directory or the directory containing a bare repository.
/// </para>
/// </summary>
/// <param name="repositoryConfigurationFileLocation">Path to a Repository configuration file.</param>
/// <param name="repositoryConfigurationFileLocation">Path to an existing Repository configuration file.</param>
/// <param name="globalConfigurationFileLocation">Path to a Global configuration file. If null, the default path for a Global configuration file will be probed.</param>
/// <returns>An instance of <see cref="Configuration"/>.</returns>
public static Configuration BuildFrom(
Expand All @@ -110,8 +151,12 @@ private void Init(Repository repository)
/// <para>
/// Generally you want to access configuration via an instance of <see cref="Repository"/> instead.
/// </para>
/// <para>
/// <paramref name="repositoryConfigurationFileLocation"/> can either contains a path to a file or a directory. In the latter case,
/// this can be the working directory, the .git directory or the directory containing a bare repository.
/// </para>
/// </summary>
/// <param name="repositoryConfigurationFileLocation">Path to a Repository configuration file.</param>
/// <param name="repositoryConfigurationFileLocation">Path to an existing Repository configuration file.</param>
/// <param name="globalConfigurationFileLocation">Path to a Global configuration file. If null, the default path for a Global configuration file will be probed.</param>
/// <param name="xdgConfigurationFileLocation">Path to a XDG configuration file. If null, the default path for a XDG configuration file will be probed.</param>
/// <returns>An instance of <see cref="Configuration"/>.</returns>
Expand All @@ -128,8 +173,12 @@ private void Init(Repository repository)
/// <para>
/// Generally you want to access configuration via an instance of <see cref="Repository"/> instead.
/// </para>
/// <para>
/// <paramref name="repositoryConfigurationFileLocation"/> can either contains a path to a file or a directory. In the latter case,
/// this can be the working directory, the .git directory or the directory containing a bare repository.
/// </para>
/// </summary>
/// <param name="repositoryConfigurationFileLocation">Path to a Repository configuration file.</param>
/// <param name="repositoryConfigurationFileLocation">Path to an existing Repository configuration file.</param>
/// <param name="globalConfigurationFileLocation">Path to a Global configuration file. If null, the default path for a Global configuration file will be probed.</param>
/// <param name="xdgConfigurationFileLocation">Path to a XDG configuration file. If null, the default path for a XDG configuration file will be probed.</param>
/// <param name="systemConfigurationFileLocation">Path to a System configuration file. If null, the default path for a System configuration file will be probed.</param>
Expand Down

0 comments on commit ca4d15e

Please sign in to comment.