Skip to content

Commit

Permalink
Set / get supported extensions
Browse files Browse the repository at this point in the history
- C# wrapper for libgit2/libgit2#6031

(I'm not sure at all for the P/Invoke code)
  • Loading branch information
jairbubbles committed Sep 29, 2021
1 parent 6329bea commit 38af05a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 2 deletions.
19 changes: 19 additions & 0 deletions LibGit2Sharp.Tests/GlobalSettingsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,24 @@ 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(new[] { "!noop" });
extensions = GlobalSettings.GetExtensions();
Assert.Empty(extensions);

// Enable two new extensions (it will reset the configuration and "noop" will be enabled)
GlobalSettings.SetExtensions(new[] { "partialclone", "newext" });
extensions = GlobalSettings.GetExtensions();
Assert.Equal(new[] { "noop", "partialclone", "newext" }, extensions);
}
}
}
8 changes: 8 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,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, string[] extensions, int 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
35 changes: 35 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,29 @@ public static string git_libgit2_opts_get_user_agent()
return userAgent;
}

public static void git_libgit2_opts_set_extensions(string[] extensions)
{
var res = NativeMethods.git_libgit2_opts((int)LibGit2Option.SetExtensions, extensions, extensions.Length);
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 @@ -525,6 +525,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
4 changes: 2 additions & 2 deletions LibGit2Sharp/LibGit2Sharp.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netcoreapp2.1</TargetFrameworks>
Expand Down Expand Up @@ -32,7 +32,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[2.0.315-alpha.0.1]" PrivateAssets="none" />
<PackageReference Include="LibGit2Sharp.NativeBinaries" Version="[2.0.315-alpha.0.4]" PrivateAssets="none" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="all" />
<PackageReference Include="Nerdbank.GitVersioning" Version="3.4.220" PrivateAssets="all" />
</ItemGroup>
Expand Down

0 comments on commit 38af05a

Please sign in to comment.