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