Skip to content

Commit

Permalink
refactor: Use package Cake.Frosting.Git to determine repository infor…
Browse files Browse the repository at this point in the history
…mation instead of calling the git commandline

Pull-Request: #151
  • Loading branch information
ap0llo authored Dec 22, 2024
1 parent 718a316 commit 40478bc
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
2 changes: 2 additions & 0 deletions build/Build.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<None Include="..\azure-pipelines.yml" Link="azure-pipelines.yml" />
<None Include="..\.github\workflows\build.yml" Link="build.yml" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,6 +18,7 @@
<PackageReference Include="Cake.GitVersioning" Version="3.7.112" />
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
<PackageReference Include="Cake.Frosting.Git" Version="5.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions build/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )",
Expand Down Expand Up @@ -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",
Expand Down
23 changes: 23 additions & 0 deletions src/SharedBuild.Test/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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, )"
Expand Down
1 change: 1 addition & 0 deletions src/SharedBuild/Grynwald.SharedBuild.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Cake.FileHelpers" Version="7.0.0" />
<PackageReference Include="Mono.Cecil" Version="0.11.6" />
<PackageReference Include="Cake.BuildSystems.Module" Version="7.1.0" />
<PackageReference Include="Cake.Frosting.Git" Version="5.0.1" />
</ItemGroup>

<!-- Cake.GitHub (currently pulled in via git submodules instead of NuGet) -->
Expand Down
30 changes: 4 additions & 26 deletions src/SharedBuild/_Context/_Default/DefaultGitContext.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Git;

namespace Grynwald.SharedBuild;

public class DefaultGitContext(DefaultBuildContext context) : IGitContext
{
private readonly DefaultBuildContext m_Context = context ?? throw new ArgumentNullException(nameof(context));


/// <inheritdoc />
public virtual string BranchName
{
Expand All @@ -27,27 +26,25 @@ public virtual string BranchName
}
else
{
return StartGit("rev-parse", "--abbrev-ref", "HEAD").Trim();
return m_Context.GitBranchCurrent(context.RootDirectory).FriendlyName;
}

}
}

/// <inheritdoc />
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;

/// <inheritdoc />
public virtual string RemoteUrl => StartGit("remote", "get-url", "origin").Trim();
public virtual string RemoteUrl => m_Context.GitRemote(context.RootDirectory, "origin").Url;

/// <inheritdoc />
public virtual bool IsMainBranch => BranchName.Equals("main", StringComparison.OrdinalIgnoreCase) || BranchName.Equals("master", StringComparison.OrdinalIgnoreCase);

/// <inheritdoc />
public virtual bool IsReleaseBranch => BranchName.StartsWith("release/", StringComparison.OrdinalIgnoreCase);


/// <inheritdoc />
public virtual void PrintToLog(ICakeLog log)
{
Expand All @@ -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;
}
}
23 changes: 23 additions & 0 deletions src/SharedBuild/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )",
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 40478bc

Please sign in to comment.