diff --git a/src/dev/register_git_hook/register_git_hook.js b/src/dev/register_git_hook/register_git_hook.js index e0a119b33e4210..670a3cb2b3aeaf 100644 --- a/src/dev/register_git_hook/register_git_hook.js +++ b/src/dev/register_git_hook/register_git_hook.js @@ -22,13 +22,23 @@ import { chmod, unlink, writeFile } from 'fs'; import dedent from 'dedent'; import { resolve } from 'path'; import { promisify } from 'util'; +import SimpleGit from 'simple-git'; import { REPO_ROOT } from '../constants'; +const simpleGit = new SimpleGit(REPO_ROOT); + const chmodAsync = promisify(chmod); +const gitRevParseAsync = promisify(simpleGit.revparse.bind(simpleGit)); const unlinkAsync = promisify(unlink); const writeFileAsync = promisify(writeFile); -const PRECOMMIT_GIT_HOOK_SCRIPT_PATH = resolve(REPO_ROOT, '.git/hooks/pre-commit'); +async function getPrecommitGitHookScriptPath(rootPath) { + // Retrieves the correct location for the .git dir for + // every git setup (including git worktree) + const gitDirPath = (await gitRevParseAsync(['--git-dir'])).trim(); + + return resolve(rootPath, gitDirPath, 'hooks/pre-commit'); +} function getKbnPrecommitGitHookScript(rootPath) { return dedent(` @@ -69,7 +79,7 @@ export async function registerPrecommitGitHook(log) { try { await writeGitHook( - PRECOMMIT_GIT_HOOK_SCRIPT_PATH, + await getPrecommitGitHookScriptPath(REPO_ROOT), getKbnPrecommitGitHookScript(REPO_ROOT) ); } catch (e) {