╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 297 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 298 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 299 const name = std.mem.trimEnd(u8, line[0..open], " "); 300 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 301 return .{ .name = name, .email = line[open + 1 .. close] }; 302 } 303 304 // import a revision after detecting a client-side merge 305 fn importMerged( 306 comptime repo_opts: rp.RepoOpts(.xit), 307 state: rp.Repo(.xit, repo_opts).State(.read_write), 308 db: *evt.EventDB(repo_opts.hash), 309 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 310 io: std.Io, 311 allocator: std.mem.Allocator, 312 repo_root_path: []const u8, 313 patch_id: *const [evt.event_id_size]u8, 314 ) !void { 315 var arena = std.heap.ArenaAllocator.init(allocator); 316 defer arena.deinit(); 317 318 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 319 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 320 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 321 defer fork_repo.deinit(io, allocator); 322 323 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, null, null, 0); 324 } 325 326 // import the revision and mark its patch merged in the current transaction 327 fn importMergedFromFork( 328 comptime repo_opts: rp.RepoOpts(.xit), 329 state: rp.Repo(.xit, repo_opts).State(.read_write), 330 db: *evt.EventDB(repo_opts.hash), 331 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 332 io: std.Io, 333 allocator: std.mem.Allocator, 334 fork_repo: *rp.Repo(.xit, repo_opts), 335 patch_id: *const [evt.event_id_size]u8, 336 expected_patch: ?evt.Patch, 337 merged_author: ?evt.CommitAuthor, 338 merged_timestamp: u64, 339 ) !bool { 340 const DB = evt.EventDB(repo_opts.hash); 341 var arena = std.heap.ArenaAllocator.init(allocator); 342 defer arena.deinit(); 343 344 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 345 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 346 const selected = patch_record.event.revision orelse return false; 347 if (patch_record.removed or patch_record.event.status == .merged) return false; 348 if (expected_patch) |expected| { 349 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 350 } 351 const revision_id = try evt.parseEventId(&selected.id); 352 353 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 354 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 355 if (!selected.matches(revision)) return false; 356 357 var fork_repo_moment = try fork_repo.core.latestMoment(); 358 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 359 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 360 if (revision.event_oid.len != event_oid.len) return false; 361 @memcpy(&event_oid, revision.event_oid); 362 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 363 defer event_object.deinit(); 364 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 365 366 var events: [2]evt.EventWithId = undefined; 367 var event_count: usize = 0; 368 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 369 var tree_entries: [2]evt.EventTreeEntry = undefined; 370 const needs_import = if (current_revision) |existing| existing.removed else true; 371 if (needs_import) { 372 tree_entries = .{ 373 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 374 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 375 }; 376 events[event_count] = .{ 377 .id = selected.id, 378 .timestamp = event_object.content.commit.metadata.timestamp, 379 .author = original_author, 380 .tree_entries = &tree_entries, 381 .event = .{ .patchrev = revision.event }, 382 }; 383 event_count += 1; 384 } else { 385 const existing = current_revision orelse unreachable; 386 if (!selected.matches(existing)) return error.InvalidPatch; 387 } 388 389 var patch = patch_record.event; 390 patch.status = .merged; 391 events[event_count] = .{ 392 .id = std.fmt.bytesToHex(patch_id.*, .lower), 393 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 394 .author = merged_author orelse original_author, 395 .event = .{ .patch = patch }, 396 }; 397 event_count += 1; 398 399 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 400 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 401 return true; 402 } 403 404 // find patches merged by commits received in a push 405 pub fn detectMerged( 406 comptime repo_opts: rp.RepoOpts(.xit), 407 state: rp.Repo(.xit, repo_opts).State(.read_write), 408 db: *evt.EventDB(repo_opts.hash), 409 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 410 io: std.Io, 411 allocator: std.mem.Allocator, 412 repo_root_path: []const u8, 413 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 414 error_writer: *std.Io.Writer, 415 ) !void { 416 const DB = evt.EventDB(repo_opts.hash); 417 var arena = std.heap.ArenaAllocator.init(allocator); 418 defer arena.deinit(); 419 420 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 421 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 422 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 423 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 424 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 425 for (updates) |update| { 426 if (!std.mem.startsWith(u8, update.ref_name, "refs/heads/") or 427 std.mem.eql(u8, update.ref_name, events_ref_path) or 428 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 429 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 430 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 431 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 432 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 433 @memcpy(&old_oid, update.old_oid); 434 @memcpy(&new_oid, update.new_oid); 435 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 436 437 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 438 defer commits.deinit(); 439 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 440 try commits.include(&new_oid); 441 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 442 while (try commits.next(arena.allocator())) |commit| { 443 defer commit.deinit(); 444 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name, &commit.oid }); 445 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 446 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 447 var ids_iter = try ids.iterator(); 448 while (try ids_iter.next()) |cursor| { 449 const pair = try cursor.readKeyValuePair(); 450 var patch_id: [evt.event_id_size]u8 = undefined; 451 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 452 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 453 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id) catch |err| { 454 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 455 continue; 456 }; 457 } 458 } 459 } 460 } 461