Skip to content

Commit a9db1fd

Browse files
committed
pirate: partial filter capability - add FilterSpec and cmd_clone parsing
1 parent ab79ff5 commit a9db1fd

3 files changed

Lines changed: 117 additions & 1 deletion

File tree

‎src/cmd_clone.zig‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const succinct_mod = @import("succinct.zig");
88
const fetch_cmd = helpers.fetch_cmd;
99
const cmd_checkout = @import("cmd_checkout.zig");
1010

11+
const filter_spec_mod = @import("git/filter_spec.zig");
1112
// Re-export commonly used types from helpers
1213
const objects = helpers.objects;
1314
const index_mod = helpers.index_mod;
@@ -54,6 +55,7 @@ pub fn cmdClone(allocator: std.mem.Allocator, args: *platform_mod.ArgIterator, p
5455
var clone_depth: u32 = 0;
5556
var shallow_since: ?[]const u8 = null;
5657
var shallow_exclude: ?[]const u8 = null;
58+
var filter_spec: ?filter_spec_mod.FilterSpec = null;
5759
{
5860
var i: usize = 0;
5961
while (i < all_args.items.len) : (i += 1) {
@@ -82,6 +84,19 @@ pub fn cmdClone(allocator: std.mem.Allocator, args: *platform_mod.ArgIterator, p
8284
i += 1; // skip the value
8385
}
8486
} else if (std.mem.startsWith(u8, arg, "--shallow-exclude=")) {
87+
} else if (std.mem.eql(u8, arg, "--filter")) {
88+
if (i + 1 < all_args.items.len) {
89+
filter_spec = filter_spec_mod.FilterSpec.parse(allocator, all_args.items[i + 1]) catch {
90+
try platform_impl.writeStderr("error: invalid filter specification\n");
91+
return;
92+
};
93+
i += 1; // skip the value
94+
}
95+
} else if (std.mem.startsWith(u8, arg, "--filter=")) {
96+
filter_spec = filter_spec_mod.FilterSpec.parse(allocator, arg["--filter=".len..]) catch {
97+
try platform_impl.writeStderr("error: invalid filter specification\n");
98+
return;
99+
};
85100
shallow_exclude = arg["--shallow-exclude=".len..];
86101
}
87102
}

‎src/git/filter_spec.zig‎

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}

‎src/git/git_serve.zig‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const filter_spec_mod = @import("filter_spec.zig");
23
const objects = @import("objects.zig");
34

45
/// A ref entry for advertisement.
@@ -226,7 +227,7 @@ pub const GitServer = struct {
226227
ref_list.deinit();
227228
}
228229

229-
const capabilities = "multi_ack thin-pack side-band side-band-64k ofs-delta shallow deepen-since deepen-not deepen-relative no-progress include-tag multi_ack_detailed symref=HEAD:refs/heads/master object-format=sha1 agent=ziggit/1.0";
230+
const capabilities = "multi_ack thin-pack side-band side-band-64k ofs-delta shallow deepen-since deepen-not deepen-relative filter no-progress include-tag multi_ack_detailed symref=HEAD:refs/heads/master object-format=sha1 agent=ziggit/1.0";
230231

231232
// Build and send ref advertisement
232233
var out = std.array_list.Managed(u8).init(self.allocator);

0 commit comments

Comments
 (0)