Skip to content

Commit d9bf967

Browse files
authoredMar 16, 2024··
Merge pull request #2084 from libgit2/trimming
Updates for trimming compatibility
2 parents de87973 + ea4e6c9 commit d9bf967

File tree

10 files changed

+55
-3
lines changed

10 files changed

+55
-3
lines changed
 

‎.github/workflows/ci.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Install .NET SDK
2222
uses: actions/setup-dotnet@v3.0.3
2323
with:
24-
dotnet-version: 7.0.x
24+
dotnet-version: 8.0.x
2525
- name: Build
2626
run: dotnet build LibGit2Sharp.sln --configuration Release
2727
- name: Upload packages
@@ -30,6 +30,8 @@ jobs:
3030
name: NuGet packages
3131
path: bin/Packages/
3232
retention-days: 7
33+
- name: Verify trimming compatibility
34+
run: dotnet publish TrimmingTestApp
3335
test:
3436
name: Test / ${{ matrix.os }} / ${{ matrix.arch }} / ${{ matrix.tfm }}
3537
runs-on: ${{ matrix.os }}
@@ -51,6 +53,7 @@ jobs:
5153
uses: actions/setup-dotnet@v3.0.3
5254
with:
5355
dotnet-version: |
56+
8.0.x
5457
7.0.x
5558
6.0.x
5659
- name: Run ${{ matrix.tfm }} tests

‎LibGit2Sharp.Tests/CloneFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public void CanInspectCertificateOnClone(string url, string hostname, Type certT
267267
Assert.True(valid);
268268
var x509 = ((CertificateX509)cert).Certificate;
269269
// we get a string with the different fields instead of a structure, so...
270-
Assert.Contains("CN=github.com,", x509.Subject);
270+
Assert.Contains("CN=github.com", x509.Subject);
271271
checksHappy = true;
272272
return false;
273273
}

‎LibGit2Sharp/Core/GitObjectLazyGroup.cs

+6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.CodeAnalysis;
23
using LibGit2Sharp.Core.Handles;
34

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

25+
#if NET
26+
public static ILazy<TResult> Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
27+
#else
2428
public static ILazy<TResult> Singleton<TResult>(Repository repo, ObjectId id, Func<ObjectHandle, TResult> resultSelector, bool throwIfMissing = false)
29+
#endif
30+
2531
{
2632
return Singleton(() =>
2733
{

‎LibGit2Sharp/Core/LazyGroup.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace LibGit2Sharp.Core
56
{
@@ -44,7 +45,11 @@ public void Evaluate()
4445

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

48+
#if NET
49+
protected static ILazy<TResult> Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Func<TResult> resultSelector)
50+
#else
4751
protected static ILazy<TResult> Singleton<TResult>(Func<TResult> resultSelector)
52+
#endif
4853
{
4954
return new LazyWrapper<TResult>(resultSelector);
5055
}
@@ -90,7 +95,11 @@ void IEvaluator<TInput>.Evaluate(TInput input)
9095
}
9196
}
9297

98+
#if NET
99+
protected class LazyWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TType> : Lazy<TType>, ILazy<TType>
100+
#else
93101
protected class LazyWrapper<TType> : Lazy<TType>, ILazy<TType>
102+
#endif
94103
{
95104
public LazyWrapper(Func<TType> evaluator)
96105
: base(evaluator)

‎LibGit2Sharp/Core/NativeMethods.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor
102102
{
103103
// The libraries are located at 'runtimes/<rid>/native/lib{libraryName}.so'
104104
// The <rid> ends with the processor architecture. e.g. fedora-x64.
105-
string assemblyDirectory = Path.GetDirectoryName(typeof(NativeMethods).Assembly.Location);
105+
string assemblyDirectory = Path.GetDirectoryName(AppContext.BaseDirectory);
106106
string processorArchitecture = RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
107107
string runtimesDirectory = Path.Combine(assemblyDirectory, "runtimes");
108108

‎LibGit2Sharp/Core/Platform.cs

+4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ public static string GetNativeLibraryExtension()
5858
/// Returns true if the runtime is Mono.
5959
/// </summary>
6060
public static bool IsRunningOnMono()
61+
#if NETFRAMEWORK
6162
=> Type.GetType("Mono.Runtime") != null;
63+
#else
64+
=> false;
65+
#endif
6266

6367
/// <summary>
6468
/// Returns true if the runtime is .NET Framework.

‎LibGit2Sharp/LibGit2Sharp.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
<MinVerBuildMetadata Condition="'$(libgit2_hash)' != ''">libgit2-$(libgit2_hash.Substring(0,7))</MinVerBuildMetadata>
2424
</PropertyGroup>
2525

26+
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0'">
27+
<IsTrimmable>true</IsTrimmable>
28+
</PropertyGroup>
29+
2630
<ItemGroup>
2731
<None Include="..\square-logo.png" Pack="true" PackagePath="" Visible="false" />
2832
<None Include="..\README.md" Pack="true" PackagePath="App_Readme/" Visible="false" />

‎LibGit2Sharp/ReferenceWrapper.cs

+5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics;
3+
using System.Diagnostics.CodeAnalysis;
34
using System.Globalization;
45
using LibGit2Sharp.Core;
56

@@ -10,7 +11,11 @@ namespace LibGit2Sharp
1011
/// </summary>
1112
/// <typeparam name="TObject">The type of the referenced Git object.</typeparam>
1213
[DebuggerDisplay("{DebuggerDisplay,nq}")]
14+
#if NET
15+
public abstract class ReferenceWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TObject> : IEquatable<ReferenceWrapper<TObject>>, IBelongToARepository where TObject : GitObject
16+
#else
1317
public abstract class ReferenceWrapper<TObject> : IEquatable<ReferenceWrapper<TObject>>, IBelongToARepository where TObject : GitObject
18+
#endif
1419
{
1520
/// <summary>
1621
/// The repository.

‎TrimmingTestApp/Program.cs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using LibGit2Sharp;
2+
3+
_ = new Repository();
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<OutputType>Exe</OutputType>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
9+
<PublishTrimmed>true</PublishTrimmed>
10+
<PublishSingleFile>true</PublishSingleFile>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\LibGit2Sharp\LibGit2Sharp.csproj" />
15+
<TrimmerRootAssembly Include="LibGit2Sharp" />
16+
</ItemGroup>
17+
18+
</Project>

0 commit comments

Comments
 (0)
Please sign in to comment.