╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
113 }; 114 } else blk: { 115 const resolved = (try ui.ResolvedRefOrOid(repo_kind, repo_opts).init(repo, io, aa, route.ref_or_oid, route.value.slice())) orelse return error.NotFound; 116 evt.PatchRev.validateOid(repo_opts.hash, &resolved.oid) catch return error.NotFound; 117 evt.PatchRev.validateOid(repo_opts.hash, route.base_oid.slice()) catch return error.NotFound; 118 119 var moment = try repo.core.latestMoment(); 120 const state = rp.Repo(repo_kind, repo_opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 121 // peel annotated tags before comparing their trees 122 var tip = try xit.object.Object(repo_kind, repo_opts).initCommit(state, io, gpa, &resolved.oid); 123 defer tip.deinit(); 124 break :blk .{ 125 route.base_oid.slice()[0..oid_len].*, 126 tip.oid, 127 Route{ .repo = .{ 128 .identity = identity, 129 .ref_or_oid = resolved.ref_or_oid, 130 .value = resolved.value, 131 .base_oid = try aa.dupe(u8, route.base_oid.slice()), 132 } }, 133 }; 134 }; 135 const path = try aa.dupe(u8, route.path.slice()); 136 return .{ 137 .route = diff_route, 138 .path = path, 139 .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), 140 }; 141 } 142 143 // render a window of the net diff between two commits or trees 144 pub fn render( 145 comptime repo_kind: rp.RepoKind, 146 comptime repo_opts: rp.RepoOpts(repo_kind), 147 io: std.Io, 148 gpa: std.mem.Allocator, 149 arena: std.mem.Allocator, 150 repo: *rp.Repo(repo_kind, repo_opts), 151 base_oid: ?*const [xit.hash.hexLen(repo_opts.hash)]u8, 152 head_oid: [xit.hash.hexLen(repo_opts.hash)]u8, 153 start: usize, 154 path: []const u8, 155 ) !Window { 156 var tree_diff = try repo.treeDiff(io, gpa, base_oid, &head_oid); 157 defer tree_diff.deinit(); 158 159 var file_iter = try repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }); 160 161 var hunks: std.ArrayList(Hunk) = .empty; 162 var index: usize = 0; // running hunk index across all files 163 var has_more = false; 164 165 file_loop: while (try file_iter.next()) |pair_val| { 166 var pair = pair_val; 167 defer pair.deinit(); 168 169 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 170 171 // an added or removed file shows only its path 172 const status: Status = if (pair.a.source == .nothing) .added else if (pair.b.source == .nothing) .removed else .changed; 173 if (status != .changed) { 174 const i = index; 175 index += 1; 176 if (i < start) continue; 177 if (hunks.items.len >= page_size) { 178 has_more = true; 179 break :file_loop; 180 } 181 try hunks.append(arena, .{ .path = try arena.dupe(u8, pair.path), .text = "", .prefixes = "", .status = status }); 182 continue; 183 } 184 185 var hunk_iter = try df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b); 186 defer hunk_iter.deinit(gpa); 187 188 // the path label rides on the first of this file's hunks we actually show. 189 var path_attached = false; 190 while (try hunk_iter.next(gpa)) |hunk_val| { 191 var hunk = hunk_val; 192 defer hunk.deinit(gpa); 193 194 const i = index; 195 index += 1; 196 if (i < start) continue; // before this window 197 if (hunks.items.len >= page_size) { 198 has_more = true; 199 break :file_loop; 200 } 201 const rendered = try renderHunk(repo_kind, repo_opts, arena, &hunk_iter, &hunk); 202 try hunks.append(arena, .{ 203 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 204 .text = rendered.text, 205 .prefixes = rendered.prefixes, 206 }); 207 path_attached = true; 208 } 209 } 210 211 return .{ .hunks = try hunks.toOwnedSlice(arena), .start = start, .has_more = has_more }; 212 } 213 214 // render edits with right-aligned line numbers, alongside each line's prefix 215 fn renderHunk( 216 comptime repo_kind: rp.RepoKind, 217 comptime repo_opts: rp.RepoOpts(repo_kind), 218 arena: std.mem.Allocator, 219 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 220 hunk: *df.Hunk(repo_kind, repo_opts), 221 ) !struct { text: []const u8, prefixes: []const u8 } { 222 var out: std.Io.Writer.Allocating = .init(arena); 223 defer out.deinit(); 224 var prefixes: std.ArrayList(u8) = .empty; 225 var max_num: usize = 1; 226 for (hunk.edits.items) |edit| { 227 const n = editLineNum(edit) + 1; 228 if (n > max_num) max_num = n; 229 } 230 const width = std.fmt.count("{d}", .{max_num}); 231 for (hunk.edits.items, 0..) |edit, i| { 232 const text = switch (edit) { 233 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 234 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 235 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 236 }; 237 const prefix: u8 = switch (edit) { 238 .eql => ' ', 239 .ins => '+', 240 .del => '-', 241 }; 242 if (i != 0) try out.writer.writeByte('\n'); 243 const num = editLineNum(edit) + 1; 244 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 245 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 246 try prefixes.append(arena, prefix); 247 } 248 return .{ .text = try out.toOwnedSlice(), .prefixes = try prefixes.toOwnedSlice(arena) }; 249 } 250 251 // the line number to show for an edit: the new-side number for kept and inserted 252 // lines, the old-side number for deletions (0-based in the diff machinery). 253 fn editLineNum(edit: df.Edit) usize { 254 return switch (edit) { 255 .eql => |e| e.new_line.num, 256 .ins => |e| e.new_line.num, 257 .del => |e| e.old_line.num, 258 }; 259 } 260 261 fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 262 if (data.path.len != 0) { 263 const status = if (data.window.hunks.len > 0) data.window.hunks[0].status else .changed; 264 try addSpans(allocator, box, &.{.{ .text = data.path }}, try data.route.fileLink(session.page_arena, data.path, status), status.label(), status.border()); 265 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 266 } 267 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 268 for (data.window.hunks) |hunk| { 269 if (data.path.len == 0) if (hunk.path) |path| { 270 // a changed file's path narrows the diff to it, the others open the file 271 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); 272 try addSpans(allocator, box, &.{.{ .text = path }}, link, hunk.status.label(), hunk.status.border()); 273 }; 274 if (hunk.text.len != 0) try addHunk(allocator, box, hunk); 275 } 276 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 277 } 278 279 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 280 try addSpans(allocator, box, &.{.{ .text = text }}, link, "", .single); 281 } 282 283 // one span per line, inserted lines green and deleted lines red. the text 284 // box copies the text, so the spans only live until init. 285 fn addHunk(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), hunk: Hunk) !void { 286 var spans: std.ArrayList(wgt.Span) = .empty; 287 defer spans.deinit(allocator); 288 var start: usize = 0; 289 for (hunk.prefixes) |prefix| { 290 // each span keeps its trailing newline so the text box still breaks there 291 const end = if (std.mem.indexOfScalarPos(u8, hunk.text, start, '\n')) |nl| nl + 1 else hunk.text.len; 292 const style: wgt.Style = switch (prefix) { 293 '+' => .{ .fg = .{ .ansi = .green } }, 294 '-' => .{ .fg = .{ .ansi = .red } }, 295 else => .{}, 296 }; 297 try spans.append(allocator, .{ .text = hunk.text[start..end], .style = style }); 298 start = end; 299 } 300 try addSpans(allocator, box, spans.items, "", "", .single); 301 } 302 303 fn addSpans(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8, label: []const u8, border: wgt.Border) !void { 304 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border = border, .round_corners = true, .wrap_kind = .none, .top_label = .{ .text = label } }); 305 errdefer tb.deinit(allocator); 306 tb.getFocus().mode = .all; 307 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 308 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 309 } 310 311 pub const View = struct { 312 scroll: wgt.Scroll(ui.Widget), 313 314 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 315 var view: View = blk: { 316 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 317 errdefer box.deinit(allocator); 318 break :blk .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }) }; 319 }; 320 errdefer view.deinit(allocator); 321 const box = view.inner(); 322 try data.appendWindow(allocator, session, box); 323 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 324 box.getFocus().child_id = box.children.keys()[0]; 325 return view; 326 } 327 328 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 329 self.scroll.deinit(allocator); 330 } 331 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 332 self.clearGrid(); 333 try self.scroll.build(allocator, constraint, root_focus); 334 } 335 pub fn clearGrid(self: *View) void { 336 self.scroll.clearGrid(); 337 } 338 pub fn getGrid(self: View) ?Grid { 339 return self.scroll.getGrid(); 340 } 341 pub fn getFocus(self: *View) *Focus { 342 return self.scroll.getFocus(); 343 } 344 pub fn atTop(self: *View) bool { 345 const box = self.inner(); 346 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 347 } 348 fn inner(self: *View) *wgt.Box(ui.Widget) { 349 return &self.scroll.child.box; 350 } 351 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 352 _ = allocator; 353 const sc = &self.scroll; 354 const box = self.inner(); 355 if (inp.rowDelta(key, @intCast(box.children.count()))) |delta| return ui.widget.moveRowFocus(box, sc, root_focus, delta); 356 switch (key) { 357 // scroll horizontally within the diff 358 .arrow_left => if (!ui.widget.moveInSelectedRow(box, root_focus, false) and sc.x > 0) { 359 sc.x -= 1; 360 sc.clampToContent(); 361 }, 362 .arrow_right => if (!ui.widget.moveInSelectedRow(box, root_focus, true)) { 363 sc.x += 1; 364 sc.clampToContent(); 365 }, 366 // Enter / a click on a "previous"/"next" row follow its "a:" link in 367 // the host (it reloads the adjacent window), so they don't reach here. 368 else => {}, 369 } 370 } 371 }; 372