╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const ui = @import("../ui.zig"); 4 const Commits = @import("../ui/Repo/Commits.zig"); 5 const Events = @import("../ui/Repo/Events.zig"); 6 const xit = @import("xit"); 7 8 // the "next" row at the bottom of the commits list must be recognized as a 9 // cross-page link (so a click navigates), exactly like the diff pane's "next". 10 test "commits list next row is a cross-page link" { 11 const allocator = std.testing.allocator; 12 13 var arena = std.heap.ArenaAllocator.init(allocator); 14 defer arena.deinit(); 15 const identity = "alice/ziglings"; 16 const oid0 = "1111111111111111111111111111111111111111"; 17 const next_oid = "2222222222222222222222222222222222222222"; 18 19 const data = Commits{ 20 .location = .{ .repo = identity }, 21 .ref_or_oid = .object, 22 .ref_or_oid_value = oid0, 23 .commits = &.{ 24 .{ .oid = oid0, .date = "2024-01-01", .message = "first", .hunks = &.{}, .window_start = 0, .has_prev = false, .has_more = false }, 25 }, 26 .next_start = next_oid, 27 .header = try Commits.Header.init(arena.allocator(), .object, oid0), 28 }; 29 30 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .is_terminal = true }; 31 session.data.current_page = ui.RoutablePage.repoCommitsRoute(identity, .object, oid0, 0, "").?; 32 33 var view = try Commits.View.init(allocator, &data, &session); 34 defer view.deinit(allocator); 35 36 const root_focus = view.getFocus(); 37 try view.build(allocator, .{ 38 .min_size = .{ .width = null, .height = null }, 39 .max_size = .{ .width = 120, .height = 60 }, 40 }, root_focus); 41 42 // the "next" row is the last child of the list box: the view's outer box 43 // holds the sub-header then the list/diff split, whose first child is the 44 // list scroll. 45 const content = &view.box.children.values()[1].widget.box; 46 const lb = &content.children.values()[0].widget.scroll.child.box; 47 const next_id = lb.children.keys()[lb.children.count() - 1]; 48 49 const route = ui.crossPageLink(root_focus, next_id, session.data); 50 try std.testing.expect(route != null); 51 } 52 53 // a ref name with a '/' is url-encoded in the route, so it survives the 54 // '/'-delimited url round-trip as a single segment (kind + value) rather than 55 // being mis-parsed as extra path. 56 test "encoded ref name survives the commits url round-trip" { 57 var arena = std.heap.ArenaAllocator.init(std.testing.allocator); 58 defer arena.deinit(); 59 const RP = ui.RoutablePage; 60 61 // the route layer holds the value already url-encoded ("feature%2Ffoo"). 62 const route = RP.repoCommitsRoute("alice/ziglings", .branch, "feature%2Ffoo", 0, "").?; 63 const url = try route.toUrl(&arena); 64 try std.testing.expectEqualStrings("/repo/alice/ziglings/commits/branch:feature%2Ffoo", url); 65 66 // parsing it back yields the same route, and the ref splits out intact. 67 const parsed = RP.fromUrl(url); 68 try std.testing.expect(parsed != null); 69 const parsed_route = parsed.?.repo_commits; 70 try std.testing.expectEqual(RP.RefOrOid.branch, parsed_route.ref_or_oid.?); 71 try std.testing.expectEqualStrings("feature%2Ffoo", parsed_route.value.slice()); 72 } 73 74 test "sync creates missing event branches and preserves head" { 75 const io = std.testing.io; 76 const allocator = std.testing.allocator; 77 78 var temp = std.testing.tmpDir(.{}); 79 defer temp.cleanup(); 80 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 81 defer allocator.free(temp_path); 82 83 const local_path = try std.fs.path.join(allocator, &.{ temp_path, "local" }); 84 defer allocator.free(local_path); 85 const remote_path = try std.fs.path.join(allocator, &.{ temp_path, "remote" }); 86 defer allocator.free(remote_path); 87 88 const Repo = xit.repo.Repo(.git, .{}); 89 const RemoteRepo = xit.repo.Repo(.git, .{}); 90 { 91 var remote = try RemoteRepo.init(io, allocator, .{ .path = remote_path }); 92 remote.deinit(io, allocator); 93 } 94 { 95 var local = try Repo.init(io, allocator, .{ .path = local_path }); 96 defer local.deinit(io, allocator); 97 try local.addConfig(io, allocator, .{ .name = "user.name", .value = "user" }); 98 try local.addConfig(io, allocator, .{ .name = "user.email", .value = "user@haxy" }); 99 { 100 const file = try local.core.work_dir.createFile(io, "keep.txt", .{}); 101 defer file.close(io); 102 try file.writeStreamingAll(io, "keep"); 103 } 104 try local.add(io, allocator, &.{"keep.txt"}); 105 _ = try local.commit(io, allocator, .{ .message = "master" }); 106 try local.addRemote(io, allocator, .{ .name = "origin", .value = remote_path }); 107 108 const id = [_]u8{1} ** evt.event_id_size; 109 try evt.consume(.repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 110 .id = std.fmt.bytesToHex(id, .lower), 111 .author = .{ .name = "haxy", .email = "user@haxy" }, 112 .event = .{ .issue = .{ .title = "sync", .description = "", .tags = "" } }, 113 }}); 114 } 115 116 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 117 { 118 var local = try Repo.open(io, allocator, .{ .path = local_path }); 119 defer local.deinit(io, allocator); 120 const id = [_]u8{2} ** evt.event_id_size; 121 try evt.consume(.repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 122 .id = std.fmt.bytesToHex(id, .lower), 123 .author = .{ .name = "haxy", .email = "user@haxy" }, 124 .event = .{ .issue = .{ .title = "local", .description = "", .tags = "" } }, 125 }}); 126 } 127 { 128 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 129 defer remote.deinit(io, allocator); 130 const id = [_]u8{3} ** evt.event_id_size; 131 try evt.consume(.repo, .git, .{}, io, allocator, &remote, evt.events_ref, &.{.{ 132 .id = std.fmt.bytesToHex(id, .lower), 133 .author = .{ .name = "haxy", .email = "user@haxy" }, 134 .event = .{ .issue = .{ .title = "remote", .description = "", .tags = "" } }, 135 }}); 136 } 137 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 138 { 139 var local = try Repo.open(io, allocator, .{ .path = local_path }); 140 defer local.deinit(io, allocator); 141 try local.removeBranch(io, .{ .name = evt.events_ref.name }); 142 try evt.consume(.repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{}); 143 } 144 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 145 146 var local = try Repo.open(io, allocator, .{ .path = local_path }); 147 defer local.deinit(io, allocator); 148 var head_buffer: [xit.ref.MAX_REF_CONTENT_SIZE]u8 = undefined; 149 const head = try local.head(io, &head_buffer); 150 switch (head) { 151 .ref => |ref| try std.testing.expectEqualStrings("master", ref.name), 152 .oid => return error.TestExpectedEqual, 153 } 154 const kept = try local.core.work_dir.openFile(io, "keep.txt", .{}); 155 kept.close(io); 156 const local_tip = (try local.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 157 158 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 159 defer remote.deinit(io, allocator); 160 const remote_tip = (try remote.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 161 try std.testing.expectEqualStrings(&local_tip, &remote_tip); 162 163 var start_oids = [_][local_tip.len]u8{local_tip}; 164 var commit_iter = try local.log(io, allocator, .{ .start_oids = &start_oids }); 165 defer commit_iter.deinit(); 166 var merge_commit = (try commit_iter.next(allocator)) orelse return error.TestExpectedEqual; 167 defer merge_commit.deinit(); 168 const parent_oids = merge_commit.content.commit.metadata.parent_oids orelse return error.TestExpectedEqual; 169 try std.testing.expectEqual(2, parent_oids.len); 170 171 var arena = std.heap.ArenaAllocator.init(allocator); 172 defer arena.deinit(); 173 const status = try Events.Header.local(.git, .{}, arena.allocator(), &local, io, null); 174 try std.testing.expectEqualStrings("nothing to sync", status.sync_status orelse return error.TestExpectedEqual); 175 } 176