| 1 | import { readFile, writeFile } from 'node:fs/promises'; |
| 2 | import path from 'node:path |
| 3 | |
| 4 | import { Injectable, Logger |
| 5 | |
| 6 | import { runGit } |
| 7 | import { listRefs } |
| 8 | import { headRef } |
| 9 | import { emptyIndex, type |
| 10 | import { SingleFlight } |
| 11 | import { RepositoryStorageService } |
| 12 | import { WalStoreService } |
| 13 | |
| 14 | const SEQ_MARKER = ' |
| 15 | const DEFAULT_BRANCH_PREFERENCE = [ |
| 16 | |
| 17 | /** Brings a cached bare repository up to the log. The cache is only ever behind it, so replay is forward-only: apply the packs past the cached sequence, then reconcile refs to the index snapshot. */ |
| 18 | @Injectable() |
| 19 | export class RepositoryMaterializerService |
| 20 | private readonly logger |
| 21 | private readonly inFlight |
| 22 | |
| 23 | constructor( |
| 24 | private readonly store |
| 25 | private readonly storage |
| 26 | ) {} |
| 27 | |
| 28 | /** The repository's cache directory, current with its log. Everything that hands a repository to git opens it this way. */ |
| 29 | async open(repository |
| 30 | const directory = |
| 31 | await this.materialize |
| 32 | return directory; |
| 33 | } |
| 34 | |
| 35 | async materialize( |
| 36 | repoId: string, |
| 37 | repoDirectory: string, |
| 38 | defaultBranch: string |
| 39 | ): Promise<WalIndex |
| 40 | return this.inFlight |
| 41 | this.replay(repoId |
| 42 | ); |
| 43 | } |
| 44 | |
| 45 | /** Resolves once a replay in flight has finished, whatever its outcome, so its writes cannot land after the cache is removed. */ |
| 46 | async settle(repoId |
| 47 | await this.inFlight |
| 48 | } |
| 49 | |
| 50 | private async replay |
| 51 | repoId: string, |
| 52 | repoDirectory: string, |
| 53 | defaultBranch: string |
| 54 | ): Promise<WalIndex |
| 55 | const stored = |
| 56 | if (!stored) |
| 57 | |
| 58 | const { index |
| 59 | const cachedSeq = |
| 60 | // The default branch changes without a push, so HEAD is checked even when the cache is current. |
| 61 | if (cachedSeq >= |
| 62 | await this.ensureHead |
| 63 | return index; |
| 64 | } |
| 65 | |
| 66 | for (const [offset |
| 67 | const seq = |
| 68 | if (seq <= |
| 69 | |
| 70 | // Pushes since 0034 store self-contained packs, but older layers may be thin, with deltas against objects from earlier layers, which is why replay must stay in sequence order. |
| 71 | if (layer.size |
| 72 | await runGit({ |
| 73 | args: ['index-pack', |
| 74 | gitDir: repoDirectory, |
| 75 | input: await this |
| 76 | }); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | await this.reconcileRefs |
| 81 | await this.ensureHead |
| 82 | await writeFile( |
| 83 | path.join(repoDirectory |
| 84 | `${index.seq} |
| 85 | 'utf8', |
| 86 | ); |
| 87 | this.logger.log |
| 88 | `Materialized ${repoId} from seq ${ |
| 89 | ); |
| 90 | |
| 91 | return index; |
| 92 | } |
| 93 | |
| 94 | private async reconcileRefs |
| 95 | const existing = |
| 96 | const commands: |
| 97 | |
| 98 | for (const [ref |
| 99 | commands.push(`update |
| 100 | } |
| 101 | for (const ref |
| 102 | if (!index.refs |
| 103 | } |
| 104 | |
| 105 | if (commands.length |
| 106 | await runGit({ |
| 107 | args: ['update-ref', |
| 108 | gitDir: repoDirectory, |
| 109 | input: Buffer.from |
| 110 | }); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** HEAD follows the chosen default branch while it exists. Otherwise a HEAD naming a missing branch clones as empty, so it is pointed at a branch that actually exists. */ |
| 115 | private async ensureHead |
| 116 | repoDirectory: string, |
| 117 | index: WalIndex |
| 118 | defaultBranch: string |
| 119 | ) { |
| 120 | if (index.refs |
| 121 | |
| 122 | const current = |
| 123 | |
| 124 | const chosen = |
| 125 | const target = |
| 126 | [chosen, current, |
| 127 | (ref) => |
| 128 | ) ?? [...index |
| 129 | if (!target || |
| 130 | |
| 131 | await runGit({ |
| 132 | args: ['symbolic-ref', |
| 133 | gitDir: repoDirectory, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | private async readCachedSeq |
| 138 | try { |
| 139 | const raw = |
| 140 | const seq = |
| 141 | return Number.isInteger |
| 142 | } catch { |
| 143 | return 0; |
| 144 | } |
| 145 | } |
| 146 | } |