| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { atLeast } |
| 3 | import { Inject, Injectable |
| 4 | import { and, asc |
| 5 | |
| 6 | import { DATABASE } |
| 7 | import { |
| 8 | LfsLockForbiddenError, |
| 9 | LfsLockNotFoundError, |
| 10 | } from '../../../lib/git/lfs/lfs.errors.js'; |
| 11 | import type { AuthorizedRepository |
| 12 | import { isoTimestamp } |
| 13 | |
| 14 | const DEFAULT_PAGE = 100 |
| 15 | |
| 16 | const lockColumns = { |
| 17 | id: schema.lfsLock |
| 18 | path: schema.lfsLock |
| 19 | locked_at: isoTimestamp(schema |
| 20 | owner: { |
| 21 | name: sql<string |
| 22 | }, |
| 23 | }; |
| 24 | |
| 25 | /** Git LFS's file locking API. Locks are advisory: git-lfs checks them before a push, the server does not. */ |
| 26 | @Injectable() |
| 27 | export class LfsLocksService |
| 28 | constructor(@Inject( |
| 29 | |
| 30 | /** The new lock, or with `created` false the one already holding the path. */ |
| 31 | async create(repositoryId |
| 32 | const [created] |
| 33 | .insert(schema.lfsLock |
| 34 | .values({ repositoryId |
| 35 | .onConflictDoNothing() |
| 36 | .returning({ id: |
| 37 | const [lock] |
| 38 | and( |
| 39 | eq(schema.lfsLock |
| 40 | eq(schema.lfsLock |
| 41 | ), |
| 42 | ); |
| 43 | // the holder unlocked between the conflict and the read |
| 44 | if (!lock) |
| 45 | return { lock, |
| 46 | } |
| 47 | |
| 48 | /** Ordered by path, which is unique per repository and so serves as the cursor. */ |
| 49 | async list( |
| 50 | repositoryId: string, |
| 51 | { |
| 52 | path, |
| 53 | id, |
| 54 | cursor, |
| 55 | limit = DEFAULT_PAGE, |
| 56 | }: { path |
| 57 | ) { |
| 58 | const rows = |
| 59 | and( |
| 60 | eq(schema.lfsLock |
| 61 | path === undefined ? |
| 62 | id === undefined ? |
| 63 | cursor === undefined ? |
| 64 | ), |
| 65 | ) |
| 66 | .orderBy(asc(schema |
| 67 | .limit(limit + |
| 68 | const locks = |
| 69 | return { |
| 70 | locks, |
| 71 | next_cursor: rows.length |
| 72 | }; |
| 73 | } |
| 74 | |
| 75 | /** What git-lfs asks before a push: the locks the pusher holds, and everyone else's. */ |
| 76 | async verify( |
| 77 | repositoryId: string, |
| 78 | userId: string, |
| 79 | page: { |
| 80 | ) { |
| 81 | const { locks, |
| 82 | const owned = |
| 83 | .select({ id: |
| 84 | .from(schema.lfsLock |
| 85 | .where( |
| 86 | and( |
| 87 | eq(schema.lfsLock |
| 88 | eq(schema.lfsLock |
| 89 | ), |
| 90 | ); |
| 91 | const ours = |
| 92 | return { |
| 93 | ours: locks.filter |
| 94 | theirs: locks.filter |
| 95 | next_cursor, |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | /** Only the owner may unlock, or an admin who forces it. */ |
| 100 | async unlock( |
| 101 | repository: AuthorizedRepository |
| 102 | userId: string, |
| 103 | id: string, |
| 104 | force = false, |
| 105 | ) { |
| 106 | const [held] |
| 107 | .select({ ownerId: |
| 108 | .from(schema.lfsLock |
| 109 | .where( |
| 110 | and( |
| 111 | eq(schema.lfsLock |
| 112 | eq(schema.lfsLock |
| 113 | ), |
| 114 | ); |
| 115 | if (!held) |
| 116 | if ( |
| 117 | held.ownerId !== |
| 118 | !(force && atLeast |
| 119 | ) { |
| 120 | throw new LfsLockForbiddenError() |
| 121 | } |
| 122 | |
| 123 | const [lock] |
| 124 | await this.db |
| 125 | return { lock |
| 126 | } |
| 127 | |
| 128 | private select(where |
| 129 | return this.db |
| 130 | .select(lockColumns) |
| 131 | .from(schema.lfsLock |
| 132 | .innerJoin(schema.user |
| 133 | .where(where) |
| 134 | .$dynamic(); |
| 135 | } |
| 136 | } |