╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
172 if (data.route == .repo) { 173 try std.testing.expectEqualStrings("file-00.txt", file.repo_files.path.slice()); 174 try std.testing.expectEqualStrings(&head, file.repo_files.ref_value.slice()); 175 try std.testing.expectEqualStrings(&base, next.repo_diff.base_oid.slice()); 176 try std.testing.expectEqualStrings(&head, next.repo_diff.value.slice()); 177 try std.testing.expectEqual(Diff.page_size, next.repo_diff.start); 178 } else { 179 try std.testing.expectEqualStrings("file-00.txt", file.fork_files.path.slice()); 180 try std.testing.expectEqual(Diff.page_size, next.fork_diff.start); 181 } 182 } 183 } 184 } 185 186 // the "next" row at the bottom of the commits list must be recognized as a 187 // cross-page link (so a click navigates), exactly like the diff pane's "next". 188 test "commits list next row is a cross-page link" { 189 const allocator = std.testing.allocator; 190 191 var arena = std.heap.ArenaAllocator.init(allocator); 192 defer arena.deinit(); 193 const identity = "alice:ziglings"; 194 const oid0 = "1111111111111111111111111111111111111111"; 195 const next_oid = "2222222222222222222222222222222222222222"; 196 const base_oid = "3333333333333333333333333333333333333333"; 197 198 const data = Commits{ 199 .handle = .{ .location = .{ .repo = identity } }, 200 .ref_or_oid = .object, 201 .ref_or_oid_value = oid0, 202 .base_oid = base_oid, 203 .commits = &.{ 204 .{ .oid = oid0, .parent_oid = base_oid, .message = "first", .timestamp = 1_704_067_200 }, 205 }, 206 .next_start = next_oid, 207 }; 208 209 var session = ui.Session{ .arena = &arena, .page_arena = &arena, .io = std.testing.io, .is_terminal = true }; 210 session.data.current_page = ui.RoutablePage.repoCommitsRoute(identity, .object, oid0, base_oid).?; 211 212 var view = try Commits.View.init(allocator, &data, &session); 213 defer view.deinit(allocator); 214 215 const root_focus = view.getFocus(); 216 try view.build(allocator, .{ 217 .min_size = .{ .width = null, .height = null }, 218 .max_size = .{ .width = 120, .height = 60 }, 219 }, root_focus); 220 221 // the "next" row is the last child of the list box: the view's outer box 222 // holds the sub-header then the list/detail split, whose first child is the 223 // list scroll. 224 const content = &view.box.children.values()[1].widget.box; 225 const lb = &content.children.values()[0].widget.scroll.child.box; 226 const next_id = lb.children.keys()[lb.children.count() - 1]; 227 228 const route = ui.crossPageLink(root_focus, next_id, session.data); 229 try std.testing.expect(route != null); 230 try std.testing.expectEqualStrings(base_oid, route.?.repo_commits.base_oid.slice()); 231 } 232 233 // a ref name with a '/' is url-encoded in the route, so it survives the 234 // '/'-delimited url round-trip as a single segment (kind + value) rather than 235 // being mis-parsed as extra path. 236 test "encoded ref name survives the commits url round-trip" { 237 var arena = std.heap.ArenaAllocator.init(std.testing.allocator); 238 defer arena.deinit(); 239 const RP = ui.RoutablePage; 240 const base_oid = "3333333333333333333333333333333333333333"; 241 242 // the route layer holds the value already url-encoded ("feature%2Ffoo"). 243 const route = RP.repoCommitsRoute("alice:ziglings", .branch, "feature%2Ffoo", base_oid).?; 244 const url = try route.toUrl(&arena); 245 try std.testing.expectEqualStrings("/alice:ziglings/commits/branch:feature%2Ffoo/base:" ++ base_oid, url); 246 247 // parsing it back yields the same route, and the ref splits out intact. 248 const parsed = RP.fromUrl(url); 249 try std.testing.expect(parsed != null); 250 const parsed_route = parsed.?.repo_commits; 251 try std.testing.expectEqual(RP.RefOrOid.branch, parsed_route.ref_or_oid.?); 252 try std.testing.expectEqualStrings("feature%2Ffoo", parsed_route.value.slice()); 253 try std.testing.expectEqualStrings(base_oid, parsed_route.base_oid.slice()); 254 const unfiltered = RP.repoCommitsRoute("alice:ziglings", .branch, "feature%2Ffoo", "").?; 255 try std.testing.expect(!RP.eql(route, unfiltered)); 256 } 257 258 test "commit pages stop at the base without excluding other ancestors" { 259 try testCommitBase(.git); 260 try testCommitBase(.xit); 261 } 262 263 fn testCommitBase(comptime kind: xit.repo.RepoKind) !void { 264 const io = std.testing.io; 265 const allocator = std.testing.allocator; 266 const opts: xit.repo.RepoOpts(kind) = .{ .is_test = true }; 267 var temp = std.testing.tmpDir(.{}); 268 defer temp.cleanup(); 269 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 270 defer allocator.free(path); 271 var repo = try xit.repo.Repo(kind, opts).init(io, allocator, .{ .path = path }); 272 defer repo.deinit(io, allocator); 273 var arena = std.heap.ArenaAllocator.init(allocator); 274 defer arena.deinit(); 275 const handle = ui.RepoHandle{ .location = .{ .repo = "" } }; 276 const branch = xit.ref.Ref{ .kind = .head, .name = "master" }; 277 278 // one more commit than fits on the first page 279 const base = try repo.commitAtRef(io, allocator, .{ .message = "base" }, null, branch); 280 var tip = base; 281 var previous = base; 282 for (0..21) |_| { 283 previous = tip; 284 tip = try repo.commitAtRef(io, allocator, .{ .message = "next" }, null, branch); 285 } 286 const first = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, .object, &tip, false, &base, "", ""); 287 try std.testing.expectEqual(20, first.commits.len); 288 const last = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, .object, first.next_start orelse return error.MissingNext, false, &base, "", ""); 289 try std.testing.expectEqual(1, last.commits.len); 290 try std.testing.expectEqual(null, last.next_start); 291 292 // the base at a page boundary must not create an empty next page 293 const full = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, .object, &previous, false, &base, "", ""); 294 try std.testing.expectEqual(20, full.commits.len); 295 try std.testing.expectEqual(null, full.next_start); 296 const tag_oid = try repo.addTag(io, allocator, .{ .name = "tip", .message = "annotated" }); 297 const sources = [_]struct { ref: ui.RoutablePage.RefOrOid, value: []const u8 }{ 298 .{ .ref = .object, .value = &tip }, 299 .{ .ref = .object, .value = &tag_oid }, 300 .{ .ref = .branch, .value = branch.name }, 301 .{ .ref = .tag, .value = "tip" }, 302 }; 303 for (sources) |source| { 304 const empty = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, source.ref, source.value, false, &tip, "", ""); 305 try std.testing.expectEqual(0, empty.commits.len); 306 const paged = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, source.ref, source.value, false, &base, "", first.next_start orelse return error.MissingNext); 307 try std.testing.expectEqual(1, paged.commits.len); 308 try std.testing.expectEqualStrings(last.commits[0].oid, paged.commits[0].oid); 309 try std.testing.expectEqual(source.ref, paged.ref_or_oid); 310 try std.testing.expectEqualStrings(source.value, paged.ref_or_oid_value); 311 } 312 313 // an off-chain stopping point must not hide its parent on this chain 314 const other = try repo.commitAtRef(io, allocator, .{ .message = "other", .parent_oids = &.{base} }, null, .{ .kind = .head, .name = "other" }); 315 const off_chain = try Commits.init(kind, opts, &arena, &repo, io, allocator, null, handle, .object, last.commits[0].oid, false, &other, "", ""); 316 try std.testing.expectEqual(2, off_chain.commits.len); 317 try std.testing.expectEqualStrings(&base, off_chain.commits[1].oid); 318 } 319 320 test "sync creates missing event branches and preserves head" { 321 const io = std.testing.io; 322 const allocator = std.testing.allocator; 323 324 var temp = std.testing.tmpDir(.{}); 325 defer temp.cleanup(); 326 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 327 defer allocator.free(temp_path); 328 329 const local_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "local" }); 330 defer allocator.free(local_path); 331 const remote_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "remote" }); 332 defer allocator.free(remote_path); 333 334 const Repo = xit.repo.Repo(.git, .{}); 335 const RemoteRepo = xit.repo.Repo(.git, .{}); 336 { 337 var remote = try RemoteRepo.init(io, allocator, .{ .path = remote_path }); 338 remote.deinit(io, allocator); 339 } 340 { 341 var local = try Repo.init(io, allocator, .{ .path = local_path }); 342 defer local.deinit(io, allocator); 343 try local.addConfig(io, allocator, .{ .name = "user.name", .value = "user" }); 344 try local.addConfig(io, allocator, .{ .name = "user.email", .value = "user@haxy" }); 345 { 346 const file = try local.core.work_dir.createFile(io, "keep.txt", .{}); 347 defer file.close(io); 348 try file.writeStreamingAll(io, "keep"); 349 } 350 try local.add(io, allocator, &.{"keep.txt"}); 351 _ = try local.commit(io, allocator, .{ .message = "master" }); 352 try local.addRemote(io, allocator, .{ .name = "origin", .value = remote_path }); 353 354 const id: [evt.event_id_size]u8 = @splat(1); 355 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 356 .id = std.fmt.bytesToHex(id, .lower), 357 .author = .{ .name = "haxy", .email = "user@haxy" }, 358 .event = .{ .issue = .{ .title = "sync", .description = "", .labels = "" } }, 359 }}); 360 } 361 362 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 363 { 364 var local = try Repo.open(io, allocator, .{ .path = local_path }); 365 defer local.deinit(io, allocator); 366 const id: [evt.event_id_size]u8 = @splat(2); 367 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{.{ 368 .id = std.fmt.bytesToHex(id, .lower), 369 .author = .{ .name = "haxy", .email = "user@haxy" }, 370 .event = .{ .issue = .{ .title = "local", .description = "", .labels = "" } }, 371 }}); 372 } 373 { 374 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 375 defer remote.deinit(io, allocator); 376 const id: [evt.event_id_size]u8 = @splat(3); 377 try evt.consume(.local, .repo, .git, .{}, io, allocator, &remote, evt.events_ref, &.{.{ 378 .id = std.fmt.bytesToHex(id, .lower), 379 .author = .{ .name = "haxy", .email = "user@haxy" }, 380 .event = .{ .issue = .{ .title = "remote", .description = "", .labels = "" } }, 381 }}); 382 } 383 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 384 { 385 var local = try Repo.open(io, allocator, .{ .path = local_path }); 386 defer local.deinit(io, allocator); 387 try local.removeBranch(io, .{ .name = evt.events_ref.name }); 388 try evt.consume(.local, .repo, .git, .{}, io, allocator, &local, evt.events_ref, &.{}); 389 } 390 try std.testing.expectEqual(null, try Events.sync(io, allocator, .{ .path = local_path, .repo_kind = .git })); 391 392 var local = try Repo.open(io, allocator, .{ .path = local_path }); 393 defer local.deinit(io, allocator); 394 var head_buffer: [xit.ref.MAX_REF_CONTENT_SIZE]u8 = undefined; 395 const head = try local.head(io, &head_buffer); 396 switch (head) { 397 .ref => |ref| try std.testing.expectEqualStrings("master", ref.name), 398 .oid => return error.TestExpectedEqual, 399 } 400 const kept = try local.core.work_dir.openFile(io, "keep.txt", .{}); 401 kept.close(io); 402 const local_tip = (try local.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 403 404 var remote = try RemoteRepo.open(io, allocator, .{ .path = remote_path }); 405 defer remote.deinit(io, allocator); 406 const remote_tip = (try remote.readRef(io, evt.events_ref)) orelse return error.TestExpectedEqual; 407 try std.testing.expectEqualStrings(&local_tip, &remote_tip); 408 409 var start_oids = [_][local_tip.len]u8{local_tip}; 410 var commit_iter = try local.log(io, allocator, .{ .start_oids = &start_oids }); 411 defer commit_iter.deinit(); 412 var merge_commit = (try commit_iter.next(allocator)) orelse return error.TestExpectedEqual; 413 defer merge_commit.deinit(); 414 const parent_oids = merge_commit.content.commit.metadata.parent_oids orelse return error.TestExpectedEqual; 415 try std.testing.expectEqual(2, parent_oids.len); 416 417 var arena = std.heap.ArenaAllocator.init(allocator); 418 defer arena.deinit(); 419 const status = try Events.Header.local(.git, .{}, arena.allocator(), &local, io, null); 420 try std.testing.expectEqualStrings("nothing to sync", status.sync_status orelse return error.TestExpectedEqual); 421 } 422