Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix git dir discovery into the precommit hook setup #25870

Merged
merged 4 commits into from
Nov 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/dev/register_git_hook/register_git_hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -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) {
Expand Down