Forge
csharp72c0d4fb
1using Ignore;
2
3namespace HybridCodebaseIndex.Core;
4
5internal static class GitIgnoreRules
6{
7 internal static Ignore.Ignore TryLoad(string workspaceRoot, IReadOnlyList<string> ignoreFiles)
8 {
9 var ig = new Ignore.Ignore();
10
11 // Baseline denylist already handled by WorkspaceScanner.ShouldExcludePath.
12 // Here we only apply gitignore-like rules.
13 foreach (var relPath0 in ignoreFiles)
14 {
15 var relPath = relPath0?.Trim();
16 if (string.IsNullOrWhiteSpace(relPath))
17 continue;
18
19 // Avoid path traversal / absolute paths in configuration.
20 if (Path.IsPathRooted(relPath) || relPath.Contains("..", StringComparison.Ordinal))
21 continue;
22
23 var parts = relPath.Split(['/', '\\'], StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
24 if (parts.Length == 0)
25 continue;
26
27 var abs = Path.Combine([workspaceRoot, ..parts]);
28 TryAddFileRules(ig, abs);
29 }
30
31 return ig;
32 }
33
34 internal static bool IsIgnored(Ignore.Ignore rules, string relativePathUnix)
35 => rules.IsIgnored(relativePathUnix);
36
37 private static void TryAddFileRules(Ignore.Ignore rules, string path)
38 {
39 try
40 {
41 if (!File.Exists(path))
42 return;
43
44 // The library accepts raw lines (comments/empties are handled by lib).
45 var lines = File.ReadAllLines(path);
46 rules.Add(lines);
47 }
48 catch
49 {
50 // best-effort; treat as "no extra rules"
51 }
52 }
53}
54
View only · write via MCP/CIDE