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