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