╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
935 .event = .{ .patch = null }, 936 }}); 937 } 938 939 // mark the fork as published in the administration repository 940 if (fork_record.event.stage == .draft) { 941 var published = fork_record.event; 942 published.stage = .publish; 943 try evt.consume(.server, .admin, .xit, evt.admin_repo_opts, io, allocator, admin_repo, evt.events_ref, &.{.{ 944 .id = input.id, 945 .timestamp = input.timestamp, 946 .author = input.author, 947 .event = .{ .fork = published }, 948 }}); 949 } 950 } 951 952 pub fn editDraft( 953 comptime repo_opts: rp.RepoOpts(.xit), 954 io: std.Io, 955 allocator: std.mem.Allocator, 956 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 957 fork_path: []const u8, 958 input: EditDraftInput, 959 ) !bool { 960 // validate the submitted fields before opening repositories 961 if (!evt.Patch.fieldsValid(input.title, input.tags)) return error.InvalidFields; 962 if (!evt.Patch.branchValid(input.target_branch)) return error.InvalidTargetBranch; 963 const patch_id = try evt.parseEventId(&input.id); 964 var arena = std.heap.ArenaAllocator.init(allocator); 965 defer arena.deinit(); 966 967 // validate the draft ownership and editing identity 968 { 969 const admin_moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 970 const fork_record = (try evt.Fork.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &patch_id)) orelse return false; 971 if (fork_record.event.stage != .draft) return false; 972 if (fork_record.removed or 973 !std.mem.eql(u8, fork_record.event.user_id, &input.user_id) or 974 !std.mem.eql(u8, fork_record.event.repo_id, &input.repo_id)) return error.InvalidPatchDraft; 975 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, admin_moment, &arena, &input.user_id)) orelse return error.InvalidPatchDraft; 976 if (user.removed or 977 !std.mem.eql(u8, user.event.name, input.author.name) or 978 !std.mem.eql(u8, user.event.email, input.author.email)) return error.InvalidPatchDraft; 979 } 980 981 // read the draft patch from its fork 982 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 983 defer fork_repo.deinit(io, allocator); 984 const fork_moment = evt.currentMoment(repo_opts, &fork_repo) catch return error.PatchDataUnavailable; 985 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena, &patch_id)) orelse return error.PatchDataUnavailable; 986 if (record.removed) return error.PatchDataUnavailable; 987 988 // apply the edits and invalidate a revision targeting another branch 989 var patch = record.event; 990 patch.title = input.title; 991 patch.tags = input.tags; 992 patch.description = input.description; 993 if (!std.mem.eql(u8, patch.target_branch, input.target_branch)) patch.revision = null; 994 patch.target_branch = input.target_branch; 995 996 // commit the updated draft metadata 997 try evt.consume(.server, .fork, .xit, repo_opts, io, allocator, &fork_repo, evt.events_ref, &.{.{ 998 .id = input.id, 999 .timestamp = input.timestamp, 1000 .author = input.author, 1001 .event = .{ .patch = patch }, 1002 }}); 1003 return true; 1004 } 1005 1006 // merge the selected patch revision into its target branch 1007 pub fn merge( 1008 comptime repo_opts: rp.RepoOpts(.xit), 1009 io: std.Io, 1010 allocator: std.mem.Allocator, 1011 repo_root_path: []const u8, 1012 target_repo: *rp.Repo(.xit, repo_opts), 1013 input: MergeInput, 1014 ) !void { 1015 const patch_id = try evt.parseEventId(&input.id); 1016 errdefer |err| { 1017 if (err == error.PatchDataUnavailable or err == error.PatchOutOfDate) { 1018 refreshMergeability(repo_opts, io, allocator, target_repo, patch_id); 1019 } 1020 } 1021 var arena = std.heap.ArenaAllocator.init(allocator); 1022 defer arena.deinit(); 1023 1024 // validate the target patch 1025 const target_moment = try evt.currentMoment(repo_opts, target_repo); 1026 const patch_record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_moment, &arena, &patch_id)) orelse return error.InvalidPatch; 1027 if (patch_record.removed) return error.InvalidPatch; 1028 switch (patch_record.event.status) { 1029 .open => {}, 1030 .closed => return error.PatchClosed, 1031 .merged => return error.PatchAlreadyMerged, 1032 } 1033 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 1034 const target_ref = rf.Ref{ .kind = .head, .name = patch_record.event.target_branch }; 1035 1036 // open the fork 1037 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 1038 var fork_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (patch_record.event.source_branch != null) null else rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 1039 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1040 1041 // select the commit and merge identity 1042 const selected_oid = oidArray(repo_opts.hash, switch (input.revision) { 1043 .squash => selected.squash_oid, 1044 .source => selected.source_oid, 1045 }) catch return error.InvalidPatch; 1046 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 1047 1048 // merge the code and events in one target transaction 1049 { 1050 const DB = rp.Repo(.xit, repo_opts).DB; 1051 const State = rp.Repo(.xit, repo_opts).State; 1052 const Ctx = struct { 1053 core: *rp.Repo(.xit, repo_opts).Core, 1054 fork_repo: ?*rp.Repo(.xit, repo_opts), 1055 io: std.Io, 1056 allocator: std.mem.Allocator, 1057 patch_id: [evt.event_id_size]u8, 1058 expected_patch: evt.Patch, 1059 target_ref: rf.Ref, 1060 selected_oid: [hash.hexLen(repo_opts.hash)]u8, 1061 revision: evt.Patch.MergeRevision, 1062 identity: []const u8, 1063 author: evt.CommitAuthor, 1064 timestamp: u64, 1065 1066 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 1067 var moment = try DB.HashMap(.read_write).init(cursor.*); 1068 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 1069 1070 var check_arena = std.heap.ArenaAllocator.init(ctx.allocator); 1071 defer check_arena.deinit(); 1072 1073 // require the fork's newest revision while both repos are locked 1074 const selected_revision = ctx.expected_patch.revision orelse unreachable; 1075 const revision_id = try evt.parseEventId(&selected_revision.id); 1076 if (ctx.fork_repo) |repo| { 1077 const fork_moment = try evt.currentMoment(repo_opts, repo); 1078 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &check_arena)) orelse return error.PatchDataUnavailable; 1079 if (!std.mem.eql(u8, &newest.id, &revision_id) or !selected_revision.matches(newest.record)) return error.PatchOutOfDate; 1080 } else if (!try branchCurrent(repo_opts, state.readOnly(), ctx.io, &check_arena, ctx.expected_patch)) return error.PatchOutOfDate; 1081 1082 // require a clean cached result for the selected merge style. 1083 { 1084 const availability = try readMergeability(repo_opts, state.readOnly(), ctx.io, &check_arena, &ctx.patch_id, ctx.expected_patch); 1085 switch (availability.get(ctx.revision)) { 1086 .clean => {}, 1087 .conflict => return error.MergeConflict, 1088 .unknown => return error.MergeCheckUnavailable, 1089 } 1090 } 1091 1092 // copy the selected commit and its dependencies 1093 if (ctx.fork_repo) |repo| { 1094 var fork_repo_moment = try repo.core.latestMoment(); 1095 const fork_state = State(.read_only){ .core = &repo.core, .extra = .{ .moment = &fork_repo_moment } }; 1096 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 1097 defer objects.deinit(); 1098 try objects.include(&ctx.selected_oid); 1099 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 1100 } 1101 1102 // merge the selected commit 1103 const before_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1104 { 1105 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 1106 .kind = .full, 1107 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.selected_oid }}, .algo = .patch } }, 1108 .commit_metadata = .{ 1109 .author = ctx.identity, 1110 .committer = ctx.identity, 1111 .message = ctx.expected_patch.title, 1112 .timestamp = ctx.timestamp, 1113 }, 1114 }, ctx.target_ref, null); 1115 defer merge_result.deinit(); 1116 switch (merge_result.result) { 1117 .conflict => return error.MergeConflict, 1118 .success, .fast_forward, .nothing => {}, 1119 .clean => unreachable, 1120 } 1121 } 1122 const after_oid = (try rf.readRecur(.xit, repo_opts, state.readOnly(), ctx.io, .{ .ref = ctx.target_ref })) orelse return error.InvalidTargetBranch; 1123 1124 // retain the submitted revision and snapshot the merge result 1125 if (!try importMergedRevision(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.fork_repo, &ctx.patch_id, .{ .revision = ctx.revision, .before_oid = &before_oid, .after_oid = &after_oid }, ctx.expected_patch, ctx.author, ctx.timestamp)) return error.PatchOutOfDate; 1126 1127 // record the merge as one undoable action. 1128 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = merge_undo_action } }); 1129 } 1130 }; 1131 1132 // lock the fork before the target for the full merge transaction. 1133 if (fork_repo_maybe) |*repo| try repo.core.db_file.lock(io, .shared); 1134 defer if (fork_repo_maybe) |*repo| repo.core.db_file.unlock(io); 1135 try target_repo.core.db_file.lock(io, .exclusive); 1136 defer target_repo.core.db_file.unlock(io); 1137 1138 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 1139 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 1140 .core = &target_repo.core, 1141 .fork_repo = if (fork_repo_maybe) |*repo| repo else null, 1142 .io = io, 1143 .allocator = allocator, 1144 .patch_id = patch_id, 1145 .expected_patch = patch_record.event, 1146 .target_ref = target_ref, 1147 .selected_oid = selected_oid, 1148 .revision = input.revision, 1149 .identity = identity, 1150 .author = input.author, 1151 .timestamp = input.timestamp, 1152 }); 1153 } 1154 1155 // refresh other patches affected by the updated target branch. 1156 refreshOpenMergeability(repo_opts, io, allocator, target_repo, null, null); 1157 1158 // the merge moved the target branch, so its indexes follow 1159 find.refresh(repo_opts, io, allocator, target_repo) catch |err| { 1160 std.log.warn("failed to refresh file index: {s}", .{@errorName(err)}); 1161 }; 1162 srch_cmmt.refresh(repo_opts, io, allocator, target_repo, null) catch |err| { 1163 std.log.warn("failed to refresh commit index: {s}", .{@errorName(err)}); 1164 }; 1165 } 1166 1167 // merge a published patch and remove its fork 1168 pub fn mergeAndRemoveFork( 1169 comptime repo_opts: rp.RepoOpts(.xit), 1170 io: std.Io, 1171 allocator: std.mem.Allocator, 1172 repo_root_path: []const u8, 1173 admin_repo: *rp.Repo(.xit, evt.admin_repo_opts), 1174 target_repo: *rp.Repo(.xit, repo_opts), 1175 input: MergeInput, 1176 ) !void { 1177 try merge(repo_opts, io, allocator, repo_root_path, target_repo, input); 1178 var arena = std.heap.ArenaAllocator.init(allocator); 1179 defer arena.deinit(); 1180 1181 // remove fork data only for patches backed by a fork. 1182 const record = (try evt.readFromRepo(evt.Patch, .xit, repo_opts, io, allocator, &arena, target_repo, &try evt.parseEventId(&input.id))) orelse return error.InvalidPatch; 1183 if (record.event.source_branch == null) try fork.remove(io, allocator, repo_root_path, admin_repo, &input.id, null, input.author); 1184 } 1185 1186 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 1187 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 1188 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 1189 const name = std.mem.trimEnd(u8, line[0..open], " "); 1190 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 1191 return .{ .name = name, .email = line[open + 1 .. close] }; 1192 } 1193 1194 // import a revision after detecting a client-side merge 1195 fn importMerged( 1196 comptime repo_opts: rp.RepoOpts(.xit), 1197 state: rp.Repo(.xit, repo_opts).State(.read_write), 1198 db: *evt.EventDB(repo_opts.hash), 1199 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1200 io: std.Io, 1201 allocator: std.mem.Allocator, 1202 repo_root_path: []const u8, 1203 patch_id: *const [evt.event_id_size]u8, 1204 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1205 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1206 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1207 ) !void { 1208 var arena = std.heap.ArenaAllocator.init(allocator); 1209 defer arena.deinit(); 1210 1211 // identify which selected revision was merged by the client. 1212 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 1213 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 1214 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1215 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 1216 const selected = record.event.revision orelse return; 1217 const revision: evt.Patch.MergeRevision = if (std.mem.eql(u8, matched_oid, selected.source_oid)) .source else if (std.mem.eql(u8, matched_oid, selected.squash_oid)) .squash else return; 1218 1219 // open the fork when the revision is not stored in the target repository. 1220 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 }); 1221 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1222 1223 // import the revision and record the client-side merge. 1224 _ = try importMergedRevision(repo_opts, state, db, moment, io, allocator, if (fork_repo_maybe) |*repo| repo else null, patch_id, .{ .revision = revision, .before_oid = before_oid, .after_oid = after_oid }, null, null, 0); 1225 } 1226 1227 // import the revision and mark its patch merged in the current transaction 1228 fn importMergedRevision( 1229 comptime repo_opts: rp.RepoOpts(.xit), 1230 state: rp.Repo(.xit, repo_opts).State(.read_write), 1231 db: *evt.EventDB(repo_opts.hash), 1232 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1233 io: std.Io, 1234 allocator: std.mem.Allocator, 1235 fork_repo: ?*rp.Repo(.xit, repo_opts), 1236 patch_id: *const [evt.event_id_size]u8, 1237 merged: struct { 1238 revision: evt.Patch.MergeRevision, 1239 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1240 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1241 }, 1242 expected_patch: ?evt.Patch, 1243 merged_author: ?evt.CommitAuthor, 1244 merged_timestamp: u64, 1245 ) !bool { 1246 const DB = evt.EventDB(repo_opts.hash); 1247 var arena = std.heap.ArenaAllocator.init(allocator); 1248 defer arena.deinit(); 1249 1250 // read the patch and reject outdated or conflicting merge requests. 1251 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1252 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1253 const selected = patch_record.event.revision orelse return false; 1254 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1255 if (expected_patch) |expected| { 1256 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1257 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1258 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1259 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1260 } 1261 } 1262 const revision_id = try evt.parseEventId(&selected.id); 1263 1264 // resolve the selected revision from the target or its fork. 1265 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1266 const revision = (if (fork_repo) |repo| 1267 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1268 else 1269 current_revision) orelse return false; 1270 if (!selected.matches(revision)) return false; 1271 1272 // read the original revision commit and author. 1273 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1274 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 } }; 1275 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1276 if (revision.event_oid.len != event_oid.len) return false; 1277 @memcpy(&event_oid, revision.event_oid); 1278 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1279 defer event_object.deinit(); 1280 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1281 1282 // retain the submitted revision if the target has not imported it yet. 1283 var events_buffer: [3]evt.EventWithId = undefined; 1284 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1285 var tree_entries: [2]evt.EventTreeEntry = undefined; 1286 const needs_import = if (current_revision) |existing| existing.removed else true; 1287 if (needs_import) { 1288 tree_entries = .{ 1289 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1290 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1291 }; 1292 events.appendAssumeCapacity(.{ 1293 .id = selected.id, 1294 .timestamp = event_object.content.commit.metadata.timestamp, 1295 .author = original_author, 1296 .tree_entries = &tree_entries, 1297 .event = .{ .patchrev = revision.event }, 1298 }); 1299 } else { 1300 const existing = current_revision orelse unreachable; 1301 if (!selected.matches(existing)) return error.InvalidPatch; 1302 } 1303 1304 // snapshot the merge result and mark the patch as merged. 1305 { 1306 const author = merged_author orelse original_author; 1307 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1308 var patch = patch_record.event; 1309 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1310 events.appendAssumeCapacity(result.event); 1311 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1312 events.appendAssumeCapacity(.{ 1313 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1314 .timestamp = timestamp, 1315 .author = author, 1316 .event = .{ .patch = patch }, 1317 }); 1318 } 1319 1320 // commit and consume the revision and patch events together. 1321 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1322 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1323 return true; 1324 } 1325 1326 // find patches merged by commits received in a push 1327 pub fn detectMerged( 1328 comptime repo_opts: rp.RepoOpts(.xit), 1329 state: rp.Repo(.xit, repo_opts).State(.read_write), 1330 db: *evt.EventDB(repo_opts.hash), 1331 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1332 io: std.Io, 1333 allocator: std.mem.Allocator, 1334 repo_root_path: []const u8, 1335 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1336 error_writer: *std.Io.Writer, 1337 ) !void { 1338 const DB = evt.EventDB(repo_opts.hash); 1339 var arena = std.heap.ArenaAllocator.init(allocator); 1340 defer arena.deinit(); 1341 1342 // read the revision index used to identify merged patches. 1343 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1344 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1345 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1346 1347 // ignore event refs and inspect only changed branch tips. 1348 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1349 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1350 const heads_prefix = "refs/heads/"; 1351 for (updates) |update| { 1352 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1353 std.mem.eql(u8, update.ref_name, events_ref_path) or 1354 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1355 1356 // validate the ref tips and skip branch deletions. 1357 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1358 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1359 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1360 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1361 @memcpy(&old_oid, update.old_oid); 1362 @memcpy(&new_oid, update.new_oid); 1363 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1364 1365 // traverse commits introduced by this branch update. 1366 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1367 defer commits.deinit(); 1368 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1369 var targets: std.ArrayList(struct { 1370 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1371 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1372 }) = .empty; 1373 1374 // collect merge boundaries along the target's first-parent history. 1375 { 1376 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1377 defer first_parents.deinit(); 1378 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1379 try first_parents.include(&new_oid); 1380 while (try first_parents.next(arena.allocator())) |commit| { 1381 defer commit.deinit(); 1382 try targets.append(arena.allocator(), .{ 1383 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1384 .after_oid = commit.oid, 1385 }); 1386 } 1387 } 1388 1389 // visit target commits oldest first, attributing each revision to the 1390 // commit that introduced it; the shared iterator visits each ancestor once 1391 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1392 while (targets.pop()) |target| { 1393 try commits.include(&target.after_oid); 1394 while (try commits.next(arena.allocator())) |commit| { 1395 defer commit.deinit(); 1396 1397 // find patches whose selected revision matches this commit. 1398 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1399 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1400 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1401 var ids_iter = try ids.iterator(); 1402 while (try ids_iter.next()) |cursor| { 1403 const pair = try cursor.readKeyValuePair(); 1404 var patch_id: [evt.event_id_size]u8 = undefined; 1405 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1406 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1407 1408 // a fast-forward has no recorded merge boundary; use the old ref tip 1409 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1410 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1411 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1412 continue; 1413 }; 1414 } 1415 } 1416 } 1417 } 1418 } 1419