╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
80 gpa: std.mem.Allocator, 81 // the admin db's moment, for resolving author emails to user names (null 82 // in local mode, which has no users) 83 admin_moment: ?evt.AdminDB.HashMap(.read_only), 84 location: ui.RoutablePage.RepoLocation, 85 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 86 requested_value: []const u8, 87 content: ui.RoutablePage.RepoCommitsRoute.Content, 88 base_oid: []const u8, 89 search: []const u8, 90 from: []const u8, 91 ) !Self { 92 const aa = arena.allocator(); 93 const hex_len = ui.ResolvedRefOrOid(repo_kind, repo_opts).hex_len; 94 if (base_oid.len != 0) { 95 evt.PatchRev.validateOid(repo_opts.hash, base_oid) catch return error.NotFound; 96 } 97 if (from.len != 0) { 98 evt.PatchRev.validateOid(repo_opts.hash, from) catch return error.NotFound; 99 } 100 const query: ?[]const u8 = if (search.len == 0) null else std.Uri.percentDecodeInPlace(try aa.dupe(u8, search)); 101 102 // the walk root's message replaces its diff, so it's read whole and its 103 // hunks aren't rendered. the window and the filter are its diff's. 104 const root_message = std.meta.activeTag(content) == .message; 105 const root_start: usize = switch (content) { 106 .diff => |d| d.start, 107 .message => 0, 108 }; 109 const root_path: []const u8 = switch (content) { 110 .diff => |*d| d.path.slice(), 111 .message => "", 112 }; 113 114 // resolve the requested ref (or the default branch) to the commit oid to 115 // walk from. an explicitly named ref that doesn't resolve is a bad url 116 // (NotFound -> 404); the default-branch path falls through to empty. 117 var resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, requested_ref_or_oid, requested_value)) orelse { 118 if (requested_ref_or_oid != null) return error.NotFound; 119 return emptyResult(aa, location, .branch, requested_value, content, base_oid); 120 }; 121 122 var moment = repo.core.latestMoment() catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, content, base_oid); 123 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 124 125 // resolve annotated tags once, independently of the page's starting commit. 126 { 127 var tip = obj.Object(repo_kind, repo_opts).initCommit(state, io, gpa, &resolved.oid) catch { 128 if (query != null) return error.NotFound; 129 return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, content, base_oid); 130 }; 131 defer tip.deinit(); 132 resolved.oid = tip.oid; 133 } 134 135 // collect this page's commit metadata, plus a peek at the one after it (its 136 // oid is the next page's start). the diff for each is rendered afterward, so 137 // the log iterator is closed before opening per-commit diff iterators. 138 var buf: [page_size]Commit = undefined; 139 var oids: [page_size][hex_len]u8 = undefined; 140 var count: usize = 0; 141 var next_start: ?[]const u8 = null; 142 var search_available = false; 143 var searched = false; 144 145 // the index covers a repository's branch and tag tips, so a fork or an 146 // object view gets no search box and refuses a search: url. nor does a list 147 // bounded by a base, whose version also covers the commits before it. 148 if (comptime repo_kind == .xit) index: { 149 if (location != .repo or resolved.ref_or_oid == .object or base_oid.len != 0) break :index; 150 const index = (try cms.lookup(repo_opts, moment, &resolved.oid)) orelse break :index; 151 search_available = true; 152 const text = query orelse break :index; 153 154 var results = try srch.Query(rp.Repo(.xit, repo_opts).DB).init(index, aa, text); 155 if (from.len != 0) { 156 var start = obj.Object(.xit, repo_opts).initCommit(state, io, gpa, from[0..hex_len]) catch return error.NotFound; 157 defer start.deinit(); 158 const key = try cms.docKey(repo_opts.hash, start.content.commit.metadata.timestamp, &start.oid); 159 results.seek(&key); 160 } 161 while (try results.next()) |key| { 162 const oid = try cms.keyOid(repo_opts.hash, key); 163 if (count == page_size) { 164 next_start = try aa.dupe(u8, &oid); 165 break; 166 } 167 // a result whose object is gone is skipped rather than shown 168 var commit_object = obj.Object(.xit, repo_opts).init(state, io, gpa, &oid) catch continue; 169 defer commit_object.deinit(); 170 if (commit_object.content != .commit) continue; 171 @memcpy(&oids[count], &oid); 172 buf[count] = try commitEntry(repo_kind, repo_opts, arena, repo, io, gpa, admin_moment, &commit_object, root_message and count == 0); 173 count += 1; 174 } 175 searched = true; 176 } 177 if (query != null and !searched) return error.NotFound; 178 179 if (!searched) { 180 // paging keeps the ref and starts the walk at the url's commit 181 var start_oid = resolved.oid; 182 if (from.len != 0) @memcpy(&start_oid, from); 183 184 var iter = repo.log(io, gpa, .{ .start_oids = &.{start_oid}, .first_parent = true }) catch return emptyResult(aa, location, resolved.ref_or_oid, resolved.value, content, base_oid); 185 defer iter.deinit(); 186 while (try iter.next(gpa)) |commit_object| { 187 defer commit_object.deinit(); 188 // the base is a stopping point, not an ancestry exclusion 189 if (std.mem.eql(u8, &commit_object.oid, base_oid)) break; 190 if (count == page_size) { 191 next_start = try aa.dupe(u8, &commit_object.oid); 192 break; 193 } 194 @memcpy(&oids[count], &commit_object.oid); 195 buf[count] = try commitEntry(repo_kind, repo_opts, arena, repo, io, gpa, admin_moment, commit_object, root_message and count == 0); 196 count += 1; 197 } 198 } 199 200 // render each commit's diff (best effort: a failed diff leaves it empty). 201 // the diff machinery isn't wasm-clean and the wasm client never runs it (it 202 // renders from the snapshot), so gate it out of the wasm build. 203 if (!builtin.cpu.arch.isWasm()) { 204 for (buf[0..count], oids[0..count], 0..) |*commit, oid, i| { 205 if (root_message and i == 0) continue; 206 // the walk root shows the window the url asks for and its filter; 207 // the rest show their first window, unfiltered. 208 const window_start = if (i == 0) root_start else 0; 209 commit.window = renderCommitDiff(repo_kind, repo_opts, io, gpa, aa, repo, oid, window_start, if (i == 0) root_path else "") catch .{ .start = window_start }; 210 } 211 } 212 213 return .{ 214 .location = try location.dupe(aa), 215 .ref_or_oid = resolved.ref_or_oid, 216 .ref_or_oid_value = resolved.value, 217 .base_oid = try aa.dupe(u8, base_oid), 218 .commit_count = if (repo_kind == .xit and location == .repo) blk: { 219 if (base_oid.len == 0) { 220 const stats = (xit.patch.readCommitStats(repo_opts, state.extra.moment, &resolved.oid) catch null) orelse break :blk null; 221 break :blk stats.first_parent_depth; 222 } 223 break :blk evt.PatchRev.commitCount(repo_kind, repo_opts, state, io, gpa, base_oid, &resolved.oid) catch null; 224 } else null, 225 .commits = try aa.dupe(Commit, buf[0..count]), 226 .next_start = next_start, 227 .content = try pageContent(aa, content), 228 .search = query, 229 .search_available = search_available, 230 }; 231 } 232 233 // one commit's row: its message, author and stats. the diff window is rendered 234 // afterward, once the log iterator is closed. 235 fn commitEntry( 236 comptime repo_kind: rp.RepoKind, 237 comptime repo_opts: rp.RepoOpts(repo_kind), 238 arena: *std.heap.ArenaAllocator, 239 repo: *rp.Repo(repo_kind, repo_opts), 240 io: std.Io, 241 gpa: std.mem.Allocator, 242 admin_moment: ?evt.AdminDB.HashMap(.read_only), 243 commit_object: *obj.Object(repo_kind, repo_opts), 244 full_message: bool, 245 ) !Commit { 246 const aa = arena.allocator(); 247 const md = commit_object.content.commit.metadata; 248 const text, const truncated = try readMessage(repo_kind, repo_opts, aa, commit_object, full_message); 249 return .{ 250 .oid = try aa.dupe(u8, &commit_object.oid), 251 .date = try formatDate(aa, md.timestamp), 252 .message = text, 253 .message_truncated = truncated, 254 .author = try ui.Author.init(admin_moment, arena, md.author orelse ""), 255 .stats = if (repo_kind == .xit) try repo.commitStats(io, gpa, .{ .oid = &commit_object.oid }) else null, 256 }; 257 } 258 259 // an empty listing pinned to a ref, for the wasm / no-repo / unresolved paths. 260 pub fn emptyResult(aa: std.mem.Allocator, location: ui.RoutablePage.RepoLocation, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, content: ui.RoutablePage.RepoCommitsRoute.Content, base_oid: []const u8) !Self { 261 return .{ 262 .location = try location.dupe(aa), 263 .ref_or_oid = ref_or_oid, 264 .ref_or_oid_value = try aa.dupe(u8, value), 265 .base_oid = try aa.dupe(u8, base_oid), 266 .commit_count = null, 267 .commits = &.{}, 268 .next_start = null, 269 .content = try pageContent(aa, content), 270 }; 271 } 272 273 // the page's content for a route's, duped into `aa`. the diff window doesn't 274 // carry over: each commit holds the one its pane shows as `window.start`. 275 fn pageContent(aa: std.mem.Allocator, content: ui.RoutablePage.RepoCommitsRoute.Content) !Content { 276 return switch (content) { 277 .diff => |d| .{ .diff = .{ .path = try aa.dupe(u8, d.path.slice()) } }, 278 .message => .message, 279 }; 280 } 281 282 // render the window [start, start+len) of a commit's diff against its first 283 // parent into `arena`-owned hunks. start/has_more identify adjacent windows. 284 // a non-empty `path` filters to that file, so the window indexes its hunks. 285 fn renderCommitDiff( 286 comptime repo_kind: rp.RepoKind, 287 comptime repo_opts: rp.RepoOpts(repo_kind), 288 io: std.Io, 289 gpa: std.mem.Allocator, 290 arena: std.mem.Allocator, 291 repo: *rp.Repo(repo_kind, repo_opts), 292 oid: [xit.hash.hexLen(repo_opts.hash)]u8, 293 start: usize, 294 path: []const u8, 295 ) !Diff.Window { 296 const empty = Diff.Window{ .start = start }; 297 298 // load the commit so we can diff it against its first parent. 299 var commit_iter = repo.log(io, gpa, .{ .start_oids = &.{oid} }) catch return empty; 300 defer commit_iter.deinit(); 301 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 302 defer commit_object.deinit(); 303 304 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 305 306 return Diff.render(repo_kind, repo_opts, io, gpa, arena, repo, parent_maybe, oid, start, path); 307 } 308 309 // "YYYY-MM-DD" for a unix timestamp. 310 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 311 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 312 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 313 const month_day = year_day.calculateMonthDay(); 314 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 315 year_day.year, 316 month_day.month.numeric(), 317 month_day.day_index + 1, 318 }); 319 } 320 321 // the commit message, trimmed and read into `aa`, plus whether its preview was 322 // cut short. 323 fn readMessage( 324 comptime repo_kind: rp.RepoKind, 325 comptime repo_opts: rp.RepoOpts(repo_kind), 326 aa: std.mem.Allocator, 327 commit_object: *xit.object.Object(repo_kind, repo_opts), 328 full_message: bool, 329 ) !struct { []const u8, bool } { 330 var message: std.ArrayList(u8) = .empty; 331 const byte_truncated = if (commit_object.readMessage(aa, &message, .limited(max_message_size))) |_| 332 false 333 else |err| switch (err) { 334 error.StreamTooLong => true, 335 else => |e| return e, 336 }; 337 const preview_end = if (full_message) null else ui.detailPreviewEnd(message.items); 338 const text = if (preview_end) |end| 339 message.items[0..end] 340 else if (byte_truncated) 341 trimIncompleteCodepoint(message.items) 342 else 343 message.items; 344 return .{ std.mem.trim(u8, text, " \t\r\n"), byte_truncated or preview_end != null }; 345 } 346 347 /// drop any incomplete UTF data from the end of a byte array 348 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 349 var i = bytes.len; 350 while (i > 0 and bytes.len - i < 4) { 351 i -= 1; 352 const byte = bytes[i]; 353 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 354 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 355 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 356 } 357 return bytes; 358 } 359 360 // a focusable, word-wrapping box holding a commit message. `bottom_label` 361 // marks a message the page only shows part of ("" when it's whole). 362 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 363 var tb = try wgt.TextBox.init(allocator, message, .{ 364 .border_style = .single, 365 .rounded_corners = true, 366 .wrap_kind = .word, 367 .label = " message ", 368 .bottom_label = bottom_label, 369 }); 370 tb.getFocus().mode = .all; 371 return tb; 372 } 373 374 // the first line of a commit message, for the one-row list entries. 375 fn firstLine(message: []const u8) []const u8 { 376 const nl = std.mem.indexOfScalar(u8, message, '\n'); 377 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 378 } 379 380 pub const View = struct { 381 // a vertical stack: the clone url row on top, then a horizontal 382 // split with the commit list on the left and a diff pane on the right 383 // showing the selected commit's diff. 384 box: wgt.Box(ui.Widget), // vert: [header_index] = clone url row, [content_index] = split 385 data: *const Self, 386 session: *ui.Session, 387 // the commit whose diff the pane currently shows (index into data.commits). 388 diffed_index: ?usize, 389 390 const header_index: usize = 0; 391 const content_index: usize = 1; 392 // indices within the content box (the horizontal split). 393 const list_index: usize = 0; 394 const diff_index: usize = 1; 395 const list_max_width: usize = 35; 396 const diff_min_width: usize = 40; 397 398 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 399 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 400 errdefer outer.deinit(allocator); 401 402 // the search box and the clone url at the top. 403 { 404 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.location, " search ", "search", data.search, data.search_available); 405 errdefer header_view.deinit(allocator); 406 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 407 } 408 409 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 410 errdefer box.deinit(allocator); 411 412 // the commit list (one focusable row each), plus a "next" link 413 { 414 var list_scroll = blk: { 415 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 416 errdefer list_box.deinit(allocator); 417 for (data.commits, 0..) |commit, index| { 418 // an in-page "ai:" anchor so a commit row is clickable with 419 // js off (the browser follows it, rooting the list there); 420 // with wasm the click just selects it and swaps the diff pane. 421 const content = if (index == 0) data.content else Content{ .diff = .{} }; 422 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data, commit, content)); 423 } 424 if (data.next_start) |next| { 425 try addRow(allocator, &list_box, "next →", try nextPageLink(session.page_arena, data, next)); 426 } 427 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 428 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 429 }; 430 errdefer list_scroll.deinit(allocator); 431 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 432 } 433 434 // the diff pane — a frame around a scroll of the hunks 435 { 436 var diff_outer = blk: { 437 var diff_scroll = try Diff.View.initEmpty(allocator, session); 438 errdefer diff_scroll.deinit(allocator); 439 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 440 errdefer frame.deinit(allocator); 441 // the frame's selected child is its scroll, so the focus chain 442 // reaches a hunk (populateDiff points the scroll's inner box at 443 // one), letting focus recovery descend into the diff pane after 444 // it's laid out beside a too-narrow list. 445 frame.getFocus().child_id = diff_scroll.getFocus().id; 446 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .diff_view = diff_scroll }, .rect = null, .min_size = null }); 447 break :blk frame; 448 }; 449 errdefer diff_outer.deinit(allocator); 450 diff_outer.getFocus().mode = .mouse; 451 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 452 } 453 454 box.getFocus().child_id = box.children.keys()[list_index]; 455 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 456 457 // focus lives in the split, except that search results start in the 458 // search box so the query can be refined right away. 459 outer.getFocus().child_id = outer.children.keys()[if (data.search != null) header_index else content_index]; 460 461 return .{ 462 .box = outer, 463 .data = data, 464 .session = session, 465 .diffed_index = null, 466 }; 467 } 468 469 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 470 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 471 errdefer row.deinit(allocator); 472 row.getFocus().mode = .all; 473 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 474 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 475 } 476 477 // a focusable window-navigation row ("previous"/"next"). it's a link to this 478 // commit's route at `target_start`, so activating it (the host follows the 479 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 480 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 481 const link = try commitsLink(self.session.page_arena, self.data, oid, target_start, path); 482 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 483 errdefer tb.deinit(allocator); 484 tb.getFocus().mode = .all; 485 tb.getFocus().kind = .{ .custom = link }; 486 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 487 } 488 489 // a focusable row for the top of the diff pane linking to the files tab at 490 // this commit's tree, so its files are always viewable as of this object. 491 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 492 const link = try filesObjectLink(self.session.page_arena, self.data.location, oid); 493 var tb = try wgt.TextBox.init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 494 errdefer tb.deinit(allocator); 495 tb.getFocus().mode = .all; 496 tb.getFocus().kind = .{ .custom = link }; 497 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 498 } 499 500 // how much of the message a pane's box holds: a truncated preview links to 501 // the pane holding the whole thing, which is that link's destination. 502 const MessageBox = enum { preview, whole }; 503 504 // the selected commit's message. 505 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 506 // a truncated message keeps its box even with no whole line to show, 507 // since the box is what links to the whole thing. 508 if (commit.message.len == 0 and !commit.message_truncated) return; 509 const cut_short = commit.message_truncated and kind == .preview; 510 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 511 errdefer tb.deinit(allocator); 512 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data, commit.oid) }; 513 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 514 } 515 516 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 517 self.box.deinit(allocator); 518 } 519 520 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 521 return &self.box.children.values()[content_index].widget.box; 522 } 523 524 fn header(self: *View) *ui.widget.SearchHeader { 525 return &self.box.children.values()[header_index].widget.search_header; 526 } 527 528 fn headerActive(self: *View) bool { 529 return self.box.getFocus().child_id == self.header().getFocus().id; 530 } 531 532 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 533 return &self.contentBox().children.values()[list_index].widget.scroll; 534 } 535 536 fn listBox(self: *View) *wgt.Box(ui.Widget) { 537 return &self.listScroll().child.box; 538 } 539 540 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 541 return &self.contentBox().children.values()[diff_index].widget.box; 542 } 543 544 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 545 return &self.diffOuter().children.values()[0].widget.diff_view.scroll; 546 } 547 548 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 549 return &self.diffScroll().child.box; 550 } 551 552 fn diffActive(self: *View) bool { 553 const content = self.contentBox(); 554 const cid = content.getFocus().child_id orelse return false; 555 return content.children.getIndex(cid) == diff_index; 556 } 557 558 // what the pane shows for the commit at `sel`. the page's content only 559 // applies to the commit it walks from; the rest show their plain diff. 560 fn paneContent(self: *View, sel: usize) Self.Content { 561 return if (sel == 0) self.data.content else .{ .diff = .{} }; 562 } 563 564 // the selected commit's index, or null when the "next" row is selected. 565 fn selectedCommitIndex(self: *View) ?usize { 566 const lb = self.listBox(); 567 const cid = lb.getFocus().child_id orelse return null; 568 const idx = lb.children.getIndex(cid) orelse return null; 569 if (idx >= self.data.commits.len) return null; 570 return idx; 571 } 572 573 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 574 self.clearGrid(); 575 576 // swap the diff pane to the selected commit when it changes. 577 try self.refreshDiff(allocator); 578 579 // the selected list row shows a border (the focused TextBox upgrades it 580 // to a double border itself); the rest stay borderless. 581 const lb = self.listBox(); 582 for (lb.children.keys(), lb.children.values()) |id, *child| { 583 switch (child.widget) { 584 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 585 else => {}, 586 } 587 } 588 589 // cap the list at list_max_width only while the diff pane fits beside it. 590 // the box drops the diff when the width can't hold both minimums, so when 591 // it's that narrow we lift the cap and let the list fill the whole width. 592 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 593 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 594 595 // stretch the diff pane across the rest of the width so it fills the area 596 // rather than shrinking to its content; its scroll fills the pane. when 597 // too narrow for both, it fills the whole width. 598 const diff_width: usize = if (constraint.max_size.width) |w| 599 (if (both_panes_fit) w - list_max_width else w) 600 else 601 diff_min_width; 602 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 603 604 // the message is the pane's only wrapping row, and wrapping needs a 605 // bounded width, which the pane's scroll doesn't grant (it scrolls 606 // horizontally too). cap it to the pane, leaving room for the border 607 // and the scrollbar column. re-capped each build so it tracks resizes. 608 for (self.diffInner().children.values()) |*child| switch (child.widget) { 609 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 610 child.max_size = .{ .width = diff_width -| 3, .height = null }; 611 }, 612 else => {}, 613 }; 614 615 // the web bounds the layout to the browser viewport like the terminal; 616 // each Scroll's web-native mode hands its full content to a real 617 // scrollable element, so we no longer build unbounded here. 618 // 619 // crossing panes only re-selects the content box's child (focusDiff / 620 // focusList); when the window is too narrow to show both, the pane that 621 // held focus is dropped here. the framework recovers focus after the 622 // top-level build by re-deriving it down the selected-child chain, so 623 // there's nothing to fix up afterward. 624 try self.box.build(allocator, constraint, root_focus); 625 } 626 627 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 628 const sel = self.selectedCommitIndex() orelse return; 629 if (self.diffed_index) |d| if (d == sel) return; 630 try self.populateDiff(allocator, sel); 631 self.diffed_index = sel; 632 } 633 634 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 635 const commit = self.data.commits[sel]; 636 const inner = self.diffInner(); 637 638 for (inner.children.values()) |*child| child.widget.deinit(allocator); 639 inner.children.clearAndFree(allocator); 640 inner.getFocus().child_id = null; 641 642 switch (self.paneContent(sel)) { 643 .message => { 644 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 645 try self.addMessageBox(allocator, inner, commit, .whole); 646 }, 647 .diff => |d| { 648 if (d.path.len == 0) { 649 try self.addMessageBox(allocator, inner, commit, .preview); 650 if (commit.author != .unknown) { 651 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 652 errdefer tb.deinit(allocator); 653 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 654 } 655 if (commit.stats) |stats| { 656 var text: std.Io.Writer.Allocating = .init(allocator); 657 defer text.deinit(); 658 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 659 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 660 const bytes_added = stats.bytes_added >= stats.bytes_removed; 661 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 662 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 663 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 664 }); 665 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 666 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " stats " }); 667 errdefer tb.deinit(allocator); 668 tb.getFocus().mode = .all; 669 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 670 } 671 try self.addViewFilesLink(allocator, inner, commit.oid); 672 } 673 674 try (Diff{ 675 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid, .base_oid = self.data.base_oid } }, 676 .path = d.path, 677 .window = commit.window, 678 }).appendWindow(allocator, self.session, inner); 679 }, 680 } 681 682 // point the pane at its first row so focus recovery can land here. 683 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 684 685 // reset the scroll to the top for the newly-shown commit: directly on the 686 // terminal (the wasm offset), and via a version bump on the web (so the 687 // renderer's scroll id changes and JS drops the preserved position). 688 const sc = self.diffScroll(); 689 sc.x = 0; 690 sc.y = 0; 691 sc.getFocus().version +%= 1; 692 } 693 694 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 695 if (self.headerActive()) { 696 if (key == .arrow_down) { 697 root_focus.setFocus(self.contentBox().getFocus().id); 698 return; 699 } 700 if (key == .enter) if (try self.header().submittedText(allocator)) |text| { 701 defer allocator.free(text); 702 return self.submit(text); 703 }; 704 return self.header().input(allocator, key, root_focus); 705 } 706 if (key == .arrow_up and self.contentAtTop() and self.header().focusHeader(root_focus)) return; 707 if (self.diffActive()) { 708 if (key == .arrow_left and self.diffScroll().x == 0) { 709 self.focusList(root_focus); 710 } else { 711 try self.diffOuter().children.values()[0].widget.diff_view.input(allocator, key, root_focus); 712 } 713 } else { 714 try self.listInput(key, root_focus); 715 } 716 } 717 718 // navigate to the typed query's results, pinned to the list's ref. an 719 // empty query leaves the results for the plain log. 720 fn submit(self: *View, text: []const u8) !void { 721 if (text.len == 0 and self.data.search == null) return; 722 const route = self.data.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, 0, "", self.data.base_oid) orelse return; 723 try self.session.navigate(route.withSearch(text) orelse return); 724 } 725 726 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 727 // up/down (and the scroll wheel) move the selection a row; page up/down 728 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 729 // on the "next" row become navigation in the host before reaching here. 730 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 731 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 732 return; 733 } 734 switch (key) { 735 .enter => if (self.selectedCommitIndex() != null) 736 self.focusDiff(root_focus) 737 else if (self.data.next_start) |next| { 738 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 739 }, 740 .arrow_right => self.focusDiff(root_focus), 741 else => {}, 742 } 743 } 744 745 // enter the diff pane. the host arrives here on right-arrow or Enter from the 746 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 747 // case where the pane isn't laid out yet (it gets selected, then focused after 748 // the next build, landing on its remembered hunk). 749 fn focusDiff(self: *View, root_focus: *Focus) void { 750 if (self.diffInner().children.count() == 0) return; 751 root_focus.setFocus(self.diffOuter().getFocus().id); 752 } 753 754 // return to the list. 755 fn focusList(self: *View, root_focus: *Focus) void { 756 root_focus.setFocus(self.listScroll().getFocus().id); 757 } 758 759 pub fn clearGrid(self: *View) void { 760 self.box.clearGrid(); 761 } 762 763 pub fn getGrid(self: View) ?Grid { 764 return self.box.getGrid(); 765 } 766 767 pub fn getFocus(self: *View) *Focus { 768 return self.box.getFocus(); 769 } 770 771 fn contentAtTop(self: *View) bool { 772 if (self.diffActive()) { 773 const inner = self.diffInner(); 774 const cid = inner.getFocus().child_id orelse return true; 775 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 776 return first_focused and self.diffScroll().y == 0; 777 } 778 const lb = self.listBox(); 779 const cid = lb.getFocus().child_id orelse return true; 780 return lb.children.getIndex(cid) == 0; 781 } 782 783 // only the header's own row sits directly below the repository header. 784 pub fn atTop(self: *View) bool { 785 return self.headerActive() or (!self.header().hasFocusable() and self.contentAtTop()); 786 } 787 788 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 789 return self.header().focusHeader(root_focus); 790 } 791 }; 792 793 // the "a:" navigation link for the commits page walking from commit `oid` within 794 // `data.location`, windowing the selected commit's diff from hunk `start`, filtered 795 // to `path` ("" = every file). 796 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 797 const route = data.location.commitsRoute(.object, oid, start, path, data.base_oid) orelse return error.RouteTooLong; 798 const url = try route.toUrl(page_arena); 799 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 800 } 801 802 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 803 // its query) and moves `from` along, so the search box survives paging. a fork 804 // or an object view roots the page at the commit itself. 805 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 806 if (data.location != .repo or data.ref_or_oid == .object) { 807 return data.location.commitsRoute(.object, oid, 0, "", data.base_oid); 808 } 809 const route = data.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, 0, "", data.base_oid) orelse return null; 810 const paged = route.withFrom(oid) orelse return null; 811 return paged.withSearch(data.search orelse return paged); 812 } 813 814 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 815 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 816 const url = try route.toUrl(page_arena); 817 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 818 } 819 820 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 821 // directory, within `location`. 822 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 823 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 824 const url = try route.toUrl(page_arena); 825 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 826 } 827 828 // the in-page "ai:" anchor for selecting a commit with its current pane content 829 // and window. the href is only followed with js off. 830 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, content: Content) ![]const u8 { 831 // a result roots the same results page at its commit, so the query 832 // survives following the row 833 if (data.search != null) { 834 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 835 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 836 } 837 const route = (switch (content) { 838 .message => data.location.commitMessageRoute(.object, commit.oid, data.base_oid), 839 .diff => |diff| data.location.commitsRoute(.object, commit.oid, commit.window.start, diff.path, data.base_oid), 840 }) orelse return error.RouteTooLong; 841 const url = try route.toUrl(page_arena); 842 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 843 } 844 845 // the "a:" link to the page showing `oid`'s message on its own. 846 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 847 const route = data.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 848 const url = try route.toUrl(page_arena); 849 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 850 } 851