xonecas.io
79113
Ma

mini-coding-agent / commit 72084db

fix: better gitignore support

We also respect child dir's ignores as well now

xonecas2026-10-12 72084db9602a81227b279b8b93a6593697d43ee8 parent 35fe0f4

2 files changed

src/gitignore.zig+229−0
@@ -0,0 +1,229 @@
1+const std = @import("std");
2+
3+/// A single compiled gitignore pattern.
4+const Pattern = struct {
5+ /// Pattern text with leading `!`, leading `/` and trailing `/` stripped.
6+ text: []const u8,
7+ /// Re-include rather than ignore.
8+ negated: bool,
9+ /// Match directories only (trailing `/`).
10+ dir_only: bool,
11+ /// Match the full path relative to the source dir, not the basename.
12+ anchored: bool,
13+};
14+
15+/// One `.gitignore` file's rules and the directory they were read from.
16+const Source = struct {
17+ base: []const u8,
18+ patterns: []const Pattern,
19+};
20+
21+/// Compile a single line. Returns null for blanks and comments.
22+fn compilePattern(a: std.mem.Allocator, raw: []const u8) ?Pattern {
23+ var line = raw;
24+ if (line.len > 0 and line[line.len - 1] == '\r') line = line[0 .. line.len - 1];
25+ var end = line.len;
26+ while (end > 0 and (line[end - 1] == ' ' or line[end - 1] == '\t')) {
27+ if (end >= 2 and line[end - 2] == '\\') break;
28+ end -= 1;
29+ }
30+ line = line[0..end];
31+ if (line.len == 0 or line[0] == '#') return null;
32+
33+ var negated = false;
34+ if (line[0] == '!') {
35+ negated = true;
36+ line = line[1..];
37+ }
38+
39+ var anchored = false;
40+ if (line.len > 0 and line[0] == '/') {
41+ anchored = true;
42+ line = line[1..];
43+ }
44+ var dir_only = false;
45+ if (line.len > 0 and line[line.len - 1] == '/') {
46+ dir_only = true;
47+ line = line[0 .. line.len - 1];
48+ }
49+ if (line.len == 0) return null;
50+ if (std.mem.indexOfScalar(u8, line, '/') != null) anchored = true;
51+
52+ return .{
53+ .text = a.dupe(u8, line) catch return null,
54+ .negated = negated,
55+ .dir_only = dir_only,
56+ .anchored = anchored,
57+ };
58+}
59+
60+fn classMatch(pat: []const u8, p: *usize, c: u8) bool {
61+ var i = p.* + 1;
62+ var negate = false;
63+ if (i < pat.len and (pat[i] == '!' or pat[i] == '^')) {
64+ negate = true;
65+ i += 1;
66+ }
67+ var matched = false;
68+ var first = true;
69+ while (i < pat.len and (pat[i] != ']' or first)) {
70+ first = false;
71+ if (i + 2 < pat.len and pat[i + 1] == '-' and pat[i + 2] != ']') {
72+ if (c >= pat[i] and c <= pat[i + 2]) matched = true;
73+ i += 3;
74+ } else {
75+ if (c == pat[i]) matched = true;
76+ i += 1;
77+ }
78+ }
79+ if (i >= pat.len) {
80+ p.* += 1;
81+ return c == '[';
82+ }
83+ p.* = i + 1;
84+ return matched != negate;
85+}
86+
87+fn matchChar(pat: []const u8, p: *usize, c: u8) bool {
88+ switch (pat[p.*]) {
89+ '?' => {
90+ p.* += 1;
91+ return true;
92+ },
93+ '[' => return classMatch(pat, p, c),
94+ '\\' => {
95+ if (p.* + 1 < pat.len) p.* += 1;
96+ const lit = pat[p.*];
97+ p.* += 1;
98+ return lit == c;
99+ },
100+ else => |ch| {
101+ p.* += 1;
102+ return ch == c;
103+ },
104+ }
105+}
106+
107+/// Glob match within one path segment (`*` and `?` never cross `/`).
108+fn segmentMatch(pat: []const u8, s: []const u8) bool {
109+ var p: usize = 0;
110+ var i: usize = 0;
111+ var star: ?usize = null;
112+ var star_i: usize = 0;
113+ while (i < s.len) {
114+ if (p < pat.len and pat[p] == '*') {
115+ star = p;
116+ p += 1;
117+ star_i = i;
118+ } else if (p < pat.len and matchChar(pat, &p, s[i])) {
119+ i += 1;
120+ } else if (star) |sp| {
121+ star_i += 1;
122+ i = star_i;
123+ p = sp + 1;
124+ } else return false;
125+ }
126+ while (p < pat.len and pat[p] == '*') p += 1;
127+ return p == pat.len;
128+}
129+
130+/// End index (exclusive) of the pattern segment starting at `start`.
131+fn segmentEnd(pat: []const u8, start: usize) usize {
132+ var i = start;
133+ while (i < pat.len) : (i += 1) {
134+ if (pat[i] == '\\') {
135+ i += 1;
136+ continue;
137+ }
138+ if (pat[i] == '/') return i;
139+ }
140+ return pat.len;
141+}
142+
143+/// Match a full relative path against a pattern; `**` spans path separators.
144+fn pathMatchFrom(pat: []const u8, pi: usize, path: []const u8) bool {
145+ if (pi >= pat.len) return path.len == 0;
146+ const pend = segmentEnd(pat, pi);
147+ const next = if (pend < pat.len) pend + 1 else pat.len;
148+ if (std.mem.eql(u8, pat[pi..pend], "**")) {
149+ if (next >= pat.len) return path.len > 0;
150+ if (pathMatchFrom(pat, next, path)) return true;
151+ var rest = path;
152+ while (rest.len > 0) {
153+ const s = std.mem.indexOfScalar(u8, rest, '/');
154+ rest = if (s) |i| rest[i + 1 ..] else "";
155+ if (pathMatchFrom(pat, next, rest)) return true;
156+ }
157+ return false;
158+ }
159+ const s = std.mem.indexOfScalar(u8, path, '/');
160+ const seg = if (s) |i| path[0..i] else path;
161+ if (!segmentMatch(pat[pi..pend], seg)) return false;
162+ return pathMatchFrom(pat, next, if (s) |i| path[i + 1 ..] else "");
163+}
164+
165+fn patternMatches(pat: Pattern, rel: []const u8, is_dir: bool) bool {
166+ if (pat.dir_only and !is_dir) return false;
167+ if (pat.anchored) return pathMatchFrom(pat.text, 0, rel);
168+ return segmentMatch(pat.text, std.fs.path.basename(rel));
169+}
170+
171+/// Strip an ancestor `base` prefix from `rel`; null when not under `base`.
172+fn stripBase(rel: []const u8, base: []const u8) ?[]const u8 {
173+ if (base.len == 0) return rel;
174+ if (rel.len > base.len and rel[base.len] == '/' and std.mem.startsWith(u8, rel, base))
175+ return rel[base.len + 1 ..];
176+ return null;
177+}
178+
179+/// Ordered set of gitignore rules, shallow sources first.
180+pub const Matcher = struct {
181+ a: std.mem.Allocator,
182+ sources: std.ArrayList(Source) = .empty,
183+
184+ pub fn init(a: std.mem.Allocator) Matcher {
185+ return .{ .a = a };
186+ }
187+
188+ pub fn deinit(self: *Matcher) void {
189+ self.sources.deinit(self.a);
190+ }
191+
192+ fn appendSource(self: *Matcher, base: []const u8, pats: []const Pattern) !void {
193+ if (pats.len == 0) return;
194+ try self.sources.append(self.a, .{ .base = base, .patterns = pats });
195+ }
196+
197+ /// Add the rules from one `.gitignore` file located in `base`.
198+ pub fn pushFile(self: *Matcher, base: []const u8, content: []const u8) !void {
199+ var pats: std.ArrayList(Pattern) = .empty;
200+ var it = std.mem.tokenizeScalar(u8, content, '\n');
201+ while (it.next()) |line| {
202+ if (compilePattern(self.a, line)) |p| try pats.append(self.a, p);
203+ }
204+ try self.appendSource(base, try pats.toOwnedSlice(self.a));
205+ }
206+
207+ /// Add caller-supplied patterns (e.g. config dirs) as a source at `base`.
208+ pub fn pushPatterns(self: *Matcher, base: []const u8, lines: []const []const u8) !void {
209+ var pats: std.ArrayList(Pattern) = .empty;
210+ for (lines) |line| {
211+ if (compilePattern(self.a, line)) |p| try pats.append(self.a, p);
212+ }
213+ try self.appendSource(base, try pats.toOwnedSlice(self.a));
214+ }
215+
216+ /// Decide whether `rel` (relative to the search root) is ignored.
217+ /// null means no rule matched.
218+ pub fn check(self: *const Matcher, rel: []const u8, is_dir: bool) ?bool {
219+ var decision: ?bool = null;
220+ for (self.sources.items) |src| {
221+ const sub = stripBase(rel, src.base) orelse continue;
222+ if (sub.len == 0) continue;
223+ for (src.patterns) |p| {
224+ if (patternMatches(p, sub, is_dir)) decision = !p.negated;
225+ }
226+ }
227+ return decision;
228+ }
229+};
src/tools/snapshot.zig+22−63
@@ -3,10 +3,12 @@ const platform = @import("../platform.zig");
33 const filesystem = @import("../filesystem.zig");
44 const text = @import("../text.zig");
55 const diff = @import("../diff.zig");
6+const gitignore = @import("../gitignore.zig");
67 const tools = @import("../tools.zig");
78
89 const max_file_bytes = 1 << 20;
910 const max_total_bytes = 32 << 20;
11+const max_ignore_bytes = 1 << 20;
1012
1113 const Kind = enum { text, binary, large, link, untracked };
1214
@@ -25,66 +27,6 @@ pub const Ignore = struct {
2527 uses_gitignore: bool = true,
2628 };
2729
28-const Pattern = struct { text: []const u8, anchored: bool };
29-
30-fn normalizePattern(raw: []const u8) ?Pattern {
31- var line = std.mem.trim(u8, raw, " \t\r");
32- if (line.len == 0 or line[0] == '#') return null;
33- if (std.mem.startsWith(u8, line, "./")) line = line[2..];
34- const anchored = line.len > 0 and line[0] == '/';
35- if (anchored) line = line[1..];
36- line = std.mem.trimEnd(u8, line, "/");
37- return if (line.len == 0) null else .{ .text = line, .anchored = anchored };
38-}
39-
40-fn ignorePatterns(tmp: std.mem.Allocator, ignore: Ignore) ![]const Pattern {
41- var list: std.ArrayList([]const u8) = .empty;
42- try list.appendSlice(tmp, ignore.dirs);
43- if (ignore.uses_gitignore) {
44- if (filesystem.readFileAlloc(tmp, ".gitignore", 1 << 20)) |gitignore| {
45- var it = std.mem.tokenizeScalar(u8, gitignore, '\n');
46- while (it.next()) |line| try list.append(tmp, line);
47- } else |_| {}
48- }
49- var out: std.ArrayList(Pattern) = .empty;
50- for (list.items) |line| {
51- if (normalizePattern(line)) |p| try out.append(tmp, p);
52- }
53- return out.items;
54-}
55-
56-fn glob(pat: []const u8, str: []const u8) bool {
57- var p: usize = 0;
58- var s: usize = 0;
59- var star: ?usize = null;
60- var star_s: usize = 0;
61- while (s < str.len) {
62- if (p < pat.len and (pat[p] == '?' or pat[p] == str[s])) {
63- p += 1;
64- s += 1;
65- } else if (p < pat.len and pat[p] == '*') {
66- star = p;
67- star_s = s;
68- p += 1;
69- } else if (star) |sp| {
70- star_s += 1;
71- s = star_s;
72- p = sp + 1;
73- } else return false;
74- }
75- while (p < pat.len and pat[p] == '*') p += 1;
76- return p == pat.len;
77-}
78-
79-fn ignored(patterns: []const Pattern, rel: []const u8) bool {
80- const name = std.fs.path.basename(rel);
81- for (patterns) |p| {
82- const subject = if (p.anchored or std.mem.indexOfScalar(u8, p.text, '/') != null) rel else name;
83- if (glob(p.text, subject)) return true;
84- }
85- return false;
86-}
87-
8830 pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
8931 var tree = Tree.init(a);
9032
@@ -92,7 +34,13 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
9234 defer arena.deinit();
9335 const tmp = arena.allocator();
9436
95- const ignores = try ignorePatterns(tmp, ignore);
37+ var matcher = gitignore.Matcher.init(tmp);
38+ if (ignore.dirs.len > 0) try matcher.pushPatterns("", ignore.dirs);
39+ if (ignore.uses_gitignore) {
40+ if (filesystem.readFileAlloc(tmp, ".gitignore", max_ignore_bytes)) |content| {
41+ try matcher.pushFile("", content);
42+ } else |_| {}
43+ }
9644
9745 var stack: std.ArrayList([]const u8) = .empty;
9846 try stack.append(tmp, "");
@@ -110,8 +58,19 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
11058 else
11159 try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, entry.name });
11260
113- if (entry.kind == .directory) {
114- if (!ignored(ignores, rel)) try stack.append(tmp, rel);
61+ const is_dir = entry.kind == .directory;
62+ if (matcher.check(rel, is_dir)) |ig| {
63+ if (ig) continue;
64+ }
65+
66+ if (is_dir) {
67+ if (ignore.uses_gitignore) {
68+ const gi = try std.fmt.allocPrint(tmp, "{s}/.gitignore", .{rel});
69+ if (filesystem.readFileAlloc(tmp, gi, max_ignore_bytes)) |content| {
70+ try matcher.pushFile(rel, content);
71+ } else |_| {}
72+ }
73+ try stack.append(tmp, rel);
11574 continue;
11675 }