Skip to content

Commit

Permalink
Merge pull request #2084 from libgit2/trimming
Browse files Browse the repository at this point in the history
Updates for trimming compatibility
  • Loading branch information
bording committed Mar 16, 2024
2 parents de87973 + ea4e6c9 commit d9bf967
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Install .NET SDK
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x
- name: Build
run: dotnet build LibGit2Sharp.sln --configuration Release
- name: Upload packages
Expand All @@ -30,6 +30,8 @@ jobs:
name: NuGet packages
path: bin/Packages/
retention-days: 7
- name: Verify trimming compatibility
run: dotnet publish TrimmingTestApp
test:
name: Test / ${{ matrix.os }} / ${{ matrix.arch }} / ${{ matrix.tfm }}
runs-on: ${{ matrix.os }}
Expand All @@ -51,6 +53,7 @@ jobs:
uses: actions/setup-dotnet@v3.0.3
with:
dotnet-version: |
8.0.x
7.0.x
6.0.x
- name: Run ${{ matrix.tfm }} tests
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/CloneFixture.cs
Expand Up @@ -267,7 +267,7 @@ public void CanInspectCertificateOnClone(string url, string hostname, Type certT
Assert.True(valid);
var x509 = ((CertificateX509)cert).Certificate;
// we get a string with the different fields instead of a structure, so...
Assert.Contains("CN=github.com,", x509.Subject);
Assert.Contains("CN=github.com", x509.Subject);
checksHappy = true;
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions LibGit2Sharp/Core/GitObjectLazyGroup.cs
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using LibGit2Sharp.Core.Handles;

namespace LibGit2Sharp.Core
Expand All @@ -21,7 +22,12 @@ protected override void EvaluateInternal(Action<ObjectHandle> evaluator)
}
}

#if NET
public static ILazy<TResult> Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
#else
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
#endif

{
return Singleton(() =>
{
Expand Down
9 changes: 9 additions & 0 deletions LibGit2Sharp/Core/LazyGroup.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace LibGit2Sharp.Core
{
Expand Down Expand Up @@ -44,7 +45,11 @@ public void Evaluate()

protected abstract void EvaluateInternal(Action<T> evaluator);

#if NET
protected static ILazy<TResult> Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Func<TResult> resultSelector)
#else
protected static ILazy<TResult> Singleton<TResult>(Func<TResult> resultSelector)
#endif
{
return new LazyWrapper<TResult>(resultSelector);
}
Expand Down Expand Up @@ -90,7 +95,11 @@ void IEvaluator<TInput>.Evaluate(TInput input)
}
}

#if NET
protected class LazyWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TType> : Lazy<TType>, ILazy<TType>
#else
protected class LazyWrapper<TType> : Lazy<TType>, ILazy<TType>
#endif
{
public LazyWrapper(Func<TType> evaluator)
: base(evaluator)
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/NativeMethods.cs
Expand Up @@ -102,7 +102,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
{
// The libraries are located at 'runtimes/<rid>/native/lib{libraryName}.so'
// The <rid> ends with the processor architecture. e.g. fedora-x64.
string assemblyDirectory = Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location);
string assemblyDirectory = Path.GetDirectoryName(AppContext.BaseDirectory);
string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
string runtimesDirectory = Path.Combine(assemblyDirectory, "runtimes");

Expand Down
4 changes: 4 additions & 0 deletions LibGit2Sharp/Core/Platform.cs
Expand Up @@ -58,7 +58,11 @@ public static string GetNativeLibraryExtension()
/// Returns true if the runtime is Mono.
/// </summary>
public static bool IsRunningOnMono()
#if NETFRAMEWORK
=> Type.GetType("Mono.Runtime") != null;
#else
=> false;
#endif

/// <summary>
/// Returns true if the runtime is .NET Framework.
Expand Down
4 changes: 4 additions & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Expand Up @@ -23,6 +23,10 @@
<MinVerBuildMetadata Condition="'$(libgit2_hash)' != ''">libgit2-$(libgit2_hash.Substring(0,7))</MinVerBuildMetadata>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
<IsTrimmable>true</IsTrimmable>
</PropertyGroup>

<ItemGroup>
<None Include="..\square-logo.png" Pack="true" PackagePath="" Visible="false" />
<None Include="..\README.md" Pack="true" PackagePath="App_Readme/" Visible="false" />
Expand Down
5 changes: 5 additions & 0 deletions LibGit2Sharp/ReferenceWrapper.cs
@@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using LibGit2Sharp.Core;

Expand All @@ -10,7 +11,11 @@ namespace LibGit2Sharp
/// </summary>
/// <typeparam name="TObject">The type of the referenced Git object.</typeparam>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
#if NET
public abstract class ReferenceWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TObject> : IEquatable<ReferenceWrapper<TObject>>, IBelongToARepository where TObject : GitObject
#else
public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>>, IBelongToARepository where TObject : GitObject
#endif
{
/// <summary>
/// The repository.
Expand Down
3 changes: 3 additions & 0 deletions TrimmingTestApp/Program.cs
@@ -0,0 +1,3 @@
using LibGit2Sharp;

_ = new Repository();
18 changes: 18 additions & 0 deletions TrimmingTestApp/TrimmingTestApp.csproj
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PublishTrimmed>true</PublishTrimmed>
<PublishSingleFile>true</PublishSingleFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
<TrimmerRootAssembly Include="LibGit2Sharp" />
</ItemGroup>

</Project>

0 comments on commit d9bf967

Please sign in to comment.