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