Skip to content
Draft
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
6 changes: 6 additions & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
<PackageVersion Include="Microsoft.CSharp" Version="4.7.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging" Version="10.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.0" />
<PackageVersion Include="Microsoft.IO.Redist" Version="6.1.3" />
<PackageVersion Include="Serilog" Version="4.2.0" />
<PackageVersion Include="Serilog.Extensions.Logging" Version="9.0.0" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="Serilog.Sinks.File" Version="6.0.0" />
<PackageVersion Include="Polly" Version="8.6.4" />
<PackageVersion Include="Roslynator.Analyzers" Version="4.14.1" />
<PackageVersion Include="Roslynator.Formatting.Analyzers" Version="4.14.1" />
Expand Down
41 changes: 35 additions & 6 deletions src/GitVersion.App/CliHost.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,64 @@
using System.IO.Abstractions;
using GitVersion;

Check warning on line 2 in src/GitVersion.App/CliHost.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Using directive is unnecessary.

Check warning on line 2 in src/GitVersion.App/CliHost.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Using directive is unnecessary.

Check warning on line 2 in src/GitVersion.App/CliHost.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Using directive is unnecessary.

Check warning on line 2 in src/GitVersion.App/CliHost.cs

View workflow job for this annotation

GitHub Actions / DotNet Format

Using directive is unnecessary.
using GitVersion.Agents;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Output;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Serilog;

namespace GitVersion;

internal static class CliHost
{
internal static HostApplicationBuilder CreateCliHostBuilder(string[] args)
{
// Parse arguments early to configure logging
var tempServices = new ServiceCollection();
tempServices.AddSingleton<IArgumentParser, ArgumentParser>();
var tempProvider = tempServices.BuildServiceProvider();
var argumentParser = tempProvider.GetRequiredService<IArgumentParser>();
var arguments = argumentParser.ParseArguments(args);
var gitVersionOptions = arguments.ToOptions();

var builder = Host.CreateApplicationBuilder(args);

// Configure Serilog based on parsed arguments
ConfigureSerilog(builder, gitVersionOptions);

builder.Services.AddModule(new GitVersionCoreModule());
builder.Services.AddModule(new GitVersionLibGit2SharpModule());
builder.Services.AddModule(new GitVersionBuildAgentsModule());
builder.Services.AddModule(new GitVersionConfigurationModule());
builder.Services.AddModule(new GitVersionOutputModule());
builder.Services.AddModule(new GitVersionAppModule());

builder.Services.AddSingleton(sp =>
{
var arguments = sp.GetRequiredService<IArgumentParser>().ParseArguments(args);
var gitVersionOptions = arguments.ToOptions();
return Options.Create(gitVersionOptions);
});
builder.Services.AddSingleton(sp => Options.Create(gitVersionOptions));

builder.Services.AddSingleton<GitVersionApp>();

return builder;
}

private static void ConfigureSerilog(HostApplicationBuilder builder, GitVersionOptions options)
{
var loggerConfiguration = LoggingModule.CreateLoggerConfiguration(
options.Verbosity,
options.Output.Contains(OutputType.BuildServer) || options.LogFilePath == "console",
options.LogFilePath != null && options.LogFilePath != "console" ? options.LogFilePath : null,
new FileSystem()
);

Serilog.Log.Logger = loggerConfiguration.CreateLogger();

builder.Services.AddLogging(loggingBuilder =>
{
loggingBuilder.ClearProviders();
loggingBuilder.AddSerilog(Serilog.Log.Logger, dispose: true);
});
}
}
1 change: 1 addition & 0 deletions src/GitVersion.App/GitVersion.App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.Hosting" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" />
<PackageReference Include="Microsoft.Extensions.Logging" />
</ItemGroup>

<ItemGroup>
Expand Down
12 changes: 6 additions & 6 deletions src/GitVersion.Configuration/ConfigurationFileLocator.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System.IO.Abstractions;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace GitVersion.Configuration;

internal class ConfigurationFileLocator(
IFileSystem fileSystem,
ILog log,
ILogger<ConfigurationFileLocator> logger,
IOptions<GitVersionOptions> options)
: IConfigurationFileLocator
{
Expand All @@ -26,7 +26,7 @@ internal class ConfigurationFileLocator(
];

private readonly IFileSystem fileSystem = fileSystem.NotNull();
private readonly ILog log = log.NotNull();
private readonly ILogger<ConfigurationFileLocator> logger = log.NotNull();
private readonly IOptions<GitVersionOptions> options = options.NotNull();

private string? ConfigurationFile => options.Value.ConfigurationInfo.ConfigurationFile;
Expand All @@ -43,7 +43,7 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
var customConfigurationFile = GetCustomConfigurationFilePathIfEligable(directoryPath);
if (!string.IsNullOrWhiteSpace(customConfigurationFile))
{
this.log.Info($"Found configuration file at '{customConfigurationFile}'");
this.logger.LogInformation($"Found configuration file at '{customConfigurationFile}'");
return customConfigurationFile;
}

Expand All @@ -55,10 +55,10 @@ public void Verify(string? workingDirectory, string? projectRootDirectory)
var files = fileSystem.Directory.GetFiles(directoryPath);
foreach (var fileName in this.SupportedConfigFileNames)
{
this.log.Debug($"Trying to find configuration file {fileName} at '{directoryPath}'");
this.logger.LogDebug($"Trying to find configuration file {fileName} at '{directoryPath}'");
var matchingFile = files.FirstOrDefault(file => string.Equals(FileSystemHelper.Path.GetFileName(file), fileName, StringComparison.OrdinalIgnoreCase));
if (matchingFile == null) continue;
this.log.Info($"Found configuration file at '{matchingFile}'");
this.logger.LogInformation($"Found configuration file at '{matchingFile}'");
return matchingFile;
}

Expand Down
12 changes: 6 additions & 6 deletions src/GitVersion.Configuration/ConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.IO.Abstractions;
using GitVersion.Configuration.Workflows;
using GitVersion.Extensions;
using GitVersion.Logging;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using YamlDotNet.Core;

Expand All @@ -10,14 +10,14 @@ namespace GitVersion.Configuration;
internal class ConfigurationProvider(
IConfigurationFileLocator configFileLocator,
IFileSystem fileSystem,
ILog log,
ILogger<ConfigurationProvider> logger,
IConfigurationSerializer configurationSerializer,
IOptions<GitVersionOptions> options)
: IConfigurationProvider
{
private readonly IConfigurationFileLocator configFileLocator = configFileLocator.NotNull();
private readonly IFileSystem fileSystem = fileSystem.NotNull();
private readonly ILog log = log.NotNull();
private readonly ILogger<ConfigurationProvider> logger = log.NotNull();
private readonly IConfigurationSerializer configurationSerializer = configurationSerializer.NotNull();
private readonly IOptions<GitVersionOptions> options = options.NotNull();

Expand Down Expand Up @@ -77,17 +77,17 @@ private IGitVersionConfiguration ProvideConfiguration(string? configFile,
{
if (configFilePath == null)
{
this.log.Info("No configuration file found, using default configuration");
this.logger.LogInformation("No configuration file found, using default configuration");
return null;
}

if (!this.fileSystem.File.Exists(configFilePath))
{
this.log.Info($"Configuration file '{configFilePath}' not found");
this.logger.LogInformation($"Configuration file '{configFilePath}' not found");
return null;
}

this.log.Info($"Using configuration file '{configFilePath}'");
this.logger.LogInformation($"Using configuration file '{configFilePath}'");
var content = fileSystem.File.ReadAllText(configFilePath);
return configurationSerializer.Deserialize<Dictionary<object, object?>>(content);
}
Expand Down
19 changes: 10 additions & 9 deletions src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using GitVersion.Common;
using GitVersion.Extensions;
using GitVersion.Git;
using Microsoft.Extensions.Logging;
using GitVersion.Logging;

namespace GitVersion;

internal class BranchesContainingCommitFinder(IRepositoryStore repositoryStore, ILog log)
internal class BranchesContainingCommitFinder(IRepositoryStore repositoryStore, ILogger<BranchesContainingCommitFinder> logger)

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / ubuntu-24.04

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / ubuntu-24.04

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / macos-15

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / macos-15

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / windows-2025

Parameter 'logger' is unread.

Check warning on line 9 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / windows-2025

Parameter 'logger' is unread.
{
private readonly ILog log = log.NotNull();
private readonly ILogger<BranchesContainingCommitFinder> logger = log.NotNull();

Check failure on line 11 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / ubuntu-24.04

The name 'log' does not exist in the current context

Check failure on line 11 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / macos-15

The name 'log' does not exist in the current context

Check failure on line 11 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Analyze (csharp)

The name 'log' does not exist in the current context

Check failure on line 11 in src/GitVersion.Core/Core/BranchesContainingCommitFinder.cs

View workflow job for this annotation

GitHub Actions / Prepare / windows-2025

The name 'log' does not exist in the current context
private readonly IRepositoryStore repositoryStore = repositoryStore.NotNull();

public IEnumerable<IBranch> GetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch>? branches = null, bool onlyTrackedBranches = false)
Expand All @@ -23,16 +24,16 @@

private IEnumerable<IBranch> InnerGetBranchesContainingCommit(ICommit commit, IEnumerable<IBranch> branches, bool onlyTrackedBranches)
{
using (log.IndentLog($"Getting branches containing the commit '{commit.Id}'."))
using (logger.IndentLog($"Getting branches containing the commit '{commit.Id}'."))
{
var directBranchHasBeenFound = false;
log.Info("Trying to find direct branches.");
logger.LogInformation("Trying to find direct branches.");
// TODO: It looks wasteful looping through the branches twice. Can't these loops be merged somehow? @asbjornu
var branchList = branches.ToList();
foreach (var branch in branchList.Where(branch => BranchTipIsNullOrCommit(branch, commit) && !IncludeTrackedBranches(branch, onlyTrackedBranches)))
{
directBranchHasBeenFound = true;
log.Info($"Direct branch found: '{branch}'.");
logger.LogInformation($"Direct branch found: '{branch}'.");
yield return branch;
}

Expand All @@ -41,20 +42,20 @@
yield break;
}

log.Info($"No direct branches found, searching through {(onlyTrackedBranches ? "tracked" : "all")} branches.");
logger.LogInformation($"No direct branches found, searching through {(onlyTrackedBranches ? "tracked" : "all")} branches.");
foreach (var branch in branchList.Where(b => IncludeTrackedBranches(b, onlyTrackedBranches)))
{
log.Info($"Searching for commits reachable from '{branch}'.");
logger.LogInformation($"Searching for commits reachable from '{branch}'.");

var commits = this.repositoryStore.GetCommitsReacheableFrom(commit, branch);

if (!commits.Any())
{
log.Info($"The branch '{branch}' has no matching commits.");
logger.LogInformation($"The branch '{branch}' has no matching commits.");
continue;
}

log.Info($"The branch '{branch}' has a matching commit.");
logger.LogInformation($"The branch '{branch}' has a matching commit.");
yield return branch;
}
}
Expand Down
Loading
Loading