Skip to content

Commit

Permalink
Merge pull request #27 from UiPath/fix/schannel_config
Browse files Browse the repository at this point in the history
select schannel ssl backend if configured in .gitconfig globally
  • Loading branch information
andrei-balint authored Jun 11, 2024
2 parents 4162bcd + bd0c16e commit 824d21d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
23 changes: 23 additions & 0 deletions LibGit2Sharp/Core/ConfigurationFileReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#nullable enable
using System.Runtime.InteropServices;
using System.Text;

internal sealed class ConfigurationFileReader
{
private readonly string _path;

[DllImport("kernel32", CharSet = CharSet.Unicode)]
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string filePath);

public ConfigurationFileReader(string path)
{
_path = path;
}

public string Read(string section, string key)
{
var result = new StringBuilder(255);
GetPrivateProfileString(section, key, "", result, 255, _path);
return result.ToString();
}
}
29 changes: 28 additions & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor

if (libraryName == libgit2)
{
if (Environment.GetEnvironmentVariable("UIPATH_STUDIO_GIT_USE_SCHANNEL") == "1")
if (Environment.GetEnvironmentVariable("UIPATH_STUDIO_GIT_USE_SCHANNEL") == "1" || IsSchannelSelectedInGitConfig())
{
Trace.TraceInformation("Using git with schannel");
libraryName = libraryName + "_schannel";
Expand Down Expand Up @@ -140,6 +140,33 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor

return handle;
}

private static bool IsSchannelSelectedInGitConfig()
{
string globalConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gitconfig");
string systemConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gitconfig");
string[] probingPaths = [globalConfigPath, systemConfigPath];
foreach(var path in probingPaths)
{
try
{
if (File.Exists(path))
{
var value = new ConfigurationFileReader(path).Read("http", "sslBackend");
if (!string.IsNullOrEmpty(value))
{
return value == "schannel";
}
}
}
catch(Exception ex)
{
Trace.TraceError("Error when reading " + path + " " + ex);
}
}

return false;
}
#endif

public const int RTLD_NOW = 0x002;
Expand Down
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<MinVerDefaultPreReleaseIdentifiers>preview.0</MinVerDefaultPreReleaseIdentifiers>
<MinVerBuildMetadata Condition="'$(libgit2_hash)' != ''">libgit2-$(libgit2_hash.Substring(0,7))</MinVerBuildMetadata>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit 824d21d

Please sign in to comment.