╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ net.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1669 .http => try std.fmt.allocPrint(allocator, "http://localhost:{}/admin/{s}", .{ port, repo_name }), 1670 .raw => try std.fmt.allocPrint(allocator, "git://localhost:{}/admin/{s}", .{ port, repo_name }), 1671 .ssh => try std.fmt.allocPrint(allocator, "git@localhost:admin/{s}", .{repo_name}), 1672 }, 1673 }; 1674 } 1675 1676 // the ssh command the haxy git client should invoke, or null when not using 1677 // ssh. haxy generates a fresh host key on every run, so accept the unknown host 1678 // and skip writing to known_hosts. 1679 fn sshCommand( 1680 is_ssh: bool, 1681 allocator: std.mem.Allocator, 1682 temp_path: []const u8, 1683 port: u16, 1684 ) !?[]u8 { 1685 if (!is_ssh) return null; 1686 1687 const priv_key_path = try std.fs.path.join(allocator, &.{ temp_path, "key" }); 1688 defer allocator.free(priv_key_path); 1689 1690 if (builtin.os.tag == .windows) std.mem.replaceScalar(u8, priv_key_path, '\\', '/'); 1691 const null_path = if (builtin.os.tag == .windows) "NUL" else "/dev/null"; 1692 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 }); 1693 } 1694 1695 const ServerFiles = union(enum) { 1696 files: []const struct { path: []const u8, content: []const u8 }, 1697 dirs: []const []const u8, 1698 }; 1699 1700 /// commit files without writing the server's index or worktree. 1701 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 { 1702 const R = @TypeOf(repo.*); 1703 const opts = repo.self_repo_opts; 1704 const Index = xit.index.Index(.xit, opts); 1705 var result: [xit.hash.hexLen(opts.hash)]u8 = undefined; 1706 const Ctx = struct { 1707 core: *R.Core, 1708 io: std.Io, 1709 allocator: std.mem.Allocator, 1710 files: ServerFiles, 1711 metadata: xit.object.CommitMetadata(opts.hash), 1712 result: *[xit.hash.hexLen(opts.hash)]u8, 1713 1714 fn addFile(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8, content: []const u8) !void { 1715 var reader = std.Io.Reader.fixed(content); 1716 var oid: [xit.hash.byteLen(opts.hash)]u8 = undefined; 1717 try xit.object.writeObject(.xit, opts, state, ctx.io, ctx.allocator, &reader, .{ .kind = .blob, .size = content.len }, &oid); 1718 const path_parts = try xit.fs.splitPath(ctx.allocator, path); 1719 defer ctx.allocator.free(path_parts); 1720 try index.addTreeEntryFile(&.{ .oid = oid, .mode = @bitCast(@as(u32, 0o100644)) }, path_parts, content.len, 0); 1721 } 1722 1723 fn addDir(ctx: @This(), state: R.State(.read_write), index: *Index, path: []const u8) anyerror!void { 1724 var dir = try std.Io.Dir.cwd().openDir(ctx.io, path, .{ .iterate = true }); 1725 defer dir.close(ctx.io); 1726 var entries = dir.iterate(); 1727 while (try entries.next(ctx.io)) |entry| { 1728 const child = try std.fs.path.join(ctx.allocator, &.{ path, entry.name }); 1729 defer ctx.allocator.free(child); 1730 switch (entry.kind) { 1731 .directory => try ctx.addDir(state, index, child), 1732 .file => { 1733 const content = try dir.readFileAlloc(ctx.io, entry.name, ctx.allocator, .unlimited); 1734 defer ctx.allocator.free(content); 1735 try ctx.addFile(state, index, child, content); 1736 }, 1737 else => {}, 1738 } 1739 } 1740 } 1741 1742 pub fn run(ctx: @This(), cursor: *R.DB.Cursor(.read_write)) !void { 1743 var moment = try R.DB.HashMap(.read_write).init(cursor.*); 1744 const state = R.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1745 const head_oid = try rf.readHeadRecurMaybe(.xit, opts, state.readOnly(), ctx.io); 1746 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); 1747 defer index.deinit(); 1748 switch (ctx.files) { 1749 .files => |updates| for (updates) |file| try ctx.addFile(state, &index, file.path, file.content), 1750 .dirs => |dirs| for (dirs) |dir| try ctx.addDir(state, &index, dir), 1751 } 1752 var tree = try xit.object.Tree.initFromIndex(.xit, opts, state, ctx.io, ctx.allocator, &index); 1753 defer tree.deinit(); 1754 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1755 const head = try rf.readHead(.xit, opts, state.readOnly(), ctx.io, &head_buffer) orelse return error.HeadNotFound; 1756 ctx.result.* = try xit.object.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, ctx.metadata, &tree, head.ref); 1757 try xit.undo.writeMessage(opts, state, .{ .commit = ctx.metadata }); 1758 } 1759 }; 1760 try repo.core.db_file.lock(io, .exclusive); 1761 defer repo.core.db_file.unlock(io); 1762 const history = try R.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1763 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1764 .core = &repo.core, 1765 .io = io, 1766 .allocator = allocator, 1767 .files = files, 1768 .metadata = metadata, 1769 .result = &result, 1770 }); 1771 return result; 1772 } 1773