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