╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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