╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
118 try std.testing.expectEqualStrings(&before_edit, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 119 120 // missing branches fail without writing events 121 var invalid = patch; 122 invalid.source_branch = "missing"; 123 try std.testing.expectError(error.InvalidSourceBranch, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), invalid, retargeted.event, author)); 124 invalid = patch; 125 invalid.target_branch = "missing"; 126 try std.testing.expectError(error.InvalidTargetBranch, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), invalid, retargeted.event, author)); 127 128 // editing metadata without changing the target must also record a patch edit. 129 try evt.Patch.update(.local, kind, opts, io, allocator, &repo, &id, .{ .fields = .{ 130 .title = "renamed patch", 131 .labels = patch.labels, 132 .description = patch.description, 133 .target_branch = "other-target", 134 } }, author); 135 136 // removing the patch leaves its branch intact 137 try evt.remove(.local, .repo, kind, opts, io, allocator, &repo, &id, .patch, author); 138 try pch.refreshBranches(.local, kind, opts, io, allocator, &repo, null, null); 139 const removed = (try evt.readFromRepo(evt.Patch, kind, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 140 try std.testing.expect(removed.removed); 141 try std.testing.expectEqualStrings(&next, &(try repo.readRef(io, .{ .kind = .head, .name = "feature" }) orelse return error.NotFound)); 142 143 // an edit started before removal must not restore the patch 144 stale_edit = retargeted.event; 145 stale_edit.revision = null; 146 const before_restore = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 147 try std.testing.expectError(error.PatchOutOfDate, pch.writeBranchPatch(.local, kind, opts, io, allocator, &repo, std.fmt.bytesToHex(id, .lower), stale_edit, retargeted.event, author)); 148 try std.testing.expectEqualStrings(&before_restore, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 149 } 150 151 test "branch patches merge without a fork" { 152 try testBranchMerge(.source); 153 try testBranchMerge(.squash); 154 } 155 156 fn testBranchMerge(selection: evt.Patch.MergeRevision) !void { 157 const io = std.testing.io; 158 const allocator = std.testing.allocator; 159 const opts: rp.RepoOpts(.xit) = .{ .is_test = true, .ProgressCtx = *progress.Sideband }; 160 var temp = std.testing.tmpDir(.{}); 161 defer temp.cleanup(); 162 const path = try temp.dir.realPathFileAlloc(io, ".", allocator); 163 defer allocator.free(path); 164 var repo = try rp.Repo(.xit, opts).init(io, allocator, .{ .path = path }); 165 defer repo.deinit(io, allocator); 166 { 167 const file = try repo.core.work_dir.createFile(io, "base.txt", .{}); 168 defer file.close(io); 169 try file.writeStreamingAll(io, "base\n"); 170 } 171 try repo.add(io, allocator, &.{"base.txt"}); 172 _ = try repo.commit(io, allocator, .{ .message = "base" }); 173 try repo.addBranch(io, .{ .name = "feature" }); 174 { 175 var selected = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "feature" } } }); 176 defer selected.deinit(); 177 const file = try repo.core.work_dir.createFile(io, "base.txt", .{}); 178 defer file.close(io); 179 try file.writeStreamingAll(io, "source\n"); 180 try repo.add(io, allocator, &.{"base.txt"}); 181 _ = try repo.commit(io, allocator, .{ .message = "edit file" }); 182 var restored = try repo.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 183 defer restored.deinit(); 184 } 185 const id: [evt.event_id_size]u8 = @splat(9); 186 const id_hex = std.fmt.bytesToHex(id, .lower); 187 const before_new = try repo.core.db.rootCursor().count(); 188 try pch.writeBranchPatch(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, id_hex, .{ 189 .title = "merge branch", 190 .description = "", 191 .labels = "", 192 .source_branch = "feature", 193 .target_branch = "master", 194 }, null, author); 195 try std.testing.expectEqual(before_new + 1, try repo.core.db.rootCursor().count()); 196 197 // updating a branch and checking its mergeability counts as one patch 198 _ = try repo.commitAtRef(io, allocator, .{ .message = "revise feature" }, null, .{ .kind = .head, .name = "feature" }); 199 var response = xit.net_server_receive_pack.Response.init(allocator); 200 defer response.deinit(); 201 response.sideband = true; 202 var output = std.Io.Writer.Allocating.init(allocator); 203 defer output.deinit(); 204 var sideband = progress.Sideband{ .response = &response, .writer = &output.writer }; 205 const before_refresh = try repo.core.db.rootCursor().count(); 206 try pch.refreshBranches(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, null, &sideband); 207 try std.testing.expectEqual(before_refresh + 1, try repo.core.db.rootCursor().count()); 208 try std.testing.expect(std.mem.indexOf(u8, output.written(), "Checking mergeability: 100% (1/1)\n") != null); 209 210 // closed patches skip source updates and contribute no progress 211 try evt.Patch.update(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, &id, .{ .status = .closed }, author); 212 const next = try repo.commitAtRef(io, allocator, .{ .message = "revise while closed" }, null, .{ .kind = .head, .name = "feature" }); 213 const closed_tip = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 214 const closed_progress_len = output.written().len; 215 try pch.refreshBranches(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, null, &sideband); 216 try std.testing.expectEqualStrings(&closed_tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 217 try std.testing.expectEqual(closed_progress_len, output.written().len); 218 219 // reopening captures the latest source and checks both merge styles 220 const before_reopen = try repo.core.db.rootCursor().count(); 221 try evt.Patch.update(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, &id, .{ .status = .open }, author); 222 try std.testing.expectEqual(before_reopen + 1, try repo.core.db.rootCursor().count()); 223 var arena = std.heap.ArenaAllocator.init(allocator); 224 defer arena.deinit(); 225 const before = (try evt.readFromRepo(evt.Patch, .xit, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 226 const revision = before.event.revision orelse return error.NotFound; 227 try std.testing.expectEqualStrings(&next, revision.source_oid); 228 { 229 var moment = try repo.core.latestMoment(); 230 const result = try pch.readMergeability(opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, &id, before.event); 231 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .clean, .squash = .clean }, result); 232 } 233 234 // a diverged target produces a merge commit in both merge styles 235 { 236 const file = try repo.core.work_dir.createFile(io, "target.txt", .{}); 237 defer file.close(io); 238 try file.writeStreamingAll(io, "target only\n"); 239 } 240 try repo.add(io, allocator, &.{"target.txt"}); 241 const target_oid = try repo.commit(io, allocator, .{ .message = "advance target" }); 242 pch.refreshMergeability(opts, io, allocator, &repo, path, id); 243 244 // both merge styles use the stored revision, without fork data 245 try pch.merge(opts, io, allocator, .{ .users_dir = path }, &repo, .{ .id = id_hex, .revision = selection, .author = author, .timestamp = 100 }); 246 const merged = (try evt.readFromRepo(evt.Patch, .xit, opts, io, allocator, &arena, &repo, &id)) orelse return error.NotFound; 247 const merged_status = switch (merged.event.status) { 248 .merged => |value| value, 249 else => return error.NotMerged, 250 }; 251 const result_oid = (try repo.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 252 try std.testing.expectEqual(selection, merged_status.revision); 253 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 254 const result = (try evt.readFromRepo(evt.PatchRev, .xit, opts, io, allocator, &arena, &repo, &result_id)) orelse return error.NotFound; 255 try std.testing.expectEqualStrings(&target_oid, result.event.base_oid); 256 try std.testing.expectEqualStrings(&result_oid, result.event.source_oid); 257 try std.testing.expect(!std.mem.eql(u8, &revision.id, &merged_status.patchrev_id)); 258 try std.testing.expectEqualDeep(before.event.revision, merged.event.revision); 259 { 260 var commits = try repo.log(io, allocator, .{ .start_oids = &.{result_oid} }); 261 defer commits.deinit(); 262 const commit = (try commits.next(allocator)) orelse return error.NotFound; 263 defer commit.deinit(); 264 const parents = commit.content.commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 265 try std.testing.expectEqual(2, parents.len); 266 try std.testing.expectEqualStrings(&target_oid, &parents[0]); 267 try std.testing.expectEqualStrings(if (selection == .source) revision.source_oid else revision.squash_oid, &parents[1]); 268 } 269 270 // the source survives, and merged patches no longer follow it 271 _ = try repo.commitAtRef(io, allocator, .{ .message = "later" }, null, .{ .kind = .head, .name = "feature" }); 272 const tip = (try repo.readRef(io, evt.events_ref)) orelse return error.NotFound; 273 const progress_len = output.written().len; 274 try pch.refreshBranches(.{ .server = .{ .users_dir = path } }, .xit, opts, io, allocator, &repo, null, &sideband); 275 try std.testing.expectEqual(progress_len, output.written().len); 276 try std.testing.expectEqualStrings(&tip, &(try repo.readRef(io, evt.events_ref) orelse return error.NotFound)); 277 278 // retain the exact merge diff after its original commits are collected 279 const diff_route = (try ui.Repo.Patches.diffRoute("", .{ .id = &id_hex, .record = merged })) orelse return error.BadRoute; 280 const diff_before = try ui.Repo.Diff.init(.xit, opts, &arena, &repo, io, allocator, diff_route.repo_diff); 281 try std.testing.expectEqual(1, diff_before.window.hunks.len); 282 try std.testing.expectEqualStrings("base.txt", diff_before.window.hunks[0].path orelse return error.MissingPath); 283 _ = try repo.commitAtRef(io, allocator, .{ .message = "rewrite history", .parent_oids = &.{} }, null, .{ .kind = .head, .name = "master" }); 284 try repo.removeBranch(io, .{ .name = "feature" }); 285 _ = try repo.garbageCollect(io, allocator, .{}, null); 286 { 287 var moment = try repo.core.latestMoment(); 288 const state = rp.Repo(.xit, opts).State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 289 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, opts).initCommit(state, io, allocator, &result_oid)); 290 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, opts).initCommit(state, io, allocator, &target_oid)); 291 } 292 const diff_after = try ui.Repo.Diff.init(.xit, opts, &arena, &repo, io, allocator, diff_route.repo_diff); 293 try std.testing.expectEqualDeep(diff_before.window, diff_after.window); 294 const files = try ui.Repo.Files.initPatchRev(.xit, opts, &arena, &repo, io, allocator, .{ .location = .{ .repo = "" } }, &merged_status.patchrev_id, "base.txt", 0); 295 try std.testing.expectEqual(2, files.entries.len); 296 try std.testing.expectEqualStrings("base.txt", files.entries[0].name); 297 try std.testing.expectEqualStrings("source", files.entries[0].lines[0]); 298 const files_route = files.filesRoute("base.txt", 0) orelse return error.BadRoute; 299 try std.testing.expectEqualStrings(&merged_status.patchrev_id, files_route.repo_files.patchrev_id.slice()); 300 301 // opening either tab must not load a commit log from the pruned history 302 var session = ui.Session{ .io = io, .arena = &arena, .page_arena = &arena, .local = .{ .path = path, .repo_kind = .xit }, .data = .{ .host_kind = .local } }; 303 for ([_]ui.RoutablePage{ diff_route, files_route }) |route| { 304 session.data.current_page = route; 305 const page = try ui.Repo.init(&arena, &session, route); 306 const diff = switch (page.changes) { 307 .diff => |diff| diff, 308 .commits => return error.MissingDiff, 309 }; 310 try std.testing.expectEqualDeep(diff_before.window, diff.window); 311 try std.testing.expectEqualStrings(&merged_status.patchrev_id, page.files.patchrev_id orelse return error.MissingRevision); 312 } 313 } 314 315 fn commitTree( 316 io: std.Io, 317 allocator: std.mem.Allocator, 318 repo: *Repo, 319 oid: *const [hash.hexLen(repo_opts.hash)]u8, 320 ) ![hash.hexLen(repo_opts.hash)]u8 { 321 var moment = try repo.core.latestMoment(); 322 const state = Repo.State(.read_only){ .core = &repo.core, .extra = .{ .moment = &moment } }; 323 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 324 defer object.deinit(); 325 return switch (object.content) { 326 .commit => |commit| commit.tree, 327 else => error.InvalidObject, 328 }; 329 } 330 331 fn consumePatchWithRevision( 332 comptime role: evt.RepoRole, 333 io: std.Io, 334 allocator: std.mem.Allocator, 335 repo: *Repo, 336 ref: rf.Ref, 337 revision_id: [evt.event_id_size]u8, 338 revision: evt.PatchRev, 339 tree_entries: []const evt.EventTreeEntry, 340 revision_timestamp: u64, 341 patch_id: [evt.event_id_size]u8, 342 patch_value: evt.Patch, 343 patch_timestamp: u64, 344 ) !void { 345 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 346 .id = std.fmt.bytesToHex(revision_id, .lower), 347 .timestamp = revision_timestamp, 348 .author = author, 349 .tree_entries = tree_entries, 350 .event = .{ .patchrev = revision }, 351 }}); 352 353 var arena = std.heap.ArenaAllocator.init(allocator); 354 defer arena.deinit(); 355 const moment = try evt.currentMoment(repo_opts, repo); 356 const record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &revision_id)) orelse return error.NotFound; 357 var patch = patch_value; 358 patch.revision = evt.Patch.Revision.fromRecord(revision_id, record); 359 try evt.consume(.local, role, .xit, repo_opts, io, allocator, repo, ref, &.{.{ 360 .id = std.fmt.bytesToHex(patch_id, .lower), 361 .timestamp = patch_timestamp, 362 .author = author, 363 .event = .{ .patch = patch }, 364 }}); 365 } 366 367 test "patch event conflicts, stacking, and gc" { 368 const io = std.testing.io; 369 const allocator = std.testing.allocator; 370 371 // create the temp dir 372 var temp = std.testing.tmpDir(.{}); 373 defer temp.cleanup(); 374 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 375 defer allocator.free(temp_path); 376 377 const upstream_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "upstream" }); 378 defer allocator.free(upstream_path); 379 const target_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "target" }); 380 defer allocator.free(target_path); 381 const source_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "source" }); 382 defer allocator.free(source_path); 383 384 // 385 // create the upstream repo and its base commit 386 // 387 388 const base_oid = blk: { 389 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 390 defer upstream.deinit(io, allocator); 391 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 392 defer file.close(io); 393 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 394 try upstream.add(io, allocator, &.{"main.zig"}); 395 break :blk try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 396 }; 397 398 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 399 defer target.deinit(io, allocator); 400 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 401 402 // 403 // fork the target and commit the proposed change 404 // 405 406 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 407 defer source.deinit(io, allocator); 408 409 const source_oid = blk: { 410 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 411 defer file.close(io); 412 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 413 try source.add(io, allocator, &.{"feature.zig"}); 414 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 415 }; 416 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 417 418 // 419 // copy the proposed objects without creating a target ref 420 // 421 422 { 423 var source_moment = try source.core.latestMoment(); 424 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 425 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 426 defer objects.deinit(); 427 try objects.include(&source_oid); 428 try target.copyObjects(.xit, repo_opts, &objects, io, null); 429 } 430 431 // 432 // define the patch revision and patch 433 // 434 435 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 436 const patchrev_id = evt.EventWithId.randomId(prng.random()); 437 const patch_id = evt.EventWithId.randomId(prng.random()); 438 const patch = evt.Patch{ 439 .title = "add the answer", 440 .description = "adds a reusable answer constant", 441 .labels = "enhancement", 442 .target_branch = "master", 443 }; 444 const tree_entries = [_]evt.EventTreeEntry{ 445 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 446 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 447 }; 448 449 // reject invalid branches even when the event bypasses patch creation 450 { 451 const cases = [_]struct { source: ?[]const u8, target: []const u8, expected_error: anyerror }{ 452 .{ .source = "master", .target = "master", .expected_error = error.SameBranch }, 453 .{ .source = null, .target = evt.events_ref.name, .expected_error = error.InvalidTargetBranch }, 454 }; 455 for (cases) |case| { 456 var invalid = patch; 457 invalid.source_branch = case.source; 458 invalid.target_branch = case.target; 459 try std.testing.expectError(case.expected_error, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 460 .id = std.fmt.bytesToHex(patch_id, .lower), 461 .timestamp = 3, 462 .author = author, 463 .event = .{ .patch = invalid }, 464 }})); 465 try std.testing.expectEqual(null, try target.readRef(io, evt.events_ref)); 466 } 467 } 468 469 // 470 // consume the patch revision and patch 471 // 472 473 { 474 try consumePatchWithRevision( 475 .repo, 476 io, 477 allocator, 478 &target, 479 evt.events_ref, 480 patchrev_id, 481 .{ .base_oid = &base_oid, .source_oid = &source_oid, .message = patch.title }, 482 &tree_entries, 483 3, 484 patch_id, 485 patch, 486 4, 487 ); 488 } 489 490 // 491 // check the records and squash commit 492 // 493 494 var arena = std.heap.ArenaAllocator.init(allocator); 495 defer arena.deinit(); 496 const moment = try evt.currentMoment(repo_opts, &target); 497 const patch_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 498 const patchrev_record = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &patchrev_id)) orelse return error.NotFound; 499 const records_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.record_map_key)) orelse return error.NotFound; 500 const records = try Repo.DB.HashMap(.read_only).init(records_cursor); 501 const record_cursor = try records.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 502 const record_map = try Repo.DB.HashMap(.read_only).init(record_cursor); 503 const event_cursor = try record_map.getCursor(hash.hashInt(repo_opts.hash, "event")) orelse return error.NotFound; 504 const event_map = try Repo.DB.HashMap(.read_only).init(event_cursor); 505 const revision_cursor = try event_map.getCursor(hash.hashInt(repo_opts.hash, "revision")) orelse return error.NotFound; 506 const revision_map = try Repo.DB.HashMap(.read_only).init(revision_cursor); 507 try std.testing.expect(null != try revision_map.getCursor(hash.hashInt(repo_opts.hash, "id"))); 508 try std.testing.expectEqual(null, try record_map.getCursor(hash.hashInt(repo_opts.hash, "title"))); 509 try std.testing.expectEqualStrings(&base_tree_oid, patchrev_record.base_tree_oid); 510 try std.testing.expectEqualStrings(&head_tree_oid, patchrev_record.head_tree_oid); 511 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_id, .lower), &(patch_record.event.revision orelse return error.NotFound).id); 512 513 var initial_patch_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 514 @memcpy(&initial_patch_oid, patchrev_record.patch_oid); 515 { 516 var repo_moment = try target.core.latestMoment(); 517 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &repo_moment } }; 518 var patch_object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &initial_patch_oid); 519 defer patch_object.deinit(); 520 switch (patch_object.content) { 521 .commit => |commit| { 522 try std.testing.expectEqualStrings(&head_tree_oid, &commit.tree); 523 const parents = commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 524 try std.testing.expectEqual(1, parents.len); 525 try std.testing.expectEqualStrings(&base_oid, &parents[0]); 526 }, 527 else => return error.InvalidObject, 528 } 529 } 530 531 // 532 // update metadata without creating another patch revision 533 // 534 535 var updated_patch = patch; 536 updated_patch.revision = .{ 537 .id = std.fmt.bytesToHex(patchrev_id, .lower), 538 .squash_oid = &initial_patch_oid, 539 .source_oid = &source_oid, 540 }; 541 updated_patch.title = "add a reusable answer"; 542 { 543 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 544 .id = std.fmt.bytesToHex(patch_id, .lower), 545 .timestamp = 5, 546 .author = author, 547 .event = .{ .patch = updated_patch }, 548 }}); 549 } 550 551 _ = arena.reset(.retain_capacity); 552 const updated_moment = try evt.currentMoment(repo_opts, &target); 553 const updated = (try evt.Patch.readById(Repo.DB, repo_opts.hash, updated_moment, &arena, &patch_id)) orelse return error.NotFound; 554 try std.testing.expectEqualStrings("add a reusable answer", updated.event.title); 555 556 // 557 // branch the events history before creating competing revisions 558 // 559 560 const side_events_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-side" }; 561 { 562 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 563 defer result.deinit(); 564 try target.addBranch(io, .{ .name = side_events_ref.name }); 565 } 566 567 // 568 // create two competing code revisions 569 // 570 571 const source_a_oid = blk: { 572 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 573 defer file.close(io); 574 try file.writeStreamingAll(io, "pub const answer = 43;\n"); 575 try source.add(io, allocator, &.{"feature.zig"}); 576 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature one way", .timestamp = 6 }); 577 }; 578 const source_a_tree = try commitTree(io, allocator, &source, &source_a_oid); 579 const source_b_oid = blk: { 580 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 581 defer file.close(io); 582 try file.writeStreamingAll(io, "pub const answer = 44;\n"); 583 try source.add(io, allocator, &.{"feature.zig"}); 584 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "revise feature another way", .timestamp = 7 }); 585 }; 586 const source_b_tree = try commitTree(io, allocator, &source, &source_b_oid); 587 { 588 var source_moment = try source.core.latestMoment(); 589 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 590 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 591 defer objects.deinit(); 592 try objects.include(&source_b_oid); 593 try target.copyObjects(.xit, repo_opts, &objects, io, null); 594 } 595 596 // 597 // select the first revision on the events branch 598 // 599 600 const patchrev_a_id = evt.EventWithId.randomId(prng.random()); 601 const target_entries = [_]evt.EventTreeEntry{ 602 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 603 .{ .tree = .{ .name = "head", .oid = &source_a_tree } }, 604 }; 605 var target_patch = updated_patch; 606 target_patch.status = .closed; 607 { 608 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, patchrev_a_id, .{ 609 .base_oid = &base_oid, 610 .source_oid = &source_a_oid, 611 .message = updated_patch.title, 612 }, &target_entries, 8, patch_id, target_patch, 9); 613 } 614 615 // 616 // select the second revision on the side branch 617 // 618 619 const patchrev_b_id = evt.EventWithId.randomId(prng.random()); 620 const parent_entries = [_]evt.EventTreeEntry{ 621 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 622 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 623 }; 624 var parent_patch = updated_patch; 625 parent_patch.status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(patchrev_b_id, .lower) } }; 626 { 627 try consumePatchWithRevision(.repo, io, allocator, &target, side_events_ref, patchrev_b_id, .{ 628 .base_oid = &base_oid, 629 .source_oid = &source_b_oid, 630 .message = updated_patch.title, 631 }, &parent_entries, 10, patch_id, parent_patch, 11); 632 } 633 634 // 635 // merge the competing patch revisions 636 // 637 638 { 639 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, side_events_ref); 640 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 641 } 642 643 // 644 // check the selected revision and conflict 645 // 646 647 _ = arena.reset(.retain_capacity); 648 const merged_moment = try evt.currentMoment(repo_opts, &target); 649 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 650 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_a_id, .lower), &(merged.event.revision orelse return error.NotFound).id); 651 const merged_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patchrev_a_id)) orelse return error.NotFound; 652 try std.testing.expectEqualStrings(&source_a_oid, merged_patchrev.event.source_oid); 653 try std.testing.expectEqualStrings(&source_a_tree, merged_patchrev.head_tree_oid); 654 655 const conflicts_cursor = try merged_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 656 const conflicts = try Repo.DB.SortedMap(.read_only).init(conflicts_cursor); 657 const conflict_cursor = try conflicts.getCursor(&evt.orderKeyDesc(merged.created_order, &patch_id)) orelse return error.NotFound; 658 const conflict = try Repo.DB.HashMap(.read_only).init(conflict_cursor); 659 const fields_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key)) orelse return error.NotFound; 660 try std.testing.expectEqualStrings("revision status", try fields_cursor.readBytesAlloc(arena.allocator(), null)); 661 const their_cursor = try conflict.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key)) orelse return error.NotFound; 662 const theirs = try evt.read(evt.Patch.Record, Repo.DB, repo_opts.hash, &arena, try Repo.DB.HashMap(.read_only).init(their_cursor)); 663 try std.testing.expectEqualStrings(&std.fmt.bytesToHex(patchrev_b_id, .lower), &(theirs.event.revision orelse return error.NotFound).id); 664 665 // 666 // create a patch stacked on the selected revision 667 // 668 669 const child_patchrev_id = evt.EventWithId.randomId(prng.random()); 670 const child_id = evt.EventWithId.randomId(prng.random()); 671 const child_entries = [_]evt.EventTreeEntry{ 672 .{ .tree = .{ .name = "base", .oid = merged_patchrev.head_tree_oid } }, 673 .{ .tree = .{ .name = "head", .oid = &source_b_tree } }, 674 }; 675 { 676 try consumePatchWithRevision(.repo, io, allocator, &target, evt.events_ref, child_patchrev_id, .{ 677 .base_oid = merged_patchrev.patch_oid, 678 .source_oid = &source_b_oid, 679 .message = "stack another answer change", 680 }, &child_entries, 12, child_id, .{ 681 .title = "stack another answer change", 682 .description = "depends on the first patch", 683 .labels = "enhancement", 684 .target_branch = "master", 685 .target_patch_id = std.fmt.bytesToHex(patch_id, .lower), 686 }, 13); 687 } 688 689 // 690 // check the squash commit and reverse index 691 // 692 693 _ = arena.reset(.retain_capacity); 694 const stacked_moment = try evt.currentMoment(repo_opts, &target); 695 const child = (try evt.Patch.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &child_id)) orelse return error.NotFound; 696 var selected_patchrev_id: [evt.event_id_size]u8 = undefined; 697 _ = try std.fmt.hexToBytes(&selected_patchrev_id, &(child.event.revision orelse return error.NotFound).id); 698 const child_patchrev = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, stacked_moment, &arena, &selected_patchrev_id)) orelse return error.NotFound; 699 var child_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 700 @memcpy(&child_oid, child_patchrev.patch_oid); 701 { 702 var child_repo_moment = try target.core.latestMoment(); 703 const child_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &child_repo_moment } }; 704 var child_object = try obj.Object(.xit, repo_opts).init(child_state, io, allocator, &child_oid); 705 defer child_object.deinit(); 706 const child_commit = switch (child_object.content) { 707 .commit => |commit| commit, 708 else => return error.InvalidObject, 709 }; 710 const child_parents = child_commit.metadata.parent_oids orelse return error.ParentOidsNotFound; 711 try std.testing.expectEqualStrings(child_patchrev.event.base_oid, &child_parents[0]); 712 } 713 714 const targets_cursor = try stacked_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.target_patch_id_to_patch_id_set_key)) orelse return error.NotFound; 715 const targets = try Repo.DB.HashMap(.read_only).init(targets_cursor); 716 const children_cursor = try targets.getCursor(hash.hashInt(repo_opts.hash, &patch_id)) orelse return error.NotFound; 717 const children = try Repo.DB.SortedSet(.read_only).init(children_cursor); 718 try std.testing.expectEqual(1, try children.count()); 719 720 // 721 // close and merge the same revision concurrently 722 // 723 724 const status_side_ref: rf.Ref = .{ .kind = .head, .name = "haxy/patch-status-side" }; 725 { 726 var result = try target.switchDir(io, allocator, .{ .target = .{ .ref = evt.events_ref } }); 727 defer result.deinit(); 728 try target.addBranch(io, .{ .name = status_side_ref.name }); 729 } 730 var closed_child = child.event; 731 closed_child.status = .closed; 732 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 733 .id = std.fmt.bytesToHex(child_id, .lower), 734 .timestamp = 14, 735 .author = author, 736 .event = .{ .patch = closed_child }, 737 }}); 738 var merged_child = child.event; 739 merged_child.status = .{ .merged = .{ .revision = .source, .patchrev_id = (child.event.revision orelse return error.NotFound).id } }; 740 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, status_side_ref, &.{.{ 741 .id = std.fmt.bytesToHex(child_id, .lower), 742 .timestamp = 15, 743 .author = author, 744 .event = .{ .patch = merged_child }, 745 }}); 746 try evt.mergeEvents(.xit, repo_opts, io, allocator, &target, status_side_ref); 747 try evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{}); 748 749 _ = arena.reset(.retain_capacity); 750 const status_moment = try evt.currentMoment(repo_opts, &target); 751 const merged_child_record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, status_moment, &arena, &child_id)) orelse return error.NotFound; 752 try std.testing.expectEqual(.merged, merged_child_record.event.status.kind()); 753 try std.testing.expectEqual(0, try patchStatusCount(status_moment, .open)); 754 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .closed)); 755 try std.testing.expectEqual(1, try patchStatusCount(status_moment, .merged)); 756 try std.testing.expectEqual(1, try patchLabelStatusCount(status_moment, "enhancement", .merged)); 757 const status_conflicts_cursor = try status_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key)) orelse return error.NotFound; 758 const status_conflicts = try Repo.DB.SortedMap(.read_only).init(status_conflicts_cursor); 759 const child_conflict = try status_conflicts.getCursor(&evt.orderKeyDesc(merged_child_record.created_order, &child_id)); 760 try std.testing.expectEqual(null, child_conflict); 761 762 var reopened_child = merged_child_record.event; 763 reopened_child.status = .open; 764 try std.testing.expectError(error.PatchAlreadyMerged, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 765 .id = std.fmt.bytesToHex(child_id, .lower), 766 .timestamp = 16, 767 .author = author, 768 .event = .{ .patch = reopened_child }, 769 }})); 770 771 const invalid_merged_id = evt.EventWithId.randomId(prng.random()); 772 try std.testing.expectError(error.InvalidPatch, evt.consume(.local, .repo, .xit, repo_opts, io, allocator, &target, evt.events_ref, &.{.{ 773 .id = std.fmt.bytesToHex(invalid_merged_id, .lower), 774 .timestamp = 16, 775 .author = author, 776 .event = .{ .patch = .{ 777 .title = "invalid merged patch", 778 .description = "has no revision", 779 .labels = "enhancement", 780 .target_branch = "master", 781 .status = .{ .merged = .{ .revision = .source, .patchrev_id = std.fmt.bytesToHex(invalid_merged_id, .lower) } }, 782 } }, 783 }})); 784 785 // 786 // retain the ref-free commits during garbage collection 787 // 788 789 { 790 const roots = try evt.PatchRev.gcRoots(Repo.DB, repo_opts.hash, allocator, stacked_moment); 791 defer allocator.free(roots); 792 try std.testing.expectEqual(4, roots.len); 793 _ = try target.garbageCollect(io, allocator, .{ .extra_roots = roots }, null); 794 795 var after_gc_moment = try target.core.latestMoment(); 796 const after_gc_state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &after_gc_moment } }; 797 var retained = try obj.Object(.xit, repo_opts).init(after_gc_state, io, allocator, &roots[0]); 798 retained.deinit(); 799 } 800 } 801 802 test "patch lifecycle" { 803 try patchLifecycle(.squash); 804 try patchLifecycle(.source); 805 } 806 807 fn patchLifecycle(merge_revision: evt.Patch.MergeRevision) !void { 808 const io = std.testing.io; 809 const allocator = std.testing.allocator; 810 811 var temp = std.testing.tmpDir(.{}); 812 defer temp.cleanup(); 813 const temp_path = try temp.dir.realPathFileAlloc(io, ".", allocator); 814 defer allocator.free(temp_path); 815 816 const users_dir = try std.Io.Dir.path.join(allocator, &.{ temp_path, "users" }); 817 defer allocator.free(users_dir); 818 const admin_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "admin" }); 819 defer allocator.free(admin_path); 820 const upstream_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "upstream" }); 821 defer allocator.free(upstream_path); 822 const source_path = try std.Io.Dir.path.join(allocator, &.{ temp_path, "source" }); 823 defer allocator.free(source_path); 824 825 var prng = std.Random.DefaultPrng.init(std.testing.random_seed); 826 const user_id = evt.EventWithId.randomId(prng.random()); 827 const repo_id = evt.EventWithId.randomId(prng.random()); 828 const repo_id_hex = std.fmt.bytesToHex(repo_id, .lower); 829 const target_path = try evt.repoPath(allocator, users_dir, &user_id, &repo_id); 830 defer allocator.free(target_path); 831 832 var admin = try rp.Repo(.xit, evt.admin_repo_opts).init(io, allocator, .{ .path = admin_path }); 833 defer admin.deinit(io, allocator); 834 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .admin, .xit, evt.admin_repo_opts, io, allocator, &admin, evt.events_ref, &.{ 835 .{ 836 .id = std.fmt.bytesToHex(user_id, .lower), 837 .timestamp = 1, 838 .author = author, 839 .event = .{ .user = .{ 840 .name = author.name, 841 .email = author.email, 842 .password_hash = "", 843 } }, 844 }, 845 }); 846 const user_repo_path = try evt.userRepoPath(allocator, users_dir, &user_id); 847 defer allocator.free(user_repo_path); 848 var user_repo = try evt.initUserRepo(io, allocator, user_repo_path); 849 defer user_repo.deinit(io, allocator); 850 try evt.consume(.{ .server = .{ .users_dir = users_dir } }, .user, .xit, evt.user_repo_opts, io, allocator, &user_repo, evt.events_ref, &.{ 851 .{ 852 .id = repo_id_hex, 853 .timestamp = 1, 854 .author = author, 855 .event = .{ .repo = .{ 856 .user_id = std.fmt.bytesToHex(user_id, .lower), 857 .name = "repo", 858 .description = "", 859 .patch_role = .read, 860 } }, 861 }, 862 }); 863 864 // 865 // create the target repo 866 // 867 868 { 869 var upstream = try rp.Repo(.git, .{ .is_test = true }).init(io, allocator, .{ .path = upstream_path }); 870 defer upstream.deinit(io, allocator); 871 const file = try upstream.core.work_dir.createFile(io, "main.zig", .{ .truncate = true }); 872 defer file.close(io); 873 try file.writeStreamingAll(io, "pub fn main() void {}\n"); 874 try upstream.add(io, allocator, &.{"main.zig"}); 875 _ = try upstream.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "initial code", .timestamp = 1 }); 876 } 877 var target = try Repo.clone(io, allocator, upstream_path, target_path, target_path, null, .{}); 878 defer target.deinit(io, allocator); 879 880 // 881 // create the patch draft in the fork 882 // 883 884 const patch_id = evt.EventWithId.randomId(prng.random()); 885 const patch_id_hex = std.fmt.bytesToHex(patch_id, .lower); 886 const draft_path = try fork.create(.{ .is_test = true }, io, allocator, users_dir, .{ 887 .target_branch = "master", 888 .id = patch_id_hex, 889 .user_id = user_id, 890 .repo_id = repo_id, 891 .repo_user_id = user_id, 892 .repo_name = "target", 893 .title = "add the answer", 894 .labels = "enhancement", 895 .description = "adds a reusable answer constant", 896 .author = author, 897 .timestamp = 2, 898 }); 899 defer allocator.free(draft_path); 900 901 // 902 // create and record the proposed revision in the fork 903 // 904 905 const base_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 906 const base_tree_oid = try commitTree(io, allocator, &target, &base_oid); 907 var source = try Repo.clone(io, allocator, upstream_path, source_path, source_path, null, .{}); 908 defer source.deinit(io, allocator); 909 const source_oid = blk: { 910 const file = try source.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 911 defer file.close(io); 912 try file.writeStreamingAll(io, "pub const answer = 42;\n"); 913 try source.add(io, allocator, &.{"feature.zig"}); 914 break :blk try source.commit(io, allocator, .{ .author = "alice <alice@example.test>", .message = "add feature", .timestamp = 2 }); 915 }; 916 const head_tree_oid = try commitTree(io, allocator, &source, &source_oid); 917 const first_patchrev_id = evt.EventWithId.randomId(prng.random()); 918 const first_patchrev_hex = std.fmt.bytesToHex(first_patchrev_id, .lower); 919 const first_entries = [_]evt.EventTreeEntry{ 920 .{ .tree = .{ .name = "base", .oid = &base_tree_oid } }, 921 .{ .tree = .{ .name = "head", .oid = &head_tree_oid } }, 922 }; 923 { 924 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 925 defer draft.deinit(io, allocator); 926 var arena = std.heap.ArenaAllocator.init(allocator); 927 defer arena.deinit(); 928 929 const initial_moment = try evt.currentMoment(repo_opts, &draft); 930 const initial_patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, initial_moment, &arena, &patch_id)) orelse return error.NotFound; 931 try std.testing.expect(!initial_patch.removed); 932 try std.testing.expectEqualStrings("add the answer", initial_patch.event.title); 933 try std.testing.expectEqual(null, initial_patch.event.revision); 934 try std.testing.expectEqual(null, try evt.PatchRev.readNewest(Repo.DB, repo_opts.hash, initial_moment, &arena)); 935 936 var source_moment = try source.core.latestMoment(); 937 const source_state = Repo.State(.read_only){ .core = &source.core, .extra = .{ .moment = &source_moment } }; 938 var objects = try obj.ObjectIterator(.xit, repo_opts).init(source_state, io, allocator, .{ .kind = .all }); 939 defer objects.deinit(); 940 try objects.include(&source_oid); 941 try draft.copyObjects(.xit, repo_opts, &objects, io, null); 942 try consumePatchWithRevision(.fork, io, allocator, &draft, evt.events_ref, first_patchrev_id, .{ 943 .base_oid = &base_oid, 944 .source_oid = &source_oid, 945 .message = "add the answer", 946 }, &first_entries, 3, patch_id, .{ 947 .title = "add the answer", 948 .labels = "enhancement", 949 .description = "adds a reusable answer constant", 950 .target_branch = "master", 951 }, 3); 952 953 _ = arena.reset(.retain_capacity); 954 const moment = try evt.currentMoment(repo_opts, &draft); 955 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 956 try std.testing.expect(!patch.removed); 957 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 958 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 959 } 960 try std.testing.expectError(error.NotFound, evt.currentMoment(repo_opts, &target)); 961 962 // draft mergeability checks leave the target repo untouched 963 const before_draft_check = (try target.core.latestMoment()).cursor.slot(); 964 pch.refreshMergeability(repo_opts, io, allocator, &target, users_dir, patch_id); 965 try std.testing.expectEqualDeep(before_draft_check, (try target.core.latestMoment()).cursor.slot()); 966 967 // 968 // publish another draft before its first push 969 // 970 971 const unpushed_id = evt.EventWithId.randomId(prng.random()); 972 const unpushed_id_hex = std.fmt.bytesToHex(unpushed_id, .lower); 973 const unpushed_path = try fork.create(.{ .is_test = true }, io, allocator, users_dir, .{ 974 .id = unpushed_id_hex, 975 .user_id = user_id, 976 .repo_id = repo_id, 977 .repo_user_id = user_id, 978 .repo_name = "target", 979 .title = "explain the answer", 980 .labels = "documentation", 981 .description = "describes the existing answer", 982 .target_branch = "master", 983 .author = author, 984 .timestamp = 3, 985 }); 986 defer allocator.free(unpushed_path); 987 try pch.publish(repo_opts, io, allocator, .{ .users_dir = users_dir }, &admin, &target, .{ 988 .id = unpushed_id_hex, 989 .user_id = user_id, 990 .repo_id = repo_id, 991 .author = author, 992 .timestamp = 4, 993 }); 994 { 995 var verify_arena = std.heap.ArenaAllocator.init(allocator); 996 defer verify_arena.deinit(); 997 const target_moment = try evt.currentMoment(repo_opts, &target); 998 const unpushed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, target_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 999 try std.testing.expectEqual(null, unpushed.event.revision); 1000 1001 var unpushed_repo = try Repo.open(io, allocator, .{ .path = unpushed_path }); 1002 defer unpushed_repo.deinit(io, allocator); 1003 const fork_moment = try evt.currentMoment(repo_opts, &unpushed_repo); 1004 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, fork_moment, &verify_arena, &unpushed_id)) orelse return error.NotFound; 1005 try std.testing.expect(removed.removed); 1006 } 1007 1008 // 1009 // edit the patch metadata in the fork 1010 // 1011 1012 try std.testing.expect(try pch.editDraft(repo_opts, io, allocator, users_dir, &admin, .{ 1013 .id = patch_id_hex, 1014 .user_id = user_id, 1015 .repo_id = repo_id, 1016 .title = "explain the answer", 1017 .labels = "enhancement documentation", 1018 .description = "adds and explains a reusable answer constant", 1019 .target_branch = "master", 1020 .author = author, 1021 .timestamp = 4, 1022 })); 1023 1024 // 1025 // publish the metadata and revision pointer 1026 // 1027 1028 const before_publish = try target.core.db.rootCursor().count(); 1029 try pch.publish(repo_opts, io, allocator, .{ .users_dir = users_dir }, &admin, &target, .{ 1030 .id = patch_id_hex, 1031 .user_id = user_id, 1032 .repo_id = repo_id, 1033 .author = author, 1034 .timestamp = 5, 1035 }); 1036 try std.testing.expectEqual(before_publish + 1, try target.core.db.rootCursor().count()); 1037 try pch.publish(repo_opts, io, allocator, .{ .users_dir = users_dir }, &admin, &target, .{ 1038 .id = patch_id_hex, 1039 .user_id = user_id, 1040 .repo_id = repo_id, 1041 .author = author, 1042 .timestamp = 6, 1043 }); 1044 1045 var arena = std.heap.ArenaAllocator.init(allocator); 1046 defer arena.deinit(); 1047 const moment = try evt.currentMoment(repo_opts, &target); 1048 const patch = (try evt.Patch.readById(Repo.DB, repo_opts.hash, moment, &arena, &patch_id)) orelse return error.NotFound; 1049 try std.testing.expectEqualStrings("explain the answer", patch.event.title); 1050 try std.testing.expectEqualStrings(&first_patchrev_hex, &(patch.event.revision orelse return error.NotFound).id); 1051 try std.testing.expectEqual(.open, patch.event.status); 1052 try std.testing.expectEqual(null, try evt.PatchRev.readById(Repo.DB, repo_opts.hash, moment, &arena, &first_patchrev_id)); 1053 1054 _ = arena.reset(.retain_capacity); 1055 const user_moment = try evt.currentMoment(evt.user_repo_opts, &user_repo); 1056 const published_fork = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, user_moment, &arena, &patch_id)) orelse return error.NotFound; 1057 try std.testing.expect(!published_fork.removed); 1058 try std.testing.expectEqual(.publish, published_fork.event.stage); 1059 try std.testing.expectEqual(0, try draftForkCount(user_moment, &repo_id)); 1060 try std.testing.expectEqual(2, try activeForkCount(user_moment)); 1061 var squash_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1062 { 1063 var draft = try Repo.open(io, allocator, .{ .path = draft_path }); 1064 defer draft.deinit(io, allocator); 1065 _ = arena.reset(.retain_capacity); 1066 const draft_moment = try evt.currentMoment(repo_opts, &draft); 1067 const removed = (try evt.Patch.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &patch_id)) orelse return error.NotFound; 1068 try std.testing.expect(removed.removed); 1069 const revision = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, draft_moment, &arena, &first_patchrev_id)) orelse return error.NotFound; 1070 @memcpy(&squash_oid, revision.patch_oid); 1071 try std.testing.expectEqualStrings("master", removed.event.target_branch); 1072 try std.testing.expectEqual(hash.hexLen(repo_opts.hash), revision.event_oid.len); 1073 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1074 @memcpy(&event_oid, revision.event_oid); 1075 var repo_moment = try draft.core.latestMoment(); 1076 const state = Repo.State(.read_only){ .core = &draft.core, .extra = .{ .moment = &repo_moment } }; 1077 var event_object = try obj.Object(.xit, repo_opts).initCommit(state, io, allocator, &event_oid); 1078 defer event_object.deinit(); 1079 const trees = try evt.PatchRev.readTrees(.xit, repo_opts, state, io, allocator, &event_object.content.commit.tree); 1080 try std.testing.expectEqualStrings(revision.base_tree_oid, &trees.base); 1081 try std.testing.expectEqualStrings(revision.head_tree_oid, &trees.head); 1082 } 1083 1084 // 1085 // merge the selected revision into the target 1086 // 1087 1088 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .closed }, author); 1089 try std.testing.expectError(error.PatchClosed, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, &target, .{ 1090 .id = patch_id_hex, 1091 .revision = merge_revision, 1092 .author = author, 1093 .timestamp = 5, 1094 })); 1095 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, &target, &patch_id, .{ .status = .open }, author); 1096 1097 // run the cache scenario once; both lifecycle runs still perform a merge 1098 if (merge_revision == .source) { 1099 try testMergeability(&target, io, allocator, users_dir, &user_id, &patch_id, &base_oid, &source_oid); 1100 } 1101 1102 const selected_oid = switch (merge_revision) { 1103 .squash => squash_oid, 1104 .source => source_oid, 1105 }; 1106 try pch.mergeAndRemoveFork(repo_opts, io, allocator, .{ .users_dir = users_dir }, &target, .{ 1107 .id = patch_id_hex, 1108 .revision = merge_revision, 1109 .author = author, 1110 .timestamp = 5, 1111 }); 1112 const target_oid = (try target.readRef(io, .{ .kind = .head, .name = "master" })) orelse return error.NotFound; 1113 try std.testing.expectEqualStrings(&selected_oid, &target_oid); 1114 1115 _ = arena.reset(.retain_capacity); 1116 const merged_moment = try evt.currentMoment(repo_opts, &target); 1117 const merged = (try evt.Patch.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &patch_id)) orelse return error.NotFound; 1118 try std.testing.expectEqual(.merged, merged.event.status.kind()); 1119 const merged_status = switch (merged.event.status) { 1120 .merged => |value| value, 1121 else => return error.InvalidPatch, 1122 }; 1123 try std.testing.expectEqual(merge_revision, merged_status.revision); 1124 const result_id = try evt.parseEventId(&merged_status.patchrev_id); 1125 const result = (try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &result_id)) orelse return error.NotFound; 1126 try std.testing.expectEqualStrings(&base_oid, result.event.base_oid); 1127 try std.testing.expectEqualStrings(&selected_oid, result.event.source_oid); 1128 try std.testing.expect(null != try evt.PatchRev.readById(Repo.DB, repo_opts.hash, merged_moment, &arena, &first_patchrev_id)); 1129 1130 _ = arena.reset(.retain_capacity); 1131 const removed_fork = (try evt.Fork.readById(evt.UserDB, evt.user_repo_opts.hash, try evt.currentMoment(evt.user_repo_opts, &user_repo), &arena, &patch_id)) orelse return error.NotFound; 1132 try std.testing.expect(removed_fork.removed); 1133 } 1134 1135 fn draftForkCount(moment: evt.UserDB.HashMap(.read_only), repo_id: []const u8) !u64 { 1136 const index_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.repo_to_draft_id_set_key)) orelse return 0; 1137 const index = try evt.UserDB.HashMap(.read_only).init(index_cursor); 1138 const ids_cursor = try index.getCursor(hash.hashInt(evt.user_repo_opts.hash, repo_id)) orelse return 0; 1139 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1140 return try ids.count(); 1141 } 1142 1143 fn activeForkCount(moment: evt.UserDB.HashMap(.read_only)) !u64 { 1144 const ids_cursor = try moment.getCursor(hash.hashInt(evt.user_repo_opts.hash, evt.Fork.active_id_set_key)) orelse return 0; 1145 const ids = try evt.UserDB.SortedSet(.read_only).init(ids_cursor); 1146 return try ids.count(); 1147 } 1148 1149 fn patchStatusCount(moment: Repo.DB.HashMap(.read_only), status: evt.Patch.StatusKind) !u64 { 1150 const statuses_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.status_to_id_set_key)) orelse return 0; 1151 const statuses = try Repo.DB.SortedMap(.read_only).init(statuses_cursor); 1152 const ids_cursor = try statuses.getCursor(@tagName(status)) orelse return 0; 1153 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1154 return try ids.count(); 1155 } 1156 1157 fn patchLabelStatusCount(moment: Repo.DB.HashMap(.read_only), label: []const u8, status: evt.Patch.StatusKind) !u64 { 1158 const labels_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.label_status_to_id_set_key)) orelse return 0; 1159 const labels = try Repo.DB.SortedMap(.read_only).init(labels_cursor); 1160 var key_buffer: evt.Patch.LabelStatusKey = undefined; 1161 const ids_cursor = try labels.getCursor(try evt.Patch.labelStatusKey(&key_buffer, label, status)) orelse return 0; 1162 const ids = try Repo.DB.SortedSet(.read_only).init(ids_cursor); 1163 return try ids.count(); 1164 } 1165 1166 fn readMergeability(repo: *Repo, io: std.Io, allocator: std.mem.Allocator, id: *const [evt.event_id_size]u8, patch: evt.Patch) !pch.Mergeability { 1167 var arena = std.heap.ArenaAllocator.init(allocator); 1168 defer arena.deinit(); 1169 var moment = try repo.core.latestMoment(); 1170 return pch.readMergeability(repo_opts, .{ .core = &repo.core, .extra = .{ .moment = &moment } }, io, &arena, id, patch); 1171 } 1172 1173 fn expectMergeabilityShortBytes(moment: Repo.DB.HashMap(.read_only), id: *const [evt.event_id_size]u8) !void { 1174 try std.testing.expectEqual(null, try moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))); 1175 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment); 1176 const checks = try Repo.DB.HashMap(.read_only).init((try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.patch_id_to_mergeability_key))) orelse return error.NotFound); 1177 const entry = try Repo.DB.HashMap(.read_only).init((try checks.getCursor(hash.hashInt(repo_opts.hash, id))) orelse return error.NotFound); 1178 const result = try Repo.DB.HashMap(.read_only).init((try entry.getCursor(hash.hashInt(repo_opts.hash, "result"))) orelse return error.NotFound); 1179 for ([_][]const u8{ "source", "squash" }) |field| { 1180 const cursor = (try result.getCursor(hash.hashInt(repo_opts.hash, field))) orelse return error.NotFound; 1181 try std.testing.expectEqual(.short_bytes, cursor.slot().tag); 1182 } 1183 } 1184 1185 fn testMergeability( 1186 target: *Repo, 1187 io: std.Io, 1188 allocator: std.mem.Allocator, 1189 users_dir: []const u8, 1190 forker_id: *const [evt.event_id_size]u8, 1191 id: *const [evt.event_id_size]u8, 1192 base: *const [hash.hexLen(repo_opts.hash)]u8, 1193 source: *const [hash.hexLen(repo_opts.hash)]u8, 1194 ) !void { 1195 var arena = std.heap.ArenaAllocator.init(allocator); 1196 defer arena.deinit(); 1197 const events = try evt.currentMoment(repo_opts, target); 1198 const record = (try evt.Patch.readById(Repo.DB, repo_opts.hash, events, &arena, id)) orelse return error.NotFound; 1199 const patch = record.event; 1200 1201 // publication checks both methods, and reads and cache hits do no work 1202 const clean = pch.Mergeability{ .source = .clean, .squash = .clean }; 1203 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1204 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1205 const before = (try target.core.latestMoment()).cursor.slot(); 1206 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1207 try std.testing.expectEqualDeep(before, (try target.core.latestMoment()).cursor.slot()); 1208 { 1209 var moment = try target.core.latestMoment(); 1210 const state = Repo.State(.read_only){ .core = &target.core, .extra = .{ .moment = &moment } }; 1211 try std.testing.expectError(error.ObjectNotFound, obj.Object(.xit, repo_opts).initCommit(state, io, allocator, source)); 1212 } 1213 1214 // a target update invalidates the old result before refresh 1215 { 1216 const file = try target.core.work_dir.createFile(io, "feature.zig", .{ .truncate = true }); 1217 defer file.close(io); 1218 try file.writeStreamingAll(io, "pub const answer = 99;\n"); 1219 } 1220 try target.add(io, allocator, &.{"feature.zig"}); 1221 const conflicting_target = try target.commit(io, allocator, .{ .message = "different answer", .timestamp = 20 }); 1222 const input = pch.MergeInput{ .id = std.fmt.bytesToHex(id.*, .lower), .revision = .source, .author = author, .timestamp = 21 }; 1223 var squash_input = input; 1224 squash_input.revision = .squash; 1225 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1226 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1227 try std.testing.expectError(error.MergeCheckUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, squash_input)); 1228 1229 // local edits and server comments leave checks alone 1230 var edit = evt.Patch.Update{ .fields = .{ 1231 .title = "edit the answer", 1232 .labels = patch.labels, 1233 .description = patch.description, 1234 .target_branch = patch.target_branch, 1235 } }; 1236 try evt.Patch.update(.local, .xit, repo_opts, io, allocator, target, id, edit, author); 1237 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1238 _ = try evt.Comment.create(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, .patch, &input.id, &input.id, "reviewing the answer", author); 1239 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1240 1241 // a server patch edit refreshes its mergeability 1242 edit.fields.title = patch.title; 1243 const before_edit = try target.core.db.rootCursor().count(); 1244 try evt.Patch.update(.{ .server = .{ .users_dir = users_dir } }, .xit, repo_opts, io, allocator, target, id, edit, author); 1245 try std.testing.expectEqual(before_edit + 1, try target.core.db.rootCursor().count()); 1246 const events_before = try target.readRef(io, evt.events_ref); 1247 try std.testing.expectEqualDeep(pch.Mergeability{ .source = .conflict, .squash = .conflict }, try readMergeability(target, io, allocator, id, patch)); 1248 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1249 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1250 try std.testing.expectError(error.MergeConflict, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, squash_input)); 1251 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1252 try std.testing.expectEqual(conflicting_target, (try target.readRef(io, .{ .kind = .head, .name = "master" })).?); 1253 try std.testing.expectEqual(null, try target.readRef(io, .{ .kind = .none, .name = "MERGE_HEAD" })); 1254 1255 // deleting and restoring the target cannot reuse the conflict result 1256 { 1257 var detached = try target.switchDir(io, allocator, .{ .target = .{ .oid = base } }); 1258 defer detached.deinit(); 1259 } 1260 try target.removeBranch(io, .{ .name = "master" }); 1261 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1262 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1263 try expectMergeabilityShortBytes(try target.core.latestMoment(), id); 1264 try target.addBranch(io, .{ .name = "master" }); 1265 { 1266 var restored = try target.switchDir(io, allocator, .{ .target = .{ .ref = .{ .kind = .head, .name = "master" } } }); 1267 defer restored.deinit(); 1268 } 1269 pch.refreshOpenMergeability(repo_opts, io, allocator, target, users_dir, null, null); 1270 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1271 1272 // a missing fork disables merging without changing the accepted events 1273 const draft_path = try fork.forkPath(allocator, users_dir, forker_id, id); 1274 defer allocator.free(draft_path); 1275 const away_path = try allocator.print("{s}.away", .{draft_path}); 1276 defer allocator.free(away_path); 1277 { 1278 try std.Io.Dir.renameAbsolute(draft_path, away_path, io); 1279 defer std.Io.Dir.renameAbsolute(away_path, draft_path, io) catch {}; 1280 { 1281 const log_level = std.testing.log_level; 1282 std.testing.log_level = .err; 1283 defer std.testing.log_level = log_level; 1284 try std.testing.expectError(error.PatchDataUnavailable, pch.merge(repo_opts, io, allocator, .{ .users_dir = users_dir }, target, input)); 1285 } 1286 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, patch)); 1287 try std.testing.expectEqual(events_before, try target.readRef(io, evt.events_ref)); 1288 } 1289 pch.refreshMergeability(repo_opts, io, allocator, target, users_dir, id.*); 1290 try std.testing.expectEqualDeep(clean, try readMergeability(target, io, allocator, id, patch)); 1291 1292 // a changed revision is unknown even if its target has not moved 1293 var changed = patch; 1294 changed.revision.?.id[0] = if (changed.revision.?.id[0] == 'a') 'b' else 'a'; 1295 try std.testing.expectEqualDeep(pch.Mergeability{}, try readMergeability(target, io, allocator, id, changed)); 1296 } 1297