Skip to content

Commit c2de984

Browse files
authored
Some small performance improvements to ignore file (#1639)
1 parent b1101fb commit c2de984

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

Src/CSharpier.Cli/IgnoreFile.cs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ internal class IgnoreFile
1414
**/.git
1515
""";
1616

17+
private static readonly Lazy<(Regex positives, Regex negatives)> defaultRules = new(() =>
18+
{
19+
var (alwaysPositives, alwaysNegatives) = GitignoreParserNet.GitignoreParser.Parse(
20+
alwaysIgnoredText,
21+
true
22+
);
23+
return (alwaysPositives.Merged, alwaysNegatives.Merged);
24+
});
25+
1726
private IgnoreFile(List<IgnoreWithBasePath> ignores)
1827
{
1928
this.Ignores = ignores;
@@ -22,6 +31,7 @@ private IgnoreFile(List<IgnoreWithBasePath> ignores)
2231
public bool IsIgnored(string filePath)
2332
{
2433
filePath = filePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
34+
2535
foreach (var ignore in this.Ignores)
2636
{
2737
// when using one of the ignore files to determine if a given file is ignored or not
@@ -87,13 +97,10 @@ CancellationToken cancellationToken
8797

8898
private static void AddDefaultRules(IgnoreWithBasePath ignore)
8999
{
90-
var (alwaysPositives, alwaysNegatives) = GitignoreParserNet.GitignoreParser.Parse(
91-
alwaysIgnoredText,
92-
true
93-
);
100+
var (positives, negatives) = defaultRules.Value;
94101

95-
ignore.AddPositives(alwaysPositives.Merged);
96-
ignore.AddNegatives(alwaysNegatives.Merged);
102+
ignore.AddPositives(positives);
103+
ignore.AddNegatives(negatives);
97104
}
98105

99106
// this will return the ignore paths in order of priority
@@ -157,17 +164,15 @@ private class IgnoreWithBasePath(string basePath)
157164

158165
var isIgnored = false;
159166
var hasMatchingRule = false;
167+
160168
foreach (var rule in this.positives)
161169
{
162170
var isMatch = rule.IsMatch(pathRelativeToIgnoreFile);
163171
if (isMatch)
164172
{
165173
hasMatchingRule = true;
166-
}
167-
168-
if (!isIgnored && isMatch)
169-
{
170174
isIgnored = true;
175+
break;
171176
}
172177
}
173178

@@ -177,11 +182,11 @@ private class IgnoreWithBasePath(string basePath)
177182
if (isMatch)
178183
{
179184
hasMatchingRule = true;
180-
}
181-
182-
if (isIgnored && isMatch)
183-
{
184-
isIgnored = false;
185+
if (isIgnored)
186+
{
187+
isIgnored = false;
188+
break;
189+
}
185190
}
186191
}
187192

0 commit comments

Comments
 (0)