Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Oct 15, 2024
1 parent ec25813 commit 1200ae1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
16 changes: 4 additions & 12 deletions src/GitVersion.Core/Configuration/IgnoreConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,20 @@ namespace GitVersion.Configuration;

internal static class IgnoreConfigurationExtensions
{
public static IEnumerable<ITag> Filter(this IIgnoreConfiguration ignore, IEnumerable<ITag> source)
public static IEnumerable<ITag> Filter(this IIgnoreConfiguration ignore, ITag[] source)
{
ignore.NotNull();
source.NotNull();

if (!ignore.IsEmpty)
{
return source.Where(element => ShouldBeIgnored(element.Commit, ignore));
}
return source;
return !ignore.IsEmpty ? source.Where(element => ShouldBeIgnored(element.Commit, ignore)) : source;
}

public static IEnumerable<ICommit> Filter(this IIgnoreConfiguration ignore, IEnumerable<ICommit> source)
public static IEnumerable<ICommit> Filter(this IIgnoreConfiguration ignore, ICommit[] source)
{
ignore.NotNull();
source.NotNull();

if (!ignore.IsEmpty)
{
return source.Where(element => ShouldBeIgnored(element, ignore));
}
return source;
return !ignore.IsEmpty ? source.Where(element => ShouldBeIgnored(element, ignore)) : source;
}

private static bool ShouldBeIgnored(ICommit commit, IIgnoreConfiguration ignore)
Expand Down
21 changes: 11 additions & 10 deletions src/GitVersion.Core/Core/RepositoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ internal class RepositoryStore(ILog log, IGitRepository repository) : IRepositor
this.log.Info("Using latest commit on specified branch");
}

commits = ignore.Filter(commits);
commits = ignore.Filter(commits.ToArray());
return commits.FirstOrDefault();
}

Expand Down Expand Up @@ -219,8 +219,7 @@ public IReadOnlyList<ICommit> GetCommitLog(ICommit? baseVersionSource, ICommit c
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Time
};

var commits = this.repository.Commits.QueryBy(filter);

var commits = FilterCommits(filter).ToArray();
return ignore.Filter(commits).ToList();
}

Expand All @@ -232,16 +231,16 @@ public IReadOnlyList<ICommit> GetCommitsReacheableFromHead(ICommit? headCommit,
SortBy = CommitSortStrategies.Topological | CommitSortStrategies.Reverse
};

var commits = this.repository.Commits.QueryBy(filter);
var commits = FilterCommits(filter).ToArray();
return ignore.Filter(commits).ToList();
}

public IReadOnlyList<ICommit> GetCommitsReacheableFrom(IGitObject commit, IBranch branch)
{
var filter = new CommitFilter { IncludeReachableFrom = branch };
var commitCollection = this.repository.Commits.QueryBy(filter);

return commitCollection.Where(c => c.Sha == commit.Sha).ToList();
var commits = FilterCommits(filter);
return commits.Where(c => c.Sha == commit.Sha).ToList();
}

public ICommit? GetForwardMerge(ICommit? commitToFindCommonBase, ICommit? findMergeBase)
Expand All @@ -251,18 +250,20 @@ public IReadOnlyList<ICommit> GetCommitsReacheableFrom(IGitObject commit, IBranc
IncludeReachableFrom = commitToFindCommonBase,
ExcludeReachableFrom = findMergeBase
};
var commitCollection = this.repository.Commits.QueryBy(filter);

return commitCollection.FirstOrDefault(c => c.Parents.Contains(findMergeBase));
var commits = FilterCommits(filter);
return commits.FirstOrDefault(c => c.Parents.Contains(findMergeBase));
}

public bool IsCommitOnBranch(ICommit? baseVersionSource, IBranch branch, ICommit firstMatchingCommit)
{
var filter = new CommitFilter { IncludeReachableFrom = branch, ExcludeReachableFrom = baseVersionSource, FirstParentOnly = true };
var commitCollection = this.repository.Commits.QueryBy(filter);
return commitCollection.Contains(firstMatchingCommit);
var commits = FilterCommits(filter);
return commits.Contains(firstMatchingCommit);
}

private IEnumerable<ICommit> FilterCommits(CommitFilter filter) => this.repository.Commits.QueryBy(filter);

public ICommit? FindMergeBase(ICommit commit, ICommit mainlineTip) => this.repository.FindMergeBase(commit, mainlineTip);

private IBranch? FindBranch(string branchName) => this.repository.Branches.FirstOrDefault(x => x.Name.EquivalentTo(branchName));
Expand Down
6 changes: 3 additions & 3 deletions src/GitVersion.Core/Core/TaggedSemanticVersionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
{
var semanticVersions = GetTaggedSemanticVersions(tagPrefix, format, ignore);

foreach (var commit in ignore.Filter(branch.Commits))
foreach (var commit in ignore.Filter(branch.Commits.ToArray()))
{
foreach (var semanticVersion in semanticVersions[commit])
{
Expand Down Expand Up @@ -88,7 +88,7 @@ public ILookup<ICommit, SemanticVersionWithTag> GetTaggedSemanticVersionsOfMerge
using (this.log.IndentLog($"Getting tagged semantic versions by track merge target '{branch.Name.Canonical}'. " +
$"TagPrefix: {tagPrefix} and Format: {format}"))
{
var shaHashSet = new HashSet<string>(ignore.Filter(branch.Commits).Select(element => element.Id.Sha));
var shaHashSet = new HashSet<string>(ignore.Filter(branch.Commits.ToArray()).Select(element => element.Id.Sha));

foreach (var semanticVersion in GetTaggedSemanticVersions(tagPrefix, format, ignore).SelectMany(v => v))
{
Expand Down Expand Up @@ -124,7 +124,7 @@ IEnumerable<SemanticVersionWithTag> GetElements()
{
this.log.Info($"Getting tagged semantic versions. TagPrefix: {tagPrefix} and Format: {format}");

foreach (var tag in ignore.Filter(this.repositoryStore.Tags))
foreach (var tag in ignore.Filter(this.repositoryStore.Tags.ToArray()))
{
if (SemanticVersion.TryParse(tag.Name.Friendly, tagPrefix, out var semanticVersion, format))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration con
configuration: branchConfiguration
);

var commitsInReverseOrder = Context.Configuration.Ignore.Filter(Context.CurrentBranchCommits);
var commitsInReverseOrder = Context.Configuration.Ignore.Filter(Context.CurrentBranchCommits.ToArray());

TaggedSemanticVersions taggedSemanticVersion = TaggedSemanticVersions.OfBranch;
if (branchConfiguration.TrackMergeTarget == true) taggedSemanticVersion |= TaggedSemanticVersions.OfMergeTargets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private IEnumerable<BaseVersion> GetBaseVersionsInternal(EffectiveBranchConfigur
|| !configuration.Value.TrackMergeMessage)
yield break;

foreach (var commit in configuration.Value.Ignore.Filter(Context.CurrentBranchCommits))
foreach (var commit in configuration.Value.Ignore.Filter(Context.CurrentBranchCommits.ToArray()))
{
if (MergeMessage.TryParse(commit, Context.Configuration, out var mergeMessage)
&& mergeMessage.Version is not null
Expand Down

0 comments on commit 1200ae1

Please sign in to comment.