╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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