diff --git a/LibGit2Sharp.Tests/GlobalSettingsFixture.cs b/LibGit2Sharp.Tests/GlobalSettingsFixture.cs index 8f1aa9d0b..bd9a53d1b 100644 --- a/LibGit2Sharp.Tests/GlobalSettingsFixture.cs +++ b/LibGit2Sharp.Tests/GlobalSettingsFixture.cs @@ -83,5 +83,29 @@ public void LoadFromSpecifiedPath(string architecture) DirectoryHelper.DeleteDirectory(tempDir); } } + + [Fact] + public void SetExtensions() + { + var extensions = GlobalSettings.GetExtensions(); + + // Assert that "noop" is supported by default + Assert.Equal(new[] { "noop" }, extensions); + + // Disable "noop" extensions + GlobalSettings.SetExtensions("!noop"); + extensions = GlobalSettings.GetExtensions(); + Assert.Empty(extensions); + + // Enable two new extensions (it will reset the configuration and "noop" will be enabled) + GlobalSettings.SetExtensions("partialclone", "newext"); + extensions = GlobalSettings.GetExtensions(); + Assert.Equal(new[] { "noop", "partialclone", "newext" }, extensions); + + // You can have multiple times the same extension + GlobalSettings.SetExtensions("noop", "test", "test" ); + extensions = GlobalSettings.GetExtensions(); + Assert.Equal(new[] { "noop", "noop", "test", "test" }, extensions); + } } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 809a77309..8c24cff3a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -754,6 +754,14 @@ internal static extern int git_libgit2_opts(int option, // git_libgit2_opts(GIT_OPT_GET_USER_AGENT, git_buf *buf) [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern int git_libgit2_opts(int option, GitBuf buf); + + // git_libgit2_opts(GIT_OPT_SET_EXTENSIONS, const char **extensions, size_t len) + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern int git_libgit2_opts(int option, IntPtr extensions, UIntPtr len); + + // git_libgit2_opts(GIT_OPT_GET_EXTENSIONS, git_strarray *out) + [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] + internal static extern int git_libgit2_opts(int option, out GitStrArray extensions); #endregion [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index ca9a69f6d..78c8bc914 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -3381,6 +3381,18 @@ private enum LibGit2Option GetWindowsSharemode, // GIT_OPT_GET_WINDOWS_SHAREMODE SetWindowsSharemode, // GIT_OPT_SET_WINDOWS_SHAREMODE EnableStrictHashVerification, // GIT_OPT_ENABLE_STRICT_HASH_VERIFICATION + SetAllocator, // GIT_OPT_SET_ALLOCATOR, + EnableUnsavedIndexSafety, // GIT_OPT_ENABLE_UNSAVED_INDEX_SAFETY, + GetPackMaxObject, // GIT_OPT_GET_PACK_MAX_OBJECTS, + SetPackMaxObjects, // GIT_OPT_SET_PACK_MAX_OBJECTS, + DisabledPackKeepFileChecks, // GIT_OPT_DISABLE_PACK_KEEP_FILE_CHECKS, + EnableHttpExpectContinue, // GIT_OPT_ENABLE_HTTP_EXPECT_CONTINUE, + GetMWindowFileLimit, // GIT_OPT_GET_MWINDOW_FILE_LIMIT, + SetMWindowFileLimit, // GIT_OPT_SET_MWINDOW_FILE_LIMIT, + SetOdbPackedPriority, // GIT_OPT_SET_ODB_PACKED_PRIORITY, + SetOdbLoosePriority, // GIT_OPT_SET_ODB_LOOSE_PRIORITY, + GetExtensions, // GIT_OPT_GET_EXTENSIONS, + SetExtensions, // GIT_OPT_SET_EXTENSIONS } /// @@ -3489,6 +3501,32 @@ public static string git_libgit2_opts_get_user_agent() return userAgent; } + public static void git_libgit2_opts_set_extensions(string[] extensions) + { + using (var array = GitStrArrayManaged.BuildFrom(extensions)) + { + var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetExtensions, array.Array.Strings, array.Array.Count); + Ensure.ZeroResult(res); + } + } + + public static string[] git_libgit2_opts_get_extensions() + { + var array = new GitStrArrayNative(); + + try + { + var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.GetExtensions, out array.Array); + Ensure.ZeroResult(res); + + return array.ReadStrings(); + } + finally + { + array.Dispose(); + } + } + #endregion #region git_worktree_ diff --git a/LibGit2Sharp/GlobalSettings.cs b/LibGit2Sharp/GlobalSettings.cs index d24637df0..f8db7401a 100644 --- a/LibGit2Sharp/GlobalSettings.cs +++ b/LibGit2Sharp/GlobalSettings.cs @@ -383,6 +383,30 @@ public static void SetUserAgent(string userAgent) Proxy.git_libgit2_opts_set_user_agent(userAgent); } + /// + /// Set that the given git extensions are supported by the caller. + /// + /// + /// Extensions supported by libgit2 may be negated by prefixing them with a `!`. For example: setting extensions to { "!noop", "newext" } indicates that the caller does not want + /// to support repositories with the `noop` extension but does want to support repositories with the `newext` extension. + /// + /// Supported extensions + public static void SetExtensions(params string[] extensions) + { + Proxy.git_libgit2_opts_set_extensions(extensions); + } + + /// + /// Returns the list of git extensions that are supported. + /// + /// + /// This is the list of built-in extensions supported by libgit2 and custom extensions that have been added with `SetExtensions`. Extensions that have been negated will not be returned. + /// + public static string[] GetExtensions() + { + return Proxy.git_libgit2_opts_get_extensions(); + } + /// /// Gets the user-agent string used by libgit2. ///