|
| 1 | +import { type SpawnSyncReturns, spawnSync } from 'node:child_process'; |
| 2 | +import { readFileSync } from 'node:fs'; |
| 3 | +import path from 'node:path'; |
| 4 | + |
| 5 | +export const generatedDirectoryName = '_'; |
| 6 | +export const ownerFileName = '.owner'; |
| 7 | + |
| 8 | +export type FailedHooksResult = { |
| 9 | + status: 'failed'; |
| 10 | + reason: string; |
| 11 | + message: string; |
| 12 | +}; |
| 13 | + |
| 14 | +export type GitContext = { |
| 15 | + defaultHooksDirectory: string; |
| 16 | + effectiveHooksDirectory: string; |
| 17 | + gitRoot: string; |
| 18 | + projectPath: string; |
| 19 | +}; |
| 20 | + |
| 21 | +export type HooksPathScope = |
| 22 | + 'command' | 'worktree' | 'local' | 'global' | 'system'; |
| 23 | + |
| 24 | +export const fail = (reason: string, message: string): FailedHooksResult => ({ |
| 25 | + status: 'failed', |
| 26 | + reason, |
| 27 | + message, |
| 28 | +}); |
| 29 | + |
| 30 | +export const runGit = (cwd: string, args: string[]): SpawnSyncReturns<string> => |
| 31 | + spawnSync('git', args, { cwd, encoding: 'utf8' }); |
| 32 | + |
| 33 | +const removeLineEnding = (value: string): string => |
| 34 | + value.replace(/\r?\n$/u, ''); |
| 35 | + |
| 36 | +export const gitFailure = ( |
| 37 | + error: NodeJS.ErrnoException | undefined, |
| 38 | + stderr: string, |
| 39 | +): FailedHooksResult => { |
| 40 | + if (error?.code === 'ENOENT') { |
| 41 | + return fail('git-not-found', 'Git command not found.'); |
| 42 | + } |
| 43 | + |
| 44 | + return fail( |
| 45 | + 'git-command-failed', |
| 46 | + `Failed to run Git: ${error?.message || stderr.trim()}`, |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +export const resolveHooksPathScope = ( |
| 51 | + cwd: string, |
| 52 | +): HooksPathScope | FailedHooksResult | undefined => { |
| 53 | + const configured = runGit(cwd, [ |
| 54 | + 'config', |
| 55 | + '--show-scope', |
| 56 | + '--get', |
| 57 | + 'core.hooksPath', |
| 58 | + ]); |
| 59 | + if (configured.error || configured.status === null) { |
| 60 | + return gitFailure(configured.error, configured.stderr); |
| 61 | + } |
| 62 | + |
| 63 | + // Exit status 1 means core.hooksPath is not configured yet. |
| 64 | + if (configured.status === 1) { |
| 65 | + return undefined; |
| 66 | + } |
| 67 | + if (configured.status !== 0) { |
| 68 | + return fail( |
| 69 | + 'git-config-failed', |
| 70 | + `Failed to resolve the core.hooksPath scope: ${configured.stderr.trim()}`, |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + const separator = configured.stdout.indexOf('\t'); |
| 75 | + const scope = separator === -1 ? '' : configured.stdout.slice(0, separator); |
| 76 | + if ( |
| 77 | + scope === 'command' || |
| 78 | + scope === 'worktree' || |
| 79 | + scope === 'local' || |
| 80 | + scope === 'global' || |
| 81 | + scope === 'system' |
| 82 | + ) { |
| 83 | + return scope; |
| 84 | + } |
| 85 | + |
| 86 | + return fail( |
| 87 | + 'git-config-failed', |
| 88 | + 'Failed to resolve the core.hooksPath scope.', |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +export const resolveGitHooksPath = ( |
| 93 | + cwd: string, |
| 94 | +): string | FailedHooksResult => { |
| 95 | + const hooksDirectory = runGit(cwd, [ |
| 96 | + 'rev-parse', |
| 97 | + '--path-format=absolute', |
| 98 | + '--git-path', |
| 99 | + 'hooks', |
| 100 | + ]); |
| 101 | + if (hooksDirectory.error || hooksDirectory.status === null) { |
| 102 | + return gitFailure(hooksDirectory.error, hooksDirectory.stderr); |
| 103 | + } |
| 104 | + if (hooksDirectory.status !== 0) { |
| 105 | + return fail( |
| 106 | + 'git-command-failed', |
| 107 | + `Failed to resolve the Git hooks path: ${hooksDirectory.stderr.trim()}`, |
| 108 | + ); |
| 109 | + } |
| 110 | + |
| 111 | + const resolvedDirectory = removeLineEnding(hooksDirectory.stdout); |
| 112 | + if (!resolvedDirectory) { |
| 113 | + return fail('git-command-failed', 'Failed to resolve the Git hooks path.'); |
| 114 | + } |
| 115 | + return resolvedDirectory; |
| 116 | +}; |
| 117 | + |
| 118 | +export const resolveGitContext = ( |
| 119 | + cwd: string, |
| 120 | +): |
| 121 | + | GitContext |
| 122 | + | FailedHooksResult |
| 123 | + | { status: 'skipped'; reason: 'not-git-repository' } => { |
| 124 | + // Resolve every repository path in one Git process. `--git-path hooks` |
| 125 | + // accounts for the effective core.hooksPath configuration across Git scopes. |
| 126 | + const repository = runGit(cwd, [ |
| 127 | + 'rev-parse', |
| 128 | + '--is-inside-work-tree', |
| 129 | + '--path-format=absolute', |
| 130 | + '--show-toplevel', |
| 131 | + '--show-prefix', |
| 132 | + '--git-common-dir', |
| 133 | + '--git-path', |
| 134 | + 'hooks', |
| 135 | + ]); |
| 136 | + if (repository.error || repository.status === null) { |
| 137 | + return gitFailure(repository.error, repository.stderr); |
| 138 | + } |
| 139 | + |
| 140 | + const [ |
| 141 | + insideWorkTree = '', |
| 142 | + gitRoot = '', |
| 143 | + repositoryPrefix = '', |
| 144 | + gitCommonDirectory = '', |
| 145 | + effectiveHooksDirectory = '', |
| 146 | + ] = removeLineEnding(repository.stdout).split(/\r?\n/u); |
| 147 | + |
| 148 | + if (insideWorkTree !== 'true') { |
| 149 | + return { status: 'skipped', reason: 'not-git-repository' }; |
| 150 | + } |
| 151 | + |
| 152 | + if (repository.status !== 0) { |
| 153 | + return fail( |
| 154 | + 'git-command-failed', |
| 155 | + `Failed to resolve the Git repository paths: ${repository.stderr.trim()}`, |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + if (!gitRoot || !gitCommonDirectory || !effectiveHooksDirectory) { |
| 160 | + return fail( |
| 161 | + 'git-command-failed', |
| 162 | + 'Failed to resolve the Git repository paths.', |
| 163 | + ); |
| 164 | + } |
| 165 | + |
| 166 | + return { |
| 167 | + defaultHooksDirectory: path.join(gitCommonDirectory, 'hooks'), |
| 168 | + effectiveHooksDirectory, |
| 169 | + gitRoot, |
| 170 | + projectPath: |
| 171 | + repositoryPrefix.replaceAll('\\', '/').replace(/\/$/u, '') || '.', |
| 172 | + }; |
| 173 | +}; |
| 174 | + |
| 175 | +export const isSamePath = (first: string, second: string): boolean => |
| 176 | + path.resolve(first) === path.resolve(second); |
| 177 | + |
| 178 | +export const readOwner = (directory: string): string | undefined => { |
| 179 | + try { |
| 180 | + const content = readFileSync(path.join(directory, ownerFileName), 'utf8'); |
| 181 | + const owner = removeLineEnding(content); |
| 182 | + return content === `${owner}\n` && |
| 183 | + owner.length > 0 && |
| 184 | + !/[\r\n]/u.test(owner) |
| 185 | + ? owner |
| 186 | + : undefined; |
| 187 | + } catch { |
| 188 | + return undefined; |
| 189 | + } |
| 190 | +}; |
| 191 | + |
| 192 | +export const displayPath = (gitRoot: string, filePath: string): string => { |
| 193 | + const relativePath = path.relative(gitRoot, filePath).replaceAll('\\', '/'); |
| 194 | + return relativePath.length > 0 && !relativePath.startsWith('../') |
| 195 | + ? relativePath |
| 196 | + : filePath; |
| 197 | +}; |
0 commit comments