Skip to content

Commit ad76a5f

Browse files
authored
Merge pull request #4147 from arturcic/main
Remove dependency on "git" cmdline
2 parents 170d3d1 + 73c2433 commit ad76a5f

File tree

10 files changed

+26
-41
lines changed

10 files changed

+26
-41
lines changed

src/GitVersion.App/GitVersionExecutor.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,13 @@ private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
8282
var error = $"An unexpected error occurred:{PathHelper.NewLine}{exception}";
8383
this.log.Error(error);
8484

85-
this.log.Info("Attempting to show the current git graph (please include in issue): ");
86-
this.log.Info("Showing max of 100 commits");
87-
8885
try
8986
{
90-
GitExtensions.DumpGraph(gitVersionOptions.WorkingDirectory, mess => this.log.Info(mess), 100);
87+
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
9188
}
9289
catch (Exception dumpGraphException)
9390
{
94-
this.log.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
91+
this.log.Error($"Couldn't dump the git graph due to the following error: {dumpGraphException}");
9592
}
9693
return 1;
9794
}
@@ -131,8 +128,7 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
131128
var workingDirectory = gitVersionOptions.WorkingDirectory;
132129
if (gitVersionOptions.Diag)
133130
{
134-
this.log.Info("Dumping commit graph: ");
135-
GitExtensions.DumpGraph(workingDirectory, mess => this.log.Info(mess), 100);
131+
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
136132
}
137133

138134
if (!Directory.Exists(workingDirectory))

src/GitVersion.Core.Tests/Extensions/GitToolsTestingExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static ICommit CreateMockCommit()
3030
commit.When.Returns(when.AddSeconds(1));
3131
return commit;
3232
}
33+
3334
public static IBranch CreateMockBranch(string name, params ICommit[] commits)
3435
{
3536
var branch = Substitute.For<IBranch>();
@@ -55,13 +56,13 @@ public static void DiscoverRepository(this IServiceProvider sp)
5556

5657
public static IBranch FindBranch(this IGitRepository repository, string branchName)
5758
=> repository.Branches.FirstOrDefault(branch => branch.Name.WithoutOrigin == branchName)
58-
?? throw new GitVersionException($"Branch {branchName} not found");
59+
?? throw new GitVersionException($"Branch {branchName} not found");
5960

6061
public static void DumpGraph(this IGitRepository repository, Action<string>? writer = null, int? maxCommits = null)
61-
=> GitExtensions.DumpGraph(repository.Path, writer, maxCommits);
62+
=> DumpGraph(repository.Path, writer, maxCommits);
6263

6364
public static void DumpGraph(this IRepository repository, Action<string>? writer = null, int? maxCommits = null)
64-
=> GitExtensions.DumpGraph(repository.ToGitRepository().Path, writer, maxCommits);
65+
=> DumpGraph(repository.ToGitRepository().Path, writer, maxCommits);
6566

6667
public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture, IGitVersionConfiguration? configuration = null,
6768
IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
@@ -173,4 +174,7 @@ private static IServiceProvider ConfigureServices(Action<IServiceCollection>? se
173174
servicesOverrides?.Invoke(services);
174175
return services.BuildServiceProvider();
175176
}
177+
178+
private static void DumpGraph(string workingDirectory, Action<string>? writer = null, int? maxCommits = null)
179+
=> GitTestExtensions.ExecuteGitCmd(GitExtensions.CreateGitLogArgs(maxCommits), workingDirectory, writer);
176180
}

src/GitVersion.Core/Core/GitVersionContextFactory.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
2929
currentBranch, gitVersionOptions.RepositoryInfo.CommitId, configuration.Ignore
3030
);
3131

32-
if (currentCommit is null) throw new GitVersionException("No commits found on the current branch.");
32+
if (currentCommit is null)
33+
throw new GitVersionException("No commits found on the current branch.");
3334

3435
if (currentBranch.IsDetachedHead)
3536
{
Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,11 @@
1-
using GitVersion.Helpers;
2-
31
namespace GitVersion.Extensions;
42

53
public static class GitExtensions
64
{
7-
public static void DumpGraph(string workingDirectory, Action<string>? writer = null, int? maxCommits = null)
5+
public static void DumpGraphLog(Action<string>? writer = null, int? maxCommits = null)
86
{
97
var output = new StringBuilder();
10-
try
11-
{
12-
ProcessHelper.Run(
13-
o => output.AppendLine(o),
14-
e => output.AppendLineFormat("ERROR: {0}", e),
15-
null,
16-
"git",
17-
CreateGitLogArgs(maxCommits),
18-
workingDirectory);
19-
}
20-
catch (FileNotFoundException exception) when (exception.FileName == "git")
21-
{
22-
output.AppendLine("Could not execute 'git log' due to the following error:");
23-
output.AppendLine(exception.ToString());
24-
}
25-
8+
output.AppendLine($"Please run `git {CreateGitLogArgs(maxCommits)}` to see the git graph. This can help you troubleshoot any issues.");
269
if (writer != null)
2710
{
2811
writer(output.ToString());
@@ -36,6 +19,6 @@ public static void DumpGraph(string workingDirectory, Action<string>? writer = n
3619
public static string CreateGitLogArgs(int? maxCommits)
3720
{
3821
var commits = maxCommits != null ? $" -n {maxCommits}" : null;
39-
return $@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*{commits}";
22+
return $"""log --graph --format="%h %cr %d" --decorate --date=relative --all --remotes=*{commits}""";
4023
}
4124
}

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ static GitVersion.Extensions.EnumerableExtensions.AddRange<T>(this System.Collec
753753
static GitVersion.Extensions.EnumerableExtensions.OnlyOrDefault<T>(this System.Collections.Generic.IEnumerable<T>! source) -> T?
754754
static GitVersion.Extensions.EnumerableExtensions.SingleOfType<T>(this System.Collections.IEnumerable! source) -> T
755755
static GitVersion.Extensions.GitExtensions.CreateGitLogArgs(int? maxCommits) -> string!
756-
static GitVersion.Extensions.GitExtensions.DumpGraph(string! workingDirectory, System.Action<string!>? writer = null, int? maxCommits = null) -> void
756+
static GitVersion.Extensions.GitExtensions.DumpGraphLog(System.Action<string!>? writer = null, int? maxCommits = null) -> void
757757
static GitVersion.Extensions.IncrementStrategyExtensions.ToVersionField(this GitVersion.IncrementStrategy strategy) -> GitVersion.VersionField
758758
static GitVersion.Extensions.ReadEmbeddedResourceExtensions.ReadAsStringFromEmbeddedResource(this string! resourceName, System.Reflection.Assembly! assembly) -> string!
759759
static GitVersion.Extensions.ReadEmbeddedResourceExtensions.ReadAsStringFromEmbeddedResource<T>(this string! resourceName) -> string!

src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public void Remove(string branch)
7373
SequenceDiagram.Destroy(branch);
7474
}
7575

76-
public static void Init(string path, string branchName = "main") => GitTestExtensions.ExecuteGitCmd($"init {path} -b {branchName}");
76+
public static void Init(string path, string branchName = "main") => GitTestExtensions.ExecuteGitCmd($"init {path} -b {branchName}", ".");
7777

7878
public string MakeATaggedCommit(string tag)
7979
{
@@ -165,8 +165,8 @@ protected static Repository CreateNewRepository(string path, string branchName,
165165
/// </summary>
166166
public void MakeShallow()
167167
{
168-
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} pull --depth 1");
169-
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} gc --prune=all");
168+
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} pull --depth 1", ".");
169+
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} gc --prune=all", ".");
170170
}
171171

172172
public void Fetch(string remote, FetchOptions? options = null)

src/GitVersion.Testing/GitTestExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static Commit CreatePullRequestRef(this IRepository repository, string fr
6969
return commit;
7070
}
7171

72-
public static void ExecuteGitCmd(string gitCmd, Action<string>? writer = null)
72+
public static void ExecuteGitCmd(string gitCmd, string workingDirectory, Action<string>? writer = null)
7373
{
7474
var output = new StringBuilder();
7575
try
@@ -80,7 +80,7 @@ public static void ExecuteGitCmd(string gitCmd, Action<string>? writer = null)
8080
null,
8181
"git",
8282
gitCmd,
83-
".");
83+
workingDirectory);
8484
}
8585
catch (FileNotFoundException exception) when (exception.FileName == "git")
8686
{
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
44
</PropertyGroup>
@@ -7,6 +7,5 @@
77
</ItemGroup>
88
<ItemGroup>
99
<Compile Include="..\GitVersion.Core\Helpers\PathHelper.cs" Link="Helpers\PathHelper.cs" />
10-
<Compile Include="..\GitVersion.Core\Helpers\ProcessHelper.cs" Link="Helpers\ProcessHelper.cs" />
1110
</ItemGroup>
1211
</Project>

src/GitVersion.Core/Helpers/ProcessHelper.cs renamed to src/GitVersion.Testing/Helpers/ProcessHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using System.ComponentModel;
22
using System.Runtime.InteropServices;
33

4-
namespace GitVersion.Helpers;
4+
namespace GitVersion.Testing;
55

6-
internal static class ProcessHelper
6+
public static class ProcessHelper
77
{
88
private static readonly object LockObject = new();
99

src/mark-shipped.ps1

100644100755
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#! /usr/bin/env pwsh
2+
13
[CmdletBinding(PositionalBinding = $false)]
24
param ()
35

0 commit comments

Comments
 (0)