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