╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1454 }, 1455 } 1456 try server_repo.addConfig(io, allocator, .{ .name = "http.receivepack", .value = "true" }); 1457 1458 // export server repo 1459 { 1460 const export_file = try server_repo.core.repo_dir.createFile(io, "git-daemon-export-ok", .{}); 1461 defer export_file.close(io); 1462 } 1463 1464 const client_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "client" }); 1465 defer allocator.free(client_path); 1466 1467 var client_repo = try rp.Repo(repo_kind, .{ .is_test = true }).init(io, allocator, .{ .path = client_path }); 1468 defer client_repo.deinit(io, allocator); 1469 1470 var client_dir = try cwd.openDir(io, client_path, .{}); 1471 defer client_dir.close(io); 1472 1473 { 1474 const hello_txt = try client_repo.core.work_dir.createFile(io, "hello.txt", .{ .truncate = true }); 1475 defer hello_txt.close(io); 1476 try hello_txt.writeStreamingAll(io, "hello, world!"); 1477 try client_repo.add(io, allocator, &.{"hello.txt"}); 1478 } 1479 1480 // copy files from current repo into client dir 1481 for (&[_][]const u8{"src"}) |dir_name| { 1482 var src_repo_dir = try cwd.openDir(io, dir_name, .{ .iterate = true }); 1483 defer src_repo_dir.close(io); 1484 1485 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{}); 1486 defer dest_repo_dir.close(io); 1487 1488 try copyDir(io, src_repo_dir, dest_repo_dir); 1489 1490 try client_repo.add(io, allocator, &.{dir_name}); 1491 } 1492 1493 _ = try client_repo.commit(io, allocator, .{ .message = "let there be light" }); 1494 1495 // change the files so git will send them as delta objects 1496 for (&[_][]const u8{"src"}) |dir_name| { 1497 var dest_repo_dir = try client_dir.createDirPathOpen(io, dir_name, .{ .open_options = .{ .iterate = true } }); 1498 defer dest_repo_dir.close(io); 1499 1500 { 1501 var iter = dest_repo_dir.iterate(); 1502 while (try iter.next(io)) |entry| { 1503 switch (entry.kind) { 1504 .file => { 1505 const file = try dest_repo_dir.openFile(io, entry.name, .{ .mode = .read_write }); 1506 defer file.close(io); 1507 var writer = file.writer(io, &.{}); 1508 try writer.interface.writeAll("EDIT"); 1509 }, 1510 else => {}, 1511 } 1512 } 1513 } 1514 1515 try client_repo.add(io, allocator, &.{dir_name}); 1516 } 1517 1518 const commit2 = try client_repo.commit(io, allocator, .{ .message = "more stuff" }); 1519 1520 // add remote 1521 { 1522 if (.windows == builtin.os.tag) { 1523 std.mem.replaceScalar(u8, server_path, '\\', '/'); 1524 } 1525 const separator = if (server_path[0] == '/') "" else "/"; 1526 1527 const remote_url = switch (transport_def) { 1528 //.file => try std.fmt.allocPrint(allocator, "file://{s}{s}", .{ separator, server_path }), 1529 .file => try std.fmt.allocPrint(allocator, "../server", .{}), // relative file paths work too 1530 .wire => |wire_kind| switch (wire_kind) { 1531 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/server", .{port}), 1532 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/server", .{port}), 1533 .ssh => try std.fmt.allocPrint(allocator, "ssh://localhost:{}{s}{s}", .{ port, separator, server_path }), 1534 }, 1535 }; 1536 defer allocator.free(remote_url); 1537 1538 try client_repo.addRemote(io, allocator, .{ .name = "origin", .value = remote_url }); 1539 try client_repo.addConfig(io, allocator, .{ .name = "branch.master.remote", .value = "origin" }); 1540 } 1541 1542 const is_ssh = switch (transport_def) { 1543 .file => false, 1544 .wire => |wire_kind| .ssh == wire_kind, 1545 }; 1546 1547 const receive_pack_command = try receivePackCommand(is_ssh, allocator, cwd_path, port); 1548 defer allocator.free(receive_pack_command); 1549 1550 if (shell_out_to_git) { 1551 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1552 defer allocator.free(priv_key_path); 1553 const ssh_config_arg = try std.fmt.allocPrint(allocator, "core.sshCommand=ssh -o StrictHostKeyChecking=no -o IdentityFile={s}", .{priv_key_path}); 1554 defer allocator.free(ssh_config_arg); 1555 1556 // shell out to git so it will send delta objects 1557 var process = try std.process.spawn(io, .{ 1558 .argv = if (is_ssh) 1559 &.{ "git", "-c", ssh_config_arg, "push", "--receive-pack", receive_pack_command, "origin", "master" } 1560 else 1561 &.{ "git", "push", "origin", "master" }, 1562 .cwd = .{ .path = client_path }, 1563 .stdin = .ignore, 1564 .stdout = .ignore, 1565 .stderr = .ignore, 1566 }); 1567 const term = try process.wait(io); 1568 if (term != .exited) { 1569 return error.GitCommandFailed; 1570 } 1571 } else { 1572 const ssh_cmd_maybe: ?[]const u8 = if (is_ssh) blk: { 1573 const known_hosts_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "known_hosts" }); 1574 defer allocator.free(known_hosts_path); 1575 1576 const priv_key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 1577 defer allocator.free(priv_key_path); 1578 1579 break :blk try std.fmt.allocPrint(allocator, "ssh -o UserKnownHostsFile=\"{s}\" -o IdentityFile=\"{s}\"", .{ known_hosts_path, priv_key_path }); 1580 } else null; 1581 defer if (ssh_cmd_maybe) |ssh_cmd| allocator.free(ssh_cmd); 1582 1583 try client_repo.push( 1584 io, 1585 allocator, 1586 "origin", 1587 "master", 1588 false, 1589 .{ .wire = .{ .ssh = .{ 1590 .command = ssh_cmd_maybe, 1591 .receive_pack_command = receive_pack_command, 1592 } } }, 1593 ); 1594 1595 if (transport_def == .file) { 1596 // update the working dir 1597 try server_repo.restore(io, allocator, "."); 1598 } 1599 } 1600 1601 // make sure push was successful 1602 { 1603 const oid_master = (try server_repo.readRef(io, .{ .kind = .head, .name = "master" })).?; 1604 try std.testing.expectEqualStrings(&commit2, &oid_master); 1605 1606 const hello_txt = try temp_dir.openFile(io, "repos/server/hello.txt", .{}); 1607 defer hello_txt.close(io); 1608 } 1609 } 1610 1611 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1612 var iter = src_dir.iterate(); 1613 while (try iter.next(io)) |entry| { 1614 switch (entry.kind) { 1615 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1616 .directory => { 1617 try dest_dir.createDirPath(io, entry.name); 1618 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1619 defer dest_entry_dir.close(io); 1620 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1621 defer src_entry_dir.close(io); 1622 try copyDir(io, src_entry_dir, dest_entry_dir); 1623 }, 1624 else => {}, 1625 } 1626 } 1627 } 1628