libgit2 / libgit2sharp

Git + .NET = ❤
http://libgit2.github.com
MIT License
3.12k stars 878 forks source link

Expose the repository directory ownership validation option #2036

Open CyberSinh opened 1 year ago

CyberSinh commented 1 year ago

You can now disable the repository directory ownership validation by using the GIT_OPT_SET_OWNER_VALIDATION with git_libgit2_opts(): https://github.com/libgit2/libgit2/pull/6266

This option is not yet exposed in the C# bindings, e.g. in the GlobalSettings static class.

Thanks.

kdlslyv commented 1 year ago

Just opened a pull request for that.

For now you can do it like this:


    public static void DisableOwnershipCheckOpts()
    {
        CallNativeMethod("git_libgit2_opts", 36, 0);
    }

    public static void CallNativeMethod(string methodName, params object[] args)
    {
        Assembly libGit2SharpAssembly = typeof(LibGit2Sharp.Repository).GetTypeInfo().Assembly;
        Type proxyType = libGit2SharpAssembly.GetType("LibGit2Sharp.Core.NativeMethods")!;
        MethodInfo methodInfo = proxyType.GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Static, new[] { typeof(int), typeof(int) })!;
        methodInfo.Invoke(null, args);
    }
CyberSinh commented 1 year ago

Thanks @kdlslyv for the pull request and the workaround. Hope your PR will be merged soon. Another workaround avoiding reflection:

internal static partial class GlobalSettings2
{
   [LibraryImport("git2-e632535")]
   private static partial int git_libgit2_opts(int option, int enabled);

   public static void SetDirectoryOwnershipValidation(bool enable)
   {
      // https://github.com/libgit2/libgit2/blob/fc4c00b21983ddbe7a5e67e240c40fece4fea285/include/git2/common.h#L225
      const int GIT_OPT_SET_OWNER_VALIDATION = 36;

      // https://github.com/libgit2/libgit2/blob/fc4c00b21983ddbe7a5e67e240c40fece4fea285/include/git2/common.h#L487
      int enabled = enable ? 1 : 0;
      if (git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, enabled) < 0)
         throw new Exception($"git_libgit2_opts(GIT_OPT_SET_OWNER_VALIDATION, {enabled}) failed.");
   }
}