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