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