| 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 { RepositoryMaterializerService } |
| 17 | import { RepositoryAccessService } |
| 18 | import { PushTransactionService } |
| 19 | import { BranchNotFoundError } |
| 20 | import type { BranchDTO, |
| 21 | import { |
| 22 | BranchAlreadyExistsError, |
| 23 | BranchInUseError, |
| 24 | BranchSourceNotFoundError, |
| 25 | DefaultBranchDeletionError, |
| 26 | InvalidBranchNameError, |
| 27 | } from '../../lib/branches/branches.errors.js'; |
| 28 | import { listBranches } |
| 29 | |
| 30 | type RepositoryRef |
| 31 | |
| 32 | /** Creating and deleting branches from the API. Both are ref-only entries in the log: every object they point at is already there. */ |
| 33 | @Injectable() |
| 34 | export class RepositoryBranchesService |
| 35 | constructor( |
| 36 | @Inject(DATABASE) |
| 37 | private readonly access |
| 38 | private readonly materializer |
| 39 | private readonly pushTransaction |
| 40 | ) {} |
| 41 | |
| 42 | async createBranch({ |
| 43 | body, |
| 44 | ...target |
| 45 | }: RepositoryRef |
| 46 | const repository = |
| 47 | ...target, |
| 48 | operation: 'write', |
| 49 | }); |
| 50 | if (!isValidRefName(' |
| 51 | throw new InvalidBranchNameError( |
| 52 | } |
| 53 | |
| 54 | const directory = |
| 55 | const [branches, |
| 56 | listBranches(directory), |
| 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 | ...target, |
| 93 | operation: 'write', |
| 94 | }); |
| 95 | const directory = |
| 96 | |
| 97 | if (!(await |
| 98 | throw new BranchNotFoundError( |
| 99 | } |
| 100 | const ref = |
| 101 | const defaultRef = |
| 102 | gitDir: directory, |
| 103 | defaultBranch: repository.defaultBranch |
| 104 | }); |
| 105 | if (ref === |
| 106 | |
| 107 | const [pullRequest] |
| 108 | .select({ number: |
| 109 | .from(schema.pullRequest |
| 110 | .innerJoin(schema.issue |
| 111 | .where( |
| 112 | and( |
| 113 | eq(schema.pullRequest |
| 114 | or( |
| 115 | and( |
| 116 | eq(schema.pullRequest |
| 117 | eq(schema.pullRequest |
| 118 | ), |
| 119 | and( |
| 120 | eq(schema.pullRequest |
| 121 | eq(schema.pullRequest |
| 122 | ), |
| 123 | ), |
| 124 | ), |
| 125 | ) |
| 126 | .limit(1); |
| 127 | if (pullRequest) throw |
| 128 | |
| 129 | const sha = |
| 130 | if (!sha) |
| 131 | |
| 132 | // Pinned to the tip read above, so a push that moved the branch in between wins and this fails with a conflict. |
| 133 | await this.commitRef |
| 134 | ref, |
| 135 | oldOid: Buffer.from |
| 136 | newOid: ZERO_OID, |
| 137 | }); |
| 138 | } |
| 139 | |
| 140 | private commitRef( |
| 141 | repository: AuthorizedRepository |
| 142 | requesterId: string, |
| 143 | transition: RefTransition |
| 144 | ) { |
| 145 | return this.pushTransaction |
| 146 | repoId: repository.id |
| 147 | transitions: [transition], |
| 148 | body: bufferBody(Buffer |
| 149 | packOffset: 0, |
| 150 | pushedBy: requesterId, |
| 151 | }); |
| 152 | } |
| 153 | } |