From 40478bce6b49fa607a7f331cee04081532381ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20Gr=C3=BCnwald?= Date: Sun, 22 Dec 2024 15:14:55 +0100 Subject: [PATCH] refactor: Use package Cake.Frosting.Git to determine repository information instead of calling the git commandline Pull-Request: #151 --- build/Build.csproj | 2 ++ build/packages.lock.json | 23 ++++++++++++++ src/SharedBuild.Test/packages.lock.json | 23 ++++++++++++++ src/SharedBuild/Grynwald.SharedBuild.csproj | 1 + .../_Context/_Default/DefaultGitContext.cs | 30 +++---------------- src/SharedBuild/packages.lock.json | 23 ++++++++++++++ 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/build/Build.csproj b/build/Build.csproj index 2cdbb83..9bcba5e 100644 --- a/build/Build.csproj +++ b/build/Build.csproj @@ -8,6 +8,7 @@ + @@ -17,6 +18,7 @@ + diff --git a/build/packages.lock.json b/build/packages.lock.json index 32e65ed..ad5fcb8 100644 --- a/build/packages.lock.json +++ b/build/packages.lock.json @@ -52,6 +52,16 @@ "System.Security.Cryptography.Pkcs": "9.0.0" } }, + "Cake.Frosting.Git": { + "type": "Direct", + "requested": "[5.0.1, )", + "resolved": "5.0.1", + "contentHash": "85K0OGIEHj2oBvhITAJXRix7sUXLMLJmv5CNygH5LDIB3IubW5v7DxHZlyR16+gVgLygSx+DEVPzYLGqRRsR2Q==", + "dependencies": { + "Cake.Core": "5.0.0", + "LibGit2Sharp": "0.30.0" + } + }, "Cake.GitVersioning": { "type": "Direct", "requested": "[3.7.112, )", @@ -149,6 +159,19 @@ "System.Security.Cryptography.Pkcs": "9.0.0" } }, + "LibGit2Sharp": { + "type": "Transitive", + "resolved": "0.30.0", + "contentHash": "1cBg/7nz6q+yzvComhqQaTS4nbXloLp/x29ZgY/Rr82ohpkuaWT8ZYzsPLFvs1zmCh/4xQbVt5sQxazGblgRig==", + "dependencies": { + "LibGit2Sharp.NativeBinaries": "[2.0.322]" + } + }, + "LibGit2Sharp.NativeBinaries": { + "type": "Transitive", + "resolved": "2.0.322", + "contentHash": "EWQaEevzc8uS1ZGS6T83jqUPBY//2WUSCHwbZHBoHOjlfSehqr30nm/VAhJPzjam/69sv7nlLKVcho9t2XuR/Q==" + }, "Microsoft.Bcl.TimeProvider": { "type": "Transitive", "resolved": "8.0.1", diff --git a/src/SharedBuild.Test/packages.lock.json b/src/SharedBuild.Test/packages.lock.json index d54dd90..abd65f6 100644 --- a/src/SharedBuild.Test/packages.lock.json +++ b/src/SharedBuild.Test/packages.lock.json @@ -155,6 +155,15 @@ "System.Security.Cryptography.Pkcs": "9.0.0" } }, + "Cake.Frosting.Git": { + "type": "Transitive", + "resolved": "5.0.1", + "contentHash": "85K0OGIEHj2oBvhITAJXRix7sUXLMLJmv5CNygH5LDIB3IubW5v7DxHZlyR16+gVgLygSx+DEVPzYLGqRRsR2Q==", + "dependencies": { + "Cake.Core": "5.0.0", + "LibGit2Sharp": "0.30.0" + } + }, "Cake.GitVersioning": { "type": "Transitive", "resolved": "3.7.112", @@ -186,6 +195,19 @@ "System.Diagnostics.EventLog": "6.0.0" } }, + "LibGit2Sharp": { + "type": "Transitive", + "resolved": "0.30.0", + "contentHash": "1cBg/7nz6q+yzvComhqQaTS4nbXloLp/x29ZgY/Rr82ohpkuaWT8ZYzsPLFvs1zmCh/4xQbVt5sQxazGblgRig==", + "dependencies": { + "LibGit2Sharp.NativeBinaries": "[2.0.322]" + } + }, + "LibGit2Sharp.NativeBinaries": { + "type": "Transitive", + "resolved": "2.0.322", + "contentHash": "EWQaEevzc8uS1ZGS6T83jqUPBY//2WUSCHwbZHBoHOjlfSehqr30nm/VAhJPzjam/69sv7nlLKVcho9t2XuR/Q==" + }, "Microsoft.Bcl.AsyncInterfaces": { "type": "Transitive", "resolved": "6.0.0", @@ -484,6 +506,7 @@ "Cake.BuildSystems.Module": "[7.1.0, )", "Cake.FileHelpers": "[7.0.0, )", "Cake.Frosting": "[5.0.0, )", + "Cake.Frosting.Git": "[5.0.1, )", "Cake.GitVersioning": "[3.7.112, )", "Mono.Cecil": "[0.11.6, )", "Octokit": "[13.0.1, )" diff --git a/src/SharedBuild/Grynwald.SharedBuild.csproj b/src/SharedBuild/Grynwald.SharedBuild.csproj index dd2713b..3660fc8 100644 --- a/src/SharedBuild/Grynwald.SharedBuild.csproj +++ b/src/SharedBuild/Grynwald.SharedBuild.csproj @@ -10,6 +10,7 @@ + diff --git a/src/SharedBuild/_Context/_Default/DefaultGitContext.cs b/src/SharedBuild/_Context/_Default/DefaultGitContext.cs index 98df4bf..e2235f2 100644 --- a/src/SharedBuild/_Context/_Default/DefaultGitContext.cs +++ b/src/SharedBuild/_Context/_Default/DefaultGitContext.cs @@ -1,6 +1,6 @@ using System; using Cake.Core.Diagnostics; -using Cake.Core.IO; +using Cake.Git; namespace Grynwald.SharedBuild; @@ -8,7 +8,6 @@ public class DefaultGitContext(DefaultBuildContext context) : IGitContext { private readonly DefaultBuildContext m_Context = context ?? throw new ArgumentNullException(nameof(context)); - /// public virtual string BranchName { @@ -27,19 +26,18 @@ public virtual string BranchName } else { - return StartGit("rev-parse", "--abbrev-ref", "HEAD").Trim(); + return m_Context.GitBranchCurrent(context.RootDirectory).FriendlyName; } - } } /// public virtual string CommitId => m_Context.AzurePipelines.IsActive ? m_Context.AzurePipelines.Environment.Repository.SourceVersion - : StartGit("rev-parse", "HEAD").Trim(); + : m_Context.GitBranchCurrent(context.RootDirectory).Tip.Sha; /// - public virtual string RemoteUrl => StartGit("remote", "get-url", "origin").Trim(); + public virtual string RemoteUrl => m_Context.GitRemote(context.RootDirectory, "origin").Url; /// public virtual bool IsMainBranch => BranchName.Equals("main", StringComparison.OrdinalIgnoreCase) || BranchName.Equals("master", StringComparison.OrdinalIgnoreCase); @@ -47,7 +45,6 @@ public virtual string BranchName /// public virtual bool IsReleaseBranch => BranchName.StartsWith("release/", StringComparison.OrdinalIgnoreCase); - /// public virtual void PrintToLog(ICakeLog log) { @@ -57,23 +54,4 @@ public virtual void PrintToLog(ICakeLog log) log.Information($"{nameof(IsMainBranch)}: {IsMainBranch}"); log.Information($"{nameof(IsReleaseBranch)}: {IsReleaseBranch}"); } - - - private string StartGit(params string[] args) - { - var process = m_Context.ProcessRunner.Start("git", new ProcessSettings() - { - Arguments = ProcessArgumentBuilder.FromStrings(args), - RedirectStandardOutput = true - }); - - process.WaitForExit(); - - var exitCode = process.GetExitCode(); - if (exitCode != 0) - throw new Exception($"Command 'git {String.Join(" ", args)}' completed with exit code {exitCode}"); - - var stdOut = String.Join(Environment.NewLine, process.GetStandardOutput()); - return stdOut; - } } diff --git a/src/SharedBuild/packages.lock.json b/src/SharedBuild/packages.lock.json index f1e92c2..33dcba2 100644 --- a/src/SharedBuild/packages.lock.json +++ b/src/SharedBuild/packages.lock.json @@ -43,6 +43,16 @@ "System.Security.Cryptography.Pkcs": "9.0.0" } }, + "Cake.Frosting.Git": { + "type": "Direct", + "requested": "[5.0.1, )", + "resolved": "5.0.1", + "contentHash": "85K0OGIEHj2oBvhITAJXRix7sUXLMLJmv5CNygH5LDIB3IubW5v7DxHZlyR16+gVgLygSx+DEVPzYLGqRRsR2Q==", + "dependencies": { + "Cake.Core": "5.0.0", + "LibGit2Sharp": "0.30.0" + } + }, "Cake.GitVersioning": { "type": "Direct", "requested": "[3.7.112, )", @@ -140,6 +150,19 @@ "System.Security.Cryptography.Pkcs": "9.0.0" } }, + "LibGit2Sharp": { + "type": "Transitive", + "resolved": "0.30.0", + "contentHash": "1cBg/7nz6q+yzvComhqQaTS4nbXloLp/x29ZgY/Rr82ohpkuaWT8ZYzsPLFvs1zmCh/4xQbVt5sQxazGblgRig==", + "dependencies": { + "LibGit2Sharp.NativeBinaries": "[2.0.322]" + } + }, + "LibGit2Sharp.NativeBinaries": { + "type": "Transitive", + "resolved": "2.0.322", + "contentHash": "EWQaEevzc8uS1ZGS6T83jqUPBY//2WUSCHwbZHBoHOjlfSehqr30nm/VAhJPzjam/69sv7nlLKVcho9t2XuR/Q==" + }, "Microsoft.Bcl.TimeProvider": { "type": "Transitive", "resolved": "8.0.1",