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