| 1 | import { Inject, Injectable, Logger } from '@nestjs/common'; |
| 2 | import { type Database, |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { runGit, runGitStream |
| 7 | import { resolveCommit } |
| 8 | import { languageForPath } |
| 9 | import { excluded } |
| 10 | |
| 11 | const INSERT_CHUNK = 1_000 |
| 12 | |
| 13 | /** Symlinks and submodules are not files of the repository, so they hold no bytes. */ |
| 14 | const SKIPPED_MODES = new |
| 15 | |
| 16 | export interface LanguageBytes |
| 17 | language: string; |
| 18 | bytes: number; |
| 19 | } |
| 20 | |
| 21 | @Injectable() |
| 22 | export class RepositoryLanguageService |
| 23 | private readonly logger |
| 24 | private readonly inFlight |
| 25 | |
| 26 | constructor(@Inject( |
| 27 | |
| 28 | async getLanguages({ |
| 29 | repositoryId, |
| 30 | repoDirectory, |
| 31 | ref, |
| 32 | }: { |
| 33 | repositoryId: string |
| 34 | repoDirectory: string |
| 35 | ref: string |
| 36 | }): Promise |
| 37 | const key = |
| 38 | const pending = |
| 39 | if (pending) return |
| 40 | |
| 41 | const run = |
| 42 | this.inFlight.delete |
| 43 | ); |
| 44 | this.inFlight.set |
| 45 | return run; |
| 46 | } |
| 47 | |
| 48 | private async sync |
| 49 | repositoryId, |
| 50 | repoDirectory, |
| 51 | ref, |
| 52 | }: { |
| 53 | repositoryId: string |
| 54 | repoDirectory: string |
| 55 | ref: string |
| 56 | }): Promise |
| 57 | const tip = |
| 58 | if (!tip) |
| 59 | await this.forget |
| 60 | return []; |
| 61 | } |
| 62 | |
| 63 | const [state] |
| 64 | .select() |
| 65 | .from(schema.repositoryLanguageIndex |
| 66 | .where( |
| 67 | and( |
| 68 | eq(schema.repositoryLanguageIndex |
| 69 | eq(schema.repositoryLanguageIndex |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | if (state?.indexedCommitSha |
| 74 | |
| 75 | // Only a fast-forward can be topped up: a force push or a pruned object makes the stored totals unrelated to the three, so count it again |
| 76 | const incremental = |
| 77 | state !== undefined && |
| 78 | (await this.isAncestor |
| 79 | |
| 80 | const totals = |
| 81 | ? await this. |
| 82 | repoDirectory, |
| 83 | from: state!.indexedCommitSha |
| 84 | to: tip, |
| 85 | totals: await this |
| 86 | }) |
| 87 | : await this. |
| 88 | |
| 89 | await this.forget |
| 90 | await this.write |
| 91 | |
| 92 | this.logger.log |
| 93 | `Counted ${totals.size |
| 94 | incremental ? `${state |
| 95 | })`, |
| 96 | ); |
| 97 | |
| 98 | return sorted(totals |
| 99 | } |
| 100 | |
| 101 | private async countTree |
| 102 | const totals = |
| 103 | |
| 104 | const records = |
| 105 | runGitStream({ |
| 106 | args: ['ls-tree', |
| 107 | gitDir: repoDirectory, |
| 108 | }), |
| 109 | ); |
| 110 | |
| 111 | for await (const |
| 112 | // "<mode> <type> <oid> <size>\t<path>", size padded, "-" for non-blobs |
| 113 | const tab = |
| 114 | if (tab === |
| 115 | |
| 116 | const [mode, |
| 117 | if (type !== |
| 118 | |
| 119 | add(totals, record |
| 120 | } |
| 121 | |
| 122 | return totals; |
| 123 | } |
| 124 | |
| 125 | private async applyDiff |
| 126 | repoDirectory, |
| 127 | from, |
| 128 | to, |
| 129 | totals, |
| 130 | }: { |
| 131 | repoDirectory: string |
| 132 | from: string |
| 133 | to: string |
| 134 | totals: Map |
| 135 | }) { |
| 136 | const changes = |
| 137 | const sizes = |
| 138 | repoDirectory, |
| 139 | changes.flatMap(({ |
| 140 | [before?.oid, |
| 141 | ), |
| 142 | ); |
| 143 | |
| 144 | for (const { |
| 145 | if (before) add |
| 146 | if (after) add |
| 147 | } |
| 148 | |
| 149 | // a size git no longer agrees with would otherwise drift negative forever |
| 150 | for (const [language |
| 151 | if (bytes <= |
| 152 | } |
| 153 | |
| 154 | return totals; |
| 155 | } |
| 156 | |
| 157 | private async sizesOf |
| 158 | const sizes = |
| 159 | if (oids.length |
| 160 | |
| 161 | const output = |
| 162 | args: [ |
| 163 | 'cat-file', |
| 164 | '--batch-check=%(objectname) %(objecttype) %(objectsize)', |
| 165 | ], |
| 166 | gitDir: repoDirectory, |
| 167 | input: Buffer.from |
| 168 | }); |
| 169 | |
| 170 | for (const line |
| 171 | const [oid, |
| 172 | if (type === |
| 173 | } |
| 174 | |
| 175 | return sizes; |
| 176 | } |
| 177 | |
| 178 | private async read |
| 179 | return sorted(await |
| 180 | } |
| 181 | |
| 182 | private async readMap |
| 183 | const rows = |
| 184 | .select({ |
| 185 | language: schema.repositoryLanguageStat |
| 186 | bytes: schema.repositoryLanguageStat |
| 187 | }) |
| 188 | .from(schema.repositoryLanguageStat |
| 189 | .where( |
| 190 | and( |
| 191 | eq(schema.repositoryLanguageStat |
| 192 | eq(schema.repositoryLanguageStat |
| 193 | ), |
| 194 | ); |
| 195 | |
| 196 | return new Map |
| 197 | } |
| 198 | |
| 199 | private async write |
| 200 | repositoryId: string, |
| 201 | ref: string, |
| 202 | tip: string, |
| 203 | totals: Map |
| 204 | ) { |
| 205 | const rows = |
| 206 | .filter(([, bytes |
| 207 | .map(([language |
| 208 | |
| 209 | for (let i |
| 210 | await this.db |
| 211 | .insert(schema.repositoryLanguageStat |
| 212 | .values(rows.slice |
| 213 | .onConflictDoUpdate({ |
| 214 | target: [ |
| 215 | schema.repositoryLanguageStat.repositoryId |
| 216 | schema.repositoryLanguageStat.ref |
| 217 | schema.repositoryLanguageStat.language |
| 218 | ], |
| 219 | set: { bytes: excluded |
| 220 | }); |
| 221 | } |
| 222 | |
| 223 | await this.db |
| 224 | .insert(schema.repositoryLanguageIndex |
| 225 | .values({ repositoryId |
| 226 | .onConflictDoUpdate({ |
| 227 | target: [ |
| 228 | schema.repositoryLanguageIndex.repositoryId |
| 229 | schema.repositoryLanguageIndex.ref |
| 230 | ], |
| 231 | set: { indexedCommitSha: tip |
| 232 | }); |
| 233 | } |
| 234 | |
| 235 | private async forget |
| 236 | await this.db |
| 237 | .delete(schema.repositoryLanguageStat |
| 238 | .where( |
| 239 | and( |
| 240 | eq(schema.repositoryLanguageStat |
| 241 | eq(schema.repositoryLanguageStat |
| 242 | ), |
| 243 | ); |
| 244 | await this.db |
| 245 | .delete(schema.repositoryLanguageIndex |
| 246 | .where( |
| 247 | and( |
| 248 | eq(schema.repositoryLanguageIndex |
| 249 | eq(schema.repositoryLanguageIndex |
| 250 | ), |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | private async isAncestor |
| 255 | repoDirectory: string, |
| 256 | ancestor: string, |
| 257 | descendant: string, |
| 258 | ) { |
| 259 | return runGit({ |
| 260 | args: ['merge-base', |
| 261 | gitDir: repoDirectory, |
| 262 | }).then( |
| 263 | () => true, |
| 264 | () => false, |
| 265 | ); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | interface Side { |
| 270 | path: string; |
| 271 | oid: string; |
| 272 | } |
| 273 | |
| 274 | export interface RawChange |
| 275 | before?: Side |
| 276 | after?: Side |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * `git diff --raw -r -z` as sides to subtract and add. |
| 281 | * |
| 282 | * Each entry is ":<srcmode> <dstmode> <srcoid> <dstoid> <status>" followed by one path, or two for a rename or copy. Symlinks and submodules are dropped per side, so a file becoming a symlink still subtracts its old bytes. |
| 283 | */ |
| 284 | export async function readRawDiff |
| 285 | gitDir: string, |
| 286 | from: string, |
| 287 | to: string, |
| 288 | ): Promise<RawChange |
| 289 | const raw = |
| 290 | args: [ |
| 291 | 'diff', |
| 292 | '--raw', |
| 293 | '-r', |
| 294 | '-z', |
| 295 | '--no-abbrev', // --no-abbrev keeps the oids full, which is what cat-file echoes back |
| 296 | '--end-of-options', |
| 297 | from, |
| 298 | to, |
| 299 | ], |
| 300 | gitDir, |
| 301 | }); |
| 302 | |
| 303 | const fields = |
| 304 | const changes: |
| 305 | |
| 306 | for (let i |
| 307 | if (!fields[i |
| 308 | |
| 309 | const [srcMode, |
| 310 | .slice(1) |
| 311 | .split(' ') |
| 312 | // a rename or a copy names the destination in a second path field |
| 313 | const renamed = |
| 314 | const srcPath = |
| 315 | const dstPath = |
| 316 | if (srcPath === |
| 317 | |
| 318 | changes.push({ |
| 319 | before: isCounted(srcMode |
| 320 | ? { path: srcPath |
| 321 | : undefined, |
| 322 | after: isCounted(dstMode |
| 323 | ? { path: dstPath |
| 324 | : undefined, |
| 325 | }); |
| 326 | } |
| 327 | |
| 328 | return changes; |
| 329 | } |
| 330 | |
| 331 | function isCounted(mode: |
| 332 | return !SKIPPED_MODES.has |
| 333 | } |
| 334 | |
| 335 | /** NUL-terminated records, streamed, so a large tree is never held whole. */ |
| 336 | async function* splitRecords |
| 337 | let carry = |
| 338 | |
| 339 | for await (const |
| 340 | carry += chunk; |
| 341 | const records = |
| 342 | carry = records. |
| 343 | for (const record |
| 344 | } |
| 345 | |
| 346 | if (carry) yield |
| 347 | } |
| 348 | |
| 349 | function add(totals: |
| 350 | const language = |
| 351 | if (!language || |
| 352 | totals.set(language |
| 353 | } |
| 354 | |
| 355 | function sorted(totals: |
| 356 | return [...totals |
| 357 | .filter(([, bytes |
| 358 | .map(([language |
| 359 | .sort((a, |
| 360 | } |