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