| 1 | import { Inject, Injectable, Logger } from '@nestjs/common'; |
| 2 | import { type Database, |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { runGit } |
| 7 | import { walkCommits } |
| 8 | |
| 9 | /** Rows buffered before a flush. Keeps a full rebuild's memory bounded. */ |
| 10 | const FLUSH_THRESHOLD = 5_000 |
| 11 | /** Postgres caps a statement at 65535 bind parameters; this table binds six. */ |
| 12 | const INSERT_CHUNK = 1_000 |
| 13 | |
| 14 | export interface PathCommit |
| 15 | commitSha: string; |
| 16 | committedAt: Date |
| 17 | subject: string; |
| 18 | } |
| 19 | |
| 20 | interface PendingRow extends |
| 21 | path: string; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Keeps `repository_path_commit` in step with a ref, so listing a directory |
| 26 | * never has to walk history per entry. |
| 27 | * |
| 28 | * The index is a cache of git, not a second source of truth: it is rebuilt from |
| 29 | * the object database whenever the stored position stops making sense. |
| 30 | */ |
| 31 | @Injectable() |
| 32 | export class RepositoryPathIndexService |
| 33 | private readonly logger |
| 34 | private readonly inFlight |
| 35 | |
| 36 | constructor(@Inject( |
| 37 | |
| 38 | /** |
| 39 | * Brings the index up to the ref's tip, returning that tip (null when the ref |
| 40 | * does not exist). Concurrent callers share one walk. |
| 41 | */ |
| 42 | async sync({ |
| 43 | repositoryId, |
| 44 | repoDirectory, |
| 45 | ref, |
| 46 | }: { |
| 47 | repositoryId: string |
| 48 | repoDirectory: string |
| 49 | ref: string |
| 50 | }): Promise |
| 51 | const key = |
| 52 | const pending = |
| 53 | if (pending) return |
| 54 | |
| 55 | const run = |
| 56 | this.inFlight.delete |
| 57 | ); |
| 58 | this.inFlight.set |
| 59 | return run; |
| 60 | } |
| 61 | |
| 62 | /** Latest commit per path, for the paths a listing actually shows. */ |
| 63 | async lookup({ |
| 64 | repositoryId, |
| 65 | ref, |
| 66 | paths, |
| 67 | }: { |
| 68 | repositoryId: string |
| 69 | ref: string |
| 70 | paths: string |
| 71 | }): Promise |
| 72 | if (paths.length |
| 73 | |
| 74 | const rows = |
| 75 | .select({ |
| 76 | path: schema.repositoryPathCommit |
| 77 | commitSha: schema.repositoryPathCommit |
| 78 | committedAt: schema.repositoryPathCommit |
| 79 | subject: schema.repositoryPathCommit |
| 80 | }) |
| 81 | .from(schema.repositoryPathCommit |
| 82 | .where( |
| 83 | and( |
| 84 | eq(schema.repositoryPathCommit |
| 85 | eq(schema.repositoryPathCommit |
| 86 | inArray(schema.repositoryPathCommit |
| 87 | ), |
| 88 | ); |
| 89 | |
| 90 | return new Map |
| 91 | } |
| 92 | |
| 93 | private async reindex |
| 94 | repositoryId, |
| 95 | repoDirectory, |
| 96 | ref, |
| 97 | }: { |
| 98 | repositoryId: string |
| 99 | repoDirectory: string |
| 100 | ref: string |
| 101 | }): Promise |
| 102 | const tip = |
| 103 | if (!tip) |
| 104 | await this.forget |
| 105 | return null; |
| 106 | } |
| 107 | |
| 108 | const [state] |
| 109 | .select() |
| 110 | .from(schema.repositoryRefIndex |
| 111 | .where( |
| 112 | and( |
| 113 | eq(schema.repositoryRefIndex |
| 114 | eq(schema.repositoryRefIndex |
| 115 | ), |
| 116 | ); |
| 117 | |
| 118 | if (state?.indexedCommitSha |
| 119 | |
| 120 | // Only a fast-forward can be topped up. A force push or a pruned object |
| 121 | // makes the stored rows unrelated to the ref, so start over. |
| 122 | const incremental = |
| 123 | state !== undefined && |
| 124 | (await this.isAncestor |
| 125 | |
| 126 | if (!incremental) |
| 127 | |
| 128 | const range = |
| 129 | const written = |
| 130 | repositoryId, |
| 131 | repoDirectory, |
| 132 | ref, |
| 133 | range, |
| 134 | }); |
| 135 | |
| 136 | await this.db |
| 137 | .insert(schema.repositoryRefIndex |
| 138 | .values({ repositoryId |
| 139 | .onConflictDoUpdate({ |
| 140 | target: [ |
| 141 | schema.repositoryRefIndex.repositoryId |
| 142 | schema.repositoryRefIndex.ref |
| 143 | ], |
| 144 | set: { |
| 145 | indexedCommitSha: tip, |
| 146 | updatedAt: new Date |
| 147 | }, |
| 148 | }); |
| 149 | |
| 150 | this.logger.log |
| 151 | `Indexed ${written} paths for ${ |
| 152 | ); |
| 153 | |
| 154 | return tip; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Walks the range oldest-first, keeping the last commit seen per path. Every |
| 159 | * ancestor directory of a changed file is recorded too, so a directory row |
| 160 | * carries the newest commit anywhere beneath it. |
| 161 | */ |
| 162 | private async walk |
| 163 | repositoryId, |
| 164 | repoDirectory, |
| 165 | ref, |
| 166 | range, |
| 167 | }: { |
| 168 | repositoryId: string |
| 169 | repoDirectory: string |
| 170 | ref: string |
| 171 | range: string |
| 172 | }) { |
| 173 | const pending = |
| 174 | let written = |
| 175 | |
| 176 | for await (const |
| 177 | const touched: |
| 178 | commitSha: commit.sha |
| 179 | committedAt: commit.committedAt |
| 180 | subject: commit.subject |
| 181 | }; |
| 182 | |
| 183 | for (const path |
| 184 | for (const ancestor |
| 185 | pending.set(ancestor |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (pending.size |
| 190 | written += await |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | written += await |
| 195 | return written; |
| 196 | } |
| 197 | |
| 198 | private async flush |
| 199 | repositoryId: string, |
| 200 | ref: string, |
| 201 | pending: Map |
| 202 | ) { |
| 203 | const rows = |
| 204 | repositoryId, |
| 205 | ref, |
| 206 | ...row, |
| 207 | })); |
| 208 | pending.clear(); |
| 209 | |
| 210 | for (let i |
| 211 | await this.db |
| 212 | .insert(schema.repositoryPathCommit |
| 213 | .values(rows.slice |
| 214 | .onConflictDoUpdate({ |
| 215 | target: [ |
| 216 | schema.repositoryPathCommit.repositoryId |
| 217 | schema.repositoryPathCommit.ref |
| 218 | schema.repositoryPathCommit.path |
| 219 | ], |
| 220 | set: { |
| 221 | commitSha: sql`excluded."commitSha"` |
| 222 | committedAt: sql`excluded."committedAt"` |
| 223 | subject: sql`excluded."subject"` |
| 224 | }, |
| 225 | }); |
| 226 | } |
| 227 | |
| 228 | return rows.length; |
| 229 | } |
| 230 | |
| 231 | private async forget |
| 232 | await this.db |
| 233 | .delete(schema.repositoryPathCommit |
| 234 | .where( |
| 235 | and( |
| 236 | eq(schema.repositoryPathCommit |
| 237 | eq(schema.repositoryPathCommit |
| 238 | ), |
| 239 | ); |
| 240 | await this.db |
| 241 | .delete(schema.repositoryRefIndex |
| 242 | .where( |
| 243 | and( |
| 244 | eq(schema.repositoryRefIndex |
| 245 | eq(schema.repositoryRefIndex |
| 246 | ), |
| 247 | ); |
| 248 | } |
| 249 | |
| 250 | private async resolve |
| 251 | const oid = |
| 252 | args: [ |
| 253 | 'rev-parse', |
| 254 | '--verify', |
| 255 | '--quiet', |
| 256 | '--end-of-options', |
| 257 | `${ref}^{commit}`, |
| 258 | ], |
| 259 | gitDir: repoDirectory, |
| 260 | }).catch(() |
| 261 | return oid.trim |
| 262 | } |
| 263 | |
| 264 | private async isAncestor |
| 265 | repoDirectory: string, |
| 266 | ancestor: string, |
| 267 | descendant: string, |
| 268 | ) { |
| 269 | // Exits non-zero both for "not an ancestor" and for an object that is gone; |
| 270 | // either way the stored position is unusable. |
| 271 | return runGit({ |
| 272 | args: ['merge-base', |
| 273 | gitDir: repoDirectory, |
| 274 | }).then( |
| 275 | () => true, |
| 276 | () => false, |
| 277 | ); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** "src/a/b.ts" -> ["src/a/b.ts", "src/a", "src", ""] */ |
| 282 | export function ancestorsOf(path |
| 283 | const segments = |
| 284 | const paths: |
| 285 | for (let i |
| 286 | paths.push(segments |
| 287 | } |
| 288 | paths.push('') |
| 289 | return paths; |
| 290 | } |