Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select schannel ssl backend if configured in .gitconfig globally #27

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading