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