╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
742 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 743 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 744 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 745 const name = std.mem.trimEnd(u8, line[0..open], " "); 746 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 747 return .{ .name = name, .email = line[open + 1 .. close] }; 748 } 749 750 // import a revision after detecting a client-side merge 751 fn importMerged( 752 comptime repo_opts: rp.RepoOpts(.xit), 753 state: rp.Repo(.xit, repo_opts).State(.read_write), 754 db: *evt.EventDB(repo_opts.hash), 755 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 756 io: std.Io, 757 allocator: std.mem.Allocator, 758 repo_root_path: []const u8, 759 patch_id: *const [evt.event_id_size]u8, 760 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 761 ) !void { 762 var arena = std.heap.ArenaAllocator.init(allocator); 763 defer arena.deinit(); 764 765 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 766 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 767 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 768 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 769 var fork_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (record.event.source_branch != null) null else try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 770 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 771 772 _ = try importMergedRevision(repo_opts, state, db, moment, io, allocator, if (fork_repo_maybe) |*repo| repo else null, patch_id, merged_oid, null, null, 0); 773 } 774 775 // import the revision and mark its patch merged in the current transaction 776 fn importMergedRevision( 777 comptime repo_opts: rp.RepoOpts(.xit), 778 state: rp.Repo(.xit, repo_opts).State(.read_write), 779 db: *evt.EventDB(repo_opts.hash), 780 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 781 io: std.Io, 782 allocator: std.mem.Allocator, 783 fork_repo: ?*rp.Repo(.xit, repo_opts), 784 patch_id: *const [evt.event_id_size]u8, 785 merged_oid: *const [hash.hexLen(repo_opts.hash)]u8, 786 expected_patch: ?evt.Patch, 787 merged_author: ?evt.CommitAuthor, 788 merged_timestamp: u64, 789 ) !bool { 790 const DB = evt.EventDB(repo_opts.hash); 791 var arena = std.heap.ArenaAllocator.init(allocator); 792 defer arena.deinit(); 793 794 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 795 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 796 const selected = patch_record.event.revision orelse return false; 797 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 798 if (expected_patch) |expected| { 799 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 800 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 801 const conflicts = try DB.SortedMap(.read_only).init(cursor); 802 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 803 } 804 } 805 const revision_id = try evt.parseEventId(&selected.id); 806 if (!selected.includesOid(merged_oid)) return false; 807 808 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 809 const revision = (if (fork_repo) |repo| 810 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 811 else 812 current_revision) orelse return false; 813 if (!selected.matches(revision)) return false; 814 815 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 816 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 } }; 817 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 818 if (revision.event_oid.len != event_oid.len) return false; 819 @memcpy(&event_oid, revision.event_oid); 820 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 821 defer event_object.deinit(); 822 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 823 824 var events: [2]evt.EventWithId = undefined; 825 var event_count: usize = 0; 826 var tree_entries: [2]evt.EventTreeEntry = undefined; 827 const needs_import = if (current_revision) |existing| existing.removed else true; 828 if (needs_import) { 829 tree_entries = .{ 830 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 831 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 832 }; 833 events[event_count] = .{ 834 .id = selected.id, 835 .timestamp = event_object.content.commit.metadata.timestamp, 836 .author = original_author, 837 .tree_entries = &tree_entries, 838 .event = .{ .patchrev = revision.event }, 839 }; 840 event_count += 1; 841 } else { 842 const existing = current_revision orelse unreachable; 843 if (!selected.matches(existing)) return error.InvalidPatch; 844 } 845 846 var patch = patch_record.event; 847 patch.status = .{ .merged = merged_oid }; 848 events[event_count] = .{ 849 .id = std.fmt.bytesToHex(patch_id.*, .lower), 850 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 851 .author = merged_author orelse original_author, 852 .event = .{ .patch = patch }, 853 }; 854 event_count += 1; 855 856 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 857 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 858 return true; 859 } 860 861 // find patches merged by commits received in a push 862 pub fn detectMerged( 863 comptime repo_opts: rp.RepoOpts(.xit), 864 state: rp.Repo(.xit, repo_opts).State(.read_write), 865 db: *evt.EventDB(repo_opts.hash), 866 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 867 io: std.Io, 868 allocator: std.mem.Allocator, 869 repo_root_path: []const u8, 870 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 871 error_writer: *std.Io.Writer, 872 ) !void { 873 const DB = evt.EventDB(repo_opts.hash); 874 var arena = std.heap.ArenaAllocator.init(allocator); 875 defer arena.deinit(); 876 877 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 878 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 879 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 880 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 881 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 882 const heads_prefix = "refs/heads/"; 883 for (updates) |update| { 884 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 885 std.mem.eql(u8, update.ref_name, events_ref_path) or 886 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 887 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 888 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 889 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 890 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 891 @memcpy(&old_oid, update.old_oid); 892 @memcpy(&new_oid, update.new_oid); 893 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 894 895 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 896 defer commits.deinit(); 897 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 898 try commits.include(&new_oid); 899 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 900 while (try commits.next(arena.allocator())) |commit| { 901 defer commit.deinit(); 902 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 903 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 904 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 905 var ids_iter = try ids.iterator(); 906 while (try ids_iter.next()) |cursor| { 907 const pair = try cursor.readKeyValuePair(); 908 var patch_id: [evt.event_id_size]u8 = undefined; 909 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 910 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 911 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid) catch |err| { 912 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 913 continue; 914 }; 915 } 916 } 917 } 918 } 919