╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
901 repo_root_path: []const u8, 902 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 903 error_writer: *std.Io.Writer, 904 ) !void { 905 const DB = evt.EventDB(repo_opts.hash); 906 var arena = std.heap.ArenaAllocator.init(allocator); 907 defer arena.deinit(); 908 909 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 910 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 911 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 912 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 913 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 914 const heads_prefix = "refs/heads/"; 915 for (updates) |update| { 916 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 917 std.mem.eql(u8, update.ref_name, events_ref_path) or 918 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 919 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 920 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 921 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 922 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 923 @memcpy(&old_oid, update.old_oid); 924 @memcpy(&new_oid, update.new_oid); 925 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 926 927 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 928 defer commits.deinit(); 929 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 930 var targets: std.ArrayList(struct { 931 before_oid: [hash.hexLen(repo_opts.hash)]u8, 932 after_oid: [hash.hexLen(repo_opts.hash)]u8, 933 }) = .empty; 934 { 935 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 936 defer first_parents.deinit(); 937 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 938 try first_parents.include(&new_oid); 939 while (try first_parents.next(arena.allocator())) |commit| { 940 defer commit.deinit(); 941 try targets.append(arena.allocator(), .{ 942 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 943 .after_oid = commit.oid, 944 }); 945 } 946 } 947 948 // visit target commits oldest first, attributing each revision to the 949 // commit that introduced it; the shared iterator visits each ancestor once 950 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 951 while (targets.pop()) |target| { 952 try commits.include(&target.after_oid); 953 while (try commits.next(arena.allocator())) |commit| { 954 defer commit.deinit(); 955 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 956 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 957 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 958 var ids_iter = try ids.iterator(); 959 while (try ids_iter.next()) |cursor| { 960 const pair = try cursor.readKeyValuePair(); 961 var patch_id: [evt.event_id_size]u8 = undefined; 962 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 963 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 964 // a fast-forward has no recorded merge boundary; use the old ref tip 965 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 966 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 967 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 968 continue; 969 }; 970 } 971 } 972 } 973 } 974 } 975