|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +/// Git partial clone filter specification |
| 4 | +pub const FilterSpec = union(enum) { |
| 5 | + /// No filtering - include all objects |
| 6 | + none, |
| 7 | + /// Skip all blob objects (blobless clone) |
| 8 | + blob_none, |
| 9 | + /// Skip trees beyond specified depth (0 = no trees) |
| 10 | + tree_depth: u32, |
| 11 | + /// Skip blobs larger than specified size in bytes |
| 12 | + blob_limit: u64, |
| 13 | + |
| 14 | + /// Parse a filter specification string |
| 15 | + pub fn parse(allocator: std.mem.Allocator, filter_str: []const u8) !FilterSpec { |
| 16 | + _ = allocator; // May be needed for future complex filters |
| 17 | + |
| 18 | + if (std.mem.eql(u8, filter_str, "blob:none")) { |
| 19 | + return FilterSpec.blob_none; |
| 20 | + } |
| 21 | + |
| 22 | + if (std.mem.startsWith(u8, filter_str, "tree:")) { |
| 23 | + const depth_str = filter_str["tree:".len..]; |
| 24 | + const depth = std.fmt.parseInt(u32, depth_str, 10) catch return error.InvalidFilter; |
| 25 | + return FilterSpec{ .tree_depth = depth }; |
| 26 | + } |
| 27 | + |
| 28 | + if (std.mem.startsWith(u8, filter_str, "blob:limit=")) { |
| 29 | + const limit_str = filter_str["blob:limit=".len..]; |
| 30 | + const limit = std.fmt.parseInt(u64, limit_str, 10) catch return error.InvalidFilter; |
| 31 | + return FilterSpec{ .blob_limit = limit }; |
| 32 | + } |
| 33 | + |
| 34 | + return error.InvalidFilter; |
| 35 | + } |
| 36 | + |
| 37 | + /// Convert filter to string for protocol transmission |
| 38 | + pub fn toString(self: FilterSpec, allocator: std.mem.Allocator) ![]u8 { |
| 39 | + return switch (self) { |
| 40 | + .none => try allocator.dupe(u8, ""), |
| 41 | + .blob_none => try allocator.dupe(u8, "blob:none"), |
| 42 | + .tree_depth => |depth| try std.fmt.allocPrint(allocator, "tree:{d}", .{depth}), |
| 43 | + .blob_limit => |limit| try std.fmt.allocPrint(allocator, "blob:limit={d}", .{limit}), |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + /// Check if an object should be included based on this filter |
| 48 | + pub fn shouldInclude(self: FilterSpec, obj_type: ObjectType, obj_size: u64) bool { |
| 49 | + return switch (self) { |
| 50 | + .none => true, |
| 51 | + .blob_none => obj_type != .blob, |
| 52 | + .tree_depth => |depth| if (obj_type == .tree and depth == 0) false else true, |
| 53 | + .blob_limit => |limit| if (obj_type == .blob and obj_size > limit) false else true, |
| 54 | + }; |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +pub const ObjectType = enum { |
| 59 | + commit, |
| 60 | + tree, |
| 61 | + blob, |
| 62 | + tag, |
| 63 | +}; |
| 64 | + |
| 65 | +test "FilterSpec.parse" { |
| 66 | + const allocator = std.testing.allocator; |
| 67 | + |
| 68 | + // Test blob:none |
| 69 | + const blob_none = try FilterSpec.parse(allocator, "blob:none"); |
| 70 | + try std.testing.expect(blob_none == .blob_none); |
| 71 | + |
| 72 | + // Test tree:0 |
| 73 | + const tree_0 = try FilterSpec.parse(allocator, "tree:0"); |
| 74 | + try std.testing.expect(tree_0 == .tree_depth and tree_0.tree_depth == 0); |
| 75 | + |
| 76 | + // Test blob:limit=1024 |
| 77 | + const blob_limit = try FilterSpec.parse(allocator, "blob:limit=1024"); |
| 78 | + try std.testing.expect(blob_limit == .blob_limit and blob_limit.blob_limit == 1024); |
| 79 | + |
| 80 | + // Test invalid filter |
| 81 | + try std.testing.expectError(error.InvalidFilter, FilterSpec.parse(allocator, "invalid:filter")); |
| 82 | +} |
| 83 | + |
| 84 | +test "FilterSpec.shouldInclude" { |
| 85 | + const blob_none = FilterSpec.blob_none; |
| 86 | + try std.testing.expect(blob_none.shouldInclude(.commit, 100)); |
| 87 | + try std.testing.expect(blob_none.shouldInclude(.tree, 100)); |
| 88 | + try std.testing.expect(!blob_none.shouldInclude(.blob, 100)); |
| 89 | + try std.testing.expect(blob_none.shouldInclude(.tag, 100)); |
| 90 | + |
| 91 | + const tree_0 = FilterSpec{ .tree_depth = 0 }; |
| 92 | + try std.testing.expect(tree_0.shouldInclude(.commit, 100)); |
| 93 | + try std.testing.expect(!tree_0.shouldInclude(.tree, 100)); |
| 94 | + try std.testing.expect(tree_0.shouldInclude(.blob, 100)); |
| 95 | + |
| 96 | + const blob_limit = FilterSpec{ .blob_limit = 1024 }; |
| 97 | + try std.testing.expect(blob_limit.shouldInclude(.blob, 512)); |
| 98 | + try std.testing.expect(!blob_limit.shouldInclude(.blob, 2048)); |
| 99 | + try std.testing.expect(blob_limit.shouldInclude(.commit, 2048)); |
| 100 | +} |
0 commit comments