╭ find.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("event.zig"); 3 const xit = @import("xit"); 4 const rp = xit.repo; 5 const hash = xit.hash; 6 const rf = xit.ref; 7 const tr = xit.tree; 8 const obj = xit.object; 9 const fs = xit.fs; 10 const serve_common = @import("serve_common.zig"); 11 const progress = @import("./progress.zig"); 12 13 // the repo moment key holding one file map per branch tip. it sits outside the 14 // haxy moment because pushes change it and events never do. each entry is 15 // keyed by its branch name's hash: its value is the branch's file map, and its 16 // key slot holds the root tree oid that map covers rather than the name. 17 const index_key = "haxy/branch->file-name+path->oid"; 18 19 // what a search returns: the matching files and whether the cap cut the list 20 // short. 21 pub fn Results(comptime hash_kind: hash.HashKind) type { 22 return struct { 23 matches: []const Match, 24 capped: bool = false, 25 26 pub const Match = struct { 27 path: []const u8, 28 oid: [hash.byteLen(hash_kind)]u8, 29 }; 30 }; 31 } 32 33 // `branch`'s file index, or null when it has none or its entry is stale: one 34 // that doesn't cover `tree_oid`, the branch tip's root tree. 35 pub fn lookup( 36 comptime repo_opts: rp.RepoOpts(.xit), 37 moment: rp.Repo(.xit, repo_opts).DB.HashMap(.read_only), 38 branch: []const u8, 39 tree_oid: *const [hash.hexLen(repo_opts.hash)]u8, 40 ) !?rp.Repo(.xit, repo_opts).DB.SortedMap(.read_only) { 41 const DB = rp.Repo(.xit, repo_opts).DB; 42 const index_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, index_key)) orelse return null; 43 const index = try DB.HashMap(.read_only).init(index_cursor); 44 const pair = try index.getKeyValuePair(hash.hashInt(repo_opts.hash, branch)) orelse return null; 45 const entry_tree = try readTree(repo_opts, pair.key_cursor); 46 if (!std.mem.eql(u8, &entry_tree, tree_oid)) return null; 47 return try DB.SortedMap(.read_only).init(pair.value_cursor); 48 } 49 50 // the indexed files whose basename starts with `term`, ordered by basename then 51 // path. the separator between basename and path keeps a match inside the 52 // basename, so the walk stops at the first key that doesn't share the prefix. 53 pub fn search( 54 comptime repo_opts: rp.RepoOpts(.xit), 55 files: rp.Repo(.xit, repo_opts).DB.SortedMap(.read_only), 56 aa: std.mem.Allocator, 57 term: []const u8, 58 max: usize, 59 ) !Results(repo_opts.hash) { 60 const Match = Results(repo_opts.hash).Match; 61 const prefix = try aa.alloc(u8, term.len); 62 _ = std.ascii.lowerString(prefix, term); 63 64 var matches: std.ArrayList(Match) = .empty; 65 var iter = try files.iteratorFrom(prefix); 66 while (try iter.next()) |cursor| { 67 const pair = try cursor.readKeyValuePair(); 68 const key = try pair.key_cursor.readBytesAlloc(aa, null); 69 if (!std.mem.startsWith(u8, key, prefix)) break; 70 if (matches.items.len == max) return .{ .matches = matches.items, .capped = true }; 71 const separator = std.mem.indexOfScalar(u8, key, 0) orelse return error.InvalidFileIndex; 72 const value = try pair.value_cursor.readBytesAlloc(aa, null); 73 try matches.append(aa, .{ .path = key[separator + 1 ..], .oid = try parseOid(repo_opts.hash, value) }); 74 } 75 return .{ .matches = matches.items }; 76 } 77 78 // the indexed file at `path`, for a selected result the cap cut off. 79 pub fn get( 80 comptime repo_opts: rp.RepoOpts(.xit), 81 files: rp.Repo(.xit, repo_opts).DB.SortedMap(.read_only), 82 aa: std.mem.Allocator, 83 path: []const u8, 84 ) !?[hash.byteLen(repo_opts.hash)]u8 { 85 var key: std.ArrayList(u8) = .empty; 86 try fileKey(aa, &key, path); 87 const cursor = (try files.getCursor(key.items)) orelse return null; 88 return try parseOid(repo_opts.hash, try cursor.readBytesAlloc(aa, null)); 89 } 90 91 // peel an oid to the root tree it names, following commits and tags. 92 pub fn rootTreeOid( 93 comptime repo_opts: rp.RepoOpts(.xit), 94 state: rp.Repo(.xit, repo_opts).State(.read_only), 95 io: std.Io, 96 allocator: std.mem.Allocator, 97 oid: *const [hash.hexLen(repo_opts.hash)]u8, 98 ) ![hash.hexLen(repo_opts.hash)]u8 { 99 var current = oid.*; 100 while (true) { 101 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, &current); 102 defer object.deinit(); 103 switch (object.content) { 104 .tree => return current, 105 .commit => |commit| current = commit.tree, 106 .tag => |tag| current = tag.target, 107 .blob => return error.ObjectInvalid, 108 } 109 } 110 } 111 112 // the action a file index refresh records when it runs on its own 113 pub const undo_action = "haxy/find-index"; 114 115 // reconcile every branch tip's index, in a transaction of its own 116 pub fn refresh( 117 comptime repo_opts: rp.RepoOpts(.xit), 118 io: std.Io, 119 allocator: std.mem.Allocator, 120 repo: *rp.Repo(.xit, repo_opts), 121 ) !void { 122 const Repo = rp.Repo(.xit, repo_opts); 123 const DB = Repo.DB; 124 const Save = struct { 125 core: *Repo.Core, 126 io: std.Io, 127 allocator: std.mem.Allocator, 128 129 pub fn run(ctx: @This(), cursor: *DB.Cursor(.read_write)) !void { 130 var moment = try DB.HashMap(.read_write).init(cursor.*); 131 const state = Repo.State(.read_write){ .core = ctx.core, .extra = .{ .moment = &moment } }; 132 if (!try refreshInTransaction(repo_opts, state, &moment, ctx.io, ctx.allocator, null)) return error.CancelTransaction; 133 134 // record the user action for undo 135 try xit.undo.write(repo_opts, state, std.Io.Timestamp.now(ctx.io, .real).toSeconds(), .{ .custom = .{ .action_kind = undo_action } }); 136 } 137 }; 138 139 // held across the reads and the writes, so a concurrent refresh can't 140 // change the entries these decisions were made from. 141 try repo.core.db_file.lock(io, .exclusive); 142 defer repo.core.db_file.unlock(io); 143 144 const history = try DB.ArrayList(.read_write).init(repo.core.db.rootCursor()); 145 history.appendContext(.{ .slot = try history.getSlot(-1) }, Save{ .core = &repo.core, .io = io, .allocator = allocator }) catch |err| switch (err) { 146 error.CancelTransaction => {}, 147 else => return err, 148 }; 149 } 150 151 // reconcile them inside the caller's transaction, so the index lands with the 152 // push that invalidated it. every branch is reconciled, since an up-to-date 153 // one costs a ref read and an oid compare. reports whether anything was 154 // written, so a caller that owns an otherwise empty transaction can cancel it 155 pub fn refreshInTransaction( 156 comptime repo_opts: rp.RepoOpts(.xit), 157 state: rp.Repo(.xit, repo_opts).State(.read_write), 158 moment: *rp.Repo(.xit, repo_opts).DB.HashMap(.read_write), 159 io: std.Io, 160 allocator: std.mem.Allocator, 161 progress_ctx_maybe: ?repo_opts.ProgressCtx, 162 ) !bool { 163 const Repo = rp.Repo(.xit, repo_opts); 164 const DB = Repo.DB; 165 const HashInt = hash.HashInt(repo_opts.hash); 166 const Tree = [hash.hexLen(repo_opts.hash)]u8; 167 168 // an entry in the index: the root tree it covers and its file map 169 const Entry = struct { 170 tree: Tree, 171 files: xit.xitdb.Slot, 172 173 fn init(pair: DB.KeyValuePairCursor(.read_only)) !@This() { 174 return .{ .tree = try readTree(repo_opts, pair.key_cursor), .files = pair.value_cursor.slot() }; 175 } 176 }; 177 // what one branch needs written 178 const Work = struct { 179 branch_hash: HashInt, 180 // the tip's root tree (null = the branch is gone, so its entry goes) 181 tree: ?Tree, 182 // the entry the branch has now 183 own: ?Entry, 184 }; 185 186 var arena = std.heap.ArenaAllocator.init(allocator); 187 defer arena.deinit(); 188 const aa = arena.allocator(); 189 190 var work: std.ArrayList(Work) = .empty; 191 var base: ?Entry = null; 192 193 { 194 const read_state = state.readOnly(); 195 196 // entries carry no branch name, so branches are matched by hash 197 var existing: std.AutoArrayHashMapUnmanaged(HashInt, Entry) = .empty; 198 if (try moment.getCursor(hash.hashInt(repo_opts.hash, index_key))) |index_cursor| { 199 const index = try DB.HashMap(.read_only).init(index_cursor); 200 var iter = try index.iterator(); 201 while (try iter.next()) |cursor| { 202 const pair = try cursor.readKeyValuePair(); 203 try existing.put(aa, pair.hash, try Entry.init(pair)); 204 } 205 } 206 207 // a brand-new branch copies the default branch's map when there is one, 208 // else any other branch's, so only its own changes are written. 209 if (existing.count() > 0) base = existing.values()[0]; 210 var head_buffer: [rf.MAX_REF_CONTENT_SIZE]u8 = undefined; 211 if (rf.readHead(.xit, repo_opts, read_state, io, &head_buffer) catch null) |head| switch (head) { 212 .ref => |ref| if (existing.get(hash.hashInt(repo_opts.hash, ref.name))) |entry| { 213 base = entry; 214 }, 215 .oid => {}, 216 }; 217 218 var branches = try rf.RefIterator(.xit, repo_opts).init(read_state, io, allocator, .head, .beginning); 219 defer branches.deinit(); 220 while (try branches.next()) |ref| { 221 if (std.mem.eql(u8, ref.name, evt.events_ref.name)) continue; 222 const branch_hash = hash.hashInt(repo_opts.hash, ref.name); 223 const own = if (existing.fetchSwapRemove(branch_hash)) |kv| kv.value else null; 224 const oid = (try rf.readRecur(.xit, repo_opts, read_state, io, .{ .ref = .{ .kind = .head, .name = ref.name } })) orelse continue; 225 const tree = try rootTreeOid(repo_opts, read_state, io, allocator, &oid); 226 if (own) |entry| if (std.mem.eql(u8, &entry.tree, &tree)) continue; 227 try work.append(aa, .{ .branch_hash = branch_hash, .tree = tree, .own = own }); 228 } 229 230 // whatever is left belongs to a branch that no longer exists 231 for (existing.keys()) |branch_hash| try work.append(aa, .{ .branch_hash = branch_hash, .tree = null, .own = null }); 232 } 233 234 if (work.items.len == 0) return false; 235 236 { 237 const index_hash = hash.hashInt(repo_opts.hash, index_key); 238 var new_branch_base = base; 239 progress.report(repo_opts, io, progress_ctx_maybe, .{ .text = "Indexing files" }); 240 241 var key: std.ArrayList(u8) = .empty; 242 defer key.deinit(allocator); 243 244 for (work.items) |item| { 245 if (progress.cancelled(repo_opts, progress_ctx_maybe)) return error.ClientGone; 246 const index = try DB.HashMap(.read_write).init(try moment.putCursor(index_hash)); 247 const tree = item.tree orelse { 248 _ = try index.remove(item.branch_hash); 249 continue; 250 }; 251 // the entry this one starts from (null = index the whole tree) 252 const from = item.own orelse new_branch_base; 253 254 var tree_bytes: [hash.byteLen(repo_opts.hash)]u8 = undefined; 255 _ = try std.fmt.hexToBytes(&tree_bytes, &tree); 256 // putKey only fills an empty key slot, and this one changes 257 // whenever the branch moves 258 var tree_cursor = try index.putKeyCursor(item.branch_hash); 259 try tree_cursor.write(.{ .bytes = &tree_bytes }); 260 var files_cursor = try index.putCursor(item.branch_hash); 261 if (from) |entry| try files_cursor.write(.{ .slot = entry.files }); 262 const files = try DB.SortedMap(.read_write).init(files_cursor); 263 264 if (from) |entry| { 265 var tree_diff = tr.TreeDiff(.xit, repo_opts).init(allocator); 266 defer tree_diff.deinit(); 267 try tree_diff.compare(state.readOnly(), io, &entry.tree, &tree, null); 268 progress.report(repo_opts, io, progress_ctx_maybe, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = tree_diff.changes.count() } }); 269 for (tree_diff.changes.keys(), tree_diff.changes.values()) |path, change| { 270 defer progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .writing_patch }); 271 try fileKey(allocator, &key, path); 272 const oid = if (change.new) |*tree_entry| indexedOid(repo_opts.hash, tree_entry) else null; 273 if (oid) |bytes| { 274 try files.put(key.items, .{ .bytes = bytes }); 275 } else { 276 _ = try files.remove(key.items); 277 } 278 } 279 } else { 280 // a whole tree has no count until it is walked, so it reports 281 // the files it has indexed so far 282 progress.report(repo_opts, io, progress_ctx_maybe, .{ .start = .{ .kind = .writing_patch, .estimated_total_items = 0 } }); 283 var paths = std.heap.ArenaAllocator.init(allocator); 284 defer paths.deinit(); 285 try indexTree(repo_opts, state.readOnly(), io, allocator, paths.allocator(), files, &key, "", &tree, progress_ctx_maybe); 286 } 287 progress.report(repo_opts, io, progress_ctx_maybe, .{ .end = .writing_patch }); 288 289 // the first entry ever written becomes the base for the next 290 // new branch, so only one branch walks a whole tree. 291 if (new_branch_base == null) new_branch_base = .{ .tree = tree, .files = (try index.getSlot(item.branch_hash)) orelse unreachable }; 292 // a later entry copies an earlier one's slot, so everything 293 // written so far has to be shared rather than mutated 294 try state.core.db.freeze(); 295 } 296 } 297 return true; 298 } 299 300 // insert every file under `oid` (a tree), as tr.Tree.read walks one. 301 fn indexTree( 302 comptime repo_opts: rp.RepoOpts(.xit), 303 state: rp.Repo(.xit, repo_opts).State(.read_only), 304 io: std.Io, 305 allocator: std.mem.Allocator, 306 paths: std.mem.Allocator, 307 files: rp.Repo(.xit, repo_opts).DB.SortedMap(.read_write), 308 key: *std.ArrayList(u8), 309 prefix: []const u8, 310 oid: *const [hash.hexLen(repo_opts.hash)]u8, 311 progress_ctx_maybe: ?repo_opts.ProgressCtx, 312 ) !void { 313 var object = try obj.Object(.xit, repo_opts).init(state, io, allocator, oid); 314 defer object.deinit(); 315 const tree = switch (object.content) { 316 .tree => |tree| tree, 317 else => return, 318 }; 319 for (tree.entries.keys(), tree.entries.values()) |name, *tree_entry| { 320 if (progress.cancelled(repo_opts, progress_ctx_maybe)) return error.ClientGone; 321 const path = try fs.joinPath(paths, &.{ prefix, name }); 322 if (tree_entry.isTree()) { 323 try indexTree(repo_opts, state, io, allocator, paths, files, key, path, &std.fmt.bytesToHex(tree_entry.oid, .lower), progress_ctx_maybe); 324 } else if (indexedOid(repo_opts.hash, tree_entry)) |bytes| { 325 try fileKey(allocator, key, path); 326 try files.put(key.items, .{ .bytes = bytes }); 327 progress.report(repo_opts, io, progress_ctx_maybe, .{ .complete_one = .writing_patch }); 328 } 329 } 330 } 331 332 // the root tree an index entry covers, read from its key slot as hex. 333 fn readTree( 334 comptime repo_opts: rp.RepoOpts(.xit), 335 key_cursor: rp.Repo(.xit, repo_opts).DB.Cursor(.read_only), 336 ) ![hash.hexLen(repo_opts.hash)]u8 { 337 var tree: [hash.byteLen(repo_opts.hash)]u8 = undefined; 338 if ((try key_cursor.readBytes(&tree)).len != tree.len) return error.InvalidFileIndex; 339 return std.fmt.bytesToHex(tree, .lower); 340 } 341 342 // build a file's key: its lowercased basename, a separator, then its full path. 343 fn fileKey(allocator: std.mem.Allocator, key: *std.ArrayList(u8), path: []const u8) !void { 344 const slash = std.mem.lastIndexOfScalar(u8, path, '/'); 345 const base = if (slash) |i| path[i + 1 ..] else path; 346 key.clearRetainingCapacity(); 347 try key.ensureTotalCapacity(allocator, base.len + 1 + path.len); 348 for (base) |char| key.appendAssumeCapacity(std.ascii.toLower(char)); 349 key.appendAssumeCapacity(0); 350 key.appendSliceAssumeCapacity(path); 351 } 352 353 // the blob oid a tree entry is indexed under, or null for a submodule: its oid 354 // is a commit in another repo, not a file here. 355 fn indexedOid(comptime hash_kind: hash.HashKind, tree_entry: *const tr.TreeEntry(hash_kind)) ?[]const u8 { 356 if (tree_entry.mode.content.object_type == .gitlink) return null; 357 return &tree_entry.oid; 358 } 359 360 fn parseOid(comptime hash_kind: hash.HashKind, value: []const u8) ![hash.byteLen(hash_kind)]u8 { 361 const len = comptime hash.byteLen(hash_kind); 362 if (value.len != len) return error.InvalidFileIndex; 363 return value[0..len].*; 364 } 365