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