╭ 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 rp = xit.repo; 7 const rf = xit.ref; 8 const work = xit.workdir; 9 const hash = xit.hash; 10 const net = xit.net; 11 12 const http_port: u16 = 3000; 13 const ssh_port: u16 = 3001; 14 15 test "fetch small" { 16 const io = std.testing.io; 17 const allocator = std.testing.allocator; 18 try testFetch(.xit, .{ .wire = .http }, io, allocator); 19 if (.windows != builtin.os.tag) { 20 try testFetch(.xit, .{ .wire = .ssh }, 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 }, io, allocator); 29 } 30 } 31 32 test "push creates missing repo under serve" { 33 const io = std.testing.io; 34 const allocator = std.testing.allocator; 35 if (.windows != builtin.os.tag) { 36 try testPushCreatesMissingRepo(.xit, .{ .wire = .ssh }, io, allocator); 37 } 38 } 39 40 test "push events" { 41 const io = std.testing.io; 42 const allocator = std.testing.allocator; 43 if (.windows != builtin.os.tag) { 44 try testPushEvents(.xit, .{ .wire = .ssh }, io, allocator); 45 } 46 } 47 48 test "clone small" { 49 const io = std.testing.io; 50 const allocator = std.testing.allocator; 51 try testClone(.xit, .{ .wire = .http }, false, io, allocator); 52 if (.windows != builtin.os.tag) { 53 try testClone(.xit, .{ .wire = .ssh }, false, io, allocator); 54 } 55 } 56 57 test "clone small subprocess" { 58 const io = std.testing.io; 59 const allocator = std.testing.allocator; 60 try testClone(.git, .{ .wire = .http }, true, io, allocator); 61 if (.windows != builtin.os.tag) { 62 try testClone(.git, .{ .wire = .ssh }, true, io, allocator); 63 } 64 } 65 66 test "fetch large subprocess" { 67 const io = std.testing.io; 68 const allocator = std.testing.allocator; 69 try testFetchLarge(.git, .{ .wire = .http }, true, io, allocator); 70 if (.windows != builtin.os.tag) { 71 try testFetchLarge(.git, .{ .wire = .ssh }, true, io, allocator); 72 } 73 } 74 75 test "push large subprocess" { 76 const io = std.testing.io; 77 const allocator = std.testing.allocator; 78 if (.windows != builtin.os.tag) { 79 try testPushLarge(.git, .{ .wire = .ssh }, true, io, allocator); 80 } 81 } 82 83 fn runServer( 84 io: std.Io, 85 allocator: std.mem.Allocator, 86 comptime temp_dir_name: []const u8, 87 ) !std.process.Child { 88 { 89 const priv_key_file = try std.Io.Dir.cwd().createFile(io, temp_dir_name ++ "/key", .{}); 90 defer priv_key_file.close(io); 91 try priv_key_file.writeStreamingAll(io, 92 \\-----BEGIN OPENSSH PRIVATE KEY----- 93 \\b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 94 \\QyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22JgAAAJA+7hikPu4Y 95 \\pAAAAAtzc2gtZWQyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22Jg 96 \\AAAEDVlopOMnKt/7by/IA8VZvQXUS/O6VLkixOqnnahUdPCKeIs8mJqigBZ5y84J4COgnA 97 \\JJ5bHPKy+lM2SliMXbYmAAAAC3JhZGFyQHJvYXJrAQI= 98 \\-----END OPENSSH PRIVATE KEY----- 99 \\ 100 ); 101 if (.windows != builtin.os.tag) { 102 try priv_key_file.setPermissions(io, @enumFromInt(0o600)); 103 } 104 } 105 106 // seed the admin event store so the server can resolve <owner>/<repo> paths 107 // and authenticate pushes from the dev key above 108 try setupAdmin(io, allocator, temp_dir_name); 109 110 const cwd_path = try std.process.currentPathAlloc(io, allocator); 111 defer allocator.free(cwd_path); 112 const haxy_path = try std.fs.path.join(allocator, &.{ cwd_path, "zig-out/bin/haxy" }); 113 defer allocator.free(haxy_path); 114 115 const http_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{http_port}); 116 const ssh_listen_arg = std.fmt.comptimePrint("127.0.0.1:{}", .{ssh_port}); 117 118 const process = try std.process.spawn(io, .{ 119 .argv = &.{ haxy_path, "serve", "--http-listen", http_listen_arg, "--ssh-listen", ssh_listen_arg, "--data-dir", temp_dir_name }, 120 .stdin = .ignore, 121 .stdout = .ignore, 122 .stderr = .ignore, 123 }); 124 125 const address = try std.Io.net.IpAddress.parseIp4("127.0.0.1", http_port); 126 for (0..50) |_| { 127 const stream = address.connect(io, .{ .mode = .stream }) catch { 128 try std.Io.sleep(io, .fromMilliseconds(100), .real); 129 continue; 130 }; 131 stream.close(io); 132 break; 133 } 134 135 return process; 136 } 137 138 fn testFetch( 139 comptime repo_kind: rp.RepoKind, 140 comptime transport_def: net.TransportDefinition, 141 io: std.Io, 142 allocator: std.mem.Allocator, 143 ) !void { 144 const temp_dir_name = "temp-testnet-fetch"; 145 146 // create the temp dir 147 const cwd = std.Io.Dir.cwd(); 148 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 149 if (temp_dir_or_err) |*temp_dir| { 150 temp_dir.close(io); 151 try cwd.deleteTree(io, temp_dir_name); 152 } else |_| {} 153 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 154 defer cwd.deleteTree(io, temp_dir_name) catch {}; 155 defer temp_dir.close(io); 156 157 // init server 158 var server_process = try runServer(io, allocator, temp_dir_name); 159 defer _ = server_process.kill(io); 160 161 // register the repo under admin and locate its on-disk directory 162 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 163 defer allocator.free(server_path); 164 165 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 166 defer server_repo.deinit(io, allocator); 167 168 // make a commit 169 const commit1 = blk: { 170 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 171 defer hello_txt.close(io); 172 try hello_txt.writeStreamingAll(io, "hello, world!"); 173 try server_repo.add(io, allocator, &.{"hello.txt"}); 174 break :blk try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 175 }; 176 177 // export server repo 178 { 179 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 180 defer export_file.close(io); 181 182 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowAnySHA1InWant", .value = "true" }); 183 } 184 185 // add a tag 186 _ = try server_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 187 188 const cwd_path = try std.process.currentPathAlloc(io, allocator); 189 defer allocator.free(cwd_path); 190 191 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 192 defer allocator.free(client_path); 193 194 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 195 defer client_repo.deinit(io, allocator); 196 197 // add remote 198 { 199 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 200 defer allocator.free(remote_url); 201 202 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 203 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 204 } 205 206 // create refspec with oid as a test 207 const oid_refspec = try std.fmt.allocPrint(allocator, "+{s}:refs/heads/foo", .{&commit1}); 208 defer allocator.free(oid_refspec); 209 210 const refspecs = &.{ 211 "+refs/heads/master:refs/heads/master", 212 oid_refspec, 213 }; 214 215 const is_ssh = switch (transport_def) { 216 .file => false, 217 .wire => |wire_kind| .ssh == wire_kind, 218 }; 219 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 220 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 221 222 try client_repo.fetch( 223 io, 224 allocator, 225 "origin", 226 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 227 .command = ssh_cmd_maybe, 228 } } }, 229 ); 230 231 // update the working dir 232 try client_repo.restore(io, allocator, "."); 233 234 // make sure fetch was successful 235 { 236 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 237 defer hello_txt.close(io); 238 239 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 240 try std.testing.expect(null != try client_repo.readRef(io, .{ .kind = .head, .name = "foo" })); 241 242 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 243 try std.testing.expectEqualStrings(&commit1, &oid_master); 244 } 245 246 // make another commit 247 const commit2 = blk: { 248 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 249 defer goodbye_txt.close(io); 250 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 251 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 252 break :blk try server_repo.commit(io, allocator, .{ .message = "goodbye" }); 253 }; 254 255 try client_repo.fetch( 256 io, 257 allocator, 258 "origin", 259 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 260 .command = ssh_cmd_maybe, 261 } } }, 262 ); 263 264 // update the working dir 265 try client_repo.restore(io, allocator, "."); 266 267 // make sure fetch was successful 268 { 269 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 270 defer goodbye_txt.close(io); 271 272 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 273 try std.testing.expectEqualStrings(&commit2, &oid_master); 274 } 275 } 276 277 fn testPush( 278 comptime repo_kind: rp.RepoKind, 279 comptime transport_def: net.TransportDefinition, 280 io: std.Io, 281 allocator: std.mem.Allocator, 282 ) !void { 283 const temp_dir_name = "temp-testnet-push"; 284 285 // create the temp dir 286 const cwd = std.Io.Dir.cwd(); 287 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 288 if (temp_dir_or_err) |*temp_dir| { 289 temp_dir.close(io); 290 try cwd.deleteTree(io, temp_dir_name); 291 } else |_| {} 292 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 293 defer cwd.deleteTree(io, temp_dir_name) catch {}; 294 defer temp_dir.close(io); 295 296 // init server 297 var server_process = try runServer(io, allocator, temp_dir_name); 298 defer _ = server_process.kill(io); 299 300 // register the repo under admin and locate its on-disk directory 301 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 302 defer allocator.free(server_path); 303 304 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 305 defer server_repo.deinit(io, allocator); 306 307 // add config 308 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 309 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 310 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 311 312 // export server repo 313 { 314 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 315 defer export_file.close(io); 316 } 317 318 const cwd_path = try std.process.currentPathAlloc(io, allocator); 319 defer allocator.free(cwd_path); 320 321 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 322 defer allocator.free(client_path); 323 324 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 325 defer client_repo.deinit(io, allocator); 326 327 // make a commit 328 const commit1 = blk: { 329 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 330 defer hello_txt.close(io); 331 try hello_txt.writeStreamingAll(io, "hello, world!"); 332 try client_repo.add(io, allocator, &.{"hello.txt"}); 333 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 334 }; 335 336 // add a tag 337 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 338 339 // add remote 340 { 341 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 342 defer allocator.free(remote_url); 343 344 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 345 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 346 } 347 348 const refspecs = &.{ 349 "refs/tags/1.0.0:refs/tags/1.0.0", 350 }; 351 352 const is_ssh = switch (transport_def) { 353 .file => false, 354 .wire => |wire_kind| .ssh == wire_kind, 355 }; 356 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 357 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 358 359 try client_repo.push( 360 io, 361 allocator, 362 "origin", 363 "master", 364 false, 365 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 366 .command = ssh_cmd_maybe, 367 } } }, 368 ); 369 370 // make sure push was successful 371 { 372 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 373 374 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 375 try std.testing.expectEqualStrings(&commit1, &oid_master); 376 } 377 378 // make a commit on the server 379 { 380 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 381 defer hello_txt.close(io); 382 try hello_txt.writeStreamingAll(io, "hello, world from the server!"); 383 try server_repo.add(io, allocator, &.{"hello.txt"}); 384 _ = try server_repo.commit(io, allocator, .{ .message = "new commit from the server" }); 385 } 386 387 // make another commit 388 const commit2 = blk: { 389 const goodbye_txt = try client_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 390 defer goodbye_txt.close(io); 391 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 392 try client_repo.add(io, allocator, &.{"goodbye.txt"}); 393 break :blk try client_repo.commit(io, allocator, .{ .message = "goodbye" }); 394 }; 395 396 // can't push because server has commit not found locally 397 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 398 io, 399 allocator, 400 "origin", 401 "master", 402 false, 403 .{ .wire = .{ .ssh = .{ 404 .command = ssh_cmd_maybe, 405 } } }, 406 )); 407 408 // make a commit on the server with no parents, thus creating an incompatible git history 409 { 410 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 411 defer hello_txt.close(io); 412 try hello_txt.writeStreamingAll(io, "hello, world from the server again!"); 413 try server_repo.add(io, allocator, &.{"hello.txt"}); 414 _ = try server_repo.commit(io, allocator, .{ .message = "new git history on the server", .parent_oids = &.{} }); 415 } 416 417 // can't push because commit doesn't exist locally 418 try std.testing.expectError(error.RemoteRefContainsCommitsNotFoundLocally, client_repo.push( 419 io, 420 allocator, 421 "origin", 422 "master", 423 false, 424 .{ .wire = .{ .ssh = .{ 425 .command = ssh_cmd_maybe, 426 } } }, 427 )); 428 429 // retrieve the commit object 430 try client_repo.fetch( 431 io, 432 allocator, 433 "origin", 434 .{ .wire = .{ .ssh = .{ 435 .command = ssh_cmd_maybe, 436 } } }, 437 ); 438 439 // can't push because server's history is incompatible 440 try std.testing.expectError(error.RemoteRefContainsIncompatibleHistory, client_repo.push( 441 io, 442 allocator, 443 "origin", 444 "master", 445 false, 446 .{ .wire = .{ .ssh = .{ 447 .command = ssh_cmd_maybe, 448 } } }, 449 )); 450 451 // test denyNonFastForwards 452 { 453 // set denyNonFastForwards on server 454 try server_repo.addConfig(io, allocator, .{ .name = "receive.denynonfastforwards", .value = "true" }); 455 456 // save the server's current master ref 457 const oid_before_denied_push = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 458 459 // force push should be rejected by server due to denyNonFastForwards 460 try std.testing.expectError(error.RemoteRejectedRef, client_repo.push( 461 io, 462 allocator, 463 "origin", 464 "master", 465 true, 466 .{ .wire = .{ .ssh = .{ 467 .command = ssh_cmd_maybe, 468 } } }, 469 )); 470 471 // verify the server ref was not updated (push was denied) 472 { 473 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 474 try std.testing.expectEqualStrings(&oid_before_denied_push, &oid_master); 475 } 476 477 // remove denyNonFastForwards from server 478 try server_repo.removeConfig(io, allocator, .{ .name = "receive.denynonfastforwards" }); 479 } 480 481 // force push 482 try client_repo.push( 483 io, 484 allocator, 485 "origin", 486 "master", 487 true, 488 .{ .wire = .{ .ssh = .{ 489 .command = ssh_cmd_maybe, 490 } } }, 491 ); 492 493 // make sure push was successful 494 { 495 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 496 try std.testing.expectEqualStrings(&commit2, &oid_master); 497 } 498 499 // remove the remote tag 500 try client_repo.push( 501 io, 502 allocator, 503 "origin", 504 ":refs/tags/1.0.0", 505 false, 506 .{ .wire = .{ .ssh = .{ 507 .command = ssh_cmd_maybe, 508 } } }, 509 ); 510 511 // make sure push was successful 512 try std.testing.expect(null == try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 513 } 514 515 fn testPushCreatesMissingRepo( 516 comptime repo_kind: rp.RepoKind, 517 comptime transport_def: net.TransportDefinition, 518 io: std.Io, 519 allocator: std.mem.Allocator, 520 ) !void { 521 const temp_dir_name = "temp-testnet-push-create"; 522 523 const cwd = std.Io.Dir.cwd(); 524 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 525 if (temp_dir_or_err) |*temp_dir| { 526 temp_dir.close(io); 527 try cwd.deleteTree(io, temp_dir_name); 528 } else |_| {} 529 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 530 defer cwd.deleteTree(io, temp_dir_name) catch {}; 531 defer temp_dir.close(io); 532 533 var server_process = try runServer(io, allocator, temp_dir_name); 534 defer _ = server_process.kill(io); 535 536 const cwd_path = try std.process.currentPathAlloc(io, allocator); 537 defer allocator.free(cwd_path); 538 539 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 540 defer allocator.free(client_path); 541 542 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 543 defer client_repo.deinit(io, allocator); 544 545 const commit1 = blk: { 546 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 547 defer hello_txt.close(io); 548 try hello_txt.writeStreamingAll(io, "hello, world!"); 549 try client_repo.add(io, allocator, &.{"hello.txt"}); 550 break :blk try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 551 }; 552 553 _ = try client_repo.addTag(io, allocator, .{ .name = "1.0.0", .message = "hi" }); 554 555 // the repo is not pre-registered; the push should mint the repo event under 556 // admin and create the repo on disk 557 { 558 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 559 defer allocator.free(remote_url); 560 561 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 562 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 563 } 564 565 const is_ssh = switch (transport_def) { 566 .file => false, 567 .wire => |wire_kind| .ssh == wire_kind, 568 }; 569 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 570 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 571 572 try client_repo.push( 573 io, 574 allocator, 575 "origin", 576 "master", 577 false, 578 .{ .refspecs = &.{"refs/tags/1.0.0:refs/tags/1.0.0"}, .wire = .{ .ssh = .{ 579 .command = ssh_cmd_maybe, 580 } } }, 581 ); 582 583 // the push registered admin/server; resolve its on-disk directory 584 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", false)).?; 585 defer allocator.free(server_path); 586 587 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).open(io, allocator, .{ .path = server_path }); 588 defer server_repo.deinit(io, allocator); 589 590 try std.testing.expect(null != try server_repo.readRef(io, .{ .kind = .tag, .name = "1.0.0" })); 591 592 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 593 try std.testing.expectEqualStrings(&commit1, &oid_master); 594 } 595 596 fn testPushEvents( 597 comptime repo_kind: rp.RepoKind, 598 comptime transport_def: net.TransportDefinition, 599 io: std.Io, 600 allocator: std.mem.Allocator, 601 ) !void { 602 const temp_dir_name = "temp-testnet-push-events"; 603 604 // create the temp dir 605 const cwd = std.Io.Dir.cwd(); 606 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 607 if (temp_dir_or_err) |*temp_dir| { 608 temp_dir.close(io); 609 try cwd.deleteTree(io, temp_dir_name); 610 } else |_| {} 611 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 612 defer cwd.deleteTree(io, temp_dir_name) catch {}; 613 defer temp_dir.close(io); 614 615 // init server 616 var server_process = try runServer(io, allocator, temp_dir_name); 617 defer _ = server_process.kill(io); 618 619 // register the repo under admin and locate its on-disk directory 620 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 621 defer allocator.free(server_path); 622 623 const repo_opts: rp.RepoOpts(.xit) = .{ .is_test = true }; 624 const ServerRepo = rp.Repo(.xit, repo_opts); 625 var server_repo = try ServerRepo.init(io, allocator, .{ .path = server_path }); 626 defer server_repo.deinit(io, allocator); 627 628 const cwd_path = try std.process.currentPathAlloc(io, allocator); 629 defer allocator.free(cwd_path); 630 631 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 632 defer allocator.free(client_path); 633 634 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 635 defer client_repo.deinit(io, allocator); 636 637 // add remote 638 { 639 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 640 defer allocator.free(remote_url); 641 642 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 643 } 644 645 const is_ssh = switch (transport_def) { 646 .file => false, 647 .wire => |wire_kind| .ssh == wire_kind, 648 }; 649 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 650 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 651 652 var arena = std.heap.ArenaAllocator.init(allocator); 653 defer arena.deinit(); 654 655 // 656 // define test events 657 // 658 659 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 660 const issue_event_id = evt.EventWithId.randomId(prng.random()); 661 662 const events_to_push = [_]evt.EventWithId{ 663 .{ 664 .id = std.fmt.bytesToHex(issue_event_id, .lower), 665 .author = .{ .name = "haxy", .email = "user@haxy" }, 666 .event = .{ 667 .issue = .{ 668 .title = "Login form clears password on validation error", 669 .description = "Submitting an invalid email address resets the password field. Preserve the field value and show an inline validation message.", 670 .tags = "bug priority-high ui", 671 }, 672 }, 673 }, 674 }; 675 676 // commit the issue on the client and push it to the server 677 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push); 678 try client_repo.push( 679 io, 680 allocator, 681 "origin", 682 "haxy/events", 683 false, 684 .{ .wire = .{ .ssh = .{ 685 .command = ssh_cmd_maybe, 686 } } }, 687 ); 688 689 // the push consumed the issue into the server repo's db 690 { 691 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 692 693 // get the map of issues 694 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 695 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 696 697 // get the issue out of the map 698 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 699 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 700 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 701 702 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.description, issue.description); 703 try std.testing.expectEqualStrings(events_to_push[0].event.issue.?.tags, issue.tags); 704 } 705 706 // 707 // edit the issue 708 // 709 710 // this event edits the previous one because it has the same id 711 const events_to_push2 = [_]evt.EventWithId{ 712 .{ 713 .id = std.fmt.bytesToHex(issue_event_id, .lower), 714 .author = .{ .name = "haxy", .email = "user@haxy" }, 715 .event = .{ 716 .issue = .{ 717 .title = "Login form clears password on validation error", 718 .description = "Submitting an invalid email address resets the password field and removes typed input. Preserve the field value and show an inline validation message.", 719 .tags = "bug priority-low ui", 720 }, 721 }, 722 }, 723 }; 724 725 // commit the edit on the client and push it to the server 726 try evt.consume(repo_kind, .{ .is_test = true }, io, allocator, &client_repo, evt.events_ref, &events_to_push2); 727 try client_repo.push( 728 io, 729 allocator, 730 "origin", 731 "haxy/events", 732 false, 733 .{ .wire = .{ .ssh = .{ 734 .command = ssh_cmd_maybe, 735 } } }, 736 ); 737 738 // the push consumed the edit into the server repo's db 739 { 740 const haxy_moment = try evt.currentMoment(repo_opts, &server_repo); 741 742 // get the map of issues 743 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "event-id->issue")) orelse return error.NotFound; 744 const event_id_to_issue = try ServerRepo.DB.HashMap(.read_only).init(event_id_to_issue_cursor); 745 746 // get the issue out of the map that was edited 747 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(repo_opts.hash, &issue_event_id)) orelse return error.NotFound; 748 const issue_map = try ServerRepo.DB.HashMap(.read_only).init(issue_cursor); 749 const issue = try evt.read(evt.Issue, ServerRepo.DB, repo_opts.hash, &arena, issue_map); 750 751 // the description and tags were correctly edited 752 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.description, issue.description); 753 try std.testing.expectEqualStrings(events_to_push2[0].event.issue.?.tags, issue.tags); 754 } 755 } 756 757 fn testClone( 758 comptime repo_kind: rp.RepoKind, 759 comptime transport_def: net.TransportDefinition, 760 comptime shell_out_to_git: bool, 761 io: std.Io, 762 allocator: std.mem.Allocator, 763 ) !void { 764 const temp_dir_name = "temp-testnet-clone"; 765 766 // create the temp dir 767 const cwd = std.Io.Dir.cwd(); 768 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 769 if (temp_dir_or_err) |*temp_dir| { 770 temp_dir.close(io); 771 try cwd.deleteTree(io, temp_dir_name); 772 } else |_| {} 773 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 774 defer cwd.deleteTree(io, temp_dir_name) catch {}; 775 defer temp_dir.close(io); 776 777 // init server 778 var server_process = try runServer(io, allocator, temp_dir_name); 779 defer _ = server_process.kill(io); 780 781 const cwd_path = try std.process.currentPathAlloc(io, allocator); 782 defer allocator.free(cwd_path); 783 784 const temp_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name }); 785 defer allocator.free(temp_path); 786 787 // register the repo under admin and locate its on-disk directory 788 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 789 defer allocator.free(server_path); 790 791 // init server repo with default branch name as main 792 // is_test must be false when shell_out_to_git so commits get real timestamps (needed for --shallow-since) 793 var server_repo = try rp.Repo(.xit, .{ .is_test = !shell_out_to_git }).init(io, allocator, .{ .path = server_path, .create_default_branch = "main" }); 794 defer server_repo.deinit(io, allocator); 795 796 if (shell_out_to_git) { 797 try server_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "test" }); 798 try server_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "test@test" }); 799 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowfilter", .value = "true" }); 800 } 801 802 // make a commit 803 { 804 const hello_txt = try server_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 805 defer hello_txt.close(io); 806 try hello_txt.writeStreamingAll(io, "hello, world!"); 807 try server_repo.add(io, allocator, &.{"hello.txt"}); 808 _ = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 809 } 810 811 // tag first commit 812 _ = try server_repo.addTag(io, allocator, .{ .name = "v1", .message = "first" }); 813 814 // make a commit 815 { 816 const goodbye_txt = try server_repo.core.work_dir.createFile(io, "goodbye.txt", .{ .truncate = true }); 817 defer goodbye_txt.close(io); 818 try goodbye_txt.writeStreamingAll(io, "goodbye, world!"); 819 try server_repo.add(io, allocator, &.{"goodbye.txt"}); 820 _ = try server_repo.commit(io, allocator, .{ .message = "add goodbye file" }); 821 } 822 823 // export server repo 824 { 825 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 826 defer export_file.close(io); 827 } 828 829 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 830 defer allocator.free(client_path); 831 832 // get remote url 833 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 834 defer allocator.free(remote_url); 835 836 const is_ssh = switch (transport_def) { 837 .file => false, 838 .wire => |wire_kind| .ssh == wire_kind, 839 }; 840 841 if (shell_out_to_git) { 842 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 843 defer allocator.free(priv_key_path); 844 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}", .{ ssh_port, priv_key_path }); 845 defer allocator.free(ssh_config_arg); 846 847 { 848 var process = try std.process.spawn(io, .{ 849 .argv = if (is_ssh) 850 &.{ "git", "-c", ssh_config_arg, "clone", "--depth", "1", remote_url, "client" } 851 else 852 &.{ "git", "clone", "--depth", "1", remote_url, "client" }, 853 .cwd = .{ .path = temp_path }, 854 .stdin = .ignore, 855 .stdout = .ignore, 856 .stderr = .ignore, 857 }); 858 const term = try process.wait(io); 859 if (term != .exited or term.exited != 0) { 860 return error.GitCommandFailed; 861 } 862 } 863 864 // make sure shallow clone was successful 865 { 866 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 867 hello_txt.close(io); 868 } 869 870 // make a third commit on the server 871 { 872 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 873 defer extra_txt.close(io); 874 try extra_txt.writeStreamingAll(io, "extra content"); 875 try server_repo.add(io, allocator, &.{"extra.txt"}); 876 _ = try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 877 } 878 879 // pull --unshallow to deepen the clone and get the new commit 880 { 881 var process = try std.process.spawn(io, .{ 882 .argv = if (is_ssh) 883 &.{ "git", "-c", ssh_config_arg, "pull", "--unshallow" } 884 else 885 &.{ "git", "pull", "--unshallow" }, 886 .cwd = .{ .path = client_path }, 887 .stdin = .ignore, 888 .stdout = .ignore, 889 .stderr = .ignore, 890 }); 891 const term = try process.wait(io); 892 if (term != .exited or term.exited != 0) { 893 return error.GitCommandFailed; 894 } 895 } 896 897 // make sure unshallow pull was successful 898 { 899 const extra_txt = try temp_dir.openFile(io, "client/extra.txt", .{}); 900 extra_txt.close(io); 901 } 902 903 // delete client and clone again with --shallow-since 904 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 905 906 { 907 var process = try std.process.spawn(io, .{ 908 .argv = if (is_ssh) 909 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-since=2000-01-01", remote_url, "client" } 910 else 911 &.{ "git", "clone", "--shallow-since=2000-01-01", remote_url, "client" }, 912 .cwd = .{ .path = temp_path }, 913 .stdin = .ignore, 914 .stdout = .ignore, 915 .stderr = .ignore, 916 }); 917 const term = try process.wait(io); 918 if (term != .exited or term.exited != 0) { 919 return error.GitCommandFailed; 920 } 921 } 922 923 // make sure shallow clone was successful 924 { 925 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 926 hello_txt.close(io); 927 } 928 929 // delete client and clone again with --shallow-exclude 930 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 931 932 { 933 var process = try std.process.spawn(io, .{ 934 .argv = if (is_ssh) 935 &.{ "git", "-c", ssh_config_arg, "clone", "--shallow-exclude=v1", remote_url, "client" } 936 else 937 &.{ "git", "clone", "--shallow-exclude=v1", remote_url, "client" }, 938 .cwd = .{ .path = temp_path }, 939 .stdin = .ignore, 940 .stdout = .ignore, 941 .stderr = .ignore, 942 }); 943 const term = try process.wait(io); 944 if (term != .exited or term.exited != 0) { 945 return error.GitCommandFailed; 946 } 947 } 948 949 // make sure shallow clone was successful 950 { 951 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 952 hello_txt.close(io); 953 } 954 955 // delete client and clone again with --filter=blob:none 956 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 957 958 { 959 var process = try std.process.spawn(io, .{ 960 .argv = if (is_ssh) 961 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=blob:none", remote_url, "client" } 962 else 963 &.{ "git", "clone", "--filter=blob:none", remote_url, "client" }, 964 .cwd = .{ .path = temp_path }, 965 .stdin = .ignore, 966 .stdout = .ignore, 967 .stderr = .ignore, 968 }); 969 const term = try process.wait(io); 970 if (term != .exited or term.exited != 0) { 971 return error.GitCommandFailed; 972 } 973 } 974 975 // make sure partial clone was successful 976 { 977 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 978 hello_txt.close(io); 979 } 980 981 // delete client and clone again with --filter=tree:0 982 try cwd.deleteTree(io, temp_dir_name ++ "/client"); 983 984 { 985 var process = try std.process.spawn(io, .{ 986 .argv = if (is_ssh) 987 &.{ "git", "-c", ssh_config_arg, "clone", "--filter=tree:0", remote_url, "client" } 988 else 989 &.{ "git", "clone", "--filter=tree:0", remote_url, "client" }, 990 .cwd = .{ .path = temp_path }, 991 .stdin = .ignore, 992 .stdout = .ignore, 993 .stderr = .ignore, 994 }); 995 const term = try process.wait(io); 996 if (term != .exited or term.exited != 0) { 997 return error.GitCommandFailed; 998 } 999 } 1000 1001 // make sure treeless clone was successful 1002 { 1003 const goodbye_txt = try temp_dir.openFile(io, "client/goodbye.txt", .{}); 1004 goodbye_txt.close(io); 1005 } 1006 } else { 1007 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1008 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1009 1010 // clone repo 1011 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).clone( 1012 io, 1013 allocator, 1014 remote_url, 1015 temp_path, 1016 client_path, 1017 null, 1018 .{ .wire = .{ .ssh = .{ 1019 .command = ssh_cmd_maybe, 1020 } } }, 1021 ); 1022 defer client_repo.deinit(io, allocator); 1023 1024 // make sure HEAD points to the right default branch 1025 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1026 const head = try client_repo.head(io, &current_branch_buffer); 1027 try std.testing.expectEqualStrings("main", head.ref.name); 1028 1029 // make sure clone was successful 1030 const hello_txt = try temp_dir.openFile(io, "client/hello.txt", .{}); 1031 defer hello_txt.close(io); 1032 } 1033 } 1034 1035 fn testFetchLarge( 1036 comptime repo_kind: rp.RepoKind, 1037 comptime transport_def: net.TransportDefinition, 1038 comptime shell_out_to_git: bool, 1039 io: std.Io, 1040 allocator: std.mem.Allocator, 1041 ) !void { 1042 const temp_dir_name = "temp-testnet-fetch-large"; 1043 1044 // create the temp dir 1045 const cwd = std.Io.Dir.cwd(); 1046 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1047 if (temp_dir_or_err) |*temp_dir| { 1048 temp_dir.close(io); 1049 try cwd.deleteTree(io, temp_dir_name); 1050 } else |_| {} 1051 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1052 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1053 defer temp_dir.close(io); 1054 1055 // init server 1056 var server_process = try runServer(io, allocator, temp_dir_name); 1057 defer _ = server_process.kill(io); 1058 1059 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1060 defer allocator.free(cwd_path); 1061 1062 // register the repo under admin and locate its on-disk directory 1063 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1064 defer allocator.free(server_path); 1065 1066 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1067 defer server_repo.deinit(io, allocator); 1068 1069 var server_dir = try cwd.openDir(io, server_path, .{}); 1070 defer server_dir.close(io); 1071 1072 // copy files from current repo into server dir 1073 for (&[_][]const u8{"src"}) |dir_name| { 1074 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1075 defer src_repo_dir.close(io); 1076 1077 var dest_repo_dir = try server_dir.createDirPathOpen(io, dir_name, .{}); 1078 defer dest_repo_dir.close(io); 1079 1080 try copyDir(io, src_repo_dir, dest_repo_dir); 1081 1082 try server_repo.add(io, allocator, &.{dir_name}); 1083 } 1084 1085 // make a commit 1086 const commit1 = try server_repo.commit(io, allocator, .{ .message = "let there be light" }); 1087 1088 // export server repo 1089 { 1090 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1091 defer export_file.close(io); 1092 } 1093 1094 if (shell_out_to_git) { 1095 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1096 } 1097 1098 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1099 defer allocator.free(client_path); 1100 1101 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1102 defer client_repo.deinit(io, allocator); 1103 1104 // add remote 1105 { 1106 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1107 defer allocator.free(remote_url); 1108 1109 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1110 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1111 } 1112 1113 const is_ssh = switch (transport_def) { 1114 .file => false, 1115 .wire => |wire_kind| .ssh == wire_kind, 1116 }; 1117 1118 if (shell_out_to_git) { 1119 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1120 defer allocator.free(priv_key_path); 1121 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}", .{ ssh_port, priv_key_path }); 1122 defer allocator.free(ssh_config_arg); 1123 1124 { 1125 var process = try std.process.spawn(io, .{ 1126 .argv = if (is_ssh) 1127 &.{ "git", "-c", ssh_config_arg, "pull", "origin", "master" } 1128 else 1129 &.{ "git", "pull", "origin", "master" }, 1130 .cwd = .{ .path = client_path }, 1131 .stdin = .ignore, 1132 .stdout = .ignore, 1133 .stderr = .ignore, 1134 }); 1135 const term = try process.wait(io); 1136 if (term != .exited or term.exited != 0) { 1137 return error.GitCommandFailed; 1138 } 1139 } 1140 1141 // make sure pull was successful 1142 { 1143 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1144 try std.testing.expectEqualStrings(&commit1, &oid_master); 1145 } 1146 1147 // make another commit on the server 1148 const commit2 = blk: { 1149 const extra_txt = try server_repo.core.work_dir.createFile(io, "extra.txt", .{ .truncate = true }); 1150 defer extra_txt.close(io); 1151 try extra_txt.writeStreamingAll(io, "extra content"); 1152 try server_repo.add(io, allocator, &.{"extra.txt"}); 1153 break :blk try server_repo.commit(io, allocator, .{ .message = "add extra file" }); 1154 }; 1155 1156 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1157 { 1158 var process = try std.process.spawn(io, .{ 1159 .argv = if (is_ssh) 1160 &.{ "git", "-c", ssh_config_arg, "fetch", "origin", "master" } 1161 else 1162 &.{ "git", "fetch", "origin", "master" }, 1163 .cwd = .{ .path = client_path }, 1164 .stdin = .ignore, 1165 .stdout = .ignore, 1166 .stderr = .ignore, 1167 }); 1168 const term = try process.wait(io); 1169 if (term != .exited or term.exited != 0) { 1170 return error.GitCommandFailed; 1171 } 1172 } 1173 1174 // make sure fetch with want-ref was successful 1175 { 1176 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1177 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1178 } 1179 } else { 1180 const refspecs = &.{ 1181 "+refs/heads/master:refs/heads/master", 1182 }; 1183 1184 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1185 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1186 1187 try client_repo.fetch( 1188 io, 1189 allocator, 1190 "origin", 1191 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1192 .command = ssh_cmd_maybe, 1193 } } }, 1194 ); 1195 1196 // update the working dir 1197 try client_repo.restore(io, allocator, "."); 1198 } 1199 1200 // make sure fetch was successful 1201 { 1202 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1203 try std.testing.expectEqualStrings(&commit1, &oid_master); 1204 } 1205 } 1206 1207 fn testPushLarge( 1208 comptime repo_kind: rp.RepoKind, 1209 comptime transport_def: net.TransportDefinition, 1210 comptime shell_out_to_git: bool, 1211 io: std.Io, 1212 allocator: std.mem.Allocator, 1213 ) !void { 1214 const temp_dir_name = "temp-testnet-push-large"; 1215 1216 // create the temp dir 1217 const cwd = std.Io.Dir.cwd(); 1218 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 1219 if (temp_dir_or_err) |*temp_dir| { 1220 temp_dir.close(io); 1221 try cwd.deleteTree(io, temp_dir_name); 1222 } else |_| {} 1223 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 1224 defer cwd.deleteTree(io, temp_dir_name) catch {}; 1225 defer temp_dir.close(io); 1226 1227 // init server 1228 var server_process = try runServer(io, allocator, temp_dir_name); 1229 defer _ = server_process.kill(io); 1230 1231 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1232 defer allocator.free(cwd_path); 1233 1234 // register the repo under admin and locate its on-disk directory 1235 const server_path = (try repoOnDiskPath(io, allocator, temp_dir_name, "testrepo", true)).?; 1236 defer allocator.free(server_path); 1237 1238 var server_repo = try rp.Repo(.xit, .{ .is_test = true }).init(io, allocator, .{ .path = server_path }); 1239 defer server_repo.deinit(io, allocator); 1240 1241 // add config 1242 try server_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1243 try server_repo.addConfig(io, allocator, .{ .name = "receive.denycurrentbranch", .value = "updateinstead" }); 1244 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1245 1246 // export server repo 1247 { 1248 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1249 defer export_file.close(io); 1250 } 1251 1252 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1253 defer allocator.free(client_path); 1254 1255 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1256 defer client_repo.deinit(io, allocator); 1257 1258 var client_dir = try cwd.openDir(io, client_path, .{}); 1259 defer client_dir.close(io); 1260 1261 { 1262 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1263 defer hello_txt.close(io); 1264 try hello_txt.writeStreamingAll(io, "hello, world!"); 1265 try client_repo.add(io, allocator, &.{"hello.txt"}); 1266 } 1267 1268 // copy files from current repo into client dir 1269 for (&[_][]const u8{"src"}) |dir_name| { 1270 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1271 defer src_repo_dir.close(io); 1272 1273 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1274 defer dest_repo_dir.close(io); 1275 1276 try copyDir(io, src_repo_dir, dest_repo_dir); 1277 1278 try client_repo.add(io, allocator, &.{dir_name}); 1279 } 1280 1281 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1282 1283 // change the files so git will send them as delta objects 1284 for (&[_][]const u8{"src"}) |dir_name| { 1285 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1286 defer dest_repo_dir.close(io); 1287 1288 { 1289 var iter = dest_repo_dir.iterate(); 1290 while (try iter.next(io)) |entry| { 1291 switch (entry.kind) { 1292 .file => { 1293 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1294 defer file.close(io); 1295 var writer = file.writer(io, &.{}); 1296 try writer.interface.writeAll("EDIT"); 1297 }, 1298 else => {}, 1299 } 1300 } 1301 } 1302 1303 try client_repo.add(io, allocator, &.{dir_name}); 1304 } 1305 1306 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1307 1308 // add remote 1309 { 1310 const remote_url = try remoteUrl(transport_def, allocator, "testrepo"); 1311 defer allocator.free(remote_url); 1312 1313 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1314 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1315 } 1316 1317 const is_ssh = switch (transport_def) { 1318 .file => false, 1319 .wire => |wire_kind| .ssh == wire_kind, 1320 }; 1321 1322 if (shell_out_to_git) { 1323 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1324 defer allocator.free(priv_key_path); 1325 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}", .{ ssh_port, priv_key_path }); 1326 defer allocator.free(ssh_config_arg); 1327 1328 // shell out to git so it will send delta objects 1329 var process = try std.process.spawn(io, .{ 1330 .argv = if (is_ssh) 1331 &.{ "git", "-c", ssh_config_arg, "push", "origin", "master" } 1332 else 1333 &.{ "git", "push", "origin", "master" }, 1334 .cwd = .{ .path = client_path }, 1335 .stdin = .ignore, 1336 .stdout = .ignore, 1337 .stderr = .ignore, 1338 }); 1339 const term = try process.wait(io); 1340 if (term != .exited or term.exited != 0) { 1341 return error.GitCommandFailed; 1342 } 1343 } else { 1344 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, cwd_path, temp_dir_name); 1345 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1346 1347 try client_repo.push( 1348 io, 1349 allocator, 1350 "origin", 1351 "master", 1352 false, 1353 .{ .wire = .{ .ssh = .{ 1354 .command = ssh_cmd_maybe, 1355 } } }, 1356 ); 1357 } 1358 1359 // make sure push was successful 1360 { 1361 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1362 try std.testing.expectEqualStrings(&commit2, &oid_master); 1363 1364 const hello_txt = try server_repo.core.work_dir.openFile(io, "hello.txt", .{}); 1365 defer hello_txt.close(io); 1366 } 1367 } 1368 1369 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1370 var iter = src_dir.iterate(); 1371 while (try iter.next(io)) |entry| { 1372 switch (entry.kind) { 1373 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1374 .directory => { 1375 try dest_dir.createDirPath(io, entry.name); 1376 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1377 defer dest_entry_dir.close(io); 1378 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1379 defer src_entry_dir.close(io); 1380 try copyDir(io, src_entry_dir, dest_entry_dir); 1381 }, 1382 else => {}, 1383 } 1384 } 1385 } 1386 1387 // the dev SSH public key matching the private key written by runServer 1388 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1389 1390 // create the admin event repo with a user holding the dev SSH public key 1391 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_dir_name: []const u8) !void { 1392 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1393 defer allocator.free(cwd_path); 1394 1395 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1396 defer allocator.free(admin_repo_path); 1397 1398 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_repo_path }); 1399 defer repo.deinit(io, allocator); 1400 1401 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1402 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1403 1404 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1405 const user_id = evt.EventWithId.randomId(prng.random()); 1406 1407 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1408 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1409 1410 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1411 .id = std.fmt.bytesToHex(user_id, .lower), 1412 .author = .{ .name = "admin", .email = "admin@example.test" }, 1413 .event = .{ .user = .{ 1414 .name = "admin", 1415 .email = "admin@example.test", 1416 .password_hash = password_hash, 1417 .ssh_keys = admin_ssh_key, 1418 } }, 1419 }}); 1420 } 1421 1422 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1423 // the event store 1424 fn repoOnDiskPath( 1425 io: std.Io, 1426 allocator: std.mem.Allocator, 1427 data_dir_name: []const u8, 1428 repo_name: []const u8, 1429 create: bool, 1430 ) !?[]u8 { 1431 const cwd_path = try std.process.currentPathAlloc(io, allocator); 1432 defer allocator.free(cwd_path); 1433 1434 const admin_repo_path = try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "admin" }); 1435 defer allocator.free(admin_repo_path); 1436 1437 const event_id_hex = (try evt.resolveOrCreateRepo(io, allocator, admin_repo_path, "admin", repo_name, create)) orelse return null; 1438 return try std.fs.path.join(allocator, &.{ cwd_path, data_dir_name, "repos", &event_id_hex }); 1439 } 1440 1441 // build the remote URL addressing admin/<repo_name> over the given transport 1442 fn remoteUrl( 1443 comptime transport_def: net.TransportDefinition, 1444 allocator: std.mem.Allocator, 1445 repo_name: []const u8, 1446 ) ![]u8 { 1447 return switch (transport_def) { 1448 .file => unreachable, 1449 .wire => |wire_kind| switch (wire_kind) { 1450 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1451 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ http_port, repo_name }), 1452 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1453 }, 1454 }; 1455 } 1456 1457 // the ssh command the haxy git client should invoke, or null when not using 1458 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1459 // and skip writing to known_hosts. 1460 fn sshCommand( 1461 is_ssh: bool, 1462 allocator: std.mem.Allocator, 1463 cwd_path: []const u8, 1464 temp_dir_name: []const u8, 1465 ) !?[]u8 { 1466 if (!is_ssh) return null; 1467 1468 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1469 defer allocator.free(priv_key_path); 1470 1471 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}\"", .{ ssh_port, priv_key_path }); 1472 } 1473