╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ patch.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
155 .author = input.author, 156 .event = .{ .patch = patch }, 157 }}); 158 return true; 159 } 160 161 // merge the selected patch revision into its target branch 162 pub fn merge( 163 comptime repo_opts: rp.RepoOpts(.xit), 164 io: std.Io, 165 allocator: std.mem.Allocator, 166 repo_root_path: []const u8, 167 target_repo: *rp.Repo(.xit, repo_opts), 168 input: MergeInput, 169 ) !void { 170 const patch_id = try evt.parseEventId(&input.id); 171 var arena = std.heap.ArenaAllocator.init(allocator); 172 defer arena.deinit(); 173 174 // validate the target patch 175 const target_moment = try evt.currentMoment(repo_opts, target_repo); 176 const patch_record = (try evt.Patch.readById(evt.EventDB(repo_opts.hash), repo_opts.hash, target_moment, &arena, &patch_id)) orelse return error.InvalidPatch; 177 if (patch_record.removed) return error.InvalidPatch; 178 switch (patch_record.event.status) { 179 .open => {}, 180 .closed => return error.PatchClosed, 181 .merged => return error.PatchAlreadyMerged, 182 } 183 const selected = patch_record.event.revision orelse return error.PatchNotPushed; 184 const revision_id = try evt.parseEventId(&selected.id); 185 const target_ref = rf.Ref.initFromPath(selected.target_ref, null) orelse return error.InvalidPatch; 186 switch (target_ref.kind) { 187 .head => {}, 188 else => return error.InvalidPatch, 189 } 190 191 // open and lock the fork 192 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &input.id); 193 var fork_repo = rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }) catch return error.PatchDataUnavailable; 194 defer fork_repo.deinit(io, allocator); 195 try fork_repo.core.db_file.lock(io, .shared); 196 defer fork_repo.core.db_file.unlock(io); 197 198 // require the fork's newest revision 199 { 200 const fork_moment = try evt.currentMoment(repo_opts, &fork_repo); 201 const newest = (try evt.PatchRev.readNewest(evt.EventDB(repo_opts.hash), repo_opts.hash, fork_moment, &arena)) orelse return error.PatchDataUnavailable; 202 if (!std.mem.eql(u8, &newest.id, &revision_id) or 203 !selected.matches(newest.record)) return error.PatchOutOfDate; 204 } 205 206 // select the commit and merge identity 207 const merge_oid = blk: { 208 const selected_oid = switch (input.revision) { 209 .squash => selected.squash_oid, 210 .source => selected.source_oid, 211 }; 212 var oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 213 if (selected_oid.len != oid.len) return error.InvalidPatch; 214 @memcpy(&oid, selected_oid); 215 break :blk oid; 216 }; 217 const identity = try std.fmt.allocPrint(arena.allocator(), "{s} <{s}>", .{ input.author.name, input.author.email }); 218 219 // merge the code and events in one target transaction 220 { 221 const DB = rp.Repo(.xit, repo_opts).DB; 222 const State = rp.Repo(.xit, repo_opts).State; 223 const Ctx = struct { 224 core: *rp.Repo(.xit, repo_opts).Core, 225 fork_repo: *rp.Repo(.xit, repo_opts), 226 io: std.Io, 227 allocator: std.mem.Allocator, 228 patch_id: [evt.event_id_size]u8, 229 expected_patch: evt.Patch, 230 target_ref: rf.Ref, 231 merge_oid: [hash.hexLen(repo_opts.hash)]u8, 232 identity: []const u8, 233 author: evt.CommitAuthor, 234 timestamp: u64, 235 236 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 237 var moment = try DB.HashMap(.read_write).init(cursor.*); 238 const state = State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 239 240 // copy the selected commit and its dependencies 241 { 242 var fork_repo_moment = try ctx.fork_repo.core.latestMoment(); 243 const fork_state = State(.read_only){ .core = &ctx.fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 244 var objects = try obj.ObjectIterator(.xit, repo_opts).init(fork_state, ctx.io, ctx.allocator, .{ .kind = .all }); 245 defer objects.deinit(); 246 try objects.include(&ctx.merge_oid); 247 try obj.copyFromObjectIterator(.xit, repo_opts, state, .xit, repo_opts, &objects, ctx.io, null); 248 } 249 250 // import the revision and mark the patch merged 251 if (!try importMergedFromFork(repo_opts, state, &ctx.core.db, &moment, ctx.io, ctx.allocator, ctx.fork_repo, &ctx.patch_id, ctx.expected_patch, ctx.author, ctx.timestamp)) return error.PatchOutOfDate; 252 253 // merge the selected commit 254 { 255 var merge_result = try mrg.Merge(.xit, repo_opts).init(state, ctx.io, ctx.allocator, .{ 256 .kind = .full, 257 .action = .{ .new = .{ .source = &.{.{ .oid = &ctx.merge_oid }}, .algo = .diff3 } }, 258 .commit_metadata = .{ 259 .author = ctx.identity, 260 .committer = ctx.identity, 261 .message = ctx.expected_patch.title, 262 .timestamp = ctx.timestamp, 263 }, 264 }, ctx.target_ref, null); 265 defer merge_result.deinit(); 266 switch (merge_result.result) { 267 .conflict => return error.MergeConflict, 268 .success, .nothing, .fast_forward => {}, 269 } 270 } 271 272 try xit.undo.writeMessage(repo_opts, state, .{ .custom = "merge patch" }); 273 } 274 }; 275 276 try target_repo.core.db_file.lock(io, .exclusive); 277 defer target_repo.core.db_file.unlock(io); 278 279 const history = try DB.ArrayList(.read_write).init(target_repo.core.db.rootCursor()); 280 try history.appendContext(.{ .slot = try history.getSlot(-1) }, Ctx{ 281 .core = &target_repo.core, 282 .fork_repo = &fork_repo, 283 .io = io, 284 .allocator = allocator, 285 .patch_id = patch_id, 286 .expected_patch = patch_record.event, 287 .target_ref = target_ref, 288 .merge_oid = merge_oid, 289 .identity = identity, 290 .author = input.author, 291 .timestamp = input.timestamp, 292 }); 293 } 294 } 295 296 fn commitAuthor(line: []const u8) !evt.CommitAuthor { 297 const open = std.mem.lastIndexOfScalar(u8, line, '<') orelse return error.AuthorNotFound; 298 const close = std.mem.indexOfScalarPos(u8, line, open + 1, '>') orelse return error.AuthorNotFound; 299 const name = std.mem.trimEnd(u8, line[0..open], " "); 300 if (name.len == 0 or close == open + 1) return error.AuthorNotFound; 301 return .{ .name = name, .email = line[open + 1 .. close] }; 302 } 303 304 // import a revision after detecting a client-side merge 305 fn importMerged( 306 comptime repo_opts: rp.RepoOpts(.xit), 307 state: rp.Repo(.xit, repo_opts).State(.read_write), 308 db: *evt.EventDB(repo_opts.hash), 309 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 310 io: std.Io, 311 allocator: std.mem.Allocator, 312 repo_root_path: []const u8, 313 patch_id: *const [evt.event_id_size]u8, 314 ) !void { 315 var arena = std.heap.ArenaAllocator.init(allocator); 316 defer arena.deinit(); 317 318 const patch_hex = std.fmt.bytesToHex(patch_id.*, .lower); 319 const fork_path = try fork.forkPath(arena.allocator(), repo_root_path, &patch_hex); 320 var fork_repo = try rp.Repo(.xit, repo_opts).open(io, allocator, .{ .path = fork_path, .require_repo_root = true }); 321 defer fork_repo.deinit(io, allocator); 322 323 _ = try importMergedFromFork(repo_opts, state, db, moment, io, allocator, &fork_repo, patch_id, null, null, 0); 324 } 325 326 // import the revision and mark its patch merged in the current transaction 327 fn importMergedFromFork( 328 comptime repo_opts: rp.RepoOpts(.xit), 329 state: rp.Repo(.xit, repo_opts).State(.read_write), 330 db: *evt.EventDB(repo_opts.hash), 331 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 332 io: std.Io, 333 allocator: std.mem.Allocator, 334 fork_repo: *rp.Repo(.xit, repo_opts), 335 patch_id: *const [evt.event_id_size]u8, 336 expected_patch: ?evt.Patch, 337 merged_author: ?evt.CommitAuthor, 338 merged_timestamp: u64, 339 ) !bool { 340 const DB = evt.EventDB(repo_opts.hash); 341 var arena = std.heap.ArenaAllocator.init(allocator); 342 defer arena.deinit(); 343 344 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 345 const patch_record = (try evt.Patch.readById(DB, repo_opts.hash, haxy_moment, &arena, patch_id)) orelse return false; 346 const selected = patch_record.event.revision orelse return false; 347 if (patch_record.removed or patch_record.event.status == .merged) return false; 348 if (expected_patch) |expected| { 349 if (!evt.fieldEqual(evt.Patch, expected, patch_record.event)) return false; 350 } 351 const revision_id = try evt.parseEventId(&selected.id); 352 353 const fork_moment = try evt.currentMoment(repo_opts, fork_repo); 354 const revision = (try evt.PatchRev.readById(DB, repo_opts.hash, fork_moment, &arena, &revision_id)) orelse return false; 355 if (!selected.matches(revision)) return false; 356 357 var fork_repo_moment = try fork_repo.core.latestMoment(); 358 const fork_state = rp.Repo(.xit, repo_opts).State(.read_only){ .core = &fork_repo.core, .extra = .{ .moment = &fork_repo_moment } }; 359 var event_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 360 if (revision.event_oid.len != event_oid.len) return false; 361 @memcpy(&event_oid, revision.event_oid); 362 var event_object = try obj.Object(.xit, repo_opts).initCommit(fork_state, io, allocator, &event_oid); 363 defer event_object.deinit(); 364 const original_author = try commitAuthor(event_object.content.commit.metadata.author orelse return false); 365 366 var events: [2]evt.EventWithId = undefined; 367 var event_count: usize = 0; 368 const current_revision = try evt.PatchRev.readById(DB, repo_opts.hash, haxy_moment, &arena, &revision_id); 369 var tree_entries: [2]evt.EventTreeEntry = undefined; 370 const needs_import = if (current_revision) |existing| existing.removed else true; 371 if (needs_import) { 372 tree_entries = .{ 373 .{ .tree = .{ .name = "base", .oid = revision.base_tree_oid } }, 374 .{ .tree = .{ .name = "head", .oid = revision.head_tree_oid } }, 375 }; 376 events[event_count] = .{ 377 .id = selected.id, 378 .timestamp = event_object.content.commit.metadata.timestamp, 379 .author = original_author, 380 .tree_entries = &tree_entries, 381 .event = .{ .patchrev = revision.event }, 382 }; 383 event_count += 1; 384 } else { 385 const existing = current_revision orelse unreachable; 386 if (!selected.matches(existing)) return error.InvalidPatch; 387 } 388 389 var patch = patch_record.event; 390 patch.status = .merged; 391 events[event_count] = .{ 392 .id = std.fmt.bytesToHex(patch_id.*, .lower), 393 .timestamp = if (merged_author != null) merged_timestamp else @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 394 .author = merged_author orelse original_author, 395 .event = .{ .patch = patch }, 396 }; 397 event_count += 1; 398 399 try evt.commitEvents(.xit, repo_opts, state, io, allocator, evt.events_ref, events[0..event_count], null); 400 if (!try evt.consumeInTransaction(.repo, .xit, repo_opts, state, db, moment, io, allocator, evt.events_ref)) return error.CancelTransaction; 401 return true; 402 } 403 404 // find patches merged by commits received in a push 405 pub fn detectMerged( 406 comptime repo_opts: rp.RepoOpts(.xit), 407 state: rp.Repo(.xit, repo_opts).State(.read_write), 408 db: *evt.EventDB(repo_opts.hash), 409 moment: *evt.EventDB(repo_opts.hash).HashMap(.read_write), 410 io: std.Io, 411 allocator: std.mem.Allocator, 412 repo_root_path: []const u8, 413 updates: []const xit.net_server_receive_pack.AppliedRefUpdate, 414 error_writer: *std.Io.Writer, 415 ) !void { 416 const DB = evt.EventDB(repo_opts.hash); 417 var arena = std.heap.ArenaAllocator.init(allocator); 418 defer arena.deinit(); 419 420 const haxy_moment = try evt.currentMomentFromRepoMoment(repo_opts.hash, moment.readOnly()); 421 const revisions_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.Patch.revision_to_id_set_key)) orelse return; 422 const revisions = try DB.HashMap(.read_only).init(revisions_cursor); 423 var events_ref_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 424 const events_ref_path = try evt.events_ref.toPath(&events_ref_buffer); 425 for (updates) |update| { 426 if (!std.mem.startsWith(u8, update.ref_name, "refs/heads/") or 427 std.mem.eql(u8, update.ref_name, events_ref_path) or 428 std.mem.eql(u8, update.old_oid, update.new_oid)) continue; 429 if (update.old_oid.len != hash.hexLen(repo_opts.hash) or 430 update.new_oid.len != hash.hexLen(repo_opts.hash)) return error.InvalidOid; 431 var old_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 432 var new_oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 433 @memcpy(&old_oid, update.old_oid); 434 @memcpy(&new_oid, update.new_oid); 435 if (std.mem.allEqual(u8, &new_oid, '0')) continue; 436 437 var commits = try obj.ObjectIterator(.xit, repo_opts).init(state.readOnly(), io, arena.allocator(), .{ .kind = .commit }); 438 defer commits.deinit(); 439 if (!std.mem.allEqual(u8, &old_oid, '0')) try commits.exclude(&old_oid); 440 try commits.include(&new_oid); 441 var revision_key_buffer: [rf.MAX_REF_CONTENT_SIZE + 1 + hash.hexLen(repo_opts.hash)]u8 = undefined; 442 while (try commits.next(arena.allocator())) |commit| { 443 defer commit.deinit(); 444 const key = try std.fmt.bufPrint(&revision_key_buffer, "{s}\x00{s}", .{ update.ref_name, &commit.oid }); 445 const ids_cursor = try revisions.getCursor(hash.hashInt(repo_opts.hash, key)) orelse continue; 446 const ids = try DB.CountedHashSet(.read_only).init(ids_cursor); 447 var ids_iter = try ids.iterator(); 448 while (try ids_iter.next()) |cursor| { 449 const pair = try cursor.readKeyValuePair(); 450 var patch_id: [evt.event_id_size]u8 = undefined; 451 if ((try pair.key_cursor.readBytes(&patch_id)).len != patch_id.len) return error.InvalidPatch; 452 const patch_hex = std.fmt.bytesToHex(patch_id, .lower); 453 importMerged(repo_opts, state, db, moment, io, allocator, repo_root_path, &patch_id) catch |err| { 454 serve_common.logError(io, error_writer, "failed to mark patch {s} as merged: {s}\n", .{ &patch_hex, @errorName(err) }); 455 continue; 456 }; 457 } 458 } 459 } 460 } 461