forked from GerHobbelt/gitignore-parser
-
Notifications
You must be signed in to change notification settings - Fork 3
/
GitignoreParser.cs
767 lines (720 loc) · 29.4 KB
/
GitignoreParser.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using static GitignoreParserNet.RegexPatterns;
namespace GitignoreParserNet;
/// <summary>
/// A simple yet complete .gitignore parser for .NET
/// </summary>
/// <remarks>
/// This is a .NET port of the npm package gitignore-parser by Ger Hobbelt.
/// </remarks>
/// <seealso href="https://github.com/GerHobbelt/gitignore-parser"/>
/// <seealso href="https://github.com/GerHobbelt"/>
public sealed class GitignoreParser
{
private static readonly string[] LINEBREAKS = ["\r\n", "\r", "\n"];
private readonly (Regex Merged, Regex[] Individual) Positives;
private readonly (Regex Merged, Regex[] Individual) Negatives;
/// <summary>
/// Parses a string containing the gitignore rules.
/// </summary>
/// <param name="content">The string containing the gitignore rules.</param>
/// <param name="compileRegex">If <see langword="true"/>, the Regex objects will be compiled to improve consecutive uses.</param>
public GitignoreParser(string content, bool compileRegex = false)
{
(Positives, Negatives) = Parse(content, compileRegex);
}
/// <summary>
/// Parses a file containing the gitignore rules.
/// </summary>
/// <param name="gitignorePath">Path to the file containing the gitignore rules.</param>
/// <param name="fileEncoding">The encoding applied to the contents of the file.</param>
/// <param name="compileRegex">If <see langword="true"/>, the Regex objects will be compiled to improve consecutive uses.</param>
public GitignoreParser(string gitignorePath, Encoding fileEncoding, bool compileRegex = false)
{
(Positives, Negatives) = Parse(File.ReadAllText(gitignorePath, fileEncoding), compileRegex);
}
/// <summary>
/// Parses the given gitignore content and returns regex objects for matching positive and negativ filters.
/// </summary>
/// <param name="content">The string containing the gitignore rules.</param>
/// <param name="compileRegex">If <see langword="true"/>, the Regex objects will be compiled to improve consecutive uses.</param>
/// <returns><see cref="Regex"/> objects for positive and negative matching for the given gitignore rules.</returns>
public static ((Regex Merged, Regex[] Individual) positives, (Regex Merged, Regex[] Individual) negatives) Parse(string content, bool compileRegex = false)
{
var regexOptions = compileRegex ? RegexOptions.Compiled : RegexOptions.None;
(List<string> positive, List<string> negative) = content
.Split(LINEBREAKS, StringSplitOptions.None)
.Select(line => line.Trim())
.Where(line => !string.IsNullOrWhiteSpace(line) && !line.StartsWith('#'))
.Aggregate<string, (List<string>, List<string>), (List<string>, List<string>)>(
(new List<string>(), new List<string>()),
((List<string> positive, List<string> negative) lists, string line) =>
{
if (line.StartsWith('!'))
lists.negative.Add(line.Substring(1));
else
lists.positive.Add(line);
return (lists.positive, lists.negative);
},
((List<string> positive, List<string> negative) lists) => lists
);
static (Regex Merged, Regex[] Individual) Submatch(List<string> list, RegexOptions regexOptions)
{
if (list.Count == 0)
{
return (MatchEmptyRegex, Array.Empty<Regex>());
}
else
{
var reList = list.OrderBy(str => str).Select(PrepareRegexPattern).ToList();
return (
new Regex($"(?:{string.Join(")|(?:", reList)})", regexOptions),
reList.Select(s => new Regex(s, regexOptions)).ToArray()
);
}
}
return (Submatch(positive, regexOptions), Submatch(negative, regexOptions));
}
/// <summary>
/// Parses a gitignore file and filters the files/directories inside the given directory recursively.
/// </summary>
/// <param name="content">The string containing the gitignore rules.</param>
/// <param name="directoryPath">The directory path to the contents of which to apply the gitignore rules.</param>
/// <param name="compileRegex">If <see langword="true"/>, the Regex objects will be compiled to improve consecutive uses.</param>
/// <returns>Files and directories filtered with the given gitignore rules.</returns>
public static (IEnumerable<string> Accepted, IEnumerable<string> Denied) Parse(string content, string directoryPath, bool compileRegex = false)
{
DirectoryInfo directory = new(directoryPath);
GitignoreParser parser = new(content, compileRegex);
var fileResults = parser.ProcessFiles(directory);
var accepted = fileResults.Where(x => x.Accepted).Select(x => x.FilePath).ToArray();
var denied = fileResults.Where(x => x.Denied).Select(x => x.FilePath).ToArray();
return (accepted, denied);
}
/// <summary>
/// Parses a gitignore file and filters the files/directories inside the given directory recursively.
/// If no directory is given, the parent directory of the gitignore file is taken.
/// </summary>
/// <param name="gitignorePath">Path to the gitignore file.</param>
/// <param name="fileEncoding">The encoding applied to the contents of the file.</param>
/// <param name="compileRegex">If <see langword="true"/>, the Regex objects will be compiled to improve consecutive uses.</param>
/// <returns>Files and directories filtered with the given gitignore rules.</returns>
/// <exception cref="DirectoryNotFoundException">Couldn't find the parent dirrectory for <paramref name="gitignorePath"/>.</exception>
public static (IEnumerable<string> Accepted, IEnumerable<string> Denied) Parse(string gitignorePath, Encoding fileEncoding, string? directoryPath = null, bool compileRegex = false)
{
DirectoryInfo directory = directoryPath != null
? new(directoryPath)
: (new FileInfo(gitignorePath).Directory ?? throw new DirectoryNotFoundException($"Couldn't find the parent dirrectory for \"{gitignorePath}\""));
GitignoreParser parser = new(gitignorePath, fileEncoding, compileRegex);
var fileResults = parser.ProcessFiles(directory);
var accepted = fileResults.Where(x => x.Accepted).Select(x => x.FilePath).ToArray();
var denied = fileResults.Where(x => x.Denied).Select(x => x.FilePath).ToArray();
return (accepted, denied);
}
/// <summary>
/// Returns a list of relative paths of all subdirectories and files under the given directory (including the given directory itself).
/// </summary>
/// <param name="directory">The directory to traverse.</param>
/// <returns>The list of relative paths of subdirectories and files.</returns>
private static string[] ListFiles(DirectoryInfo directory)
{
var directoryPath = directory.FullName;
return ((IEnumerable<string>)["/"]).Concat(
Directory.GetFiles(directoryPath, "*.*", SearchOption.AllDirectories)
.Select(f => f.Substring(directoryPath.Length + 1))
).ToArray();
}
/// <summary>
/// Returns a tuple containing information about the acceptance or denieal of a file.
/// </summary>
/// <param name="directory">The directory to traverse.</param>
/// <returns>A tuple containing information about the acceptance or denieal of a file.</returns>
private (string FilePath, bool Accepted, bool Denied)[] ProcessFiles(DirectoryInfo directory)
{
var files = ListFiles(directory);
return files.Select(f => (FilePath: f, Accepted: Accepts(f), Denied: Denies(f))).ToArray();
}
/// <summary>
/// Tests whether the given file/directory passes the gitignore filters.
/// </summary>
/// <param name="input">The file/directory path to test.</param>
/// <param name="expected">If not <see langword="null"/>, when the result of the method doesn't match the expected, print</param>
/// <returns>
/// <see langword="true"/> when the given `input` path <strong>passes</strong> the gitignore filters,
/// i.e. when the given input path is <strong>denied</strong> (<i>ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
#if DEBUG
public bool Accepts(string input, bool? expected = null)
#else
public bool Accepts(string input)
#endif
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
input = input.Replace('\\', '/');
if (!input.StartsWith('/'))
input = "/" + input;
var acceptTest = Negatives.Merged.IsMatch(input);
var denyTest = Positives.Merged.IsMatch(input);
var returnVal = acceptTest || !denyTest;
// See the test/fixtures/gitignore.manpage.txt near line 680 (grep for "uber-nasty"):
// to resolve chained rules which reject, then accept, we need to establish
// the precedence of both accept and reject parts of the compiled gitignore by
// comparing match lengths.
// Since the generated consolidated regexes are lazy, we must loop through all lines' regexes instead:
#if DEBUG
Match? acceptMatch = null, denyMatch = null;
#endif
if (acceptTest && denyTest)
{
int acceptLength = 0, denyLength = 0;
foreach (var re in Negatives.Individual)
{
var m = re.Match(input);
if (m.Success && acceptLength < m.Value.Length)
{
#if DEBUG
acceptMatch = m;
#endif
acceptLength = m.Value.Length;
}
}
foreach (var re in Positives.Individual)
{
var m = re.Match(input);
if (m.Success && denyLength < m.Value.Length)
{
#if DEBUG
denyMatch = m;
#endif
denyLength = m.Value.Length;
}
}
returnVal = acceptLength >= denyLength;
}
#if DEBUG
if (expected != null && expected != returnVal)
{
Diagnose(
"accepts",
input,
(bool)expected,
Negatives.Merged,
acceptTest,
acceptMatch,
Positives.Merged,
denyTest,
denyMatch,
"(Accept || !Deny)",
returnVal
);
}
#endif
return returnVal;
}
/// <summary>
/// Tests whether the given files/directories pass the gitignore filters.
/// </summary>
/// <param name="inputs">The file/directory paths to test.</param>
/// <returns>
/// <see cref="IEnumerable{string}"/> with the paths that <strong>pass</strong> the gitignore filters,
/// i.e. the paths that are <strong>denied</strong> (<i>ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
public IEnumerable<string> Accepted(IEnumerable<string> inputs)
{
return inputs.Where(Accepts);
}
/// <summary>
/// Tests whether the given files/directories pass the gitignore filters.
/// </summary>
/// <param name="directory">The directory to test.</param>
/// <returns>
/// <see cref="IEnumerable{string}"/> with the paths that <strong>pass</strong> the gitignore filters,
/// i.e. the paths that are <strong>denied</strong> (<i>ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
public IEnumerable<string> Accepted(DirectoryInfo directory)
{
var files = ListFiles(directory);
return files.Where(Accepts);
}
/// <summary>
/// Tests whether the given file/directory fails the gitignore filters.
/// </summary>
/// <param name="input">The file/directory path to test.</param>
/// <param name="expected">If not <see langword="null"/>, when the result of the method doesn't match the expected, print</param>
/// <returns>
/// <see langword="true"/> when the given `input` path <strong>fails</strong> the gitignore filters,
/// i.e. when the given input path is <strong>accepted</strong> (<i>not ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
#if DEBUG
public bool Denies(string input, bool? expected = null)
#else
public bool Denies(string input)
#endif
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
input = input.Replace('\\', '/');
if (!input.StartsWith('/'))
input = "/" + input;
var acceptTest = Negatives.Merged.IsMatch(input);
var denyTest = Positives.Merged.IsMatch(input);
// boolean logic:
//
// Denies = !Accepts =>
// Denies = !(Accept || !Deny) =>
// Denies = (!Accept && !!Deny) =>
// Denies = (!Accept && Deny)
var returnVal = !acceptTest && denyTest;
// See the test/fixtures/gitignore.manpage.txt near line 680 (grep for "uber-nasty"):
// to resolve chained rules which reject, then accept, we need to establish
// the precedence of both accept and reject parts of the compiled gitignore by
// comparing match lengths.
// Since the generated regexes are all set up to be GREEDY, we can use the
// consolidated regex for this, instead of having to loop through all lines' regexes:
#if DEBUG
Match? acceptMatch = null, denyMatch = null;
#endif
if (acceptTest && denyTest)
{
int acceptLength = 0, denyLength = 0;
foreach (var re in Negatives.Individual)
{
var m = re.Match(input);
if (m.Success && acceptLength < m.Value.Length)
{
#if DEBUG
acceptMatch = m;
#endif
acceptLength = m.Value.Length;
}
}
foreach (var re in Positives.Individual)
{
var m = re.Match(input);
if (m.Success && denyLength < m.Value.Length)
{
#if DEBUG
denyMatch = m;
#endif
denyLength = m.Value.Length;
}
}
returnVal = acceptLength < denyLength;
}
#if DEBUG
if (expected != null && expected != returnVal)
{
Diagnose(
"denies",
input,
(bool)expected,
Negatives.Merged,
acceptTest,
acceptMatch,
Positives.Merged,
denyTest,
denyMatch,
"(!Accept && Deny)",
returnVal
);
}
#endif
return returnVal;
}
/// <summary>
/// Tests whether the given files/directories fail the gitignore filters.
/// </summary>
/// <param name="inputs">The file/directory paths to test.</param>
/// <returns>
/// <see cref="IEnumerable{string}"/> with the paths that <strong>fail</strong> the gitignore filters,
/// i.e. the paths that are <strong>accepted</strong> (<i>not ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
public IEnumerable<string> Denied(IEnumerable<string> inputs)
{
return inputs.Where(Denies);
}
/// <summary>
/// Tests whether the given files/subdirectories under the specified directory fail the gitignore filters.
/// </summary>
/// <param name="directory">The directory to test.</param>
/// <returns>
/// <see cref="IEnumerable{string}"/> with the paths that <strong>fail</strong> the gitignore filters,
/// i.e. the paths that are <strong>accepted</strong> (<i>not ignored</i>).
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
public IEnumerable<string> Denied(DirectoryInfo directory)
{
var files = ListFiles(directory);
return files.Where(Denies);
}
/// <summary>
/// You can use this method to help construct the decision path when you
/// process nested gitignore files: gitignore filters in subdirectories
/// <strong>may</strong> override parent gitignore filters only when
/// there's actually <strong>any</strong> filter in the child gitignore
/// after all.
/// </summary>
/// <param name="input">The file/directory path to test.</param>
/// <param name="expected">If not <see langword="null"/>, when the result of the method doesn't match the expected, print</param>
/// <returns>
/// <see langword="true"/> when the given `input` path is inspected by the gitignore filters.
/// </returns>
/// <remarks>
/// <list type="bullet">
/// <item>
/// <description>
/// You <strong>must</strong> postfix a input directory with a slash
/// ('/') to ensure the gitignore rules can be applied conform spec.
/// </description>
/// </item>
/// <item>
/// <description>
/// You <strong>may</strong> prefix a input directory with a slash ('/')
/// when that directory is 'rooted' in the search directory.
/// </description>
/// </item>
/// </list>
/// </remarks>
#if DEBUG
public bool Inspects(string input, bool? expected = null)
#else
public bool Inspects(string input)
#endif
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
input = input.Replace('\\', '/');
if (!input.StartsWith('/'))
input = "/" + input;
var acceptTest = Negatives.Merged.IsMatch(input);
var denyTest = Positives.Merged.IsMatch(input);
// when any filter 'touches' the input path, it must match,
// no matter whether it's a deny or accept filter line:
var returnVal = acceptTest || denyTest;
#if DEBUG
if (expected != null && expected != returnVal)
{
Diagnose(
"inspects",
input,
(bool)expected,
Negatives.Merged,
acceptTest,
null,
Positives.Merged,
denyTest,
null,
"(Accept || Deny)",
returnVal
);
}
#endif
return returnVal;
}
[SuppressMessage("Major Code Smell", "S1121:Assignments should not be made from within sub-expressions")]
private static string PrepareRegexPattern(string pattern)
{
// https://git-scm.com/docs/gitignore#_pattern_format
//
// * ...
//
// * If there is a separator at the beginning or middle (or both) of the pattern,
// then the pattern is relative to the directory level of the particular
// .gitignore file itself.
// Otherwise the pattern may also match at any level below the .gitignore level.
//
// * ...
//
// * For example, a pattern `doc/frotz/` matches `doc/frotz` directory, but
// not `a/doc/frotz` directory; however `frotz/` matches `frotz` and `a/frotz`
// that is a directory (all paths are relative from the .gitignore file).
//
#if DEBUG
string input = pattern;
#endif
var reBuilder = new StringBuilder();
bool rooted = false, directory = false;
if (pattern.StartsWith('/'))
{
rooted = true;
pattern = pattern.Substring(1);
}
if (pattern.EndsWith('/'))
{
directory = true;
pattern = pattern.Substring(0, pattern.Length - 1);
}
string transpileRegexPart(string _re)
{
if (_re.Length == 0) return _re;
// unescape for these will be escaped again in the subsequent `.Replace(...)`,
// whether they were escaped before or not:
_re = BackslashRegex.Replace(_re, "$1");
// escape special regex characters:
_re = SpecialCharactersRegex.Replace(_re, @"\$&");
_re = QuestionMarkRegex.Replace(_re, "[^/]");
_re = SlashDoubleAsteriksSlashRegex.Replace(_re, "(?:/|(?:/.+/))");
_re = DoubleAsteriksSlashRegex.Replace(_re, "(?:|(?:.+/))");
_re = SlashDoubleAsteriksRegex.Replace(_re, _ =>
{
directory = true; // `a/**` should match `a/`, `a/b/` and `a/b`, the latter by implication of matching directory `a/`
return "(?:|(?:/.+))"; // `a/**` also accepts `a/` itself
});
_re = DoubleAsteriksRegex.Replace(_re, ".*");
// `a/*` should match `a/b` and `a/b/` but NOT `a` or `a/`
// meanwhile, `a/*/` should match `a/b/` and `a/b/c` but NOT `a` or `a/` or `a/b`
_re = SlashAsteriksEndOrSlashRegex.Replace(_re, "/[^/]+$1");
_re = AsteriksRegex.Replace(_re, "[^/]*");
_re = SlashRegex.Replace(_re, @"\/");
return _re;
}
// keep character ranges intact:
Regex rangeRe = RangeRegex;
// ^ could have used the 'y' sticky flag, but there's some trouble with infinite loops inside
// the matcher below then...
for (Match match; (match = rangeRe.Match(pattern)).Success;)
{
if (match.Groups[1].Value.Contains('/'))
{
rooted = true;
// ^ cf. man page:
//
// If there is a separator at the beginning or middle (or both)
// of the pattern, then the pattern is relative to the directory
// level of the particular .gitignore file itself. Otherwise
// the pattern may also match at any level below the .gitignore level.
}
reBuilder.Append(transpileRegexPart(match.Groups[1].Value));
reBuilder.Append('[').Append(match.Groups[2].Value).Append(']');
pattern = pattern.Substring(match.Length);
}
if (!string.IsNullOrWhiteSpace(pattern))
{
if (pattern.Contains('/'))
{
rooted = true;
// ^ cf. man page:
//
// If there is a separator at the beginning or middle (or both)
// of the pattern, then the pattern is relative to the directory
// level of the particular .gitignore file itself. Otherwise
// the pattern may also match at any level below the .gitignore level.
}
reBuilder.Append(transpileRegexPart(pattern));
}
// prep regexes assuming we'll always prefix the check string with a '/':
reBuilder.Prepend(rooted ? @"^\/" : @"\/");
// cf spec:
//
// If there is a separator at the end of the pattern then the pattern
// will only match directories, otherwise the pattern can match
// **both files and directories**. (emphasis mine)
// if `directory`: match the directory itself and anything within
// otherwise: match the file itself, or, when it is a directory, match the directory and anything within
reBuilder.Append(directory ? @"\/" : @"(?:$|\/)");
// regex validation diagnostics: better to check if the part is valid
// then to discover it's gone haywire in the big conglomerate at the end.
string re = reBuilder.ToString();
#if DEBUG
try
{
#pragma warning disable S1481 // Unused local variables should be removed
Regex regex = new($"(?:{re})"); // throws ArgumentException if a regular expression parsing error occurred.
#pragma warning restore S1481 // Unused local variables should be removed
}
catch (ArgumentException ex)
{
Console.WriteLine("Failed regex: \n\tinput: {0}\n\tregex: {1}\n\texception: {2}", input, re, ex);
}
#endif
return re;
}
#if DEBUG
public sealed class OnExpectedMatchFailEventArgs(
string query,
string input,
bool expected,
Regex acceptRe,
bool acceptTest,
Match? acceptMatch,
Regex denyRe,
bool denyTest,
Match? denyMatch,
string combine,
bool returnVal) : EventArgs
{
public string Query { get; set; } = query;
public string Input { get; set; } = input;
public bool Expected { get; set; } = expected;
public Regex AcceptRe { get; set; } = acceptRe;
public bool AcceptTest { get; set; } = acceptTest;
public Match? AcceptMatch { get; set; } = acceptMatch;
public Regex DenyRe { get; set; } = denyRe;
public bool DenyTest { get; set; } = denyTest;
public Match? DenyMatch { get; set; } = denyMatch;
public string Combine { get; set; } = combine;
public bool ReturnVal { get; set; } = returnVal;
}
/// <summary>
/// This event will be invoked if any of `Accepts()`, `Denies()` or `Inspects()`
/// fail to match the expected result.
/// </summary>
public event EventHandler<OnExpectedMatchFailEventArgs>? OnExpectedMatchFail;
/// <summary>
/// Helper invoked when any of `Accepts()`, `Denies()` or `Inspects()`
/// fail, to help the developer analyze what is going on inside:
/// some gitignore spec bits are non-intuitive / non-trivial, after all.
/// </summary>
private void Diagnose(
string query,
string input,
bool expected,
Regex acceptRe,
bool acceptTest,
Match? acceptMatch,
Regex denyRe,
bool denyTest,
Match? denyMatch,
string combine,
bool returnVal
)
{
if (OnExpectedMatchFail != null)
{
OnExpectedMatchFail(this,
new OnExpectedMatchFailEventArgs(
query,
input,
expected,
acceptRe,
acceptTest,
acceptMatch,
denyRe,
denyTest,
denyMatch,
combine,
returnVal
)
);
return;
}
Console.WriteLine(
$$"""
'{{query}}': {
'query': '{{query}}',
'input': '{{input}}',
'expected': '{{expected}}',
'acceptRe': '{{acceptRe}}',
'acceptTest': '{{acceptTest}}',
'acceptMatch': '{{acceptMatch}}',
'denyRe': '{{denyRe}}',
'denyTest': '{{denyTest}}',
'denyMatch': '{{denyMatch}}',
'combine': '{{combine}}',
'returnVal': '{{returnVal}}'
}
"""
);
}
#endif
}