╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
343 .id = std.fmt.bytesToHex(id.*, .lower), 344 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 345 .author = author, 346 .event = .{ .patch = updated }, 347 }}); 348 } 349 350 fn resolveFields( 351 comptime repo_kind: rp.RepoKind, 352 comptime repo_opts: rp.RepoOpts(repo_kind), 353 io: std.Io, 354 allocator: std.mem.Allocator, 355 arena: *std.heap.ArenaAllocator, 356 repo: *rp.Repo(repo_kind, repo_opts), 357 id: *const [evt.event_id_size]u8, 358 live: Record, 359 resolve: Resolve, 360 ) !Self { 361 const DB = evt.EventDB(repo_opts.hash); 362 var updated = live.event; 363 if (resolve.title) |title| updated.title = title; 364 if (resolve.tags) |tags| updated.tags = tags; 365 366 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 367 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 368 const moment = (if (event_db_maybe) |*event_db| 369 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 370 else if (repo_kind == .git) 371 return updated 372 else 373 evt.currentMoment(repo_opts, repo)) catch return updated; 374 const conflicts_cursor = (try moment.getCursor(hash.hashInt(repo_opts.hash, conflicts_key))) orelse return updated; 375 const conflicts = try DB.SortedMap(.read_only).init(conflicts_cursor); 376 const conflict_cursor = (try conflicts.getCursor(&evt.orderKeyDesc(live.created_order, id))) orelse return updated; 377 const entry = try DB.HashMap(.read_only).init(conflict_cursor); 378 const fields_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.conflicted_fields_key))) orelse return updated; 379 const fields = try fields_cursor.readBytesAlloc(arena.allocator(), null); 380 const their_cursor = (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.their_record_key))) orelse return updated; 381 const theirs = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(their_cursor)); 382 383 const original_target_branch = updated.target_branch; 384 var field_iter = std.mem.splitScalar(u8, fields, ' '); 385 while (field_iter.next()) |field| { 386 if (std.mem.eql(u8, field, "status") and fieldListed(resolve.theirs, ',', field)) updated.status = theirs.event.status; 387 if (std.mem.eql(u8, field, "revision") and fieldListed(resolve.theirs, ',', field)) updated.revision = theirs.event.revision; 388 if (std.mem.eql(u8, field, "target_branch") and fieldListed(resolve.theirs, ',', field)) updated.target_branch = theirs.event.target_branch; 389 } 390 if (!std.mem.eql(u8, original_target_branch, updated.target_branch)) updated.revision = null; 391 392 if (!fieldListed(fields, ' ', "description")) return updated; 393 var base_description: []const u8 = ""; 394 if (try entry.getCursor(hash.hashInt(repo_opts.hash, evt.base_record_key))) |base_cursor| { 395 const base = try evt.read(Record, DB, repo_opts.hash, arena, try DB.HashMap(.read_only).init(base_cursor)); 396 base_description = base.event.description; 397 } 398 const chunks = try diff3.chunks(io, allocator, arena, base_description, live.event.description, theirs.event.description); 399 var resolutions: std.ArrayList([]const u8) = .empty; 400 var hunk_index: usize = 0; 401 for (chunks) |chunk| { 402 if (chunk != .conflict) continue; 403 try resolutions.append(arena.allocator(), if (hunk_index < resolve.hunks.len) resolve.hunks[hunk_index] else (chunk.conflict.ours orelse "")); 404 hunk_index += 1; 405 } 406 updated.description = try diff3.assemble(arena.allocator(), chunks, resolutions.items); 407 return updated; 408 } 409 410 fn fieldListed(fields: []const u8, delimiter: u8, name: []const u8) bool { 411 var iter = std.mem.splitScalar(u8, fields, delimiter); 412 while (iter.next()) |field| { 413 if (std.mem.eql(u8, field, name)) return true; 414 } 415 return false; 416 } 417 418 fn readFromRepo( 419 comptime repo_kind: rp.RepoKind, 420 comptime repo_opts: rp.RepoOpts(repo_kind), 421 io: std.Io, 422 allocator: std.mem.Allocator, 423 arena: *std.heap.ArenaAllocator, 424 repo: *rp.Repo(repo_kind, repo_opts), 425 id: *const [evt.event_id_size]u8, 426 ) !?Record { 427 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, allocator, repo.core.repo_dir) else null; 428 defer if (event_db_maybe) |*event_db| event_db.deinit(io, allocator); 429 const moment = (if (event_db_maybe) |*event_db| 430 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 431 else if (repo_kind == .git) 432 return null 433 else 434 evt.currentMoment(repo_opts, repo)) catch return null; 435 return readById(evt.EventDB(repo_opts.hash), repo_opts.hash, moment, arena, id); 436 } 437 438 fn revisionKey(allocator: std.mem.Allocator, target_branch: []const u8, oid: []const u8) ![]u8 { 439 return std.mem.concat(allocator, u8, &.{ target_branch, "\x00", oid }); 440 } 441 442 fn statusSet( 443 comptime DB: type, 444 statuses: DB.SortedMap(.read_write), 445 status: StatusKind, 446 ) !DB.SortedSet(.read_write) { 447 return DB.SortedSet(.read_write).init(try statuses.putCursor(@tagName(status))); 448 } 449 450 fn removeFromTagSets( 451 comptime DB: type, 452 tag_statuses: DB.SortedMap(.read_write), 453 tags: []const u8, 454 status: StatusKind, 455 order_key: []const u8, 456 ) !void { 457 var tag_iter = tagIterator(tags); 458 while (tag_iter.next()) |tag| { 459 if (tag.len == 0 or tag.len > tag_max_len) continue; 460 var key_buffer: TagStatusKey = undefined; 461 const key = try tagStatusKey(&key_buffer, tag, status); 462 const set = try DB.SortedSet(.read_write).init(try tag_statuses.putCursor(key)); 463 _ = try set.remove(order_key); 464 if (0 == try set.count()) _ = try tag_statuses.remove(key); 465 } 466 } 467 468 pub fn readById( 469 comptime DB: type, 470 comptime hash_kind: hash.HashKind, 471 haxy_moment: DB.HashMap(.read_only), 472 arena: *std.heap.ArenaAllocator, 473 id: *const [evt.event_id_size]u8, 474 ) !?Record { 475 const records_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, record_map_key)) orelse return null; 476 const records = try DB.HashMap(.read_only).init(records_cursor); 477 const record_cursor = try records.getCursor(hash.hashInt(hash_kind, id)) orelse return null; 478 return try evt.read(Record, DB, hash_kind, arena, try DB.HashMap(.read_only).init(record_cursor)); 479 } 480