| 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 { isGitServiceName, type |
| 17 | import { UnsupportedGitServiceError } |
| 18 | |
| 19 | export interface GitTransportResponse |
| 20 | headers: Record |
| 21 | body: Readable |
| 22 | } |
| 23 | |
| 24 | interface RepositoryRef { |
| 25 | repositoryId: string; |
| 26 | } |
| 27 | |
| 28 | @Injectable() |
| 29 | export class GitService |
| 30 | private readonly logger |
| 31 | |
| 32 | constructor( |
| 33 | private readonly storage |
| 34 | private readonly refAdvertisement |
| 35 | private readonly packProcess |
| 36 | private readonly pushTransaction |
| 37 | private readonly materializer |
| 38 | private readonly contributions |
| 39 | private readonly codeSearch |
| 40 | ) {} |
| 41 | |
| 42 | async advertiseRefs({ |
| 43 | repositoryId, |
| 44 | service, |
| 45 | }: RepositoryRef |
| 46 | if (!isGitServiceName(service |
| 47 | |
| 48 | const repoDirectory = |
| 49 | |
| 50 | return { |
| 51 | headers: { |
| 52 | 'Content-Type': `application/x-${service |
| 53 | 'Cache-Control': 'no-cache', |
| 54 | }, |
| 55 | body: this.refAdvertisement |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | async uploadPack({ |
| 60 | repositoryId, |
| 61 | body, |
| 62 | }: RepositoryRef |
| 63 | const repoDirectory = |
| 64 | |
| 65 | return { |
| 66 | headers: resultHeaders('git-upload-pack |
| 67 | body: this.packProcess |
| 68 | repoDirectory, |
| 69 | input: body.open |
| 70 | }), |
| 71 | }; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * `POST /:username/:repo/git-receive-pack` - a push. |
| 76 | * |
| 77 | * 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. |
| 78 | */ |
| 79 | async receivePack({ |
| 80 | repositoryId, |
| 81 | isPublic, |
| 82 | body, |
| 83 | pushedBy = null, |
| 84 | }: RepositoryRef |
| 85 | isPublic: boolean |
| 86 | body: GitRequestBody |
| 87 | pushedBy?: string |
| 88 | }): Promise |
| 89 | if (await isProbeRequest |
| 90 | return { |
| 91 | headers: resultHeaders('git-receive-pack |
| 92 | body: Readable.from |
| 93 | }; |
| 94 | } |
| 95 | |
| 96 | const { transitions, |
| 97 | const repoDirectory = |
| 98 | |
| 99 | await this.pushTransaction |
| 100 | repoId: repositoryId, |
| 101 | transitions, |
| 102 | body, |
| 103 | packOffset, |
| 104 | pushedBy, |
| 105 | }); |
| 106 | |
| 107 | const result = |
| 108 | repoDirectory, |
| 109 | input: body.open |
| 110 | }); |
| 111 | |
| 112 | // 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. |
| 113 | result.once('close |
| 114 | this.codeSearch.indexInBackground |
| 115 | repositoryId, |
| 116 | isPublic, |
| 117 | repoDirectory, |
| 118 | }); |
| 119 | this.contributions |
| 120 | .sync({ repositoryId |
| 121 | .catch((error: |
| 122 | this.logger.warn |
| 123 | `Contribution index update failed for ${repositoryId}: ${ |
| 124 | ), |
| 125 | ); |
| 126 | }); |
| 127 | |
| 128 | return { |
| 129 | headers: resultHeaders('git-receive-pack |
| 130 | body: result, |
| 131 | }; |
| 132 | } |
| 133 | |
| 134 | /** The local cache directory, current with the log. Every transport opens a repository this way before it hands anything to git. */ |
| 135 | async openRepository(repositoryId |
| 136 | const repoDirectory = |
| 137 | await this.materializer |
| 138 | return repoDirectory; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | function resultHeaders(service: |
| 143 | return { |
| 144 | 'Content-Type': `application/x-${service |
| 145 | 'Cache-Control': 'no-cache', |
| 146 | }; |
| 147 | } |