| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { createHash } |
| 3 | import { mkdtempSync, rmSync |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { afterEach, beforeEach |
| 7 | |
| 8 | import { InMemoryWalStore } |
| 9 | import { bufferBody } |
| 10 | import { createUlid } |
| 11 | import { encodeEntryHeader } |
| 12 | import { RepositoryDeletedError } |
| 13 | import { |
| 14 | applyTransitions, |
| 15 | emptyIndex, |
| 16 | type RefTransition, |
| 17 | ZERO_OID, |
| 18 | } from '../../../lib/git/wal/wal.types.js'; |
| 19 | import { RepositoryMaterializerService } |
| 20 | import type { RepositoryStorageService |
| 21 | import { PushTransactionService } |
| 22 | import type { WalStoreService |
| 23 | import { RepositoryRepairService } |
| 24 | |
| 25 | const REPO = ' |
| 26 | const identity = { |
| 27 | GIT_AUTHOR_NAME: 'Test', |
| 28 | GIT_AUTHOR_EMAIL: 'test@example.com', |
| 29 | GIT_COMMITTER_NAME: 'Test', |
| 30 | GIT_COMMITTER_EMAIL: 'test@example.com', |
| 31 | }; |
| 32 | const garbage = Buffer |
| 33 | Buffer.from('PACK |
| 34 | Buffer.from([0, |
| 35 | ]); |
| 36 | |
| 37 | describe('RepositoryRepairService', () |
| 38 | let root: |
| 39 | let work: |
| 40 | let cache: |
| 41 | let store: |
| 42 | let repair: |
| 43 | let first: |
| 44 | |
| 45 | const git = |
| 46 | const env: |
| 47 | delete env.GIT_DIR; |
| 48 | return execFileSync('git |
| 49 | }; |
| 50 | const text = |
| 51 | git(cwd, args |
| 52 | const set = |
| 53 | ref, |
| 54 | oldOid: old, |
| 55 | newOid: Buffer.from |
| 56 | }); |
| 57 | const pack = |
| 58 | git(work, [ |
| 59 | const storage = |
| 60 | ({ |
| 61 | getRepoPath: async () |
| 62 | }) as unknown |
| 63 | |
| 64 | /** Writes an entry the way the log did before 0034, with no check of any kind: the only way a broken log comes to exist. */ |
| 65 | async function append |
| 66 | const stored = |
| 67 | const index = |
| 68 | const ulid = |
| 69 | await store.putEntry |
| 70 | REPO, |
| 71 | ulid, |
| 72 | encodeEntryHeader({ |
| 73 | ulid, |
| 74 | createdAt: Date.now |
| 75 | pushedBy: null, |
| 76 | transitions, |
| 77 | }), |
| 78 | bufferBody(bytes), |
| 79 | 0, |
| 80 | ); |
| 81 | await store.casIndex |
| 82 | REPO, |
| 83 | { |
| 84 | ...index, |
| 85 | seq: index.seq |
| 86 | refs: applyTransitions(index |
| 87 | layers: [ |
| 88 | ...index.layers, |
| 89 | { |
| 90 | ulid, |
| 91 | packSha: createHash('sha256 |
| 92 | size: bytes.length, |
| 93 | }, |
| 94 | ], |
| 95 | }, |
| 96 | stored?.etag ?? |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | /** What a node holding nothing but the log ends up with: its refs, or the error that stops it. */ |
| 101 | async function replayFresh |
| 102 | const fresh = |
| 103 | execFileSync('git', |
| 104 | await new RepositoryMaterializerService |
| 105 | store as unknown |
| 106 | storage(fresh), |
| 107 | ).materialize(REPO, |
| 108 | return Object.fromEntries |
| 109 | text(fresh, ' |
| 110 | .split('\n') |
| 111 | .filter(Boolean) |
| 112 | .map((line) |
| 113 | ) as Record< |
| 114 | } |
| 115 | |
| 116 | /** The local cache as the API keeps it, up to date with the log as it stood before the damage. */ |
| 117 | async function warmCache |
| 118 | await new RepositoryMaterializerService |
| 119 | store as unknown |
| 120 | storage(cache), |
| 121 | ).materialize(REPO, |
| 122 | } |
| 123 | |
| 124 | beforeEach(async () |
| 125 | root = mkdtempSync( |
| 126 | work = path. |
| 127 | cache = path. |
| 128 | execFileSync('git', |
| 129 | execFileSync('git', |
| 130 | writeFileSync(path.join |
| 131 | git(work, [ |
| 132 | git(work, [ |
| 133 | first = text( |
| 134 | |
| 135 | store = new |
| 136 | repair = new |
| 137 | store as unknown |
| 138 | new PushTransactionService(store |
| 139 | storage(cache), |
| 140 | ); |
| 141 | await append([set |
| 142 | await warmCache(); |
| 143 | }); |
| 144 | |
| 145 | afterEach(() => |
| 146 | |
| 147 | it('leaves a log that replays alone untouched', |
| 148 | const before = |
| 149 | |
| 150 | expect(await repair |
| 151 | expect(await repair |
| 152 | expect((await store |
| 153 | }); |
| 154 | |
| 155 | it('has nothing to say about a repository with no log', |
| 156 | expect(await repair |
| 157 | expect(await repair |
| 158 | }); |
| 159 | |
| 160 | it.each(['check |
| 161 | '%s treats a deleted repository as absent', |
| 162 | async (operation) |
| 163 | vi.spyOn(store |
| 164 | new RepositoryDeletedError(), |
| 165 | ); |
| 166 | const openPack = |
| 167 | const writeIndex = |
| 168 | |
| 169 | await expect(repair |
| 170 | expect(openPack).not |
| 171 | expect(writeIndex).not |
| 172 | }, |
| 173 | ); |
| 174 | |
| 175 | it.each(['check |
| 176 | '%s propagates other index read errors', |
| 177 | async (operation) |
| 178 | const error = |
| 179 | vi.spyOn(store |
| 180 | |
| 181 | await expect(repair |
| 182 | }, |
| 183 | ); |
| 184 | |
| 185 | it('only reports when checking, and writes nothing', |
| 186 | await append([set |
| 187 | const before = |
| 188 | |
| 189 | expect(await repair |
| 190 | layers: [], |
| 191 | rescued: [], |
| 192 | dropped: [ |
| 193 | { |
| 194 | ref: 'refs/heads/evil', |
| 195 | oid: 'ab'.repeat |
| 196 | reason: 'objects it reaches are in neither the log nor the cache', |
| 197 | }, |
| 198 | ], |
| 199 | }); |
| 200 | expect((await store |
| 201 | await expect(replayFresh |
| 202 | }); |
| 203 | |
| 204 | it('drops a ref at an object nobody holds, and the log replays again', |
| 205 | await append([set |
| 206 | |
| 207 | const report = |
| 208 | |
| 209 | expect(report?.dropped |
| 210 | expect(await replayFresh |
| 211 | expect(await repair |
| 212 | }); |
| 213 | |
| 214 | it('skips an entry git cannot read, keeping the refs it can still serve', |
| 215 | await append([set |
| 216 | |
| 217 | const report = |
| 218 | |
| 219 | expect(report?.layers |
| 220 | expect.objectContaining({ |
| 221 | ]); |
| 222 | expect(report?.dropped |
| 223 | expect(await replayFresh |
| 224 | 'refs/heads/main': first, |
| 225 | 'refs/heads/copy': first, |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | it('skips an entry object storage lost', |
| 230 | const stored = |
| 231 | await store.casIndex |
| 232 | REPO, |
| 233 | { |
| 234 | ...stored!.index |
| 235 | seq: stored!.index |
| 236 | layers: [ |
| 237 | ...stored!.index |
| 238 | { ulid: createUlid() |
| 239 | ], |
| 240 | }, |
| 241 | stored!.etag, |
| 242 | ); |
| 243 | |
| 244 | const report = |
| 245 | |
| 246 | expect(report?.layers |
| 247 | { |
| 248 | ulid: expect.any |
| 249 | outcome: 'dropped', |
| 250 | reason: 'the entry is missing from object storage', |
| 251 | }, |
| 252 | ]); |
| 253 | expect(await replayFresh |
| 254 | }); |
| 255 | |
| 256 | it('rebuilds an entry thin against objects only the cache held, and puts back the history it reached', |
| 257 | // Random lines, so the next version can only travel as a delta against this one. |
| 258 | const lines = |
| 259 | { length: 3000 }, |
| 260 | (_, i) |
| 261 | ); |
| 262 | writeFileSync(path.join |
| 263 | git(work, [ |
| 264 | git(work, [ |
| 265 | const cached = |
| 266 | execFileSync('git', |
| 267 | env: { ...process |
| 268 | input: pack(`${ |
| 269 | }); |
| 270 | writeFileSync( |
| 271 | path.join(work |
| 272 | `${lines.join( |
| 273 | ); |
| 274 | git(work, [ |
| 275 | const tip = |
| 276 | await append( |
| 277 | [set('refs/heads/main', |
| 278 | pack(`${tip} |
| 279 | ); |
| 280 | await expect(replayFresh |
| 281 | |
| 282 | const report = |
| 283 | |
| 284 | expect(report?.layers |
| 285 | expect.objectContaining({ |
| 286 | ]); |
| 287 | expect(report?.rescued |
| 288 | expect(report?.dropped |
| 289 | expect(await replayFresh |
| 290 | }); |
| 291 | |
| 292 | it('drops a branch at anything but a commit', |
| 293 | const tree = |
| 294 | await append([set |
| 295 | |
| 296 | const report = |
| 297 | |
| 298 | expect(report?.dropped |
| 299 | { |
| 300 | ref: 'refs/heads/tree', |
| 301 | oid: tree, |
| 302 | reason: 'a branch must point at a commit, not a tree', |
| 303 | }, |
| 304 | ]); |
| 305 | expect(await replayFresh |
| 306 | }); |
| 307 | |
| 308 | it('drops a ref git refuses by name', |
| 309 | await append([set |
| 310 | |
| 311 | const report = |
| 312 | |
| 313 | expect(report?.dropped |
| 314 | expect(await replayFresh |
| 315 | }); |
| 316 | |
| 317 | it('of two refs that cannot coexist, drops the one written later', |
| 318 | await append([set |
| 319 | |
| 320 | const report = |
| 321 | |
| 322 | expect(report?.dropped |
| 323 | { |
| 324 | ref: 'refs/heads/main/nested', |
| 325 | oid: first, |
| 326 | reason: 'it conflicts with refs/heads/main', |
| 327 | }, |
| 328 | ]); |
| 329 | expect(await replayFresh |
| 330 | }); |
| 331 | |
| 332 | it('starts over when a push moves the index during the repair', |
| 333 | await append([set |
| 334 | const swap = |
| 335 | |
| 336 | const report = |
| 337 | |
| 338 | expect(swap).toHaveBeenCalledTimes |
| 339 | expect(report?.layers |
| 340 | expect(await replayFresh |
| 341 | 'refs/heads/main': first, |
| 342 | 'refs/heads/copy': first, |
| 343 | }); |
| 344 | }); |
| 345 | }); |