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