╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
18 var arena = std.heap.ArenaAllocator.init(allocator); 19 defer arena.deinit(); 20 const initial = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 21 try std.testing.expectEqual(1, initial.count); 22 for (0..25) |index| { 23 const value = try std.fmt.allocPrint(arena.allocator(), "{d}", .{index}); 24 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = value }); 25 } 26 const first = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 27 try std.testing.expectEqual(20, first.items.len); 28 try std.testing.expectEqual(25, first.items[0].index); 29 const second = try Undo.init(opts, &arena, &repo, null, "", first.next, .{ .location = .{ .repo = "" } }); 30 try std.testing.expectEqual(6, second.items.len); 31 try std.testing.expectEqual(5, second.items[0].index); 32 try std.testing.expectEqual(0, second.items[5].index); 33 try std.testing.expectEqual(null, second.next); 34 try std.testing.expectError(error.NotFound, Undo.init(opts, &arena, &repo, null, "", 26, .{ .location = .{ .repo = "" } })); 35 try std.testing.expectError(error.InvalidHistoryIndex, repo.undo(io, 0)); 36 try std.testing.expectError(error.InvalidHistoryIndex, repo.undo(io, 26)); 37 38 // undo the newest change, then a range, then undo that undo. 39 try repo.undo(io, 25); 40 try expectConfig(&repo, "23"); 41 try repo.undo(io, 10); 42 try expectConfig(&repo, "8"); 43 const undone = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 44 try std.testing.expectEqual(28, undone.count); 45 try std.testing.expectEqualStrings("undo", undone.items[0].action); 46 try repo.undo(io, undone.items[0].index); 47 try expectConfig(&repo, "23"); 48 } 49 50 fn expectConfig(repo: anytype, expected: []const u8) !void { 51 var config = try repo.listConfig(io, allocator); 52 defer config.deinit(); 53 const section = config.local_sections.get("undo") orelse return error.MissingSection; 54 try std.testing.expectEqualStrings(expected, section.get("value") orelse return error.MissingValue); 55 } 56 57 test "repo undo visibility and fresh owner authorization" { 58 var temp = std.testing.tmpDir(.{}); 59 defer temp.cleanup(); 60 var arena = std.heap.ArenaAllocator.init(allocator); 61 defer arena.deinit(); 62 const aa = arena.allocator(); 63 const base = try temp.dir.realPathFileAlloc(io, ".", aa); 64 const admin_path = try std.fs.path.join(aa, &.{ base, "admin" }); 65 const users_dir = try std.fs.path.join(aa, &.{ base, "users" }); 66 var admin = try xit.repo.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 67 defer admin.deinit(io, allocator); 68 const owner = [_]u8{1} ** evt.event_id_size; 69 const writer = [_]u8{2} ** evt.event_id_size; 70 const reader = [_]u8{3} ** evt.event_id_size; 71 const repo_id = [_]u8{4} ** evt.event_id_size; 72 const author: evt.CommitAuthor = .{ .name = "test", .email = "test@example.test" }; 73 const writer_hex = std.fmt.bytesToHex(writer, .lower); 74 var repo_event: evt.Repo = .{ .user_id = &owner, .name = "demo", .description = "", .read_access = .public, .write_user_ids = &writer_hex }; 75 const seed = [_]evt.EventWithId{ 76 .{ .id = std.fmt.bytesToHex(owner, .lower), .author = author, .event = .{ .user = .{ .name = "alice", .email = "alice@example.test", .password_hash = "unused" } } }, 77 .{ .id = writer_hex, .author = author, .event = .{ .user = .{ .name = "writer", .email = "writer@example.test", .password_hash = "unused" } } }, 78 .{ .id = std.fmt.bytesToHex(reader, .lower), .author = author, .event = .{ .user = .{ .name = "reader", .email = "reader@example.test", .password_hash = "unused" } } }, 79 }; 80 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &seed); 81 var user_repo = try evt.initUserRepo(io, allocator, try evt.userRepoPath(aa, users_dir, &owner)); 82 defer user_repo.deinit(io, allocator); 83 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{.{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = repo_event } }}); 84 const path = try evt.repoPath(aa, users_dir, &owner, &repo_id); 85 { 86 var repo = try xit.repo.Repo(.xit, .{}).init(io, allocator, .{ .path = path }); 87 defer repo.deinit(io, allocator); 88 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = "first" }); 89 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = "second" }); 90 } 91 for ([_]?[]const u8{ &owner, &writer, &reader, null }, [_]bool{ true, true, false, false }) |user, allowed| { 92 var session = try ui.Session.init(&arena, &admin, .{ .user_id = user }); 93 session.io = io; 94 session.users_dir = users_dir; 95 const files = ui.RoutablePage.repoFilesRoute("alice/demo", null, "", "", 0).?; 96 session.data.current_page = files; 97 const page = try ui.Repo.init(&arena, &session, files); 98 try std.testing.expectEqual(allowed, page.undo != null); 99 if (page.undo) |undo| try std.testing.expectEqual(user != null and std.mem.eql(u8, user.?, &owner), undo.handle.canUndo()); 100 var root = ui.Widget{ .repo = try ui.Repo.View.init(allocator, &page, &session) }; 101 defer root.deinit(allocator); 102 try root.build(allocator, constraint, root.getFocus()); 103 const html = try @import("../web.zig").generateHtml(allocator, &root, &session); 104 defer allocator.free(html); 105 try std.testing.expectEqual(allowed, std.mem.indexOf(u8, html, "/repo/alice/demo/undo") != null); 106 const route = ui.RoutablePage.repoUndoRoute("alice/demo", null).?; 107 if (allowed) { 108 _ = try ui.Repo.init(&arena, &session, route); 109 } else { 110 try std.testing.expectError(error.NotFound, ui.Repo.init(&arena, &session, route)); 111 } 112 if (user == null or !std.mem.eql(u8, user.?, &owner)) { 113 try std.testing.expectError(error.Forbidden, Undo.perform(allocator, &session, ui.RoutablePage.repoUndoRoute("alice/demo", 2).?.repo_undo)); 114 } 115 } 116 var session = try ui.Session.init(&arena, &admin, .{ .user_id = &owner }); 117 session.io = io; 118 session.users_dir = users_dir; 119 try Undo.perform(allocator, &session, ui.RoutablePage.repoUndoRoute("alice/demo", 2).?.repo_undo); 120 try std.testing.expectEqual(null, session.next_page.?.repo_undo.index); 121 { 122 var repo = try xit.repo.Repo(.xit, .{}).open(io, allocator, .{ .path = path }); 123 defer repo.deinit(io, allocator); 124 try expectConfig(&repo, "first"); 125 const data = try Undo.init(.{}, &arena, &repo, null, "alice/demo", null, .{ .location = .{ .repo = "" } }); 126 try std.testing.expectEqualStrings("undo", data.items[0].action); 127 } 128 // keep the old session/page moment, but transfer ownership to the writer. 129 repo_event.user_id = &writer; 130 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{.{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = repo_event } }}); 131 session.next_page = null; 132 Undo.handleRequest(allocator, &session, ui.RoutablePage.repoUndoRoute("alice/demo", 1).?.repo_undo); 133 try std.testing.expectEqualStrings("Forbidden", session.data.undo_failure.?); 134 try std.testing.expectEqual(null, session.next_page); 135 136 // public-write repos still require login for history and undo. 137 repo_event.user_id = &owner; 138 repo_event.write_access = .public; 139 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{.{ .id = std.fmt.bytesToHex(repo_id, .lower), .author = author, .event = .{ .repo = repo_event } }}); 140 var anonymous = try ui.Session.init(&arena, &admin, .{}); 141 anonymous.io = io; 142 anonymous.users_dir = users_dir; 143 try std.testing.expectError(error.NotFound, ui.Repo.init(&arena, &anonymous, ui.RoutablePage.repoUndoRoute("alice/demo", null).?)); 144 } 145 146 test "local git has no undo and local xit can undo and refresh" { 147 inline for ([_]xit.repo.RepoKind{ .git, .xit }) |kind| { 148 var temp = std.testing.tmpDir(.{}); 149 defer temp.cleanup(); 150 var arena = std.heap.ArenaAllocator.init(allocator); 151 defer arena.deinit(); 152 const path = try temp.dir.realPathFileAlloc(io, ".", arena.allocator()); 153 { 154 var repo = try xit.repo.Repo(kind, .{}).init(io, allocator, .{ .path = path }); 155 defer repo.deinit(io, allocator); 156 try repo.addConfig(io, allocator, .{ .name = "undo.value", .value = "first" }); 157 } 158 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .io = io, .local = .{ .path = path, .repo_kind = kind }, .data = .{ .host_kind = .local } }; 159 const files = ui.RoutablePage.repoFilesRoute("", null, "", "", 0).?; 160 const data = try ui.Repo.init(&arena, &session, files); 161 try std.testing.expectEqual(kind == .xit, data.undo != null); 162 if (kind == .git) { 163 try std.testing.expectError(error.NotFound, ui.Repo.init(&arena, &session, ui.RoutablePage.repoUndoRoute("", null).?)); 164 try std.testing.expectError(error.NotFound, Undo.execute(io, allocator, session.local.?, 1)); 165 } else { 166 Undo.handleRequest(allocator, &session, ui.RoutablePage.repoUndoRoute("", 999).?.repo_undo); 167 try std.testing.expectEqualStrings("InvalidHistoryIndex", session.data.undo_failure.?); 168 try std.testing.expectEqual(null, session.next_page); 169 Undo.handleRequest(allocator, &session, ui.RoutablePage.repoUndoRoute("", 1).?.repo_undo); 170 try std.testing.expectEqual(null, session.data.undo_failure); 171 const refreshed = try ui.Repo.init(&arena, &session, session.next_page.?); 172 try std.testing.expectEqualStrings("undo", refreshed.undo.?.items[0].action); 173 } 174 } 175 } 176 177 test "commit undo records use the formatted description" { 178 var temp = std.testing.tmpDir(.{}); 179 defer temp.cleanup(); 180 var arena = std.heap.ArenaAllocator.init(allocator); 181 defer arena.deinit(); 182 const path = try temp.dir.realPathFileAlloc(io, ".", arena.allocator()); 183 const opts: xit.repo.RepoOpts(.xit) = .{ .is_test = true }; 184 var repo = try xit.repo.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 185 defer repo.deinit(io, allocator); 186 _ = try repo.commitAtRef(io, allocator, .{ .message = "example commit" }, null, .{ .kind = .head, .name = "master" }); 187 const data = try Undo.init(opts, &arena, &repo, null, "", null, .{ .location = .{ .repo = "" } }); 188 try std.testing.expect(std.mem.startsWith(u8, data.items[0].description, "commit -m \"example commit\"")); 189 } 190 191 test "malformed undo history is shown as an error" { 192 var temp = std.testing.tmpDir(.{}); 193 defer temp.cleanup(); 194 var arena = std.heap.ArenaAllocator.init(allocator); 195 defer arena.deinit(); 196 const path = try temp.dir.realPathFileAlloc(io, ".", arena.allocator()); 197 const Repo = xit.repo.Repo(.xit, .{}); 198 var repo = try Repo.init(io, allocator, .{ .path = path }); 199 defer repo.deinit(io, allocator); 200 201 // append an invalid undo record without damaging the repository data. 202 const Corrupt = struct { 203 pub fn run(_: @This(), cursor: *Repo.DB.Cursor(.read_write)) !void { 204 const moment = try Repo.DB.HashMap(.read_write).init(cursor.*); 205 try moment.put(xit.hash.hashInt(.sha1, "undo"), .{ .bytes = "bad" }); 206 } 207 }; 208 const history = try Repo.DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 209 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Corrupt{}); 210 211 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .io = io, .local = .{ .path = path, .repo_kind = .xit }, .data = .{ .host_kind = .local } }; 212 // tabs switch in-page, so every tab carries the undo data and its failure 213 const files = try ui.Repo.init(&arena, &session, ui.RoutablePage.repoFilesRoute("", null, "", "", 0).?); 214 try std.testing.expectEqual(0, files.undo.?.items.len); 215 try std.testing.expectEqualStrings("InvalidUndoRecord", files.undo.?.failure.?); 216 const undo = try ui.Repo.init(&arena, &session, ui.RoutablePage.repoUndoRoute("", null).?); 217 try std.testing.expectEqualStrings("InvalidUndoRecord", undo.undo.?.failure.?); 218 219 var root = ui.Widget{ .repo_undo = try Undo.View.init(allocator, &undo.undo.?, &session) }; 220 defer root.deinit(allocator); 221 try root.build(allocator, constraint, root.getFocus()); 222 const html = try @import("../web.zig").generateHtml(allocator, &root, &session); 223 defer allocator.free(html); 224 try std.testing.expect(std.mem.indexOf(u8, html, "InvalidUndoRecord") != null); 225 } 226