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