╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1449 // read the revision index used to identify merged patches. 1450 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1451 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1452 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1453 1454 // ignore event refs and inspect only changed branch tips. 1455 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1456 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1457 const heads_prefix = "refs/heads/"; 1458 for (updates) |update| { 1459 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1460 std.mem.eql(u8, update.ref_name, events_ref_path) or 1461 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1462 1463 // validate the ref tips and skip branch deletions. 1464 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1465 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1466 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1467 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1468 @memcpy(&old_oid, update.old_oid); 1469 @memcpy(&new_oid, update.new_oid); 1470 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1471 1472 // traverse commits introduced by this branch update. 1473 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1474 defer commits.deinit(); 1475 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1476 var targets: std.ArrayList(struct { 1477 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1478 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1479 }) = .empty; 1480 1481 // collect merge boundaries along the target's first-parent history. 1482 { 1483 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1484 defer first_parents.deinit(); 1485 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1486 try first_parents.include(&new_oid); 1487 while (try first_parents.next(arena.allocator())) |commit| { 1488 defer commit.deinit(); 1489 try targets.append(arena.allocator(), .{ 1490 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1491 .after_oid = commit.oid, 1492 }); 1493 } 1494 } 1495 1496 // visit target commits oldest first, attributing each revision to the 1497 // commit that introduced it; the shared iterator visits each ancestor once 1498 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1499 while (targets.pop()) |target| { 1500 try commits.include(&target.after_oid); 1501 while (try commits.next(arena.allocator())) |commit| { 1502 defer commit.deinit(); 1503 1504 // find patches whose selected revision matches this commit. 1505 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1506 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1507 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1508 var ids_iter = try ids.iterator(); 1509 while (try ids_iter.next()) |cursor| { 1510 const pair = try cursor.readKeyValuePair(); 1511 var patch_id: [evt.event_id_size]u8 = undefined; 1512 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1513 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1514 1515 // a fast-forward has no recorded merge boundary; use the old ref tip 1516 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1517 importMerged(repo_opts, state, db, moment, io, allocator, users_dir, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1518 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1519 continue; 1520 }; 1521 } 1522 } 1523 } 1524 } 1525 } 1526