╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1628 "admin", 1629 repo_name, 1630 if (create) .{ .read_access = .public } else null, 1631 )) orelse return null; 1632 return try std.fs.path.join(allocator, &.{ data_path, "repos", &event_id_hex }); 1633 } 1634 1635 // build the remote URL addressing admin/<repo_name> over the given transport 1636 fn remoteUrl( 1637 comptime transport_def: net.TransportDefinition, 1638 allocator: std.mem.Allocator, 1639 repo_name: []const u8, 1640 port: u16, 1641 ) ![]u8 { 1642 return switch (transport_def) { 1643 .file => unreachable, 1644 .wire => |wire_kind| switch (wire_kind) { 1645 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1646 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1647 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1648 }, 1649 }; 1650 } 1651 1652 // the ssh command the haxy git client should invoke, or null when not using 1653 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1654 // and skip writing to known_hosts. 1655 fn sshCommand( 1656 is_ssh: bool, 1657 allocator: std.mem.Allocator, 1658 temp_path: []const u8, 1659 port: u16, 1660 ) !?[]u8 { 1661 if (!is_ssh) return null; 1662 1663 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1664 defer allocator.free(priv_key_path); 1665 1666 return try std.fmt.allocPrint(allocator, "ssh -p {} -o BatchMode=yes -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o LogLevel=ERROR -o IdentitiesOnly=yes -o IdentityFile=\"{s}\"", .{ port + 1, priv_key_path }); 1667 } 1668 1669 const ServerFiles = union(enum) { 1670 files: []const struct { path: []const u8, content: []const u8 }, 1671 dirs: []const []const u8, 1672 }; 1673 1674 /// commit files without writing the server's index or worktree. 1675 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 { 1676 const R = @TypeOf(repo.*); 1677 const opts = repo.self_repo_opts; 1678 const Index = xit.index.Index(.xit, opts); 1679 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1680 const Ctx = struct { 1681 core: *R.Core, 1682 io: std.Io, 1683 allocator: std.mem.Allocator, 1684 files: ServerFiles, 1685 metadata: xit.object.CommitMetadata(opts.hash), 1686 result: *[xit.hash.hexLen(opts.hash)]u8, 1687 1688 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1689 var reader = std.Io.Reader.fixed(content); 1690 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1691 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1692 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1693 defer ctx.allocator.free(path_parts); 1694 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1695 } 1696 1697 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1698 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1699 defer dir.close(ctx.io); 1700 var entries = dir.iterate(); 1701 while (try entries.next(ctx.io)) |entry| { 1702 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1703 defer ctx.allocator.free(child); 1704 switch (entry.kind) { 1705 .directory => try ctx.addDir(state, index, child), 1706 .file => { 1707 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1708 defer ctx.allocator.free(content); 1709 try ctx.addFile(state, index, child, content); 1710 }, 1711 else => {}, 1712 } 1713 } 1714 } 1715 1716 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1717 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1718 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1719 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1720 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); 1721 defer index.deinit(); 1722 switch (ctx.files) { 1723 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1724 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1725 } 1726 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1727 defer tree.deinit(); 1728 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1729 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1730 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1731 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1732 } 1733 }; 1734 try repo.core.db_file.lock(io, .exclusive); 1735 defer repo.core.db_file.unlock(io); 1736 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1737 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1738 .core = &repo.core, 1739 .io = io, 1740 .allocator = allocator, 1741 .files = files, 1742 .metadata = metadata, 1743 .result = &result, 1744 }); 1745 return result; 1746 } 1747