╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
860 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 861 if (revision.event_oid.len != event_oid.len) return false; 862 @memcpy(&event_oid, revision.event_oid); 863 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 864 defer event_object.deinit(); 865 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 866 867 var events_buffer: [3]evt.EventWithId = undefined; 868 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 869 var tree_entries: [2]evt.EventTreeEntry = undefined; 870 const needs_import = if (current_revision) |existing| existing.removed else true; 871 if (needs_import) { 872 tree_entries = .{ 873 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 874 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 875 }; 876 events.appendAssumeCapacity(.{ 877 .id = selected.id, 878 .timestamp = event_object.content.commit.metadata.timestamp, 879 .author = original_author, 880 .tree_entries = &tree_entries, 881 .event = .{ .patchrev = revision.event }, 882 }); 883 } else { 884 const existing = current_revision orelse unreachable; 885 if (!selected.matches(existing)) return error.InvalidPatch; 886 } 887 888 const author = merged_author orelse original_author; 889 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 890 var patch = patch_record.event; 891 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 892 events.appendAssumeCapacity(result.event); 893 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 894 events.appendAssumeCapacity(.{ 895 .id = std.fmt.bytesToHex(patch_id.*, .lower), 896 .timestamp = timestamp, 897 .author = author, 898 .event = .{ .patch = patch }, 899 }); 900 901 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 902 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 903 return true; 904 } 905 906 // find patches merged by commits received in a push 907 pub fn detectMerged( 908 comptime repo_opts: rp.RepoOpts(.xit), 909 state: rp.Repo(.xit, repo_opts).State(.read_write), 910 db: *evt.EventDB(repo_opts.hash), 911 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 912 io: std.Io, 913 allocator: std.mem.Allocator, 914 repo_root_path: []const u8, 915 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 916 error_writer: *std.Io.Writer, 917 ) !void { 918 const DB = evt.EventDB(repo_opts.hash); 919 var arena = std.heap.ArenaAllocator.init(allocator); 920 defer arena.deinit(); 921 922 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 923 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 924 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 925 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 926 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 927 const heads_prefix = "refs/heads/"; 928 for (updates) |update| { 929 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 930 std.mem.eql(u8, update.ref_name, events_ref_path) or 931 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 932 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 933 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 934 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 935 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 936 @memcpy(&old_oid, update.old_oid); 937 @memcpy(&new_oid, update.new_oid); 938 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 939 940 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 941 defer commits.deinit(); 942 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 943 var targets: std.ArrayList(struct { 944 before_oid: [hash.hexLen(repo_opts.hash)]u8, 945 after_oid: [hash.hexLen(repo_opts.hash)]u8, 946 }) = .empty; 947 { 948 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 949 defer first_parents.deinit(); 950 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 951 try first_parents.include(&new_oid); 952 while (try first_parents.next(arena.allocator())) |commit| { 953 defer commit.deinit(); 954 try targets.append(arena.allocator(), .{ 955 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 956 .after_oid = commit.oid, 957 }); 958 } 959 } 960 961 // visit target commits oldest first, attributing each revision to the 962 // commit that introduced it; the shared iterator visits each ancestor once 963 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 964 while (targets.pop()) |target| { 965 try commits.include(&target.after_oid); 966 while (try commits.next(arena.allocator())) |commit| { 967 defer commit.deinit(); 968 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 969 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 970 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 971 var ids_iter = try ids.iterator(); 972 while (try ids_iter.next()) |cursor| { 973 const pair = try cursor.readKeyValuePair(); 974 var patch_id: [evt.event_id_size]u8 = undefined; 975 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 976 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 977 // a fast-forward has no recorded merge boundary; use the old ref tip 978 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 979 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 980 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 981 continue; 982 }; 983 } 984 } 985 } 986 } 987 } 988