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