| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { buffer } |
| 6 | import { afterEach, beforeEach |
| 7 | |
| 8 | import { type RefTransition, |
| 9 | import { bufferBody, type |
| 10 | import { PushRejectedError } |
| 11 | import { withVerifiedPack } |
| 12 | |
| 13 | const identity = { |
| 14 | GIT_AUTHOR_NAME: 'Test', |
| 15 | GIT_AUTHOR_EMAIL: 'test@example.com', |
| 16 | GIT_COMMITTER_NAME: 'Test', |
| 17 | GIT_COMMITTER_EMAIL: 'test@example.com', |
| 18 | }; |
| 19 | |
| 20 | describe('withVerifiedPack', () |
| 21 | let root: |
| 22 | let cache: |
| 23 | let work: |
| 24 | let first: |
| 25 | |
| 26 | const git = |
| 27 | const env: |
| 28 | delete env.GIT_DIR; |
| 29 | return execFileSync('git |
| 30 | }; |
| 31 | const text = |
| 32 | git(cwd, args |
| 33 | const update = |
| 34 | ref, |
| 35 | oldOid: old, |
| 36 | newOid: Buffer.from |
| 37 | }); |
| 38 | const deletion = |
| 39 | ref, |
| 40 | oldOid: Buffer.from |
| 41 | newOid: ZERO_OID, |
| 42 | }); |
| 43 | /** What a client sends: the objects `tip` adds over `excluded`, thin against them. */ |
| 44 | const thinPack = |
| 45 | git( |
| 46 | work, |
| 47 | ['pack-objects', '--stdout' |
| 48 | `${tip}\n${excluded |
| 49 | ); |
| 50 | const verified = |
| 51 | let stored = |
| 52 | await withVerifiedPack( |
| 53 | { gitDir: cache, |
| 54 | async ({ |
| 55 | body, |
| 56 | packOffset, |
| 57 | }: { |
| 58 | body: GitRequestBody; |
| 59 | packOffset: number; |
| 60 | }) => { |
| 61 | stored = await |
| 62 | }, |
| 63 | ); |
| 64 | return stored; |
| 65 | }; |
| 66 | /** A repository with no objects at all, given only `pack`: indexing fails if the pack leans on a delta base it does not carry. */ |
| 67 | const indexesAlone = |
| 68 | const node = |
| 69 | execFileSync('git', |
| 70 | git(node, [ |
| 71 | return node; |
| 72 | }; |
| 73 | |
| 74 | beforeEach(() => |
| 75 | root = mkdtempSync( |
| 76 | cache = path. |
| 77 | work = path. |
| 78 | execFileSync('git', |
| 79 | writeFileSync(path.join |
| 80 | git(work, [ |
| 81 | git(work, [ |
| 82 | first = text( |
| 83 | execFileSync('git', |
| 84 | }); |
| 85 | |
| 86 | afterEach(() => |
| 87 | |
| 88 | it('stores a pack that replays without borrowing a delta base from anywhere', |
| 89 | // Random lines, so the new version is only small as a delta against the old. |
| 90 | const lines = |
| 91 | { length: 3000 }, |
| 92 | (_, i) |
| 93 | ); |
| 94 | writeFileSync(path.join |
| 95 | git(work, [ |
| 96 | git(work, [ |
| 97 | const large = |
| 98 | git(work, [ |
| 99 | writeFileSync( |
| 100 | path.join(work |
| 101 | `${lines.join( |
| 102 | ); |
| 103 | git(work, [ |
| 104 | const tip = |
| 105 | const pushed = |
| 106 | expect(() => |
| 107 | |
| 108 | const stored = |
| 109 | [update('refs/heads/main', |
| 110 | pushed, |
| 111 | ); |
| 112 | |
| 113 | expect(text(indexesAlone |
| 114 | }); |
| 115 | |
| 116 | it('refuses a ref at an object only the cache holds, with or without a pack', |
| 117 | // Left behind the way a merge that never landed leaves its commit. |
| 118 | const orphan = |
| 119 | cache, |
| 120 | 'commit-tree', |
| 121 | `${first}^{tree}`, |
| 122 | '-p', |
| 123 | first, |
| 124 | '-m', |
| 125 | 'never in the log', |
| 126 | ); |
| 127 | writeFileSync(path.join |
| 128 | git(work, [ |
| 129 | git(work, [ |
| 130 | const b = |
| 131 | |
| 132 | await expect( |
| 133 | verified([update('refs/heads/stray |
| 134 | ).rejects.toThrow( |
| 135 | await expect( |
| 136 | verified( |
| 137 | [ |
| 138 | update('refs/heads/main', |
| 139 | update('refs/heads/stray', |
| 140 | ], |
| 141 | thinPack(b, first |
| 142 | ), |
| 143 | ).rejects.toThrow( |
| 144 | }); |
| 145 | |
| 146 | it('refuses a ref at an object nobody holds', |
| 147 | await expect( |
| 148 | verified([update('refs/heads/evil |
| 149 | ).rejects.toThrow( |
| 150 | }); |
| 151 | |
| 152 | it('refuses a branch at anything but a commit', |
| 153 | const tree = |
| 154 | await expect( |
| 155 | verified([update('refs/heads/tree |
| 156 | ).rejects.toThrow( |
| 157 | }); |
| 158 | |
| 159 | it('refuses a pack git cannot read, even one that updates nothing', |
| 160 | const garbage = |
| 161 | Buffer.from('PACK |
| 162 | Buffer.from([0, |
| 163 | ]); |
| 164 | for (const transitions |
| 165 | [update('refs/heads/copy', |
| 166 | [deletion('refs/heads/main', |
| 167 | ]) { |
| 168 | await expect(verified |
| 169 | PushRejectedError, |
| 170 | ); |
| 171 | } |
| 172 | }); |
| 173 | |
| 174 | it('hands a delete with no pack straight through', |
| 175 | expect( |
| 176 | await verified([deletion |
| 177 | ).toEqual(Buffer. |
| 178 | }); |
| 179 | |
| 180 | it('verifies a push of more refs than argv can carry', |
| 181 | writeFileSync(path.join |
| 182 | git(work, [ |
| 183 | const second = |
| 184 | // ~1.2 MB of object names, past macOS's 1 MB ARG_MAX, as an import's pull request refs are |
| 185 | const refs = |
| 186 | update(`refs/pull/${number |
| 187 | ); |
| 188 | expect( |
| 189 | (await verified(refs |
| 190 | ).toBeGreaterThan(0); |
| 191 | }); |
| 192 | |
| 193 | it('accepts a new branch at a commit the repository already reaches', |
| 194 | expect( |
| 195 | await verified([update |
| 196 | ).toEqual(Buffer. |
| 197 | }); |
| 198 | }); |