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