Skip to content

Remove dependency on "git" executable #4147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/GitVersion.App/GitVersionExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,13 @@ private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
var error = $"An unexpected error occurred:{PathHelper.NewLine}{exception}";
this.log.Error(error);

this.log.Info("Attempting to show the current git graph (please include in issue): ");
this.log.Info("Showing max of 100 commits");

try
{
GitExtensions.DumpGraph(gitVersionOptions.WorkingDirectory, mess => this.log.Info(mess), 100);
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
}
catch (Exception dumpGraphException)
{
this.log.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
this.log.Error($"Couldn't dump the git graph due to the following error: {dumpGraphException}");
}
return 1;
}
Expand Down Expand Up @@ -131,8 +128,7 @@ private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int e
var workingDirectory = gitVersionOptions.WorkingDirectory;
if (gitVersionOptions.Diag)
{
this.log.Info("Dumping commit graph: ");
GitExtensions.DumpGraph(workingDirectory, mess => this.log.Info(mess), 100);
GitExtensions.DumpGraphLog(logMessage => this.log.Info(logMessage));
}

if (!Directory.Exists(workingDirectory))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static ICommit CreateMockCommit()
commit.When.Returns(when.AddSeconds(1));
return commit;
}

public static IBranch CreateMockBranch(string name, params ICommit[] commits)
{
var branch = Substitute.For<IBranch>();
Expand All @@ -55,13 +56,13 @@ public static void DiscoverRepository(this IServiceProvider sp)

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

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

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

public static GitVersionVariables GetVersion(this RepositoryFixtureBase fixture, IGitVersionConfiguration? configuration = null,
IRepository? repository = null, string? commitId = null, bool onlyTrackedBranches = true, string? targetBranch = null)
Expand Down Expand Up @@ -173,4 +174,7 @@ private static IServiceProvider ConfigureServices(Action<IServiceCollection>? se
servicesOverrides?.Invoke(services);
return services.BuildServiceProvider();
}

private static void DumpGraph(string workingDirectory, Action<string>? writer = null, int? maxCommits = null)
=> GitTestExtensions.ExecuteGitCmd(GitExtensions.CreateGitLogArgs(maxCommits), workingDirectory, writer);
}
3 changes: 2 additions & 1 deletion src/GitVersion.Core/Core/GitVersionContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public GitVersionContext Create(GitVersionOptions gitVersionOptions)
currentBranch, gitVersionOptions.RepositoryInfo.CommitId, configuration.Ignore
);

if (currentCommit is null) throw new GitVersionException("No commits found on the current branch.");
if (currentCommit is null)
throw new GitVersionException("No commits found on the current branch.");

if (currentBranch.IsDetachedHead)
{
Expand Down
23 changes: 3 additions & 20 deletions src/GitVersion.Core/Extensions/GitExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
using GitVersion.Helpers;

namespace GitVersion.Extensions;

public static class GitExtensions
{
public static void DumpGraph(string workingDirectory, Action<string>? writer = null, int? maxCommits = null)
public static void DumpGraphLog(Action<string>? writer = null, int? maxCommits = null)
{
var output = new StringBuilder();
try
{
ProcessHelper.Run(
o => output.AppendLine(o),
e => output.AppendLineFormat("ERROR: {0}", e),
null,
"git",
CreateGitLogArgs(maxCommits),
workingDirectory);
}
catch (FileNotFoundException exception) when (exception.FileName == "git")
{
output.AppendLine("Could not execute 'git log' due to the following error:");
output.AppendLine(exception.ToString());
}

output.AppendLine($"Please run `git {CreateGitLogArgs(maxCommits)}` to see the git graph. This can help you troubleshoot any issues.");
if (writer != null)
{
writer(output.ToString());
Expand All @@ -36,6 +19,6 @@ public static void DumpGraph(string workingDirectory, Action<string>? writer = n
public static string CreateGitLogArgs(int? maxCommits)
{
var commits = maxCommits != null ? $" -n {maxCommits}" : null;
return $@"log --graph --format=""%h %cr %d"" --decorate --date=relative --all --remotes=*{commits}";
return $"""log --graph --format="%h %cr %d" --decorate --date=relative --all --remotes=*{commits}""";
}
}
2 changes: 1 addition & 1 deletion src/GitVersion.Core/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ static GitVersion.Extensions.EnumerableExtensions.AddRange<T>(this System.Collec
static GitVersion.Extensions.EnumerableExtensions.OnlyOrDefault<T>(this System.Collections.Generic.IEnumerable<T>! source) -> T?
static GitVersion.Extensions.EnumerableExtensions.SingleOfType<T>(this System.Collections.IEnumerable! source) -> T
static GitVersion.Extensions.GitExtensions.CreateGitLogArgs(int? maxCommits) -> string!
static GitVersion.Extensions.GitExtensions.DumpGraph(string! workingDirectory, System.Action<string!>? writer = null, int? maxCommits = null) -> void
static GitVersion.Extensions.GitExtensions.DumpGraphLog(System.Action<string!>? writer = null, int? maxCommits = null) -> void
static GitVersion.Extensions.IncrementStrategyExtensions.ToVersionField(this GitVersion.IncrementStrategy strategy) -> GitVersion.VersionField
static GitVersion.Extensions.ReadEmbeddedResourceExtensions.ReadAsStringFromEmbeddedResource(this string! resourceName, System.Reflection.Assembly! assembly) -> string!
static GitVersion.Extensions.ReadEmbeddedResourceExtensions.ReadAsStringFromEmbeddedResource<T>(this string! resourceName) -> string!
Expand Down
6 changes: 3 additions & 3 deletions src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Remove(string branch)
SequenceDiagram.Destroy(branch);
}

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

public string MakeATaggedCommit(string tag)
{
Expand Down Expand Up @@ -165,8 +165,8 @@ protected static Repository CreateNewRepository(string path, string branchName,
/// </summary>
public void MakeShallow()
{
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} pull --depth 1");
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} gc --prune=all");
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} pull --depth 1", ".");
GitTestExtensions.ExecuteGitCmd($"-C {RepositoryPath} gc --prune=all", ".");
}

public void Fetch(string remote, FetchOptions? options = null)
Expand Down
4 changes: 2 additions & 2 deletions src/GitVersion.Testing/GitTestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static Commit CreatePullRequestRef(this IRepository repository, string fr
return commit;
}

public static void ExecuteGitCmd(string gitCmd, Action<string>? writer = null)
public static void ExecuteGitCmd(string gitCmd, string workingDirectory, Action<string>? writer = null)
{
var output = new StringBuilder();
try
Expand All @@ -80,7 +80,7 @@ public static void ExecuteGitCmd(string gitCmd, Action<string>? writer = null)
null,
"git",
gitCmd,
".");
workingDirectory);
}
catch (FileNotFoundException exception) when (exception.FileName == "git")
{
Expand Down
3 changes: 1 addition & 2 deletions src/GitVersion.Testing/GitVersion.Testing.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
Expand All @@ -7,6 +7,5 @@
</ItemGroup>
<ItemGroup>
<Compile Include="..\GitVersion.Core\Helpers\PathHelper.cs" Link="Helpers\PathHelper.cs" />
<Compile Include="..\GitVersion.Core\Helpers\ProcessHelper.cs" Link="Helpers\ProcessHelper.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace GitVersion.Helpers;
namespace GitVersion.Testing;

internal static class ProcessHelper
public static class ProcessHelper
{
private static readonly object LockObject = new();

Expand Down
2 changes: 2 additions & 0 deletions src/mark-shipped.ps1
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#! /usr/bin/env pwsh

[CmdletBinding(PositionalBinding = $false)]
param ()

Expand Down