╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1285 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1286 var tree_entries: [2]evt.EventTreeEntry = undefined; 1287 const needs_import = if (current_revision) |existing| existing.removed else true; 1288 if (needs_import) { 1289 tree_entries = .{ 1290 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1291 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1292 }; 1293 events.appendAssumeCapacity(.{ 1294 .id = selected.id, 1295 .timestamp = event_object.content.commit.metadata.timestamp, 1296 .author = original_author, 1297 .tree_entries = &tree_entries, 1298 .event = .{ .patchrev = revision.event }, 1299 }); 1300 } else { 1301 const existing = current_revision orelse unreachable; 1302 if (!selected.matches(existing)) return error.InvalidPatch; 1303 } 1304 1305 // snapshot the merge result and mark the patch as merged. 1306 { 1307 const author = merged_author orelse original_author; 1308 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1309 var patch = patch_record.event; 1310 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1311 events.appendAssumeCapacity(result.event); 1312 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1313 events.appendAssumeCapacity(.{ 1314 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1315 .timestamp = timestamp, 1316 .author = author, 1317 .event = .{ .patch = patch }, 1318 }); 1319 } 1320 1321 // commit and consume the revision and patch events together. 1322 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1323 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 1324 return true; 1325 } 1326 1327 // find patches merged by commits received in a push 1328 pub fn detectMerged( 1329 comptime repo_opts: rp.RepoOpts(.xit), 1330 state: rp.Repo(.xit, repo_opts).State(.read_write), 1331 db: *evt.EventDB(repo_opts.hash), 1332 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1333 io: std.Io, 1334 allocator: std.mem.Allocator, 1335 repo_root_path: []const u8, 1336 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1337 error_writer: *std.Io.Writer, 1338 ) !void { 1339 const DB = evt.EventDB(repo_opts.hash); 1340 var arena = std.heap.ArenaAllocator.init(allocator); 1341 defer arena.deinit(); 1342 1343 // read the revision index used to identify merged patches. 1344 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1345 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1346 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1347 1348 // ignore event refs and inspect only changed branch tips. 1349 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1350 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1351 const heads_prefix = "refs/heads/"; 1352 for (updates) |update| { 1353 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1354 std.mem.eql(u8, update.ref_name, events_ref_path) or 1355 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1356 1357 // validate the ref tips and skip branch deletions. 1358 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1359 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1360 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1361 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1362 @memcpy(&old_oid, update.old_oid); 1363 @memcpy(&new_oid, update.new_oid); 1364 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1365 1366 // traverse commits introduced by this branch update. 1367 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1368 defer commits.deinit(); 1369 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1370 var targets: std.ArrayList(struct { 1371 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1372 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1373 }) = .empty; 1374 1375 // collect merge boundaries along the target's first-parent history. 1376 { 1377 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1378 defer first_parents.deinit(); 1379 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1380 try first_parents.include(&new_oid); 1381 while (try first_parents.next(arena.allocator())) |commit| { 1382 defer commit.deinit(); 1383 try targets.append(arena.allocator(), .{ 1384 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1385 .after_oid = commit.oid, 1386 }); 1387 } 1388 } 1389 1390 // visit target commits oldest first, attributing each revision to the 1391 // commit that introduced it; the shared iterator visits each ancestor once 1392 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1393 while (targets.pop()) |target| { 1394 try commits.include(&target.after_oid); 1395 while (try commits.next(arena.allocator())) |commit| { 1396 defer commit.deinit(); 1397 1398 // find patches whose selected revision matches this commit. 1399 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1400 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1401 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1402 var ids_iter = try ids.iterator(); 1403 while (try ids_iter.next()) |cursor| { 1404 const pair = try cursor.readKeyValuePair(); 1405 var patch_id: [evt.event_id_size]u8 = undefined; 1406 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1407 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1408 1409 // a fast-forward has no recorded merge boundary; use the old ref tip 1410 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1411 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1412 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1413 continue; 1414 }; 1415 } 1416 } 1417 } 1418 } 1419 } 1420