╭ try.zig ──────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 //! start a server with some fake data and then launch 2 //! the TUI. this provides a nice way to test things 3 //! out safely. 4 5 const std = @import("std"); 6 const builtin = @import("builtin"); 7 const hx = @import("haxy"); 8 const srv = hx.serve; 9 const evt = hx.event; 10 const xit = hx.xit; 11 const rp = xit.repo; 12 const ui = hx.ui; 13 14 // cook the terminal before a panic/segfault trace is printed, so the trace 15 // isn't mangled by raw mode and the alternate buffer 16 pub const std_options_debug_io = xit.xitui.terminal.crash_debug_io; 17 18 pub fn main(init: std.process.Init) !void { 19 const allocator = std.heap.smp_allocator; 20 21 var threaded = std.Io.Threaded.init(allocator, .{ 22 .environ = init.minimal.environ, 23 }); 24 defer threaded.deinit(); 25 const io = threaded.io(); 26 27 const temp_dir_name = "temp-try"; 28 29 // create the temp dir 30 const cwd = std.Io.Dir.cwd(); 31 var temp_dir_or_err = cwd.openDir(io, temp_dir_name, .{}); 32 if (temp_dir_or_err) |*temp_dir| { 33 temp_dir.close(io); 34 try cwd.deleteTree(io, temp_dir_name); 35 } else |_| {} 36 var temp_dir = try cwd.createDirPathOpen(io, temp_dir_name, .{}); 37 defer cwd.deleteTree(io, temp_dir_name) catch {}; 38 defer temp_dir.close(io); 39 40 // write the dev SSH private key so pushes can authenticate against the 41 // matching public key seeded on the admin account below 42 { 43 const priv_key_file = try temp_dir.createFile(io, "key", .{}); 44 defer priv_key_file.close(io); 45 try priv_key_file.writeStreamingAll(io, 46 \\-----BEGIN OPENSSH PRIVATE KEY----- 47 \\b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW 48 \\QyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22JgAAAJA+7hikPu4Y 49 \\pAAAAAtzc2gtZWQyNTUxOQAAACCniLPJiaooAWecvOCeAjoJwCSeWxzysvpTNkpYjF22Jg 50 \\AAAEDVlopOMnKt/7by/IA8VZvQXUS/O6VLkixOqnnahUdPCKeIs8mJqigBZ5y84J4COgnA 51 \\JJ5bHPKy+lM2SliMXbYmAAAAC3JhZGFyQHJvYXJrAQI= 52 \\-----END OPENSSH PRIVATE KEY----- 53 \\ 54 ); 55 if (.windows != builtin.os.tag) { 56 try priv_key_file.setPermissions(io, @enumFromInt(0o600)); 57 } 58 } 59 60 const cwd_path = try std.process.currentPathAlloc(io, allocator); 61 defer allocator.free(cwd_path); 62 63 var cli = false; 64 var serve_args: std.ArrayList([]const u8) = .empty; 65 defer serve_args.deinit(allocator); 66 var arg_it = try init.minimal.args.iterateAllocator(allocator); 67 defer arg_it.deinit(); 68 _ = arg_it.skip(); 69 while (arg_it.next()) |arg| { 70 if (std.mem.eql(u8, "--cli", arg)) { 71 cli = true; 72 } else { 73 try serve_args.append(allocator, arg); 74 } 75 } 76 var serve_options = try hx.command.parseServeOptions(allocator, serve_args.items); 77 78 const server_path = if (std.mem.eql(u8, serve_options.data_dir, ".")) 79 try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "server" }) 80 else 81 try std.fs.path.resolve(allocator, &.{ cwd_path, serve_options.data_dir }); 82 defer allocator.free(server_path); 83 serve_options.data_dir = server_path; 84 85 const work_path = try std.fs.path.join(allocator, &.{ server_path, "admin" }); 86 defer allocator.free(work_path); 87 88 const Repo = rp.Repo(.xit, evt.admin_repo_opts); 89 var repo = try Repo.init(io, allocator, .{ .path = work_path }); 90 defer repo.deinit(io, allocator); 91 92 var session_arena = std.heap.ArenaAllocator.init(allocator); 93 defer session_arena.deinit(); 94 95 // the seeded admin's event id, so both uis start logged in as them 96 var admin_user_id: [evt.event_id_size]u8 = undefined; 97 98 var session: ui.Session = blk: { 99 var arena = std.heap.ArenaAllocator.init(allocator); 100 defer arena.deinit(); 101 102 // define test events 103 104 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 105 106 const user_data = [_]struct { 107 name: []const u8, 108 display_name: []const u8, 109 email: []const u8, 110 }{ 111 .{ .name = "admin", .display_name = "Admin", .email = "admin@example.test" }, 112 .{ .name = "alice", .display_name = "Alice Tulley", .email = "alice@example.test" }, 113 .{ .name = "bob", .display_name = "Bob Smith", .email = "bob@example.test" }, 114 .{ .name = "carol", .display_name = "Carol Johnson", .email = "carol@example.test" }, 115 .{ .name = "dave", .display_name = "Dave Wilson", .email = "dave@example.test" }, 116 .{ .name = "eve", .display_name = "Eve Anderson", .email = "eve@example.test" }, 117 .{ .name = "frank", .display_name = "Frank Miller", .email = "frank@example.test" }, 118 .{ .name = "grace", .display_name = "Grace Lee", .email = "grace@example.test" }, 119 .{ .name = "henry", .display_name = "Henry Davis", .email = "henry@example.test" }, 120 .{ .name = "ivy", .display_name = "Ivy Martinez", .email = "ivy@example.test" }, 121 .{ .name = "jack", .display_name = "Jack Thompson", .email = "jack@example.test" }, 122 .{ .name = "kate", .display_name = "Kate Robinson", .email = "kate@example.test" }, 123 .{ .name = "liam", .display_name = "Liam Walker", .email = "liam@example.test" }, 124 .{ .name = "mona", .display_name = "Mona Patel", .email = "mona@example.test" }, 125 .{ .name = "noah", .display_name = "Noah Garcia", .email = "noah@example.test" }, 126 .{ .name = "olivia", .display_name = "Olivia Hernandez", .email = "olivia@example.test" }, 127 .{ .name = "peter", .display_name = "Peter Wright", .email = "peter@example.test" }, 128 .{ .name = "quinn", .display_name = "Quinn Foster", .email = "quinn@example.test" }, 129 .{ .name = "rachel", .display_name = "Rachel Bennett", .email = "rachel@example.test" }, 130 .{ .name = "sam", .display_name = "Sam Brooks", .email = "sam@example.test" }, 131 .{ .name = "tina", .display_name = "Tina Cooper", .email = "tina@example.test" }, 132 }; 133 134 const repo_data = [_]struct { 135 user_index: usize, 136 name: []const u8, 137 description: []const u8, 138 }{ 139 .{ .user_index = 1, .name = "ziglings", .description = "Learn the Zig programming language by fixing tiny broken programs" }, 140 .{ .user_index = 2, .name = "linux", .description = "Linux kernel source tree" }, 141 .{ .user_index = 3, .name = "kubernetes", .description = "Production-grade container orchestration" }, 142 .{ .user_index = 4, .name = "react", .description = "A declarative, efficient, and flexible JavaScript library for building user interfaces" }, 143 .{ .user_index = 5, .name = "typescript", .description = "TypeScript is a superset of JavaScript that compiles to clean JavaScript output" }, 144 .{ .user_index = 6, .name = "rust", .description = "Empowering everyone to build reliable and efficient software" }, 145 .{ .user_index = 7, .name = "go", .description = "The Go programming language" }, 146 .{ .user_index = 8, .name = "nodejs", .description = "Node.js JavaScript runtime" }, 147 .{ .user_index = 9, .name = "cpython", .description = "The Python programming language" }, 148 .{ .user_index = 10, .name = "docker", .description = "Container platform for developing, shipping, and running applications" }, 149 .{ .user_index = 1, .name = "vim", .description = "The ubiquitous text editor" }, 150 .{ .user_index = 2, .name = "neovim", .description = "Hyperextensible Vim-based text editor" }, 151 .{ .user_index = 3, .name = "emacs", .description = "GNU Emacs source code mirror" }, 152 .{ .user_index = 4, .name = "tmux", .description = "Terminal multiplexer" }, 153 .{ .user_index = 5, .name = "zsh", .description = "Mirror of the Z shell source code repository" }, 154 .{ .user_index = 6, .name = "git", .description = "Distributed version control system" }, 155 .{ .user_index = 7, .name = "mercurial", .description = "Source-control management tool" }, 156 .{ .user_index = 8, .name = "tensorflow", .description = "An end-to-end open source machine learning platform" }, 157 .{ .user_index = 9, .name = "pytorch", .description = "Tensors and dynamic neural networks in Python with strong GPU acceleration" }, 158 .{ .user_index = 10, .name = "numpy", .description = "The fundamental package for scientific computing with Python" }, 159 .{ .user_index = 1, .name = "pandas", .description = "Flexible and powerful data analysis and manipulation library for Python" }, 160 .{ .user_index = 2, .name = "scikit-learn", .description = "Machine learning in Python" }, 161 .{ .user_index = 3, .name = "nginx", .description = "High performance HTTP server and reverse proxy" }, 162 .{ .user_index = 4, .name = "redis", .description = "In-memory data structure store, used as a database, cache, and message broker" }, 163 .{ .user_index = 5, .name = "postgres", .description = "The world's most advanced open source relational database" }, 164 .{ .user_index = 6, .name = "sqlite", .description = "Self-contained, serverless, zero-configuration SQL database engine" }, 165 .{ .user_index = 7, .name = "mongodb", .description = "The MongoDB Database" }, 166 .{ .user_index = 8, .name = "elasticsearch", .description = "Free and open, distributed, RESTful search engine" }, 167 .{ .user_index = 9, .name = "kafka", .description = "Distributed event streaming platform" }, 168 .{ .user_index = 10, .name = "terraform", .description = "Infrastructure as code tool" }, 169 .{ .user_index = 11, .name = "svelte", .description = "Cybernetically enhanced web apps" }, 170 .{ .user_index = 12, .name = "vue", .description = "The progressive JavaScript framework" }, 171 .{ .user_index = 13, .name = "flask", .description = "The Python micro framework for building web applications" }, 172 .{ .user_index = 14, .name = "django", .description = "The web framework for perfectionists with deadlines" }, 173 .{ .user_index = 15, .name = "rails", .description = "Ruby on Rails web framework" }, 174 .{ .user_index = 16, .name = "phoenix", .description = "Peace of mind from prototype to production for Elixir web apps" }, 175 .{ .user_index = 17, .name = "laravel", .description = "The PHP framework for web artisans" }, 176 .{ .user_index = 18, .name = "prometheus", .description = "The Prometheus monitoring system and time series database" }, 177 .{ .user_index = 19, .name = "grafana", .description = "The open and composable observability and data visualization platform" }, 178 .{ .user_index = 20, .name = "ansible", .description = "Simple, agentless IT automation" }, 179 }; 180 181 var user_ids: [user_data.len][evt.event_id_size]u8 = undefined; 182 for (&user_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 183 // admin is the first entry of user_data above 184 admin_user_id = user_ids[0]; 185 186 var repo_event_ids: [repo_data.len][evt.event_id_size]u8 = undefined; 187 for (&repo_event_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 188 189 var password_hash_buf: [evt.User.password_hash_max_len]u8 = undefined; 190 const password_hash = try evt.User.hashPassword("password", &password_hash_buf, io); 191 192 // public key matching temp-try/key, given to admin so we can push as admin 193 const admin_ssh_key = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKeIs8mJqigBZ5y84J4COgnAJJ5bHPKy+lM2SliMXbYm radar@roark"; 194 195 var events_to_consume: [user_data.len + repo_data.len]evt.EventWithId = undefined; 196 // inserted back to front, so the newest-first listing shows them in the 197 // order they're written above 198 for (0..user_data.len) |slot| { 199 const i = user_data.len - 1 - slot; 200 const u = user_data[i]; 201 events_to_consume[slot] = .{ 202 .id = std.fmt.bytesToHex(user_ids[i], .lower), 203 // stepped timestamps so the seeded users/repos list in a stable order 204 .timestamp = @intCast(slot + 1), 205 .author_email = "admin@example.test", 206 .event = .{ 207 .user = .{ 208 .name = u.name, 209 .display_name = u.display_name, 210 .email = u.email, 211 .password_hash = password_hash, 212 .ssh_keys = if (std.mem.eql(u8, "admin", u.name)) admin_ssh_key else "", 213 }, 214 }, 215 }; 216 } 217 for (repo_data, 0..) |r, i| { 218 events_to_consume[user_data.len + i] = .{ 219 .id = std.fmt.bytesToHex(repo_event_ids[i], .lower), 220 .timestamp = @intCast(user_data.len + i + 1), 221 .author_email = user_data[r.user_index].email, 222 .event = .{ 223 .repo = .{ 224 .user_id = &user_ids[r.user_index], 225 .name = r.name, 226 .description = r.description, 227 }, 228 }, 229 }; 230 } 231 232 // commit the seed events and consume them into the database 233 try evt.consume(.xit, evt.admin_repo_opts, io, allocator, &repo, evt.events_ref, &events_to_consume); 234 235 // every repo gets the same generated history, so build it once into a 236 // template repo and copy that to each repo's location below rather than 237 // redoing the expensive commit work for every repo. 238 const template_path = try std.fs.path.join(arena.allocator(), &.{ cwd_path, temp_dir_name, "template" }); 239 { 240 var template_repo = try rp.Repo(.xit, .{}).init(io, allocator, .{ .path = template_path }); 241 defer template_repo.deinit(io, allocator); 242 243 try template_repo.addConfig(io, allocator, .{ .name = "user.name", .value = "haxy" }); 244 try template_repo.addConfig(io, allocator, .{ .name = "user.email", .value = "admin@example.test" }); 245 246 // a README plus a nested doc so the file tree has a directory to 247 // descend into 248 { 249 var repo_dir = try cwd.openDir(io, template_path, .{}); 250 defer repo_dir.close(io); 251 252 const readme = try repo_dir.createFile(io, "README.md", .{}); 253 defer readme.close(io); 254 try readme.writeStreamingAll(io, 255 \\# Sample Repo 256 \\ 257 \\A repository seeded with test data for exercising the UI. 258 \\ 259 \\## Features 260 \\ 261 \\- Fast and lightweight 262 \\- Zero configuration required 263 \\- Works out of the box 264 \\ 265 \\## Installation 266 \\ 267 \\Clone the repository and build it from source: 268 \\ 269 \\```sh 270 \\git clone https://example.test/sample-repo.git 271 \\cd sample-repo 272 \\make install 273 \\``` 274 \\ 275 \\## Usage 276 \\ 277 \\Run the program with the `--help` flag to see all available 278 \\options: 279 \\ 280 \\```sh 281 \\sample-repo --help 282 \\``` 283 \\ 284 \\| Option | Description | 285 \\| ----------- | ------------------------------ | 286 \\| `--verbose` | Print extra diagnostic output | 287 \\| `--quiet` | Suppress all non-error output | 288 \\| `--version` | Print the version and exit | 289 \\ 290 \\## Contributing 291 \\ 292 \\Contributions are welcome! Please open an issue to discuss 293 \\any significant changes before submitting a pull request. 294 \\See the docs under `docs/dev` for more details. 295 \\ 296 \\## License 297 \\ 298 \\Released under the MIT License. See `LICENSE` for the full 299 \\text. 300 \\ 301 ); 302 303 try repo_dir.createDirPath(io, "docs/dev"); 304 const doc = try repo_dir.createFile(io, "docs/dev/contribute.md", .{}); 305 defer doc.close(io); 306 try doc.writeStreamingAll(io, "To contribute, please make a pull request"); 307 } 308 309 try template_repo.add(io, allocator, &.{ "README.md", "docs/dev/contribute.md" }); 310 _ = try template_repo.commit(io, allocator, .{ .message = "let there be light" }); 311 312 // tag every commit in creation order as v1, v2, and so on 313 var tag_num: usize = 1; 314 try addNextTag(&template_repo, io, allocator, &tag_num); 315 316 // a batch of commits so the commits tab has more than one page to 317 // paginate through. each rewrites a few files with scattered line 318 // edits, so every commit is a multi-file diff with several separate 319 // hunks to look at. stepped timestamps vary the date column. 320 const base_ts: u64 = 1_700_000_000; // 2023-11-14 321 const edit_files = [_][]const u8{ "src/alpha.txt", "src/beta.txt", "src/gamma.txt" }; 322 // commit subjects, cycled then padded to a varying length. 323 const subjects = [_][]const u8{ 324 "fix off-by-one in scatter loop", 325 "encode and decode buffers", 326 "tune kernel socket timeouts", 327 "render matrix vector product", 328 "gather falcon and otter stats", 329 "refactor badger walrus module", 330 "drop dead ferret marmot branch", 331 }; 332 const commit_count = 30; 333 var c: usize = 0; 334 while (c < commit_count) : (c += 1) { 335 { 336 var repo_dir = try cwd.openDir(io, template_path, .{}); 337 defer repo_dir.close(io); 338 try repo_dir.createDirPath(io, "src"); 339 for (edit_files, 0..) |path, fi| { 340 try writeScatterFile(io, allocator, repo_dir, path, fi, c); 341 } 342 } 343 try template_repo.add(io, allocator, &edit_files); 344 // a cycling subject padded to a c-varying length so the commit 345 // list shows messages of different widths, capped at 120. 346 var msg_writer = std.Io.Writer.Allocating.init(allocator); 347 defer msg_writer.deinit(); 348 try msg_writer.writer.print("{s}", .{subjects[c % subjects.len]}); 349 const msg_target = 16 + (c * 41) % 96; 350 var mw = c; 351 while (true) : (mw += 1) { 352 const word = scatter_words[mw % scatter_words.len]; 353 const len = msg_writer.written().len; 354 if (len >= msg_target or len + 1 + word.len > 120) break; 355 try msg_writer.writer.print(" {s}", .{word}); 356 } 357 // the newest commit's message runs past what the detail pane 358 // reads, so it shows the truncated message and its link. 359 if (c == commit_count - 1) { 360 var line: usize = 0; 361 while (msg_writer.written().len <= ui.Repo.Commits.max_message_size) : (line += 1) { 362 try msg_writer.writer.print("\n{d} {s}", .{ line, scatter_words[line % scatter_words.len] }); 363 } 364 } 365 const message = try arena.allocator().dupe(u8, msg_writer.written()); 366 _ = try template_repo.commit(io, allocator, .{ .message = message, .timestamp = base_ts + c * std.time.s_per_day }); 367 try addNextTag(&template_repo, io, allocator, &tag_num); 368 } 369 370 // two more branches forked off master, each adding a single commit 371 // that makes a scattered edit to one file. tag each new commit too, 372 // and switch back to master after each so the next branch also 373 // forks from master and the template ends up back on master. 374 const branch_data = [_]struct { 375 name: []const u8, 376 file: []const u8, 377 fi: usize, 378 message: []const u8, 379 rev: usize, 380 }{ 381 .{ .name = "extra", .file = "src/alpha.txt", .fi = 0, .message = "scatter alpha on the extra branch", .rev = 30 }, 382 .{ .name = "feature", .file = "src/beta.txt", .fi = 1, .message = "scatter beta on the feature branch", .rev = 31 }, 383 }; 384 for (branch_data) |b| { 385 try template_repo.addBranch(io, .{ .name = b.name }); 386 { 387 var to_branch = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = b.name } } }); 388 defer to_branch.deinit(); 389 } 390 391 { 392 var repo_dir = try cwd.openDir(io, template_path, .{}); 393 defer repo_dir.close(io); 394 try writeScatterFile(io, allocator, repo_dir, b.file, b.fi, b.rev); 395 } 396 try template_repo.add(io, allocator, &.{b.file}); 397 _ = try template_repo.commit(io, allocator, .{ .message = b.message, .timestamp = base_ts + b.rev * std.time.s_per_day }); 398 try addNextTag(&template_repo, io, allocator, &tag_num); 399 400 { 401 var to_master = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 402 defer to_master.deinit(); 403 } 404 } 405 406 // the timestamps issue's description, in paragraphs, so the 407 // seeded conflict below can insert and delete among them 408 const tz_p1 = "All timestamps in the activity feed render in UTC regardless of the system timezone."; 409 const tz_p2 = "The tooltip on each entry repeats the same UTC value, so there is no way to see the local time without converting by hand."; 410 const tz_p3 = "A quick survey of other clients shows every one of them rendering local time by default."; 411 const tz_p4 = "Convert to local time and include the offset in tooltips."; 412 413 // the sync issue's description, in paragraphs, so each side of its 414 // seeded conflict can rework a different one 415 const sync_p1 = "The sync engine only has unit tests with mocked transports."; 416 const sync_p2 = "Failures seen in production involve reconnects and partial writes, which the mocks can't reproduce."; 417 const sync_p3 = "Add end-to-end tests that run two instances against a real local server."; 418 419 // seed issues so every repo's issue tracker has content 420 const issue_data = [_]struct { 421 title: []const u8, 422 description: []const u8, 423 tags: []const u8, 424 status: evt.Issue.Status = .open, 425 }{ 426 .{ 427 .title = "Crash on startup when config file is missing", 428 .description = "Running the program without a config file present dereferences a null pointer and segfaults. Fall back to the built-in defaults and log a warning instead.", 429 .tags = "bug priority-high crash", 430 }, 431 .{ 432 .title = "Support dark mode in the settings panel", 433 .description = "The settings panel is always rendered with the light palette even when the rest of the app is in dark mode. Read the active theme and pick colors accordingly.", 434 .tags = "enhancement ui theme", 435 }, 436 .{ 437 .title = "Memory leak when reconnecting after network failure", 438 .description = "Each reconnect allocates a new connection state without freeing the previous one. After a flaky network session, memory usage grows by several megabytes per hour.", 439 .tags = "bug memory networking", 440 .status = .closed, 441 }, 442 .{ 443 .title = "Document the plugin API", 444 .description = "The plugin interface has no documentation beyond the header comments. Add a guide covering the lifecycle hooks, the event callbacks, and a minimal working plugin.", 445 .tags = "documentation plugins", 446 }, 447 .{ 448 .title = "Slow file indexing on large directories", 449 .description = "Indexing a directory with more than 100k files takes several minutes because every entry is stat'd twice. Cache the first stat result and batch the reads.", 450 .tags = "performance indexing", 451 }, 452 .{ 453 .title = "Add keyboard shortcut for quick search", 454 .description = "Opening the search box currently requires clicking the toolbar icon. Bind a shortcut and show it in the tooltip so keyboard users can search without the mouse.", 455 .tags = "enhancement ux keyboard", 456 }, 457 .{ 458 .title = "Unicode filenames are garbled in the export dialog", 459 .description = "Filenames containing non-ASCII characters display as replacement characters in the export dialog. The dialog decodes the path as Latin-1 instead of UTF-8.", 460 .tags = "bug unicode i18n", 461 }, 462 .{ 463 .title = "Flaky test: integration suite times out on CI", 464 .description = "The integration suite intermittently exceeds the CI time limit because the server fixture waits for a fixed 30 seconds. Poll for readiness instead of sleeping.", 465 .tags = "bug testing ci", 466 }, 467 .{ 468 .title = "Upgrade bundled zlib to the latest release", 469 .description = "The vendored zlib is two major releases behind and misses several upstream fixes. Update the bundled copy and re-run the compression benchmarks.", 470 .tags = "dependencies maintenance", 471 }, 472 .{ 473 .title = "Progress bar overshoots 100% during resumed downloads", 474 .description = "Resuming a partial download counts the already-downloaded bytes twice, so the progress bar reads up to 150%. Subtract the resume offset from the total.", 475 .tags = "bug ui downloads", 476 .status = .closed, 477 }, 478 .{ 479 .title = "Config parser rejects trailing commas", 480 .description = "A trailing comma after the last entry in a config block is reported as a syntax error. Most editors add one automatically, so accept it.", 481 .tags = "bug config parser", 482 }, 483 .{ 484 .title = "Add a --json flag to the status command", 485 .description = "Scripts currently scrape the human-readable status output, which breaks whenever the format changes. Emit a stable machine-readable JSON form behind a flag.", 486 .tags = "enhancement cli", 487 }, 488 .{ 489 .title = "Race condition between autosave and manual save", 490 .description = "Saving manually while an autosave is in flight can interleave the two writes and corrupt the file. Serialize saves through a single queue.", 491 .tags = "bug priority-high data-loss", 492 }, 493 .{ 494 .title = "Reduce binary size of release builds", 495 .description = "The release binary has grown past 40 MB, mostly from debug info and an unused bundled font. Strip symbols and drop the font from the default build.", 496 .tags = "performance build", 497 }, 498 .{ 499 .title = "Tooltips flicker when the cursor moves between adjacent buttons", 500 .description = "Moving the cursor across a toolbar hides and re-shows the tooltip for every button. Keep the tooltip open with a short grace period between neighbors.", 501 .tags = "bug ui polish", 502 }, 503 .{ 504 .title = "Support environment variable expansion in config paths", 505 .description = "Paths in the config file are taken literally, so shared configs can't refer to the home directory portably. Expand environment variables when loading.", 506 .tags = "enhancement config", 507 }, 508 .{ 509 .title = "Log rotation deletes the newest file instead of the oldest", 510 .description = "When the log directory hits its size cap, the rotation logic sorts by name rather than mtime and removes the most recent log. Sort by modification time.", 511 .tags = "bug logging", 512 .status = .closed, 513 }, 514 .{ 515 .title = "Add man pages for all subcommands", 516 .description = "Only the top-level command has a man page. Generate one per subcommand from the existing help text as part of the release build.", 517 .tags = "documentation cli", 518 }, 519 .{ 520 .title = "High CPU usage while idle in the background", 521 .description = "The main loop polls for file changes every 10 ms even when no window is visible. Switch to native file watching and idle at zero CPU.", 522 .tags = "performance priority-high", 523 }, 524 .{ 525 .title = "Paste from clipboard drops the final newline", 526 .description = "Pasting text that ends with a newline silently trims it, which breaks pasted shell snippets. Preserve the clipboard content exactly.", 527 .tags = "bug editor clipboard", 528 }, 529 .{ 530 .title = "Improve error message for expired credentials", 531 .description = "An expired token currently surfaces as a bare 401 with no guidance. Detect the expiry case and tell the user how to re-authenticate.", 532 .tags = "enhancement ux auth", 533 }, 534 .{ 535 .title = "Crash when window is resized during startup animation", 536 .description = "Resizing the window while the splash animation is running dereferences a freed layout node. Cancel the animation before rebuilding the layout.", 537 .tags = "bug crash ui", 538 }, 539 .{ 540 .title = "Add integration tests for the sync engine", 541 .description = sync_p1 ++ "\n\n" ++ sync_p2 ++ "\n\n" ++ sync_p3, 542 .tags = "testing sync", 543 }, 544 .{ 545 .title = "Timestamps display in UTC instead of local time", 546 .description = tz_p1 ++ "\n\n" ++ tz_p2 ++ "\n\n" ++ tz_p3 ++ "\n\n" ++ tz_p4, 547 .tags = "bug i18n time", 548 }, 549 .{ 550 .title = "Deprecate the legacy plugin format", 551 .description = "Both plugin formats are currently loaded, doubling the maintenance surface. Warn on legacy plugins this release and drop support in the next.", 552 .tags = "maintenance plugins", 553 }, 554 }; 555 556 var issue_events: [issue_data.len]evt.EventWithId = undefined; 557 for (issue_data, 0..) |issue, i| { 558 // the fifth-newest issue's description runs past what the detail 559 // pane shows, so it shows the truncated description and its 560 // link. 561 const description = if (i == issue_data.len - 5) desc: { 562 var desc_writer = std.Io.Writer.Allocating.init(allocator); 563 defer desc_writer.deinit(); 564 try desc_writer.writer.print("{s}", .{issue.description}); 565 var line: usize = 0; 566 while (desc_writer.written().len <= ui.Repo.Issues.max_description_size) : (line += 1) { 567 try desc_writer.writer.print("\n{d} {s}", .{ line, scatter_words[line % scatter_words.len] }); 568 } 569 break :desc try arena.allocator().dupe(u8, desc_writer.written()); 570 } else issue.description; 571 issue_events[i] = .{ 572 .id = std.fmt.bytesToHex(evt.EventWithId.randomId(prng.random()), .lower), 573 // stepped timestamps so the issues list in a stable order 574 .timestamp = @intCast(i + 1), 575 .author_email = user_data[i % user_data.len].email, 576 .event = .{ 577 .issue = .{ 578 .title = issue.title, 579 .description = description, 580 .tags = issue.tags, 581 .status = issue.status, 582 }, 583 }, 584 }; 585 } 586 try evt.consume(.xit, .{}, io, allocator, &template_repo, evt.events_ref, &issue_events); 587 // the tip is the branch point for the conflicting edits below 588 const seed_tip = (try template_repo.readRef(io, evt.events_ref)) orelse unreachable; 589 590 // two divergent edits per conflicted issue: ours on the events 591 // branch, theirs on a temp branch rooted at the seed tip, then a 592 // merge. the 4th-newest issue conflicts on title and tags; the 593 // 3rd-newest on its description with every hunk auto-resolving 594 // (each side reworks a different paragraph); the 2nd-newest on 595 // its description with a deletion conflict (ours removes a 596 // paragraph theirs rewords) and an insertion conflict (each side 597 // appends a different closing paragraph). 598 { 599 const other_ref: xit.ref.Ref = .{ .kind = .head, .name = "haxy/other" }; 600 const title_issue = issue_data[issue_data.len - 4]; 601 const sync_issue = issue_data[issue_data.len - 3]; 602 const desc_issue = issue_data[issue_data.len - 2]; 603 604 const ours = [_]evt.EventWithId{ .{ 605 .id = issue_events[issue_data.len - 4].id, 606 .timestamp = 100, 607 .author_email = user_data[1].email, 608 .event = .{ .issue = .{ 609 .title = "Crash when resizing the window during the splash animation", 610 .description = title_issue.description, 611 .tags = "bug crash ui priority-high", 612 } }, 613 }, .{ 614 .id = issue_events[issue_data.len - 3].id, 615 .timestamp = 101, 616 .author_email = user_data[1].email, 617 .event = .{ .issue = .{ 618 .title = sync_issue.title, 619 .description = "The sync engine's coverage is unit tests only, with every transport mocked out." ++ "\n\n" ++ 620 sync_p2 ++ "\n\n" ++ sync_p3, 621 .tags = sync_issue.tags, 622 } }, 623 }, .{ 624 .id = issue_events[issue_data.len - 2].id, 625 .timestamp = 102, 626 .author_email = user_data[1].email, 627 .event = .{ .issue = .{ 628 .title = desc_issue.title, 629 .description = tz_p1 ++ "\n\n" ++ tz_p2 ++ "\n\n" ++ tz_p4 ++ "\n\n" ++ 630 "The confusion is worst for teams spread across timezones, who each read a different wall-clock time from the same feed.", 631 .tags = desc_issue.tags, 632 } }, 633 } }; 634 const theirs = [_]evt.EventWithId{ .{ 635 .id = issue_events[issue_data.len - 4].id, 636 .timestamp = 103, 637 .author_email = user_data[2].email, 638 .event = .{ .issue = .{ 639 .title = "Segfault on early window resize", 640 .description = title_issue.description, 641 .tags = "bug crash rendering", 642 } }, 643 }, .{ 644 .id = issue_events[issue_data.len - 3].id, 645 .timestamp = 104, 646 .author_email = user_data[2].email, 647 .event = .{ .issue = .{ 648 .title = sync_issue.title, 649 .description = sync_p1 ++ "\n\n" ++ sync_p2 ++ "\n\n" ++ 650 "Add end-to-end tests that drive two live instances against a local server on every CI run.", 651 .tags = sync_issue.tags, 652 } }, 653 }, .{ 654 .id = issue_events[issue_data.len - 2].id, 655 .timestamp = 105, 656 .author_email = user_data[2].email, 657 .event = .{ .issue = .{ 658 .title = desc_issue.title, 659 .description = tz_p1 ++ "\n\n" ++ tz_p2 ++ "\n\n" ++ 660 "Most other clients already render local time by default, which makes our UTC output stand out as a bug." ++ "\n\n" ++ tz_p4 ++ "\n\n" ++ 661 "Log exports inherit the same UTC rendering, so downstream tooling has to guess the source timezone.", 662 .tags = desc_issue.tags, 663 } }, 664 } }; 665 666 try evt.consume(.xit, .{}, io, allocator, &template_repo, evt.events_ref, &ours); 667 668 // consume can't root a new branch, so theirs commits by hand 669 var json: std.Io.Writer.Allocating = .init(allocator); 670 defer json.deinit(); 671 for (theirs, 0..) |event, i| { 672 json.clearRetainingCapacity(); 673 try std.json.Stringify.value(event, .{}, &json.writer); 674 const author = try std.fmt.allocPrint(allocator, "haxy <{s}>", .{event.author_email}); 675 defer allocator.free(author); 676 _ = try template_repo.commitAtRef(io, allocator, .{ 677 .author = author, 678 .message = json.written(), 679 .timestamp = event.timestamp, 680 // root the branch at the shared seed tip 681 .parent_oids = if (i == 0) &.{seed_tip} else null, 682 }, null, other_ref); 683 } 684 685 { 686 var to_events = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 687 defer to_events.deinit(); 688 } 689 { 690 var merge = try template_repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_ref }} } } }, null); 691 defer merge.deinit(); 692 if (merge.result != .success) return error.MergeFailed; 693 } 694 { 695 var to_master = try template_repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 696 defer to_master.deinit(); 697 } 698 try template_repo.removeBranch(io, .{ .name = other_ref.name }); 699 } 700 701 try evt.consume(.xit, .{}, io, allocator, &template_repo, evt.events_ref, &.{}); 702 703 // seed a small comment tree on the newest issue 704 var comment_ids: [5][evt.event_id_size]u8 = undefined; 705 for (&comment_ids) |*id| id.* = evt.EventWithId.randomId(prng.random()); 706 707 const comment_bodies = [_][]const u8{ 708 "I can take this. The compatibility scanner already reports which repositories still use the legacy format.", 709 "Can we keep loading legacy plugins for one release after the warning lands? That would give downstream maintainers time to migrate.", 710 "That sounds good. I'll include the scanner output in the warning so each affected plugin is named.", 711 "Please include the replacement manifest path too. That should make the warning actionable without opening the migration guide.", 712 "I'll add release-note text once the warning format is settled.", 713 }; 714 715 var comment_events: [comment_ids.len]evt.EventWithId = undefined; 716 for (&comment_events, 0..) |*event, i| { 717 event.* = .{ 718 .id = std.fmt.bytesToHex(comment_ids[i], .lower), 719 .timestamp = @intCast(200 + i), 720 .author_email = user_data[(i + 1) % user_data.len].email, 721 .event = .{ .comment = .{ 722 .thread_id = issue_events[issue_events.len - 1].id, 723 .parent_id = switch (i) { 724 0, 1 => issue_events[issue_events.len - 1].id, 725 2, 4 => std.fmt.bytesToHex(comment_ids[0], .lower), 726 3 => std.fmt.bytesToHex(comment_ids[2], .lower), 727 else => unreachable, 728 }, 729 .body = comment_bodies[i], 730 } }, 731 }; 732 } 733 try evt.consume(.xit, .{}, io, allocator, &template_repo, evt.events_ref, &comment_events); 734 } 735 736 // copy the template to each repo's on-disk location, named by its 737 // hex-encoded event id. the template repo is deinitialized above, so its 738 // db file is fully written before we copy it. 739 { 740 var template_dir = try cwd.openDir(io, template_path, .{ .iterate = true }); 741 defer template_dir.close(io); 742 743 for (repo_event_ids) |id_bytes| { 744 const repo_id = std.fmt.bytesToHex(id_bytes, .lower); 745 const repo_path = try std.fs.path.join(arena.allocator(), &.{ server_path, "repos", &repo_id }); 746 var dest_dir = try cwd.createDirPathOpen(io, repo_path, .{}); 747 defer dest_dir.close(io); 748 try copyDir(io, template_dir, dest_dir); 749 } 750 } 751 752 break :blk try ui.Session.init(&session_arena, &repo, .{ .user_id = &admin_user_id }); 753 }; 754 session.is_terminal = true; 755 756 // start the server 757 758 // let the native TUI's page builders open the on-disk repos for the file tree 759 session.io = io; 760 session.repos_dir = try std.fs.path.join(session_arena.allocator(), &.{ server_path, "repos" }); 761 762 // leave a one-shot session for the first browser to hit the web ui, so it 763 // starts logged in as admin 764 { 765 var data_dir = try cwd.createDirPathOpen(io, server_path, .{}); 766 defer data_dir.close(io); 767 const store = try hx.web.SessionStore.init(io, data_dir); 768 defer store.deinit(); 769 const token = try store.create(&admin_user_id); 770 try store.offerAutoLogin(&token); 771 } 772 773 if (cli) { 774 var stdout_writer = std.Io.File.stdout().writer(io, &.{}); 775 var stderr_writer = std.Io.File.stderr().writer(io, &.{}); 776 const run_opts = hx.main.RunOpts{ .out = &stdout_writer.interface, .err = &stderr_writer.interface }; 777 778 const key_path = try std.fs.path.join(allocator, &.{ cwd_path, temp_dir_name, "key" }); 779 defer allocator.free(key_path); 780 781 const Runnable = struct { 782 io: std.Io, 783 key_path: []const u8, 784 785 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port_maybe: ?u16) !void { 786 _ = web_port; 787 _ = http_port; 788 const ssh_port = ssh_port_maybe orelse return error.SshDisabled; 789 std.debug.print( 790 \\ 791 \\connect to the TUI with: 792 \\ ssh -p {d} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null localhost 793 \\ 794 \\create a git repo and push over SSH: 795 \\ mkdir -p temp-try/client/test 796 \\ cd temp-try/client/test 797 \\ git init 798 \\ echo "hello" > hello.txt 799 \\ git add hello.txt 800 \\ git commit -m "let there be light" 801 \\ 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 802 \\ 803 \\to quit, press enter. 804 \\ 805 , .{ ssh_port, ssh_port, self.key_path }); 806 // portable stdin read via std.Io — equivalent to the 807 // std.posix.read(STDIN_FILENO, ...) that doesn't compile 808 // on windows. 809 var buf: [1]u8 = undefined; 810 var stdin_reader = std.Io.File.stdin().reader(self.io, &buf); 811 _ = stdin_reader.interface.takeByte() catch {}; 812 } 813 }; 814 815 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .key_path = key_path }); 816 } else { 817 var null_writer = std.Io.Writer.Discarding.init(&.{}); 818 const run_opts = hx.main.RunOpts{ .out = &null_writer.writer, .err = &null_writer.writer }; 819 820 const Runnable = struct { 821 io: std.Io, 822 allocator: std.mem.Allocator, 823 session: *ui.Session, 824 repo: *Repo, 825 826 pub fn run(self: @This(), web_port: u16, http_port: ?u16, ssh_port: ?u16) !void { 827 // launch the TUI; the footer points at whatever port was bound 828 self.session.web_port = web_port; 829 self.session.data.clone_http_port = http_port; 830 self.session.data.clone_ssh_port = ssh_port; 831 try hx.ui.run(self.io, self.allocator, self.session, self.repo); 832 } 833 }; 834 835 try srv.run(.xit, .{}, io, allocator, cwd_path, serve_options, run_opts.err, Runnable{ .io = io, .allocator = allocator, .session = &session, .repo = &repo }); 836 } 837 } 838 839 // words cycled through to pad generated lines into prose-like content 840 const scatter_words = [_][]const u8{ 841 "lorem", "ipsum", "dolor", "sit", "amet", "consectetur", 842 "quantum", "vector", "matrix", "buffer", "kernel", "socket", 843 "falcon", "otter", "badger", "walrus", "ferret", "marmot", 844 "scatter", "gather", "encode", "decode", "render", "commit", 845 }; 846 847 // write `path` as 40 lines of mostly-stable filler. revision `c` and file 848 // index `fi` shift which lines change and the words used, so each (c, fi) 849 // yields a distinct multi-hunk diff against the previous revision. untouched 850 // lines stay byte-identical across revisions, so a bump in `c` is a scatter of 851 // small hunks rather than a full rewrite. 852 fn writeScatterFile(io: std.Io, allocator: std.mem.Allocator, repo_dir: std.Io.Dir, path: []const u8, fi: usize, c: usize) !void { 853 const file = try repo_dir.createFile(io, path, .{}); 854 defer file.close(io); 855 var writer = std.Io.Writer.Allocating.init(allocator); 856 defer writer.deinit(); 857 for (0..40) |line| { 858 const start = writer.written().len; 859 if ((line + fi + c) % 8 == 0) { 860 const tag = scatter_words[(line + fi + c * 3) % scatter_words.len]; 861 try writer.writer.print("rev {d} {s}", .{ c, tag }); 862 } 863 const target = 40 + (line * 17 + fi * 23) % 70; 864 var w = line + fi; 865 while (true) : (w += 1) { 866 const word = scatter_words[w % scatter_words.len]; 867 const len = writer.written().len - start; 868 if (len >= target or len + 1 + word.len > 120) break; 869 if (len == 0) 870 try writer.writer.print("{s}", .{word}) 871 else 872 try writer.writer.print(" {s}", .{word}); 873 } 874 try writer.writer.writeByte('\n'); 875 } 876 try file.writeStreamingAll(io, writer.written()); 877 } 878 879 // tag the current HEAD as the next sequential version: v1, v2, and so on 880 fn addNextTag(repo: *rp.Repo(.xit, .{}), io: std.Io, allocator: std.mem.Allocator, n: *usize) !void { 881 var buf: [16]u8 = undefined; 882 const name = try std.fmt.bufPrint(&buf, "v{d}", .{n.*}); 883 _ = try repo.addTag(io, allocator, .{ .name = name }); 884 n.* += 1; 885 } 886 887 // recursively copy the contents of src_dir into dest_dir 888 fn copyDir(io: std.Io, src_dir: std.Io.Dir, dest_dir: std.Io.Dir) !void { 889 var iter = src_dir.iterate(); 890 while (try iter.next(io)) |entry| { 891 switch (entry.kind) { 892 .file => try src_dir.copyFile(entry.name, dest_dir, entry.name, io, .{}), 893 .directory => { 894 try dest_dir.createDirPath(io, entry.name); 895 var dest_entry_dir = try dest_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 896 defer dest_entry_dir.close(io); 897 var src_entry_dir = try src_dir.openDir(io, entry.name, .{ .access_sub_paths = true, .iterate = true, .follow_symlinks = false }); 898 defer src_entry_dir.close(io); 899 try copyDir(io, src_entry_dir, dest_entry_dir); 900 }, 901 else => {}, 902 } 903 } 904 } 905