| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { Readable } |
| 3 | |
| 4 | import { RepositoryMaterializerService } |
| 5 | import { PackProcessService } |
| 6 | import type { GitRequestBody |
| 7 | import { |
| 8 | isProbeRequest, |
| 9 | readReceivePackHeader, |
| 10 | } from '../lib/git/protocol/receive-pack-request.js'; |
| 11 | import { RefAdvertisementService } |
| 12 | import { RepositoryStorageService } |
| 13 | import { RepositoryContributionService } |
| 14 | import { PushTransactionService } |
| 15 | import { CodeSearchService } |
| 16 | import { IssueReferencesService } |
| 17 | import { listCommits } |
| 18 | import { resolveDefaultRef } |
| 19 | import { type RefTransition, |
| 20 | import { MAX_CLOSING_COMMITS } |
| 21 | import { isGitServiceName, type |
| 22 | import { UnsupportedGitServiceError } |
| 23 | |
| 24 | export interface GitTransportResponse |
| 25 | headers: Record |
| 26 | body: Readable |
| 27 | } |
| 28 | |
| 29 | interface RepositoryRef { |
| 30 | repositoryId: string; |
| 31 | defaultBranch: string |
| 32 | } |
| 33 | |
| 34 | @Injectable() |
| 35 | export class GitService |
| 36 | private readonly logger |
| 37 | |
| 38 | constructor( |
| 39 | private readonly storage |
| 40 | private readonly refAdvertisement |
| 41 | private readonly packProcess |
| 42 | private readonly pushTransaction |
| 43 | private readonly materializer |
| 44 | private readonly contributions |
| 45 | private readonly codeSearch |
| 46 | private readonly references |
| 47 | ) {} |
| 48 | |
| 49 | async advertiseRefs({ |
| 50 | repositoryId, |
| 51 | defaultBranch, |
| 52 | service, |
| 53 | }: RepositoryRef |
| 54 | if (!isGitServiceName(service |
| 55 | |
| 56 | const repoDirectory = |
| 57 | repositoryId, |
| 58 | defaultBranch, |
| 59 | ); |
| 60 | |
| 61 | return { |
| 62 | headers: { |
| 63 | 'Content-Type': `application/x-${service |
| 64 | 'Cache-Control': 'no-cache', |
| 65 | }, |
| 66 | body: this.refAdvertisement |
| 67 | }; |
| 68 | } |
| 69 | |
| 70 | async uploadPack({ |
| 71 | repositoryId, |
| 72 | defaultBranch, |
| 73 | body, |
| 74 | }: RepositoryRef |
| 75 | const repoDirectory = |
| 76 | repositoryId, |
| 77 | defaultBranch, |
| 78 | ); |
| 79 | |
| 80 | return { |
| 81 | headers: resultHeaders('git-upload-pack |
| 82 | body: this.packProcess |
| 83 | repoDirectory, |
| 84 | input: body.open |
| 85 | }), |
| 86 | }; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * `POST /:username/:repo/git-receive-pack` - a push. |
| 91 | * |
| 92 | * 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. |
| 93 | */ |
| 94 | async receivePack({ |
| 95 | repositoryId, |
| 96 | defaultBranch, |
| 97 | isPublic, |
| 98 | body, |
| 99 | pushedBy = null, |
| 100 | }: RepositoryRef |
| 101 | isPublic: boolean |
| 102 | body: GitRequestBody |
| 103 | pushedBy?: string |
| 104 | }): Promise |
| 105 | if (await isProbeRequest |
| 106 | return { |
| 107 | headers: resultHeaders('git-receive-pack |
| 108 | body: Readable.from |
| 109 | }; |
| 110 | } |
| 111 | |
| 112 | const { transitions, |
| 113 | const repoDirectory = |
| 114 | repositoryId, |
| 115 | defaultBranch, |
| 116 | ); |
| 117 | |
| 118 | await this.pushTransaction |
| 119 | repoId: repositoryId, |
| 120 | transitions, |
| 121 | body, |
| 122 | packOffset, |
| 123 | pushedBy, |
| 124 | }); |
| 125 | |
| 126 | const result = |
| 127 | repoDirectory, |
| 128 | input: body.open |
| 129 | }); |
| 130 | |
| 131 | // 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. |
| 132 | result.once('close |
| 133 | this.codeSearch.indexInBackground |
| 134 | repositoryId, |
| 135 | isPublic, |
| 136 | repoDirectory, |
| 137 | }); |
| 138 | this.contributions |
| 139 | .sync({ repositoryId |
| 140 | .catch((error: |
| 141 | this.logger.warn |
| 142 | `Contribution index update failed for ${repositoryId}: ${ |
| 143 | ), |
| 144 | ); |
| 145 | this.closeFromPush({ |
| 146 | repositoryId, |
| 147 | repoDirectory, |
| 148 | transitions, |
| 149 | pushedBy, |
| 150 | }).catch((error |
| 151 | this.logger.warn |
| 152 | `Closing referenced issues failed for ${repositoryId}: ${ |
| 153 | ), |
| 154 | ); |
| 155 | }); |
| 156 | |
| 157 | return { |
| 158 | headers: resultHeaders('git-receive-pack |
| 159 | body: result, |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | /** Commits that moved the default branch forward close the issues they name, like GitHub's `Fixes #1`. */ |
| 164 | private async closeFromPush |
| 165 | repositoryId, |
| 166 | repoDirectory, |
| 167 | transitions, |
| 168 | pushedBy, |
| 169 | }: Pick |
| 170 | repoDirectory: string |
| 171 | transitions: RefTransition |
| 172 | pushedBy: string |
| 173 | }) { |
| 174 | const defaultRef = |
| 175 | const pushed = |
| 176 | (transition) => |
| 177 | ); |
| 178 | // ponytail: creating or deleting the default branch closes nothing; the first push of a whole history would otherwise close every issue it ever fixed. |
| 179 | if ( |
| 180 | !pushed || |
| 181 | pushed.oldOid.equals |
| 182 | pushed.newOid.equals |
| 183 | ) |
| 184 | return; |
| 185 | |
| 186 | const { commits |
| 187 | gitDir: repoDirectory, |
| 188 | ref: `${pushed. |
| 189 | limit: MAX_CLOSING_COMMITS, |
| 190 | }); |
| 191 | await this.references |
| 192 | repository: { id: repositoryId |
| 193 | actorId: pushedBy, |
| 194 | commits, |
| 195 | }); |
| 196 | } |
| 197 | |
| 198 | /** The local cache directory, current with the log. Every transport opens a repository this way before it hands anything to git. */ |
| 199 | async openRepository(repositoryId |
| 200 | const repoDirectory = |
| 201 | await this.materializer |
| 202 | repositoryId, |
| 203 | repoDirectory, |
| 204 | defaultBranch, |
| 205 | ); |
| 206 | return repoDirectory; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | function resultHeaders(service: |
| 211 | return { |
| 212 | 'Content-Type': `application/x-${service |
| 213 | 'Cache-Control': 'no-cache', |
| 214 | }; |
| 215 | } |