╭ 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 const inp = @import("../input.zig"); 13 14 pub const page_size = 10; 15 pub const Hunk = struct { 16 path: ?[]const u8 = null, 17 text: []const u8, 18 // one byte per line of text: the edit's +/-/space prefix 19 prefixes: []const u8, 20 status: Status = .changed, 21 }; 22 pub const Status = enum { 23 added, 24 removed, 25 changed, 26 27 fn label(self: Status) []const u8 { 28 return switch (self) { 29 .added => " added ", 30 .removed => " removed ", 31 .changed => " changed ", 32 }; 33 } 34 }; 35 pub const Window = struct { 36 hunks: []const Hunk = &.{}, 37 start: usize = 0, 38 has_more: bool = false, 39 }; 40 41 route: Route, 42 path: []const u8 = "", 43 window: Window, 44 45 const Self = @This(); 46 47 pub const Route = union(enum) { 48 repo: struct { identity: []const u8, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8, base_oid: []const u8 }, 49 // `base_oid` is the revision's base commit, which the url leaves out 50 patchrev: struct { identity: []const u8, id: []const u8, base_oid: []const u8 }, 51 // `oid` names one commit's diff against `base_oid` ("" = the whole patch). 52 // `patch_base_oid` is the whole patch's base commit, which the url leaves out 53 fork: struct { identity: []const u8, id: []const u8, oid: []const u8 = "", base_oid: []const u8 = "", patch_base_oid: []const u8 = "" }, 54 55 pub fn page(self: Route, start: usize, path: []const u8) ?ui.RoutablePage { 56 return switch (self) { 57 .repo => |r| ui.RoutablePage.repoDiffRoute(r.identity, r.ref_or_oid, r.value, start, path, r.base_oid), 58 .patchrev => |r| ui.RoutablePage.repoPatchRevDiffRoute(r.identity, r.id, start, path), 59 .fork => |f| ui.RoutablePage.forkDiffRoute(f.identity, f.id, f.oid, f.base_oid, start, path), 60 }; 61 } 62 63 fn link(self: Route, arena: *std.heap.ArenaAllocator, start: usize, path: []const u8) ![]const u8 { 64 const route = self.page(start, path) orelse return error.RouteTooLong; 65 return arena.allocator().print("a:{s}", .{try route.toUrl(arena)}); 66 } 67 68 // a link to the file's content in the files view, from the base commit for a removed file 69 fn fileLink(self: Route, arena: *std.heap.ArenaAllocator, path: []const u8, status: Status) ![]const u8 { 70 const route = (if (status == .removed) switch (self) { 71 .repo => |r| ui.RoutablePage.repoFilesRoute(r.identity, .object, r.base_oid, path, 0), 72 .patchrev => |r| ui.RoutablePage.repoFilesRoute(r.identity, .object, r.base_oid, path, 0), 73 .fork => |f| ui.RoutablePage.forkFilesRoute(f.identity, f.id, if (f.base_oid.len != 0) f.base_oid else f.patch_base_oid, path, 0), 74 } else switch (self) { 75 .repo => |r| ui.RoutablePage.repoFilesRoute(r.identity, r.ref_or_oid, r.value, path, 0), 76 .patchrev => |r| ui.RoutablePage.repoPatchRevFilesRoute(r.identity, r.id, path, 0), 77 .fork => |f| ui.RoutablePage.forkFilesRoute(f.identity, f.id, f.oid, path, 0), 78 }) orelse return error.RouteTooLong; 79 return arena.allocator().print("a:{s}", .{try route.toUrl(arena)}); 80 } 81 }; 82 83 pub fn init( 84 comptime repo_kind: rp.RepoKind, 85 comptime repo_opts: rp.RepoOpts(repo_kind), 86 arena: *std.heap.ArenaAllocator, 87 repo: *rp.Repo(repo_kind, repo_opts), 88 io: std.Io, 89 gpa: std.mem.Allocator, 90 route: ui.RoutablePage.RepoDiffRoute, 91 ) !Self { 92 const aa = arena.allocator(); 93 const oid_len = comptime xit.hash.hexLen(repo_opts.hash); 94 const identity = try aa.dupe(u8, route.name.slice()); 95 const base_oid, const head_oid, const diff_route = if (route.patchrev_id.len != 0) blk: { 96 const id = evt.parseEventId(route.patchrev_id.slice()) catch return error.NotFound; 97 const record = (try evt.readFromRepo(evt.PatchRev, repo_kind, repo_opts, io, gpa, arena, repo, &id)) orelse return error.NotFound; 98 if (record.removed) return error.NotFound; 99 if (record.base_tree_oid.len != oid_len or record.head_tree_oid.len != oid_len) return error.NotFound; 100 break :blk .{ 101 record.base_tree_oid[0..oid_len].*, 102 record.head_tree_oid[0..oid_len].*, 103 Route{ .patchrev = .{ .identity = identity, .id = try aa.dupe(u8, route.patchrev_id.slice()), .base_oid = try aa.dupe(u8, record.event.base_oid) } }, 104 }; 105 } else blk: { 106 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, route.ref_or_oid, route.value.slice())) orelse return error.NotFound; 107 evt.PatchRev.validateOid(repo_opts.hash, &resolved.oid) catch return error.NotFound; 108 evt.PatchRev.validateOid(repo_opts.hash, route.base_oid.slice()) catch return error.NotFound; 109 110 var moment = try repo.core.latestMoment(); 111 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 112 // peel annotated tags before comparing their trees 113 var tip = try xit.object.Object(repo_kind, repo_opts).initCommit(state, io, gpa, &resolved.oid); 114 defer tip.deinit(); 115 break :blk .{ 116 route.base_oid.slice()[0..oid_len].*, 117 tip.oid, 118 Route{ .repo = .{ 119 .identity = identity, 120 .ref_or_oid = resolved.ref_or_oid, 121 .value = resolved.value, 122 .base_oid = try aa.dupe(u8, route.base_oid.slice()), 123 } }, 124 }; 125 }; 126 const path = try aa.dupe(u8, route.path.slice()); 127 return .{ 128 .route = diff_route, 129 .path = path, 130 .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), 131 }; 132 } 133 134 // render a window of the net diff between two commits or trees 135 pub fn render( 136 comptime repo_kind: rp.RepoKind, 137 comptime repo_opts: rp.RepoOpts(repo_kind), 138 io: std.Io, 139 gpa: std.mem.Allocator, 140 arena: std.mem.Allocator, 141 repo: *rp.Repo(repo_kind, repo_opts), 142 base_oid: ?*const [xit.hash.hexLen(repo_opts.hash)]u8, 143 head_oid: [xit.hash.hexLen(repo_opts.hash)]u8, 144 start: usize, 145 path: []const u8, 146 ) !Window { 147 var tree_diff = try repo.treeDiff(io, gpa, base_oid, &head_oid); 148 defer tree_diff.deinit(); 149 150 var file_iter = try repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }); 151 152 var hunks: std.ArrayList(Hunk) = .empty; 153 var index: usize = 0; // running hunk index across all files 154 var has_more = false; 155 156 file_loop: while (try file_iter.next()) |pair_val| { 157 var pair = pair_val; 158 defer pair.deinit(); 159 160 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 161 162 // an added or removed file shows only its path 163 const status: Status = if (pair.a.source == .nothing) .added else if (pair.b.source == .nothing) .removed else .changed; 164 if (status != .changed) { 165 const i = index; 166 index += 1; 167 if (i < start) continue; 168 if (hunks.items.len >= page_size) { 169 has_more = true; 170 break :file_loop; 171 } 172 try hunks.append(arena, .{ .path = try arena.dupe(u8, pair.path), .text = "", .prefixes = "", .status = status }); 173 continue; 174 } 175 176 var hunk_iter = try df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b); 177 defer hunk_iter.deinit(gpa); 178 179 // the path label rides on the first of this file's hunks we actually show. 180 var path_attached = false; 181 while (try hunk_iter.next(gpa)) |hunk_val| { 182 var hunk = hunk_val; 183 defer hunk.deinit(gpa); 184 185 const i = index; 186 index += 1; 187 if (i < start) continue; // before this window 188 if (hunks.items.len >= page_size) { 189 has_more = true; 190 break :file_loop; 191 } 192 const rendered = try renderHunk(repo_kind, repo_opts, arena, &hunk_iter, &hunk); 193 try hunks.append(arena, .{ 194 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 195 .text = rendered.text, 196 .prefixes = rendered.prefixes, 197 }); 198 path_attached = true; 199 } 200 } 201 202 return .{ .hunks = try hunks.toOwnedSlice(arena), .start = start, .has_more = has_more }; 203 } 204 205 // render edits with right-aligned line numbers, alongside each line's prefix 206 fn renderHunk( 207 comptime repo_kind: rp.RepoKind, 208 comptime repo_opts: rp.RepoOpts(repo_kind), 209 arena: std.mem.Allocator, 210 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 211 hunk: *df.Hunk(repo_kind, repo_opts), 212 ) !struct { text: []const u8, prefixes: []const u8 } { 213 var out: std.Io.Writer.Allocating = .init(arena); 214 defer out.deinit(); 215 var prefixes: std.ArrayList(u8) = .empty; 216 var max_num: usize = 1; 217 for (hunk.edits.items) |edit| { 218 const n = editLineNum(edit) + 1; 219 if (n > max_num) max_num = n; 220 } 221 const width = std.fmt.count("{d}", .{max_num}); 222 for (hunk.edits.items, 0..) |edit, i| { 223 const text = switch (edit) { 224 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 225 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 226 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 227 }; 228 const prefix: u8 = switch (edit) { 229 .eql => ' ', 230 .ins => '+', 231 .del => '-', 232 }; 233 if (i != 0) try out.writer.writeByte('\n'); 234 const num = editLineNum(edit) + 1; 235 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 236 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 237 try prefixes.append(arena, prefix); 238 } 239 return .{ .text = try out.toOwnedSlice(), .prefixes = try prefixes.toOwnedSlice(arena) }; 240 } 241 242 // the line number to show for an edit: the new-side number for kept and inserted 243 // lines, the old-side number for deletions (0-based in the diff machinery). 244 fn editLineNum(edit: df.Edit) usize { 245 return switch (edit) { 246 .eql => |e| e.new_line.num, 247 .ins => |e| e.new_line.num, 248 .del => |e| e.old_line.num, 249 }; 250 } 251 252 fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 253 if (data.path.len != 0) { 254 const status = if (data.window.hunks.len > 0) data.window.hunks[0].status else .changed; 255 try addSpans(allocator, box, &.{.{ .text = data.path }}, try data.route.fileLink(session.page_arena, data.path, status), status.label()); 256 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 257 } 258 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 259 for (data.window.hunks) |hunk| { 260 if (data.path.len == 0) if (hunk.path) |path| { 261 // a changed file's path narrows the diff to it, the others open the file 262 const link = if (hunk.status == .changed) try data.route.link(session.page_arena, 0, path) else try data.route.fileLink(session.page_arena, path, hunk.status); 263 try addSpans(allocator, box, &.{.{ .text = path }}, link, hunk.status.label()); 264 }; 265 if (hunk.text.len != 0) try addHunk(allocator, box, hunk); 266 } 267 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 268 } 269 270 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 271 try addSpans(allocator, box, &.{.{ .text = text }}, link, ""); 272 } 273 274 // one span per line, inserted lines green and deleted lines red. the text 275 // box copies the text, so the spans only live until init. 276 fn addHunk(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), hunk: Hunk) !void { 277 var spans: std.ArrayList(wgt.Span) = .empty; 278 defer spans.deinit(allocator); 279 var start: usize = 0; 280 for (hunk.prefixes) |prefix| { 281 // each span keeps its trailing newline so the text box still breaks there 282 const end = if (std.mem.indexOfScalarPos(u8, hunk.text, start, '\n')) |nl| nl + 1 else hunk.text.len; 283 const style: wgt.Style = switch (prefix) { 284 '+' => .{ .fg = .{ .ansi = .green } }, 285 '-' => .{ .fg = .{ .ansi = .red } }, 286 else => .{}, 287 }; 288 try spans.append(allocator, .{ .text = hunk.text[start..end], .style = style }); 289 start = end; 290 } 291 try addSpans(allocator, box, spans.items, "", ""); 292 } 293 294 fn addSpans(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8, label: []const u8) !void { 295 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none, .label = label }); 296 errdefer tb.deinit(allocator); 297 tb.getFocus().mode = .all; 298 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 299 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 300 } 301 302 pub const View = struct { 303 scroll: wgt.Scroll(ui.Widget), 304 305 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 306 var view: View = blk: { 307 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 308 errdefer box.deinit(allocator); 309 break :blk .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }) }; 310 }; 311 errdefer view.deinit(allocator); 312 const box = view.inner(); 313 try data.appendWindow(allocator, session, box); 314 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 315 box.getFocus().child_id = box.children.keys()[0]; 316 return view; 317 } 318 319 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 320 self.scroll.deinit(allocator); 321 } 322 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 323 self.clearGrid(); 324 try self.scroll.build(allocator, constraint, root_focus); 325 } 326 pub fn clearGrid(self: *View) void { 327 self.scroll.clearGrid(); 328 } 329 pub fn getGrid(self: View) ?Grid { 330 return self.scroll.getGrid(); 331 } 332 pub fn getFocus(self: *View) *Focus { 333 return self.scroll.getFocus(); 334 } 335 pub fn atTop(self: *View) bool { 336 const box = self.inner(); 337 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 338 } 339 fn inner(self: *View) *wgt.Box(ui.Widget) { 340 return &self.scroll.child.box; 341 } 342 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 343 _ = allocator; 344 const sc = &self.scroll; 345 const box = self.inner(); 346 if (inp.rowDelta(key, @intCast(box.children.count()))) |delta| return ui.widget.moveRowFocus(box, sc, root_focus, delta); 347 switch (key) { 348 // scroll horizontally within the diff 349 .arrow_left => if (!ui.widget.moveInSelectedRow(box, root_focus, false) and sc.x > 0) { 350 sc.x -= 1; 351 sc.clampToContent(); 352 }, 353 .arrow_right => if (!ui.widget.moveInSelectedRow(box, root_focus, true)) { 354 sc.x += 1; 355 sc.clampToContent(); 356 }, 357 // Enter / a click on a "previous"/"next" row follow its "a:" link in 358 // the host (it reloads the adjacent window), so they don't reach here. 359 else => {}, 360 } 361 } 362 }; 363