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

Updates for trimming compatibility #2084

Merged
merged 4 commits into from
Mar 16, 2024
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using LibGit2Sharp;

_ = new Repository();
18 changes: 18 additions & 0 deletions TrimmingTestApp/TrimmingTestApp.csproj
Original file line number Diff line number Diff line change
@@ -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>