| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { readFile, writeFile |
| 3 | import path from 'node:path |
| 4 | |
| 5 | import { runGit } |
| 6 | import { WalStoreService } |
| 7 | import { emptyIndex, type |
| 8 | |
| 9 | const SEQ_MARKER = ' |
| 10 | const DEFAULT_BRANCH_PREFERENCE = [ |
| 11 | |
| 12 | /** |
| 13 | * Brings a cached bare repository up to the state the log describes. |
| 14 | * |
| 15 | * The cache is only ever behind the log, never ahead of it, so replay is always forward-only: apply the packfiles of every layer past the cached sequence, then reconcile refs to the index snapshot. |
| 16 | */ |
| 17 | @Injectable() |
| 18 | export class RepositoryMaterializerService |
| 19 | private readonly logger |
| 20 | private readonly inFlight |
| 21 | |
| 22 | constructor(private readonly |
| 23 | |
| 24 | async materialize( |
| 25 | repoId: string, |
| 26 | repoDirectory: string, |
| 27 | defaultBranch: string |
| 28 | ): Promise<WalIndex |
| 29 | const pending = |
| 30 | if (pending) return |
| 31 | |
| 32 | const run = |
| 33 | this.inFlight.delete |
| 34 | ); |
| 35 | this.inFlight.set |
| 36 | return run; |
| 37 | } |
| 38 | |
| 39 | /** Resolves once a replay in flight has finished, whatever its outcome, so its writes cannot land after the cache is removed. */ |
| 40 | async settle(repoId |
| 41 | await this.inFlight |
| 42 | } |
| 43 | |
| 44 | private async replay |
| 45 | repoId: string, |
| 46 | repoDirectory: string, |
| 47 | defaultBranch: string |
| 48 | ): Promise<WalIndex |
| 49 | const stored = |
| 50 | if (!stored) |
| 51 | |
| 52 | const { index |
| 53 | const cachedSeq = |
| 54 | // The default branch changes without a push, so HEAD is checked even when the cache is current. |
| 55 | if (cachedSeq >= |
| 56 | await this.ensureHead |
| 57 | return index; |
| 58 | } |
| 59 | |
| 60 | for (const [offset |
| 61 | const seq = |
| 62 | if (seq <= |
| 63 | |
| 64 | // Packs from receive-pack are thin: deltas may reference objects from earlier layers, which is why replay must stay in sequence order. |
| 65 | if (layer.size |
| 66 | await runGit({ |
| 67 | args: ['index-pack', |
| 68 | gitDir: repoDirectory, |
| 69 | input: await this |
| 70 | }); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | await this.reconcileRefs |
| 75 | await this.ensureHead |
| 76 | await this.writeCachedSeq |
| 77 | this.logger.log |
| 78 | `Materialized ${repoId} from seq ${ |
| 79 | ); |
| 80 | |
| 81 | return index; |
| 82 | } |
| 83 | |
| 84 | private async reconcileRefs |
| 85 | const existing = |
| 86 | const commands: |
| 87 | |
| 88 | for (const [ref |
| 89 | commands.push(`update |
| 90 | } |
| 91 | for (const ref |
| 92 | if (!index.refs |
| 93 | } |
| 94 | |
| 95 | if (commands.length |
| 96 | await runGit({ |
| 97 | args: ['update-ref', |
| 98 | gitDir: repoDirectory, |
| 99 | input: Buffer.from |
| 100 | }); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private async listRefs |
| 105 | const output = |
| 106 | args: ['for-each-ref', |
| 107 | gitDir: repoDirectory, |
| 108 | }); |
| 109 | return output.split |
| 110 | } |
| 111 | |
| 112 | /** 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. */ |
| 113 | private async ensureHead |
| 114 | repoDirectory: string, |
| 115 | index: WalIndex |
| 116 | defaultBranch: string |
| 117 | ) { |
| 118 | if (index.refs |
| 119 | |
| 120 | const current = |
| 121 | await runGit({ |
| 122 | args: ['symbolic-ref', |
| 123 | gitDir: repoDirectory, |
| 124 | }).catch(() |
| 125 | ).trim(); |
| 126 | |
| 127 | const chosen = |
| 128 | const target = |
| 129 | [chosen, current, |
| 130 | (ref) => |
| 131 | ) ?? [...index |
| 132 | if (!target || |
| 133 | |
| 134 | await runGit({ |
| 135 | args: ['symbolic-ref', |
| 136 | gitDir: repoDirectory, |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | private seqMarkerPath(repoDirectory |
| 141 | return path.join |
| 142 | } |
| 143 | |
| 144 | private async readCachedSeq |
| 145 | try { |
| 146 | const raw = |
| 147 | const seq = |
| 148 | return Number.isInteger |
| 149 | } catch { |
| 150 | return 0; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | private async writeCachedSeq |
| 155 | await writeFile(this |
| 156 | } |
| 157 | } |