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

Expose the repository directory ownership validation option #2036

Open
CyberSinh opened this issue May 3, 2023 · 2 comments · May be fixed by #2093
Open

Expose the repository directory ownership validation option #2036

CyberSinh opened this issue May 3, 2023 · 2 comments · May be fixed by #2093

Comments

@CyberSinh
Copy link

You can now disable the repository directory ownership validation by using the GIT_OPT_SET_OWNER_VALIDATION with git_libgit2_opts(): libgit2/libgit2#6266

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

Thanks.

@kdlslyv
Copy link

kdlslyv commented Jun 10, 2023

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
Copy link
Author

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.");
   }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants