╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
672 for (theirs, 0..) |event, i| { 673 json.clearRetainingCapacity(); 674 try std.json.Stringify.value(event, .{}, &json.writer); 675 const author = try std.fmt.allocPrint(allocator, "{s} <{s}>", .{ event.author.name, event.author.email }); 676 defer allocator.free(author); 677 _ = try template_repo.commitAtRef(io, allocator, .{ 678 .author = author, 679 .message = json.written(), 680 .timestamp = event.timestamp, 681 // root the branch at the shared seed tip 682 .parent_oids = if (i == 0) &.{seed_tip} else null, 683 }, null, other_ref); 684 } 685 686 { 687 var to_events = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 688 defer to_events.deinit(); 689 } 690 { 691 var merge = try template_repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, null); 692 defer merge.deinit(); 693 if (merge.result != .success) return error.MergeFailed; 694 } 695 { 696 var to_master = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 697 defer to_master.deinit(); 698 } 699 try template_repo.removeBranch(io, .{ .name = other_ref.name }); 700 } 701 702 try evt.consume(.repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &.{}); 703 704 // seed a small comment tree on the newest issue 705 var comment_ids: [5][evt.event_id_size]u8 = undefined; 706 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 707 708 const comment_bodies = [_][]const u8{ 709 "I can take this. The compatibility scanner already reports which repositories still use the legacy format.", 710 "Can we keep loading legacy plugins for one release after the warning lands? That would give downstream maintainers time to migrate.", 711 "That sounds good. I'll include the scanner output in the warning so each affected plugin is named.", 712 "Please include the replacement manifest path too. That should make the warning actionable without opening the migration guide.", 713 "I'll add release-note text once the warning format is settled.", 714 }; 715 716 var comment_events: [comment_ids.len]evt.EventWithId = undefined; 717 for (&comment_events, 0..) |*event, i| { 718 event.* = .{ 719 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 720 .timestamp = @intCast(200 + i), 721 .author = .{ .name = user_data[(i + 1) % user_data.len].name, .email = user_data[(i + 1) % user_data.len].email }, 722 .event = .{ .comment = .{ 723 .thread_id = issue_events[issue_events.len - 1].id, 724 .parent_id = switch (i) { 725 0, 1 => issue_events[issue_events.len - 1].id, 726 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 727 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 728 else => unreachable, 729 }, 730 .body = comment_bodies[i], 731 } }, 732 }; 733 } 734 try evt.consume(.repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &comment_events); 735 736 const discussion_data = [_]struct { 737 title: []const u8, 738 description: []const u8, 739 tags: []const u8, 740 }{ 741 .{ 742 .title = "How should plugins declare capabilities?", 743 .description = "I'd like the manifest to make privileged capabilities explicit without making simple plugins verbose.", 744 .tags = "plugins design", 745 }, 746 .{ 747 .title = "Ideas for making large repositories faster", 748 .description = "This is a place to collect profiling results and discuss which indexing work is worth pursuing first.", 749 .tags = "performance indexing", 750 }, 751 .{ 752 .title = "What should the next release focus on?", 753 .description = "Let's compare the most important reliability fixes with the larger features already in progress.", 754 .tags = "release planning", 755 }, 756 .{ 757 .title = "Improving keyboard navigation", 758 .description = "Share workflows that still require a mouse and suggestions for making their focus behavior predictable.", 759 .tags = "ui keyboard accessibility", 760 }, 761 .{ 762 .title = "Configuration format discussion", 763 .description = "Should the next configuration format favor strict validation or accept common conveniences such as trailing commas?", 764 .tags = "config design", 765 }, 766 }; 767 768 var discussion_events: [discussion_data.len]evt.EventWithId = undefined; 769 for (discussion_data, 0..) |discussion, i| { 770 discussion_events[i] = .{ 771 .id = std.fmt.bytesToHex(evt.EventWithId.randomId(prng.random()), .lower), 772 .timestamp = @intCast(300 + i), 773 .author = .{ .name = user_data[i % user_data.len].name, .email = user_data[i % user_data.len].email }, 774 .event = .{ .discuss = .{ 775 .title = discussion.title, 776 .description = discussion.description, 777 .tags = discussion.tags, 778 } }, 779 }; 780 } 781 try evt.consume(.repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &discussion_events); 782 783 var discussion_comment_ids: [discussion_data.len][3][evt.event_id_size]u8 = undefined; 784 for (&discussion_comment_ids) |*ids| { 785 for (ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 786 } 787 var discussion_comment_events: [discussion_data.len * 3]evt.EventWithId = undefined; 788 for (&discussion_comment_events, 0..) |*event, i| { 789 const discussion_index = i / 3; 790 const comment_index = i % 3; 791 event.* = .{ 792 .id = std.fmt.bytesToHex(discussion_comment_ids[discussion_index][comment_index], .lower), 793 .timestamp = @intCast(400 + i), 794 .author = .{ .name = user_data[(i + 1) % user_data.len].name, .email = user_data[(i + 1) % user_data.len].email }, 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