|
| 1 | +/* |
| 2 | + * SonarQube CLI |
| 3 | + * Copyright (C) 2026 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | + |
| 21 | +// Git repository abstraction for hook installation: root dir, hooks path, and framework detection. |
| 22 | + |
| 23 | +import { existsSync, statSync } from 'node:fs'; |
| 24 | +import { isAbsolute, join } from 'node:path'; |
| 25 | +import { CommandFailedError } from './error'; |
| 26 | +import { spawnProcess } from '../../../lib/process'; |
| 27 | + |
| 28 | +const PRE_COMMIT_CONFIG_FILE = '.pre-commit-config.yaml'; |
| 29 | +export const toForwardSlash = (p: string) => p.replaceAll('\\', '/'); |
| 30 | + |
| 31 | +/** |
| 32 | + * Resolves the directory git uses for hooks (core.hooksPath or .git/hooks). |
| 33 | + */ |
| 34 | +export async function resolveGitHooksDir(root: string): Promise<string> { |
| 35 | + let configResult; |
| 36 | + try { |
| 37 | + configResult = await spawnProcess('git', ['config', 'core.hooksPath'], { cwd: root }); |
| 38 | + } catch { |
| 39 | + configResult = null; |
| 40 | + } |
| 41 | + if (configResult?.exitCode === 0) { |
| 42 | + const configured = configResult.stdout.trim(); |
| 43 | + if (configured) { |
| 44 | + return isAbsolute(configured) ? configured : join(root, configured); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + const dotGit = join(root, '.git'); |
| 49 | + try { |
| 50 | + if (statSync(dotGit).isDirectory()) { |
| 51 | + return join(dotGit, 'hooks'); |
| 52 | + } |
| 53 | + } catch { |
| 54 | + // .git is a file (worktree) or missing — use git rev-parse |
| 55 | + } |
| 56 | + |
| 57 | + let result; |
| 58 | + try { |
| 59 | + result = await spawnProcess('git', ['rev-parse', '--git-path', 'hooks'], { cwd: root }); |
| 60 | + } catch (error) { |
| 61 | + const message = error instanceof Error ? error.message : String(error); |
| 62 | + throw new CommandFailedError(`Failed to run git [${message}]`); |
| 63 | + } |
| 64 | + if (result.exitCode !== 0) { |
| 65 | + const detail = [result.stderr, result.stdout].filter((s) => s.length > 0).join('\n'); |
| 66 | + throw new CommandFailedError( |
| 67 | + `Could not resolve git hooks directory (exit code ${result.exitCode}) ${detail}`, |
| 68 | + ); |
| 69 | + } |
| 70 | + const resolved = result.stdout.trim(); |
| 71 | + return isAbsolute(resolved) ? resolved : join(root, resolved); |
| 72 | +} |
| 73 | + |
| 74 | +/** |
| 75 | + * Represents a git repository at a given root. Use to decide hook installation strategy |
| 76 | + * without resolving all state up front (e.g. only resolve hooks dir when not using pre-commit). |
| 77 | + */ |
| 78 | +export class GitRepo { |
| 79 | + readonly rootDir: string; |
| 80 | + private _hooksDir: Promise<string> | null = null; |
| 81 | + |
| 82 | + constructor(rootDir: string) { |
| 83 | + this.rootDir = rootDir; |
| 84 | + } |
| 85 | + |
| 86 | + /** True if the repo uses the pre-commit framework (.pre-commit-config.yaml). */ |
| 87 | + usesPreCommitFramework(): boolean { |
| 88 | + return existsSync(join(this.rootDir, PRE_COMMIT_CONFIG_FILE)); |
| 89 | + } |
| 90 | + |
| 91 | + private async getHooksDirOnce(): Promise<string> { |
| 92 | + this._hooksDir ??= resolveGitHooksDir(this.rootDir); |
| 93 | + return this._hooksDir; |
| 94 | + } |
| 95 | + |
| 96 | + /** True if git's hooks path points to .husky (Husky is in use). */ |
| 97 | + async usesHusky(): Promise<boolean> { |
| 98 | + const hooksDir = await this.getHooksDirOnce(); |
| 99 | + return toForwardSlash(hooksDir).startsWith(toForwardSlash(join(this.rootDir, '.husky'))); |
| 100 | + } |
| 101 | + |
| 102 | + /** Resolved git hooks directory (core.hooksPath or .git/hooks). */ |
| 103 | + async getHooksDir(): Promise<string> { |
| 104 | + return this.getHooksDirOnce(); |
| 105 | + } |
| 106 | + |
| 107 | + /** Path to the Husky hook file for the given hook name (e.g. 'pre-commit', 'pre-push'). */ |
| 108 | + getHuskyHookPath(hook: string): string { |
| 109 | + return join(this.rootDir, '.husky', hook); |
| 110 | + } |
| 111 | +} |
0 commit comments