Skip to content

Commit

Permalink
Fix git dir discovery into the precommit hook setup (#25870) (#25980)
Browse files Browse the repository at this point in the history
* fix(NA): git dir discovery into the precommt hook setup.

* refact(NA): promisify gitrevparse function.

* docs(NA): fix typo on comments
  • Loading branch information
mistic authored Nov 21, 2018
1 parent 696c49b commit d9b81ce
Showing 1 changed file with 12 additions and 2 deletions.
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

0 comments on commit d9b81ce

Please sign in to comment.