Skip to content

Commit

Permalink
Cache tags in IncrementStrategyFinder to prevent multiple reads from …
Browse files Browse the repository at this point in the history
…repository
  • Loading branch information
AntonMTolmachev committed Jun 9, 2023
1 parent a9c3f4e commit 448de3e
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder
private readonly Dictionary<string, VersionField?> commitIncrementCache = new();
private readonly Dictionary<string, Dictionary<string, int>> headCommitsMapCache = new();
private readonly Dictionary<string, ICommit[]> headCommitsCache = new();
private readonly Lazy<IReadOnlySet<string?>> tagsShaCache;

private static readonly Regex DefaultMajorPatternRegex = new(DefaultMajorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex DefaultMinorPatternRegex = new(DefaultMinorPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
Expand All @@ -24,7 +25,11 @@ internal class IncrementStrategyFinder : IIncrementStrategyFinder

private readonly IGitRepository repository;

public IncrementStrategyFinder(IGitRepository repository) => this.repository = repository.NotNull();
public IncrementStrategyFinder(IGitRepository repository)
{
this.repository = repository.NotNull();
this.tagsShaCache = new Lazy<IReadOnlySet<string?>>(ReadRepositoryTagsSha);
}

public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersion baseVersion, EffectiveConfiguration configuration)
{
Expand Down Expand Up @@ -79,12 +84,10 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
}

var commits = GetIntermediateCommits(baseCommit, currentCommit);

// consider commit messages since latest tag only (see #3071)
var tags = new HashSet<string?>(repository.Tags.Select(t => t.TargetSha));
commits = commits
.Reverse()
.TakeWhile(x => !tags.Contains(x.Sha))
.TakeWhile(x => !this.tagsShaCache.Value.Contains(x.Sha))
.Reverse();

if (configuration.CommitMessageIncrementing == CommitMessageIncrementMode.MergeMessageOnly)
Expand All @@ -101,6 +104,8 @@ public VersionField DetermineIncrementedField(ICommit? currentCommit, BaseVersio
);
}

private IReadOnlySet<string?> ReadRepositoryTagsSha() => repository.Tags.Select(t => t.TargetSha).ToHashSet();

private static Regex TryGetRegexOrDefault(string? messageRegex, Regex defaultRegex) =>
messageRegex == null
? defaultRegex
Expand Down

0 comments on commit 448de3e

Please sign in to comment.