| 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 { type LinguistAttributes, |
| 9 | import { excluded } |
| 10 | import { isAncestor } |
| 11 | import { SingleFlight } |
| 12 | import { splitRecords } |
| 13 | import { |
| 14 | isGitAttributes, |
| 15 | readAttributes, |
| 16 | readRawDiff, |
| 17 | SKIPPED_MODES, |
| 18 | } from '../../../lib/git/languages/git-files.js'; |
| 19 | |
| 20 | const INSERT_CHUNK = 1_000 |
| 21 | |
| 22 | export interface LanguageBytes |
| 23 | language: string; |
| 24 | bytes: number; |
| 25 | } |
| 26 | |
| 27 | @Injectable() |
| 28 | export class RepositoryLanguageService |
| 29 | private readonly logger |
| 30 | private readonly inFlight |
| 31 | |
| 32 | constructor(@Inject( |
| 33 | |
| 34 | async getLanguages({ |
| 35 | repositoryId, |
| 36 | repoDirectory, |
| 37 | ref, |
| 38 | }: { |
| 39 | repositoryId: string |
| 40 | repoDirectory: string |
| 41 | ref: string |
| 42 | }): Promise |
| 43 | const key = |
| 44 | return this.inFlight |
| 45 | this.sync({ |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | private async sync |
| 50 | repositoryId, |
| 51 | repoDirectory, |
| 52 | ref, |
| 53 | }: { |
| 54 | repositoryId: string |
| 55 | repoDirectory: string |
| 56 | ref: string |
| 57 | }): Promise |
| 58 | const tip = |
| 59 | if (!tip) |
| 60 | await this.forget |
| 61 | return []; |
| 62 | } |
| 63 | |
| 64 | const [state] |
| 65 | .select() |
| 66 | .from(schema.repositoryLanguageIndex |
| 67 | .where( |
| 68 | and( |
| 69 | eq(schema.repositoryLanguageIndex |
| 70 | eq(schema.repositoryLanguageIndex |
| 71 | ), |
| 72 | ); |
| 73 | |
| 74 | if (state?.indexedCommitSha |
| 75 | |
| 76 | // 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 |
| 77 | const incremental = |
| 78 | state !== undefined && |
| 79 | (await isAncestor(repoDirectory |
| 80 | |
| 81 | const delta = |
| 82 | ? await this. |
| 83 | repoDirectory, |
| 84 | from: state!.indexedCommitSha |
| 85 | to: tip, |
| 86 | totals: await this |
| 87 | }) |
| 88 | : null; |
| 89 | const totals = |
| 90 | |
| 91 | await this.forget |
| 92 | await this.write |
| 93 | |
| 94 | this.logger.log |
| 95 | `Counted ${totals.size |
| 96 | delta ? `${state |
| 97 | })`, |
| 98 | ); |
| 99 | |
| 100 | return sorted(totals |
| 101 | } |
| 102 | |
| 103 | private async countTree |
| 104 | // ponytail: every blob's path is held until the attributes are read; stream them through check-attr if trees outgrow memory |
| 105 | const files: |
| 106 | |
| 107 | const records = |
| 108 | runGitStream({ |
| 109 | args: ['ls-tree', |
| 110 | gitDir: repoDirectory, |
| 111 | }), |
| 112 | '\0', |
| 113 | ); |
| 114 | |
| 115 | for await (const |
| 116 | // "<mode> <type> <oid> <size>\t<path>", size padded, "-" for non-blobs |
| 117 | const tab = |
| 118 | if (tab === |
| 119 | |
| 120 | const [mode, |
| 121 | if (type !== |
| 122 | |
| 123 | files.push({ |
| 124 | } |
| 125 | |
| 126 | const attributes = |
| 127 | ? await readAttributes( |
| 128 | repoDirectory, |
| 129 | tip, |
| 130 | files.map(({ |
| 131 | ) |
| 132 | : new Map< |
| 133 | |
| 134 | const totals = |
| 135 | for (const { |
| 136 | add(totals, file |
| 137 | } |
| 138 | |
| 139 | return totals; |
| 140 | } |
| 141 | |
| 142 | private async applyDiff |
| 143 | repoDirectory, |
| 144 | from, |
| 145 | to, |
| 146 | totals, |
| 147 | }: { |
| 148 | repoDirectory: string |
| 149 | from: string |
| 150 | to: string |
| 151 | totals: Map |
| 152 | }): Promise |
| 153 | const changes = |
| 154 | |
| 155 | const files = |
| 156 | [before?.path, |
| 157 | Boolean(file), |
| 158 | ), |
| 159 | ); |
| 160 | // a changed .gitattributes can move any file in or out of the stats, so the stored totals no longer apply |
| 161 | if (files.some |
| 162 | |
| 163 | // .gitattributes is the same at both ends, so `to` decides for the old paths too |
| 164 | const attributes = |
| 165 | const sizes = |
| 166 | repoDirectory, |
| 167 | changes.flatMap(({ |
| 168 | [before?.oid, |
| 169 | ), |
| 170 | ); |
| 171 | |
| 172 | for (const { |
| 173 | if (before) { |
| 174 | add( |
| 175 | totals, |
| 176 | before.path, |
| 177 | -(sizes.get( |
| 178 | attributes.get(before |
| 179 | ); |
| 180 | } |
| 181 | if (after) { |
| 182 | add( |
| 183 | totals, |
| 184 | after.path, |
| 185 | sizes.get(after |
| 186 | attributes.get(after |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // a size git no longer agrees with would otherwise drift negative forever |
| 192 | for (const [language |
| 193 | if (bytes <= |
| 194 | } |
| 195 | |
| 196 | return totals; |
| 197 | } |
| 198 | |
| 199 | private async sizesOf |
| 200 | const sizes = |
| 201 | if (oids.length |
| 202 | |
| 203 | const output = |
| 204 | args: [ |
| 205 | 'cat-file', |
| 206 | '--batch-check=%(objectname) %(objecttype) %(objectsize)', |
| 207 | ], |
| 208 | gitDir: repoDirectory, |
| 209 | input: Buffer.from |
| 210 | }); |
| 211 | |
| 212 | for (const line |
| 213 | const [oid, |
| 214 | if (type === |
| 215 | } |
| 216 | |
| 217 | return sizes; |
| 218 | } |
| 219 | |
| 220 | private async read |
| 221 | return sorted(await |
| 222 | } |
| 223 | |
| 224 | private async readMap |
| 225 | const rows = |
| 226 | .select({ |
| 227 | language: schema.repositoryLanguageStat |
| 228 | bytes: schema.repositoryLanguageStat |
| 229 | }) |
| 230 | .from(schema.repositoryLanguageStat |
| 231 | .where( |
| 232 | and( |
| 233 | eq(schema.repositoryLanguageStat |
| 234 | eq(schema.repositoryLanguageStat |
| 235 | ), |
| 236 | ); |
| 237 | |
| 238 | return new Map |
| 239 | } |
| 240 | |
| 241 | private async write |
| 242 | repositoryId: string, |
| 243 | ref: string, |
| 244 | tip: string, |
| 245 | totals: Map |
| 246 | ) { |
| 247 | const rows = |
| 248 | .filter(([, bytes |
| 249 | .map(([language |
| 250 | |
| 251 | for (let i |
| 252 | await this.db |
| 253 | .insert(schema.repositoryLanguageStat |
| 254 | .values(rows.slice |
| 255 | .onConflictDoUpdate({ |
| 256 | target: [ |
| 257 | schema.repositoryLanguageStat.repositoryId |
| 258 | schema.repositoryLanguageStat.ref |
| 259 | schema.repositoryLanguageStat.language |
| 260 | ], |
| 261 | set: { bytes: excluded |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | await this.db |
| 266 | .insert(schema.repositoryLanguageIndex |
| 267 | .values({ repositoryId |
| 268 | .onConflictDoUpdate({ |
| 269 | target: [ |
| 270 | schema.repositoryLanguageIndex.repositoryId |
| 271 | schema.repositoryLanguageIndex.ref |
| 272 | ], |
| 273 | set: { indexedCommitSha: tip |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | private async forget |
| 278 | await this.db |
| 279 | .delete(schema.repositoryLanguageStat |
| 280 | .where( |
| 281 | and( |
| 282 | eq(schema.repositoryLanguageStat |
| 283 | eq(schema.repositoryLanguageStat |
| 284 | ), |
| 285 | ); |
| 286 | await this.db |
| 287 | .delete(schema.repositoryLanguageIndex |
| 288 | .where( |
| 289 | and( |
| 290 | eq(schema.repositoryLanguageIndex |
| 291 | eq(schema.repositoryLanguageIndex |
| 292 | ), |
| 293 | ); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | function add( |
| 298 | totals: Map |
| 299 | path: string, |
| 300 | bytes: number, |
| 301 | attributes?: LinguistAttributes |
| 302 | ) { |
| 303 | const language = |
| 304 | if (!language || |
| 305 | totals.set(language |
| 306 | } |
| 307 | |
| 308 | function sorted(totals: |
| 309 | return [...totals |
| 310 | .filter(([, bytes |
| 311 | .map(([language |
| 312 | .sort((a, |
| 313 | } |