Skip to content

Commit be1656f

Browse files
committed
code cleanup
1 parent 4ade32f commit be1656f

File tree

7 files changed

+52
-79
lines changed

7 files changed

+52
-79
lines changed

src/GitVersionCore.Tests/GitToolsTestingExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using GitVersion.Logging;
99
using Microsoft.Extensions.Options;
1010
using GitVersion.Extensions;
11+
using GitVersion.VersionCalculation;
1112
using GitVersionCore.Tests.Helpers;
1213
using Microsoft.Extensions.DependencyInjection;
1314

@@ -29,10 +30,10 @@ public static VersionVariables GetVersion(this RepositoryFixtureBase fixture, Co
2930

3031
var log = sp.GetService<ILog>();
3132
var variableProvider = sp.GetService<IVariableProvider>();
32-
var versionFinder = sp.GetService<IGitVersionFinder>();
33+
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
3334

3435
var gitVersionContext = new GitVersionContext(repository ?? fixture.Repository, log, targetBranch, configuration, onlyTrackedBranches, commitId);
35-
var executeGitVersion = versionFinder.FindVersion(gitVersionContext);
36+
var executeGitVersion = nextVersionCalculator.FindVersion(gitVersionContext);
3637
var variables = variableProvider.GetVariablesFor(executeGitVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
3738

3839
try

src/GitVersionCore.Tests/VersionCalculation/NextVersionCalculatorTests.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ public void ShouldIncrementVersionBasedOnConfig()
2828
services.AddSingleton<IMetaDataCalculator>(new TestMetaDataCalculator(semanticVersionBuildMetaData));
2929
});
3030

31-
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
31+
var nextVersionCalculator = sp.GetService<INextVersionCalculator>() as NextVersionCalculator;
32+
nextVersionCalculator.ShouldNotBeNull();
3233

3334
var context = new GitVersionContextBuilder().WithConfig(new Config()).Build();
3435

35-
var version = nextVersionCalculator.FindVersion(context);
36+
var version = nextVersionCalculator.FindVersionInternal(context);
3637

3738
version.ToString().ShouldBe("1.0.1");
3839
}
@@ -47,11 +48,12 @@ public void DoesNotIncrementWhenBaseVersionSaysNotTo()
4748
services.AddSingleton<IMetaDataCalculator>(new TestMetaDataCalculator(semanticVersionBuildMetaData));
4849
});
4950

50-
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
51+
var nextVersionCalculator = sp.GetService<INextVersionCalculator>() as NextVersionCalculator;
5152

53+
nextVersionCalculator.ShouldNotBeNull();
5254
var context = new GitVersionContextBuilder().WithConfig(new Config()).Build();
5355

54-
var version = nextVersionCalculator.FindVersion(context);
56+
var version = nextVersionCalculator.FindVersionInternal(context);
5557

5658
version.ToString().ShouldBe("1.0.0");
5759
}
@@ -67,13 +69,14 @@ public void AppliesBranchPreReleaseTag()
6769
services.AddSingleton<IMetaDataCalculator>(new TestMetaDataCalculator(semanticVersionBuildMetaData));
6870
});
6971

70-
var nextVersionCalculator = sp.GetService<INextVersionCalculator>();
72+
var nextVersionCalculator = sp.GetService<INextVersionCalculator>() as NextVersionCalculator;
73+
nextVersionCalculator.ShouldNotBeNull();
7174

7275
var context = new GitVersionContextBuilder()
7376
.WithDevelopBranch()
7477
.Build();
7578

76-
var version = nextVersionCalculator.FindVersion(context);
79+
var version = nextVersionCalculator.FindVersionInternal(context);
7780

7881
version.ToString("f").ShouldBe("1.0.0-alpha.1+2");
7982
}

src/GitVersionCore/GitVersionCalculator.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using GitVersion.Logging;
66
using Microsoft.Extensions.Options;
77
using GitVersion.Extensions;
8+
using GitVersion.VersionCalculation;
89

910
namespace GitVersion
1011
{
@@ -14,21 +15,21 @@ public class GitVersionCalculator : IGitVersionCalculator
1415
private readonly IConfigProvider configProvider;
1516
private readonly IBuildServerResolver buildServerResolver;
1617
private readonly IGitVersionCache gitVersionCache;
17-
private readonly IGitVersionFinder gitVersionFinder;
18+
private readonly INextVersionCalculator nextVersionCalculator;
1819
private readonly IGitPreparer gitPreparer;
1920
private readonly IVariableProvider variableProvider;
2021
private readonly IOptions<Arguments> options;
2122
private readonly IGitVersionCacheKeyFactory cacheKeyFactory;
2223

2324
public GitVersionCalculator(ILog log, IConfigProvider configProvider, IBuildServerResolver buildServerResolver,
24-
IGitVersionCache gitVersionCache, IGitVersionFinder gitVersionFinder, IGitPreparer gitPreparer, IVariableProvider variableProvider,
25+
IGitVersionCache gitVersionCache, INextVersionCalculator nextVersionCalculator, IGitPreparer gitPreparer, IVariableProvider variableProvider,
2526
IOptions<Arguments> options, IGitVersionCacheKeyFactory cacheKeyFactory)
2627
{
2728
this.log = log ?? throw new ArgumentNullException(nameof(log));
2829
this.configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
2930
this.buildServerResolver = buildServerResolver ?? throw new ArgumentNullException(nameof(buildServerResolver));
3031
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
31-
this.gitVersionFinder = gitVersionFinder ?? throw new ArgumentNullException(nameof(gitVersionFinder));
32+
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
3233
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
3334
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
3435
this.options = options ?? throw new ArgumentNullException(nameof(options));
@@ -106,7 +107,7 @@ private VersionVariables ExecuteInternal(string targetBranch, string commitId, C
106107
return gitPreparer.GetDotGitDirectory().WithRepository(repo =>
107108
{
108109
var gitVersionContext = new GitVersionContext(repo, log, targetBranch, configuration, commitId: commitId);
109-
var semanticVersion = gitVersionFinder.FindVersion(gitVersionContext);
110+
var semanticVersion = nextVersionCalculator.FindVersion(gitVersionContext);
110111

111112
return variableProvider.GetVariablesFor(semanticVersion, gitVersionContext.Configuration, gitVersionContext.IsCurrentCommitTagged);
112113
});

src/GitVersionCore/GitVersionCoreModule.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public void RegisterTypes(IServiceCollection services)
2424

2525
services.AddSingleton<IConfigProvider, ConfigProvider>();
2626
services.AddSingleton<IVariableProvider, VariableProvider>();
27-
services.AddSingleton<IGitVersionFinder, GitVersionFinder>();
2827

2928
services.AddSingleton<IMetaDataCalculator, MetaDataCalculator>();
3029
services.AddSingleton<IBaseVersionCalculator, BaseVersionCalculator>();

src/GitVersionCore/GitVersionFinder.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/GitVersionCore/IGitVersionFinder.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/GitVersionCore/VersionCalculation/NextVersionCalculator.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Text.RegularExpressions;
45

56
using GitVersion.VersionCalculation.BaseVersionCalculators;
67
using GitVersion.VersioningModes;
78
using GitVersion.Configuration;
9+
using GitVersion.Exceptions;
810
using GitVersion.Logging;
911
using GitVersion.Extensions;
1012

@@ -27,6 +29,39 @@ public NextVersionCalculator(ILog log, IMetaDataCalculator metaDataCalculator, I
2729
}
2830

2931
public SemanticVersion FindVersion(GitVersionContext context)
32+
{
33+
log.Info($"Running against branch: {context.CurrentBranch.FriendlyName} ({(context.CurrentCommit == null ? "-" : context.CurrentCommit.Sha)})");
34+
if (context.IsCurrentCommitTagged)
35+
{
36+
log.Info($"Current commit is tagged with version {context.CurrentCommitTaggedVersion}, " +
37+
"version calculation is for metadata only.");
38+
}
39+
EnsureHeadIsNotDetached(context);
40+
41+
var filePath = Path.Combine(context.Repository.GetRepositoryDirectory(), "NextVersion.txt");
42+
if (File.Exists(filePath))
43+
{
44+
throw new WarningException("NextVersion.txt has been deprecated. See http://gitversion.readthedocs.org/en/latest/configuration/ for replacement");
45+
}
46+
47+
return FindVersionInternal(context);
48+
}
49+
50+
private static void EnsureHeadIsNotDetached(GitVersionContext context)
51+
{
52+
if (!context.CurrentBranch.IsDetachedHead())
53+
{
54+
return;
55+
}
56+
57+
var message = string.Format(
58+
"It looks like the branch being examined is a detached Head pointing to commit '{0}'. " +
59+
"Without a proper branch name GitVersion cannot determine the build version.",
60+
context.CurrentCommit.Id.ToString(7));
61+
throw new WarningException(message);
62+
}
63+
64+
public SemanticVersion FindVersionInternal(GitVersionContext context)
3065
{
3166
SemanticVersion taggedSemanticVersion = null;
3267

0 commit comments

Comments
 (0)