╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
753 754 // start the server 755 756 // let the native TUI's page builders open the on-disk repos for the file tree 757 session.io = io; 758 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 759 760 // leave a one-shot session for the first browser to hit the web ui, so it 761 // starts logged in as admin 762 { 763 var data_dir = try cwd.createDirPathOpen(io, server_path, .{}); 764 defer data_dir.close(io); 765 const store = try hx.web.SessionStore.init(io, data_dir); 766 defer store.deinit(); 767 const token = try store.create(&admin_user_id); 768 try store.offerAutoLogin(&token); 769 } 770 771 if (cli) { 772 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 773 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 774 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 775 776 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 777 defer allocator.free(key_path); 778 779 const Runnable = struct { 780 io: std.Io, 781 key_path: []const u8, 782 783 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port_maybe: ?u16) !void { 784 _ = web_port; 785 _ = http_port; 786 const ssh_port = ssh_port_maybe orelse return error.SshDisabled; 787 std.debug.print( 788 \\ 789 \\connect to the TUI with: 790 \\ ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 791 \\ 792 \\create a git repo and push over SSH: 793 \\ mkdir -p temp-try/client/test 794 \\ cd temp-try/client/test 795 \\ git init 796 \\ echo "hello" > hello.txt 797 \\ git add hello.txt 798 \\ git commit -m "let there be light" 799 \\ 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 800 \\ 801 \\to quit, press enter. 802 \\ 803 , .{ ssh_port, ssh_port, self.key_path }); 804 // portable stdin read via std.Io — equivalent to the 805 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 806 // on windows. 807 var buf: [1]u8 = undefined; 808 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 809 _ = stdin_reader.interface.takeByte() catch {}; 810 } 811 }; 812 813 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 814 } else { 815 var null_writer = std.Io.Writer.Discarding.init(&.{}); 816 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 817 818 const Runnable = struct { 819 io: std.Io, 820 allocator: std.mem.Allocator, 821 session: *ui.Session, 822 repo: *Repo, 823 824 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port: ?u16) !void { 825 // launch the TUI; the footer points at whatever port was bound 826 self.session.web_port = web_port; 827 self.session.data.clone_http_port = http_port; 828 self.session.data.clone_ssh_port = ssh_port; 829 try hx.ui.run(self.io, self.allocator, self.session, self.repo); 830 } 831 }; 832 833 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session, .repo = &repo }); 834 } 835 } 836 837 // words cycled through to pad generated lines into prose-like content 838 const scatter_words = [_][]const u8{ 839 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 840 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 841 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 842 "scatter", "gather", "encode", "decode", "render", "commit", 843 }; 844 845 // write `path` as 40 lines of mostly-stable filler. revision `c` and file 846 // index `fi` shift which lines change and the words used, so each (c, fi) 847 // yields a distinct multi-hunk diff against the previous revision. untouched 848 // lines stay byte-identical across revisions, so a bump in `c` is a scatter of 849 // small hunks rather than a full rewrite. 850 fn writeScatterFile(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir, path: []const u8, fi: usize, c: usize) !void { 851 const file = try repo_dir.createFile(io, path, .{}); 852 defer file.close(io); 853 var writer = std.Io.Writer.Allocating.init(allocator); 854 defer writer.deinit(); 855 for (0..40) |line| { 856 const start = writer.written().len; 857 if ((line + fi + c) % 8 == 0) { 858 const tag = scatter_words[(line + fi + c * 3) % scatter_words.len]; 859 try writer.writer.print("rev {d} {s}", .{ c, tag }); 860 } 861 const target = 40 + (line * 17 + fi * 23) % 70; 862 var w = line + fi; 863 while (true) : (w += 1) { 864 const word = scatter_words[w % scatter_words.len]; 865 const len = writer.written().len - start; 866 if (len >= target or len + 1 + word.len > 120) break; 867 if (len == 0) 868 try writer.writer.print("{s}", .{word}) 869 else 870 try writer.writer.print(" {s}", .{word}); 871 } 872 try writer.writer.writeByte('\n'); 873 } 874 try file.writeStreamingAll(io, writer.written()); 875 } 876 877 // tag the current HEAD as the next sequential version: v1, v2, and so on 878 fn addNextTag(repo: *rp.Repo(.xit, .{}), io: std.Io, allocator: std.mem.Allocator, n: *usize) !void { 879 var buf: [16]u8 = undefined; 880 const name = try std.fmt.bufPrint(&buf, "v{d}", .{n.*}); 881 _ = try repo.addTag(io, allocator, .{ .name = name }); 882 n.* += 1; 883 } 884 885 // recursively copy the contents of src_dir into dest_dir 886 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 887 var iter = src_dir.iterate(); 888 while (try iter.next(io)) |entry| { 889 switch (entry.kind) { 890 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 891 .directory => { 892 try dest_dir.createDirPath(io, entry.name); 893 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 894 defer dest_entry_dir.close(io); 895 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 896 defer src_entry_dir.close(io); 897 try copyDir(io, src_entry_dir, dest_entry_dir); 898 }, 899 else => {}, 900 } 901 } 902 } 903