|
| 1 | +#!/usr/bin/env bun |
| 2 | +import { readFileSync } from 'node:fs'; |
| 3 | + |
| 4 | +//MARK: Configs |
| 5 | +const protectedBranchPrefixRegex = /^(experiments?|mock(up)?s?|private)\//; |
| 6 | +const allowedRemoteName = 'git.fivem.net'; |
| 7 | + |
| 8 | +//MARK: Parsing push data |
| 9 | +const remoteName = process.argv[2] ?? ''; |
| 10 | +const remoteUrl = process.argv[3] ?? ''; |
| 11 | + |
| 12 | +//No restrictions on the allowed remote |
| 13 | +if (remoteName === allowedRemoteName) { |
| 14 | + process.exit(0); |
| 15 | +} |
| 16 | + |
| 17 | +// Read refs being pushed from stdin. |
| 18 | +const stdin = readFileSync(0, 'utf8'); |
| 19 | +const lines = stdin.split('\n').map((l) => l.trim()).filter(Boolean); |
| 20 | + |
| 21 | +const blocked: string[] = []; |
| 22 | +for (const line of lines) { |
| 23 | + //Parse line |
| 24 | + const parts = line.split(/\s+/); |
| 25 | + if (parts.length < 4) continue; |
| 26 | + const [localRef, localSha, remoteRef, remoteSha] = parts; |
| 27 | + |
| 28 | + // Check branches |
| 29 | + if (!localRef.startsWith('refs/heads/')) continue; |
| 30 | + const branch = localRef.slice('refs/heads/'.length); |
| 31 | + if (protectedBranchPrefixRegex.test(branch)) { |
| 32 | + blocked.push(branch); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +//MARK: Output |
| 37 | +if (!blocked.length) { |
| 38 | + process.exit(0); |
| 39 | +} |
| 40 | + |
| 41 | +//Oh noooo :( |
| 42 | +const msg = [ |
| 43 | + `❌ Push blocked: these branches may only be pushed to ${allowedRemoteName}.`, |
| 44 | + `Remote: ${remoteName} (${remoteUrl})`, |
| 45 | + `Blocked branches:`, |
| 46 | + ...blocked.map((b) => ` - ${b}`), |
| 47 | + ``, |
| 48 | + `Fix: push to ${allowedRemoteName}.` |
| 49 | +].join('\n'); |
| 50 | +process.stderr.write(msg); |
| 51 | +process.exit(1); |
0 commit comments