╭ 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 const base_oid = blk: { 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 break :blk 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 a descendant of 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{ base_oid, third_source_oid }; 752 const merge_oid = try client.commit(io, allocator, .{ .message = "merge patch", .parent_oids = &merge_parents }); 753 // later commits in the push must not be mistaken for the merge itself 754 _ = try client.commit(io, allocator, .{ .message = "after merge", .allow_empty = true }); 755 try client.push(io, allocator, "origin", "master:feature", false, .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } }); 756 { 757 var target = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = target_path }); 758 defer target.deinit(io, allocator); 759 var arena = std.heap.ArenaAllocator.init(allocator); 760 defer arena.deinit(); 761 const moment = try evt.currentMoment(repo_opts, &target); 762 const patch = (try evt.Patch.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &fork_id)) orelse return error.NotFound; 763 try std.testing.expectEqual(.merged, patch.event.status.kind()); 764 const merged_range = switch (patch.event.status) { 765 .merged => |range| range, 766 else => return error.InvalidPatch, 767 }; 768 try std.testing.expectEqual(.source, merged_range.revision); 769 try std.testing.expectEqualStrings(&base_oid, merged_range.before_oid); 770 try std.testing.expectEqualStrings(&merge_oid, merged_range.after_oid); 771 const imported = (try evt.PatchRev.readById(evt.EventDB(hash_kind), hash_kind, moment, &arena, &second_revision_id)) orelse return error.NotFound; 772 try std.testing.expectEqualStrings(&second_patch_oid, imported.patch_oid); 773 } 774 775 const other_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/other/patch:{s}", .{&fork_id_hex}); 776 defer allocator.free(other_url); 777 try client.addRemote(io, allocator, .{ .name = "other", .value = other_url }); 778 // early rejections need no input. give ssh eof so windows openssh 779 // doesn't hang closing stdin before forwarding the error. 780 const reject_ssh_cmd = try std.fmt.allocPrint(allocator, "{s} -n", .{ssh_cmd}); 781 defer allocator.free(reject_ssh_cmd); 782 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "other", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 783 784 // 785 // the fork remains cloneable after the target repo is removed 786 // 787 788 { 789 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 790 defer admin.deinit(io, allocator); 791 try evt.remove(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, &repo_id, .repo, .{ .name = "admin", .email = "admin@example.test" }); 792 } 793 const clone_path = try std.fs.path.join(allocator, &.{ temp_path, "fork-clone" }); 794 defer allocator.free(clone_path); 795 var fork_clone = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).clone( 796 io, 797 allocator, 798 remote_url, 799 clone_path, 800 clone_path, 801 null, 802 .{ .transport = .{ .wire = .{ .ssh = .{ .command = ssh_cmd } } } }, 803 ); 804 defer fork_clone.deinit(io, allocator); 805 try std.testing.expectEqualStrings(&third_source_oid, &(try fork_clone.readRef(io, fork.ref) orelse return error.NotFound)); 806 807 { 808 var admin = try rp.Repo(.xit, evt.admin_repo_opts).open(io, allocator, .{ .path = admin_path }); 809 defer admin.deinit(io, allocator); 810 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 811 .{ 812 .id = std.fmt.bytesToHex(repo_id, .lower), 813 .timestamp = 2, 814 .author = .{ .name = "admin", .email = "admin@example.test" }, 815 .event = .{ .repo = .{ 816 .user_id = &user_id, 817 .name = "target", 818 .description = "", 819 } }, 820 }, 821 .{ 822 .id = std.fmt.bytesToHex(user_id, .lower), 823 .timestamp = 2, 824 .author = .{ .name = "admin", .email = "admin@example.test" }, 825 .event = .{ .user = .{ 826 .name = "admin", 827 .email = "admin@example.test", 828 .password_hash = "", 829 .ssh_keys = "", 830 } }, 831 }, 832 }); 833 } 834 835 const denied_url = try std.fmt.allocPrint(allocator, "git@localhost:fork/admin/target/patch:{s}", .{&fork_id_hex}); 836 defer allocator.free(denied_url); 837 try client.addRemote(io, allocator, .{ .name = "denied", .value = denied_url }); 838 try std.testing.expectError(error.ServerReportedError, client.push(io, allocator, "denied", "master:patch", false, .{ .wire = .{ .ssh = .{ .command = reject_ssh_cmd } } })); 839 } 840 841 fn testPushCreatesMissingRepo( 842 comptime repo_kind: rp.RepoKind, 843 comptime transport_def: net.TransportDefinition, 844 comptime port: u16, 845 io: std.Io, 846 allocator: std.mem.Allocator, 847 ) !void { 848 var temp = std.testing.tmpDir(.{}); 849 defer temp.cleanup(); 850 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 851 defer allocator.free(temp_path); 852 853 var server_process = try runServer(io, allocator, temp_path, port); 854 defer _ = server_process.kill(io); 855 856 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 857 defer allocator.free(client_path); 858 859 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 860 defer client_repo.deinit(io, allocator); 861 862 const commit1 = blk: { 863 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 864 defer hello_txt.close(io); 865 try hello_txt.writeStreamingAll(io, "hello, world!"); 866 try client_repo.add(io, allocator, &.{"hello.txt"}); 867 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 868 }; 869 870 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 871 872 // the repo is not pre-registered; the push should mint the repo event under 873 // admin and create the repo on disk 874 { 875 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 876 defer allocator.free(remote_url); 877 878 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 879 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 880 } 881 882 const is_ssh = switch (transport_def) { 883 .file => false, 884 .wire => |wire_kind| .ssh == wire_kind, 885 }; 886 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 887 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 888 889 try client_repo.push( 890 io, 891 allocator, 892 "origin", 893 "master", 894 false, 895 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 896 .command = ssh_cmd_maybe, 897 } } }, 898 ); 899 900 // the push registered admin/server; resolve its on-disk directory 901 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", false)).?; 902 defer allocator.free(server_path); 903 904 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 905 defer server_repo.deinit(io, allocator); 906 try std.testing.expect(try server_repo.isBare(io, allocator)); 907 908 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 909 910 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 911 try std.testing.expectEqualStrings(&commit1, &oid_master); 912 } 913 914 fn testPushEvents( 915 comptime repo_kind: rp.RepoKind, 916 comptime transport_def: net.TransportDefinition, 917 comptime hash_kind: hash.HashKind, 918 comptime port: u16, 919 io: std.Io, 920 allocator: std.mem.Allocator, 921 ) !void { 922 // create the temp dir 923 var temp = std.testing.tmpDir(.{}); 924 defer temp.cleanup(); 925 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 926 defer allocator.free(temp_path); 927 928 // init server 929 var server_process = try runServer(io, allocator, temp_path, port); 930 defer _ = server_process.kill(io); 931 932 // register the repo under admin and locate its on-disk directory 933 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 934 defer allocator.free(server_path); 935 936 const repo_opts: rp.RepoOpts(.xit) = .{ .hash = hash_kind, .is_test = true }; 937 const ServerRepo = rp.Repo(.xit, repo_opts); 938 var server_repo = try ServerRepo.init(io, allocator, .{ .bare = true, .path = server_path }); 939 defer server_repo.deinit(io, allocator); 940 941 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 942 defer allocator.free(client_path); 943 944 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 945 defer client_repo.deinit(io, allocator); 946 947 // add remote 948 { 949 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 950 defer allocator.free(remote_url); 951 952 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 953 } 954 955 const is_ssh = switch (transport_def) { 956 .file => false, 957 .wire => |wire_kind| .ssh == wire_kind, 958 }; 959 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 960 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 961 962 var arena = std.heap.ArenaAllocator.init(allocator); 963 defer arena.deinit(); 964 965 // 966 // define test events 967 // 968 969 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 970 const issue_event_id = evt.EventWithId.randomId(prng.random()); 971 972 const events_to_push = [_]evt.EventWithId{ 973 .{ 974 .id = std.fmt.bytesToHex(issue_event_id, .lower), 975 .author = .{ .name = "haxy", .email = "user@haxy" }, 976 .event = .{ 977 .issue = .{ 978 .title = "Login form clears password on validation error", 979 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 980 .tags = "bug priority-high ui", 981 }, 982 }, 983 }, 984 }; 985 986 // commit the issue on the client and push it to the server 987 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 988 try client_repo.push( 989 io, 990 allocator, 991 "origin", 992 "haxy/events", 993 false, 994 .{ .wire = .{ .ssh = .{ 995 .command = ssh_cmd_maybe, 996 } } }, 997 ); 998 999 // the push consumed the issue into the server repo's db 1000 { 1001 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1002 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1003 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1004 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1005 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1006 1007 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.event.description); 1008 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.event.tags); 1009 } 1010 1011 // 1012 // edit the issue 1013 // 1014 1015 // this event edits the previous one because it has the same id 1016 const events_to_push2 = [_]evt.EventWithId{ 1017 .{ 1018 .id = std.fmt.bytesToHex(issue_event_id, .lower), 1019 .author = .{ .name = "haxy", .email = "user@haxy" }, 1020 .event = .{ 1021 .issue = .{ 1022 .title = "Login form clears password on validation error", 1023 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 1024 .tags = "bug priority-low ui", 1025 }, 1026 }, 1027 }, 1028 }; 1029 1030 // commit the edit on the client and push it to the server 1031 try evt.consume(.local, .repo, repo_kind, .{ .hash = hash_kind, .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 1032 try client_repo.push( 1033 io, 1034 allocator, 1035 "origin", 1036 "haxy/events", 1037 false, 1038 .{ .wire = .{ .ssh = .{ 1039 .command = ssh_cmd_maybe, 1040 } } }, 1041 ); 1042 1043 // the push consumed the edit into the server repo's db 1044 { 1045 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 1046 const records_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.record_map_key)) orelse return error.NotFound; 1047 const records = try ServerRepo.DB.HashMap(.read_only).init(records_cursor); 1048 const issue_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 1049 const issue = try evt.read(evt.Issue.Record, ServerRepo.DB, repo_opts.hash, &arena, try ServerRepo.DB.HashMap(.read_only).init(issue_cursor)); 1050 1051 // the description and tags were correctly edited 1052 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.event.description); 1053 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.event.tags); 1054 } 1055 } 1056 1057 fn testClone( 1058 comptime repo_kind: rp.RepoKind, 1059 comptime transport_def: net.TransportDefinition, 1060 comptime shell_out_to_git: bool, 1061 comptime hash_kind: hash.HashKind, 1062 comptime port: u16, 1063 io: std.Io, 1064 allocator: std.mem.Allocator, 1065 ) !void { 1066 // create the temp dir 1067 var temp = std.testing.tmpDir(.{}); 1068 defer temp.cleanup(); 1069 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1070 defer allocator.free(temp_path); 1071 1072 // init server 1073 var server_process = try runServer(io, allocator, temp_path, port); 1074 defer _ = server_process.kill(io); 1075 1076 // register the repo under admin and locate its on-disk directory 1077 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1078 defer allocator.free(server_path); 1079 1080 // init server repo with default branch name as main 1081 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 1082 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" }); 1083 defer server_repo.deinit(io, allocator); 1084 1085 if (shell_out_to_git) { 1086 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 1087 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 1088 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 1089 } 1090 1091 // make a commit 1092 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "hello.txt", .content = "hello, world!" }} }, .{ .message = "let there be light" }); 1093 1094 // tag first commit 1095 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 1096 1097 // make a commit 1098 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "goodbye.txt", .content = "goodbye, world!" }} }, .{ .message = "add goodbye file" }); 1099 1100 // export server repo 1101 { 1102 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1103 defer export_file.close(io); 1104 } 1105 1106 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1107 defer allocator.free(client_path); 1108 1109 // get remote url 1110 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1111 defer allocator.free(remote_url); 1112 1113 const is_ssh = switch (transport_def) { 1114 .file => false, 1115 .wire => |wire_kind| .ssh == wire_kind, 1116 }; 1117 1118 if (shell_out_to_git) { 1119 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1120 defer allocator.free(ssh_cmd); 1121 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1122 defer allocator.free(ssh_config_arg); 1123 1124 { 1125 var process = try std.process.spawn(io, .{ 1126 .argv = if (is_ssh) 1127 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--depth", "1", remote_url, "client" } 1128 else 1129 &.{ "git", "clone", "--quiet", "--depth", "1", remote_url, "client" }, 1130 .cwd = .{ .path = temp_path }, 1131 .stdin = .ignore, 1132 .stdout = .ignore, 1133 .stderr = .inherit, 1134 }); 1135 defer process.kill(io); 1136 const term = try process.wait(io); 1137 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1138 } 1139 1140 // make sure shallow clone was successful 1141 { 1142 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1143 hello_txt.close(io); 1144 } 1145 1146 // make a third commit on the server 1147 _ = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1148 1149 // pull --unshallow to deepen the clone and get the new commit 1150 { 1151 var process = try std.process.spawn(io, .{ 1152 .argv = if (is_ssh) 1153 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "--unshallow" } 1154 else 1155 &.{ "git", "pull", "--quiet", "--unshallow" }, 1156 .cwd = .{ .path = client_path }, 1157 .stdin = .ignore, 1158 .stdout = .ignore, 1159 .stderr = .inherit, 1160 }); 1161 defer process.kill(io); 1162 const term = try process.wait(io); 1163 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1164 } 1165 1166 // make sure unshallow pull was successful 1167 { 1168 const extra_txt = try temp.dir.openFile(io, "client/extra.txt", .{}); 1169 extra_txt.close(io); 1170 } 1171 1172 // delete client and clone again with --shallow-since 1173 try temp.dir.deleteTree(io, "client"); 1174 1175 { 1176 var process = try std.process.spawn(io, .{ 1177 .argv = if (is_ssh) 1178 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" } 1179 else 1180 &.{ "git", "clone", "--quiet", "--shallow-since=2000-01-01", remote_url, "client" }, 1181 .cwd = .{ .path = temp_path }, 1182 .stdin = .ignore, 1183 .stdout = .ignore, 1184 .stderr = .inherit, 1185 }); 1186 defer process.kill(io); 1187 const term = try process.wait(io); 1188 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1189 } 1190 1191 // make sure shallow clone was successful 1192 { 1193 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1194 hello_txt.close(io); 1195 } 1196 1197 // delete client and clone again with --shallow-exclude 1198 try temp.dir.deleteTree(io, "client"); 1199 1200 { 1201 var process = try std.process.spawn(io, .{ 1202 .argv = if (is_ssh) 1203 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1204 else 1205 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1206 .cwd = .{ .path = temp_path }, 1207 .stdin = .ignore, 1208 .stdout = .ignore, 1209 .stderr = .inherit, 1210 }); 1211 defer process.kill(io); 1212 const term = try process.wait(io); 1213 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1214 } 1215 1216 // make sure shallow clone was successful 1217 { 1218 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1219 hello_txt.close(io); 1220 } 1221 1222 // delete client and clone again with --filter=blob:none 1223 try temp.dir.deleteTree(io, "client"); 1224 1225 { 1226 var process = try std.process.spawn(io, .{ 1227 .argv = if (is_ssh) 1228 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1229 else 1230 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1231 .cwd = .{ .path = temp_path }, 1232 .stdin = .ignore, 1233 .stdout = .ignore, 1234 .stderr = .inherit, 1235 }); 1236 defer process.kill(io); 1237 const term = try process.wait(io); 1238 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1239 } 1240 1241 // make sure partial clone was successful 1242 { 1243 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1244 hello_txt.close(io); 1245 } 1246 1247 // delete client and clone again with --filter=tree:0 1248 try temp.dir.deleteTree(io, "client"); 1249 1250 { 1251 var process = try std.process.spawn(io, .{ 1252 .argv = if (is_ssh) 1253 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1254 else 1255 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1256 .cwd = .{ .path = temp_path }, 1257 .stdin = .ignore, 1258 .stdout = .ignore, 1259 .stderr = .inherit, 1260 }); 1261 defer process.kill(io); 1262 const term = try process.wait(io); 1263 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1264 } 1265 1266 // make sure treeless clone was successful 1267 { 1268 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1269 goodbye_txt.close(io); 1270 } 1271 } else { 1272 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1273 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1274 1275 // clone repo 1276 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1277 io, 1278 allocator, 1279 remote_url, 1280 temp_path, 1281 client_path, 1282 null, 1283 .{ .transport = .{ .wire = .{ .ssh = .{ 1284 .command = ssh_cmd_maybe, 1285 } } } }, 1286 ); 1287 defer clone_result.deinit(io, allocator); 1288 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1289 1290 // make sure HEAD points to the right default branch 1291 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1292 const head = try client_repo.head(io, &current_branch_buffer); 1293 try std.testing.expectEqualStrings("main", head.ref.name); 1294 1295 // make sure clone was successful 1296 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1297 defer hello_txt.close(io); 1298 } 1299 } 1300 1301 fn testFetchLarge( 1302 comptime repo_kind: rp.RepoKind, 1303 comptime transport_def: net.TransportDefinition, 1304 comptime shell_out_to_git: bool, 1305 comptime hash_kind: hash.HashKind, 1306 comptime port: u16, 1307 io: std.Io, 1308 allocator: std.mem.Allocator, 1309 ) !void { 1310 // create the temp dir 1311 var temp = std.testing.tmpDir(.{}); 1312 defer temp.cleanup(); 1313 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1314 defer allocator.free(temp_path); 1315 1316 // init server 1317 var server_process = try runServer(io, allocator, temp_path, port); 1318 defer _ = server_process.kill(io); 1319 1320 // register the repo under admin and locate its on-disk directory 1321 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1322 defer allocator.free(server_path); 1323 1324 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1325 defer server_repo.deinit(io, allocator); 1326 1327 // make a commit with files from the current repo 1328 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1329 1330 // export server repo 1331 { 1332 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1333 defer export_file.close(io); 1334 } 1335 1336 if (shell_out_to_git) { 1337 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1338 } 1339 1340 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1341 defer allocator.free(client_path); 1342 1343 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1344 defer client_repo.deinit(io, allocator); 1345 1346 // add remote 1347 { 1348 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1349 defer allocator.free(remote_url); 1350 1351 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1352 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1353 } 1354 1355 const is_ssh = switch (transport_def) { 1356 .file => false, 1357 .wire => |wire_kind| .ssh == wire_kind, 1358 }; 1359 1360 if (shell_out_to_git) { 1361 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1362 defer allocator.free(ssh_cmd); 1363 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1364 defer allocator.free(ssh_config_arg); 1365 1366 { 1367 var process = try std.process.spawn(io, .{ 1368 .argv = if (is_ssh) 1369 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1370 else 1371 &.{ "git", "pull", "--quiet", "origin", "master" }, 1372 .cwd = .{ .path = client_path }, 1373 .stdin = .ignore, 1374 .stdout = .ignore, 1375 .stderr = .inherit, 1376 }); 1377 defer process.kill(io); 1378 const term = try process.wait(io); 1379 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1380 } 1381 1382 // make sure pull was successful 1383 { 1384 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1385 try std.testing.expectEqualStrings(&commit1, &oid_master); 1386 } 1387 1388 // make another commit on the server 1389 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1390 1391 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1392 { 1393 var process = try std.process.spawn(io, .{ 1394 .argv = if (is_ssh) 1395 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1396 else 1397 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1398 .cwd = .{ .path = client_path }, 1399 .stdin = .ignore, 1400 .stdout = .ignore, 1401 .stderr = .inherit, 1402 }); 1403 defer process.kill(io); 1404 const term = try process.wait(io); 1405 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1406 } 1407 1408 // make sure fetch with want-ref was successful 1409 { 1410 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1411 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1412 } 1413 } else { 1414 const refspecs = &.{ 1415 "+refs/heads/master:refs/heads/master", 1416 }; 1417 1418 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1419 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1420 1421 try client_repo.fetch( 1422 io, 1423 allocator, 1424 "origin", 1425 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1426 .command = ssh_cmd_maybe, 1427 } } }, 1428 ); 1429 1430 // update the working dir 1431 try client_repo.restore(io, allocator, "."); 1432 } 1433 1434 // make sure fetch was successful 1435 { 1436 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1437 try std.testing.expectEqualStrings(&commit1, &oid_master); 1438 } 1439 } 1440 1441 fn testPushLarge( 1442 comptime repo_kind: rp.RepoKind, 1443 comptime transport_def: net.TransportDefinition, 1444 comptime shell_out_to_git: bool, 1445 comptime hash_kind: hash.HashKind, 1446 comptime port: u16, 1447 io: std.Io, 1448 allocator: std.mem.Allocator, 1449 ) !void { 1450 // create the temp dir 1451 var temp = std.testing.tmpDir(.{}); 1452 defer temp.cleanup(); 1453 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1454 defer allocator.free(temp_path); 1455 1456 // init server 1457 var server_process = try runServer(io, allocator, temp_path, port); 1458 defer _ = server_process.kill(io); 1459 1460 // register the repo under admin and locate its on-disk directory 1461 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1462 defer allocator.free(server_path); 1463 1464 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1465 defer server_repo.deinit(io, allocator); 1466 1467 // add config 1468 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1469 1470 // export server repo 1471 { 1472 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1473 defer export_file.close(io); 1474 } 1475 1476 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1477 defer allocator.free(client_path); 1478 1479 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1480 defer client_repo.deinit(io, allocator); 1481 1482 var client_dir = try temp.dir.openDir(io, "client", .{}); 1483 defer client_dir.close(io); 1484 1485 { 1486 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1487 defer hello_txt.close(io); 1488 try hello_txt.writeStreamingAll(io, "hello, world!"); 1489 try client_repo.add(io, allocator, &.{"hello.txt"}); 1490 } 1491 1492 // copy files from current repo into client dir 1493 const cwd = std.Io.Dir.cwd(); 1494 for (&[_][]const u8{"src"}) |dir_name| { 1495 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1496 defer src_repo_dir.close(io); 1497 1498 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1499 defer dest_repo_dir.close(io); 1500 1501 try copyDir(io, src_repo_dir, dest_repo_dir); 1502 1503 try client_repo.add(io, allocator, &.{dir_name}); 1504 } 1505 1506 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1507 1508 // change the files so git will send them as delta objects 1509 for (&[_][]const u8{"src"}) |dir_name| { 1510 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1511 defer dest_repo_dir.close(io); 1512 1513 { 1514 var iter = dest_repo_dir.iterate(); 1515 while (try iter.next(io)) |entry| { 1516 switch (entry.kind) { 1517 .file => { 1518 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1519 defer file.close(io); 1520 var writer = file.writer(io, &.{}); 1521 try writer.interface.writeAll("EDIT"); 1522 }, 1523 else => {}, 1524 } 1525 } 1526 } 1527 1528 try client_repo.add(io, allocator, &.{dir_name}); 1529 } 1530 1531 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1532 1533 // add remote 1534 { 1535 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1536 defer allocator.free(remote_url); 1537 1538 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1539 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1540 } 1541 1542 const is_ssh = switch (transport_def) { 1543 .file => false, 1544 .wire => |wire_kind| .ssh == wire_kind, 1545 }; 1546 1547 if (shell_out_to_git) { 1548 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1549 defer allocator.free(ssh_cmd); 1550 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1551 defer allocator.free(ssh_config_arg); 1552 1553 // shell out to git so it will send delta objects 1554 var process = try std.process.spawn(io, .{ 1555 .argv = if (is_ssh) 1556 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1557 else 1558 &.{ "git", "push", "--quiet", "origin", "master" }, 1559 .cwd = .{ .path = client_path }, 1560 .stdin = .ignore, 1561 .stdout = .ignore, 1562 .stderr = .inherit, 1563 }); 1564 defer process.kill(io); 1565 const term = try process.wait(io); 1566 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1567 } else { 1568 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1569 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1570 1571 try client_repo.push( 1572 io, 1573 allocator, 1574 "origin", 1575 "master", 1576 false, 1577 .{ .wire = .{ .ssh = .{ 1578 .command = ssh_cmd_maybe, 1579 } } }, 1580 ); 1581 } 1582 1583 // make sure push was successful 1584 { 1585 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1586 try std.testing.expectEqualStrings(&commit2, &oid_master); 1587 1588 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1589 } 1590 } 1591 1592 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1593 var iter = src_dir.iterate(); 1594 while (try iter.next(io)) |entry| { 1595 switch (entry.kind) { 1596 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1597 .directory => { 1598 try dest_dir.createDirPath(io, entry.name); 1599 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1600 defer dest_entry_dir.close(io); 1601 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1602 defer src_entry_dir.close(io); 1603 try copyDir(io, src_entry_dir, dest_entry_dir); 1604 }, 1605 else => {}, 1606 } 1607 } 1608 } 1609 1610 // the dev SSH public key matching the private key written by runServer 1611 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1612 1613 // create the admin event repo with a user holding the dev SSH public key 1614 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1615 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1616 defer allocator.free(admin_repo_path); 1617 1618 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1619 defer repo.deinit(io, allocator); 1620 1621 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1622 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1623 1624 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1625 const user_id = evt.EventWithId.randomId(prng.random()); 1626 1627 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1628 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1629 1630 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1631 .id = std.fmt.bytesToHex(user_id, .lower), 1632 .author = .{ .name = "admin", .email = "admin@example.test" }, 1633 .event = .{ .user = .{ 1634 .name = "admin", 1635 .email = "admin@example.test", 1636 .password_hash = password_hash, 1637 .ssh_keys = admin_ssh_key, 1638 } }, 1639 }}); 1640 } 1641 1642 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1643 // the event store 1644 fn repoOnDiskPath( 1645 io: std.Io, 1646 allocator: std.mem.Allocator, 1647 data_path: []const u8, 1648 repo_name: []const u8, 1649 create: bool, 1650 ) !?[]u8 { 1651 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1652 defer allocator.free(admin_repo_path); 1653 1654 const event_id_hex = (try evt.resolveOrCreateRepo( 1655 io, 1656 allocator, 1657 admin_repo_path, 1658 "admin", 1659 repo_name, 1660 if (create) .{ .read_access = .public } else null, 1661 )) orelse return null; 1662 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1663 } 1664 1665 // build the remote URL addressing admin/<repo_name> over the given transport 1666 fn remoteUrl( 1667 comptime transport_def: net.TransportDefinition, 1668 allocator: std.mem.Allocator, 1669 repo_name: []const u8, 1670 port: u16, 1671 ) ![]u8 { 1672 return switch (transport_def) { 1673 .file => unreachable, 1674 .wire => |wire_kind| switch (wire_kind) { 1675 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1676 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1677 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1678 }, 1679 }; 1680 } 1681 1682 // the ssh command the haxy git client should invoke, or null when not using 1683 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1684 // and skip writing to known_hosts. 1685 fn sshCommand( 1686 is_ssh: bool, 1687 allocator: std.mem.Allocator, 1688 temp_path: []const u8, 1689 port: u16, 1690 ) !?[]u8 { 1691 if (!is_ssh) return null; 1692 1693 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1694 defer allocator.free(priv_key_path); 1695 1696 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1697 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1698 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 }); 1699 } 1700 1701 const ServerFiles = union(enum) { 1702 files: []const struct { path: []const u8, content: []const u8 }, 1703 dirs: []const []const u8, 1704 }; 1705 1706 /// commit files without writing the server's index or worktree. 1707 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 { 1708 const R = @TypeOf(repo.*); 1709 const opts = repo.self_repo_opts; 1710 const Index = xit.index.Index(.xit, opts); 1711 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1712 const Ctx = struct { 1713 core: *R.Core, 1714 io: std.Io, 1715 allocator: std.mem.Allocator, 1716 files: ServerFiles, 1717 metadata: xit.object.CommitMetadata(opts.hash), 1718 result: *[xit.hash.hexLen(opts.hash)]u8, 1719 1720 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1721 var reader = std.Io.Reader.fixed(content); 1722 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1723 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1724 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1725 defer ctx.allocator.free(path_parts); 1726 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1727 } 1728 1729 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1730 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1731 defer dir.close(ctx.io); 1732 var entries = dir.iterate(); 1733 while (try entries.next(ctx.io)) |entry| { 1734 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1735 defer ctx.allocator.free(child); 1736 switch (entry.kind) { 1737 .directory => try ctx.addDir(state, index, child), 1738 .file => { 1739 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1740 defer ctx.allocator.free(content); 1741 try ctx.addFile(state, index, child, content); 1742 }, 1743 else => {}, 1744 } 1745 } 1746 } 1747 1748 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1749 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1750 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1751 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1752 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); 1753 defer index.deinit(); 1754 switch (ctx.files) { 1755 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1756 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1757 } 1758 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1759 defer tree.deinit(); 1760 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1761 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1762 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1763 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1764 } 1765 }; 1766 try repo.core.db_file.lock(io, .exclusive); 1767 defer repo.core.db_file.unlock(io); 1768 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1769 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1770 .core = &repo.core, 1771 .io = io, 1772 .allocator = allocator, 1773 .files = files, 1774 .metadata = metadata, 1775 .result = &result, 1776 }); 1777 return result; 1778 } 1779