summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/jj.zig84
1 files changed, 84 insertions, 0 deletions
diff --git a/core/jj.zig b/core/jj.zig
index 368d03a5..429ea9c9 100644
--- a/core/jj.zig
+++ b/core/jj.zig
@@ -195,6 +195,48 @@ pub fn looked(p: *Pardes, id: usize, dir: []const u8, text: []const u8, at: look
return true;
}
+// ---- what `Changes` reads (PARDES.md: pinned changes as lanes), the
+// data alone: flat arrays, no drawing yet ----
+
+/// Who last wrote each line of `path` (absolute) at `rev`: a change id
+/// (`change_id.short(8)`) a line, as `jj file annotate` says it.
+pub fn annotate(p: *Pardes, path: []const u8, rev: []const u8) Shown {
+ // A path, not a fileset (annotate takes one file as it is named).
+ return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dirOf(path), @as([]const []const u8, &.{ "file", "annotate", "-r", rev, "-T", "commit.change_id().short(8) ++ \"\\n\"", "--", std.Io.Dir.path.basename(path) }) });
+}
+
+/// The changes `revset` names, oldest first: a change id a line, as
+/// `annotate` writes them. Asked in directory `dir`, the file's.
+pub fn pinned(p: *Pardes, dir: []const u8, revset: []const u8) Shown {
+ return p.tape.site(.jj, p.gpa, runHost, .{ p.gpa, dir, @as([]const []const u8, &.{ "log", "--no-graph", "--reversed", "-r", revset, "-T", "change_id.short(8) ++ \"\\n\"" }) });
+}
+
+/// A line no pinned change wrote (`provenance`).
+pub const unpinned = std.math.maxInt(u16);
+
+/// Which pinned change wrote each line: a value a line, the change's place
+/// in `changes` (`pinned`'s answer, oldest first) or `unpinned`, from
+/// `annotated` (`annotate`'s answer). The caller's, in `gpa`.
+pub fn provenance(gpa: std.mem.Allocator, annotated: []const u8, changes: []const u8) error{ OutOfMemory, TooManyChanges }![]u16 {
+ var ids: std.ArrayList([]const u8) = .empty;
+ defer ids.deinit(gpa);
+ var it = std.mem.tokenizeScalar(u8, changes, '\n');
+ while (it.next()) |id| try ids.append(gpa, id);
+ if (ids.items.len >= unpinned) return error.TooManyChanges;
+ var out: std.ArrayList(u16) = .empty;
+ errdefer out.deinit(gpa);
+ var lines = std.mem.splitScalar(u8, annotated, '\n');
+ while (lines.next()) |id| {
+ // The empty text after the last newline is no line.
+ if (id.len == 0 and lines.peek() == null) break;
+ const at = for (ids.items, 0..) |pin, i| {
+ if (std.mem.eql(u8, pin, id)) break i;
+ } else unpinned;
+ try out.append(gpa, @intCast(at));
+ }
+ return out.toOwnedSlice(gpa);
+}
+
// ---- tests ----
const testing = std.testing;
@@ -335,3 +377,45 @@ test "a client replays the open of a file at a revision and ends equal" {
try testing.expectEqualStrings(Repo.two, r.core().panes[id].?.file.?.content);
try testing.expectEqual(pardes.tape.digestOf(a.core), pardes.tape.digestOf(r.core()));
}
+
+test "provenance: a value a line, which pinned change wrote it, from what jj answers" {
+ 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 = 80, .rows = 24 });
+ defer p.deinit();
+ // `two` wrote lines 1 and 3 of its text, `one` line 2.
+ const annotated = switch (annotate(p, repo.file, "@-")) {
+ .bytes => |b| b,
+ .refused => |why| {
+ defer gpa.free(why);
+ std.debug.print("annotate: {s}\n", .{why});
+ return error.Refused;
+ },
+ };
+ defer gpa.free(annotated);
+ for ([_][]const u8{ "@--::@-", "@-" }, [_][3]u16{ .{ 1, 0, 1 }, .{ 0, unpinned, 0 } }) |revset, want| {
+ const changes = switch (pinned(p, repo.dir, revset)) {
+ .bytes => |b| b,
+ .refused => |why| {
+ gpa.free(why);
+ return error.Refused;
+ },
+ };
+ defer gpa.free(changes);
+ const lines = try provenance(gpa, annotated, changes);
+ defer gpa.free(lines);
+ try testing.expectEqualSlices(u16, &want, lines);
+ }
+ // A revset jj refuses, in its words.
+ switch (pinned(p, repo.dir, "nosuch")) {
+ .bytes => |b| {
+ gpa.free(b);
+ return error.NotRefused;
+ },
+ .refused => |why| {
+ defer gpa.free(why);
+ try testing.expect(std.mem.indexOf(u8, why, "nosuch") != null);
+ },
+ }
+}