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