Skip to content

Commit 04f5357

Browse files
author
roeil
committed
feat(paths-filter): follow pr
1 parent 71d9a25 commit 04f5357

35 files changed

+190
-34
lines changed

docs/input/docs/workflows/GitFlow/v1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ branches:
148148
is-main-branch: false
149149
ignore:
150150
sha: []
151+
paths: []
151152
mode: ContinuousDelivery
152153
label: '{BranchName}'
153154
increment: Inherit

docs/input/docs/workflows/GitHubFlow/v1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ branches:
9797
is-main-branch: false
9898
ignore:
9999
sha: []
100+
paths: []
100101
mode: ContinuousDelivery
101102
label: '{BranchName}'
102103
increment: Inherit

docs/input/docs/workflows/TrunkBased/preview1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ branches:
8282
pre-release-weight: 30000
8383
ignore:
8484
sha: []
85+
paths: []
8586
mode: ContinuousDelivery
8687
label: '{BranchName}'
8788
increment: Inherit

schemas/6.1/GitVersion.configuration.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@
189189
"null"
190190
]
191191
},
192+
"paths": {
193+
"description": "A sequence of file paths to be excluded from the version calculations.",
194+
"type": "array",
195+
"items": {
196+
"type": "string"
197+
}
198+
},
192199
"sha": {
193200
"description": "A sequence of SHAs to be excluded from the version calculations.",
194201
"$ref": "#/$defs/hashSetOfString"

src/GitVersion.Configuration.Tests/Configuration/ConfigurationProviderTests.CanWriteOutEffectiveConfiguration.approved.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ branches:
148148
is-main-branch: false
149149
ignore:
150150
sha: []
151+
paths: []
151152
mode: ContinuousDelivery
152153
label: '{BranchName}'
153154
increment: Inherit

src/GitVersion.Configuration.Tests/Workflows/approved/GitFlow/v1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ branches:
148148
is-main-branch: false
149149
ignore:
150150
sha: []
151+
paths: []
151152
mode: ContinuousDelivery
152153
label: '{BranchName}'
153154
increment: Inherit

src/GitVersion.Configuration.Tests/Workflows/approved/GitHubFlow/v1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ branches:
9797
is-main-branch: false
9898
ignore:
9999
sha: []
100+
paths: []
100101
mode: ContinuousDelivery
101102
label: '{BranchName}'
102103
increment: Inherit

src/GitVersion.Configuration.Tests/Workflows/approved/TrunkBased/preview1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ branches:
8282
pre-release-weight: 30000
8383
ignore:
8484
sha: []
85+
paths: []
8586
mode: ContinuousDelivery
8687
label: '{BranchName}'
8788
increment: Inherit

src/GitVersion.Configuration/IgnoreConfiguration.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Collections.ObjectModel;
12
using GitVersion.Configuration.Attributes;
23

34
namespace GitVersion.Configuration;
@@ -24,5 +25,11 @@ public string? BeforeString
2425
public HashSet<string> Shas { get; init; } = [];
2526

2627
[JsonIgnore]
27-
public bool IsEmpty => Before == null && Shas.Count == 0;
28+
public bool IsEmpty => Before == null && Shas.Count == 0 && Paths.Count == 0;
29+
30+
IReadOnlyCollection<string> IIgnoreConfiguration.Paths => Paths;
31+
32+
[JsonPropertyName("paths")]
33+
[JsonPropertyDescription("A sequence of file paths to be excluded from the version calculations.")]
34+
public Collection<string> Paths { get; init; } = [];
2835
}

src/GitVersion.Core.Tests/VersionCalculation/MinDateVersionFilterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void VerifyNullGuard()
1212
var dummy = DateTimeOffset.UtcNow.AddSeconds(1.0);
1313
var sut = new MinDateVersionFilter(dummy);
1414

15-
Should.Throw<ArgumentNullException>(() => sut.Exclude(null!, out _));
15+
Should.Throw<ArgumentNullException>(() => sut.Exclude((IBaseVersion)null!, out _));
1616
}
1717

1818
[Test]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using GitVersion.Core.Tests.Helpers;
2+
using GitVersion.VersionCalculation;
3+
4+
namespace GitVersion.Core.Tests;
5+
6+
[TestFixture]
7+
public class PathFilterTests : TestBase
8+
{
9+
[Test]
10+
public void VerifyNullGuard() => Should.Throw<ArgumentNullException>(() => new PathFilter(null!));
11+
}

src/GitVersion.Core.Tests/VersionCalculation/ShaVersionFilterTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public void VerifyNullGuard()
1212
var commit = GitRepositoryTestingExtensions.CreateMockCommit();
1313
var sut = new ShaVersionFilter([commit.Sha]);
1414

15-
Should.Throw<ArgumentNullException>(() => sut.Exclude(null!, out _));
15+
Should.Throw<ArgumentNullException>(() => sut.Exclude((IBaseVersion)null!, out _));
1616
}
1717

1818
[Test]

src/GitVersion.Core.Tests/VersionCalculation/Strategies/MergeMessageBaseVersionStrategyTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ private class MockCommit : ICommit
204204
public IObjectId Id => throw new NotImplementedException();
205205
public string Sha => throw new NotImplementedException();
206206
public IReadOnlyList<ICommit> Parents => throw new NotImplementedException();
207+
public IEnumerable<string> DiffPaths => throw new NotImplementedException();
207208
public DateTimeOffset When => throw new NotImplementedException();
208209
public string Message => throw new NotImplementedException();
209210
}

src/GitVersion.Core/Configuration/EffectiveConfiguration.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public EffectiveConfiguration(
6767
PatchVersionBumpMessage = configuration.PatchVersionBumpMessage;
6868
NoBumpMessage = configuration.NoBumpMessage;
6969
CommitMessageIncrementing = branchConfiguration.CommitMessageIncrementing.Value;
70-
VersionFilters = configuration.Ignore.ToFilters();
7170
Ignore = configuration.Ignore;
7271
TracksReleaseBranches = branchConfiguration.TracksReleaseBranches ?? false;
7372
IsReleaseBranch = branchConfiguration.IsReleaseBranch ?? false;
@@ -122,8 +121,6 @@ public EffectiveConfiguration(
122121

123122
public CommitMessageIncrementMode CommitMessageIncrementing { get; }
124123

125-
public IEnumerable<IVersionFilter> VersionFilters { get; }
126-
127124
public IIgnoreConfiguration Ignore { get; }
128125

129126
public string? CommitDateFormat { get; }

src/GitVersion.Core/Configuration/IIgnoreConfiguration.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ public interface IIgnoreConfiguration
66

77
IReadOnlySet<string> Shas { get; }
88

9-
bool IsEmpty { get; }
9+
IReadOnlyCollection<string> Paths { get; }
10+
11+
bool IsEmpty => Before == null && Shas.Count == 0 && Paths.Count == 0;
1012
}

src/GitVersion.Core/Configuration/IgnoreConfigurationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ public static IEnumerable<ICommit> Filter(this IIgnoreConfiguration ignore, ICom
2222
}
2323

2424
private static bool ShouldBeIgnored(ICommit commit, IIgnoreConfiguration ignore)
25-
=> !(commit.When <= ignore.Before) && !ignore.Shas.Contains(commit.Sha);
25+
=> !ignore.ToFilters().Any(filter => filter.Exclude(commit, out var _));
2626
}

src/GitVersion.Core/Extensions/ConfigurationExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static EffectiveConfiguration GetEffectiveConfiguration(
2525
{
2626
fallbackConfiguration = parentConfiguration;
2727
}
28-
return new EffectiveConfiguration(configuration, branchConfiguration, fallbackConfiguration);
28+
return new EffectiveConfiguration(configuration, branchConfiguration, fallbackConfiguration: fallbackConfiguration);
2929
}
3030

3131
public static IBranchConfiguration GetBranchConfiguration(this IGitVersionConfiguration configuration, IBranch branch)
@@ -44,6 +44,7 @@ public static IEnumerable<IVersionFilter> ToFilters(this IIgnoreConfiguration so
4444

4545
if (source.Shas.Count != 0) yield return new ShaVersionFilter(source.Shas);
4646
if (source.Before.HasValue) yield return new MinDateVersionFilter(source.Before.Value);
47+
if (source.Paths.Count != 0) yield return new PathFilter(source.Paths);
4748
}
4849

4950
private static IEnumerable<IBranchConfiguration> GetBranchConfigurations(IGitVersionConfiguration configuration, string branchName)

src/GitVersion.Core/Git/ICommit.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface ICommit : IEquatable<ICommit?>, IComparable<ICommit>, IGitObjec
77
DateTimeOffset When { get; }
88

99
string Message { get; }
10+
IEnumerable<string> DiffPaths { get; }
1011
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace GitVersion.Git;
2+
3+
public interface ITreeChanges
4+
{
5+
IEnumerable<string> Paths { get; }
6+
}

src/GitVersion.Core/PublicAPI.Shipped.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ GitVersion.Configuration.EffectiveConfiguration.TrackMergeMessage.get -> bool
8787
GitVersion.Configuration.EffectiveConfiguration.TrackMergeTarget.get -> bool
8888
GitVersion.Configuration.EffectiveConfiguration.TracksReleaseBranches.get -> bool
8989
GitVersion.Configuration.EffectiveConfiguration.UpdateBuildNumber.get -> bool
90-
GitVersion.Configuration.EffectiveConfiguration.VersionFilters.get -> System.Collections.Generic.IEnumerable<GitVersion.VersionCalculation.IVersionFilter!>!
9190
GitVersion.Configuration.EffectiveConfiguration.VersionInBranchPattern.get -> string?
9291
GitVersion.Configuration.EffectiveConfiguration.VersionStrategy.get -> GitVersion.VersionCalculation.VersionStrategies
9392
GitVersion.Configuration.IBranchConfiguration
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
#nullable enable
2+
GitVersion.Configuration.IIgnoreConfiguration.Paths.get -> System.Collections.Generic.IReadOnlyCollection<string!>!
3+
GitVersion.Git.ICommit.DiffPaths.get -> System.Collections.Generic.IEnumerable<string!>!
4+
GitVersion.Git.ITreeChanges
5+
GitVersion.Git.ITreeChanges.Paths.get -> System.Collections.Generic.IEnumerable<string!>!
6+
GitVersion.VersionCalculation.IVersionFilter.Exclude(GitVersion.Git.ICommit! commit, out string? reason) -> bool
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
using GitVersion.Git;
2+
13
namespace GitVersion.VersionCalculation;
24

35
public interface IVersionFilter
46
{
57
bool Exclude(IBaseVersion baseVersion, out string? reason);
8+
bool Exclude(ICommit commit, out string? reason);
69
}

src/GitVersion.Core/VersionCalculation/IncrementStrategyFinder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace GitVersion.VersionCalculation;
99

10-
internal class IncrementStrategyFinder(IRepositoryStore repositoryStore, ITaggedSemanticVersionRepository taggedSemanticVersionRepository)
10+
internal class IncrementStrategyFinder(
11+
IRepositoryStore repositoryStore,
12+
ITaggedSemanticVersionRepository taggedSemanticVersionRepository)
1113
: IIncrementStrategyFinder
1214
{
1315
private readonly Dictionary<string, VersionField?> commitIncrementCache = [];

src/GitVersion.Core/VersionCalculation/MinDateVersionFilter.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using GitVersion.Extensions;
3+
using GitVersion.Git;
34

45
namespace GitVersion.VersionCalculation;
56

@@ -17,4 +18,15 @@ public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? re
1718
reason = "Source was ignored due to commit date being outside of configured range";
1819
return true;
1920
}
21+
22+
public bool Exclude(ICommit commit, [NotNullWhen(true)] out string? reason)
23+
{
24+
reason = null;
25+
26+
if (commit == null || commit.When >= minimum)
27+
return false;
28+
29+
reason = "Source was ignored due to commit date being outside of configured range";
30+
return true;
31+
}
2032
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using System.Text.RegularExpressions;
3+
using GitVersion.Git;
4+
5+
namespace GitVersion.VersionCalculation;
6+
7+
internal class PathFilter( IEnumerable<string> paths) : IVersionFilter
8+
{
9+
private readonly List<Regex> pathsRegexes = paths.Select(path => new Regex(path, RegexOptions.IgnoreCase | RegexOptions.Compiled)).ToList();
10+
private readonly Dictionary<string, bool> pathMatchCache = [];
11+
12+
public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? reason)
13+
{
14+
ArgumentNullException.ThrowIfNull(baseVersion);
15+
16+
reason = null;
17+
if (baseVersion.Source.StartsWith("Fallback") || baseVersion.Source.StartsWith("Git tag") || baseVersion.Source.StartsWith("NextVersion")) return false;
18+
19+
return Exclude(baseVersion.BaseVersionSource, out reason);
20+
}
21+
22+
public bool Exclude(ICommit? commit, [NotNullWhen(true)] out string? reason)
23+
{
24+
reason = null;
25+
26+
if (commit != null)
27+
{
28+
var patchPaths = commit.DiffPaths;
29+
30+
if (patchPaths != null)
31+
{
32+
foreach (var path in patchPaths)
33+
{
34+
if (!pathMatchCache.TryGetValue(path, out var isMatch))
35+
{
36+
isMatch = this.pathsRegexes.Any(regex => regex.IsMatch(path));
37+
pathMatchCache[path] = isMatch;
38+
}
39+
40+
if (isMatch)
41+
{
42+
reason = "Source was ignored due to commit path matching ignore regex";
43+
return true;
44+
}
45+
}
46+
}
47+
}
48+
49+
return false;
50+
}
51+
}

src/GitVersion.Core/VersionCalculation/ShaVersionFilter.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Diagnostics.CodeAnalysis;
22
using GitVersion.Extensions;
3+
using GitVersion.Git;
34

45
namespace GitVersion.VersionCalculation;
56

@@ -22,4 +23,16 @@ public bool Exclude(IBaseVersion baseVersion, [NotNullWhen(true)] out string? re
2223
reason = $"Sha {baseVersion.BaseVersionSource} was ignored due to commit having been excluded by configuration";
2324
return true;
2425
}
26+
27+
public bool Exclude(ICommit commit, [NotNullWhen(true)] out string? reason)
28+
{
29+
reason = null;
30+
31+
if (commit == null
32+
|| !this.shaList.Any(sha => commit.Sha.StartsWith(sha, StringComparison.OrdinalIgnoreCase)))
33+
return false;
34+
35+
reason = $"Sha {commit} was ignored due to commit having been excluded by configuration";
36+
return true;
37+
}
2538
}

src/GitVersion.Core/VersionCalculation/VersionSearchStrategies/ConfiguredNextVersionVersionStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public IEnumerable<BaseVersion> GetBaseVersions(EffectiveBranchConfiguration con
1818
{
1919
configuration.NotNull();
2020

21-
if (!Context.Configuration.VersionStrategy.HasFlag(VersionStrategies.ConfiguredNextVersion))
21+
if (!this.Context.Configuration.VersionStrategy.HasFlag(VersionStrategies.ConfiguredNextVersion))
2222
yield break;
2323

2424
var nextVersion = Context.Configuration.NextVersion;

src/GitVersion.LibGit2Sharp/Git/Branch.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ internal sealed class Branch : IBranch
1010

1111
private readonly LibGit2Sharp.Branch innerBranch;
1212

13-
internal Branch(LibGit2Sharp.Branch branch)
13+
internal Branch(LibGit2Sharp.Branch branch, LibGit2Sharp.Diff diff)
1414
{
1515
this.innerBranch = branch.NotNull();
1616
Name = new(branch.CanonicalName);
1717

1818
var commit = this.innerBranch.Tip;
19-
Tip = commit is null ? null : new Commit(commit);
19+
Tip = commit is null ? null : new Commit(commit, diff);
2020

2121
var commits = this.innerBranch.Commits;
22-
Commits = new CommitCollection(commits);
22+
Commits = new CommitCollection(commits, diff);
2323
}
2424

2525
public ReferenceName Name { get; }

src/GitVersion.LibGit2Sharp/Git/BranchCollection.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ internal sealed class BranchCollection : IBranchCollection
77
{
88
private readonly LibGit2Sharp.BranchCollection innerCollection;
99
private readonly Lazy<IReadOnlyCollection<IBranch>> branches;
10+
private readonly LibGit2Sharp.Diff diff;
1011

11-
internal BranchCollection(LibGit2Sharp.BranchCollection collection)
12+
internal BranchCollection(LibGit2Sharp.BranchCollection collection, LibGit2Sharp.Diff diff)
1213
{
1314
this.innerCollection = collection.NotNull();
14-
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => [.. this.innerCollection.Select(branch => new Branch(branch))]);
15+
this.branches = new Lazy<IReadOnlyCollection<IBranch>>(() => [.. this.innerCollection.Select(branch => new Branch(branch, diff))]);
16+
this.diff = diff.NotNull();
1517
}
1618

1719
public IEnumerator<IBranch> GetEnumerator()
@@ -25,7 +27,7 @@ public IBranch? this[string name]
2527
{
2628
name = name.NotNull();
2729
var branch = this.innerCollection[name];
28-
return branch is null ? null : new Branch(branch);
30+
return branch is null ? null : new Branch(branch, this.diff);
2931
}
3032
}
3133

0 commit comments

Comments
 (0)