╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1366 1367 // import the revision and mark its patch merged in the current transaction 1368 fn importMergedRevision( 1369 comptime repo_opts: rp.RepoOpts(.xit), 1370 state: rp.Repo(.xit, repo_opts).State(.read_write), 1371 db: *evt.EventDB(repo_opts.hash), 1372 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1373 io: std.Io, 1374 allocator: std.mem.Allocator, 1375 fork_repo: ?*rp.Repo(.xit, repo_opts), 1376 patch_id: *const [evt.event_id_size]u8, 1377 merged: struct { 1378 revision: evt.Patch.MergeRevision, 1379 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1380 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1381 }, 1382 expected_patch: ?evt.Patch, 1383 merged_author: ?evt.CommitAuthor, 1384 merged_timestamp: u64, 1385 ) !bool { 1386 const DB = evt.EventDB(repo_opts.hash); 1387 var arena = std.heap.ArenaAllocator.init(allocator); 1388 defer arena.deinit(); 1389 1390 // read the patch and reject outdated or conflicting merge requests. 1391 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1392 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1393 const selected = patch_record.event.revision orelse return false; 1394 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1395 if (expected_patch) |expected| { 1396 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1397 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1398 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1399 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1400 } 1401 } 1402 const revision_id = try evt.parseEventId(&selected.id); 1403 1404 // resolve the selected revision from the target or its fork. 1405 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1406 const revision = (if (fork_repo) |repo| 1407 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1408 else 1409 current_revision) orelse return false; 1410 if (!selected.matches(revision)) return false; 1411 1412 // read the original revision commit and author. 1413 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1414 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = if (fork_repo) |repo| &repo.core else state.core, .extra = .{ .moment = &fork_repo_moment } }; 1415 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1416 if (revision.event_oid.len != event_oid.len) return false; 1417 @memcpy(&event_oid, revision.event_oid); 1418 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1419 defer event_object.deinit(); 1420 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1421 1422 // retain the submitted revision if the target has not imported it yet. 1423 var events_buffer: [3]evt.EventWithId = undefined; 1424 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1425 var tree_entries: [2]evt.EventTreeEntry = undefined; 1426 const needs_import = if (current_revision) |existing| existing.removed else true; 1427 if (needs_import) { 1428 tree_entries = .{ 1429 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1430 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1431 }; 1432 events.appendAssumeCapacity(.{ 1433 .id = selected.id, 1434 .timestamp = event_object.content.commit.metadata.timestamp, 1435 .author = original_author, 1436 .tree_entries = &tree_entries, 1437 .event = .{ .patchrev = revision.event }, 1438 }); 1439 } else { 1440 const existing = current_revision orelse unreachable; 1441 if (!selected.matches(existing)) return error.InvalidPatch; 1442 } 1443 1444 // snapshot the merge result and mark the patch as merged. 1445 { 1446 const author = merged_author orelse original_author; 1447 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1448 var patch = patch_record.event; 1449 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1450 events.appendAssumeCapacity(result.event); 1451 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1452 events.appendAssumeCapacity(.{ 1453 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1454 .timestamp = timestamp, 1455 .author = author, 1456 .event = .{ .patch = patch }, 1457 }); 1458 } 1459 1460 // commit and consume the revision and patch events together. 1461 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1462 _ = try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref); 1463 return true; 1464 } 1465 1466 // find patches merged by commits received in a push 1467 pub fn detectMerged( 1468 comptime repo_opts: rp.RepoOpts(.xit), 1469 state: rp.Repo(.xit, repo_opts).State(.read_write), 1470 db: *evt.EventDB(repo_opts.hash), 1471 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1472 io: std.Io, 1473 allocator: std.mem.Allocator, 1474 users_dir: []const u8, 1475 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1476 error_writer: *std.Io.Writer, 1477 ) !void { 1478 const DB = evt.EventDB(repo_opts.hash); 1479 var arena = std.heap.ArenaAllocator.init(allocator); 1480 defer arena.deinit(); 1481 1482 // read the revision index used to identify merged patches. 1483 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1484 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1485 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1486 1487 // ignore event refs and inspect only changed branch tips. 1488 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1489 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1490 const heads_prefix = "refs/heads/"; 1491 for (updates) |update| { 1492 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1493 std.mem.eql(u8, update.ref_name, events_ref_path) or 1494 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1495 1496 // validate the ref tips and skip branch deletions. 1497 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1498 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1499 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1500 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1501 @memcpy(&old_oid, update.old_oid); 1502 @memcpy(&new_oid, update.new_oid); 1503 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1504 1505 // traverse commits introduced by this branch update. 1506 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1507 defer commits.deinit(); 1508 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1509 var targets: std.ArrayList(struct { 1510 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1511 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1512 }) = .empty; 1513 1514 // collect merge boundaries along the target's first-parent history. 1515 { 1516 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1517 defer first_parents.deinit(); 1518 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1519 try first_parents.include(&new_oid); 1520 while (try first_parents.next(arena.allocator())) |commit| { 1521 defer commit.deinit(); 1522 try targets.append(arena.allocator(), .{ 1523 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1524 .after_oid = commit.oid, 1525 }); 1526 } 1527 } 1528 1529 // visit target commits oldest first, attributing each revision to the 1530 // commit that introduced it; the shared iterator visits each ancestor once 1531 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1532 while (targets.pop()) |target| { 1533 try commits.include(&target.after_oid); 1534 while (try commits.next(arena.allocator())) |commit| { 1535 defer commit.deinit(); 1536 1537 // find patches whose selected revision matches this commit. 1538 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1539 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1540 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1541 var ids_iter = try ids.iterator(); 1542 while (try ids_iter.next()) |cursor| { 1543 const pair = try cursor.readKeyValuePair(); 1544 var patch_id: [evt.event_id_size]u8 = undefined; 1545 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1546 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1547 1548 // a fast-forward has no recorded merge boundary; use the old ref tip 1549 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1550 importMerged(repo_opts, state, db, moment, io, allocator, users_dir, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1551 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1552 continue; 1553 }; 1554 } 1555 } 1556 } 1557 } 1558 } 1559