╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
17 // one byte per line of text: the edit's +/-/space prefix 18 prefixes: []const u8, 19 }; 20 pub const Window = struct { 21 hunks: []const Hunk = &.{}, 22 start: usize = 0, 23 has_more: bool = false, 24 }; 25 26 route: Route, 27 path: []const u8 = "", 28 window: Window, 29 30 const Self = @This(); 31 32 pub const Route = union(enum) { 33 commit: struct { location: ui.RoutablePage.RepoLocation, oid: []const u8, base_oid: []const u8 = "" }, 34 repo: struct { identity: []const u8, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, base_oid: []const u8 }, 35 patchrev: struct { identity: []const u8, id: []const u8 }, 36 fork: struct { identity: []const u8, id: []const u8 }, 37 38 pub fn page(self: Route, start: usize, path: []const u8) ?ui.RoutablePage { 39 return switch (self) { 40 .commit => |c| c.location.commitsRoute(.object, c.oid, start, path, c.base_oid), 41 .repo => |r| ui.RoutablePage.repoDiffRoute(r.identity, r.ref_or_oid, r.value, start, path, r.base_oid), 42 .patchrev => |r| ui.RoutablePage.repoPatchRevDiffRoute(r.identity, r.id, start, path), 43 .fork => |f| ui.RoutablePage.forkDiffRoute(f.identity, f.id, start, path), 44 }; 45 } 46 47 fn link(self: Route, arena: *std.heap.ArenaAllocator, start: usize, path: []const u8) ![]const u8 { 48 const route = self.page(start, path) orelse return error.RouteTooLong; 49 return std.fmt.allocPrint(arena.allocator(), "a:{s}", .{try route.toUrl(arena)}); 50 } 51 }; 52 53 pub fn init( 54 comptime repo_kind: rp.RepoKind, 55 comptime repo_opts: rp.RepoOpts(repo_kind), 56 arena: *std.heap.ArenaAllocator, 57 repo: *rp.Repo(repo_kind, repo_opts), 58 io: std.Io, 59 gpa: std.mem.Allocator, 60 route: ui.RoutablePage.RepoDiffRoute, 61 ) !Self { 62 const aa = arena.allocator(); 63 const oid_len = comptime xit.hash.hexLen(repo_opts.hash); 64 const identity = try aa.dupe(u8, route.name.slice()); 65 const base_oid, const head_oid, const diff_route = if (route.patchrev_id.len != 0) blk: { 66 const id = evt.parseEventId(route.patchrev_id.slice()) catch return error.NotFound; 67 const record = (try evt.readFromRepo(evt.PatchRev, repo_kind, repo_opts, io, gpa, arena, repo, &id)) orelse return error.NotFound; 68 if (record.removed) return error.NotFound; 69 if (record.base_tree_oid.len != oid_len or record.head_tree_oid.len != oid_len) return error.NotFound; 70 break :blk .{ 71 record.base_tree_oid[0..oid_len].*, 72 record.head_tree_oid[0..oid_len].*, 73 Route{ .patchrev = .{ .identity = identity, .id = try aa.dupe(u8, route.patchrev_id.slice()) } }, 74 }; 75 } else blk: { 76 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, route.ref_or_oid, route.value.slice())) orelse return error.NotFound; 77 evt.PatchRev.validateOid(repo_opts.hash, &resolved.oid) catch return error.NotFound; 78 evt.PatchRev.validateOid(repo_opts.hash, route.base_oid.slice()) catch return error.NotFound; 79 80 var moment = try repo.core.latestMoment(); 81 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 82 // peel annotated tags before comparing their trees 83 var tip = try xit.object.Object(repo_kind, repo_opts).initCommit(state, io, gpa, &resolved.oid); 84 defer tip.deinit(); 85 break :blk .{ 86 route.base_oid.slice()[0..oid_len].*, 87 tip.oid, 88 Route{ .repo = .{ 89 .identity = identity, 90 .ref_or_oid = resolved.ref_or_oid, 91 .value = resolved.value, 92 .base_oid = try aa.dupe(u8, route.base_oid.slice()), 93 } }, 94 }; 95 }; 96 const path = try aa.dupe(u8, route.path.slice()); 97 return .{ 98 .route = diff_route, 99 .path = path, 100 .window = try render(repo_kind, repo_opts, io, gpa, aa, repo, if (std.mem.allEqual(u8, &base_oid, '0')) null else &base_oid, head_oid, route.start, path), 101 }; 102 } 103 104 // render a window of the net diff between two commits or trees 105 pub fn render( 106 comptime repo_kind: rp.RepoKind, 107 comptime repo_opts: rp.RepoOpts(repo_kind), 108 io: std.Io, 109 gpa: std.mem.Allocator, 110 arena: std.mem.Allocator, 111 repo: *rp.Repo(repo_kind, repo_opts), 112 base_oid: ?*const [xit.hash.hexLen(repo_opts.hash)]u8, 113 head_oid: [xit.hash.hexLen(repo_opts.hash)]u8, 114 start: usize, 115 path: []const u8, 116 ) !Window { 117 var tree_diff = try repo.treeDiff(io, gpa, base_oid, &head_oid); 118 defer tree_diff.deinit(); 119 120 var file_iter = try repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }); 121 122 var hunks: std.ArrayList(Hunk) = .empty; 123 var index: usize = 0; // running hunk index across all files 124 var has_more = false; 125 126 file_loop: while (try file_iter.next()) |pair_val| { 127 var pair = pair_val; 128 defer pair.deinit(); 129 130 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 131 132 var hunk_iter = try df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b); 133 defer hunk_iter.deinit(gpa); 134 135 // the path label rides on the first of this file's hunks we actually show. 136 var path_attached = false; 137 while (try hunk_iter.next(gpa)) |hunk_val| { 138 var hunk = hunk_val; 139 defer hunk.deinit(gpa); 140 141 const i = index; 142 index += 1; 143 if (i < start) continue; // before this window 144 if (hunks.items.len >= page_size) { 145 has_more = true; 146 break :file_loop; 147 } 148 const rendered = try renderHunk(repo_kind, repo_opts, arena, &hunk_iter, &hunk); 149 try hunks.append(arena, .{ 150 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 151 .text = rendered.text, 152 .prefixes = rendered.prefixes, 153 }); 154 path_attached = true; 155 } 156 } 157 158 return .{ .hunks = try hunks.toOwnedSlice(arena), .start = start, .has_more = has_more }; 159 } 160 161 // render edits with right-aligned line numbers, alongside each line's prefix 162 fn renderHunk( 163 comptime repo_kind: rp.RepoKind, 164 comptime repo_opts: rp.RepoOpts(repo_kind), 165 arena: std.mem.Allocator, 166 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 167 hunk: *df.Hunk(repo_kind, repo_opts), 168 ) !struct { text: []const u8, prefixes: []const u8 } { 169 var out: std.Io.Writer.Allocating = .init(arena); 170 defer out.deinit(); 171 var prefixes: std.ArrayList(u8) = .empty; 172 var max_num: usize = 1; 173 for (hunk.edits.items) |edit| { 174 const n = editLineNum(edit) + 1; 175 if (n > max_num) max_num = n; 176 } 177 const width = std.fmt.count("{d}", .{max_num}); 178 for (hunk.edits.items, 0..) |edit, i| { 179 const text = switch (edit) { 180 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 181 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 182 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 183 }; 184 const prefix: u8 = switch (edit) { 185 .eql => ' ', 186 .ins => '+', 187 .del => '-', 188 }; 189 if (i != 0) try out.writer.writeByte('\n'); 190 const num = editLineNum(edit) + 1; 191 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 192 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 193 try prefixes.append(arena, prefix); 194 } 195 return .{ .text = try out.toOwnedSlice(), .prefixes = try prefixes.toOwnedSlice(arena) }; 196 } 197 198 // the line number to show for an edit: the new-side number for kept and inserted 199 // lines, the old-side number for deletions (0-based in the diff machinery). 200 fn editLineNum(edit: df.Edit) usize { 201 return switch (edit) { 202 .eql => |e| e.new_line.num, 203 .ins => |e| e.new_line.num, 204 .del => |e| e.old_line.num, 205 }; 206 } 207 208 pub fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 209 if (data.path.len != 0) { 210 try addLink(allocator, box, data.path, ""); 211 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 212 } 213 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 214 for (data.window.hunks) |hunk| { 215 if (data.path.len == 0) if (hunk.path) |path| try addLink(allocator, box, path, try data.route.link(session.page_arena, 0, path)); 216 if (hunk.text.len != 0) try addHunk(allocator, box, hunk); 217 } 218 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 219 } 220 221 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 222 try addSpans(allocator, box, &.{.{ .text = text }}, link); 223 } 224 225 // one span per line, inserted lines green and deleted lines red. the text 226 // box copies the text, so the spans only live until init. 227 fn addHunk(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), hunk: Hunk) !void { 228 var spans: std.ArrayList(wgt.Span) = .empty; 229 defer spans.deinit(allocator); 230 var start: usize = 0; 231 for (hunk.prefixes) |prefix| { 232 // each span keeps its trailing newline so the text box still breaks there 233 const end = if (std.mem.indexOfScalarPos(u8, hunk.text, start, '\n')) |nl| nl + 1 else hunk.text.len; 234 const style: wgt.Style = switch (prefix) { 235 '+' => .{ .fg = .{ .ansi = .green } }, 236 '-' => .{ .fg = .{ .ansi = .red } }, 237 else => .{}, 238 }; 239 try spans.append(allocator, .{ .text = hunk.text[start..end], .style = style }); 240 start = end; 241 } 242 try addSpans(allocator, box, spans.items, ""); 243 } 244 245 fn addSpans(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8) !void { 246 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 247 errdefer tb.deinit(allocator); 248 tb.getFocus().mode = .all; 249 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 250 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 251 } 252 253 pub const View = struct { 254 scroll: wgt.Scroll(ui.Widget), 255 session: *ui.Session, 256 257 pub fn initEmpty(allocator: std.mem.Allocator, session: *ui.Session) !View { 258 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 259 errdefer box.deinit(allocator); 260 return .{ 261 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }), 262 .session = session, 263 }; 264 } 265 266 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 267 var view = try initEmpty(allocator, session); 268 errdefer view.deinit(allocator); 269 const box = view.inner(); 270 try data.appendWindow(allocator, session, box); 271 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 272 box.getFocus().child_id = box.children.keys()[0]; 273 return view; 274 } 275 276 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 277 self.scroll.deinit(allocator); 278 } 279 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 280 self.clearGrid(); 281 try self.scroll.build(allocator, constraint, root_focus); 282 } 283 pub fn clearGrid(self: *View) void { 284 self.scroll.clearGrid(); 285 } 286 pub fn getGrid(self: View) ?Grid { 287 return self.scroll.getGrid(); 288 } 289 pub fn getFocus(self: *View) *Focus { 290 return self.scroll.getFocus(); 291 } 292 pub fn atTop(self: *View) bool { 293 const box = self.inner(); 294 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 295 } 296 fn inner(self: *View) *wgt.Box(ui.Widget) { 297 return &self.scroll.child.box; 298 } 299 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 300 _ = allocator; 301 const sc = &self.scroll; 302 switch (key) { 303 // scroll horizontally within the diff 304 .arrow_left => if (!self.moveInRow(root_focus, false) and sc.x > 0) { 305 sc.x -= 1; 306 self.scroll.clampToContent(); 307 }, 308 .arrow_right => if (!self.moveInRow(root_focus, true)) { 309 sc.x += 1; 310 self.scroll.clampToContent(); 311 }, 312 .arrow_up => self.moveDiff(root_focus, -1), 313 .arrow_down => self.moveDiff(root_focus, 1), 314 .page_up => self.pageDiff(root_focus, -10), 315 .page_down => self.pageDiff(root_focus, 10), 316 .home => self.jumpDiff(root_focus, false), 317 .end => self.jumpDiff(root_focus, true), 318 // Enter / a click on a "previous"/"next" row follow its "a:" link in 319 // the host (it reloads the adjacent window), so they don't reach here. 320 .mouse => |mouse| switch (mouse.action) { 321 .scroll => |dir| self.moveDiff(root_focus, if (dir == .up) -1 else 1), 322 else => {}, 323 }, 324 else => {}, 325 } 326 } 327 328 // step across the selected row's boxes, false when the row has none that way. 329 pub fn moveInRow(self: *View, root_focus: *Focus, right: bool) bool { 330 const box = self.inner(); 331 const selected = box.getFocus().child_id orelse return false; 332 return switch ((box.children.getPtr(selected) orelse return false).widget) { 333 .box => |*row| ui.widget.moveInRow(row, root_focus, right), 334 else => false, 335 }; 336 } 337 338 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 339 // line count unless there is a new hunk visible (in which case it is a 340 // hunk count). on the web `delta` is always a hunk count. 341 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) void { 342 const box = self.inner(); 343 const keys = box.children.keys(); 344 if (keys.len == 0) return; 345 const cur: usize = if (box.getFocus().child_id) |c| (box.children.getIndex(c) orelse 0) else 0; 346 const target = @as(isize, @intCast(cur)) + delta; 347 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 348 349 if (self.session.is_terminal) { 350 if (in_range and self.hunkVisible(@intCast(target))) { 351 root_focus.setFocus(keys[@intCast(target)]); 352 return; 353 } 354 355 const sc = &self.scroll; 356 sc.y += delta * 5; // magnify because it's a line count 357 self.scroll.clampToContent(); 358 if (in_range and self.hunkVisible(@intCast(target))) { 359 root_focus.setFocus(keys[@intCast(target)]); 360 } 361 } else { 362 if (in_range) { 363 root_focus.setFocus(keys[@intCast(target)]); 364 } 365 } 366 } 367 368 // page a fixed number of lines, then focus the leading visible hunk 369 // (bottom-most when paging down, top-most when paging up). in the 370 // terminal, `delta` is a line count, and on the web it's a hunk count. 371 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) void { 372 if (self.session.is_terminal) { 373 const sc = &self.scroll; 374 sc.y += delta * 5; // magnify because it's a line count 375 self.scroll.clampToContent(); 376 self.focusVisible(root_focus, delta > 0); 377 } else { 378 const box = self.inner(); 379 const keys = box.children.keys(); 380 if (keys.len == 0) return; 381 const cur: isize = if (box.getFocus().child_id) |c| @intCast(box.children.getIndex(c) orelse 0) else 0; 382 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 383 root_focus.setFocus(keys[target]); 384 } 385 } 386 387 // jump to the first or last hunk. on the web the browser scrolls to the 388 // focused hunk; on the terminal pin the scroll to the top/bottom too. 389 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 390 const box = self.inner(); 391 const keys = box.children.keys(); 392 if (keys.len == 0) return; 393 if (self.session.is_terminal) { 394 const sc = &self.scroll; 395 sc.y = if (to_end) std.math.maxInt(isize) else 0; 396 self.scroll.clampToContent(); 397 } 398 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 399 } 400 401 // whether hunk `index` is at least partly within the diff viewport, per the 402 // last build's layout rects (content space) and the current scroll offset. 403 fn hunkVisible(self: *View, index: usize) bool { 404 const box = self.inner(); 405 const sc = &self.scroll; 406 const vp = sc.grid orelse return false; 407 const r = box.children.values()[index].rect orelse return false; 408 const top = sc.y; 409 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 410 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 411 } 412 413 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 414 // the bottom-most visible (for downward motion), else the top-most. 415 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 416 const box = self.inner(); 417 const sc = &self.scroll; 418 const vp = sc.grid orelse return; 419 const top = sc.y; 420 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 421 var chosen: ?usize = null; 422 for (box.children.keys(), box.children.values()) |id, *child| { 423 const r = child.rect orelse continue; // content-space layout rect 424 const r_top = r.y; 425 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 426 if (r_bot <= top or r_top >= bottom) continue; // not visible 427 chosen = id; 428 if (!prefer_last) break; // first visible 429 } 430 if (chosen) |id| root_focus.setFocus(id); 431 } 432 }; 433