╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1185 .stdout = .ignore, 1186 .stderr = .inherit, 1187 }); 1188 defer process.kill(io); 1189 const term = try process.wait(io); 1190 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1191 } 1192 1193 // make sure shallow clone was successful 1194 { 1195 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1196 hello_txt.close(io); 1197 } 1198 1199 // delete client and clone again with --shallow-exclude 1200 try temp.dir.deleteTree(io, "client"); 1201 1202 { 1203 var process = try std.process.spawn(io, .{ 1204 .argv = if (is_ssh) 1205 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" } 1206 else 1207 &.{ "git", "clone", "--quiet", "--shallow-exclude=v1", remote_url, "client" }, 1208 .cwd = .{ .path = temp_path }, 1209 .stdin = .ignore, 1210 .stdout = .ignore, 1211 .stderr = .inherit, 1212 }); 1213 defer process.kill(io); 1214 const term = try process.wait(io); 1215 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1216 } 1217 1218 // make sure shallow clone was successful 1219 { 1220 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1221 hello_txt.close(io); 1222 } 1223 1224 // delete client and clone again with --filter=blob:none 1225 try temp.dir.deleteTree(io, "client"); 1226 1227 { 1228 var process = try std.process.spawn(io, .{ 1229 .argv = if (is_ssh) 1230 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=blob:none", remote_url, "client" } 1231 else 1232 &.{ "git", "clone", "--quiet", "--filter=blob:none", remote_url, "client" }, 1233 .cwd = .{ .path = temp_path }, 1234 .stdin = .ignore, 1235 .stdout = .ignore, 1236 .stderr = .inherit, 1237 }); 1238 defer process.kill(io); 1239 const term = try process.wait(io); 1240 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1241 } 1242 1243 // make sure partial clone was successful 1244 { 1245 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1246 hello_txt.close(io); 1247 } 1248 1249 // delete client and clone again with --filter=tree:0 1250 try temp.dir.deleteTree(io, "client"); 1251 1252 { 1253 var process = try std.process.spawn(io, .{ 1254 .argv = if (is_ssh) 1255 &.{ "git", "-c", ssh_config_arg, "clone", "--quiet", "--filter=tree:0", remote_url, "client" } 1256 else 1257 &.{ "git", "clone", "--quiet", "--filter=tree:0", remote_url, "client" }, 1258 .cwd = .{ .path = temp_path }, 1259 .stdin = .ignore, 1260 .stdout = .ignore, 1261 .stderr = .inherit, 1262 }); 1263 defer process.kill(io); 1264 const term = try process.wait(io); 1265 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1266 } 1267 1268 // make sure treeless clone was successful 1269 { 1270 const goodbye_txt = try temp.dir.openFile(io, "client/goodbye.txt", .{}); 1271 goodbye_txt.close(io); 1272 } 1273 } else { 1274 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1275 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1276 1277 // clone repo 1278 var clone_result = try rp.AnyRepo(repo_kind, .{ .is_test = true }).clone( 1279 io, 1280 allocator, 1281 remote_url, 1282 temp_path, 1283 client_path, 1284 null, 1285 .{ .transport = .{ .wire = .{ .ssh = .{ 1286 .command = ssh_cmd_maybe, 1287 } } } }, 1288 ); 1289 defer clone_result.deinit(io, allocator); 1290 const client_repo = &@field(clone_result, @tagName(hash_kind)); 1291 1292 // make sure HEAD points to the right default branch 1293 var current_branch_buffer = [_]u8{0} ** rf.MAX_REF_CONTENT_SIZE; 1294 const head = try client_repo.head(io, &current_branch_buffer); 1295 try std.testing.expectEqualStrings("main", head.ref.name); 1296 1297 // make sure clone was successful 1298 const hello_txt = try temp.dir.openFile(io, "client/hello.txt", .{}); 1299 defer hello_txt.close(io); 1300 } 1301 } 1302 1303 fn testFetchLarge( 1304 comptime repo_kind: rp.RepoKind, 1305 comptime transport_def: net.TransportDefinition, 1306 comptime shell_out_to_git: bool, 1307 comptime hash_kind: hash.HashKind, 1308 comptime port: u16, 1309 io: std.Io, 1310 allocator: std.mem.Allocator, 1311 ) !void { 1312 // create the temp dir 1313 var temp = std.testing.tmpDir(.{}); 1314 defer temp.cleanup(); 1315 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1316 defer allocator.free(temp_path); 1317 1318 // init server 1319 var server_process = try runServer(io, allocator, temp_path, port); 1320 defer _ = server_process.kill(io); 1321 1322 // register the repo under admin and locate its on-disk directory 1323 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1324 defer allocator.free(server_path); 1325 1326 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1327 defer server_repo.deinit(io, allocator); 1328 1329 // make a commit with files from the current repo 1330 const commit1 = try commitServer(&server_repo, io, allocator, .{ .dirs = &.{"src"} }, .{ .message = "let there be light" }); 1331 1332 // export server repo 1333 { 1334 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1335 defer export_file.close(io); 1336 } 1337 1338 if (shell_out_to_git) { 1339 try server_repo.addConfig(io, allocator, .{ .name = "uploadpack.allowrefinwant", .value = "true" }); 1340 } 1341 1342 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1343 defer allocator.free(client_path); 1344 1345 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1346 defer client_repo.deinit(io, allocator); 1347 1348 // add remote 1349 { 1350 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1351 defer allocator.free(remote_url); 1352 1353 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1354 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1355 } 1356 1357 const is_ssh = switch (transport_def) { 1358 .file => false, 1359 .wire => |wire_kind| .ssh == wire_kind, 1360 }; 1361 1362 if (shell_out_to_git) { 1363 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1364 defer allocator.free(ssh_cmd); 1365 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1366 defer allocator.free(ssh_config_arg); 1367 1368 { 1369 var process = try std.process.spawn(io, .{ 1370 .argv = if (is_ssh) 1371 &.{ "git", "-c", ssh_config_arg, "pull", "--quiet", "origin", "master" } 1372 else 1373 &.{ "git", "pull", "--quiet", "origin", "master" }, 1374 .cwd = .{ .path = client_path }, 1375 .stdin = .ignore, 1376 .stdout = .ignore, 1377 .stderr = .inherit, 1378 }); 1379 defer process.kill(io); 1380 const term = try process.wait(io); 1381 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1382 } 1383 1384 // make sure pull was successful 1385 { 1386 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1387 try std.testing.expectEqualStrings(&commit1, &oid_master); 1388 } 1389 1390 // make another commit on the server 1391 const commit2 = try commitServer(&server_repo, io, allocator, .{ .files = &.{.{ .path = "extra.txt", .content = "extra content" }} }, .{ .message = "add extra file" }); 1392 1393 // fetch with ref-in-want (git uses want-ref in protocol v2 when fetching named refs) 1394 { 1395 var process = try std.process.spawn(io, .{ 1396 .argv = if (is_ssh) 1397 &.{ "git", "-c", ssh_config_arg, "fetch", "--quiet", "origin", "master" } 1398 else 1399 &.{ "git", "fetch", "--quiet", "origin", "master" }, 1400 .cwd = .{ .path = client_path }, 1401 .stdin = .ignore, 1402 .stdout = .ignore, 1403 .stderr = .inherit, 1404 }); 1405 defer process.kill(io); 1406 const term = try process.wait(io); 1407 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1408 } 1409 1410 // make sure fetch with want-ref was successful 1411 { 1412 const oid_remote_master = (try client_repo.readRef(io, .{ .kind = .{ .remote = "origin" }, .name = "master" })).?; 1413 try std.testing.expectEqualStrings(&commit2, &oid_remote_master); 1414 } 1415 } else { 1416 const refspecs = &.{ 1417 "+refs/heads/master:refs/heads/master", 1418 }; 1419 1420 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1421 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1422 1423 try client_repo.fetch( 1424 io, 1425 allocator, 1426 "origin", 1427 .{ .refspecs = refspecs, .wire = .{ .ssh = .{ 1428 .command = ssh_cmd_maybe, 1429 } } }, 1430 ); 1431 1432 // update the working dir 1433 try client_repo.restore(io, allocator, "."); 1434 } 1435 1436 // make sure fetch was successful 1437 { 1438 const oid_master = (try client_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1439 try std.testing.expectEqualStrings(&commit1, &oid_master); 1440 } 1441 } 1442 1443 fn testPushLarge( 1444 comptime repo_kind: rp.RepoKind, 1445 comptime transport_def: net.TransportDefinition, 1446 comptime shell_out_to_git: bool, 1447 comptime hash_kind: hash.HashKind, 1448 comptime port: u16, 1449 io: std.Io, 1450 allocator: std.mem.Allocator, 1451 ) !void { 1452 // create the temp dir 1453 var temp = std.testing.tmpDir(.{}); 1454 defer temp.cleanup(); 1455 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 1456 defer allocator.free(temp_path); 1457 1458 // init server 1459 var server_process = try runServer(io, allocator, temp_path, port); 1460 defer _ = server_process.kill(io); 1461 1462 // register the repo under admin and locate its on-disk directory 1463 const server_path = (try repoOnDiskPath(io, allocator, temp_path, "testrepo", true)).?; 1464 defer allocator.free(server_path); 1465 1466 var server_repo = try rp.Repo(.xit, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .bare = true, .path = server_path }); 1467 defer server_repo.deinit(io, allocator); 1468 1469 // add config 1470 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1471 1472 // export server repo 1473 { 1474 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1475 defer export_file.close(io); 1476 } 1477 1478 const client_path = try std.fs.path.join(allocator, &.{ temp_path, "client" }); 1479 defer allocator.free(client_path); 1480 1481 var client_repo = try rp.Repo(repo_kind, .{ .hash = hash_kind, .is_test = true }).init(io, allocator, .{ .path = client_path }); 1482 defer client_repo.deinit(io, allocator); 1483 1484 var client_dir = try temp.dir.openDir(io, "client", .{}); 1485 defer client_dir.close(io); 1486 1487 { 1488 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1489 defer hello_txt.close(io); 1490 try hello_txt.writeStreamingAll(io, "hello, world!"); 1491 try client_repo.add(io, allocator, &.{"hello.txt"}); 1492 } 1493 1494 // copy files from current repo into client dir 1495 const cwd = std.Io.Dir.cwd(); 1496 for (&[_][]const u8{"src"}) |dir_name| { 1497 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1498 defer src_repo_dir.close(io); 1499 1500 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1501 defer dest_repo_dir.close(io); 1502 1503 try copyDir(io, src_repo_dir, dest_repo_dir); 1504 1505 try client_repo.add(io, allocator, &.{dir_name}); 1506 } 1507 1508 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1509 1510 // change the files so git will send them as delta objects 1511 for (&[_][]const u8{"src"}) |dir_name| { 1512 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1513 defer dest_repo_dir.close(io); 1514 1515 { 1516 var iter = dest_repo_dir.iterate(); 1517 while (try iter.next(io)) |entry| { 1518 switch (entry.kind) { 1519 .file => { 1520 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1521 defer file.close(io); 1522 var writer = file.writer(io, &.{}); 1523 try writer.interface.writeAll("EDIT"); 1524 }, 1525 else => {}, 1526 } 1527 } 1528 } 1529 1530 try client_repo.add(io, allocator, &.{dir_name}); 1531 } 1532 1533 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1534 1535 // add remote 1536 { 1537 const remote_url = try remoteUrl(transport_def, allocator, "testrepo", port); 1538 defer allocator.free(remote_url); 1539 1540 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1541 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1542 } 1543 1544 const is_ssh = switch (transport_def) { 1545 .file => false, 1546 .wire => |wire_kind| .ssh == wire_kind, 1547 }; 1548 1549 if (shell_out_to_git) { 1550 const ssh_cmd = (try sshCommand(true, allocator, temp_path, port)).?; 1551 defer allocator.free(ssh_cmd); 1552 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand={s}", .{ssh_cmd}); 1553 defer allocator.free(ssh_config_arg); 1554 1555 // shell out to git so it will send delta objects 1556 var process = try std.process.spawn(io, .{ 1557 .argv = if (is_ssh) 1558 &.{ "git", "-c", ssh_config_arg, "push", "--quiet", "origin", "master" } 1559 else 1560 &.{ "git", "push", "--quiet", "origin", "master" }, 1561 .cwd = .{ .path = client_path }, 1562 .stdin = .ignore, 1563 .stdout = .ignore, 1564 .stderr = .inherit, 1565 }); 1566 defer process.kill(io); 1567 const term = try process.wait(io); 1568 if (term != .exited or term.exited != 0) return error.GitCommandFailed; 1569 } else { 1570 const ssh_cmd_maybe = try sshCommand(is_ssh, allocator, temp_path, port); 1571 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1572 1573 try client_repo.push( 1574 io, 1575 allocator, 1576 "origin", 1577 "master", 1578 false, 1579 .{ .wire = .{ .ssh = .{ 1580 .command = ssh_cmd_maybe, 1581 } } }, 1582 ); 1583 } 1584 1585 // make sure push was successful 1586 { 1587 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1588 try std.testing.expectEqualStrings(&commit2, &oid_master); 1589 1590 try std.testing.expectError(error.FileNotFound, server_repo.core.work_dir.openFile(io, "hello.txt", .{})); 1591 } 1592 } 1593 1594 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1595 var iter = src_dir.iterate(); 1596 while (try iter.next(io)) |entry| { 1597 switch (entry.kind) { 1598 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1599 .directory => { 1600 try dest_dir.createDirPath(io, entry.name); 1601 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1602 defer dest_entry_dir.close(io); 1603 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1604 defer src_entry_dir.close(io); 1605 try copyDir(io, src_entry_dir, dest_entry_dir); 1606 }, 1607 else => {}, 1608 } 1609 } 1610 } 1611 1612 // the dev SSH public key matching the private key written by runServer 1613 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 1614 1615 // create the admin event repo with a user holding the dev SSH public key 1616 fn setupAdmin(io: std.Io, allocator: std.mem.Allocator, data_path: []const u8) !void { 1617 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1618 defer allocator.free(admin_repo_path); 1619 1620 var repo = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .bare = true, .path = admin_repo_path }); 1621 defer repo.deinit(io, allocator); 1622 1623 try repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 1624 try repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@haxy" }); 1625 1626 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 1627 const user_id = evt.EventWithId.randomId(prng.random()); 1628 1629 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 1630 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 1631 1632 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &[_]evt.EventWithId{.{ 1633 .id = std.fmt.bytesToHex(user_id, .lower), 1634 .author = .{ .name = "admin", .email = "admin@example.test" }, 1635 .event = .{ .user = .{ 1636 .name = "admin", 1637 .email = "admin@example.test", 1638 .password_hash = password_hash, 1639 .ssh_keys = admin_ssh_key, 1640 } }, 1641 }}); 1642 } 1643 1644 // resolve admin/<repo_name> to its on-disk directory under <data_dir>/repos via 1645 // the event store 1646 fn repoOnDiskPath( 1647 io: std.Io, 1648 allocator: std.mem.Allocator, 1649 data_path: []const u8, 1650 repo_name: []const u8, 1651 create: bool, 1652 ) !?[]u8 { 1653 const admin_repo_path = try std.fs.path.join(allocator, &.{ data_path, "admin" }); 1654 defer allocator.free(admin_repo_path); 1655 1656 const event_id_hex = (try evt.resolveOrCreateRepo( 1657 io, 1658 allocator, 1659 admin_repo_path, 1660 "admin", 1661 repo_name, 1662 if (create) .{ .read_access = .public } else null, 1663 )) orelse return null; 1664 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1665 } 1666 1667 // build the remote URL addressing admin/<repo_name> over the given transport 1668 fn remoteUrl( 1669 comptime transport_def: net.TransportDefinition, 1670 allocator: std.mem.Allocator, 1671 repo_name: []const u8, 1672 port: u16, 1673 ) ![]u8 { 1674 return switch (transport_def) { 1675 .file => unreachable, 1676 .wire => |wire_kind| switch (wire_kind) { 1677 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1678 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1679 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1680 }, 1681 }; 1682 } 1683 1684 // the ssh command the haxy git client should invoke, or null when not using 1685 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1686 // and skip writing to known_hosts. 1687 fn sshCommand( 1688 is_ssh: bool, 1689 allocator: std.mem.Allocator, 1690 temp_path: []const u8, 1691 port: u16, 1692 ) !?[]u8 { 1693 if (!is_ssh) return null; 1694 1695 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1696 defer allocator.free(priv_key_path); 1697 1698 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1699 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1700 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 }); 1701 } 1702 1703 const ServerFiles = union(enum) { 1704 files: []const struct { path: []const u8, content: []const u8 }, 1705 dirs: []const []const u8, 1706 }; 1707 1708 /// commit files without writing the server's index or worktree. 1709 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 { 1710 const R = @TypeOf(repo.*); 1711 const opts = repo.self_repo_opts; 1712 const Index = xit.index.Index(.xit, opts); 1713 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1714 const Ctx = struct { 1715 core: *R.Core, 1716 io: std.Io, 1717 allocator: std.mem.Allocator, 1718 files: ServerFiles, 1719 metadata: xit.object.CommitMetadata(opts.hash), 1720 result: *[xit.hash.hexLen(opts.hash)]u8, 1721 1722 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1723 var reader = std.Io.Reader.fixed(content); 1724 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1725 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1726 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1727 defer ctx.allocator.free(path_parts); 1728 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1729 } 1730 1731 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1732 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1733 defer dir.close(ctx.io); 1734 var entries = dir.iterate(); 1735 while (try entries.next(ctx.io)) |entry| { 1736 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1737 defer ctx.allocator.free(child); 1738 switch (entry.kind) { 1739 .directory => try ctx.addDir(state, index, child), 1740 .file => { 1741 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1742 defer ctx.allocator.free(content); 1743 try ctx.addFile(state, index, child, content); 1744 }, 1745 else => {}, 1746 } 1747 } 1748 } 1749 1750 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1751 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1752 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1753 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1754 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); 1755 defer index.deinit(); 1756 switch (ctx.files) { 1757 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1758 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1759 } 1760 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1761 defer tree.deinit(); 1762 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1763 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1764 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1765 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1766 } 1767 }; 1768 try repo.core.db_file.lock(io, .exclusive); 1769 defer repo.core.db_file.unlock(io); 1770 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1771 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1772 .core = &repo.core, 1773 .io = io, 1774 .allocator = allocator, 1775 .files = files, 1776 .metadata = metadata, 1777 .result = &result, 1778 }); 1779 return result; 1780 } 1781