╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1094 else 1095 current_revision) orelse return false; 1096 if (!selected.matches(revision)) return false; 1097 1098 // read the original revision commit and author. 1099 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1100 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 } }; 1101 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1102 if (revision.event_oid.len != event_oid.len) return false; 1103 @memcpy(&event_oid, revision.event_oid); 1104 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1105 defer event_object.deinit(); 1106 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1107 1108 // retain the submitted revision if the target has not imported it yet. 1109 var events_buffer: [3]evt.EventWithId = undefined; 1110 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1111 var tree_entries: [2]evt.EventTreeEntry = undefined; 1112 const needs_import = if (current_revision) |existing| existing.removed else true; 1113 if (needs_import) { 1114 tree_entries = .{ 1115 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1116 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1117 }; 1118 events.appendAssumeCapacity(.{ 1119 .id = selected.id, 1120 .timestamp = event_object.content.commit.metadata.timestamp, 1121 .author = original_author, 1122 .tree_entries = &tree_entries, 1123 .event = .{ .patchrev = revision.event }, 1124 }); 1125 } else { 1126 const existing = current_revision orelse unreachable; 1127 if (!selected.matches(existing)) return error.InvalidPatch; 1128 } 1129 1130 // snapshot the merge result and mark the patch as merged. 1131 { 1132 const author = merged_author orelse original_author; 1133 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1134 var patch = patch_record.event; 1135 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1136 events.appendAssumeCapacity(result.event); 1137 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1138 events.appendAssumeCapacity(.{ 1139 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1140 .timestamp = timestamp, 1141 .author = author, 1142 .event = .{ .patch = patch }, 1143 }); 1144 } 1145 1146 // commit and consume the revision and patch events together. 1147 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1148 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1149 return true; 1150 } 1151 1152 // find patches merged by commits received in a push 1153 pub fn detectMerged( 1154 comptime repo_opts: rp.RepoOpts(.xit), 1155 state: rp.Repo(.xit, repo_opts).State(.read_write), 1156 db: *evt.EventDB(repo_opts.hash), 1157 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1158 io: std.Io, 1159 allocator: std.mem.Allocator, 1160 repo_root_path: []const u8, 1161 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1162 error_writer: *std.Io.Writer, 1163 ) !void { 1164 const DB = evt.EventDB(repo_opts.hash); 1165 var arena = std.heap.ArenaAllocator.init(allocator); 1166 defer arena.deinit(); 1167 1168 // read the revision index used to identify merged patches. 1169 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1170 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1171 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1172 1173 // ignore event refs and inspect only changed branch tips. 1174 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1175 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1176 const heads_prefix = "refs/heads/"; 1177 for (updates) |update| { 1178 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1179 std.mem.eql(u8, update.ref_name, events_ref_path) or 1180 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1181 1182 // validate the ref tips and skip branch deletions. 1183 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1184 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1185 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1186 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1187 @memcpy(&old_oid, update.old_oid); 1188 @memcpy(&new_oid, update.new_oid); 1189 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1190 1191 // traverse commits introduced by this branch update. 1192 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1193 defer commits.deinit(); 1194 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1195 var targets: std.ArrayList(struct { 1196 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1197 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1198 }) = .empty; 1199 1200 // collect merge boundaries along the target's first-parent history. 1201 { 1202 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1203 defer first_parents.deinit(); 1204 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1205 try first_parents.include(&new_oid); 1206 while (try first_parents.next(arena.allocator())) |commit| { 1207 defer commit.deinit(); 1208 try targets.append(arena.allocator(), .{ 1209 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1210 .after_oid = commit.oid, 1211 }); 1212 } 1213 } 1214 1215 // visit target commits oldest first, attributing each revision to the 1216 // commit that introduced it; the shared iterator visits each ancestor once 1217 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1218 while (targets.pop()) |target| { 1219 try commits.include(&target.after_oid); 1220 while (try commits.next(arena.allocator())) |commit| { 1221 defer commit.deinit(); 1222 1223 // find patches whose selected revision matches this commit. 1224 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1225 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1226 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1227 var ids_iter = try ids.iterator(); 1228 while (try ids_iter.next()) |cursor| { 1229 const pair = try cursor.readKeyValuePair(); 1230 var patch_id: [evt.event_id_size]u8 = undefined; 1231 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1232 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1233 1234 // a fast-forward has no recorded merge boundary; use the old ref tip 1235 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1236 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1237 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1238 continue; 1239 }; 1240 } 1241 } 1242 } 1243 } 1244 } 1245