@@ -1,5 +1,6 @@
11 const std = @import("std");
22 const platform = @import("../platform.zig");
3+const util = @import("../util.zig");
34 const diff = @import("../diff.zig");
45 const common = @import("common.zig");
56
@@ -7,12 +8,6 @@ const max_file_bytes = 1 << 20;
78 const max_total_bytes = 32 << 20;
89 const binary_sniff = 8192;
910
10-const ignored_dirs = [_][]const u8{
11- ".git", ".hg", ".svn", "node_modules",
12- ".venv", "venv", "__pycache__", ".mypy_cache",
13- ".pytest_cache",
14-};
15-
1611 const Kind = enum { text, binary, large, link, untracked };
1712
1813 const FileState = struct {
@@ -25,20 +20,37 @@ const FileState = struct {
2520
2621 pub const Tree = std.StringHashMap(FileState);
2722
28-fn ignored(name: []const u8) bool {
29- for (ignored_dirs) |d| {
30- if (std.mem.eql(u8, d, name)) return true;
23+pub const Ignore = struct {
24+ dirs: []const []const u8 = &.{},
25+ uses_gitignore: bool = false,
26+};
27+
28+fn ignoreSet(tmp: std.mem.Allocator, ignore: Ignore) !std.StringHashMap(void) {
29+ var set = std.StringHashMap(void).init(tmp);
30+ var list: std.ArrayList([]const u8) = .empty;
31+ try list.appendSlice(tmp, ignore.dirs);
32+ if (ignore.uses_gitignore) {
33+ if (util.readFileAlloc(tmp, ".gitignore", 1 << 20)) |text| {
34+ var it = std.mem.tokenizeScalar(u8, text, '\n');
35+ while (it.next()) |line| try list.append(tmp, line);
36+ } else |_| {}
37+ }
38+ for (list.items) |dir| {
39+ const gop = try set.getOrPut(dir);
40+ if (!gop.found_existing) gop.key_ptr.* = dir;
3141 }
32- return false;
42+ return set;
3343 }
3444
35-pub fn capture(a: std.mem.Allocator) !Tree {
45+pub fn capture(a: std.mem.Allocator, ignore: Ignore) !Tree {
3646 var tree = Tree.init(a);
3747
3848 var arena = std.heap.ArenaAllocator.init(a);
3949 defer arena.deinit();
4050 const tmp = arena.allocator();
4151
52+ const ignores = try ignoreSet(tmp, ignore);
53+
4254 var stack: std.ArrayList([]const u8) = .empty;
4355 try stack.append(tmp, "");
4456
@@ -56,7 +68,7 @@ pub fn capture(a: std.mem.Allocator) !Tree {
5668 try std.fmt.allocPrint(a, "{s}/{s}", .{ dir_rel, entry.name });
5769
5870 if (entry.kind == .directory) {
59- if (!ignored(entry.name)) try stack.append(tmp, rel);
71+ if (!ignores.contains(entry.name)) try stack.append(tmp, rel);
6072 continue;
6173 }