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