diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-21 02:03:46 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-21 06:47:11 -0300 |
| commit | f9ff1acdf7cebf417a3c74d53f8e2d67cfbdbcc2 (patch) | |
| tree | 336dcffe051f046e732c41d48e58404f639548b1 /core/jj.zig | |
| parent | 2d25d58f8132c54ca0f853899d6b525c4ae892e4 (diff) | |
| download | pardes-f9ff1acdf7cebf417a3c74d53f8e2d67cfbdbcc2.tar.gz pardes-f9ff1acdf7cebf417a3c74d53f8e2d67cfbdbcc2.zip | |
layers: a file at a revision: the address path@rev and path@rev:line, resolved by a Look anywhere (a file really named so wins; the path must be a file), and At <rev> in a file's pane, open the file as jj has it at that revision in a pane named path@rev, coloured as its file (File.langPath), read-only (File.edit refuses a key's splice, edit.zig a whole text's; a bare Save says so); At in such a pane turns it to another revision in its place, bare it goes back to the file; what jj says is the session machine's: core/jj.zig runs it as a site of the step's tape (Site.jj: jj --ignore-working-copy file show, in the file's directory, the name a literal fileset), on an io of its own (the tape's spawns nothing: its allocator fails), its answer recorded, a replica replaying it and never running jj; jj's refusals in its words (At: Revision `x` doesn't exist, No such path, no jj); tests in a jj repository made in the test's directory: At's open, the read-only edits, the Look at a line, the turn in place, the refusals, and a client that replays the open of path@rev:2 ends with the session's digest; tui core-test 879/1037 (158 skipped); the tip gated whole: core-test 880/1038, shell-test 1080/1083, pane-test 183/184, behaviour-test 275/275 and --replay, process-test 0 of 145 failed, term 17/17, kitty 45/45, addons 5/5, consumer-test, both soaks diverged 0, unequal 0, unlisted 0, corpus through a client 22 scenes, daemon.py far, games, window-clients, clients x5, relink, launch, pair, gui, and --clients x2 on ReleaseSafe, macOS and FreeBSD cross-builds; daemon.py plain on ReleaseSafe times out at pair rows as main itself does (2 of 2 on main)
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'core/jj.zig')
| -rw-r--r-- | core/jj.zig | 337 |
1 files changed, 337 insertions, 0 deletions
diff --git a/core/jj.zig b/core/jj.zig new file mode 100644 index 00000000..368d03a5 --- /dev/null +++ b/core/jj.zig @@ -0,0 +1,337 @@ +//! A file at a revision, as jj shows it (PARDES.md "Changes on the source +//! view"): the address `path@rev`, with a place `path@rev:line`, which a +//! Look resolves anywhere, and `At <rev>` in a file's pane. +//! +//! What jj says is the session machine's: it is run out of the turn as a +//! site of the step's tape (`Site.jj`), its answer recorded; a replica +//! replays the answer and never runs jj. A revision's pane is an output +//! (`Output.Origin.revision`, its argument the revision), named +//! `path@rev`, coloured as its file (File.langPath), read-only (File.edit +//! refuses its text a change; a bare Save says why). +const std = @import("std"); +const pardes = @import("root.zig"); +const Pardes = pardes.Pardes; +const Pane = pardes.Pane; +const File = pardes.panes.File; +const Output = pardes.panes.Output; +const look = @import("look.zig"); +const layout = @import("layout.zig"); + +/// What jj answered: why not, in its own words (its first line, without +/// `Error: `), or the file's bytes. The caller's, in the allocator the site +/// was given. +pub const Shown = union(enum) { refused: []u8, bytes: []u8 }; + +/// The longest text a revision's pane takes. +const bytes_max = 256 << 20; + +/// `text`, not a file of its own, read as `path@rev`: the text before its +/// first `@` the path, the rest the revision (`main`, `@-`, `main@origin`). +pub fn split(text: []const u8) ?struct { path: []const u8, rev: []const u8 } { + const at = std.mem.indexOfScalar(u8, text, '@') orelse return null; + if (at == 0 or at + 1 == text.len or text[at - 1] == '/') return null; + return .{ .path = text[0..at], .rev = text[at + 1 ..] }; +} + +/// The file at `path` (absolute) at revision `rev`, as jj shows it. +pub fn show(p: *Pardes, path: []const u8, rev: []const u8) Shown { + const set = fileset(p.scratch.allocator(), path) catch return refusal(p.gpa, "out of memory"); + return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "show", "-r", rev, "--", set }) }); +} + +fn dirOf(path: []const u8) []const u8 { + return std.Io.Dir.path.dirname(path) orelse "/"; +} + +/// The name of `path` as a fileset of that one file in its directory, +/// taken literally whatever it is called. +fn fileset(arena: std.mem.Allocator, path: []const u8) ![]const u8 { + var set: std.ArrayList(u8) = .empty; + try set.appendSlice(arena, "file:\""); + for (std.Io.Dir.path.basename(path)) |c| { + if (c == '"' or c == '\\') try set.append(arena, '\\'); + try set.append(arena, c); + } + try set.append(arena, '"'); + return set.items; +} + +/// jj with `args`, run in `dir`: what it printed, or its refusal. The +/// working copy is left alone (`--ignore-working-copy`): an editor's read +/// makes no operation, and `@` is the working copy as jj last saw it. +fn runHost(gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) Shown { + // The tape's io spawns nothing (its allocator fails, its environment is + // empty): one of its own, with this process's environment. + var threaded: std.Io.Threaded = .init_single_threaded; + threaded.allocator = gpa; + threaded.environ = .{ .process_environ = .{ .block = .{ .slice = std.mem.span(std.c.environ) } } }; + threaded.environ_initialized = false; + const io = threaded.io(); + var argv: std.ArrayList([]const u8) = .empty; + defer argv.deinit(gpa); + argv.appendSlice(gpa, &.{ "jj", "--ignore-working-copy", "--color=never", "--no-pager" }) catch return refusal(gpa, "out of memory"); + argv.appendSlice(gpa, args) catch return refusal(gpa, "out of memory"); + // As Stream's children: this thread's mask is the child's. + const none = std.posix.sigemptyset(); + var kept: std.posix.sigset_t = undefined; + std.posix.sigprocmask(std.posix.SIG.SETMASK, &none, &kept); + var child = std.process.spawn(io, .{ .argv = argv.items, .cwd = .{ .path = dir }, .stdin = .ignore, .stdout = .pipe, .stderr = .pipe }) catch |err| { + std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); + return refusal(gpa, if (err == error.FileNotFound) "no jj" else @errorName(err)); + }; + std.posix.sigprocmask(std.posix.SIG.SETMASK, &kept, null); + defer child.kill(io); + // One after the other, on an io with no thread to read both at once: + // jj says a refusal in a line and nothing on stdout. + var out: std.ArrayList(u8) = .empty; + defer out.deinit(gpa); + var err: std.ArrayList(u8) = .empty; + defer err.deinit(gpa); + for ([_]std.Io.File{ child.stdout.?, child.stderr.? }, [_]*std.ArrayList(u8){ &out, &err }, [_]usize{ bytes_max, 64 << 10 }) |from, to, cap| { + var chunk: [64 * 1024]u8 = undefined; + while (to.items.len <= cap) { + const n = from.readStreaming(io, &.{&chunk}) catch break; + if (n == 0) break; + to.appendSlice(gpa, chunk[0..n]) catch return refusal(gpa, "out of memory"); + } + } + const term = child.wait(io) catch |e| return refusal(gpa, @errorName(e)); + if (out.items.len > bytes_max) return refusal(gpa, "too much to show"); + if (term == .exited and term.exited == 0) return .{ .bytes = out.toOwnedSlice(gpa) catch return refusal(gpa, "out of memory") }; + var first = std.mem.trim(u8, err.items[0 .. std.mem.indexOfScalar(u8, err.items, '\n') orelse err.items.len], " \t\r"); + if (std.mem.startsWith(u8, first, "Error: ")) first = first["Error: ".len..]; + return refusal(gpa, if (first.len > 0) first else "jj failed"); +} + +fn refusal(gpa: std.mem.Allocator, why: []const u8) Shown { + return .{ .refused = gpa.dupe(u8, why) catch &.{} }; +} + +/// A revision's pane: the file it is of (its name without `@rev`) and the +/// revision. Null for any other pane. +pub fn of(f: *const File.State) ?struct { path: []const u8, rev: []const u8 } { + const o = f.output orelse return null; + if (o.from != .revision) return null; + return .{ .path = f.path[0 .. f.path.len - o.arg().len - 1], .rev = o.arg() }; +} + +/// `path` (absolute) at `rev` in a pane: the one showing it focused, else +/// one made beside pane `near` (or, with `replace`, in its place: it goes), +/// wearing `near`'s layers; its cursor on line `line` (0: the first). Said +/// at `near` when jj refuses. The pane's id. +pub fn open(p: *Pardes, near: usize, path: []const u8, rev: []const u8, at: look.Spot, replace: bool) ?usize { + const name = std.fmt.allocPrint(p.scratch.allocator(), "{s}@{s}", .{ path, rev }) catch return null; + for (p.panes, 0..) |slot, i| if (slot) |shown| if (shown.file) |*f| if (of(f) != null and std.mem.eql(u8, f.path, name)) { + look.focusPaneLine(p, i, at, .center); + return i; + }; + if (rev.len > Output.max_arg) return refused(p, near, "a revision that long"); + const free = p.freeSlot() orelse { + p.reportError(.at(near), "At", error.NoPaneSlots); + return null; + }; + const bytes = switch (show(p, path, rev)) { + .refused => |why| { + defer p.gpa.free(why); + return refused(p, near, why); + }, + .bytes => |b| b, + }; + const pane = Output.open(p, free, std.Io.Dir.path.dirname(path) orelse "/", .revision, rev, bytes) catch |err| { + p.gpa.free(bytes); + p.reportError(.at(near), "At", err); + return null; + }; + const f = &pane.file.?; + const owned = p.gpa.dupe(u8, name) catch { + p.removePane(free, near) catch {}; + return null; + }; + p.gpa.free(f.path); + f.path = owned; + if (p.panes[near]) |from| pane.layers = from.layers; + layout.placeNew(p, near, near, free, .doc); + if (replace) p.removePane(near, free) catch |err| p.reportError(.at(free), "At", err); + layout.compute(p); + // Sized for its place before it is centred there. + p.sync(); + look.focusPaneLine(p, free, at, .center); + return free; +} + +fn refused(p: *Pardes, id: usize, why: []const u8) ?usize { + var line: pardes.Kind.Buf(512) = .{}; + p.reportFailure(id, line.print("At: {s}", .{why[0..@min(why.len, 480)]})); + return null; +} + +/// The `At` word (builtins.zig registers it): in a file's pane, that file +/// at revision `rev` beside it; in a revision's pane, the same file at +/// `rev` in its place, its cursor's line kept; bare there, the file. +pub fn word(c: pardes.builtins.Ctx) void { + const p = c.p; + const f = if (c.pane.file) |*file| file else return p.reportFailure(c.id, "At: only a file's pane takes it"); + const rev = std.mem.trim(u8, c.arg orelse "", " \t\r\n"); + const at: look.Spot = .{ .line = @intCast(@max(0, c.pane.body.cur_row) + 1) }; + if (of(f)) |r| { + if (rev.len == 0) return look.lookAt(p, c.id, r.path); + const path = p.scratch.allocator().dupe(u8, r.path) catch return; + _ = open(p, c.id, path, rev, at, true); + return; + } + if (f.output != null or pardes.filesystem.localPath(f.path) == null) return p.reportFailure(c.id, "At: only a file's pane takes it"); + if (rev.len == 0) return p.reportFailure(c.id, "At: which revision?"); + _ = open(p, c.id, f.path, rev, at, false); +} + +/// A Look at `text` (a path relative to `dir`, then `@rev`) that names no +/// file: a file at a revision, if the path is a file. False when it is not +/// one, for the look to go on. +pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look.Spot) bool { + const r = split(text) orelse return false; + const path = std.Io.Dir.path.resolvePosix(p.scratch.allocator(), &.{ dir, r.path }) catch return false; + if (pardes.filesystem.localPath(path) == null or pardes.exec.kindOf(p.tape, path) != .file) return false; + _ = open(p, id, path, r.rev, at, false); + return true; +} + +// ---- tests ---- + +const testing = std.testing; + +test "path@rev splits at its first @: a path before it, a revision after" { + const cases = [_]struct { []const u8, []const u8, []const u8 }{ + .{ "a.zig@main", "a.zig", "main" }, + .{ "core/File.zig@@-", "core/File.zig", "@-" }, + .{ "x.zig@main@origin", "x.zig", "main@origin" }, + }; + for (cases) |c| { + const got = split(c[0]).?; + try testing.expectEqualStrings(c[1], got.path); + try testing.expectEqualStrings(c[2], got.rev); + } + for ([_][]const u8{ "@p12", "a.zig@", "a.zig", "dir/@x" }) |text| try testing.expect(split(text) == null); +} + +/// A jj repository in a test's directory: `a.zig` committed as `one`, then +/// as `two`, and `three` in its working copy, never snapshotted. Null with +/// no jj to run. +const Repo = struct { + tmp: testing.TmpDir, + dir: []const u8, + file: []const u8, + + const one = "const one = 1;\nconst x = 2;\n"; + const two = "const two = 2;\nconst x = 2;\nconst y = 3;\n"; + const three = "const three = 3;\n"; + + fn init(gpa: std.mem.Allocator) !?Repo { + var tmp = testing.tmpDir(.{}); + errdefer tmp.cleanup(); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = try gpa.dupe(u8, buf[0..try tmp.dir.realPath(testing.io, &buf)]); + errdefer gpa.free(dir); + if (!try run(gpa, dir, &.{ "git", "init" })) return null; + for ([_][]const u8{ one, two }, [_][]const u8{ "one", "two" }) |text, message| { + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = text }); + if (!try run(gpa, dir, &.{ "commit", "-m", message })) return null; + } + try tmp.dir.writeFile(testing.io, .{ .sub_path = "a.zig", .data = three }); + return .{ .tmp = tmp, .dir = dir, .file = try std.fmt.allocPrint(gpa, "{s}/a.zig", .{dir}) }; + } + + fn run(gpa: std.mem.Allocator, dir: []const u8, args: []const []const u8) !bool { + var argv: std.ArrayList([]const u8) = .empty; + defer argv.deinit(gpa); + try argv.appendSlice(gpa, &.{ "jj", "--config=user.name=t", "--config=user.email=t@t", "--quiet" }); + try argv.appendSlice(gpa, args); + const r = std.process.run(gpa, testing.io, .{ .argv = argv.items, .cwd = .{ .path = dir } }) catch return false; + gpa.free(r.stdout); + gpa.free(r.stderr); + return r.term == .exited and r.term.exited == 0; + } + + fn deinit(r: *Repo, gpa: std.mem.Allocator) void { + gpa.free(r.file); + gpa.free(r.dir); + r.tmp.cleanup(); + } +}; + +fn told(p: *Pardes, id: usize) []const u8 { + const pane = p.panes[id].?; + return pane.msg[0..pane.msg_len]; +} + +test "At shows a file at a revision beside it, read-only, coloured as its file; a Look at path@rev:line goes to it; At in it turns it in place; jj's refusals are said" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa) orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + var line: [4200]u8 = undefined; + p.update(.{ .command = try std.fmt.bufPrint(&line, "Look {s}", .{repo.file}) }); + const file = p.view().active; + try testing.expectEqualStrings(Repo.three, p.panes[file].?.file.?.content); + p.update(.{ .command = "At @-" }); + const shown = p.view().active; + try testing.expect(shown != file); + const pane = p.panes[shown].?; + try testing.expectEqualStrings(Repo.two, pane.file.?.content); + const name = try std.fmt.allocPrint(gpa, "{s}@@-", .{repo.file}); + defer gpa.free(name); + try testing.expectEqualStrings(name, try pardes.tagline.tagPrefix(p, pane)); + try testing.expectEqualStrings(repo.file, File.langPath(&pane.file.?)); + try testing.expect(pane.colorAlgo() == .source); + // Read-only: a key's edit and a whole-text edit (dd) change nothing, and a bare Save says so. + for ("iz") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + for ("dd") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + try testing.expectEqualStrings(Repo.two, pane.file.?.content); + p.update(.{ .command = "Save" }); + try testing.expect(std.mem.indexOf(u8, told(p, shown), "is a revision, read-only") != null); + // From the file's pane, a Look at the address with a line goes there. + p.view().active = file; + p.update(.{ .command = "Look a.zig@@-:3" }); + try testing.expectEqual(shown, p.view().active); + try testing.expectEqual(@as(i32, 2), pane.body.cur_row); + // Turned to another revision in its place: one pane, the older text. + p.update(.{ .command = "At @--" }); + const turned = p.view().active; + try testing.expect(p.panes[shown] == null); + try testing.expectEqualStrings(Repo.one, p.panes[turned].?.file.?.content); + var n: usize = 0; + for (p.panes) |slot| if (slot) |s| if (s.file) |*f| { + n += @intFromBool(of(f) != null); + }; + try testing.expectEqual(@as(usize, 1), n); + // jj's refusals, in its words. + p.update(.{ .command = "At nosuch" }); + try testing.expect(std.mem.indexOf(u8, told(p, turned), "At: Revision `nosuch` doesn't exist") != null); + p.view().active = file; + p.update(.{ .command = "At" }); + try testing.expect(std.mem.indexOf(u8, told(p, file), "At: which revision?") != null); + // Bare in a revision's pane: the file. + p.view().active = turned; + p.update(.{ .command = "At" }); + try testing.expectEqual(file, p.view().active); +} + +test "a client replays the open of a file at a revision and ends equal" { + const gpa = testing.allocator; + var repo = try Repo.init(gpa) orelse return error.SkipZigTest; + defer repo.deinit(gpa); + const R = pardes.replica; + const a = try R.Authority.init(gpa, .{ .cols = 80, .rows = 24 }, .{}, false); + defer a.deinit(); + const r = try R.Replica.init(gpa, 1, try a.unit(0, 0), false); + defer r.deinit(); + var line: [4200]u8 = undefined; + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&line, "Look {s}@@-:2", .{repo.file}) })); + r.settle(); + try testing.expect(r.diverged == null); + const id = a.core.view().active; + try testing.expectEqualStrings(Repo.two, a.core.panes[id].?.file.?.content); + try testing.expectEqualStrings(Repo.two, r.core().panes[id].?.file.?.content); + try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core())); +} |
