79113
Ma
mini-coding-agent / files
mini-coding-agent / src / gitignore.zig
6.2 KB
1const std = @import("std"); 2 3const Pattern = struct { 4 text: []const u8, 5 negated: bool, 6 dir_only: bool, 7 anchored: bool, 8}; 9 10const Source = struct { 11 base: []const u8, 12 patterns: []const Pattern, 13}; 14 15fn compilePattern(a: std.mem.Allocator, raw: []const u8) ?Pattern { 16 var line = raw; 17 if (line.len > 0 and line[line.len - 1] == '\r') line = line[0 .. line.len - 1]; 18 var end = line.len; 19 while (end > 0 and (line[end - 1] == ' ' or line[end - 1] == '\t')) { 20 if (end >= 2 and line[end - 2] == '\\') break; 21 end -= 1; 22 } 23 line = line[0..end]; 24 if (line.len == 0 or line[0] == '#') return null; 25 26 var negated = false; 27 if (line[0] == '!') { 28 negated = true; 29 line = line[1..]; 30 } 31 32 var anchored = false; 33 if (line.len > 0 and line[0] == '/') { 34 anchored = true; 35 line = line[1..]; 36 } 37 var dir_only = false; 38 if (line.len > 0 and line[line.len - 1] == '/') { 39 dir_only = true; 40 line = line[0 .. line.len - 1]; 41 } 42 if (line.len == 0) return null; 43 if (std.mem.indexOfScalar(u8, line, '/') != null) anchored = true; 44 45 return .{ 46 .text = a.dupe(u8, line) catch return null, 47 .negated = negated, 48 .dir_only = dir_only, 49 .anchored = anchored, 50 }; 51} 52 53fn classMatch(pat: []const u8, p: *usize, c: u8) bool { 54 var i = p.* + 1; 55 var negate = false; 56 if (i < pat.len and (pat[i] == '!' or pat[i] == '^')) { 57 negate = true; 58 i += 1; 59 } 60 var matched = false; 61 var first = true; 62 while (i < pat.len and (pat[i] != ']' or first)) { 63 first = false; 64 if (i + 2 < pat.len and pat[i + 1] == '-' and pat[i + 2] != ']') { 65 if (c >= pat[i] and c <= pat[i + 2]) matched = true; 66 i += 3; 67 } else { 68 if (c == pat[i]) matched = true; 69 i += 1; 70 } 71 } 72 if (i >= pat.len) { 73 p.* += 1; 74 return c == '['; 75 } 76 p.* = i + 1; 77 return matched != negate; 78} 79 80fn matchChar(pat: []const u8, p: *usize, c: u8) bool { 81 switch (pat[p.*]) { 82 '?' => { 83 p.* += 1; 84 return true; 85 }, 86 '[' => return classMatch(pat, p, c), 87 '\\' => { 88 if (p.* + 1 < pat.len) p.* += 1; 89 const lit = pat[p.*]; 90 p.* += 1; 91 return lit == c; 92 }, 93 else => |ch| { 94 p.* += 1; 95 return ch == c; 96 }, 97 } 98} 99 100fn segmentMatch(pat: []const u8, s: []const u8) bool { 101 var p: usize = 0; 102 var i: usize = 0; 103 var star: ?usize = null; 104 var star_i: usize = 0; 105 while (i < s.len) { 106 if (p < pat.len and pat[p] == '*') { 107 star = p; 108 p += 1; 109 star_i = i; 110 } else if (p < pat.len and matchChar(pat, &p, s[i])) { 111 i += 1; 112 } else if (star) |sp| { 113 star_i += 1; 114 i = star_i; 115 p = sp + 1; 116 } else return false; 117 } 118 while (p < pat.len and pat[p] == '*') p += 1; 119 return p == pat.len; 120} 121 122fn segmentEnd(pat: []const u8, start: usize) usize { 123 var i = start; 124 while (i < pat.len) : (i += 1) { 125 if (pat[i] == '\\') { 126 i += 1; 127 continue; 128 } 129 if (pat[i] == '/') return i; 130 } 131 return pat.len; 132} 133 134fn pathMatchFrom(pat: []const u8, pi: usize, path: []const u8) bool { 135 if (pi >= pat.len) return path.len == 0; 136 const pend = segmentEnd(pat, pi); 137 const next = if (pend < pat.len) pend + 1 else pat.len; 138 if (std.mem.eql(u8, pat[pi..pend], "**")) { 139 if (next >= pat.len) return path.len > 0; 140 if (pathMatchFrom(pat, next, path)) return true; 141 var rest = path; 142 while (rest.len > 0) { 143 const s = std.mem.indexOfScalar(u8, rest, '/'); 144 rest = if (s) |i| rest[i + 1 ..] else ""; 145 if (pathMatchFrom(pat, next, rest)) return true; 146 } 147 return false; 148 } 149 const s = std.mem.indexOfScalar(u8, path, '/'); 150 const seg = if (s) |i| path[0..i] else path; 151 if (!segmentMatch(pat[pi..pend], seg)) return false; 152 return pathMatchFrom(pat, next, if (s) |i| path[i + 1 ..] else ""); 153} 154 155fn patternMatches(pat: Pattern, rel: []const u8, is_dir: bool) bool { 156 if (pat.dir_only and !is_dir) return false; 157 if (pat.anchored) return pathMatchFrom(pat.text, 0, rel); 158 return segmentMatch(pat.text, std.fs.path.basename(rel)); 159} 160 161fn stripBase(rel: []const u8, base: []const u8) ?[]const u8 { 162 if (base.len == 0) return rel; 163 if (rel.len > base.len and rel[base.len] == '/' and std.mem.startsWith(u8, rel, base)) 164 return rel[base.len + 1 ..]; 165 return null; 166} 167 168pub const Matcher = struct { 169 a: std.mem.Allocator, 170 sources: std.ArrayList(Source) = .empty, 171 172 pub fn init(a: std.mem.Allocator) Matcher { 173 return .{ .a = a }; 174 } 175 176 pub fn deinit(self: *Matcher) void { 177 self.sources.deinit(self.a); 178 } 179 180 fn appendSource(self: *Matcher, base: []const u8, pats: []const Pattern) !void { 181 if (pats.len == 0) return; 182 try self.sources.append(self.a, .{ .base = base, .patterns = pats }); 183 } 184 185 pub fn pushFile(self: *Matcher, base: []const u8, content: []const u8) !void { 186 var pats: std.ArrayList(Pattern) = .empty; 187 var it = std.mem.tokenizeScalar(u8, content, '\n'); 188 while (it.next()) |line| { 189 if (compilePattern(self.a, line)) |p| try pats.append(self.a, p); 190 } 191 try self.appendSource(base, try pats.toOwnedSlice(self.a)); 192 } 193 194 pub fn pushPatterns(self: *Matcher, base: []const u8, lines: []const []const u8) !void { 195 var pats: std.ArrayList(Pattern) = .empty; 196 for (lines) |line| { 197 if (compilePattern(self.a, line)) |p| try pats.append(self.a, p); 198 } 199 try self.appendSource(base, try pats.toOwnedSlice(self.a)); 200 } 201 202 pub fn check(self: *const Matcher, rel: []const u8, is_dir: bool) ?bool { 203 var decision: ?bool = null; 204 for (self.sources.items) |src| { 205 const sub = stripBase(rel, src.base) orelse continue; 206 if (sub.len == 0) continue; 207 for (src.patterns) |p| { 208 if (patternMatches(p, sub, is_dir)) decision = !p.negated; 209 } 210 } 211 return decision; 212 } 213};