|
| 1 | +using System.Diagnostics; |
1 | 2 | using System.IO.Abstractions; |
2 | 3 | using System.Text.RegularExpressions; |
3 | 4 | using CSharpier.Core; |
@@ -49,46 +50,61 @@ public bool IsIgnored(string filePath) |
49 | 50 | public static async Task<IgnoreFile?> CreateAsync( |
50 | 51 | string baseDirectoryPath, |
51 | 52 | IFileSystem fileSystem, |
| 53 | + string? ignorePath, |
52 | 54 | CancellationToken cancellationToken |
53 | 55 | ) |
54 | 56 | { |
| 57 | + async Task<IgnoreWithBasePath> CreateIgnore(string ignoreFilePath, string? overrideBasePath) |
| 58 | + { |
| 59 | + var ignore = new IgnoreWithBasePath( |
| 60 | + overrideBasePath ?? Path.GetDirectoryName(ignoreFilePath)! |
| 61 | + ); |
| 62 | + AddDefaultRules(ignore); |
| 63 | + |
| 64 | + var content = await fileSystem.File.ReadAllTextAsync(ignoreFilePath, cancellationToken); |
| 65 | + |
| 66 | + var (positives, negatives) = GitignoreParserNet.GitignoreParser.Parse(content, true); |
| 67 | + |
| 68 | + ignore.AddPositives(positives.Merged); |
| 69 | + ignore.AddNegatives(negatives.Merged); |
| 70 | + |
| 71 | + return ignore; |
| 72 | + } |
| 73 | + |
55 | 74 | return await SharedFunc<IgnoreFile?> |
56 | 75 | .GetOrAddAsync( |
57 | 76 | baseDirectoryPath, |
58 | 77 | async () => |
59 | 78 | { |
60 | 79 | DebugLogger.Log("Creating ignore file for " + baseDirectoryPath); |
61 | | - var ignoreFilePaths = FindIgnorePaths(baseDirectoryPath, fileSystem); |
62 | | - if (ignoreFilePaths.Count == 0) |
| 80 | + if (ignorePath is not null) |
63 | 81 | { |
64 | | - var ignore = new IgnoreWithBasePath(baseDirectoryPath); |
65 | | - AddDefaultRules(ignore); |
66 | | - return new IgnoreFile([ignore]); |
67 | | - } |
| 82 | + if (!fileSystem.File.Exists(ignorePath)) |
| 83 | + { |
| 84 | + throw new Exception("There was no ignore file found at " + ignorePath); |
| 85 | + } |
68 | 86 |
|
69 | | - var ignores = new List<IgnoreWithBasePath>(); |
70 | | - foreach (var ignoreFilePath in ignoreFilePaths) |
| 87 | + DebugLogger.Log("Using ignorePath: " + ignorePath); |
| 88 | + return new IgnoreFile([await CreateIgnore(ignorePath, baseDirectoryPath)]); |
| 89 | + } |
| 90 | + else |
71 | 91 | { |
72 | | - var ignore = new IgnoreWithBasePath(Path.GetDirectoryName(ignoreFilePath)!); |
73 | | - AddDefaultRules(ignore); |
74 | | - |
75 | | - var content = await fileSystem.File.ReadAllTextAsync( |
76 | | - ignoreFilePath, |
77 | | - cancellationToken |
78 | | - ); |
79 | | - |
80 | | - var (positives, negatives) = GitignoreParserNet.GitignoreParser.Parse( |
81 | | - content, |
82 | | - true |
83 | | - ); |
84 | | - |
85 | | - ignore.AddPositives(positives.Merged); |
86 | | - ignore.AddNegatives(negatives.Merged); |
87 | | - |
88 | | - ignores.Add(ignore); |
| 92 | + var ignoreFilePaths = FindIgnorePaths(baseDirectoryPath, fileSystem); |
| 93 | + if (ignoreFilePaths.Count == 0) |
| 94 | + { |
| 95 | + var ignore = new IgnoreWithBasePath(baseDirectoryPath); |
| 96 | + AddDefaultRules(ignore); |
| 97 | + return new IgnoreFile([ignore]); |
| 98 | + } |
| 99 | + |
| 100 | + var ignores = new List<IgnoreWithBasePath>(); |
| 101 | + foreach (var ignoreFilePath in ignoreFilePaths) |
| 102 | + { |
| 103 | + ignores.Add(await CreateIgnore(ignoreFilePath, null)); |
| 104 | + } |
| 105 | + |
| 106 | + return new IgnoreFile(ignores); |
89 | 107 | } |
90 | | - |
91 | | - return new IgnoreFile(ignores); |
92 | 108 | }, |
93 | 109 | cancellationToken |
94 | 110 | ) |
@@ -164,6 +180,8 @@ private class IgnoreWithBasePath(string basePath) |
164 | 180 |
|
165 | 181 | public (bool hasMatchingRule, bool isIgnored) IsIgnored(string path) |
166 | 182 | { |
| 183 | + DebugLogger.Log("path: " + path); |
| 184 | + DebugLogger.Log("basePath: " + basePath); |
167 | 185 | if (!path.StartsWith(basePath, StringComparison.Ordinal)) |
168 | 186 | { |
169 | 187 | return (false, false); |
|
0 commit comments