╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
132 .head => {}, 133 else => return error.InvalidPatch, 134 } 135 136 // open and lock the fork 137 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 138 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 139 defer fork_repo.deinit(io, allocator); 140 try fork_repo.core.db_file.lock(io, .shared); 141 defer fork_repo.core.db_file.unlock(io); 142 143 // require the fork's newest revision 144 { 145 const fork_moment = try evt.currentMoment(repo_opts, &fork_repo); 146 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena)) orelse return error.PatchDataUnavailable; 147 if (!std.mem.eql(u8, &newest.id, &revision_id) or 148 !selected.matches(newest.record)) return error.PatchOutOfDate; 149 } 150 151 // select the commit and merge identity 152 const merge_oid = blk: { 153 const selected_oid = switch (input.revision) { 154 .squash => selected.squash_oid, 155 .source => selected.source_oid, 156 }; 157 var oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 158 if (selected_oid.len != oid.len) return error.InvalidPatch; 159 @memcpy(&oid, selected_oid); 160 break :blk oid; 161 }; 162 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 163 164 // merge the code and events in one target transaction 165 { 166 const DB = rp.Repo(.xit, repo_opts).DB; 167 const State = rp.Repo(.xit, repo_opts).State; 168 const Ctx = struct { 169 core: *rp.Repo(.xit, repo_opts).Core, 170 fork_repo: *rp.Repo(.xit, repo_opts), 171 io: std.Io, 172 allocator: std.mem.Allocator, 173 patch_id: [evt.event_id_size]u8, 174 expected_patch: evt.Patch, 175 target_ref: rf.Ref, 176 merge_oid: [hash.hexLen(repo_opts.hash)]u8, 177 identity: []const u8, 178 author: evt.CommitAuthor, 179 timestamp: u64, 180 181 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 182 var moment = try DB.HashMap(.read_write).init(cursor.*); 183 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 184 185 // copy the selected commit and its dependencies 186 { 187 var fork_repo_moment = try ctx.fork_repo.core.latestMoment(); 188 const fork_state = State(.read_only){ .core = &ctx.fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 189 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 190 defer objects.deinit(); 191 try objects.include(&ctx.merge_oid); 192 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 193 } 194 195 // import the revision and mark the patch merged 196 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; 197 198 // merge the selected commit 199 { 200 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 201 .kind = .full, 202 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.merge_oid }}, .algo = .diff3 } }, 203 .commit_metadata = .{ 204 .author = ctx.identity, 205 .committer = ctx.identity, 206 .message = ctx.expected_patch.title, 207 .timestamp = ctx.timestamp, 208 }, 209 }, ctx.target_ref, null); 210 defer merge_result.deinit(); 211 switch (merge_result.result) { 212 .conflict => return error.MergeConflict, 213 .success, .nothing, .fast_forward => {}, 214 } 215 } 216 217 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "merge patch" }); 218 } 219 }; 220 221 try target_repo.core.db_file.lock(io, .exclusive); 222 defer target_repo.core.db_file.unlock(io); 223 224 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 225 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 226 .core = &target_repo.core, 227 .fork_repo = &fork_repo, 228 .io = io, 229 .allocator = allocator, 230 .patch_id = patch_id, 231 .expected_patch = patch_record.event, 232 .target_ref = target_ref, 233 .merge_oid = merge_oid, 234 .identity = identity, 235 .author = input.author, 236 .timestamp = input.timestamp, 237 }); 238 } 239 } 240 241 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 242 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 243 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 244 const name = std.mem.trimEnd(u8, line[0..open], " "); 245 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 246 return .{ .name = name, .email = line[open + 1 .. close] }; 247 } 248 249 // import a revision after detecting a client-side merge 250 fn importMerged( 251 comptime repo_opts: rp.RepoOpts(.xit), 252 state: rp.Repo(.xit, repo_opts).State(.read_write), 253 db: *evt.EventDB(repo_opts.hash), 254 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 255 io: std.Io, 256 allocator: std.mem.Allocator, 257 repo_root_path: []const u8, 258 patch_id: *const [evt.event_id_size]u8, 259 ) !void { 260 var arena = std.heap.ArenaAllocator.init(allocator); 261 defer arena.deinit(); 262 263 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 264 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 265 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 266 defer fork_repo.deinit(io, allocator); 267 268 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, null, null, 0); 269 } 270 271 // import the revision and mark its patch merged in the current transaction 272 fn importMergedFromFork( 273 comptime repo_opts: rp.RepoOpts(.xit), 274 state: rp.Repo(.xit, repo_opts).State(.read_write), 275 db: *evt.EventDB(repo_opts.hash), 276 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 277 io: std.Io, 278 allocator: std.mem.Allocator, 279 fork_repo: *rp.Repo(.xit, repo_opts), 280 patch_id: *const [evt.event_id_size]u8, 281 expected_patch: ?evt.Patch, 282 merged_author: ?evt.CommitAuthor, 283 merged_timestamp: u64, 284 ) !bool { 285 const DB = evt.EventDB(repo_opts.hash); 286 var arena = std.heap.ArenaAllocator.init(allocator); 287 defer arena.deinit(); 288 289 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 290 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 291 const selected = patch_record.event.revision orelse return false; 292 if (patch_record.removed or patch_record.event.status == .merged) return false; 293 if (expected_patch) |expected| { 294 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 295 } 296 const revision_id = try evt.parseEventId(&selected.id); 297 298 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 299 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 300 if (!selected.matches(revision)) return false; 301 302 var fork_repo_moment = try fork_repo.core.latestMoment(); 303 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 304 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 305 if (revision.event_oid.len != event_oid.len) return false; 306 @memcpy(&event_oid, revision.event_oid); 307 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 308 defer event_object.deinit(); 309 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 310 311 var events: [2]evt.EventWithId = undefined; 312 var event_count: usize = 0; 313 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 314 var tree_entries: [2]evt.EventTreeEntry = undefined; 315 const needs_import = if (current_revision) |existing| existing.removed else true; 316 if (needs_import) { 317 tree_entries = .{ 318 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 319 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 320 }; 321 events[event_count] = .{ 322 .id = selected.id, 323 .timestamp = event_object.content.commit.metadata.timestamp, 324 .author = original_author, 325 .tree_entries = &tree_entries, 326 .event = .{ .patchrev = revision.event }, 327 }; 328 event_count += 1; 329 } else { 330 const existing = current_revision orelse unreachable; 331 if (!selected.matches(existing)) return error.InvalidPatch; 332 } 333 334 var patch = patch_record.event; 335 patch.status = .merged; 336 events[event_count] = .{ 337 .id = std.fmt.bytesToHex(patch_id.*, .lower), 338 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 339 .author = merged_author orelse original_author, 340 .event = .{ .patch = patch }, 341 }; 342 event_count += 1; 343 344 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 345 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 346 return true; 347 } 348 349 // find patches merged by commits received in a push 350 pub fn detectMerged( 351 comptime repo_opts: rp.RepoOpts(.xit), 352 state: rp.Repo(.xit, repo_opts).State(.read_write), 353 db: *evt.EventDB(repo_opts.hash), 354 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 355 io: std.Io, 356 allocator: std.mem.Allocator, 357 repo_root_path: []const u8, 358 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 359 error_writer: *std.Io.Writer, 360 ) !void { 361 const DB = evt.EventDB(repo_opts.hash); 362 var arena = std.heap.ArenaAllocator.init(allocator); 363 defer arena.deinit(); 364 365 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 366 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 367 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 368 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 369 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 370 for (updates) |update| { 371 if (!std.mem.startsWith(u8, update.ref_name, "refs/heads/") or 372 std.mem.eql(u8, update.ref_name, events_ref_path) or 373 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 374 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 375 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 376 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 377 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 378 @memcpy(&old_oid, update.old_oid); 379 @memcpy(&new_oid, update.new_oid); 380 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 381 382 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 383 defer commits.deinit(); 384 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 385 try commits.include(&new_oid); 386 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 387 while (try commits.next(arena.allocator())) |commit| { 388 defer commit.deinit(); 389 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name, &commit.oid }); 390 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 391 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 392 var ids_iter = try ids.iterator(); 393 while (try ids_iter.next()) |cursor| { 394 const pair = try cursor.readKeyValuePair(); 395 var patch_id: [evt.event_id_size]u8 = undefined; 396 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 397 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 398 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id) catch |err| { 399 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 400 continue; 401 }; 402 } 403 } 404 } 405 } 406