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