╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const hx = @import("haxy"); 4 const xit = hx.xit; 5 const evt = hx.event; 6 const fork = hx.fork; 7 const pch = hx.pch; 8 const rp = xit.repo; 9 const rf = xit.ref; 10 const hash = xit.hash; 11 const net = xit.net; 12 13 test "fetch small" { 14 const io = std.testing.io; 15 const allocator = std.testing.allocator; 16 try testFetch(.xit, .{ .wire = .http }, .sha1, 3001, io, allocator); 17 try testFetch(.xit, .{ .wire = .http }, .sha256, 3003, io, allocator); 18 try testFetch(.xit, .{ .wire = .ssh }, .sha1, 3005, io, allocator); 19 try testFetch(.xit, .{ .wire = .ssh }, .sha256, 3007, io, allocator); 20 } 21 22 test "push small" { 23 const io = std.testing.io; 24 const allocator = std.testing.allocator; 25 try testPush(.xit, .{ .wire = .ssh }, .sha1, 3101, io, allocator); 26 try testPush(.xit, .{ .wire = .ssh }, .sha256, 3103, io, allocator); 27 } 28 29 test "push creates missing repo under serve" { 30 const io = std.testing.io; 31 const allocator = std.testing.allocator; 32 // the server creates missing repositories as sha1 33 try testPushCreatesMissingRepo(.xit, .{ .wire = .ssh }, 3201, io, allocator); 34 } 35 36 test "push events" { 37 const io = std.testing.io; 38 const allocator = std.testing.allocator; 39 try testPushEvents(.xit, .{ .wire = .ssh }, .sha1, 3301, io, allocator); 40 try testPushEvents(.xit, .{ .wire = .ssh }, .sha256, 3303, io, allocator); 41 } 42 43 test "clone small" { 44 const io = std.testing.io; 45 const allocator = std.testing.allocator; 46 try testClone(.xit, .{ .wire = .http }, false, .sha1, 3401, io, allocator); 47 try testClone(.xit, .{ .wire = .http }, false, .sha256, 3403, io, allocator); 48 try testClone(.xit, .{ .wire = .ssh }, false, .sha1, 3405, io, allocator); 49 try testClone(.xit, .{ .wire = .ssh }, false, .sha256, 3407, io, allocator); 50 } 51 52 test "clone small subprocess" { 53 const io = std.testing.io; 54 const allocator = std.testing.allocator; 55 try testClone(.git, .{ .wire = .http }, true, .sha1, 3501, io, allocator); 56 try testClone(.git, .{ .wire = .http }, true, .sha256, 3503, io, allocator); 57 try testClone(.git, .{ .wire = .ssh }, true, .sha1, 3505, io, allocator); 58 try testClone(.git, .{ .wire = .ssh }, true, .sha256, 3507, io, allocator); 59 } 60 61 test "fetch large subprocess" { 62 const io = std.testing.io; 63 const allocator = std.testing.allocator; 64 try testFetchLarge(.git, .{ .wire = .http }, true, .sha1, 3601, io, allocator); 65 try testFetchLarge(.git, .{ .wire = .http }, true, .sha256, 3603, io, allocator); 66 try testFetchLarge(.git, .{ .wire = .ssh }, true, .sha1, 3605, io, allocator); 67 try testFetchLarge(.git, .{ .wire = .ssh }, true, .sha256, 3607, io, allocator); 68 } 69 70 test "push large subprocess" { 71 const io = std.testing.io; 72 const allocator = std.testing.allocator; 73 try testPushLarge(.git, .{ .wire = .ssh }, true, .sha1, 3701, io, allocator); 74 try testPushLarge(.git, .{ .wire = .ssh }, true, .sha256, 3703, io, allocator); 75 } 76 77 test "push fork" { 78 const io = std.testing.io; 79 const allocator = std.testing.allocator; 80 try testPushFork(.sha1, 3801, io, allocator); 81 try testPushFork(.sha256, 3803, io, allocator); 82 } 83 84 fn runServer( 85 io: std.Io, 86 allocator: std.mem.Allocator, 87 temp_path: []const u8, 88 comptime port: u16, 89 ) !std.process.Child { 90 { 91 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 92 defer allocator.free(priv_key_path); 93 const priv_key_file = try std.Io.Dir.cwd().createFile(io, priv_key_path, .{}); 94 defer priv_key_file.close(io); 95 try priv_key_file.writeStreamingAll(io, 96 \\-----BEGIN OPENSSH PRIVATE KEY----- 97 \\b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 98 \\QyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22JgAAAJA+7hikPu4Y 99 \\pAAAAAtzc2gtZWQyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22Jg 100 \\AAAEDVlopOMnKt/7by/IA8VZvQXUS/O6VLkixOqnnahUdPCKeIs8mJqigBZ5y84J4COgnA 101 \\JJ5bHPKy+lM2SliMXbYmAAAAC3JhZGFyQHJvYXJrAQI= 102 \\-----END OPENSSH PRIVATE KEY----- 103 \\ 104 ); 105 if (.windows != builtin.os.tag) { 106 try priv_key_file.setPermissions(io, @enumFromInt(0o600)); 107 } 108 } 109 110 if (builtin.os.tag == .windows) { 111 // openssh needs an acl granting access only to the current user. 112 // use cwd so the temp path doesn't need powershell quoting. 113 var permissions_process = try std.process.spawn(io, .{ 114 .argv = &.{ 115 "powershell.exe", "-NoProfile", "-NonInteractive", "-Command", 116 \\$ErrorActionPreference = 'Stop' 117 \\$user = [System.Security.Principal.WindowsIdentity]::GetCurrent().User 118 \\$acl = [System.Security.AccessControl.FileSecurity]::new() 119 \\$acl.SetOwner($user) 120 \\$acl.SetAccessRuleProtection($true, $false) 121 \\$rule = [System.Security.AccessControl.FileSystemAccessRule]::new($user, 'FullControl', 'Allow') 122 \\$acl.AddAccessRule($rule) 123 \\Set-Acl -LiteralPath .\key -AclObject $acl 124 , 125 }, 126 .cwd = .{ .path = temp_path }, 127 .stdin = .ignore, 128 .stdout = .ignore, 129 .stderr = .inherit, 130 }); 131 defer permissions_process.kill(io); 132 const term = try permissions_process.wait(io); 133 if (term != .exited or term.exited != 0) return error.SshKeyPermissionsFailed; 134 } 135 136 // seed the admin event store so the server can resolve <owner>/<repo> paths 137 // and authenticate pushes from the dev key above 138 try setupAdmin(io, allocator, temp_path); 139 140 const cwd_path = try std.process.currentPathAlloc(io, allocator); 141 defer allocator.free(cwd_path); 142 const haxy_path = try std.fs.path.join(allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 143 defer allocator.free(haxy_path); 144 145 const http_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port}); 146 const ssh_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{port + 1}); 147 148 const process = try std.process.spawn(io, .{ 149 .argv = &.{ haxy_path, "serve", "--http-listen", http_listen_arg, "--ssh-listen", ssh_listen_arg, "--data-dir", temp_path }, 150 .stdin = .ignore, 151 .stdout = .ignore, 152 .stderr = .ignore, 153 }); 154 155 const address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", port); 156 for (0..50) |_| { 157 const stream = address.connect(io, .{ .mode = .stream }) catch { 158 try std.Io.sleep(io, .fromMilliseconds(100), .real); 159 continue; 160 }; 161 stream.close(io); 162 break; 163 } 164 165 return process; 166 } 167 168 fn testFetch( 169 comptime repo_kind: rp.RepoKind, 170 comptime transport_def: net.TransportDefinition, 171 comptime hash_kind: hash.HashKind, 172 comptime port: u16, 173 io: std.Io, 174 allocator: std.mem.Allocator, 175 ) !void { 176 // create the temp dir 177 var temp = std.testing.tmpDir(.{}); 178 defer temp.cleanup(); 179 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 180 defer allocator.free(temp_path); 181 182 // init server 183 var server_process = try runServer(io, allocator, temp_path, port); 184 defer _ = server_process.kill(io); 185 186 // register the repo under admin and locate its on-disk directory 187 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 188 defer allocator.free(server_path); 189 190 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 191 defer server_repo.deinit(io, allocator); 192 193 // make a commit 194 const commit1 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 195 196 // export server repo 197 { 198 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 199 defer export_file.close(io); 200 201 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowAnySHA1InWant", .value = "true" }); 202 } 203 204 // add a tag 205 _ = try server_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 206 207 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 208 defer allocator.free(client_path); 209 210 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 211 defer client_repo.deinit(io, allocator); 212 213 // add remote 214 { 215 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 216 defer allocator.free(remote_url); 217 218 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 219 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 220 } 221 222 // create refspec with oid as a test 223 const oid_refspec = try std.fmt.allocPrint(allocator, "+{s}:refs/heads/foo", .{&commit1}); 224 defer allocator.free(oid_refspec); 225 226 const refspecs = &.{ 227 "+refs/heads/master:refs/heads/master", 228 oid_refspec, 229 }; 230 231 const is_ssh = switch (transport_def) { 232 .file => false, 233 .wire => |wire_kind| .ssh == wire_kind, 234 }; 235 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 236 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 237 238 try client_repo.fetch( 239 io, 240 allocator, 241 "origin", 242 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 243 .command = ssh_cmd_maybe, 244 } } }, 245 ); 246 247 // update the working dir 248 try client_repo.restore(io, allocator, "."); 249 250 // make sure fetch was successful 251 { 252 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 253 defer hello_txt.close(io); 254 255 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 256 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 257 258 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 259 try std.testing.expectEqualStrings(&commit1, &oid_master); 260 } 261 262 // make another commit 263 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "goodbye" }); 264 265 try client_repo.fetch( 266 io, 267 allocator, 268 "origin", 269 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 270 .command = ssh_cmd_maybe, 271 } } }, 272 ); 273 274 // update the working dir 275 try client_repo.restore(io, allocator, "."); 276 277 // make sure fetch was successful 278 { 279 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 280 defer goodbye_txt.close(io); 281 282 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 283 try std.testing.expectEqualStrings(&commit2, &oid_master); 284 } 285 } 286 287 fn testPush( 288 comptime repo_kind: rp.RepoKind, 289 comptime transport_def: net.TransportDefinition, 290 comptime hash_kind: hash.HashKind, 291 comptime port: u16, 292 io: std.Io, 293 allocator: std.mem.Allocator, 294 ) !void { 295 // create the temp dir 296 var temp = std.testing.tmpDir(.{}); 297 defer temp.cleanup(); 298 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 299 defer allocator.free(temp_path); 300 301 // init server 302 var server_process = try runServer(io, allocator, temp_path, port); 303 defer _ = server_process.kill(io); 304 305 // register the repo under admin and locate its on-disk directory 306 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 307 defer allocator.free(server_path); 308 309 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 310 defer server_repo.deinit(io, allocator); 311 312 // add config 313 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 314 315 // export server repo 316 { 317 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 318 defer export_file.close(io); 319 } 320 321 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 322 defer allocator.free(client_path); 323 324 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 325 defer client_repo.deinit(io, allocator); 326 327 // make a commit 328 const commit1 = blk: { 329 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 330 defer hello_txt.close(io); 331 try hello_txt.writeStreamingAll(io, "hello, world!"); 332 try client_repo.add(io, allocator, &.{"hello.txt"}); 333 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 334 }; 335 336 // add a tag 337 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 338 339 // add remote 340 { 341 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 342 defer allocator.free(remote_url); 343 344 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 345 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 346 } 347 348 const refspecs = &.{ 349 "refs/tags/1.0.0:refs/tags/1.0.0", 350 }; 351 352 const is_ssh = switch (transport_def) { 353 .file => false, 354 .wire => |wire_kind| .ssh == wire_kind, 355 }; 356 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 357 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 358 359 try client_repo.push( 360 io, 361 allocator, 362 "origin", 363 "master", 364 false, 365 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 366 .command = ssh_cmd_maybe, 367 } } }, 368 ); 369 370 // make sure push was successful 371 { 372 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 373 374 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 375 try std.testing.expectEqualStrings(&commit1, &oid_master); 376 } 377 378 // make a commit on the server 379 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world from the server!" }} }, .{ .message = "new commit from the server" }); 380 381 // make another commit 382 const commit2 = blk: { 383 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 384 defer goodbye_txt.close(io); 385 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 386 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 387 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 388 }; 389 390 // can't push because server has commit not found locally 391 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 392 io, 393 allocator, 394 "origin", 395 "master", 396 false, 397 .{ .wire = .{ .ssh = .{ 398 .command = ssh_cmd_maybe, 399 } } }, 400 )); 401 402 // make a commit on the server with no parents, thus creating an incompatible git history 403 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world from the server again!" }} }, .{ .message = "new git history on the server", .parent_oids = &.{} }); 404 405 // can't push because commit doesn't exist locally 406 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 407 io, 408 allocator, 409 "origin", 410 "master", 411 false, 412 .{ .wire = .{ .ssh = .{ 413 .command = ssh_cmd_maybe, 414 } } }, 415 )); 416 417 // retrieve the commit object 418 try client_repo.fetch( 419 io, 420 allocator, 421 "origin", 422 .{ .wire = .{ .ssh = .{ 423 .command = ssh_cmd_maybe, 424 } } }, 425 ); 426 427 // can't push because server's history is incompatible 428 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 429 io, 430 allocator, 431 "origin", 432 "master", 433 false, 434 .{ .wire = .{ .ssh = .{ 435 .command = ssh_cmd_maybe, 436 } } }, 437 )); 438 439 // test denyNonFastForwards 440 { 441 // set denyNonFastForwards on server 442 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 443 444 // save the server's current master ref 445 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 446 447 // force push should be rejected by server due to denyNonFastForwards 448 try std.testing.expectError(error.RemoteRejectedRef, client_repo.push( 449 io, 450 allocator, 451 "origin", 452 "master", 453 true, 454 .{ .wire = .{ .ssh = .{ 455 .command = ssh_cmd_maybe, 456 } } }, 457 )); 458 459 // verify the server ref was not updated (push was denied) 460 { 461 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 462 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 463 } 464 465 // remove denyNonFastForwards from server 466 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 467 } 468 469 // force push 470 try client_repo.push( 471 io, 472 allocator, 473 "origin", 474 "master", 475 true, 476 .{ .wire = .{ .ssh = .{ 477 .command = ssh_cmd_maybe, 478 } } }, 479 ); 480 481 // make sure push was successful 482 { 483 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 484 try std.testing.expectEqualStrings(&commit2, &oid_master); 485 } 486 487 // remove the remote tag 488 try client_repo.push( 489 io, 490 allocator, 491 "origin", 492 ":refs/tags/1.0.0", 493 false, 494 .{ .wire = .{ .ssh = .{ 495 .command = ssh_cmd_maybe, 496 } } }, 497 ); 498 499 // make sure push was successful 500 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 501 } 502 503 fn testPushFork( 504 comptime hash_kind: hash.HashKind, 505 comptime port: u16, 506 io: std.Io, 507 allocator: std.mem.Allocator, 508 ) !void { 509 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind }; 510 511 var temp = std.testing.tmpDir(.{}); 512 defer temp.cleanup(); 513 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 514 defer allocator.free(temp_path); 515 516 var server_process = try runServer(io, allocator, temp_path, port); 517 defer _ = server_process.kill(io); 518 519 const target_path = (try repoOnDiskPath(io, allocator, temp_path, "target", true)).?; 520 defer allocator.free(target_path); 521 { 522 var target_repo = try rp.Repo(.xit, repo_opts).init(io, allocator, .{ .bare = true, .path = target_path }); 523 defer target_repo.deinit(io, allocator); 524 _ = try commitServer(&target_repo, io, allocator, .{ .files = &.{.{ .path = "main.txt", .content = "base contents\n" }} }, .{ .author = "admin <admin@example.test>", .message = "initial code" }); 525 } 526 527 const other_path = (try repoOnDiskPath(io, allocator, temp_path, "other", true)).?; 528 defer allocator.free(other_path); 529 530 const admin_path = try std.fs.path.join(allocator, &.{ temp_path, "admin" }); 531 defer allocator.free(admin_path); 532 const repos_dir = try std.fs.path.join(allocator, &.{ temp_path, "repos" }); 533 defer allocator.free(repos_dir); 534 535 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 536 const fork_id = evt.EventWithId.randomId(prng.random()); 537 const fork_id_hex = std.fmt.bytesToHex(fork_id, .lower); 538 var user_id: [evt.event_id_size]u8 = undefined; 539 var repo_id: [evt.event_id_size]u8 = undefined; 540 const draft_path = blk: { 541 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 542 defer admin.deinit(io, allocator); 543 var arena = std.heap.ArenaAllocator.init(allocator); 544 defer arena.deinit(); 545 const moment = try evt.currentMoment(evt.admin_repo_opts, &admin); 546 const target = (try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, "admin", "target")) orelse return error.NotFound; 547 repo_id = target.event_id; 548 @memcpy(&user_id, target.repo.event.user_id); 549 break :blk try fork.create(repo_opts, io, allocator, repos_dir, &admin, .{ 550 .id = fork_id_hex, 551 .user_id = user_id, 552 .repo_id = repo_id, 553 .title = "add a feature", 554 .description = "a draft patch", 555 .tags = "enhancement", 556 .target_branch = "master", 557 .author = .{ .name = "admin", .email = "admin@example.test" }, 558 .timestamp = 1, 559 }); 560 }; 561 defer allocator.free(draft_path); 562 563 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 564 defer allocator.free(client_path); 565 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 566 defer allocator.free(ssh_cmd); 567 var client = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 568 io, 569 allocator, 570 "git@localhost:admin/target", 571 client_path, 572 client_path, 573 null, 574 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 575 ); 576 defer client.deinit(io, allocator); 577 578 const source_oid = blk: { 579 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 580 defer file.close(io); 581 try file.writeStreamingAll(io, "fork contents\n"); 582 try client.add(io, allocator, &.{"feature.txt"}); 583 break :blk try client.commit(io, allocator, .{ .message = "add feature" }); 584 }; 585 586 const remote_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 587 defer allocator.free(remote_url); 588 try client.addRemote(io, allocator, .{ .name = "patch", .value = remote_url }); 589 try client.push(io, allocator, "patch", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 590 591 var first_revision_id: [evt.event_id_size]u8 = undefined; 592 { 593 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 594 defer draft.deinit(io, allocator); 595 try std.testing.expect(try draft.isBare(io, allocator)); 596 var arena = std.heap.ArenaAllocator.init(allocator); 597 defer arena.deinit(); 598 const moment = try evt.currentMoment(repo_opts, &draft); 599 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 600 try std.testing.expect(!patch.removed); 601 try std.testing.expectEqualStrings(&source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 602 const selected = patch.event.revision orelse return error.NotFound; 603 try std.testing.expectEqualStrings("master", patch.event.target_branch); 604 try std.testing.expectEqualStrings(&source_oid, selected.source_oid); 605 try std.testing.expectEqual(@as(?u64, 1), selected.commit_count); 606 first_revision_id = try evt.parseEventId(&selected.id); 607 const revision = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &first_revision_id)) orelse return error.NotFound; 608 try std.testing.expectEqualStrings(&source_oid, revision.event.source_oid); 609 } 610 611 // 612 // publish, then push another revision 613 // 614 615 { 616 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 617 defer admin.deinit(io, allocator); 618 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 619 defer target.deinit(io, allocator); 620 try target.addBranch(io, .{ .name = "feature" }); 621 try pch.publish(repo_opts, io, allocator, &admin, &target, draft_path, .{ 622 .id = fork_id_hex, 623 .user_id = user_id, 624 .repo_id = repo_id, 625 .author = .{ .name = "admin", .email = "admin@example.test" }, 626 .timestamp = 2, 627 }); 628 var arena = std.heap.ArenaAllocator.init(allocator); 629 defer arena.deinit(); 630 const moment = try evt.currentMoment(repo_opts, &target); 631 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 632 try evt.Patch.update(.server, .xit, repo_opts, io, allocator, &target, &fork_id, .{ .fields = .{ 633 .title = "edited feature", 634 .description = patch.event.description, 635 .tags = patch.event.tags, 636 .target_branch = "feature", 637 } }, .{ .name = "admin", .email = "admin@example.test" }); 638 } 639 640 const feature_remote_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 641 defer allocator.free(feature_remote_url); 642 try client.addRemote(io, allocator, .{ .name = "patch-to-feature", .value = feature_remote_url }); 643 644 const second_source_oid = blk: { 645 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 646 defer file.close(io); 647 try file.writeStreamingAll(io, "updated fork contents\n"); 648 try client.add(io, allocator, &.{"feature.txt"}); 649 break :blk try client.commit(io, allocator, .{ .message = "revise feature" }); 650 }; 651 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 652 653 var second_revision_id: [evt.event_id_size]u8 = undefined; 654 var second_patch_oid: [hash.hexLen(hash_kind)]u8 = undefined; 655 { 656 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 657 defer draft.deinit(io, allocator); 658 var arena = std.heap.ArenaAllocator.init(allocator); 659 defer arena.deinit(); 660 const moment = try evt.currentMoment(repo_opts, &draft); 661 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 662 try std.testing.expect(patch.removed); 663 const newest = (try evt.PatchRev.readNewest(evt.EventDB(hash_kind), hash_kind, moment, &arena)) orelse return error.NotFound; 664 second_revision_id = newest.id; 665 @memcpy(&second_patch_oid, newest.record.patch_oid); 666 try std.testing.expect(!std.mem.eql(u8, &first_revision_id, &second_revision_id)); 667 try std.testing.expectEqualStrings(&second_source_oid, newest.record.event.source_oid); 668 try std.testing.expectEqualStrings("edited feature", newest.record.event.message); 669 } 670 { 671 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 672 defer target.deinit(io, allocator); 673 var arena = std.heap.ArenaAllocator.init(allocator); 674 defer arena.deinit(); 675 const moment = try evt.currentMoment(repo_opts, &target); 676 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 677 try std.testing.expectEqualStrings("edited feature", patch.event.title); 678 const selected = patch.event.revision orelse return error.NotFound; 679 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(second_revision_id, .lower), &selected.id); 680 try std.testing.expectEqualStrings(&second_source_oid, selected.source_oid); 681 try std.testing.expectEqual(@as(?u64, 2), selected.commit_count); 682 try std.testing.expectEqualStrings("feature", patch.event.target_branch); 683 } 684 685 // 686 // reject extra refs without applying the patch update 687 // 688 689 const third_source_oid = blk: { 690 const file = try client.core.work_dir.createFile(io, "feature.txt", .{ .truncate = true }); 691 defer file.close(io); 692 try file.writeStreamingAll(io, "rejected fork contents\n"); 693 try client.add(io, allocator, &.{"feature.txt"}); 694 break :blk try client.commit(io, allocator, .{ .message = "rejected revision" }); 695 }; 696 const extra_ref_rejected = if (client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ 697 .refspecs = &.{"refs/heads/master:refs/heads/extra"}, 698 .wire = .{ .ssh = .{ .command = ssh_cmd } }, 699 })) |_| false else |_| true; 700 try std.testing.expect(extra_ref_rejected); 701 { 702 var draft = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = draft_path }); 703 defer draft.deinit(io, allocator); 704 try std.testing.expectEqualStrings(&second_source_oid, &(try draft.readRef(io, fork.ref) orelse return error.NotFound)); 705 try std.testing.expectEqual(null, try draft.readRef(io, .{ .kind = .head, .name = "extra" })); 706 var arena = std.heap.ArenaAllocator.init(allocator); 707 defer arena.deinit(); 708 const moment = try evt.currentMoment(repo_opts, &draft); 709 const newest = (try evt.PatchRev.readNewest(evt.EventDB(hash_kind), hash_kind, moment, &arena)) orelse return error.NotFound; 710 try std.testing.expectEqualSlices(u8, &second_revision_id, &newest.id); 711 } 712 713 // 714 // a removed target patch stays removed after another push 715 // 716 717 { 718 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 719 defer target.deinit(io, allocator); 720 try evt.remove(.server, .repo, .xit, repo_opts, io, allocator, &target, &fork_id, .patch, .{ .name = "admin", .email = "admin@example.test" }); 721 } 722 try client.push(io, allocator, "patch-to-feature", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 723 { 724 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 725 defer target.deinit(io, allocator); 726 var arena = std.heap.ArenaAllocator.init(allocator); 727 defer arena.deinit(); 728 const moment = try evt.currentMoment(repo_opts, &target); 729 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 730 try std.testing.expect(patch.removed); 731 } 732 733 // 734 // restore the patch, merge its source oid, and push the merge 735 // 736 737 { 738 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 739 defer target.deinit(io, allocator); 740 var arena = std.heap.ArenaAllocator.init(allocator); 741 defer arena.deinit(); 742 const moment = try evt.currentMoment(repo_opts, &target); 743 const removed = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 744 try evt.consume(.server, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 745 .id = fork_id_hex, 746 .timestamp = 4, 747 .author = .{ .name = "admin", .email = "admin@example.test" }, 748 .event = .{ .patch = removed.event }, 749 }}); 750 } 751 const merge_parents = [_][hash.hexLen(hash_kind)]u8{ third_source_oid, second_source_oid }; 752 _ = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 753 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 754 { 755 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 756 defer target.deinit(io, allocator); 757 var arena = std.heap.ArenaAllocator.init(allocator); 758 defer arena.deinit(); 759 const moment = try evt.currentMoment(repo_opts, &target); 760 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 761 try std.testing.expectEqual(.merged, patch.event.status.kind()); 762 const merged_oid = switch (patch.event.status) { 763 .merged => |oid| oid, 764 else => return error.InvalidPatch, 765 }; 766 try std.testing.expectEqualStrings(&second_source_oid, merged_oid); 767 const imported = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &second_revision_id)) orelse return error.NotFound; 768 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 769 } 770 771 const other_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/other/patch:{s}", .{&fork_id_hex}); 772 defer allocator.free(other_url); 773 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 774 // early rejections need no input. give ssh eof so windows openssh 775 // doesn't hang closing stdin before forwarding the error. 776 const reject_ssh_cmd = try std.fmt.allocPrint(allocator, "{s} -n", .{ssh_cmd}); 777 defer allocator.free(reject_ssh_cmd); 778 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 779 780 // 781 // the fork remains cloneable after the target repo is removed 782 // 783 784 { 785 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 786 defer admin.deinit(io, allocator); 787 try evt.remove(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 788 } 789 const clone_path = try std.fs.path.join(allocator, &.{ temp_path, "fork-clone" }); 790 defer allocator.free(clone_path); 791 var fork_clone = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 792 io, 793 allocator, 794 remote_url, 795 clone_path, 796 clone_path, 797 null, 798 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 799 ); 800 defer fork_clone.deinit(io, allocator); 801 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 802 803 { 804 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 805 defer admin.deinit(io, allocator); 806 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 807 .{ 808 .id = std.fmt.bytesToHex(repo_id, .lower), 809 .timestamp = 2, 810 .author = .{ .name = "admin", .email = "admin@example.test" }, 811 .event = .{ .repo = .{ 812 .user_id = &user_id, 813 .name = "target", 814 .description = "", 815 } }, 816 }, 817 .{ 818 .id = std.fmt.bytesToHex(user_id, .lower), 819 .timestamp = 2, 820 .author = .{ .name = "admin", .email = "admin@example.test" }, 821 .event = .{ .user = .{ 822 .name = "admin", 823 .email = "admin@example.test", 824 .password_hash = "", 825 .ssh_keys = "", 826 } }, 827 }, 828 }); 829 } 830 831 const denied_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 832 defer allocator.free(denied_url); 833 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 834 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 835 } 836 837 fn testPushCreatesMissingRepo( 838 comptime repo_kind: rp.RepoKind, 839 comptime transport_def: net.TransportDefinition, 840 comptime port: u16, 841 io: std.Io, 842 allocator: std.mem.Allocator, 843 ) !void { 844 var temp = std.testing.tmpDir(.{}); 845 defer temp.cleanup(); 846 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 847 defer allocator.free(temp_path); 848 849 var server_process = try runServer(io, allocator, temp_path, port); 850 defer _ = server_process.kill(io); 851 852 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 853 defer allocator.free(client_path); 854 855 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 856 defer client_repo.deinit(io, allocator); 857 858 const commit1 = blk: { 859 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 860 defer hello_txt.close(io); 861 try hello_txt.writeStreamingAll(io, "hello, world!"); 862 try client_repo.add(io, allocator, &.{"hello.txt"}); 863 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 864 }; 865 866 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 867 868 // the repo is not pre-registered; the push should mint the repo event under 869 // admin and create the repo on disk 870 { 871 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 872 defer allocator.free(remote_url); 873 874 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 875 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 876 } 877 878 const is_ssh = switch (transport_def) { 879 .file => false, 880 .wire => |wire_kind| .ssh == wire_kind, 881 }; 882 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 883 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 884 885 try client_repo.push( 886 io, 887 allocator, 888 "origin", 889 "master", 890 false, 891 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 892 .command = ssh_cmd_maybe, 893 } } }, 894 ); 895 896 // the push registered admin/server; resolve its on-disk directory 897 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", false)).?; 898 defer allocator.free(server_path); 899 900 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 901 defer server_repo.deinit(io, allocator); 902 try std.testing.expect(try server_repo.isBare(io, allocator)); 903 904 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 905 906 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 907 try std.testing.expectEqualStrings(&commit1, &oid_master); 908 } 909 910 fn testPushEvents( 911 comptime repo_kind: rp.RepoKind, 912 comptime transport_def: net.TransportDefinition, 913 comptime hash_kind: hash.HashKind, 914 comptime port: u16, 915 io: std.Io, 916 allocator: std.mem.Allocator, 917 ) !void { 918 // create the temp dir 919 var temp = std.testing.tmpDir(.{}); 920 defer temp.cleanup(); 921 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 922 defer allocator.free(temp_path); 923 924 // init server 925 var server_process = try runServer(io, allocator, temp_path, port); 926 defer _ = server_process.kill(io); 927 928 // register the repo under admin and locate its on-disk directory 929 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 930 defer allocator.free(server_path); 931 932 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 933 const ServerRepo = rp.Repo(.xit, repo_opts); 934 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 935 defer server_repo.deinit(io, allocator); 936 937 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 938 defer allocator.free(client_path); 939 940 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 941 defer client_repo.deinit(io, allocator); 942 943 // add remote 944 { 945 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 946 defer allocator.free(remote_url); 947 948 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 949 } 950 951 const is_ssh = switch (transport_def) { 952 .file => false, 953 .wire => |wire_kind| .ssh == wire_kind, 954 }; 955 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 956 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 957 958 var arena = std.heap.ArenaAllocator.init(allocator); 959 defer arena.deinit(); 960 961 // 962 // define test events 963 // 964 965 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 966 const issue_event_id = evt.EventWithId.randomId(prng.random()); 967 968 const events_to_push = [_]evt.EventWithId{ 969 .{ 970 .id = std.fmt.bytesToHex(issue_event_id, .lower), 971 .author = .{ .name = "haxy", .email = "user@haxy" }, 972 .event = .{ 973 .issue = .{ 974 .title = "Login form clears password on validation error", 975 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 976 .tags = "bug priority-high ui", 977 }, 978 }, 979 }, 980 }; 981 982 // commit the issue on the client and push it to the server 983 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 984 try client_repo.push( 985 io, 986 allocator, 987 "origin", 988 "haxy/events", 989 false, 990 .{ .wire = .{ .ssh = .{ 991 .command = ssh_cmd_maybe, 992 } } }, 993 ); 994 995 // the push consumed the issue into the server repo's db 996 { 997 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 998 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 999 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1000 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1001 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1002 1003 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1004 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.event.tags); 1005 } 1006 1007 // 1008 // edit the issue 1009 // 1010 1011 // this event edits the previous one because it has the same id 1012 const events_to_push2 = [_]evt.EventWithId{ 1013 .{ 1014 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1015 .author = .{ .name = "haxy", .email = "user@haxy" }, 1016 .event = .{ 1017 .issue = .{ 1018 .title = "Login form clears password on validation error", 1019 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1020 .tags = "bug priority-low ui", 1021 }, 1022 }, 1023 }, 1024 }; 1025 1026 // commit the edit on the client and push it to the server 1027 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1028 try client_repo.push( 1029 io, 1030 allocator, 1031 "origin", 1032 "haxy/events", 1033 false, 1034 .{ .wire = .{ .ssh = .{ 1035 .command = ssh_cmd_maybe, 1036 } } }, 1037 ); 1038 1039 // the push consumed the edit into the server repo's db 1040 { 1041 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1042 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1043 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1044 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1045 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1046 1047 // the description and tags were correctly edited 1048 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1049 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.event.tags); 1050 } 1051 } 1052 1053 fn testClone( 1054 comptime repo_kind: rp.RepoKind, 1055 comptime transport_def: net.TransportDefinition, 1056 comptime shell_out_to_git: bool, 1057 comptime hash_kind: hash.HashKind, 1058 comptime port: u16, 1059 io: std.Io, 1060 allocator: std.mem.Allocator, 1061 ) !void { 1062 // create the temp dir 1063 var temp = std.testing.tmpDir(.{}); 1064 defer temp.cleanup(); 1065 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1066 defer allocator.free(temp_path); 1067 1068 // init server 1069 var server_process = try runServer(io, allocator, temp_path, port); 1070 defer _ = server_process.kill(io); 1071 1072 // register the repo under admin and locate its on-disk directory 1073 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1074 defer allocator.free(server_path); 1075 1076 // init server repo with default branch name as main 1077 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1078 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = !shell_out_to_git }).init(io, allocator, .{ .bare = true, .path = server_path, .create_default_branch = "main" }); 1079 defer server_repo.deinit(io, allocator); 1080 1081 if (shell_out_to_git) { 1082 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1083 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1084 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1085 } 1086 1087 // make a commit 1088 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1089 1090 // tag first commit 1091 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1092 1093 // make a commit 1094 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1095 1096 // export server repo 1097 { 1098 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1099 defer export_file.close(io); 1100 } 1101 1102 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1103 defer allocator.free(client_path); 1104 1105 // get remote url 1106 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1107 defer allocator.free(remote_url); 1108 1109 const is_ssh = switch (transport_def) { 1110 .file => false, 1111 .wire => |wire_kind| .ssh == wire_kind, 1112 }; 1113 1114 if (shell_out_to_git) { 1115 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1116 defer allocator.free(ssh_cmd); 1117 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1118 defer allocator.free(ssh_config_arg); 1119 1120 { 1121 var process = try std.process.spawn(io, .{ 1122 .argv = if (is_ssh) 1123 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--depth", "1", remote_url, "client" } 1124 else 1125 &.{ "git", "clone", "--quiet", "--depth", "1", remote_url, "client" }, 1126 .cwd = .{ .path = temp_path }, 1127 .stdin = .ignore, 1128 .stdout = .ignore, 1129 .stderr = .inherit, 1130 }); 1131 defer process.kill(io); 1132 const term = try process.wait(io); 1133 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1134 } 1135 1136 // make sure shallow clone was successful 1137 { 1138 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1139 hello_txt.close(io); 1140 } 1141 1142 // make a third commit on the server 1143 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1144 1145 // pull --unshallow to deepen the clone and get the new commit 1146 { 1147 var process = try std.process.spawn(io, .{ 1148 .argv = if (is_ssh) 1149 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "--unshallow" } 1150 else 1151 &.{ "git", "pull", "--quiet", "--unshallow" }, 1152 .cwd = .{ .path = client_path }, 1153 .stdin = .ignore, 1154 .stdout = .ignore, 1155 .stderr = .inherit, 1156 }); 1157 defer process.kill(io); 1158 const term = try process.wait(io); 1159 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1160 } 1161 1162 // make sure unshallow pull was successful 1163 { 1164 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1165 extra_txt.close(io); 1166 } 1167 1168 // delete client and clone again with --shallow-since 1169 try temp.dir.deleteTree(io, "client"); 1170 1171 { 1172 var process = try std.process.spawn(io, .{ 1173 .argv = if (is_ssh) 1174 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" } 1175 else 1176 &.{ "git", "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" }, 1177 .cwd = .{ .path = temp_path }, 1178 .stdin = .ignore, 1179 .stdout = .ignore, 1180 .stderr = .inherit, 1181 }); 1182 defer process.kill(io); 1183 const term = try process.wait(io); 1184 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1185 } 1186 1187 // make sure shallow clone was successful 1188 { 1189 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1190 hello_txt.close(io); 1191 } 1192 1193 // delete client and clone again with --shallow-exclude 1194 try temp.dir.deleteTree(io, "client"); 1195 1196 { 1197 var process = try std.process.spawn(io, .{ 1198 .argv = if (is_ssh) 1199 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1200 else 1201 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1202 .cwd = .{ .path = temp_path }, 1203 .stdin = .ignore, 1204 .stdout = .ignore, 1205 .stderr = .inherit, 1206 }); 1207 defer process.kill(io); 1208 const term = try process.wait(io); 1209 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1210 } 1211 1212 // make sure shallow clone was successful 1213 { 1214 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1215 hello_txt.close(io); 1216 } 1217 1218 // delete client and clone again with --filter=blob:none 1219 try temp.dir.deleteTree(io, "client"); 1220 1221 { 1222 var process = try std.process.spawn(io, .{ 1223 .argv = if (is_ssh) 1224 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1225 else 1226 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1227 .cwd = .{ .path = temp_path }, 1228 .stdin = .ignore, 1229 .stdout = .ignore, 1230 .stderr = .inherit, 1231 }); 1232 defer process.kill(io); 1233 const term = try process.wait(io); 1234 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1235 } 1236 1237 // make sure partial clone was successful 1238 { 1239 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1240 hello_txt.close(io); 1241 } 1242 1243 // delete client and clone again with --filter=tree:0 1244 try temp.dir.deleteTree(io, "client"); 1245 1246 { 1247 var process = try std.process.spawn(io, .{ 1248 .argv = if (is_ssh) 1249 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1250 else 1251 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1252 .cwd = .{ .path = temp_path }, 1253 .stdin = .ignore, 1254 .stdout = .ignore, 1255 .stderr = .inherit, 1256 }); 1257 defer process.kill(io); 1258 const term = try process.wait(io); 1259 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1260 } 1261 1262 // make sure treeless clone was successful 1263 { 1264 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1265 goodbye_txt.close(io); 1266 } 1267 } else { 1268 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1269 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1270 1271 // clone repo 1272 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1273 io, 1274 allocator, 1275 remote_url, 1276 temp_path, 1277 client_path, 1278 null, 1279 .{ .transport = .{ .wire = .{ .ssh = .{ 1280 .command = ssh_cmd_maybe, 1281 } } } }, 1282 ); 1283 defer clone_result.deinit(io, allocator); 1284 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1285 1286 // make sure HEAD points to the right default branch 1287 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1288 const head = try client_repo.head(io, &current_branch_buffer); 1289 try std.testing.expectEqualStrings("main", head.ref.name); 1290 1291 // make sure clone was successful 1292 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1293 defer hello_txt.close(io); 1294 } 1295 } 1296 1297 fn testFetchLarge( 1298 comptime repo_kind: rp.RepoKind, 1299 comptime transport_def: net.TransportDefinition, 1300 comptime shell_out_to_git: bool, 1301 comptime hash_kind: hash.HashKind, 1302 comptime port: u16, 1303 io: std.Io, 1304 allocator: std.mem.Allocator, 1305 ) !void { 1306 // create the temp dir 1307 var temp = std.testing.tmpDir(.{}); 1308 defer temp.cleanup(); 1309 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1310 defer allocator.free(temp_path); 1311 1312 // init server 1313 var server_process = try runServer(io, allocator, temp_path, port); 1314 defer _ = server_process.kill(io); 1315 1316 // register the repo under admin and locate its on-disk directory 1317 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1318 defer allocator.free(server_path); 1319 1320 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1321 defer server_repo.deinit(io, allocator); 1322 1323 // make a commit with files from the current repo 1324 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1325 1326 // export server repo 1327 { 1328 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1329 defer export_file.close(io); 1330 } 1331 1332 if (shell_out_to_git) { 1333 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1334 } 1335 1336 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1337 defer allocator.free(client_path); 1338 1339 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1340 defer client_repo.deinit(io, allocator); 1341 1342 // add remote 1343 { 1344 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1345 defer allocator.free(remote_url); 1346 1347 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1348 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1349 } 1350 1351 const is_ssh = switch (transport_def) { 1352 .file => false, 1353 .wire => |wire_kind| .ssh == wire_kind, 1354 }; 1355 1356 if (shell_out_to_git) { 1357 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1358 defer allocator.free(ssh_cmd); 1359 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1360 defer allocator.free(ssh_config_arg); 1361 1362 { 1363 var process = try std.process.spawn(io, .{ 1364 .argv = if (is_ssh) 1365 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1366 else 1367 &.{ "git", "pull", "--quiet", "origin", "master" }, 1368 .cwd = .{ .path = client_path }, 1369 .stdin = .ignore, 1370 .stdout = .ignore, 1371 .stderr = .inherit, 1372 }); 1373 defer process.kill(io); 1374 const term = try process.wait(io); 1375 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1376 } 1377 1378 // make sure pull was successful 1379 { 1380 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1381 try std.testing.expectEqualStrings(&commit1, &oid_master); 1382 } 1383 1384 // make another commit on the server 1385 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1386 1387 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1388 { 1389 var process = try std.process.spawn(io, .{ 1390 .argv = if (is_ssh) 1391 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1392 else 1393 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1394 .cwd = .{ .path = client_path }, 1395 .stdin = .ignore, 1396 .stdout = .ignore, 1397 .stderr = .inherit, 1398 }); 1399 defer process.kill(io); 1400 const term = try process.wait(io); 1401 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1402 } 1403 1404 // make sure fetch with want-ref was successful 1405 { 1406 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1407 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1408 } 1409 } else { 1410 const refspecs = &.{ 1411 "+refs/heads/master:refs/heads/master", 1412 }; 1413 1414 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1415 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1416 1417 try client_repo.fetch( 1418 io, 1419 allocator, 1420 "origin", 1421 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1422 .command = ssh_cmd_maybe, 1423 } } }, 1424 ); 1425 1426 // update the working dir 1427 try client_repo.restore(io, allocator, "."); 1428 } 1429 1430 // make sure fetch was successful 1431 { 1432 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1433 try std.testing.expectEqualStrings(&commit1, &oid_master); 1434 } 1435 } 1436 1437 fn testPushLarge( 1438 comptime repo_kind: rp.RepoKind, 1439 comptime transport_def: net.TransportDefinition, 1440 comptime shell_out_to_git: bool, 1441 comptime hash_kind: hash.HashKind, 1442 comptime port: u16, 1443 io: std.Io, 1444 allocator: std.mem.Allocator, 1445 ) !void { 1446 // create the temp dir 1447 var temp = std.testing.tmpDir(.{}); 1448 defer temp.cleanup(); 1449 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1450 defer allocator.free(temp_path); 1451 1452 // init server 1453 var server_process = try runServer(io, allocator, temp_path, port); 1454 defer _ = server_process.kill(io); 1455 1456 // register the repo under admin and locate its on-disk directory 1457 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1458 defer allocator.free(server_path); 1459 1460 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1461 defer server_repo.deinit(io, allocator); 1462 1463 // add config 1464 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1465 1466 // export server repo 1467 { 1468 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1469 defer export_file.close(io); 1470 } 1471 1472 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1473 defer allocator.free(client_path); 1474 1475 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1476 defer client_repo.deinit(io, allocator); 1477 1478 var client_dir = try temp.dir.openDir(io, "client", .{}); 1479 defer client_dir.close(io); 1480 1481 { 1482 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1483 defer hello_txt.close(io); 1484 try hello_txt.writeStreamingAll(io, "hello, world!"); 1485 try client_repo.add(io, allocator, &.{"hello.txt"}); 1486 } 1487 1488 // copy files from current repo into client dir 1489 const cwd = std.Io.Dir.cwd(); 1490 for (&[_][]const u8{"src"}) |dir_name| { 1491 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1492 defer src_repo_dir.close(io); 1493 1494 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1495 defer dest_repo_dir.close(io); 1496 1497 try copyDir(io, src_repo_dir, dest_repo_dir); 1498 1499 try client_repo.add(io, allocator, &.{dir_name}); 1500 } 1501 1502 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1503 1504 // change the files so git will send them as delta objects 1505 for (&[_][]const u8{"src"}) |dir_name| { 1506 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1507 defer dest_repo_dir.close(io); 1508 1509 { 1510 var iter = dest_repo_dir.iterate(); 1511 while (try iter.next(io)) |entry| { 1512 switch (entry.kind) { 1513 .file => { 1514 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1515 defer file.close(io); 1516 var writer = file.writer(io, &.{}); 1517 try writer.interface.writeAll("EDIT"); 1518 }, 1519 else => {}, 1520 } 1521 } 1522 } 1523 1524 try client_repo.add(io, allocator, &.{dir_name}); 1525 } 1526 1527 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1528 1529 // add remote 1530 { 1531 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1532 defer allocator.free(remote_url); 1533 1534 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1535 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1536 } 1537 1538 const is_ssh = switch (transport_def) { 1539 .file => false, 1540 .wire => |wire_kind| .ssh == wire_kind, 1541 }; 1542 1543 if (shell_out_to_git) { 1544 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1545 defer allocator.free(ssh_cmd); 1546 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1547 defer allocator.free(ssh_config_arg); 1548 1549 // shell out to git so it will send delta objects 1550 var process = try std.process.spawn(io, .{ 1551 .argv = if (is_ssh) 1552 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1553 else 1554 &.{ "git", "push", "--quiet", "origin", "master" }, 1555 .cwd = .{ .path = client_path }, 1556 .stdin = .ignore, 1557 .stdout = .ignore, 1558 .stderr = .inherit, 1559 }); 1560 defer process.kill(io); 1561 const term = try process.wait(io); 1562 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1563 } else { 1564 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1565 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1566 1567 try client_repo.push( 1568 io, 1569 allocator, 1570 "origin", 1571 "master", 1572 false, 1573 .{ .wire = .{ .ssh = .{ 1574 .command = ssh_cmd_maybe, 1575 } } }, 1576 ); 1577 } 1578 1579 // make sure push was successful 1580 { 1581 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1582 try std.testing.expectEqualStrings(&commit2, &oid_master); 1583 1584 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1585 } 1586 } 1587 1588 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1589 var iter = src_dir.iterate(); 1590 while (try iter.next(io)) |entry| { 1591 switch (entry.kind) { 1592 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1593 .directory => { 1594 try dest_dir.createDirPath(io, entry.name); 1595 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1596 defer dest_entry_dir.close(io); 1597 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1598 defer src_entry_dir.close(io); 1599 try copyDir(io, src_entry_dir, dest_entry_dir); 1600 }, 1601 else => {}, 1602 } 1603 } 1604 } 1605 1606 // the dev SSH public key matching the private key written by runServer 1607 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1608 1609 // create the admin event repo with a user holding the dev SSH public key 1610 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1611 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1612 defer allocator.free(admin_repo_path); 1613 1614 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1615 defer repo.deinit(io, allocator); 1616 1617 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1618 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1619 1620 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1621 const user_id = evt.EventWithId.randomId(prng.random()); 1622 1623 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1624 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1625 1626 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1627 .id = std.fmt.bytesToHex(user_id, .lower), 1628 .author = .{ .name = "admin", .email = "admin@example.test" }, 1629 .event = .{ .user = .{ 1630 .name = "admin", 1631 .email = "admin@example.test", 1632 .password_hash = password_hash, 1633 .ssh_keys = admin_ssh_key, 1634 } }, 1635 }}); 1636 } 1637 1638 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1639 // the event store 1640 fn repoOnDiskPath( 1641 io: std.Io, 1642 allocator: std.mem.Allocator, 1643 data_path: []const u8, 1644 repo_name: []const u8, 1645 create: bool, 1646 ) !?[]u8 { 1647 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1648 defer allocator.free(admin_repo_path); 1649 1650 const event_id_hex = (try evt.resolveOrCreateRepo( 1651 io, 1652 allocator, 1653 admin_repo_path, 1654 "admin", 1655 repo_name, 1656 if (create) .{ .read_access = .public } else null, 1657 )) orelse return null; 1658 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1659 } 1660 1661 // build the remote URL addressing admin/<repo_name> over the given transport 1662 fn remoteUrl( 1663 comptime transport_def: net.TransportDefinition, 1664 allocator: std.mem.Allocator, 1665 repo_name: []const u8, 1666 port: u16, 1667 ) ![]u8 { 1668 return switch (transport_def) { 1669 .file => unreachable, 1670 .wire => |wire_kind| switch (wire_kind) { 1671 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1672 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1673 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1674 }, 1675 }; 1676 } 1677 1678 // the ssh command the haxy git client should invoke, or null when not using 1679 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1680 // and skip writing to known_hosts. 1681 fn sshCommand( 1682 is_ssh: bool, 1683 allocator: std.mem.Allocator, 1684 temp_path: []const u8, 1685 port: u16, 1686 ) !?[]u8 { 1687 if (!is_ssh) return null; 1688 1689 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1690 defer allocator.free(priv_key_path); 1691 1692 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1693 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1694 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile={s} -o LogLevel=ERROR -o IdentitiesOnly=yes -i \"{s}\"", .{ port + 1, null_path, priv_key_path }); 1695 } 1696 1697 const ServerFiles = union(enum) { 1698 files: []const struct { path: []const u8, content: []const u8 }, 1699 dirs: []const []const u8, 1700 }; 1701 1702 /// commit files without writing the server's index or worktree. 1703 fn commitServer(repo: anytype, io: std.Io, allocator: std.mem.Allocator, files: ServerFiles, metadata: xit.object.CommitMetadata(repo.self_repo_opts.hash)) ![xit.hash.hexLen(repo.self_repo_opts.hash)]u8 { 1704 const R = @TypeOf(repo.*); 1705 const opts = repo.self_repo_opts; 1706 const Index = xit.index.Index(.xit, opts); 1707 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1708 const Ctx = struct { 1709 core: *R.Core, 1710 io: std.Io, 1711 allocator: std.mem.Allocator, 1712 files: ServerFiles, 1713 metadata: xit.object.CommitMetadata(opts.hash), 1714 result: *[xit.hash.hexLen(opts.hash)]u8, 1715 1716 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1717 var reader = std.Io.Reader.fixed(content); 1718 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1719 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1720 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1721 defer ctx.allocator.free(path_parts); 1722 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1723 } 1724 1725 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1726 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1727 defer dir.close(ctx.io); 1728 var entries = dir.iterate(); 1729 while (try entries.next(ctx.io)) |entry| { 1730 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1731 defer ctx.allocator.free(child); 1732 switch (entry.kind) { 1733 .directory => try ctx.addDir(state, index, child), 1734 .file => { 1735 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1736 defer ctx.allocator.free(content); 1737 try ctx.addFile(state, index, child, content); 1738 }, 1739 else => {}, 1740 } 1741 } 1742 } 1743 1744 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1745 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1746 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1747 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1748 var index = if (head_oid) |oid| try Index.initFromCommit(state.readOnly(), ctx.io, ctx.allocator, &oid) else try Index.init(state.readOnly(), ctx.io, ctx.allocator); 1749 defer index.deinit(); 1750 switch (ctx.files) { 1751 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1752 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1753 } 1754 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1755 defer tree.deinit(); 1756 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1757 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1758 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1759 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1760 } 1761 }; 1762 try repo.core.db_file.lock(io, .exclusive); 1763 defer repo.core.db_file.unlock(io); 1764 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1765 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1766 .core = &repo.core, 1767 .io = io, 1768 .allocator = allocator, 1769 .files = files, 1770 .metadata = metadata, 1771 .result = &result, 1772 }); 1773 return result; 1774 } 1775