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

Set / get supported extensions #1908

Merged
merged 6 commits into from
Dec 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions LibGit2Sharp.Tests/GlobalSettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
8 changes: 8 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,14 @@ private sealed class NativeShutdownObject : CriticalFinalizerObject
// 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)]
Expand Down
38 changes: 38 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/// <summary>
Expand Down Expand Up @@ -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_
Expand Down
24 changes: 24 additions & 0 deletions LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,30 @@ public static void SetUserAgent(string userAgent)
Proxy.git_libgit2_opts_set_user_agent(userAgent);
}

/// <summary>
/// Set that the given git extensions are supported by the caller.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
/// <param name="extensions">Supported extensions</param>
public static void SetExtensions(params string[] extensions)
{
Proxy.git_libgit2_opts_set_extensions(extensions);
}

/// <summary>
/// Returns the list of git extensions that are supported.
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public static string[] GetExtensions()
{
return Proxy.git_libgit2_opts_get_extensions();
}

/// <summary>
/// Gets the user-agent string used by libgit2.
/// <returns>
Expand Down