╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ event.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
882 883 var merge = try repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_events_ref }} } } }, null); 884 defer merge.deinit(); 885 886 try std.testing.expect(.success == merge.result); 887 } 888 889 { 890 try evt.consume(.repo, .xit, repo_opts, io, allocator, &repo, evt.events_ref, &.{}); 891 892 const haxy_moment = try evt.currentMoment(repo_opts, &repo); 893 894 var second_id: [evt.event_id_size]u8 = undefined; 895 _ = try std.fmt.hexToBytes(&second_id, &events_to_consume[1].id); 896 897 // each side's edit to a different field survives, without conflicting 898 const merged = try readIssue(Repo.DB, repo_opts.hash, haxy_moment, &arena, &second_id); 899 try std.testing.expectEqualStrings("Search results ignore the archived project filter", merged.event.title); 900 try std.testing.expectEqualStrings("Archived projects are ranked before the flag is applied.", merged.event.description); 901 try std.testing.expectEqualStrings("bug search", merged.event.tags); 902 903 // each merged field is attributed to the branch it came from 904 try std.testing.expectEqualSlices(u8, &retitle_oid, &try readOid(Repo.DB, repo_opts.hash, haxy_moment, &second_id, "title")); 905 try std.testing.expectEqualSlices(u8, &rewrite_oid, &try readOid(Repo.DB, repo_opts.hash, haxy_moment, &second_id, "description")); 906 907 const conflicts_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Issue.conflicts_key)) orelse return error.NotFound; 908 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 909 try std.testing.expectEqual(0, try conflicts.count()); 910 911 // tags the merged-in side dropped don't survive in the tag index, 912 // whether we edited that issue or left it alone 913 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 914 const tag_to_issues = try Repo.DB.SortedMap(.read_only).init(tag_to_issues_cursor); 915 916 var third_id: [evt.event_id_size]u8 = undefined; 917 _ = try std.fmt.hexToBytes(&third_id, &events_to_consume[2].id); 918 const retagged = try readIssue(Repo.DB, repo_opts.hash, haxy_moment, &arena, &third_id); 919 try std.testing.expectEqualStrings("enhancement roadmap", retagged.event.tags); 920 921 try std.testing.expect(null == try tag_to_issues.getCursor("backend open")); 922 try std.testing.expect(null == try tag_to_issues.getCursor("preferences open")); 923 try std.testing.expect(null != try tag_to_issues.getCursor("roadmap open")); 924 try std.testing.expect(null != try tag_to_issues.getCursor("enhancement open")); 925 } 926 927 // 928 // a removal on the other branch carries over, unless we edited the issue, 929 // in which case the edit wins 930 // 931 932 { 933 const edited_issue = events_to_consume2[1].event.issue orelse return error.NotFound; 934 935 const base_oid = try repo.readRef(io, evt.events_ref) orelse return error.NotFound; 936 937 var json: std.Io.Writer.Allocating = .init(allocator); 938 defer json.deinit(); 939 940 // we retitle one of the two the other branch is about to drop 941 { 942 const event = evt.EventWithId{ .id = events_to_consume2[1].id, .author = author, .event = .{ .issue = .{ 943 .title = "Kept by the edit", 944 .description = edited_issue.description, 945 .tags = edited_issue.tags, 946 } } }; 947 try std.json.Stringify.value(event, .{}, &json.writer); 948 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, evt.events_ref); 949 } 950 951 // the other branch removes both 952 { 953 json.clearRetainingCapacity(); 954 const event = evt.EventWithId{ .id = events_to_consume2[0].id, .author = author, .event = .{ .issue = null } }; 955 try std.json.Stringify.value(event, .{}, &json.writer); 956 _ = try repo.commitAtRef(io, allocator, .{ .parent_oids = &.{base_oid}, .message = json.written() }, null, other_events_ref); 957 } 958 { 959 json.clearRetainingCapacity(); 960 const event = evt.EventWithId{ .id = events_to_consume2[1].id, .author = author, .event = .{ .issue = null } }; 961 try std.json.Stringify.value(event, .{}, &json.writer); 962 _ = try repo.commitAtRef(io, allocator, .{ .message = json.written() }, null, other_events_ref); 963 } 964 965 { 966 var result = try repo.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 967 defer result.deinit(); 968 969 var merge = try repo.merge(io, allocator, .{ .kind = .full, .action = .{ .new = .{ .source = &.{.{ .ref = other_events_ref }} } } }, null); 970 defer merge.deinit(); 971 972 try std.testing.expect(.success == merge.result); 973 } 974 975 try evt.consume(.repo, .xit, repo_opts, io, allocator, &repo, evt.events_ref, &.{}); 976 977 const haxy_moment = try evt.currentMoment(repo_opts, &repo); 978 979 // the one we left alone is removed, and its tags left the index 980 var dropped_id: [evt.event_id_size]u8 = undefined; 981 _ = try std.fmt.hexToBytes(&dropped_id, &events_to_consume2[0].id); 982 const dropped = try readIssue(Repo.DB, repo_opts.hash, haxy_moment, &arena, &dropped_id); 983 try std.testing.expect(dropped.removed); 984 985 const tag_to_issues_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, "tag+status->issue-id-set")) orelse return error.NotFound; 986 const tag_to_issues = try Repo.DB.SortedMap(.read_only).init(tag_to_issues_cursor); 987 try std.testing.expect(null == try tag_to_issues.getCursor("kanban open")); 988 989 // the one we edited survives, with our edit 990 var kept_id: [evt.event_id_size]u8 = undefined; 991 _ = try std.fmt.hexToBytes(&kept_id, &events_to_consume2[1].id); 992 const kept = try readIssue(Repo.DB, repo_opts.hash, haxy_moment, &arena, &kept_id); 993 try std.testing.expect(!kept.removed); 994 try std.testing.expectEqualStrings("Kept by the edit", kept.event.title); 995 } 996 } 997 998 // the commit that last set `field_name` on the issue `id` names 999 fn readOid( 1000 comptime DB: type, 1001 comptime hash_kind: hash.HashKind, 1002 haxy_moment: DB.HashMap(.read_only), 1003 id: *const [evt.event_id_size]u8, 1004 comptime field_name: []const u8, 1005 ) ![hash.byteLen(hash_kind)]u8 { 1006 const id_to_field_to_oid_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, evt.Issue.id_to_field_to_oid_key)) orelse return error.NotFound; 1007 const id_to_field_to_oid = try DB.HashMap(.read_only).init(id_to_field_to_oid_cursor); 1008 const field_oids_cursor = try id_to_field_to_oid.getCursor(hash.hashInt(hash_kind, id)) orelse return error.NotFound; 1009 const field_oids = try DB.SortedMap(.read_only).init(field_oids_cursor); 1010 const cursor = try field_oids.getCursor(field_name) orelse return error.NotFound; 1011 var oid: [hash.byteLen(hash_kind)]u8 = undefined; 1012 _ = try cursor.readBytes(&oid); 1013 return oid; 1014 } 1015 1016 // the issue `id` names, read out of `event-id->issue` 1017 fn readIssue( 1018 comptime DB: type, 1019 comptime hash_kind: hash.HashKind, 1020 haxy_moment: DB.HashMap(.read_only), 1021 arena: *std.heap.ArenaAllocator, 1022 id: *const [evt.event_id_size]u8, 1023 ) !evt.Issue.Record { 1024 const event_id_to_issue_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->issue")) orelse return error.NotFound; 1025 const event_id_to_issue = try DB.HashMap(.read_only).init(event_id_to_issue_cursor); 1026 const issue_cursor = try event_id_to_issue.getCursor(hash.hashInt(hash_kind, id)) orelse return error.NotFound; 1027 const issue_map = try DB.HashMap(.read_only).init(issue_cursor); 1028 return try evt.read(evt.Issue.Record, DB, hash_kind, arena, issue_map); 1029 } 1030