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