╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
640 .id = selected.id, 641 .timestamp = event_object.content.commit.metadata.timestamp, 642 .author = original_author, 643 .tree_entries = &tree_entries, 644 .event = .{ .patchrev = revision.event }, 645 }; 646 event_count += 1; 647 } else { 648 const existing = current_revision orelse unreachable; 649 if (!selected.matches(existing)) return error.InvalidPatch; 650 } 651 652 var patch = patch_record.event; 653 patch.status = .{ .merged = merged_oid }; 654 events[event_count] = .{ 655 .id = std.fmt.bytesToHex(patch_id.*, .lower), 656 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 657 .author = merged_author orelse original_author, 658 .event = .{ .patch = patch }, 659 }; 660 event_count += 1; 661 662 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 663 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 664 return true; 665 } 666 667 // find patches merged by commits received in a push 668 pub fn detectMerged( 669 comptime repo_opts: rp.RepoOpts(.xit), 670 state: rp.Repo(.xit, repo_opts).State(.read_write), 671 db: *evt.EventDB(repo_opts.hash), 672 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 673 io: std.Io, 674 allocator: std.mem.Allocator, 675 repo_root_path: []const u8, 676 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 677 error_writer: *std.Io.Writer, 678 ) !void { 679 const DB = evt.EventDB(repo_opts.hash); 680 var arena = std.heap.ArenaAllocator.init(allocator); 681 defer arena.deinit(); 682 683 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 684 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 685 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 686 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 687 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 688 const heads_prefix = "refs/heads/"; 689 for (updates) |update| { 690 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 691 std.mem.eql(u8, update.ref_name, events_ref_path) or 692 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 693 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 694 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 695 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 696 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 697 @memcpy(&old_oid, update.old_oid); 698 @memcpy(&new_oid, update.new_oid); 699 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 700 701 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 702 defer commits.deinit(); 703 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 704 try commits.include(&new_oid); 705 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 706 while (try commits.next(arena.allocator())) |commit| { 707 defer commit.deinit(); 708 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 709 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 710 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 711 var ids_iter = try ids.iterator(); 712 while (try ids_iter.next()) |cursor| { 713 const pair = try cursor.readKeyValuePair(); 714 var patch_id: [evt.event_id_size]u8 = undefined; 715 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 716 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 717 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid) catch |err| { 718 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 719 continue; 720 }; 721 } 722 } 723 } 724 } 725