╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
1264 pub fn mergeAndRemoveFork( 1265 comptime repo_opts: rp.RepoOpts(.xit), 1266 io: std.Io, 1267 allocator: std.mem.Allocator, 1268 server: evt.Host.Server, 1269 target_repo: *rp.Repo(.xit, repo_opts), 1270 input: MergeInput, 1271 ) !void { 1272 try merge(repo_opts, io, allocator, server, target_repo, input); 1273 var arena = std.heap.ArenaAllocator.init(allocator); 1274 defer arena.deinit(); 1275 1276 // remove fork data only for patches backed by a fork 1277 const patch_id = try evt.parseEventId(&input.id); 1278 const record = (try evt.readFromRepo(evt.Patch, .xit, repo_opts, io, allocator, &arena, target_repo, &patch_id)) orelse return error.InvalidPatch; 1279 if (record.event.source_branch != null) return; 1280 // a patch from another instance has no fork here 1281 const forker_id = blk: { 1282 var config = try target_repo.listConfig(io, allocator); 1283 defer config.deinit(); 1284 break :blk (try fork.readForkerId(&config.sections, &patch_id)) orelse return; 1285 }; 1286 try fork.remove(io, allocator, server.users_dir, &forker_id, &input.id, input.author); 1287 } 1288 1289 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 1290 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 1291 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 1292 const name = std.mem.trimEnd(u8, line[0..open], " "); 1293 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 1294 return .{ .name = name, .email = line[open + 1 .. close] }; 1295 } 1296 1297 // import a revision after detecting a client-side merge 1298 fn importMerged( 1299 comptime repo_opts: rp.RepoOpts(.xit), 1300 state: rp.Repo(.xit, repo_opts).State(.read_write), 1301 db: *evt.EventDB(repo_opts.hash), 1302 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1303 io: std.Io, 1304 allocator: std.mem.Allocator, 1305 users_dir: []const u8, 1306 patch_id: *const [evt.event_id_size]u8, 1307 matched_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1308 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1309 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1310 ) !void { 1311 var arena = std.heap.ArenaAllocator.init(allocator); 1312 defer arena.deinit(); 1313 1314 // identify which selected revision was merged by the client. 1315 const events = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1316 const record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, events, &arena, patch_id)) orelse return; 1317 const selected = record.event.revision orelse return; 1318 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; 1319 1320 // open the fork when the revision is not stored in the target repository. 1321 var fork_repo_maybe: ?rp.Repo(.xit, repo_opts) = if (record.event.source_branch != null) null else blk: { 1322 var config = try xit.config.Config(.xit, repo_opts).init(state.readOnly(), io, allocator); 1323 defer config.deinit(); 1324 const forker_id = (try fork.readForkerId(&config.sections, patch_id)) orelse return error.PatchDataUnavailable; 1325 const fork_path = try fork.forkPath(arena.allocator(), users_dir, &forker_id, patch_id); 1326 break :blk try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 1327 }; 1328 defer if (fork_repo_maybe) |*repo| repo.deinit(io, allocator); 1329 1330 // import the revision and record the client-side merge. 1331 _ = 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); 1332 } 1333 1334 // import the revision and mark its patch merged in the current transaction 1335 fn importMergedRevision( 1336 comptime repo_opts: rp.RepoOpts(.xit), 1337 state: rp.Repo(.xit, repo_opts).State(.read_write), 1338 db: *evt.EventDB(repo_opts.hash), 1339 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1340 io: std.Io, 1341 allocator: std.mem.Allocator, 1342 fork_repo: ?*rp.Repo(.xit, repo_opts), 1343 patch_id: *const [evt.event_id_size]u8, 1344 merged: struct { 1345 revision: evt.Patch.MergeRevision, 1346 before_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1347 after_oid: *const [hash.hexLen(repo_opts.hash)]u8, 1348 }, 1349 expected_patch: ?evt.Patch, 1350 merged_author: ?evt.CommitAuthor, 1351 merged_timestamp: u64, 1352 ) !bool { 1353 const DB = evt.EventDB(repo_opts.hash); 1354 var arena = std.heap.ArenaAllocator.init(allocator); 1355 defer arena.deinit(); 1356 1357 // read the patch and reject outdated or conflicting merge requests. 1358 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1359 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 1360 const selected = patch_record.event.revision orelse return false; 1361 if (patch_record.removed or patch_record.event.status.kind() == .merged) return false; 1362 if (expected_patch) |expected| { 1363 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 1364 if (try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.conflicts_key))) |cursor| { 1365 const conflicts = try DB.SortedMap(.read_only).init(cursor); 1366 if (try conflicts.getCursor(&evt.orderKeyDesc(patch_record.created_order, patch_id)) != null) return false; 1367 } 1368 } 1369 const revision_id = try evt.parseEventId(&selected.id); 1370 1371 // resolve the selected revision from the target or its fork. 1372 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 1373 const revision = (if (fork_repo) |repo| 1374 try evt.PatchRev.readById(DB, repo_opts.hash, try evt.currentMoment(repo_opts, repo), &arena, &revision_id) 1375 else 1376 current_revision) orelse return false; 1377 if (!selected.matches(revision)) return false; 1378 1379 // read the original revision commit and author. 1380 var fork_repo_moment = if (fork_repo) |repo| try repo.core.latestMoment() else moment.readOnly(); 1381 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 } }; 1382 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1383 if (revision.event_oid.len != event_oid.len) return false; 1384 @memcpy(&event_oid, revision.event_oid); 1385 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 1386 defer event_object.deinit(); 1387 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 1388 1389 // retain the submitted revision if the target has not imported it yet. 1390 var events_buffer: [3]evt.EventWithId = undefined; 1391 var events: std.ArrayList(evt.EventWithId) = .initBuffer(&events_buffer); 1392 var tree_entries: [2]evt.EventTreeEntry = undefined; 1393 const needs_import = if (current_revision) |existing| existing.removed else true; 1394 if (needs_import) { 1395 tree_entries = .{ 1396 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 1397 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 1398 }; 1399 events.appendAssumeCapacity(.{ 1400 .id = selected.id, 1401 .timestamp = event_object.content.commit.metadata.timestamp, 1402 .author = original_author, 1403 .tree_entries = &tree_entries, 1404 .event = .{ .patchrev = revision.event }, 1405 }); 1406 } else { 1407 const existing = current_revision orelse unreachable; 1408 if (!selected.matches(existing)) return error.InvalidPatch; 1409 } 1410 1411 // snapshot the merge result and mark the patch as merged. 1412 { 1413 const author = merged_author orelse original_author; 1414 const timestamp = if (merged_author != null) merged_timestamp else @as(u64, @intCast(std.Io.Timestamp.now(io, .real).toSeconds())); 1415 var patch = patch_record.event; 1416 const result = try evt.PatchRev.prepare(.xit, repo_opts, state, io, &arena, merged.before_oid, merged.after_oid, patch.title, author, timestamp); 1417 events.appendAssumeCapacity(result.event); 1418 patch.status = .{ .merged = .{ .revision = merged.revision, .patchrev_id = result.event.id } }; 1419 events.appendAssumeCapacity(.{ 1420 .id = std.fmt.bytesToHex(patch_id.*, .lower), 1421 .timestamp = timestamp, 1422 .author = author, 1423 .event = .{ .patch = patch }, 1424 }); 1425 } 1426 1427 // commit and consume the revision and patch events together. 1428 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events.items, null); 1429 _ = try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref); 1430 return true; 1431 } 1432 1433 // find patches merged by commits received in a push 1434 pub fn detectMerged( 1435 comptime repo_opts: rp.RepoOpts(.xit), 1436 state: rp.Repo(.xit, repo_opts).State(.read_write), 1437 db: *evt.EventDB(repo_opts.hash), 1438 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 1439 io: std.Io, 1440 allocator: std.mem.Allocator, 1441 users_dir: []const u8, 1442 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 1443 error_writer: *std.Io.Writer, 1444 ) !void { 1445 const DB = evt.EventDB(repo_opts.hash); 1446 var arena = std.heap.ArenaAllocator.init(allocator); 1447 defer arena.deinit(); 1448 1449 // read the revision index used to identify merged patches. 1450 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 1451 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 1452 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 1453 1454 // ignore event refs and inspect only changed branch tips. 1455 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 1456 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 1457 const heads_prefix = "refs/heads/"; 1458 for (updates) |update| { 1459 if (!std.mem.startsWith(u8, update.ref_name, heads_prefix) or 1460 std.mem.eql(u8, update.ref_name, events_ref_path) or 1461 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 1462 1463 // validate the ref tips and skip branch deletions. 1464 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 1465 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 1466 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1467 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 1468 @memcpy(&old_oid, update.old_oid); 1469 @memcpy(&new_oid, update.new_oid); 1470 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 1471 1472 // traverse commits introduced by this branch update. 1473 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 1474 defer commits.deinit(); 1475 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 1476 var targets: std.ArrayList(struct { 1477 before_oid: [hash.hexLen(repo_opts.hash)]u8, 1478 after_oid: [hash.hexLen(repo_opts.hash)]u8, 1479 }) = .empty; 1480 1481 // collect merge boundaries along the target's first-parent history. 1482 { 1483 var first_parents = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit, .first_parent = true }); 1484 defer first_parents.deinit(); 1485 if (!std.mem.allEqual(u8, &old_oid, '0')) try first_parents.exclude(&old_oid); 1486 try first_parents.include(&new_oid); 1487 while (try first_parents.next(arena.allocator())) |commit| { 1488 defer commit.deinit(); 1489 try targets.append(arena.allocator(), .{ 1490 .before_oid = (commit.content.commit.metadata.firstParent() orelse &old_oid).*, 1491 .after_oid = commit.oid, 1492 }); 1493 } 1494 } 1495 1496 // visit target commits oldest first, attributing each revision to the 1497 // commit that introduced it; the shared iterator visits each ancestor once 1498 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 1499 while (targets.pop()) |target| { 1500 try commits.include(&target.after_oid); 1501 while (try commits.next(arena.allocator())) |commit| { 1502 defer commit.deinit(); 1503 1504 // find patches whose selected revision matches this commit. 1505 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name[heads_prefix.len..], &commit.oid }); 1506 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 1507 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 1508 var ids_iter = try ids.iterator(); 1509 while (try ids_iter.next()) |cursor| { 1510 const pair = try cursor.readKeyValuePair(); 1511 var patch_id: [evt.event_id_size]u8 = undefined; 1512 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 1513 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 1514 1515 // a fast-forward has no recorded merge boundary; use the old ref tip 1516 const before_oid = if (std.mem.eql(u8, &commit.oid, &target.after_oid)) &old_oid else &target.before_oid; 1517 importMerged(repo_opts, state, db, moment, io, allocator, users_dir, &patch_id, &commit.oid, before_oid, &target.after_oid) catch |err| { 1518 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 1519 continue; 1520 }; 1521 } 1522 } 1523 } 1524 } 1525 } 1526