╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ search_commit.zig ────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
232 if (work.items.len == 0 and gone.items.len == 0) return false; 233 234 { 235 const index_hash = hash.hashInt(repo_opts.hash, index_key); 236 237 var message: std.ArrayList(u8) = .empty; 238 defer message.deinit(allocator); 239 240 for (work.items) |item| { 241 const index = try DB.HashMap(.read_write).init(try moment.putCursor(index_hash)); 242 const base = item.base orelse nearestVersion(repo_opts.hash, versions.items, default_tip, item.timestamp); 243 244 var oid_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 245 _ = try std.fmt.hexToBytes(&oid_bytes, &item.oid); 246 const oid_int = hash.bytesToInt(repo_opts.hash, &oid_bytes); 247 try index.putKey(oid_int, .{ .bytes = &oid_bytes }); 248 var version_cursor = try index.putCursor(oid_int); 249 if (base) |base_oid| { 250 const base_cursor = (try index.getCursor(try hash.hexToInt(repo_opts.hash, &base_oid))) orelse return error.InvalidCommitIndex; 251 try version_cursor.write(.{ .slot = base_cursor.slot() }); 252 } 253 const version = try DB.SortedMap(.read_write).init(version_cursor); 254 255 var diff_arena = std.heap.ArenaAllocator.init(allocator); 256 defer diff_arena.deinit(); 257 const diff = try commitDiff(repo_opts, state.readOnly(), io, allocator, diff_arena.allocator(), if (base) |*base_oid| base_oid else null, &item.oid, progress_ctx_maybe); 258 259 // the walk is done, so the messages left to read are the total 260 progress.report(repo_opts, io, progress_ctx_maybe, .{ .text = "Indexing commits" }); 261 progress.report(repo_opts, io, progress_ctx_maybe, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = diff.removed.len + diff.added.len } }); 262 for (diff.removed) |commit| { 263 if (progress.cancelled(repo_opts, progress_ctx_maybe)) return error.ClientGone; 264 defer progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .writing_patch }); 265 const key = try readPosting(repo_opts, state.readOnly(), io, allocator, &message, commit); 266 try srch.remove(DB, version, allocator, &key, message.items); 267 } 268 for (diff.added) |commit| { 269 if (progress.cancelled(repo_opts, progress_ctx_maybe)) return error.ClientGone; 270 defer progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .writing_patch }); 271 const key = try readPosting(repo_opts, state.readOnly(), io, allocator, &message, commit); 272 try srch.add(DB, version, allocator, &key, message.items); 273 } 274 progress.report(repo_opts, io, progress_ctx_maybe, .{ .end = .writing_patch }); 275 276 try versions.append(aa, .{ .oid = item.oid, .timestamp = item.timestamp }); 277 // the next version copies this one's slot, so everything 278 // written so far has to be shared rather than mutated 279 try state.core.db.freeze(); 280 } 281 282 // removing last keeps a tip that is going away available as a base 283 const index = try DB.HashMap(.read_write).init(try moment.putCursor(index_hash)); 284 for (gone.items) |oid| _ = try index.remove(try hash.hexToInt(repo_opts.hash, &oid)); 285 } 286 return true; 287 } 288 289 // the version a tip committed at `timestamp` derives from when no push names 290 // one: the default branch's unless it is newer than the tip, else the nearest 291 // older one, else the oldest. a guess by dates that only the cost rides on. 292 fn nearestVersion( 293 comptime hash_kind: hash.HashKind, 294 versions: []const Commit(hash_kind), 295 default_tip: ?[hash.hexLen(hash_kind)]u8, 296 timestamp: u64, 297 ) ?[hash.hexLen(hash_kind)]u8 { 298 var older: ?Commit(hash_kind) = null; 299 var oldest: ?Commit(hash_kind) = null; 300 for (versions) |version| { 301 if (version.timestamp <= timestamp) { 302 if (default_tip) |oid| if (std.mem.eql(u8, &oid, &version.oid)) return oid; 303 const nearer = if (older) |found| version.timestamp > found.timestamp else true; 304 if (nearer) older = version; 305 } 306 const earlier = if (oldest) |found| version.timestamp < found.timestamp else true; 307 if (earlier) oldest = version; 308 } 309 return if (older orelse oldest) |version| version.oid else null; 310 } 311 312 // the commit this push moved a ref to `tip` from, when it has a version to 313 // derive from. 314 fn pushedFrom( 315 comptime repo_opts: rp.RepoOpts(.xit), 316 updates: ?[]const xit.net_server_receive_pack.AppliedRefUpdate, 317 tip: *const [hash.hexLen(repo_opts.hash)]u8, 318 existing: std.AutoArrayHashMapUnmanaged([hash.hexLen(repo_opts.hash)]u8, void), 319 ) ?[hash.hexLen(repo_opts.hash)]u8 { 320 for (updates orelse &.{}) |update| { 321 if (!std.mem.eql(u8, update.new_oid, tip)) continue; 322 if (update.old_oid.len != hash.hexLen(repo_opts.hash)) continue; 323 var oid: [hash.hexLen(repo_opts.hash)]u8 = undefined; 324 @memcpy(&oid, update.old_oid); 325 if (existing.contains(oid)) return oid; 326 } 327 return null; 328 } 329 330 // read `commit`'s message into `message` and return the doc key it indexes 331 // under. 332 fn readPosting( 333 comptime repo_opts: rp.RepoOpts(.xit), 334 state: rp.Repo(.xit, repo_opts).State(.read_only), 335 io: std.Io, 336 allocator: std.mem.Allocator, 337 message: *std.ArrayList(u8), 338 commit: Commit(repo_opts.hash), 339 ) ![docKeyLen(repo_opts.hash)]u8 { 340 message.clearRetainingCapacity(); 341 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &commit.oid); 342 defer object.deinit(); 343 object.readMessage(allocator, message, .limited(srch.max_indexed_bytes)) catch |err| switch (err) { 344 // only the indexed prefix matters 345 error.StreamTooLong => {}, 346 else => |e| return e, 347 }; 348 return docKey(repo_opts.hash, commit.timestamp, &commit.oid); 349 } 350 351 // what `tip` reaches that `base_tip` doesn't, and the other way round, built 352 // into `aa`. with no base the whole history is added. 353 fn commitDiff( 354 comptime repo_opts: rp.RepoOpts(.xit), 355 state: rp.Repo(.xit, repo_opts).State(.read_only), 356 io: std.Io, 357 allocator: std.mem.Allocator, 358 aa: std.mem.Allocator, 359 base_tip: ?*const [hash.hexLen(repo_opts.hash)]u8, 360 tip: *const [hash.hexLen(repo_opts.hash)]u8, 361 progress_ctx_maybe: ?repo_opts.ProgressCtx, 362 ) !struct { removed: []const Commit(repo_opts.hash), added: []const Commit(repo_opts.hash) } { 363 var removed: std.ArrayList(Commit(repo_opts.hash)) = .empty; 364 var added: std.ArrayList(Commit(repo_opts.hash)) = .empty; 365 366 if (base_tip) |base| { 367 const Ancestry = mrg.Ancestry(.xit, repo_opts); 368 var ancestry = try Ancestry.init(state, io, allocator, base, tip); 369 defer ancestry.deinit(); 370 371 // the walk is bracketed here because it may run twice 372 progress.report(repo_opts, io, progress_ctx_maybe, .{ .start = .{ .kind = .walking_commit, .estimated_total_items = 0 } }); 373 defer progress.report(repo_opts, io, progress_ctx_maybe, .{ .end = .walking_commit }); 374 try ancestry.finish(progress_ctx_maybe); 375 376 // timestamps only choose the walk's order, so a commit the base reached 377 // can be passed before the tip turns out to reach it too. that can only 378 // make a removal wrong, never an addition. a fast-forward removes 379 // nothing, which the base tip picking up the tip's flag proves. anything 380 // else is settled by walking until nothing is left to carry down. 381 const base_node = ancestry.nodes.get(ancestry.tips[0]) orelse unreachable; 382 const fast_forward = base_node.flags & Ancestry.two != 0; 383 if (!fast_forward) { 384 var candidates = false; 385 var flagged = ancestry.nodes.valueIterator(); 386 while (flagged.next()) |node| { 387 if (node.flags & Ancestry.both == Ancestry.one) candidates = true; 388 } 389 if (candidates) while (try ancestry.step()) { 390 progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .walking_commit }); 391 }; 392 } 393 394 var iter = ancestry.nodes.iterator(); 395 while (iter.next()) |entry| { 396 // tag objects are peeled through, not walked 397 if (entry.value_ptr.tag_target != null) continue; 398 const commit = Commit(repo_opts.hash){ .oid = entry.key_ptr.*, .timestamp = entry.value_ptr.timestamp }; 399 switch (entry.value_ptr.flags & Ancestry.both) { 400 Ancestry.one => if (!fast_forward) try removed.append(aa, commit), 401 Ancestry.two => try added.append(aa, commit), 402 else => {}, 403 } 404 } 405 } else { 406 // a whole history has no count until it is walked, so the walk reports 407 // the commits it has reached so far 408 progress.report(repo_opts, io, progress_ctx_maybe, .{ .start = .{ .kind = .walking_commit, .estimated_total_items = 0 } }); 409 var iter = try obj.ObjectIterator(.xit, repo_opts).init(state, io, allocator, .{ .kind = .commit }); 410 defer iter.deinit(); 411 try iter.include(tip); 412 while (try iter.next(allocator)) |object| { 413 defer object.deinit(); 414 try added.append(aa, .{ .oid = object.oid, .timestamp = object.content.commit.metadata.timestamp }); 415 progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .walking_commit }); 416 } 417 progress.report(repo_opts, io, progress_ctx_maybe, .{ .end = .walking_commit }); 418 } 419 420 return .{ .removed = removed.items, .added = added.items }; 421 } 422