╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const obj = xit.object; 7 const mrg = xit.merge; 8 const srch_cmmt = @import("../../search_commit.zig"); 9 const srch = @import("../../search.zig"); 10 const xitui = xit.xitui; 11 const wgt = xitui.widget; 12 const layout = xitui.layout; 13 const Key = xitui.input.Key; 14 const Grid = xitui.grid.Grid; 15 const Focus = xitui.focus.Focus; 16 const inp = @import("../input.zig"); 17 18 // how many commits a page shows before a "next" link appears. 19 const page_size = 20; 20 // the largest message read even when it has fewer than the preview's line 21 // limit. 22 const max_message_size = 100 * 1024; 23 24 // one commit on the current page. 25 pub const Commit = struct { 26 oid: []const u8, 27 // what its diff is against: the first parent, all zeros for a root commit 28 parent_oid: []const u8, 29 message: []const u8, // trimmed, may be multi-line 30 // whether `message` is a shortened preview. 31 message_truncated: bool = false, 32 author: ui.Author = .unknown, 33 // .unknown when the committer is the author. 34 committer: ui.Author = .unknown, 35 // the committer timestamp, in unix seconds. 36 timestamp: i64, 37 // whether it has a second parent. 38 merge: bool = false, 39 stats: ?xit.patch.CommitStats = null, 40 }; 41 42 // where links and clone commands for this repository are rooted, and who views it. 43 handle: ui.RepoHandle, 44 // the resolved ref/oid this log walks from (the default branch when the route 45 // didn't name one), so the page can canonicalize its url to it. 46 ref_or_oid: ui.RoutablePage.RefOrOid, 47 ref_or_oid_value: []const u8, 48 base_oid: []const u8 = "", 49 commit_count: ?u64 = null, 50 commits: []const Commit, 51 // the first oid of the next page, or null when this is the last page. 52 next_start: ?[]const u8, 53 // the pane shows the whole message of the commit the log walks from. 54 message: bool = false, 55 // the decoded query this list holds the results of, or null for a plain log. 56 search: ?[]const u8 = null, 57 // whether the viewed ref has a commit index, so the search box shows and a 58 // search: route resolves. travels in the page json for the wasm view. 59 search_available: bool = false, 60 // an object view or a base-bounded list searches the default branch: that 61 // branch's url-encoded name, when it is indexed 62 default_branch: []const u8 = "", 63 // the repo's default branch has no commit yet, so the tab shows a placeholder 64 no_commits: bool = false, 65 66 const Self = @This(); 67 68 // walk the log for an opened repo. generic over the repo's backend and hash 69 // kind so the oid buffers it threads through match the repo's opts. `message` 70 // reads the walk root's message whole, up to the safety limit above. walks 71 // with the arena's backing allocator (transient; the commits we keep are 72 // duped into the page arena so they outlive it). 73 pub fn init( 74 comptime repo_kind: rp.RepoKind, 75 comptime repo_opts: rp.RepoOpts(repo_kind), 76 arena: *std.heap.ArenaAllocator, 77 repo: *rp.Repo(repo_kind, repo_opts), 78 io: std.Io, 79 gpa: std.mem.Allocator, 80 // the admin db's moment, for resolving author emails to user names (null 81 // in local mode, which has no users) 82 admin_moment: ?evt.AdminDB.HashMap(.read_only), 83 handle: ui.RepoHandle, 84 requested_ref_or_oid: ?ui.RoutablePage.RefOrOid, 85 requested_value: []const u8, 86 message: bool, 87 base_oid: []const u8, 88 search: []const u8, 89 from: []const u8, 90 ) !Self { 91 const aa = arena.allocator(); 92 const hex_len = ui.ResolvedRefOrOid(repo_kind, repo_opts).hex_len; 93 if (base_oid.len != 0) { 94 evt.PatchRev.validateOid(repo_opts.hash, base_oid) catch return error.NotFound; 95 } 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 // the commit message, trimmed and read into `aa`, plus whether its preview was 297 // cut short. 298 fn readMessage( 299 comptime repo_kind: rp.RepoKind, 300 comptime repo_opts: rp.RepoOpts(repo_kind), 301 aa: std.mem.Allocator, 302 commit_object: *xit.object.Object(repo_kind, repo_opts), 303 full_message: bool, 304 ) !struct { []const u8, bool } { 305 var message: std.ArrayList(u8) = .empty; 306 const byte_truncated = if (commit_object.readMessage(aa, &message, .limited(max_message_size))) |_| 307 false 308 else |err| switch (err) { 309 error.StreamTooLong => true, 310 else => |e| return e, 311 }; 312 const preview_end = if (full_message) null else ui.detailPreviewEnd(message.items); 313 const text = if (preview_end) |end| 314 message.items[0..end] 315 else if (byte_truncated) 316 trimIncompleteCodepoint(message.items) 317 else 318 message.items; 319 return .{ std.mem.trim(u8, text, " \t\r\n"), byte_truncated or preview_end != null }; 320 } 321 322 /// drop any incomplete UTF data from the end of a byte array 323 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 324 var i = bytes.len; 325 while (i > 0 and bytes.len - i < 4) { 326 i -= 1; 327 const byte = bytes[i]; 328 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 329 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 330 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 331 } 332 return bytes; 333 } 334 335 // a focusable, word-wrapping box holding a commit message. `bottom_label` 336 // marks a message the page only shows part of ("" when it's whole). 337 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 338 var tb = try wgt.TextBox.init(allocator, message, .{ 339 .border = .single, 340 .round_corners = true, 341 .wrap_kind = .word, 342 .top_label = .{ .text = " message " }, 343 .bottom_label = .{ .text = bottom_label }, 344 .detect_links = true, 345 }); 346 tb.getFocus().mode = .all; 347 return tb; 348 } 349 350 // the first line of a commit message, for the one-row list entries. 351 fn firstLine(message: []const u8) []const u8 { 352 const nl = std.mem.indexOfScalar(u8, message, '\n'); 353 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 354 } 355 356 pub const View = struct { 357 // a vertical stack: the clone url row on top, then a horizontal 358 // split with the commit list on the left and a pane on the right 359 // showing the selected commit's details. 360 box: wgt.Box(ui.Widget), // vert: [header_index] = clone url row, [content_index] = split 361 data: *const Self, 362 session: *ui.Session, 363 // the commit the pane currently shows (index into data.commits). 364 detailed_index: ?usize, 365 366 // the sub-header leads the box where there is one; local mode has none 367 const header_index: usize = 0; 368 // indices within the content box (the horizontal split). 369 const list_index: usize = 0; 370 const detail_index: usize = 1; 371 const list_max_width: usize = 35; 372 const detail_min_width: usize = 40; 373 374 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 375 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 376 errdefer outer.deinit(allocator); 377 378 if (data.no_commits) { 379 var center = try ui.widget.initNoCommits(allocator, session, data.handle); 380 errdefer center.deinit(allocator); 381 outer.getFocus().child_id = center.getFocus().id; 382 try outer.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 383 return .{ .box = outer, .data = data, .session = session, .detailed_index = null }; 384 } 385 386 // the search box and the clone url at the top. local mode has neither. 387 if (session.data.host_kind == .server) { 388 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.handle, " search ", "search", data.search, data.search_available); 389 errdefer header_view.deinit(allocator); 390 // a row taller than the header, leaving a blank line beneath it 391 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 392 } 393 394 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 395 errdefer box.deinit(allocator); 396 397 // the commit list (one focusable row each), plus a "next" link 398 { 399 var list_scroll = blk: { 400 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 401 errdefer list_box.deinit(allocator); 402 for (data.commits, 0..) |commit, index| { 403 // an in-page "ai:" anchor so a commit row is clickable with 404 // js off (the browser follows it, rooting the list there); 405 // with wasm the click just selects it and swaps the pane. 406 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 ""); 407 } 408 if (data.next_start) |next| { 409 try addRow(allocator, &list_box, "next →", try nextPageLink(session.page_arena, data, next), ""); 410 } 411 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 412 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 413 }; 414 errdefer list_scroll.deinit(allocator); 415 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 } }); 416 } 417 418 // the detail pane: a frame around a scroll of its rows 419 { 420 var detail_outer = blk: { 421 var detail_scroll = blk2: { 422 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 423 errdefer rows.deinit(allocator); 424 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 425 }; 426 errdefer detail_scroll.deinit(allocator); 427 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = .hidden, .direction = .vert }); 428 errdefer frame.deinit(allocator); 429 // the frame's selected child is its scroll, so the focus chain 430 // reaches a row (populateDetail points the scroll's inner box at 431 // one), letting focus recovery descend into the pane after 432 // it's laid out beside a too-narrow list. 433 frame.getFocus().child_id = detail_scroll.getFocus().id; 434 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 435 break :blk frame; 436 }; 437 errdefer detail_outer.deinit(allocator); 438 detail_outer.getFocus().mode = .mouse; 439 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 440 } 441 442 box.getFocus().child_id = box.children.keys()[if (data.commits.len > 0) detail_index else list_index]; 443 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 444 445 // focus lives in the split, except that search results start in the 446 // search box so the query can be refined right away. 447 outer.getFocus().child_id = outer.children.keys()[if (data.search != null) header_index else outer.children.count() - 1]; 448 449 return .{ 450 .box = outer, 451 .data = data, 452 .session = session, 453 .detailed_index = null, 454 }; 455 } 456 457 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8, bottom_label: []const u8) !void { 458 var row = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = .{ .text = bottom_label } }); 459 errdefer row.deinit(allocator); 460 row.getFocus().mode = .all; 461 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 462 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 463 } 464 465 // a focusable row following the "a:" `link`. 466 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 467 var tb = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 468 errdefer tb.deinit(allocator); 469 tb.getFocus().mode = .all; 470 tb.getFocus().kind = .{ .custom = link }; 471 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 472 } 473 474 // how much of the message a pane's box holds: a truncated preview links to 475 // the pane holding the whole thing, which is that link's destination. 476 const MessageBox = enum { preview, whole }; 477 478 // the selected commit's message. 479 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 480 // a truncated message keeps its box even with no whole line to show, 481 // since the box is what links to the whole thing. 482 if (commit.message.len == 0 and !commit.message_truncated) return; 483 const cut_short = commit.message_truncated and kind == .preview; 484 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 485 errdefer tb.deinit(allocator); 486 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data, commit.oid) }; 487 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 488 } 489 490 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 491 self.box.deinit(allocator); 492 } 493 494 // the split is last, after the sub-header where there is one 495 fn contentIndex(self: *View) usize { 496 return self.box.children.count() - 1; 497 } 498 499 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 500 return &self.box.children.values()[self.contentIndex()].widget.box; 501 } 502 503 fn header(self: *View) ?*ui.widget.SearchHeader { 504 switch (self.box.children.values()[header_index].widget) { 505 .search_header => |*header_view| return header_view, 506 else => return null, 507 } 508 } 509 510 fn headerActive(self: *View) bool { 511 const header_view = self.header() orelse return false; 512 return self.box.getFocus().child_id == header_view.getFocus().id; 513 } 514 515 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 516 return &self.contentBox().children.values()[list_index].widget.scroll; 517 } 518 519 fn listBox(self: *View) *wgt.Box(ui.Widget) { 520 return &self.listScroll().child.box; 521 } 522 523 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 524 return &self.contentBox().children.values()[detail_index].widget.box; 525 } 526 527 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 528 return &self.detailOuter().children.values()[0].widget.scroll; 529 } 530 531 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 532 return &self.detailScroll().child.box; 533 } 534 535 fn detailActive(self: *View) bool { 536 const content = self.contentBox(); 537 const cid = content.getFocus().child_id orelse return false; 538 return content.children.getIndex(cid) == detail_index; 539 } 540 541 // the selected commit's index, or null when the "next" row is selected. 542 fn selectedCommitIndex(self: *View) ?usize { 543 const lb = self.listBox(); 544 const cid = lb.getFocus().child_id orelse return null; 545 const idx = lb.children.getIndex(cid) orelse return null; 546 if (idx >= self.data.commits.len) return null; 547 return idx; 548 } 549 550 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 551 self.clearGrid(); 552 if (self.data.no_commits) return self.box.build(allocator, constraint, root_focus); 553 554 // swap the detail pane to the selected commit when it changes. 555 try self.refreshDetail(allocator); 556 557 // the selected list row shows a border (the focused TextBox upgrades it 558 // to a double border itself); the rest stay borderless. 559 const lb = self.listBox(); 560 for (lb.children.keys(), lb.children.values()) |id, *child| { 561 switch (child.widget) { 562 .text_box => |*tb| ui.widget.markSelected(tb, lb.getFocus().child_id == id), 563 else => {}, 564 } 565 } 566 567 // cap the list at list_max_width only while the detail pane fits beside it. 568 // the box drops the pane when the width can't hold both minimums, so when 569 // it's that narrow we lift the cap and let the list fill the whole width. 570 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 571 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 572 573 // stretch the detail pane across the rest of the width so it fills the area 574 // rather than shrinking to its content; its scroll fills the pane. when 575 // too narrow for both, it fills the whole width. 576 const detail_width: usize = if (constraint.max_size.width) |w| 577 (if (both_panes_fit) w - list_max_width else w) 578 else 579 detail_min_width; 580 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 581 582 // the web bounds the layout to the browser viewport like the terminal; 583 // each Scroll's web-native mode hands its full content to a real 584 // scrollable element, so we no longer build unbounded here. 585 // 586 // crossing panes only re-selects the content box's child (focusDetail / 587 // focusList); when the window is too narrow to show both, the pane that 588 // held focus is dropped here. the framework recovers focus after the 589 // top-level build by re-deriving it down the selected-child chain, so 590 // there's nothing to fix up afterward. 591 try self.box.build(allocator, constraint, root_focus); 592 } 593 594 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 595 const sel = self.selectedCommitIndex() orelse return; 596 if (self.detailed_index) |d| if (d == sel) return; 597 try self.populateDetail(allocator, sel); 598 self.detailed_index = sel; 599 } 600 601 fn populateDetail(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 602 const commit = self.data.commits[sel]; 603 const inner = self.detailInner(); 604 605 for (inner.children.values()) |*child| child.widget.deinit(allocator); 606 inner.children.clearAndFree(allocator); 607 inner.getFocus().child_id = null; 608 609 if (sel == 0 and self.data.message) { 610 try addLink(allocator, inner, "← back to commit", try commitsLink(self.session.page_arena, self.data, commit.oid)); 611 try self.addMessageBox(allocator, inner, commit, .whole); 612 } else { 613 const pa = self.session.page_arena; 614 { 615 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 616 errdefer row.deinit(allocator); 617 const diff_route = self.data.handle.location.commitDiffRoute(commit.oid, commit.parent_oid) orelse return error.RouteTooLong; 618 try addLink(allocator, &row, "view diff", try pa.allocator().print("a:{s}", .{try diff_route.toUrl(pa)})); 619 try addLink(allocator, &row, "view files at this commit", try filesObjectLink(pa, self.data.handle.location, commit.oid)); 620 row.getFocus().child_id = row.children.keys()[0]; 621 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 622 } 623 if (commit.merge) if (self.data.handle.location.commitsMergeRoute(commit.oid)) |route| { 624 try addLink(allocator, inner, "view commits from this merge", try pa.allocator().print("a:{s}", .{try route.toUrl(pa)})); 625 }; 626 { 627 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 628 errdefer row.deinit(allocator); 629 if (commit.author != .unknown) { 630 var tb = try ui.authorBox(allocator, pa, commit.author); 631 errdefer tb.deinit(allocator); 632 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 633 } 634 if (commit.committer != .unknown) { 635 var tb = try ui.authorBox(allocator, pa, commit.committer); 636 errdefer tb.deinit(allocator); 637 tb.options.top_label.text = " committer "; 638 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 639 } 640 const timestamp = try ui.timestamp.format(pa.allocator(), commit.timestamp, try self.session.nowSeconds()); 641 var tb = try wgt.TextBox.init(allocator, timestamp.relative, .{ .border = .single, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = timestamp.exact } }); 642 errdefer tb.deinit(allocator); 643 tb.getFocus().mode = .all; 644 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 645 row.getFocus().child_id = row.children.keys()[0]; 646 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 647 } 648 if (commit.stats) |stats| { 649 var text: std.Io.Writer.Allocating = .init(allocator); 650 defer text.deinit(); 651 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 652 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 653 const bytes_added = stats.bytes_added >= stats.bytes_removed; 654 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 655 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 656 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 657 }); 658 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 659 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border = .single, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = " stats " } }); 660 errdefer tb.deinit(allocator); 661 tb.getFocus().mode = .all; 662 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 663 } 664 try self.addMessageBox(allocator, inner, commit, .preview); 665 } 666 667 // point the pane at its first row so focus recovery can land here. 668 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 669 670 // reset the scroll to the top for the newly-shown commit: directly on the 671 // terminal (the wasm offset), and via a version bump on the web (so the 672 // renderer's scroll id changes and JS drops the preserved position). 673 const sc = self.detailScroll(); 674 sc.x = 0; 675 sc.y = 0; 676 sc.getFocus().version +%= 1; 677 } 678 679 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 680 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 681 // scrolling crosses the sub header boundary the same way arrows do 682 const direction = inp.vertDirection(key); 683 if (self.headerActive()) { 684 // active means there is one 685 const header_view = self.header() orelse unreachable; 686 if (direction == .down) { 687 root_focus.setFocus(self.contentBox().getFocus().id); 688 return; 689 } 690 if (key == .enter) if (try header_view.submittedText(allocator)) |text| { 691 defer allocator.free(text); 692 return self.submit(text); 693 }; 694 return header_view.input(allocator, key, root_focus); 695 } 696 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 697 if (self.detailActive()) { 698 const inner = self.detailInner(); 699 if (inp.rowDelta(key, @intCast(inner.children.count()))) |delta| { 700 ui.widget.moveRowFocus(inner, self.detailScroll(), root_focus, delta); 701 } else if (key == .arrow_left) { 702 if (!ui.widget.moveInSelectedRow(inner, root_focus, false)) self.focusList(root_focus); 703 } else if (key == .arrow_right) { 704 _ = ui.widget.moveInSelectedRow(inner, root_focus, true); 705 } 706 } else { 707 try self.listInput(allocator, key, root_focus); 708 } 709 } 710 711 // navigate to the typed query's results, pinned to the list's ref. an 712 // empty query leaves the results for the plain log. 713 fn submit(self: *View, text: []const u8) !void { 714 if (text.len == 0 and self.data.search == null) return; 715 // an object view or a base-bounded list searches the default branch, so the url says so 716 const route = if (self.data.default_branch.len != 0) 717 self.data.handle.location.commitsRoute(.branch, self.data.default_branch, "") 718 else 719 self.data.handle.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, self.data.base_oid); 720 try self.session.navigate((route orelse return).withSearch(text) orelse return); 721 } 722 723 fn listInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 724 // up/down (and the scroll wheel) move the selection a row; page up/down 725 // jump a fixed amount. right/Enter cross into the detail pane. Enter/clicks 726 // on the "next" row become navigation in the host before reaching here. 727 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 728 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 729 return; 730 } 731 switch (key) { 732 .enter => if (self.selectedCommitIndex() != null) 733 self.focusDetail(root_focus) 734 else if (self.data.next_start) |next| { 735 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 736 }, 737 .arrow_right => self.focusDetail(root_focus), 738 // when the window is too narrow to lay out the detail pane beside 739 // the list, a click on a row opens it like enter. the row was just 740 // selected, so the detail is swapped to it ahead of the build. 741 .mouse => |mouse| if (self.contentBox().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 742 try self.refreshDetail(allocator); 743 self.focusDetail(root_focus); 744 }, 745 else => {}, 746 } 747 } 748 749 // enter the detail pane. the host arrives here on right-arrow or Enter from the 750 // list. a pane with no rows can't be entered. setFocus handles the too-narrow 751 // case where the pane isn't laid out yet (it gets selected, then focused after 752 // the next build, landing on its remembered row). 753 fn focusDetail(self: *View, root_focus: *Focus) void { 754 if (self.detailInner().children.count() == 0) return; 755 root_focus.setFocus(self.detailOuter().getFocus().id); 756 } 757 758 // return to the list. 759 fn focusList(self: *View, root_focus: *Focus) void { 760 root_focus.setFocus(self.listScroll().getFocus().id); 761 } 762 763 pub fn clearGrid(self: *View) void { 764 self.box.clearGrid(); 765 } 766 767 pub fn getGrid(self: View) ?Grid { 768 return self.box.getGrid(); 769 } 770 771 pub fn getFocus(self: *View) *Focus { 772 return self.box.getFocus(); 773 } 774 775 fn contentAtTop(self: *View) bool { 776 if (self.detailActive()) { 777 const inner = self.detailInner(); 778 const cid = inner.getFocus().child_id orelse return true; 779 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 780 return first_focused and self.detailScroll().y == 0; 781 } 782 const lb = self.listBox(); 783 const cid = lb.getFocus().child_id orelse return true; 784 return lb.children.getIndex(cid) == 0; 785 } 786 787 // only the header's own row sits directly below the repository header. 788 pub fn atTop(self: *View) bool { 789 if (self.data.no_commits) return true; 790 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 791 return self.headerActive() or (!focusable and self.contentAtTop()); 792 } 793 794 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 795 const header_view = self.header() orelse return false; 796 return header_view.focusHeader(root_focus); 797 } 798 }; 799 800 // the "a:" navigation link for the commits page walking from commit `oid` within 801 // `data.handle.location`. 802 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 803 const route = data.handle.location.commitsRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 804 const url = try route.toUrl(page_arena); 805 return page_arena.allocator().print("a:{s}", .{url}); 806 } 807 808 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 809 // its query) and moves `from` along, so the search box survives paging. a fork 810 // or an object view roots the page at the commit itself. 811 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 812 if (data.handle.location != .repo or data.ref_or_oid == .object) { 813 return data.handle.location.commitsRoute(.object, oid, data.base_oid); 814 } 815 const route = data.handle.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, data.base_oid) orelse return null; 816 const paged = route.withFrom(oid) orelse return null; 817 return paged.withSearch(data.search orelse return paged); 818 } 819 820 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 821 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 822 const url = try route.toUrl(page_arena); 823 return page_arena.allocator().print("a:{s}", .{url}); 824 } 825 826 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 827 // directory, within `location`. 828 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 829 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 830 const url = try route.toUrl(page_arena); 831 return page_arena.allocator().print("a:{s}", .{url}); 832 } 833 834 // the in-page "ai:" anchor for selecting a commit, showing its whole message 835 // when `message` is set. the href is only followed with js off. 836 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, message: bool) ![]const u8 { 837 // a result roots the same results page at its commit, so the query 838 // survives following the row 839 if (data.search != null) { 840 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 841 return page_arena.allocator().print("ai:{s}", .{try route.toUrl(page_arena)}); 842 } 843 const route = (if (message) 844 data.handle.location.commitMessageRoute(.object, commit.oid, data.base_oid) 845 else 846 data.handle.location.commitsRoute(.object, commit.oid, data.base_oid)) orelse return error.RouteTooLong; 847 const url = try route.toUrl(page_arena); 848 return page_arena.allocator().print("ai:{s}", .{url}); 849 } 850 851 // the "a:" link to the page showing `oid`'s message on its own. 852 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 853 const route = data.handle.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 854 const url = try route.toUrl(page_arena); 855 return page_arena.allocator().print("a:{s}", .{url}); 856 } 857