╭ 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 target_branch: []const u8, 28 author: evt.CommitAuthor, 29 timestamp: u64, 30 }; 31 32 pub const MergeRevision = enum { squash, source }; 33 34 pub const MergeInput = struct { 35 id: [evt.event_id_size * 2]u8, 36 revision: MergeRevision, 37 author: evt.CommitAuthor, 38 timestamp: u64, 39 }; 40 41 pub fn publish( 42 comptime repo_opts: rp.RepoOpts(.xit), 43 io: std.Io, 44 allocator: std.mem.Allocator, 45 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 46 target_repo: *rp.Repo(.xit, repo_opts), 47 fork_path: []const u8, 48 input: PublishInput, 49 ) !void { 50 const patch_id = try evt.parseEventId(&input.id); 51 var arena = std.heap.ArenaAllocator.init(allocator); 52 defer arena.deinit(); 53 54 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 55 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return error.InvalidPatchDraft; 56 if (fork_record.removed or 57 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 58 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 59 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 60 if (user.removed or 61 !std.mem.eql(u8, user.event.name, input.author.name) or 62 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 63 64 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path }) catch return error.PatchDataUnavailable; 65 defer fork_repo.deinit(io, allocator); 66 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 67 const local_patch = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 68 69 const target_moment = evt.currentMoment(repo_opts, target_repo) catch |err| switch (err) { 70 error.NotFound => null, 71 else => |other| return other, 72 }; 73 const existing = if (target_moment) |moment| 74 try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, &arena, &patch_id) 75 else 76 null; 77 if (existing) |record| { 78 if (record.removed or !std.mem.eql(u8, record.author_email orelse "", input.author.email)) return error.InvalidPatch; 79 } 80 if (local_patch.removed) { 81 if (existing == null) return error.PatchDataUnavailable; 82 } else { 83 var patch = (existing orelse local_patch).event; 84 if (local_patch.event.revision) |selected| { 85 const revision_id = try evt.parseEventId(&selected.id); 86 const revision = (try evt.PatchRev.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return error.PatchNotPushed; 87 if (!selected.matches(revision)) return error.PatchNotPushed; 88 patch.revision = selected; 89 } 90 if (existing == null) patch.status = .open; 91 92 try evt.consume(.repo, .xit, repo_opts, io, allocator, target_repo, evt.events_ref, &.{.{ 93 .id = input.id, 94 .timestamp = input.timestamp, 95 .author = input.author, 96 .event = .{ .patch = patch }, 97 }}); 98 99 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 100 .id = input.id, 101 .timestamp = input.timestamp, 102 .author = input.author, 103 .event = .{ .patch = null }, 104 }}); 105 } 106 107 if (fork_record.event.stage == .draft) { 108 var published = fork_record.event; 109 published.stage = .publish; 110 try evt.consume(.admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 111 .id = input.id, 112 .timestamp = input.timestamp, 113 .author = input.author, 114 .event = .{ .fork = published }, 115 }}); 116 } 117 } 118 119 pub fn editDraft( 120 comptime repo_opts: rp.RepoOpts(.xit), 121 io: std.Io, 122 allocator: std.mem.Allocator, 123 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 124 fork_path: []const u8, 125 input: EditDraftInput, 126 ) !bool { 127 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidFields; 128 const patch_id = try evt.parseEventId(&input.id); 129 var arena = std.heap.ArenaAllocator.init(allocator); 130 defer arena.deinit(); 131 132 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 133 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return false; 134 if (fork_record.event.stage != .draft) return false; 135 if (fork_record.removed or 136 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 137 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 138 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 139 if (user.removed or 140 !std.mem.eql(u8, user.event.name, input.author.name) or 141 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 142 143 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 144 defer fork_repo.deinit(io, allocator); 145 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 146 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 147 if (record.removed) return error.PatchDataUnavailable; 148 149 var patch = record.event; 150 patch.title = input.title; 151 patch.tags = input.tags; 152 patch.description = input.description; 153 if (!std.mem.eql(u8, patch.target_branch, input.target_branch)) patch.revision = null; 154 patch.target_branch = input.target_branch; 155 try evt.consume(.fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 156 .id = input.id, 157 .timestamp = input.timestamp, 158 .author = input.author, 159 .event = .{ .patch = patch }, 160 }}); 161 return true; 162 } 163 164 // merge the selected patch revision into its target branch 165 pub fn merge( 166 comptime repo_opts: rp.RepoOpts(.xit), 167 io: std.Io, 168 allocator: std.mem.Allocator, 169 repo_root_path: []const u8, 170 target_repo: *rp.Repo(.xit, repo_opts), 171 input: MergeInput, 172 ) !void { 173 const patch_id = try evt.parseEventId(&input.id); 174 var arena = std.heap.ArenaAllocator.init(allocator); 175 defer arena.deinit(); 176 177 // validate the target patch 178 const target_moment = try evt.currentMoment(repo_opts, target_repo); 179 const patch_record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_moment, &arena, &patch_id)) orelse return error.InvalidPatch; 180 if (patch_record.removed) return error.InvalidPatch; 181 switch (patch_record.event.status) { 182 .open => {}, 183 .closed => return error.PatchClosed, 184 .merged => return error.PatchAlreadyMerged, 185 } 186 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 187 const revision_id = try evt.parseEventId(&selected.id); 188 const target_ref = rf.Ref{ .kind = .head, .name = patch_record.event.target_branch }; 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.merge_oid, 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 = .patch } }, 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 // merge a published patch and remove its fork 296 pub fn mergeAndRemoveFork( 297 comptime repo_opts: rp.RepoOpts(.xit), 298 io: std.Io, 299 allocator: std.mem.Allocator, 300 repo_root_path: []const u8, 301 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 302 target_repo: *rp.Repo(.xit, repo_opts), 303 input: MergeInput, 304 ) !void { 305 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 306 try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 307 } 308 309 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 310 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 311 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 312 const name = std.mem.trimEnd(u8, line[0..open], " "); 313 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 314 return .{ .name = name, .email = line[open + 1 .. close] }; 315 } 316 317 // import a revision after detecting a client-side merge 318 fn importMerged( 319 comptime repo_opts: rp.RepoOpts(.xit), 320 state: rp.Repo(.xit, repo_opts).State(.read_write), 321 db: *evt.EventDB(repo_opts.hash), 322 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 323 io: std.Io, 324 allocator: std.mem.Allocator, 325 repo_root_path: []const u8, 326 patch_id: *const [evt.event_id_size]u8, 327 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 328 ) !void { 329 var arena = std.heap.ArenaAllocator.init(allocator); 330 defer arena.deinit(); 331 332 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 333 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 334 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 335 defer fork_repo.deinit(io, allocator); 336 337 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, merged_oid, null, null, 0); 338 } 339 340 // import the revision and mark its patch merged in the current transaction 341 fn importMergedFromFork( 342 comptime repo_opts: rp.RepoOpts(.xit), 343 state: rp.Repo(.xit, repo_opts).State(.read_write), 344 db: *evt.EventDB(repo_opts.hash), 345 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 346 io: std.Io, 347 allocator: std.mem.Allocator, 348 fork_repo: *rp.Repo(.xit, repo_opts), 349 patch_id: *const [evt.event_id_size]u8, 350 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 351 expected_patch: ?evt.Patch, 352 merged_author: ?evt.CommitAuthor, 353 merged_timestamp: u64, 354 ) !bool { 355 const DB = evt.EventDB(repo_opts.hash); 356 var arena = std.heap.ArenaAllocator.init(allocator); 357 defer arena.deinit(); 358 359 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 360 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 361 const selected = patch_record.event.revision orelse return false; 362 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 363 if (expected_patch) |expected| { 364 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 365 } 366 const revision_id = try evt.parseEventId(&selected.id); 367 if (!selected.includesOid(merged_oid)) return false; 368 369 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 370 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 371 if (!selected.matches(revision)) return false; 372 373 var fork_repo_moment = try fork_repo.core.latestMoment(); 374 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 375 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 376 if (revision.event_oid.len != event_oid.len) return false; 377 @memcpy(&event_oid, revision.event_oid); 378 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 379 defer event_object.deinit(); 380 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 381 382 var events: [2]evt.EventWithId = undefined; 383 var event_count: usize = 0; 384 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 385 var tree_entries: [2]evt.EventTreeEntry = undefined; 386 const needs_import = if (current_revision) |existing| existing.removed else true; 387 if (needs_import) { 388 tree_entries = .{ 389 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 390 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 391 }; 392 events[event_count] = .{ 393 .id = selected.id, 394 .timestamp = event_object.content.commit.metadata.timestamp, 395 .author = original_author, 396 .tree_entries = &tree_entries, 397 .event = .{ .patchrev = revision.event }, 398 }; 399 event_count += 1; 400 } else { 401 const existing = current_revision orelse unreachable; 402 if (!selected.matches(existing)) return error.InvalidPatch; 403 } 404 405 var patch = patch_record.event; 406 patch.status = .{ .merged = merged_oid }; 407 events[event_count] = .{ 408 .id = std.fmt.bytesToHex(patch_id.*, .lower), 409 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 410 .author = merged_author orelse original_author, 411 .event = .{ .patch = patch }, 412 }; 413 event_count += 1; 414 415 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 416 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 417 return true; 418 } 419 420 // find patches merged by commits received in a push 421 pub fn detectMerged( 422 comptime repo_opts: rp.RepoOpts(.xit), 423 state: rp.Repo(.xit, repo_opts).State(.read_write), 424 db: *evt.EventDB(repo_opts.hash), 425 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 426 io: std.Io, 427 allocator: std.mem.Allocator, 428 repo_root_path: []const u8, 429 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 430 error_writer: *std.Io.Writer, 431 ) !void { 432 const DB = evt.EventDB(repo_opts.hash); 433 var arena = std.heap.ArenaAllocator.init(allocator); 434 defer arena.deinit(); 435 436 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 437 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 438 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 439 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 440 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 441 const heads_prefix = "refs/heads/"; 442 for (updates) |update| { 443 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 444 std.mem.eql(u8, update.ref_name, events_ref_path) or 445 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 446 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 447 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 448 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 449 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 450 @memcpy(&old_oid, update.old_oid); 451 @memcpy(&new_oid, update.new_oid); 452 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 453 454 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 455 defer commits.deinit(); 456 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 457 try commits.include(&new_oid); 458 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 459 while (try commits.next(arena.allocator())) |commit| { 460 defer commit.deinit(); 461 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 462 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 463 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 464 var ids_iter = try ids.iterator(); 465 while (try ids_iter.next()) |cursor| { 466 const pair = try cursor.readKeyValuePair(); 467 var patch_id: [evt.event_id_size]u8 = undefined; 468 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 469 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 470 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid) catch |err| { 471 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 472 continue; 473 }; 474 } 475 } 476 } 477 } 478