Skip to content

Commit 72ad9dd

Browse files
committed
improved logging information when finding the new version
1 parent ea00a28 commit 72ad9dd

File tree

12 files changed

+112
-99
lines changed

12 files changed

+112
-99
lines changed

src/GitVersion.Core/Core/MergeBaseFinder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ public MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository gitRepos
5252

5353
if (findMergeBase == null)
5454
{
55-
this.log.Info($"No merge base of {first}' and '{second} could be found.");
55+
this.log.Info($"No merge base of '{first}' and '{second}' could be found.");
5656
return null;
5757
}
5858

5959
// Store in cache.
6060
this.mergeBaseCache.Add(key, findMergeBase);
6161

62-
this.log.Info($"Merge base of {first}' and '{second} is {findMergeBase}");
62+
this.log.Info($"Merge base of '{first}' and '{second}' is '{findMergeBase}'");
6363
return findMergeBase;
6464
}
6565
}
@@ -70,7 +70,7 @@ public MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository gitRepos
7070
if (findMergeBase == null)
7171
return null;
7272

73-
this.log.Info($"Found merge base of {findMergeBase}");
73+
this.log.Info($"Found merge base of '{findMergeBase}'");
7474

7575
// We do not want to include merge base commits which got forward merged into the other branch
7676
ICommit? forwardMerge;
@@ -103,7 +103,7 @@ public MergeBaseFinder(IRepositoryStore repositoryStore, IGitRepository gitRepos
103103

104104
findMergeBase = mergeBase;
105105
commitToFindCommonBase = second;
106-
this.log.Info($"Merge base was due to a forward merge, next merge base is {findMergeBase}");
106+
this.log.Info($"next merge base --> {findMergeBase}");
107107
} while (forwardMerge != null);
108108

109109
return findMergeBase;

src/GitVersion.Core/Core/RepositoryStore.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,9 @@ public IReadOnlyList<SemanticVersionWithTag> GetTaggedSemanticVersions(string? t
283283
return this.taggedSemanticVersionsCache;
284284
}
285285

286-
using (this.log.IndentLog($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}"))
287-
{
288-
this.taggedSemanticVersionsCache = GetTaggedSemanticVersionsInternal().ToList();
289-
return this.taggedSemanticVersionsCache;
290-
}
286+
this.log.Info($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");
287+
this.taggedSemanticVersionsCache = GetTaggedSemanticVersionsInternal().ToList();
288+
return this.taggedSemanticVersionsCache;
291289

292290
IEnumerable<SemanticVersionWithTag> GetTaggedSemanticVersionsInternal()
293291
{

src/GitVersion.Core/Git/SemanticVersion.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class SemanticVersion : IFormattable, IComparable<SemanticVersion>, IEqua
3131
public bool IsLabeledWith(string value) => PreReleaseTag.HasTag() && PreReleaseTag.Name.IsEquivalentTo(value);
3232

3333
public bool IsMatchForBranchSpecificLabel(string? value)
34-
=> PreReleaseTag.Name == string.Empty || value is null || IsLabeledWith(value);
34+
=> PreReleaseTag.Name.Length == 0 || value is null || IsLabeledWith(value);
3535

3636
public SemanticVersion(long major = 0, long minor = 0, long patch = 0)
3737
{
@@ -292,7 +292,7 @@ public string ToString(string? format, IFormatProvider? formatProvider)
292292
case "s":
293293
return this.PreReleaseTag.HasTag() ? $"{ToString("j")}-{this.PreReleaseTag}" : ToString("j");
294294
case "t":
295-
return this.PreReleaseTag.HasTag() ? $"{ToString("j")}-{this.PreReleaseTag.ToString("t")}" : ToString("j");
295+
return this.PreReleaseTag.HasTag() ? $"{ToString("j")}-{this.PreReleaseTag:t}" : ToString("j");
296296
case "f":
297297
{
298298
var buildMetadata = this.BuildMetaData.ToString();

src/GitVersion.Core/Logging/Abstractions/ILog.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ public interface ILog
66
void Write(Verbosity verbosity, LogLevel level, string format, params object[] args);
77
IDisposable IndentLog(string operationDescription);
88
void AddLogAppender(ILogAppender logAppender);
9+
void Separator();
910
}

src/GitVersion.Core/Logging/Log.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,19 @@ public void Write(Verbosity verbosity, LogLevel level, string format, params obj
4343
public IDisposable IndentLog(string operationDescription)
4444
{
4545
var start = DateTime.Now;
46-
Write(Verbosity.Normal, LogLevel.Info, $"Begin: {operationDescription}");
46+
Write(Verbosity.Normal, LogLevel.Info, $"-< Begin: {operationDescription} >-");
4747
this.indent += " ";
4848

4949
return Disposable.Create(() =>
5050
{
5151
var length = this.indent.Length - 2;
52-
this.indent = length > 0 ? this.indent[..length] : this.indent;
53-
Write(Verbosity.Normal, LogLevel.Info, string.Format(CultureInfo.InvariantCulture, "End: {0} (Took: {1:N}ms)", operationDescription, DateTime.Now.Subtract(start).TotalMilliseconds));
52+
this.indent = length > 0 ? this.indent[..length] : "";
53+
Write(Verbosity.Normal, LogLevel.Info, string.Format(CultureInfo.InvariantCulture, "-< End: {0} (Took: {1:N}ms) >-", operationDescription, DateTime.Now.Subtract(start).TotalMilliseconds));
5454
});
5555
}
5656

57+
public void Separator() => Write(Verbosity.Normal, LogLevel.Info, "-------------------------------------------------------");
58+
5759
public void AddLogAppender(ILogAppender logAppender) => this.appenders = this.appenders.Concat(new[] { logAppender });
5860

5961
public override string ToString() => this.sb.ToString();

src/GitVersion.Core/Logging/NullLog.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,9 @@ public void AddLogAppender(ILogAppender logAppender)
1414
{
1515
}
1616

17+
public void Separator()
18+
{
19+
}
20+
1721
public string? Indent { get; set; }
1822
}

src/GitVersion.Core/PublicAPI.Unshipped.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ GitVersion.Logging.IConsole.WriteLine(string? msg) -> void
417417
GitVersion.Logging.ILog
418418
GitVersion.Logging.ILog.AddLogAppender(GitVersion.Logging.ILogAppender! logAppender) -> void
419419
GitVersion.Logging.ILog.IndentLog(string! operationDescription) -> System.IDisposable!
420+
GitVersion.Logging.ILog.Separator() -> void
420421
GitVersion.Logging.ILog.Verbosity.get -> GitVersion.Logging.Verbosity
421422
GitVersion.Logging.ILog.Verbosity.set -> void
422423
GitVersion.Logging.ILog.Write(GitVersion.Logging.Verbosity verbosity, GitVersion.Logging.LogLevel level, string! format, params object![]! args) -> void

src/GitVersion.Core/VersionCalculation/BaseVersionCalculators/BaseVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public BaseVersion(BaseVersion baseVersion)
3737
public override string ToString()
3838
{
3939
var externalSource = BaseVersionSource == null ? "External Source" : BaseVersionSource.Sha;
40-
return $"{Source}: {SemanticVersion.ToString("f")} with commit count source {externalSource}";
40+
return $"{Source}: {SemanticVersion:f} with commit source '{externalSource}'";
4141
}
4242
}

src/GitVersion.Core/VersionCalculation/NextVersionCalculator.cs

Lines changed: 84 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -50,129 +50,132 @@ public virtual NextVersion FindVersion()
5050
this.log.Info($"Current commit is tagged with version {Context.CurrentCommitTaggedVersion}, version calculation is for meta data only.");
5151
}
5252

53-
var nextVersion = Calculate(Context.CurrentBranch, Context.Configuration);
53+
var nextVersion = CalculateNextVersion(Context.CurrentBranch, Context.Configuration);
5454
var incrementedVersion = CalculateIncrementedVersion(nextVersion.Configuration.VersioningMode, nextVersion);
5555
return new(incrementedVersion, nextVersion.BaseVersion, new(nextVersion.Branch, nextVersion.Configuration));
5656
}
5757

5858
private SemanticVersion CalculateIncrementedVersion(VersioningMode versioningMode, NextVersion nextVersion) => versioningMode switch
5959
{
6060
VersioningMode.ContinuousDelivery => this.manualDeploymentVersionCalculator.Calculate(nextVersion),
61-
VersioningMode.ContinuousDeployment => nextVersion.Configuration.IsMainline && nextVersion.Configuration.Label is null
61+
VersioningMode.ContinuousDeployment => nextVersion.Configuration is { IsMainline: true, Label: null }
6262
? this.continuousDeploymentVersionCalculator.Calculate(nextVersion)
6363
: this.continuousDeliveryVersionCalculator.Calculate(nextVersion),
6464
VersioningMode.Mainline => this.mainlineVersionCalculator.FindMainlineModeVersion(nextVersion),
6565
_ => throw new InvalidEnumArgumentException(nameof(versioningMode), (int)versioningMode, typeof(VersioningMode)),
6666
};
6767

68-
private NextVersion Calculate(IBranch branch, IGitVersionConfiguration configuration)
68+
private NextVersion CalculateNextVersion(IBranch branch, IGitVersionConfiguration configuration)
6969
{
70-
using (log.IndentLog("Calculating the base versions"))
71-
{
72-
var nextVersions = GetNextVersions(branch, configuration).ToArray();
73-
var maxVersion = nextVersions.Max()!;
70+
var nextVersions = GetNextVersions(branch, configuration).ToArray();
71+
log.Separator();
72+
var maxVersion = nextVersions.Max()!;
7473

75-
var matchingVersionsOnceIncremented = nextVersions
76-
.Where(v => v.BaseVersion.BaseVersionSource != null && v.IncrementedVersion == maxVersion.IncrementedVersion)
77-
.ToList();
78-
ICommit? latestBaseVersionSource;
74+
var matchingVersionsOnceIncremented = nextVersions
75+
.Where(v => v.BaseVersion.BaseVersionSource != null && v.IncrementedVersion == maxVersion.IncrementedVersion)
76+
.ToList();
77+
ICommit? latestBaseVersionSource;
7978

80-
if (matchingVersionsOnceIncremented.Any())
79+
if (matchingVersionsOnceIncremented.Any())
80+
{
81+
var latestVersion = matchingVersionsOnceIncremented.Aggregate(CompareVersions);
82+
latestBaseVersionSource = latestVersion.BaseVersion.BaseVersionSource;
83+
maxVersion = latestVersion;
84+
log.Info(
85+
$"Found multiple base versions which will produce the same SemVer ({maxVersion.IncrementedVersion}), " +
86+
$"taking oldest source for commit counting ({latestVersion.BaseVersion.Source})");
87+
}
88+
else
89+
{
90+
IEnumerable<NextVersion> filteredVersions = nextVersions;
91+
if (!maxVersion.IncrementedVersion.PreReleaseTag.HasTag())
8192
{
82-
static NextVersion CompareVersions(
83-
NextVersion versions1,
84-
NextVersion version2)
85-
{
86-
if (versions1.BaseVersion.BaseVersionSource == null)
87-
{
88-
return version2;
89-
}
90-
if (version2.BaseVersion.BaseVersionSource == null)
91-
{
92-
return versions1;
93-
}
93+
// If the maximal version has no pre-release tag defined than we want to determine just the latest previous
94+
// base source which are not coming from pre-release tag.
95+
filteredVersions = filteredVersions.Where(v => !v.BaseVersion.SemanticVersion.PreReleaseTag.HasTag());
96+
}
9497

95-
return versions1.BaseVersion.BaseVersionSource.When
96-
< version2.BaseVersion.BaseVersionSource.When ? versions1 : version2;
97-
}
98+
var versions = filteredVersions as NextVersion[] ?? filteredVersions.ToArray();
99+
var version = versions
100+
.Where(v => v.BaseVersion.BaseVersionSource != null)
101+
.OrderByDescending(v => v.IncrementedVersion)
102+
.ThenByDescending(v => v.BaseVersion.BaseVersionSource?.When)
103+
.FirstOrDefault();
104+
105+
version ??= versions.Where(v => v.BaseVersion.BaseVersionSource == null)
106+
.OrderByDescending(v => v.IncrementedVersion)
107+
.First();
108+
latestBaseVersionSource = version.BaseVersion.BaseVersionSource;
109+
}
98110

99-
var latestVersion = matchingVersionsOnceIncremented.Aggregate(CompareVersions);
100-
latestBaseVersionSource = latestVersion.BaseVersion.BaseVersionSource;
101-
maxVersion = latestVersion;
102-
log.Info($"Found multiple base versions which will produce the same SemVer ({maxVersion.IncrementedVersion})," +
103-
$" taking oldest source for commit counting ({latestVersion.BaseVersion.Source})");
104-
}
105-
else
106-
{
107-
IEnumerable<NextVersion> filteredVersions = nextVersions;
108-
if (!maxVersion.IncrementedVersion.PreReleaseTag.HasTag())
109-
{
110-
// If the maximal version has no pre-release tag defined than we want to determine just the latest previous
111-
// base source which are not coming from pre-release tag.
112-
filteredVersions = filteredVersions.Where(v => !v.BaseVersion.SemanticVersion.PreReleaseTag.HasTag());
113-
}
111+
var calculatedBase = new BaseVersion(
112+
maxVersion.BaseVersion.Source,
113+
maxVersion.BaseVersion.ShouldIncrement,
114+
maxVersion.BaseVersion.SemanticVersion,
115+
latestBaseVersionSource,
116+
maxVersion.BaseVersion.BranchNameOverride
117+
);
114118

115-
var versions = filteredVersions as NextVersion[] ?? filteredVersions.ToArray();
116-
var version = versions
117-
.Where(v => v.BaseVersion.BaseVersionSource != null)
118-
.OrderByDescending(v => v.IncrementedVersion)
119-
.ThenByDescending(v => v.BaseVersion.BaseVersionSource?.When)
120-
.FirstOrDefault();
121-
122-
version ??= versions.Where(v => v.BaseVersion.BaseVersionSource == null)
123-
.OrderByDescending(v => v.IncrementedVersion)
124-
.First();
125-
latestBaseVersionSource = version.BaseVersion.BaseVersionSource;
126-
}
119+
log.Info($"Base version used: {calculatedBase}");
120+
log.Separator();
121+
return new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);
122+
}
127123

128-
var calculatedBase = new BaseVersion(
129-
maxVersion.BaseVersion.Source,
130-
maxVersion.BaseVersion.ShouldIncrement,
131-
maxVersion.BaseVersion.SemanticVersion,
132-
latestBaseVersionSource,
133-
maxVersion.BaseVersion.BranchNameOverride
134-
);
124+
private static NextVersion CompareVersions(NextVersion versions1, NextVersion version2)
125+
{
126+
if (versions1.BaseVersion.BaseVersionSource == null)
127+
return version2;
135128

136-
log.Info($"Base version used: {calculatedBase}");
129+
if (version2.BaseVersion.BaseVersionSource == null)
130+
return versions1;
137131

138-
return new NextVersion(maxVersion.IncrementedVersion, calculatedBase, maxVersion.Branch, maxVersion.Configuration);
139-
}
132+
return versions1.BaseVersion.BaseVersionSource.When < version2.BaseVersion.BaseVersionSource.When
133+
? versions1
134+
: version2;
140135
}
141136

142137
private IReadOnlyCollection<NextVersion> GetNextVersions(IBranch branch, IGitVersionConfiguration configuration)
143138
{
144-
if (branch.Tip == null)
145-
throw new GitVersionException("No commits found on the current branch.");
139+
using (log.IndentLog("Fetching the base versions for version calculation..."))
140+
{
141+
if (branch.Tip == null)
142+
throw new GitVersionException("No commits found on the current branch.");
146143

147-
var nextVersions = GetNextVersionsInternal().ToList();
148-
if (nextVersions.Count == 0)
149-
throw new GitVersionException("No base versions determined on the current branch.");
150-
return nextVersions;
144+
var nextVersions = GetNextVersionsInternal().ToList();
145+
if (nextVersions.Count == 0)
146+
throw new GitVersionException("No base versions determined on the current branch.");
147+
return nextVersions;
148+
}
151149

152150
IEnumerable<NextVersion> GetNextVersionsInternal()
153151
{
154-
foreach (var effectiveConfiguration in effectiveBranchConfigurationFinder.GetConfigurations(branch, configuration))
152+
var effectiveBranchConfigurations = this.effectiveBranchConfigurationFinder.GetConfigurations(branch, configuration).ToArray();
153+
foreach (var effectiveBranchConfiguration in effectiveBranchConfigurations)
155154
{
155+
this.log.Info($"Calculating base versions for '{effectiveBranchConfiguration.Branch.Name}'");
156156
var atLeastOneBaseVersionReturned = false;
157157
foreach (var versionStrategy in this.versionStrategies)
158158
{
159-
foreach (var baseVersion in versionStrategy.GetBaseVersions(effectiveConfiguration))
159+
using (this.log.IndentLog($"[Using '{versionStrategy.GetType().Name}' strategy]"))
160160
{
161-
log.Info(baseVersion.ToString());
162-
163-
if (IncludeVersion(baseVersion, configuration.Ignore)
164-
&& TryGetNextVersion(out var nextVersion, effectiveConfiguration, baseVersion))
161+
foreach (var baseVersion in versionStrategy.GetBaseVersions(effectiveBranchConfiguration))
165162
{
166-
yield return nextVersion;
167-
atLeastOneBaseVersionReturned = true;
163+
log.Info(baseVersion.ToString());
164+
if (IncludeVersion(baseVersion, configuration.Ignore)
165+
&& TryGetNextVersion(out var nextVersion, effectiveBranchConfiguration, baseVersion))
166+
{
167+
yield return nextVersion;
168+
atLeastOneBaseVersionReturned = true;
169+
}
168170
}
169171
}
170172
}
171173

172174
if (!atLeastOneBaseVersionReturned)
173175
{
174176
var baseVersion = new BaseVersion("Fallback base version", true, SemanticVersion.Empty, null, null);
175-
if (TryGetNextVersion(out var nextVersion, effectiveConfiguration, baseVersion)) yield return nextVersion;
177+
if (TryGetNextVersion(out var nextVersion, effectiveBranchConfiguration, baseVersion))
178+
yield return nextVersion;
176179
}
177180
}
178181
}
@@ -187,7 +190,9 @@ private bool TryGetNextVersion([NotNullWhen(true)] out NextVersion? result,
187190
Context.CurrentBranch.Name, baseVersion.BranchNameOverride
188191
);
189192
if (effectiveConfiguration.Value.Label != label)
193+
{
190194
log.Info("Using current branch name to calculate version tag");
195+
}
191196

192197
var incrementedVersion = GetIncrementedVersion(effectiveConfiguration, baseVersion, label);
193198
if (incrementedVersion.IsMatchForBranchSpecificLabel(label))

src/GitVersion.Core/VersionCalculation/ShaVersionFilter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ namespace GitVersion.VersionCalculation;
55

66
internal class ShaVersionFilter : IVersionFilter
77
{
8-
private readonly IEnumerable<string> shas;
8+
private readonly IEnumerable<string> shaList;
99

10-
public ShaVersionFilter(IEnumerable<string> shas) => this.shas = shas.NotNull();
10+
public ShaVersionFilter(IEnumerable<string> shaList) => this.shaList = shaList.NotNull();
1111

1212
public bool Exclude(BaseVersion? version, [NotNullWhen(true)] out string? reason)
1313
{
1414
version.NotNull();
1515

1616
reason = null;
1717

18-
if (version.BaseVersionSource == null || !this.shas.Any(sha => version.BaseVersionSource.Sha.StartsWith(sha, StringComparison.OrdinalIgnoreCase)))
18+
if (version.BaseVersionSource == null || !this.shaList.Any(sha => version.BaseVersionSource.Sha.StartsWith(sha, StringComparison.OrdinalIgnoreCase)))
1919
return false;
2020

2121
reason = $"Sha {version.BaseVersionSource} was ignored due to commit having been excluded by configuration";

0 commit comments

Comments
 (0)