╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 const obj = xit.object; 7 const rf = xit.ref; 8 const mrg = xit.merge; 9 const fork = @import("fork.zig"); 10 const serve_common = @import("serve_common.zig"); 11 12 pub const PublishInput = struct { 13 id: [evt.event_id_size * 2]u8, 14 user_id: [evt.event_id_size]u8, 15 repo_id: [evt.event_id_size]u8, 16 author: evt.CommitAuthor, 17 timestamp: u64, 18 }; 19 20 pub const EditDraftInput = struct { 21 id: [evt.event_id_size * 2]u8, 22 user_id: [evt.event_id_size]u8, 23 repo_id: [evt.event_id_size]u8, 24 title: []const u8, 25 tags: []const u8, 26 description: []const u8, 27 author: evt.CommitAuthor, 28 timestamp: u64, 29 }; 30 31 pub const MergeRevision = enum { squash, source }; 32 33 pub const MergeInput = struct { 34 id: [evt.event_id_size * 2]u8, 35 revision: MergeRevision, 36 author: evt.CommitAuthor, 37 timestamp: u64, 38 }; 39 40 pub fn publish( 41 comptime repo_opts: rp.RepoOpts(.xit), 42 io: std.Io, 43 allocator: std.mem.Allocator, 44 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 45 target_repo: *rp.Repo(.xit, repo_opts), 46 fork_path: []const u8, 47 input: PublishInput, 48 ) !void { 49 const patch_id = try evt.parseEventId(&input.id); 50 var arena = std.heap.ArenaAllocator.init(allocator); 51 defer arena.deinit(); 52 53 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 54 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.InvalidPatchDraft; 55 if (fork_record.removed or 56 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 57 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 58 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 59 if (user.removed or 60 !std.mem.eql(u8, user.event.name, input.author.name) or 61 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 62 63 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path }) catch return error.PatchDataUnavailable; 64 defer fork_repo.deinit(io, allocator); 65 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 66 const local_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 67 68 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 69 error.NotFound => null, 70 else => |other| return other, 71 }; 72 const existing = if (target_moment) |moment| 73 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 74 else 75 null; 76 if (existing) |record| { 77 if (record.removed or !std.mem.eql(u8, record.author_email orelse "", input.author.email)) return error.InvalidPatch; 78 } 79 if (local_patch.removed) { 80 if (existing == null) return error.PatchDataUnavailable; 81 } else { 82 const selected = local_patch.event.revision orelse return error.PatchNotPushed; 83 const revision_id = try evt.parseEventId(&selected.id); 84 const revision = (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return error.PatchNotPushed; 85 if (!selected.matches(revision)) return error.PatchNotPushed; 86 var patch = (existing orelse local_patch).event; 87 patch.revision = selected; 88 if (existing == null) patch.status = .open; 89 90 try evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 91 .id = input.id, 92 .timestamp = input.timestamp, 93 .author = input.author, 94 .event = .{ .patch = patch }, 95 }}); 96 97 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 98 .id = input.id, 99 .timestamp = input.timestamp, 100 .author = input.author, 101 .event = .{ .patch = null }, 102 }}); 103 } 104 105 if (fork_record.event.stage == .draft) { 106 var published = fork_record.event; 107 published.stage = .publish; 108 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 109 .id = input.id, 110 .timestamp = input.timestamp, 111 .author = input.author, 112 .event = .{ .fork = published }, 113 }}); 114 } 115 } 116 117 pub fn editDraft( 118 comptime repo_opts: rp.RepoOpts(.xit), 119 io: std.Io, 120 allocator: std.mem.Allocator, 121 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 122 fork_path: []const u8, 123 input: EditDraftInput, 124 ) !bool { 125 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidFields; 126 const patch_id = try evt.parseEventId(&input.id); 127 var arena = std.heap.ArenaAllocator.init(allocator); 128 defer arena.deinit(); 129 130 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 131 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return false; 132 if (fork_record.event.stage != .draft) return false; 133 if (fork_record.removed or 134 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 135 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 136 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 137 if (user.removed or 138 !std.mem.eql(u8, user.event.name, input.author.name) or 139 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 140 141 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 142 defer fork_repo.deinit(io, allocator); 143 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 144 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 145 if (record.removed) return error.PatchDataUnavailable; 146 147 var patch = record.event; 148 patch.title = input.title; 149 patch.tags = input.tags; 150 patch.description = input.description; 151 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 152 .id = input.id, 153 .timestamp = input.timestamp, 154 .author = input.author, 155 .event = .{ .patch = patch }, 156 }}); 157 return true; 158 } 159 160 // merge the selected patch revision into its target branch 161 pub fn merge( 162 comptime repo_opts: rp.RepoOpts(.xit), 163 io: std.Io, 164 allocator: std.mem.Allocator, 165 repo_root_path: []const u8, 166 target_repo: *rp.Repo(.xit, repo_opts), 167 input: MergeInput, 168 ) !void { 169 const patch_id = try evt.parseEventId(&input.id); 170 var arena = std.heap.ArenaAllocator.init(allocator); 171 defer arena.deinit(); 172 173 // validate the target patch 174 const target_moment = try evt.currentMoment(repo_opts, target_repo); 175 const patch_record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_moment, &arena, &patch_id)) orelse return error.InvalidPatch; 176 if (patch_record.removed) return error.InvalidPatch; 177 switch (patch_record.event.status) { 178 .open => {}, 179 .closed => return error.PatchClosed, 180 .merged => return error.PatchAlreadyMerged, 181 } 182 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 183 const revision_id = try evt.parseEventId(&selected.id); 184 const target_ref = rf.Ref.initFromPath(selected.target_ref, null) orelse return error.InvalidPatch; 185 switch (target_ref.kind) { 186 .head => {}, 187 else => return error.InvalidPatch, 188 } 189 190 // open and lock the fork 191 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 192 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 193 defer fork_repo.deinit(io, allocator); 194 try fork_repo.core.db_file.lock(io, .shared); 195 defer fork_repo.core.db_file.unlock(io); 196 197 // require the fork's newest revision 198 { 199 const fork_moment = try evt.currentMoment(repo_opts, &fork_repo); 200 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena)) orelse return error.PatchDataUnavailable; 201 if (!std.mem.eql(u8, &newest.id, &revision_id) or 202 !selected.matches(newest.record)) return error.PatchOutOfDate; 203 } 204 205 // select the commit and merge identity 206 const merge_oid = blk: { 207 const selected_oid = switch (input.revision) { 208 .squash => selected.squash_oid, 209 .source => selected.source_oid, 210 }; 211 var oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 212 if (selected_oid.len != oid.len) return error.InvalidPatch; 213 @memcpy(&oid, selected_oid); 214 break :blk oid; 215 }; 216 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 217 218 // merge the code and events in one target transaction 219 { 220 const DB = rp.Repo(.xit, repo_opts).DB; 221 const State = rp.Repo(.xit, repo_opts).State; 222 const Ctx = struct { 223 core: *rp.Repo(.xit, repo_opts).Core, 224 fork_repo: *rp.Repo(.xit, repo_opts), 225 io: std.Io, 226 allocator: std.mem.Allocator, 227 patch_id: [evt.event_id_size]u8, 228 expected_patch: evt.Patch, 229 target_ref: rf.Ref, 230 merge_oid: [hash.hexLen(repo_opts.hash)]u8, 231 identity: []const u8, 232 author: evt.CommitAuthor, 233 timestamp: u64, 234 235 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 236 var moment = try DB.HashMap(.read_write).init(cursor.*); 237 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 238 239 // copy the selected commit and its dependencies 240 { 241 var fork_repo_moment = try ctx.fork_repo.core.latestMoment(); 242 const fork_state = State(.read_only){ .core = &ctx.fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 243 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 244 defer objects.deinit(); 245 try objects.include(&ctx.merge_oid); 246 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 247 } 248 249 // import the revision and mark the patch merged 250 if (!try importMergedFromFork(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.fork_repo, &ctx.patch_id, ctx.expected_patch, ctx.author, ctx.timestamp)) return error.PatchOutOfDate; 251 252 // merge the selected commit 253 { 254 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 255 .kind = .full, 256 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.merge_oid }}, .algo = .diff3 } }, 257 .commit_metadata = .{ 258 .author = ctx.identity, 259 .committer = ctx.identity, 260 .message = ctx.expected_patch.title, 261 .timestamp = ctx.timestamp, 262 }, 263 }, ctx.target_ref, null); 264 defer merge_result.deinit(); 265 switch (merge_result.result) { 266 .conflict => return error.MergeConflict, 267 .success, .nothing, .fast_forward => {}, 268 } 269 } 270 271 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "merge patch" }); 272 } 273 }; 274 275 try target_repo.core.db_file.lock(io, .exclusive); 276 defer target_repo.core.db_file.unlock(io); 277 278 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 279 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 280 .core = &target_repo.core, 281 .fork_repo = &fork_repo, 282 .io = io, 283 .allocator = allocator, 284 .patch_id = patch_id, 285 .expected_patch = patch_record.event, 286 .target_ref = target_ref, 287 .merge_oid = merge_oid, 288 .identity = identity, 289 .author = input.author, 290 .timestamp = input.timestamp, 291 }); 292 } 293 } 294 295 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 296 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 297 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 298 const name = std.mem.trimEnd(u8, line[0..open], " "); 299 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 300 return .{ .name = name, .email = line[open + 1 .. close] }; 301 } 302 303 // import a revision after detecting a client-side merge 304 fn importMerged( 305 comptime repo_opts: rp.RepoOpts(.xit), 306 state: rp.Repo(.xit, repo_opts).State(.read_write), 307 db: *evt.EventDB(repo_opts.hash), 308 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 309 io: std.Io, 310 allocator: std.mem.Allocator, 311 repo_root_path: []const u8, 312 patch_id: *const [evt.event_id_size]u8, 313 ) !void { 314 var arena = std.heap.ArenaAllocator.init(allocator); 315 defer arena.deinit(); 316 317 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 318 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 319 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 320 defer fork_repo.deinit(io, allocator); 321 322 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, null, null, 0); 323 } 324 325 // import the revision and mark its patch merged in the current transaction 326 fn importMergedFromFork( 327 comptime repo_opts: rp.RepoOpts(.xit), 328 state: rp.Repo(.xit, repo_opts).State(.read_write), 329 db: *evt.EventDB(repo_opts.hash), 330 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 331 io: std.Io, 332 allocator: std.mem.Allocator, 333 fork_repo: *rp.Repo(.xit, repo_opts), 334 patch_id: *const [evt.event_id_size]u8, 335 expected_patch: ?evt.Patch, 336 merged_author: ?evt.CommitAuthor, 337 merged_timestamp: u64, 338 ) !bool { 339 const DB = evt.EventDB(repo_opts.hash); 340 var arena = std.heap.ArenaAllocator.init(allocator); 341 defer arena.deinit(); 342 343 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 344 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 345 const selected = patch_record.event.revision orelse return false; 346 if (patch_record.removed or patch_record.event.status == .merged) return false; 347 if (expected_patch) |expected| { 348 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 349 } 350 const revision_id = try evt.parseEventId(&selected.id); 351 352 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 353 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 354 if (!selected.matches(revision)) return false; 355 356 var fork_repo_moment = try fork_repo.core.latestMoment(); 357 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 358 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 359 if (revision.event_oid.len != event_oid.len) return false; 360 @memcpy(&event_oid, revision.event_oid); 361 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 362 defer event_object.deinit(); 363 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 364 365 var events: [2]evt.EventWithId = undefined; 366 var event_count: usize = 0; 367 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 368 var tree_entries: [2]evt.EventTreeEntry = undefined; 369 const needs_import = if (current_revision) |existing| existing.removed else true; 370 if (needs_import) { 371 tree_entries = .{ 372 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 373 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 374 }; 375 events[event_count] = .{ 376 .id = selected.id, 377 .timestamp = event_object.content.commit.metadata.timestamp, 378 .author = original_author, 379 .tree_entries = &tree_entries, 380 .event = .{ .patchrev = revision.event }, 381 }; 382 event_count += 1; 383 } else { 384 const existing = current_revision orelse unreachable; 385 if (!selected.matches(existing)) return error.InvalidPatch; 386 } 387 388 var patch = patch_record.event; 389 patch.status = .merged; 390 events[event_count] = .{ 391 .id = std.fmt.bytesToHex(patch_id.*, .lower), 392 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 393 .author = merged_author orelse original_author, 394 .event = .{ .patch = patch }, 395 }; 396 event_count += 1; 397 398 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 399 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 400 return true; 401 } 402 403 // find patches merged by commits received in a push 404 pub fn detectMerged( 405 comptime repo_opts: rp.RepoOpts(.xit), 406 state: rp.Repo(.xit, repo_opts).State(.read_write), 407 db: *evt.EventDB(repo_opts.hash), 408 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 409 io: std.Io, 410 allocator: std.mem.Allocator, 411 repo_root_path: []const u8, 412 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 413 error_writer: *std.Io.Writer, 414 ) !void { 415 const DB = evt.EventDB(repo_opts.hash); 416 var arena = std.heap.ArenaAllocator.init(allocator); 417 defer arena.deinit(); 418 419 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 420 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 421 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 422 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 423 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 424 for (updates) |update| { 425 if (!std.mem.startsWith(u8, update.ref_name, "refs/heads/") or 426 std.mem.eql(u8, update.ref_name, events_ref_path) or 427 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 428 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 429 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 430 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 431 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 432 @memcpy(&old_oid, update.old_oid); 433 @memcpy(&new_oid, update.new_oid); 434 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 435 436 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 437 defer commits.deinit(); 438 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 439 try commits.include(&new_oid); 440 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 441 while (try commits.next(arena.allocator())) |commit| { 442 defer commit.deinit(); 443 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name, &commit.oid }); 444 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 445 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 446 var ids_iter = try ids.iterator(); 447 while (try ids_iter.next()) |cursor| { 448 const pair = try cursor.readKeyValuePair(); 449 var patch_id: [evt.event_id_size]u8 = undefined; 450 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 451 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 452 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id) catch |err| { 453 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 454 continue; 455 }; 456 } 457 } 458 } 459 } 460