╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
795 .event = .{ .comment = .{ 796 .thread_id = discussion_events[discussion_index].id, 797 .parent_id = if (comment_index < 2) 798 discussion_events[discussion_index].id 799 else 800 std.fmt.bytesToHex(discussion_comment_ids[discussion_index][0], .lower), 801 .body = switch (comment_index) { 802 0 => "My preference is to start with the smallest useful version and expand it after we have real usage to learn from.", 803 1 => "It would help to write down the tradeoffs we are accepting so the decision is easy to revisit later.", 804 2 => "Agreed. A narrow first version should also make it easier to keep the implementation understandable.", 805 else => unreachable, 806 }, 807 } }, 808 }; 809 } 810 try evt.consume(.repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &discussion_comment_events); 811 } 812 813 // copy the template to each repo's on-disk location, named by its 814 // hex-encoded event id. the template repo is deinitialized above, so its 815 // db file is fully written before we copy it. 816 { 817 var template_dir = try cwd.openDir(io, template_path, .{ .iterate = true }); 818 defer template_dir.close(io); 819 820 for (repo_event_ids) |id_bytes| { 821 const repo_id = std.fmt.bytesToHex(id_bytes, .lower); 822 const repo_path = try std.fs.path.join(arena.allocator(), &.{ server_path, "repos", &repo_id }); 823 var dest_dir = try cwd.createDirPathOpen(io, repo_path, .{}); 824 defer dest_dir.close(io); 825 try copyDir(io, template_dir, dest_dir); 826 } 827 } 828 829 break :blk try ui.Session.init(&session_arena, &repo, .{ .user_id = &admin_user_id }); 830 }; 831 session.is_terminal = true; 832 833 // start the server 834 835 // let the native TUI's page builders open the on-disk repos for the file tree 836 session.io = io; 837 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 838 839 // leave a one-shot session for the first browser to hit the web ui, so it 840 // starts logged in as admin 841 { 842 var data_dir = try cwd.createDirPathOpen(io, server_path, .{}); 843 defer data_dir.close(io); 844 const store = try hx.web.SessionStore.init(io, data_dir); 845 defer store.deinit(); 846 const token = try store.create(&admin_user_id); 847 try store.offerAutoLogin(&token); 848 } 849 850 if (cli) { 851 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 852 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 853 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 854 855 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 856 defer allocator.free(key_path); 857 858 const Runnable = struct { 859 io: std.Io, 860 key_path: []const u8, 861 862 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port_maybe: ?u16) !void { 863 _ = web_port; 864 _ = http_port; 865 const ssh_port = ssh_port_maybe orelse return error.SshDisabled; 866 std.debug.print( 867 \\ 868 \\connect to the TUI with: 869 \\ ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 870 \\ 871 \\create a git repo and push over SSH: 872 \\ mkdir -p temp-try/client/test 873 \\ cd temp-try/client/test 874 \\ git init 875 \\ echo "hello" > hello.txt 876 \\ git add hello.txt 877 \\ git commit -m "let there be light" 878 \\ GIT_SSH_COMMAND='ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o IdentitiesOnly=yes -i {s}' git push localhost:admin/test HEAD:master 879 \\ 880 \\to quit, press enter. 881 \\ 882 , .{ ssh_port, ssh_port, self.key_path }); 883 // portable stdin read via std.Io — equivalent to the 884 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 885 // on windows. 886 var buf: [1]u8 = undefined; 887 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 888 _ = stdin_reader.interface.takeByte() catch {}; 889 } 890 }; 891 892 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 893 } else { 894 var null_writer = std.Io.Writer.Discarding.init(&.{}); 895 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 896 897 const Runnable = struct { 898 io: std.Io, 899 allocator: std.mem.Allocator, 900 session: *ui.Session, 901 repo: *Repo, 902 903 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port: ?u16) !void { 904 // launch the TUI; the footer points at whatever port was bound 905 self.session.web_port = web_port; 906 self.session.data.clone_http_port = http_port; 907 self.session.data.clone_ssh_port = ssh_port; 908 try hx.ui.run(self.io, self.allocator, self.session, self.repo); 909 } 910 }; 911 912 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session, .repo = &repo }); 913 } 914 } 915 916 // words cycled through to pad generated lines into prose-like content 917 const scatter_words = [_][]const u8{ 918 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 919 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 920 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 921 "scatter", "gather", "encode", "decode", "render", "commit", 922 }; 923 924 // write `path` as 40 lines of mostly-stable filler. revision `c` and file 925 // index `fi` shift which lines change and the words used, so each (c, fi) 926 // yields a distinct multi-hunk diff against the previous revision. untouched 927 // lines stay byte-identical across revisions, so a bump in `c` is a scatter of 928 // small hunks rather than a full rewrite. 929 fn writeScatterFile(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir, path: []const u8, fi: usize, c: usize) !void { 930 const file = try repo_dir.createFile(io, path, .{}); 931 defer file.close(io); 932 var writer = std.Io.Writer.Allocating.init(allocator); 933 defer writer.deinit(); 934 for (0..40) |line| { 935 const start = writer.written().len; 936 if ((line + fi + c) % 8 == 0) { 937 const tag = scatter_words[(line + fi + c * 3) % scatter_words.len]; 938 try writer.writer.print("rev {d} {s}", .{ c, tag }); 939 } 940 const target = 40 + (line * 17 + fi * 23) % 70; 941 var w = line + fi; 942 while (true) : (w += 1) { 943 const word = scatter_words[w % scatter_words.len]; 944 const len = writer.written().len - start; 945 if (len >= target or len + 1 + word.len > 120) break; 946 if (len == 0) 947 try writer.writer.print("{s}", .{word}) 948 else 949 try writer.writer.print(" {s}", .{word}); 950 } 951 try writer.writer.writeByte('\n'); 952 } 953 try file.writeStreamingAll(io, writer.written()); 954 } 955 956 // tag the current HEAD as the next sequential version: v1, v2, and so on 957 fn addNextTag(repo: *rp.Repo(.xit, .{}), io: std.Io, allocator: std.mem.Allocator, n: *usize) !void { 958 var buf: [16]u8 = undefined; 959 const name = try std.fmt.bufPrint(&buf, "v{d}", .{n.*}); 960 _ = try repo.addTag(io, allocator, .{ .name = name }); 961 n.* += 1; 962 } 963 964 // recursively copy the contents of src_dir into dest_dir 965 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 966 var iter = src_dir.iterate(); 967 while (try iter.next(io)) |entry| { 968 switch (entry.kind) { 969 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 970 .directory => { 971 try dest_dir.createDirPath(io, entry.name); 972 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 973 defer dest_entry_dir.close(io); 974 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 975 defer src_entry_dir.close(io); 976 try copyDir(io, src_entry_dir, dest_entry_dir); 977 }, 978 else => {}, 979 } 980 } 981 } 982