From 2cad6cd44dcb419d9bcadaeab92dd75d3b68ff9a Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Wed, 13 Mar 2024 23:50:12 -0400 Subject: [PATCH 1/4] Add project to test trimming compatibility --- .github/workflows/ci.yml | 2 ++ LibGit2Sharp/LibGit2Sharp.csproj | 4 ++++ TrimmingTestApp/Program.cs | 3 +++ TrimmingTestApp/TrimmingTestApp.csproj | 18 ++++++++++++++++++ 4 files changed, 27 insertions(+) create mode 100644 TrimmingTestApp/Program.cs create mode 100644 TrimmingTestApp/TrimmingTestApp.csproj diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5214b064f..9d3551569 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 }} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 4d0876c4d..e8836b90c 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -23,6 +23,10 @@ libgit2-$(libgit2_hash.Substring(0,7)) + + true + + diff --git a/TrimmingTestApp/Program.cs b/TrimmingTestApp/Program.cs new file mode 100644 index 000000000..e568c227b --- /dev/null +++ b/TrimmingTestApp/Program.cs @@ -0,0 +1,3 @@ +using LibGit2Sharp; + +_ = new Repository(); diff --git a/TrimmingTestApp/TrimmingTestApp.csproj b/TrimmingTestApp/TrimmingTestApp.csproj new file mode 100644 index 000000000..9cb7e75b4 --- /dev/null +++ b/TrimmingTestApp/TrimmingTestApp.csproj @@ -0,0 +1,18 @@ + + + + net8.0 + Exe + enable + enable + true + true + true + + + + + + + + From 70ac848e7faa2f42a3d90a32dbc118a932051617 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Wed, 13 Mar 2024 23:54:52 -0400 Subject: [PATCH 2/4] Address trimming warnings --- LibGit2Sharp/Core/GitObjectLazyGroup.cs | 6 ++++++ LibGit2Sharp/Core/LazyGroup.cs | 9 +++++++++ LibGit2Sharp/Core/NativeMethods.cs | 2 +- LibGit2Sharp/Core/Platform.cs | 4 ++++ LibGit2Sharp/ReferenceWrapper.cs | 5 +++++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp/Core/GitObjectLazyGroup.cs b/LibGit2Sharp/Core/GitObjectLazyGroup.cs index 11c83a81e..f00900837 100644 --- a/LibGit2Sharp/Core/GitObjectLazyGroup.cs +++ b/LibGit2Sharp/Core/GitObjectLazyGroup.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp.Core @@ -21,7 +22,12 @@ protected override void EvaluateInternal(Action evaluator) } } +#if NET + public static ILazy Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Repository repo, ObjectId id, Func resultSelector, bool throwIfMissing = false) +#else public static ILazy Singleton(Repository repo, ObjectId id, Func resultSelector, bool throwIfMissing = false) +#endif + { return Singleton(() => { diff --git a/LibGit2Sharp/Core/LazyGroup.cs b/LibGit2Sharp/Core/LazyGroup.cs index d8b13fa42..bcd160290 100644 --- a/LibGit2Sharp/Core/LazyGroup.cs +++ b/LibGit2Sharp/Core/LazyGroup.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace LibGit2Sharp.Core { @@ -44,7 +45,11 @@ public void Evaluate() protected abstract void EvaluateInternal(Action evaluator); +#if NET + protected static ILazy Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TResult>(Func resultSelector) +#else protected static ILazy Singleton(Func resultSelector) +#endif { return new LazyWrapper(resultSelector); } @@ -90,7 +95,11 @@ void IEvaluator.Evaluate(TInput input) } } +#if NET + protected class LazyWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TType> : Lazy, ILazy +#else protected class LazyWrapper : Lazy, ILazy +#endif { public LazyWrapper(Func evaluator) : base(evaluator) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index e20d755ba..cd3e1de7a 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -102,7 +102,7 @@ private static IntPtr ResolveDll(string libraryName, Assembly assembly, DllImpor { // The libraries are located at 'runtimes//native/lib{libraryName}.so' // The 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"); diff --git a/LibGit2Sharp/Core/Platform.cs b/LibGit2Sharp/Core/Platform.cs index 42b752612..1fcb59faf 100644 --- a/LibGit2Sharp/Core/Platform.cs +++ b/LibGit2Sharp/Core/Platform.cs @@ -58,7 +58,11 @@ public static string GetNativeLibraryExtension() /// Returns true if the runtime is Mono. /// public static bool IsRunningOnMono() +#if NETFRAMEWORK => Type.GetType("Mono.Runtime") != null; +#else + => false; +#endif /// /// Returns true if the runtime is .NET Framework. diff --git a/LibGit2Sharp/ReferenceWrapper.cs b/LibGit2Sharp/ReferenceWrapper.cs index 3e4243a7e..eff09e6ce 100644 --- a/LibGit2Sharp/ReferenceWrapper.cs +++ b/LibGit2Sharp/ReferenceWrapper.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using LibGit2Sharp.Core; @@ -10,7 +11,11 @@ namespace LibGit2Sharp /// /// The type of the referenced Git object. [DebuggerDisplay("{DebuggerDisplay,nq}")] +#if NET + public abstract class ReferenceWrapper<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] TObject> : IEquatable>, IBelongToARepository where TObject : GitObject +#else public abstract class ReferenceWrapper : IEquatable>, IBelongToARepository where TObject : GitObject +#endif { /// /// The repository. From 29b7380483d6de917a3edb45108d332eab197460 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Wed, 13 Mar 2024 23:58:40 -0400 Subject: [PATCH 3/4] Use .NET 8 SDK --- .github/workflows/ci.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9d3551569..f09880169 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -53,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 From ea4e6c979e9fa0c744eca4f42c5dc6bdbfde3dae Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Fri, 15 Mar 2024 21:01:32 -0400 Subject: [PATCH 4/4] Fix failing test --- LibGit2Sharp.Tests/CloneFixture.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/CloneFixture.cs b/LibGit2Sharp.Tests/CloneFixture.cs index b4de8797a..f205eddc2 100644 --- a/LibGit2Sharp.Tests/CloneFixture.cs +++ b/LibGit2Sharp.Tests/CloneFixture.cs @@ -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; }