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