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