Skip to content

Commit 824d21d

Browse files
Merge pull request #27 from UiPath/fix/schannel_config
select schannel ssl backend if configured in .gitconfig globally
2 parents 4162bcd + bd0c16e commit 824d21d

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#nullable enable
2+
using System.Runtime.InteropServices;
3+
using System.Text;
4+
5+
internal sealed class ConfigurationFileReader
6+
{
7+
private readonly string _path;
8+
9+
[DllImport("kernel32", CharSet = CharSet.Unicode)]
10+
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string filePath);
11+
12+
public ConfigurationFileReader(string path)
13+
{
14+
_path = path;
15+
}
16+
17+
public string Read(string section, string key)
18+
{
19+
var result = new StringBuilder(255);
20+
GetPrivateProfileString(section, key, "", result, 255, _path);
21+
return result.ToString();
22+
}
23+
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
9292

9393
if (libraryName == libgit2)
9494
{
95-
if (Environment.GetEnvironmentVariable("UIPATH_STUDIO_GIT_USE_SCHANNEL") == "1")
95+
if (Environment.GetEnvironmentVariable("UIPATH_STUDIO_GIT_USE_SCHANNEL") == "1" || IsSchannelSelectedInGitConfig())
9696
{
9797
Trace.TraceInformation("Using git with schannel");
9898
libraryName = libraryName + "_schannel";
@@ -140,6 +140,33 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
140140

141141
return handle;
142142
}
143+
144+
private static bool IsSchannelSelectedInGitConfig()
145+
{
146+
string globalConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".gitconfig");
147+
string systemConfigPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "gitconfig");
148+
string[] probingPaths = [globalConfigPath, systemConfigPath];
149+
foreach(var path in probingPaths)
150+
{
151+
try
152+
{
153+
if (File.Exists(path))
154+
{
155+
var value = new ConfigurationFileReader(path).Read("http", "sslBackend");
156+
if (!string.IsNullOrEmpty(value))
157+
{
158+
return value == "schannel";
159+
}
160+
}
161+
}
162+
catch(Exception ex)
163+
{
164+
Trace.TraceError("Error when reading " + path + " " + ex);
165+
}
166+
}
167+
168+
return false;
169+
}
143170
#endif
144171

145172
public const int RTLD_NOW = 0x002;

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
2121
<MinVerDefaultPreReleaseIdentifiers>preview.0</MinVerDefaultPreReleaseIdentifiers>
2222
<MinVerBuildMetadata Condition="'$(libgit2_hash)' != ''">libgit2-$(libgit2_hash.Substring(0,7))</MinVerBuildMetadata>
23+
<LangVersion>latest</LangVersion>
2324
</PropertyGroup>
2425

2526
<ItemGroup>

0 commit comments

Comments
 (0)