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