| 1 | import type { Database } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { Readable } |
| 4 | |
| 5 | import { RepositoryMaterializerService } |
| 6 | import { PackProcessService } |
| 7 | import type { GitRequestBody |
| 8 | import { |
| 9 | isProbeRequest, |
| 10 | readReceivePackHeader, |
| 11 | } from '../lib/git/protocol/receive-pack-request.js'; |
| 12 | import { RefAdvertisementService } |
| 13 | import { RepositoryStorageService } |
| 14 | import { RepositoryContributionService } |
| 15 | import { PushTransactionService } |
| 16 | import { CodeSearchService } |
| 17 | import { IssueReferencesService } |
| 18 | import { listCommits } |
| 19 | import { resolveDefaultRef } |
| 20 | import { type RefTransition, |
| 21 | import { MAX_CLOSING_COMMITS } |
| 22 | import { MAX_PUSH_COMMITS, publishEvent |
| 23 | import { DATABASE } |
| 24 | import { isGitServiceName, type |
| 25 | import { UnsupportedGitServiceError } |
| 26 | |
| 27 | export interface GitTransportResponse |
| 28 | headers: Record |
| 29 | body: Readable |
| 30 | } |
| 31 | |
| 32 | interface RepositoryRef { |
| 33 | repositoryId: string; |
| 34 | defaultBranch: string |
| 35 | } |
| 36 | |
| 37 | @Injectable() |
| 38 | export class GitService |
| 39 | private readonly logger |
| 40 | |
| 41 | constructor( |
| 42 | private readonly storage |
| 43 | private readonly refAdvertisement |
| 44 | private readonly packProcess |
| 45 | private readonly pushTransaction |
| 46 | private readonly materializer |
| 47 | private readonly contributions |
| 48 | private readonly codeSearch |
| 49 | private readonly references |
| 50 | @Inject(DATABASE) |
| 51 | ) {} |
| 52 | |
| 53 | async advertiseRefs({ |
| 54 | repositoryId, |
| 55 | defaultBranch, |
| 56 | service, |
| 57 | }: RepositoryRef |
| 58 | if (!isGitServiceName(service |
| 59 | |
| 60 | const repoDirectory = |
| 61 | repositoryId, |
| 62 | defaultBranch, |
| 63 | ); |
| 64 | |
| 65 | return { |
| 66 | headers: { |
| 67 | 'Content-Type': `application/x-${service |
| 68 | 'Cache-Control': 'no-cache', |
| 69 | }, |
| 70 | body: this.refAdvertisement |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | async uploadPack({ |
| 75 | repositoryId, |
| 76 | defaultBranch, |
| 77 | body, |
| 78 | }: RepositoryRef |
| 79 | const repoDirectory = |
| 80 | repositoryId, |
| 81 | defaultBranch, |
| 82 | ); |
| 83 | |
| 84 | return { |
| 85 | headers: resultHeaders('git-upload-pack |
| 86 | body: this.packProcess |
| 87 | repoDirectory, |
| 88 | input: body.open |
| 89 | }), |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * `POST /:username/:repo/git-receive-pack` - a push. |
| 95 | * |
| 96 | * Materialize before the ref checks so they see what the log holds. The cache keeps its pre-push sequence marker; the next materialize reconciles whatever git wrote locally. |
| 97 | */ |
| 98 | async receivePack({ |
| 99 | repositoryId, |
| 100 | defaultBranch, |
| 101 | isPublic, |
| 102 | body, |
| 103 | pushedBy = null, |
| 104 | }: RepositoryRef |
| 105 | isPublic: boolean |
| 106 | body: GitRequestBody |
| 107 | pushedBy?: string |
| 108 | }): Promise |
| 109 | if (await isProbeRequest |
| 110 | return { |
| 111 | headers: resultHeaders('git-receive-pack |
| 112 | body: Readable.from |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | const { transitions, |
| 117 | const repoDirectory = |
| 118 | repositoryId, |
| 119 | defaultBranch, |
| 120 | ); |
| 121 | |
| 122 | await this.pushTransaction |
| 123 | repoId: repositoryId, |
| 124 | transitions, |
| 125 | body, |
| 126 | packOffset, |
| 127 | pushedBy, |
| 128 | }); |
| 129 | |
| 130 | const result = |
| 131 | repoDirectory, |
| 132 | input: body.open |
| 133 | }); |
| 134 | |
| 135 | // Fire-and-forget: a push must not wait on a history walk or a reindex, and an interrupted index leaves a cursor the next sync tops up. |
| 136 | result.once('close |
| 137 | this.codeSearch.indexInBackground |
| 138 | repositoryId, |
| 139 | isPublic, |
| 140 | repoDirectory, |
| 141 | }); |
| 142 | this.contributions |
| 143 | .sync({ repositoryId |
| 144 | .catch((error: |
| 145 | this.logger.warn |
| 146 | `Contribution index update failed for ${repositoryId}: ${ |
| 147 | ), |
| 148 | ); |
| 149 | this.closeFromPush({ |
| 150 | repositoryId, |
| 151 | repoDirectory, |
| 152 | transitions, |
| 153 | pushedBy, |
| 154 | }).catch((error |
| 155 | this.logger.warn |
| 156 | `Closing referenced issues failed for ${repositoryId}: ${ |
| 157 | ), |
| 158 | ); |
| 159 | this.publishPushes({ |
| 160 | repositoryId, |
| 161 | repoDirectory, |
| 162 | transitions, |
| 163 | pushedBy, |
| 164 | }).catch((error |
| 165 | this.logger.warn |
| 166 | `Publishing push events failed for ${repositoryId}: ${ |
| 167 | ), |
| 168 | ); |
| 169 | }); |
| 170 | |
| 171 | return { |
| 172 | headers: resultHeaders('git-receive-pack |
| 173 | body: result, |
| 174 | }; |
| 175 | } |
| 176 | |
| 177 | /** Published after the push, not with it: refs live in object storage, outside any database transaction, so a crash in between loses the event but never invents one. */ |
| 178 | private async publishPushes |
| 179 | repositoryId, |
| 180 | repoDirectory, |
| 181 | transitions, |
| 182 | pushedBy, |
| 183 | }: Pick |
| 184 | repoDirectory: string |
| 185 | transitions: RefTransition |
| 186 | pushedBy: string |
| 187 | }) { |
| 188 | for (const { |
| 189 | const before = |
| 190 | const after = |
| 191 | const { commits |
| 192 | ? { commits: [] |
| 193 | : await listCommits( |
| 194 | gitDir: repoDirectory, |
| 195 | // a new ref lists from its tip, since there is no old one to start after |
| 196 | ref: oldOid.equals |
| 197 | limit: MAX_PUSH_COMMITS, |
| 198 | }); |
| 199 | await publishEvent(this |
| 200 | type: 'push', |
| 201 | repositoryId, |
| 202 | actorId: pushedBy, |
| 203 | payload: { |
| 204 | ref, |
| 205 | before, |
| 206 | after, |
| 207 | commits, |
| 208 | }, |
| 209 | }); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** Commits that moved the default branch forward close the issues they name, like GitHub's `Fixes #1`. */ |
| 214 | private async closeFromPush |
| 215 | repositoryId, |
| 216 | repoDirectory, |
| 217 | transitions, |
| 218 | pushedBy, |
| 219 | }: Pick |
| 220 | repoDirectory: string |
| 221 | transitions: RefTransition |
| 222 | pushedBy: string |
| 223 | }) { |
| 224 | const defaultRef = |
| 225 | const pushed = |
| 226 | (transition) => |
| 227 | ); |
| 228 | // ponytail: creating or deleting the default branch closes nothing; the first push of a whole history would otherwise close every issue it ever fixed. |
| 229 | if ( |
| 230 | !pushed || |
| 231 | pushed.oldOid.equals |
| 232 | pushed.newOid.equals |
| 233 | ) |
| 234 | return; |
| 235 | |
| 236 | const { commits |
| 237 | gitDir: repoDirectory, |
| 238 | ref: `${pushed. |
| 239 | limit: MAX_CLOSING_COMMITS, |
| 240 | }); |
| 241 | await this.references |
| 242 | repository: { id: repositoryId |
| 243 | actorId: pushedBy, |
| 244 | commits, |
| 245 | }); |
| 246 | } |
| 247 | |
| 248 | /** The local cache directory, current with the log. Every transport opens a repository this way before it hands anything to git. */ |
| 249 | async openRepository(repositoryId |
| 250 | const repoDirectory = |
| 251 | await this.materializer |
| 252 | repositoryId, |
| 253 | repoDirectory, |
| 254 | defaultBranch, |
| 255 | ); |
| 256 | return repoDirectory; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | function resultHeaders(service: |
| 261 | return { |
| 262 | 'Content-Type': `application/x-${service |
| 263 | 'Cache-Control': 'no-cache', |
| 264 | }; |
| 265 | } |