| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { bufferBody } |
| 7 | import type { AuthorizedRepository |
| 8 | import { isValidRefName } |
| 9 | import { listTags } |
| 10 | import { |
| 11 | resolveCommit, |
| 12 | resolveDefaultRef, |
| 13 | resolveTargetCommit, |
| 14 | } from '../../lib/git/tree/resolve-ref.js'; |
| 15 | import { type RefTransition, |
| 16 | import { BranchesService } |
| 17 | import { RepositoryMaterializerService } |
| 18 | import { RepositoryAccessService } |
| 19 | import { RepositoryStorageService } |
| 20 | import { PushTransactionService } |
| 21 | import { BranchNotFoundError } |
| 22 | import type { BranchDTO, |
| 23 | import { |
| 24 | BranchAlreadyExistsError, |
| 25 | BranchInUseError, |
| 26 | BranchSourceNotFoundError, |
| 27 | DefaultBranchDeletionError, |
| 28 | InvalidBranchNameError, |
| 29 | } from './branches.errors.js'; |
| 30 | |
| 31 | type RepositoryRef |
| 32 | |
| 33 | /** Creating and deleting branches from the API. Both are ref-only entries in the log: every object they point at is already there. */ |
| 34 | @Injectable() |
| 35 | export class RepositoryBranchesService |
| 36 | constructor( |
| 37 | @Inject(DATABASE) |
| 38 | private readonly access |
| 39 | private readonly storage |
| 40 | private readonly materializer |
| 41 | private readonly branches |
| 42 | private readonly pushTransaction |
| 43 | ) {} |
| 44 | |
| 45 | async createBranch({ |
| 46 | body, |
| 47 | ...target |
| 48 | }: RepositoryRef |
| 49 | const repository = |
| 50 | if (!(await |
| 51 | throw new InvalidBranchNameError( |
| 52 | } |
| 53 | |
| 54 | const directory = |
| 55 | const [branches, |
| 56 | this.branches.getGitBranches |
| 57 | listTags(directory), |
| 58 | ]); |
| 59 | if (branches.includes |
| 60 | throw new BranchAlreadyExistsError( |
| 61 | } |
| 62 | |
| 63 | const from = |
| 64 | const sha = |
| 65 | gitDir: directory, |
| 66 | defaultBranch: repository.defaultBranch |
| 67 | branches, |
| 68 | tags, |
| 69 | requested: from, |
| 70 | }); |
| 71 | if (!sha) |
| 72 | throw new BranchSourceNotFoundError( |
| 73 | from ?? repository. |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | // A concurrent create of the same name loses the compare-and-swap with a conflict. |
| 78 | await this.commitRef |
| 79 | ref: `refs/heads/${body |
| 80 | oldOid: ZERO_OID, |
| 81 | newOid: Buffer.from |
| 82 | }); |
| 83 | return { name: |
| 84 | } |
| 85 | |
| 86 | /** Refuses the default branch and any branch an open pull request compares, which would otherwise be left pointing at nothing. */ |
| 87 | async deleteBranch({ |
| 88 | branch, |
| 89 | ...target |
| 90 | }: RepositoryRef |
| 91 | const repository = |
| 92 | const directory = |
| 93 | |
| 94 | if (!(await |
| 95 | throw new BranchNotFoundError( |
| 96 | } |
| 97 | const ref = |
| 98 | const defaultRef = |
| 99 | gitDir: directory, |
| 100 | defaultBranch: repository.defaultBranch |
| 101 | }); |
| 102 | if (ref === |
| 103 | |
| 104 | const [pullRequest] |
| 105 | .select({ number: |
| 106 | .from(schema.pullRequest |
| 107 | .innerJoin(schema.issue |
| 108 | .where( |
| 109 | and( |
| 110 | eq(schema.pullRequest |
| 111 | or( |
| 112 | and( |
| 113 | eq(schema.pullRequest |
| 114 | eq(schema.pullRequest |
| 115 | ), |
| 116 | and( |
| 117 | eq(schema.pullRequest |
| 118 | eq(schema.pullRequest |
| 119 | ), |
| 120 | ), |
| 121 | ), |
| 122 | ) |
| 123 | .limit(1); |
| 124 | if (pullRequest) throw |
| 125 | |
| 126 | const sha = |
| 127 | if (!sha) |
| 128 | |
| 129 | // Pinned to the tip read above, so a push that moved the branch in between wins and this fails with a conflict. |
| 130 | await this.commitRef |
| 131 | ref, |
| 132 | oldOid: Buffer.from |
| 133 | newOid: ZERO_OID, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | private commitRef( |
| 138 | repository: AuthorizedRepository |
| 139 | requesterId: string, |
| 140 | transition: RefTransition |
| 141 | ) { |
| 142 | return this.pushTransaction |
| 143 | repoId: repository.id |
| 144 | transitions: [transition], |
| 145 | body: bufferBody(Buffer |
| 146 | packOffset: 0, |
| 147 | pushedBy: requesterId, |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | private async openCache |
| 152 | const directory = |
| 153 | await this.materializer |
| 154 | repository.id, |
| 155 | directory, |
| 156 | repository.defaultBranch, |
| 157 | ); |
| 158 | return directory; |
| 159 | } |
| 160 | |
| 161 | private authorize({ |
| 162 | return this.access |
| 163 | username, |
| 164 | repo, |
| 165 | actor: { userId: requesterId |
| 166 | operation: 'write', |
| 167 | }); |
| 168 | } |
| 169 | } |