diff --git a/LibGit2Sharp/Core/ConfigurationFileReader.cs b/LibGit2Sharp/Core/ConfigurationFileReader.cs new file mode 100644 index 000000000..27aaa503d --- /dev/null +++ b/LibGit2Sharp/Core/ConfigurationFileReader.cs @@ -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(); + } +} diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index aca1ae144..dbaa3a5da 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -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"; @@ -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; diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index a7301edf2..d17360f66 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -20,6 +20,7 @@ true preview.0 libgit2-$(libgit2_hash.Substring(0,7)) + latest