╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
763 .thread_id = issue_events[issue_events.len - 1].id, 764 .parent_id = switch (i) { 765 0, 1 => issue_events[issue_events.len - 1].id, 766 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 767 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 768 else => unreachable, 769 }, 770 .body = comment_bodies[i], 771 } }, 772 }; 773 } 774 try evt.consume(.local, .repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &comment_events); 775 776 const discussion_data = [_]struct { 777 title: []const u8, 778 description: []const u8, 779 labels: []const u8, 780 }{ 781 .{ 782 .title = "How should plugins declare capabilities?", 783 .description = "I'd like the manifest to make privileged capabilities explicit without making simple plugins verbose.", 784 .labels = "plugins design", 785 }, 786 .{ 787 .title = "Ideas for making large repositories faster", 788 .description = "This is a place to collect profiling results and discuss which indexing work is worth pursuing first.", 789 .labels = "performance indexing", 790 }, 791 .{ 792 .title = "What should the next release focus on?", 793 .description = "Let's compare the most important reliability fixes with the larger features already in progress.", 794 .labels = "release planning", 795 }, 796 .{ 797 .title = "Improving keyboard navigation", 798 .description = "Share workflows that still require a mouse and suggestions for making their focus behavior predictable.", 799 .labels = "ui keyboard accessibility", 800 }, 801 .{ 802 .title = "Configuration format discussion", 803 .description = "Should the next configuration format favor strict validation or accept common conveniences such as trailing commas?", 804 .labels = "config design", 805 }, 806 }; 807 808 var discussion_events: [discussion_data.len]evt.EventWithId = undefined; 809 for (discussion_data, 0..) |discussion, i| { 810 discussion_events[i] = .{ 811 .id = std.fmt.bytesToHex(evt.EventWithId.randomId(prng.random()), .lower), 812 .timestamp = @intCast(300 + i), 813 .author = .{ .name = user_data[i % user_data.len].name, .email = user_data[i % user_data.len].email }, 814 .event = .{ .discuss = .{ 815 .title = discussion.title, 816 .description = discussion.description, 817 .labels = discussion.labels, 818 } }, 819 }; 820 } 821 try evt.consume(.local, .repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &discussion_events); 822 823 var discussion_comment_ids: [discussion_data.len][3][evt.event_id_size]u8 = undefined; 824 for (&discussion_comment_ids) |*ids| { 825 for (ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 826 } 827 var discussion_comment_events: [discussion_data.len * 3]evt.EventWithId = undefined; 828 for (&discussion_comment_events, 0..) |*event, i| { 829 const discussion_index = i / 3; 830 const comment_index = i % 3; 831 event.* = .{ 832 .id = std.fmt.bytesToHex(discussion_comment_ids[discussion_index][comment_index], .lower), 833 .timestamp = @intCast(400 + i), 834 .author = .{ .name = user_data[(i + 1) % user_data.len].name, .email = user_data[(i + 1) % user_data.len].email }, 835 .event = .{ .comment = .{ 836 .thread_id = discussion_events[discussion_index].id, 837 .parent_id = if (comment_index < 2) 838 discussion_events[discussion_index].id 839 else 840 std.fmt.bytesToHex(discussion_comment_ids[discussion_index][0], .lower), 841 .body = switch (comment_index) { 842 0 => "My preference is to start with the smallest useful version and expand it after we have real usage to learn from.", 843 1 => "It would help to write down the tradeoffs we are accepting so the decision is easy to revisit later.", 844 2 => "Agreed. A narrow first version should also make it easier to keep the implementation understandable.", 845 else => unreachable, 846 }, 847 } }, 848 }; 849 } 850 try evt.consume(.local, .repo, .xit, .{}, io, allocator, &template_repo, evt.events_ref, &discussion_comment_events); 851 try template_repo.patchAll(io, allocator, null); 852 try template_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }); 853 try transaction(io, &template_repo, Reindex{ .io = io, .allocator = allocator, .core = &template_repo.core, .commits = true }); 854 // clear history only after indexing has written its transactions. 855 { 856 const moment = try evt.currentMoment(.{}, &template_repo); 857 const roots = try evt.PatchRev.gcRoots(rp.Repo(.xit, .{}).DB, .sha1, allocator, moment); 858 defer allocator.free(roots); 859 _ = try template_repo.garbageCollect(io, allocator, .{ .extra_roots = roots }); 860 } 861 } 862 863 // copy the template to each repo's on-disk location, named by its 864 // hex-encoded event id. the template repo is deinitialized above, so its 865 // db file is fully written before we copy it. 866 { 867 var template_dir = try cwd.openDir(io, template_path, .{}); 868 defer template_dir.close(io); 869 var template_repo_dir = try template_dir.openDir(io, ".xit", .{ .iterate = true }); 870 defer template_repo_dir.close(io); 871 872 for (repo_event_ids, 0..) |id_bytes, repo_index| { 873 const repo_path = try evt.repoPath(arena.allocator(), users_dir, &user_ids[repo_data[repo_index].user_index], &id_bytes); 874 { 875 var dest_dir = try cwd.createDirPathOpen(io, repo_path, .{}); 876 defer dest_dir.close(io); 877 var dest_repo_dir = try dest_dir.createDirPathOpen(io, ".xit", .{}); 878 defer dest_repo_dir.close(io); 879 try copyDir(io, template_repo_dir, dest_repo_dir); 880 } 881 882 if (repo_index + 1 == repo_data.len) { 883 var target_repo = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = repo_path, .require_repo_root = true }); 884 defer target_repo.deinit(io, allocator); 885 try seedPatches(io, allocator, server_path, &repo, &target_repo, &repo_event_ids[repo_index], &user_ids[repo_data[repo_index].user_index], &admin_user_id, prng.random()); 886 } 887 } 888 } 889 890 break :blk try ui.Session.init(&session_arena, &repo, .{ .user_id = &admin_user_id }); 891 }; 892 session.is_terminal = true; 893 session.data.git_ssh_prefix = git_ssh_prefix; 894 895 // start the server 896 897 // let the native TUI's page builders open the on-disk repos for the file tree 898 session.io = io; 899 session.users_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "users" }); 900 901 // leave a one-shot session for the first browser to hit the web ui, so it 902 // starts logged in as admin 903 { 904 var data_dir = try cwd.createDirPathOpen(io, server_path, .{}); 905 defer data_dir.close(io); 906 const store = try hx.web.SessionStore.init(io, data_dir); 907 defer store.deinit(); 908 const token = try store.create(&admin_user_id); 909 try store.offerAutoLogin(&token); 910 } 911 912 if (cli) { 913 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 914 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 915 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 916 917 const Runnable = struct { 918 io: std.Io, 919 key_path: []const u8, 920 921 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port_maybe: ?u16) !void { 922 _ = web_port; 923 _ = http_port; 924 const ssh_port = ssh_port_maybe orelse return error.SshDisabled; 925 std.debug.print( 926 \\ 927 \\connect to the TUI with: 928 \\ ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 929 \\ 930 \\create a git repo and push over SSH: 931 \\ mkdir -p temp-try/client/test 932 \\ cd temp-try/client/test 933 \\ git init 934 \\ echo "hello" > hello.txt 935 \\ git add hello.txt 936 \\ git commit -m "let there be light" 937 \\ 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 938 \\ 939 \\to quit, press enter. 940 \\ 941 , .{ ssh_port, ssh_port, self.key_path }); 942 // portable stdin read via std.Io — equivalent to the 943 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 944 // on windows. 945 var buf: [1]u8 = undefined; 946 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 947 _ = stdin_reader.interface.takeByte() catch {}; 948 } 949 }; 950 951 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 952 } else { 953 var null_writer = std.Io.Writer.Discarding.init(&.{}); 954 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 955 956 const Runnable = struct { 957 io: std.Io, 958 allocator: std.mem.Allocator, 959 session: *ui.Session, 960 repo: *Repo, 961 962 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port: ?u16) !void { 963 // launch the TUI; the footer points at whatever port was bound 964 self.session.web_port = web_port; 965 self.session.data.git_http_port = http_port; 966 self.session.data.git_ssh_port = ssh_port; 967 try hx.ui.run(self.io, self.allocator, self.session, self.repo); 968 } 969 }; 970 971 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session, .repo = &repo }); 972 } 973 } 974 975 // words cycled through to pad generated lines into prose-like content 976 const scatter_words = [_][]const u8{ 977 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 978 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 979 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 980 "scatter", "gather", "encode", "decode", "render", "commit", 981 }; 982 983 fn longDescription(allocator: std.mem.Allocator, initial: []const u8) ![]u8 { 984 var writer = std.Io.Writer.Allocating.init(allocator); 985 defer writer.deinit(); 986 try writer.writer.print("{s}", .{initial}); 987 for (0..ui.detail_preview_lines) |line| 988 try writer.writer.print("\n{d} {s}", .{ line, scatter_words[line % scatter_words.len] }); 989 return writer.toOwnedSlice(); 990 } 991 992 // write `path` as 40 lines of mostly-stable filler. revision `c` and file 993 // index `fi` shift which lines change and the words used, so each (c, fi) 994 // yields a distinct multi-hunk diff against the previous revision. untouched 995 // lines stay byte-identical across revisions, so a bump in `c` is a scatter of 996 // small hunks rather than a full rewrite. 997 fn writeScatterFile(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir, path: []const u8, fi: usize, c: usize) !void { 998 const content = try scatterContent(allocator, fi, c, null); 999 defer allocator.free(content); 1000 const file = try repo_dir.createFile(io, path, .{}); 1001 defer file.close(io); 1002 try file.writeStreamingAll(io, content); 1003 } 1004 1005 const ScatterEdit = struct { line: usize, text: []const u8 }; 1006 1007 fn scatterContent(allocator: std.mem.Allocator, fi: usize, c: usize, replacement: ?ScatterEdit) ![]u8 { 1008 var writer = std.Io.Writer.Allocating.init(allocator); 1009 defer writer.deinit(); 1010 for (0..40) |line| { 1011 if (replacement) |edit| if (line == edit.line) { 1012 try writer.writer.print("{s}\n", .{edit.text}); 1013 continue; 1014 }; 1015 const start = writer.written().len; 1016 if ((line + fi + c) % 8 == 0) { 1017 const tag = scatter_words[(line + fi + c * 3) % scatter_words.len]; 1018 try writer.writer.print("rev {d} {s}", .{ c, tag }); 1019 } 1020 const target = 40 + (line * 17 + fi * 23) % 70; 1021 var w = line + fi; 1022 while (true) : (w += 1) { 1023 const word = scatter_words[w % scatter_words.len]; 1024 const len = writer.written().len - start; 1025 if (len >= target or len + 1 + word.len > 120) break; 1026 if (len == 0) 1027 try writer.writer.print("{s}", .{word}) 1028 else 1029 try writer.writer.print(" {s}", .{word}); 1030 } 1031 try writer.writer.writeByte('\n'); 1032 } 1033 return writer.toOwnedSlice(); 1034 } 1035 1036 // tag the current HEAD with the next three sequential versions, so the refs 1037 // tab has enough tags to page through 1038 fn addNextTags(repo: *rp.Repo(.xit, .{}), io: std.Io, allocator: std.mem.Allocator, n: *usize) !void { 1039 for (0..3) |_| { 1040 var buf: [16]u8 = undefined; 1041 const name = try std.fmt.bufPrint(&buf, "v{d}", .{n.*}); 1042 _ = try repo.addTag(io, allocator, .{ .name = name }); 1043 n.* += 1; 1044 } 1045 } 1046 1047 fn commitTree( 1048 io: std.Io, 1049 allocator: std.mem.Allocator, 1050 repo: *rp.Repo(.xit, .{}), 1051 oid: *const [hash.hexLen(.sha1)]u8, 1052 ) ![hash.hexLen(.sha1)]u8 { 1053 var moment = try repo.core.latestMoment(); 1054 const state = rp.Repo(.xit, .{}).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 1055 var object = try obj.Object(.xit, .{}).init(state, io, allocator, oid); 1056 defer object.deinit(); 1057 return switch (object.content) { 1058 .commit => |commit| commit.tree, 1059 else => error.InvalidObject, 1060 }; 1061 } 1062 1063 fn seedPatchRevision( 1064 io: std.Io, 1065 allocator: std.mem.Allocator, 1066 users_dir: []const u8, 1067 forker_id: *const [evt.event_id_size]u8, 1068 patch_id: *const [evt.event_id_size]u8, 1069 title: []const u8, 1070 author: evt.CommitAuthor, 1071 timestamp: u64, 1072 random: std.Random, 1073 base_oid: [hash.hexLen(.sha1)]u8, 1074 path: []const u8, 1075 contents: []const []const u8, 1076 ) !void { 1077 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 1078 const fork_path = try fork.forkPath(allocator, users_dir, forker_id, patch_id); 1079 defer allocator.free(fork_path); 1080 var fork_repo = try rp.Repo(.xit, .{}).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 1081 defer fork_repo.deinit(io, allocator); 1082 1083 try fork_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "false" }); 1084 { 1085 var reset = try fork_repo.reset(io, allocator, .{ .target = .{ .oid = &base_oid } }); 1086 defer reset.deinit(); 1087 } 1088 const base_tree_oid = try commitTree(io, allocator, &fork_repo, &base_oid); 1089 var source_oid = base_oid; 1090 var fork_dir = try std.Io.Dir.cwd().openDir(io, fork_path, .{}); 1091 defer fork_dir.close(io); 1092 var writer = std.Io.Writer.Allocating.init(allocator); 1093 defer writer.deinit(); 1094 if (std.fs.path.dirname(path)) |dir| try fork_dir.createDirPath(io, dir); 1095 for (contents, 0..) |content, i| { 1096 { 1097 const file = try fork_dir.createFile(io, path, .{}); 1098 defer file.close(io); 1099 try file.writeStreamingAll(io, content); 1100 } 1101 try fork_repo.add(io, allocator, &.{path}); 1102 writer.clearRetainingCapacity(); 1103 try writer.writer.print("{s} ({d}/{d})", .{ title, i + 1, contents.len }); 1104 source_oid = try fork_repo.commit(io, allocator, .{ .message = writer.written(), .timestamp = timestamp + i }); 1105 } 1106 try fork_repo.patchAll(io, allocator, null); 1107 try fork_repo.addConfig(io, allocator, .{ .name = "core.bare", .value = "true" }); 1108 try transaction(io, &fork_repo, Reindex{ .io = io, .allocator = allocator, .core = &fork_repo.core, .commits = false }); 1109 _ = try fork_repo.garbageCollect(io, allocator, .{}); 1110 try fork_dir.deleteFile(io, path); 1111 const revision_id = evt.EventWithId.randomId(random); 1112 const head_tree_oid = try commitTree(io, allocator, &fork_repo, &source_oid); 1113 const revision: evt.PatchRev = .{ 1114 .base_oid = &base_oid, 1115 .source_oid = &source_oid, 1116 .message = title, 1117 }; 1118 const revision_timestamp = timestamp + 3; 1119 const tree_entries = [_]evt.EventTreeEntry{ 1120 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 1121 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 1122 }; 1123 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .fork, .xit, .{}, io, allocator, &fork_repo, evt.events_ref, &.{.{ 1124 .id = std.fmt.bytesToHex(revision_id, .lower), 1125 .timestamp = revision_timestamp, 1126 .author = author, 1127 .tree_entries = &tree_entries, 1128 .event = .{ .patchrev = revision }, 1129 }}); 1130 1131 var arena = std.heap.ArenaAllocator.init(allocator); 1132 defer arena.deinit(); 1133 const moment = try evt.currentMoment(.{}, &fork_repo); 1134 const revision_record = (try evt.PatchRev.readById(evt.EventDB(.sha1), .sha1, moment, &arena, &revision_id)) orelse return error.NotFound; 1135 const patch_record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &arena, patch_id)) orelse return error.NotFound; 1136 var patch = patch_record.event; 1137 patch.revision = evt.Patch.Revision.fromRecord(revision_id, revision_record); 1138 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .fork, .xit, .{}, io, allocator, &fork_repo, evt.events_ref, &.{.{ 1139 .id = patch_hex, 1140 .timestamp = revision_timestamp + 1, 1141 .author = author, 1142 .event = .{ .patch = patch }, 1143 }}); 1144 } 1145 1146 fn seedPatches( 1147 io: std.Io, 1148 allocator: std.mem.Allocator, 1149 server_path: []const u8, 1150 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 1151 target_repo: *rp.Repo(.xit, .{}), 1152 repo_id: *const [evt.event_id_size]u8, 1153 repo_user_id: *const [evt.event_id_size]u8, 1154 user_id: *const [evt.event_id_size]u8, 1155 random: std.Random, 1156 ) !void { 1157 const patch_data = [_]struct { 1158 title: []const u8, 1159 description: []const u8, 1160 labels: []const u8, 1161 status: ?evt.Patch.StatusKind, 1162 alpha_edit: ?ScatterEdit = null, 1163 // based behind master, so merging it makes a merge commit 1164 behind_master: bool = false, 1165 }{ 1166 .{ 1167 .title = "Draft a faster dependency scanner", 1168 .description = "Rework dependency discovery so a large workspace can be scanned without repeatedly opening the same manifests.", 1169 .labels = "performance build", 1170 .status = null, 1171 }, 1172 .{ 1173 .title = "Remove the legacy configuration loader", 1174 .description = "Delete the compatibility loader now that the replacement format has shipped and the migration warning has been available for a full release.", 1175 .labels = "cleanup config", 1176 .status = .merged, 1177 .behind_master = true, 1178 }, 1179 .{ 1180 .title = "Cache parsed manifests between commands", 1181 .description = "Keep parsed manifests in the command context so consecutive operations do not repeat identical filesystem and parsing work.", 1182 .labels = "performance cache", 1183 .status = .closed, 1184 }, 1185 .{ 1186 .title = "Add structured output to the inspect command", 1187 .description = "Add a stable JSON representation of inspect results for scripts and editor integrations.", 1188 .labels = "enhancement cli", 1189 .status = .open, 1190 }, 1191 .{ 1192 .title = "Edit an adjacent line in alpha.txt", 1193 .description = "This patch can be cleanly merged by haxy, while git throws a merge conflict!", 1194 .labels = "merge", 1195 .status = .open, 1196 .alpha_edit = .{ .line = 2, .text = "adjust alpha beside the latest scatter edit" }, 1197 }, 1198 .{ 1199 .title = "Edit a conflicting line in alpha.txt", 1200 .description = "Both source and squash merges conflict with a different edit to the same line on master.", 1201 .labels = "merge", 1202 .status = .open, 1203 .alpha_edit = .{ .line = 3, .text = "replace alpha with a conflicting edit" }, 1204 }, 1205 }; 1206 1207 const users_dir = try std.fs.path.join(allocator, &.{ server_path, "users" }); 1208 defer allocator.free(users_dir); 1209 const patch_author = evt.CommitAuthor{ .name = "admin", .email = "admin@example.test" }; 1210 var patch_ids: [patch_data.len][evt.event_id_size]u8 = undefined; 1211 for (patch_data, 0..) |patch, i| { 1212 const timestamp: u64 = @intCast(500 + i * 10); 1213 patch_ids[i] = evt.EventWithId.randomId(random); 1214 const patch_hex = std.fmt.bytesToHex(patch_ids[i], .lower); 1215 const path = try fork.create(.{}, io, allocator, users_dir, .{ 1216 .id = patch_hex, 1217 .user_id = user_id.*, 1218 .repo_id = repo_id.*, 1219 .repo_user_id = repo_user_id.*, 1220 .title = patch.title, 1221 .description = patch.description, 1222 .labels = patch.labels, 1223 .target_branch = "master", 1224 .author = patch_author, 1225 .timestamp = timestamp, 1226 }); 1227 allocator.free(path); 1228 1229 // prepare the patch's base and file contents, then seed its commits and revision 1230 { 1231 var arena = std.heap.ArenaAllocator.init(allocator); 1232 defer arena.deinit(); 1233 const aa = arena.allocator(); 1234 var base_oid = (try target_repo.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1235 if (patch.alpha_edit != null or patch.behind_master) { 1236 // v30 precedes the last scatter commit, even after the sample merge 1237 const tag_oid = (try target_repo.readRef(io, .{ .kind = .tag, .name = "v30" })) orelse return error.NotFound; 1238 var moment = try target_repo.core.latestMoment(); 1239 const state = rp.Repo(.xit, .{}).State(.read_only){ .core = &target_repo.core, .extra = .{ .moment = &moment } }; 1240 var tag = try obj.Object(.xit, .{}).init(state, io, allocator, &tag_oid); 1241 defer tag.deinit(); 1242 base_oid = switch (tag.content) { 1243 .tag => |value| value.target, 1244 else => return error.InvalidObject, 1245 }; 1246 } 1247 var contents: [3][]const u8 = undefined; 1248 const file_path: []const u8, const count: usize = if (patch.alpha_edit) |edit| blk: { 1249 // rev 29 first changes line 4; edit that line or the one above it 1250 contents[0] = try scatterContent(aa, 0, 28, edit); 1251 break :blk .{ "src/alpha.txt", 1 }; 1252 } else blk: { 1253 var writer = std.Io.Writer.Allocating.init(allocator); 1254 defer writer.deinit(); 1255 for (&contents, 0..) |*content, part| { 1256 try writer.writer.print("{s}: part {d}\n", .{ patch.title, part + 1 }); 1257 content.* = try aa.dupe(u8, writer.written()); 1258 } 1259 break :blk .{ "patch.txt", contents.len }; 1260 }; 1261 try seedPatchRevision(io, allocator, users_dir, user_id, &patch_ids[i], patch.title, patch_author, timestamp + 1, random, base_oid, file_path, contents[0..count]); 1262 } 1263 const status = patch.status orelse continue; 1264 try pch.publish(.{}, io, allocator, users_dir, admin_repo, target_repo, .{ 1265 .id = patch_hex, 1266 .user_id = user_id.*, 1267 .repo_id = repo_id.*, 1268 .author = patch_author, 1269 .timestamp = timestamp + 6, 1270 }); 1271 1272 if (status == .merged) { 1273 try pch.mergeAndRemoveFork(.{}, io, allocator, users_dir, target_repo, .{ 1274 .id = patch_hex, 1275 .revision = .source, 1276 .author = patch_author, 1277 .timestamp = timestamp + 7, 1278 }); 1279 } else if (status != .open) { 1280 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, &patch_ids[i], .{ .status = status }, patch_author); 1281 } 1282 } 1283 1284 // seed a small comment tree on the newest patch 1285 var comment_ids: [5][evt.event_id_size]u8 = undefined; 1286 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(random); 1287 const comment_bodies = [_][]const u8{ 1288 "This also needs to preserve the executable bit for files nested inside generated directories.", 1289 "I tested the archive path and the direct-copy path; both currently lose the mode.", 1290 "The archive writer is the right place to centralize it, since every exporter already goes through that layer.", 1291 "Please include a regression fixture with a mix of executable and ordinary files.", 1292 "I'll add that fixture and cover both supported archive formats.", 1293 }; 1294 var comments: [comment_ids.len]evt.EventWithId = undefined; 1295 for (&comments, 0..) |*comment, i| { 1296 comment.* = .{ 1297 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 1298 .timestamp = @intCast(700 + i), 1299 .author = .{ .name = if (i % 2 == 0) "alice" else "bob", .email = if (i % 2 == 0) "alice@example.test" else "bob@example.test" }, 1300 .event = .{ .comment = .{ 1301 .thread_id = std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1302 .parent_id = switch (i) { 1303 0, 1 => std.fmt.bytesToHex(patch_ids[patch_ids.len - 1], .lower), 1304 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 1305 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 1306 else => unreachable, 1307 }, 1308 .body = comment_bodies[i], 1309 } }, 1310 }; 1311 } 1312 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &comments); 1313 1314 // create divergent metadata edits on the next three patches 1315 var patch_arena = std.heap.ArenaAllocator.init(allocator); 1316 defer patch_arena.deinit(); 1317 const moment = try evt.currentMoment(.{}, target_repo); 1318 var values: [3]evt.Patch = undefined; 1319 for (1..4) |i| { 1320 const record = (try evt.Patch.readById(evt.EventDB(.sha1), .sha1, moment, &patch_arena, &patch_ids[i])) orelse return error.NotFound; 1321 values[i - 1] = record.event; 1322 } 1323 var ours_values = values; 1324 ours_values[0].title = "Drop the legacy configuration loader"; 1325 ours_values[0].labels = "cleanup config breaking"; 1326 const closed_description = try longDescription( 1327 patch_arena.allocator(), 1328 "Cache parsed manifests for the lifetime of a command invocation and invalidate entries when their files change.", 1329 ); 1330 ours_values[1].description = closed_description; 1331 ours_values[2].description = "Add JSON output to inspect with versioned field names and deterministic object ordering."; 1332 var theirs_values = values; 1333 theirs_values[0].title = "Delete compatibility configuration support"; 1334 theirs_values[0].labels = "config maintenance"; 1335 theirs_values[1].description = "Keep a process-wide manifest cache shared by every command and refresh it after writes."; 1336 theirs_values[2].description = "Expose inspect results as newline-delimited JSON so callers can stream large repositories."; 1337 1338 const seed_tip = (try target_repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 1339 const other_ref = xit.ref.Ref{ .kind = .head, .name = "haxy/patch-other" }; 1340 var ours = [_]evt.EventWithId{ 1341 .{ 1342 .id = std.fmt.bytesToHex(patch_ids[1], .lower), 1343 .timestamp = 800, 1344 .author = .{ .name = "alice", .email = "alice@example.test" }, 1345 .event = .{ .patch = ours_values[0] }, 1346 }, 1347 .{ 1348 .id = std.fmt.bytesToHex(patch_ids[2], .lower), 1349 .timestamp = 801, 1350 .author = .{ .name = "alice", .email = "alice@example.test" }, 1351 .event = .{ .patch = ours_values[1] }, 1352 }, 1353 .{ 1354 .id = std.fmt.bytesToHex(patch_ids[3], .lower), 1355 .timestamp = 802, 1356 .author = .{ .name = "alice", .email = "alice@example.test" }, 1357 .event = .{ .patch = ours_values[2] }, 1358 }, 1359 }; 1360 1361 var theirs = ours; 1362 theirs[0].timestamp = 810; 1363 theirs[0].author = .{ .name = "bob", .email = "bob@example.test" }; 1364 theirs[0].event.patch = theirs_values[0]; 1365 theirs[1].timestamp = 811; 1366 theirs[1].author = .{ .name = "bob", .email = "bob@example.test" }; 1367 theirs[1].event.patch = theirs_values[1]; 1368 theirs[2].timestamp = 812; 1369 theirs[2].author = .{ .name = "bob", .email = "bob@example.test" }; 1370 theirs[2].event.patch = theirs_values[2]; 1371 1372 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &ours); 1373 try commitEventsAtRef(io, allocator, target_repo, other_ref, &theirs, seed_tip); 1374 { 1375 var merge = try target_repo.mergeAtRef(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, evt.events_ref, null); 1376 defer merge.deinit(); 1377 if (merge.result != .success) return error.MergeFailed; 1378 } 1379 try target_repo.removeBranch(io, .{ .name = other_ref.name }); 1380 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .repo, .xit, .{}, io, allocator, target_repo, evt.events_ref, &.{}); 1381 try pch.writeBranchPatch(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, std.fmt.bytesToHex(evt.EventWithId.randomId(random), .lower), .{ 1382 .title = "Merge the existing feature branch", 1383 .description = "This patch tracks the feature branch in this repo without creating a fork.", 1384 .labels = "feature", 1385 .source_branch = "feature", 1386 .target_branch = "master", 1387 }, null, patch_author); 1388 1389 // a push onto the branch that patch tracks, so the undo tab shows the 1390 // action and the patch refresh it sets off 1391 try seedPush(io, allocator, target_repo, users_dir, patch_author); 1392 try pch.refreshBranches(.{ .server = .{ .users_dir = users_dir } }, .xit, .{}, io, allocator, target_repo, null, null); 1393 } 1394 1395 // stand in for a client push, which the fixture has no server to receive: one 1396 // transaction moves the branch, refreshes the patches tracking it and records 1397 // the action, the way receive-pack does 1398 fn seedPush(io: std.Io, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, .{}), users_dir: []const u8, author: evt.CommitAuthor) !void { 1399 const Repo = rp.Repo(.xit, .{}); 1400 const DB = Repo.DB; 1401 const Ctx = struct { 1402 core: *Repo.Core, 1403 io: std.Io, 1404 allocator: std.mem.Allocator, 1405 users_dir: []const u8, 1406 author: evt.CommitAuthor, 1407 1408 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1409 const opts: rp.RepoOpts(.xit) = .{}; 1410 var moment = try DB.HashMap(.read_write).init(cursor.*); 1411 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1412 var arena = std.heap.ArenaAllocator.init(ctx.allocator); 1413 defer arena.deinit(); 1414 const aa = arena.allocator(); 1415 1416 // a null tree keeps the parent's content, so the branch advances 1417 // without the fixture having to build one 1418 const author_line = try std.fmt.allocPrint(aa, "{s} <{s}>", .{ ctx.author.name, ctx.author.email }); 1419 const feature_old = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "feature" } })) orelse return error.NotFound; 1420 const feature_new = try obj.writeCommit(.xit, opts, state, ctx.io, ctx.allocator, .{ 1421 .message = "Revise the feature branch", 1422 .author = author_line, 1423 .timestamp = 820, 1424 }, null, .{ .kind = .head, .name = "feature" }); 1425 1426 // a branch the push creates has no old tip, so its range is open 1427 try bch.add(.xit, opts, state, ctx.io, .{ .name = "docs" }); 1428 const docs_new = (try rf.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = .{ .kind = .head, .name = "docs" } })) orelse return error.NotFound; 1429 1430 const updates = [_]xit.net_server_receive_pack.AppliedRefUpdate{ 1431 .{ 1432 .ref_name = try aa.dupe(u8, "refs/heads/feature"), 1433 .old_oid = try aa.dupe(u8, &feature_old), 1434 .new_oid = try aa.dupe(u8, &feature_new), 1435 }, 1436 .{ 1437 .ref_name = try aa.dupe(u8, "refs/heads/docs"), 1438 .old_oid = try aa.dupe(u8, "0" ** hash.hexLen(opts.hash)), 1439 .new_oid = try aa.dupe(u8, &docs_new), 1440 }, 1441 }; 1442 1443 _ = try pch.refreshBranchesInTransaction(.{ .server = .{ .users_dir = ctx.users_dir } }, opts, state, &moment, ctx.io, ctx.allocator, &updates, null, null); 1444 try push.writeUndo(opts, state, ctx.io, ctx.allocator, ctx.author, &updates); 1445 } 1446 }; 1447 1448 try transaction(io, repo, Ctx{ .core = &repo.core, .io = io, .allocator = allocator, .users_dir = users_dir, .author = author }); 1449 } 1450 1451 // commit `events` onto `ref`, rooted at `parent`, as one transaction. 1452 // consume always roots a new branch at nothing, so the divergent fixtures 1453 // write their side branch here instead. nothing is consumed, so it records 1454 // xit's own commit action rather than one of haxy's event actions 1455 fn commitEventsAtRef( 1456 io: std.Io, 1457 allocator: std.mem.Allocator, 1458 repo: *rp.Repo(.xit, .{}), 1459 ref: xit.ref.Ref, 1460 events: []const evt.EventWithId, 1461 parent: [hash.hexLen(.sha1)]u8, 1462 ) !void { 1463 const Repo = rp.Repo(.xit, .{}); 1464 const DB = Repo.DB; 1465 const Ctx = struct { 1466 core: *Repo.Core, 1467 io: std.Io, 1468 allocator: std.mem.Allocator, 1469 ref: xit.ref.Ref, 1470 events: []const evt.EventWithId, 1471 parent: [hash.hexLen(.sha1)]u8, 1472 1473 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1474 const opts: rp.RepoOpts(.xit) = .{}; 1475 var moment = try DB.HashMap(.read_write).init(cursor.*); 1476 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1477 try evt.commitEvents(.xit, opts, state, ctx.io, ctx.allocator, ctx.ref, ctx.events, .{ctx.parent}); 1478 const tip = (try xit.ref.readRecur(.xit, opts, state.readOnly(), ctx.io, .{ .ref = ctx.ref })) orelse return error.NotFound; 1479 try xit.undo.write(opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ 1480 .commit = try .init("seed conflicting events", tip, opts.max_read_size), 1481 }); 1482 } 1483 }; 1484 1485 try transaction(io, repo, Ctx{ 1486 .core = &repo.core, 1487 .io = io, 1488 .allocator = allocator, 1489 .ref = ref, 1490 .events = events, 1491 .parent = parent, 1492 }); 1493 } 1494 1495 // append `ctx` to the repo's history as one transaction. the fixtures write a 1496 // few states the normal paths have no way to produce 1497 // fixtures commit directly, so the ref tips are indexed by hand. a fork gets 1498 // no commit index, as in production. 1499 const Reindex = struct { 1500 io: std.Io, 1501 allocator: std.mem.Allocator, 1502 core: *rp.Repo(.xit, .{}).Core, 1503 commits: bool, 1504 1505 pub fn run(ctx: @This(), cursor: *rp.Repo(.xit, .{}).DB.Cursor(.read_write)) !void { 1506 var moment = try rp.Repo(.xit, .{}).DB.HashMap(.read_write).init(cursor.*); 1507 const state = rp.Repo(.xit, .{}).State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1508 _ = try find.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, null); 1509 if (ctx.commits) _ = try srch_cmmt.refreshInTransaction(.{}, state, &moment, ctx.io, ctx.allocator, &.{}, null); 1510 } 1511 }; 1512 1513 fn transaction(io: std.Io, repo: *rp.Repo(.xit, .{}), ctx: anytype) !void { 1514 const DB = rp.Repo(.xit, .{}).DB; 1515 try repo.core.db_file.lock(io, .exclusive); 1516 defer repo.core.db_file.unlock(io); 1517 1518 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 1519 try history.appendContext(.{ .slot = try history.getSlot(-1) }, ctx); 1520 } 1521 1522 // recursively copy the contents of src_dir into dest_dir 1523 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 1524 var iter = src_dir.iterate(); 1525 while (try iter.next(io)) |entry| { 1526 switch (entry.kind) { 1527 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 1528 .directory => { 1529 try dest_dir.createDirPath(io, entry.name); 1530 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1531 defer dest_entry_dir.close(io); 1532 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 1533 defer src_entry_dir.close(io); 1534 try copyDir(io, src_entry_dir, dest_entry_dir); 1535 }, 1536 else => {}, 1537 } 1538 } 1539 } 1540