@@ -1,12 +1,12 @@
11 const std = @import("std");
22 const platform = @import("../platform.zig");
33 const filesystem = @import("../filesystem.zig");
4+const text = @import("../text.zig");
45 const diff = @import("../diff.zig");
56 const tools = @import("../tools.zig");
67
78 const max_file_bytes = 1 << 20;
89 const max_total_bytes = 32 << 20;
9-const binary_sniff = 8192;
1010
1111 const Kind = enum { text, binary, large, link, untracked };
1212
@@ -25,29 +25,64 @@ pub const Ignore = struct {
2525 uses_gitignore: bool = true,
2626 };
2727
28-fn normalizePattern(raw: []const u8) ?[]const u8 {
28+const Pattern = struct { text: []const u8, anchored: bool };
29+
30+fn normalizePattern(raw: []const u8) ?Pattern {
2931 var line = std.mem.trim(u8, raw, " \t\r");
3032 if (line.len == 0 or line[0] == '#') return null;
3133 if (std.mem.startsWith(u8, line, "./")) line = line[2..];
34+ const anchored = line.len > 0 and line[0] == '/';
35+ if (anchored) line = line[1..];
3236 line = std.mem.trimEnd(u8, line, "/");
33- return if (line.len == 0) null else line;
37+ return if (line.len == 0) null else .{ .text = line, .anchored = anchored };
3438 }
3539
36-fn ignoreSet(tmp: std.mem.Allocator, ignore: Ignore) !std.StringHashMap(void) {
37- var set = std.StringHashMap(void).init(tmp);
40+fn ignorePatterns(tmp: std.mem.Allocator, ignore: Ignore) ![]const Pattern {
3841 var list: std.ArrayList([]const u8) = .empty;
3942 try list.appendSlice(tmp, ignore.dirs);
4043 if (ignore.uses_gitignore) {
41- if (filesystem.readFileAlloc(tmp, ".gitignore", 1 << 20)) |text| {
42- var it = std.mem.tokenizeScalar(u8, text, '\n');
44+ if (filesystem.readFileAlloc(tmp, ".gitignore", 1 << 20)) |gitignore| {
45+ var it = std.mem.tokenizeScalar(u8, gitignore, '\n');
4346 while (it.next()) |line| try list.append(tmp, line);
4447 } else |_| {}
4548 }
46- for (list.items) |dir| {
47- const name = normalizePattern(dir) orelse continue;
48- try set.put(name, {});
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;
4984 }
50- return set;
85+ return false;
5186 }
5287
5388 pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
@@ -57,7 +92,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
5792 defer arena.deinit();
5893 const tmp = arena.allocator();
5994
60- const ignores = try ignoreSet(tmp, ignore);
95+ const ignores = try ignorePatterns(tmp, ignore);
6196
6297 var stack: std.ArrayList([]const u8) = .empty;
6398 try stack.append(tmp, "");
@@ -76,7 +111,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
76111 try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, entry.name });
77112
78113 if (entry.kind == .directory) {
79- if (!ignores.contains(entry.name)) try stack.append(tmp, rel);
114+ if (!ignored(ignores, rel)) try stack.append(tmp, rel);
80115 continue;
81116 }
82117
@@ -104,8 +139,7 @@ pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
104139 try tree.put(rel, state);
105140 continue;
106141 };
107- const sniff = @min(buf.len, binary_sniff);
108- if (std.mem.indexOfScalar(u8, buf[0..sniff], 0) != null) {
142+ if (text.isBinary(buf)) {
109143 state.kind = .binary;
110144 } else {
111145 state.kind = .text;