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