From 255ec828f6a447b2a5539f730f2a1f310c3c9b64 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Mon, 19 Nov 2018 15:51:12 +0000 Subject: [PATCH 1/3] fix(NA): git dir discovery into the precommt hook setup. --- .../register_git_hook/register_git_hook.js | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/dev/register_git_hook/register_git_hook.js b/src/dev/register_git_hook/register_git_hook.js index e0a119b33e4210..92e97077b8064b 100644 --- a/src/dev/register_git_hook/register_git_hook.js +++ b/src/dev/register_git_hook/register_git_hook.js @@ -22,13 +22,32 @@ 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 unlinkAsync = promisify(unlink); const writeFileAsync = promisify(writeFile); -const PRECOMMIT_GIT_HOOK_SCRIPT_PATH = resolve(REPO_ROOT, '.git/hooks/pre-commit'); +async function getPrecommitGitHookScriptPath(rootPath) { + return new Promise((pResolve, pReject) => { + simpleGit.revparse(['--git-dir'], (error, gitDirPath) => { + if (error) { + return pReject(error); + } + + pResolve( + resolve( + rootPath, + gitDirPath.trim(), + 'hooks/pre-commit' + ) + ); + }); + }); +} function getKbnPrecommitGitHookScript(rootPath) { return dedent(` @@ -69,7 +88,7 @@ export async function registerPrecommitGitHook(log) { try { await writeGitHook( - PRECOMMIT_GIT_HOOK_SCRIPT_PATH, + await getPrecommitGitHookScriptPath(REPO_ROOT), getKbnPrecommitGitHookScript(REPO_ROOT) ); } catch (e) { From eea43f850ad5e13a3816ed7c7da8c2e9b16809c5 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 20 Nov 2018 16:29:23 +0000 Subject: [PATCH 2/3] refact(NA): promisify gitrevparse function. --- .../register_git_hook/register_git_hook.js | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/dev/register_git_hook/register_git_hook.js b/src/dev/register_git_hook/register_git_hook.js index 92e97077b8064b..16b1958e4383ed 100644 --- a/src/dev/register_git_hook/register_git_hook.js +++ b/src/dev/register_git_hook/register_git_hook.js @@ -28,25 +28,16 @@ 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); async function getPrecommitGitHookScriptPath(rootPath) { - return new Promise((pResolve, pReject) => { - simpleGit.revparse(['--git-dir'], (error, gitDirPath) => { - if (error) { - return pReject(error); - } + // gets the correct location for the .git dir for + // every git setup (including git worktree) + const gitDirPath = (await gitRevParseAsync(['--git-dir'])).trim(); - pResolve( - resolve( - rootPath, - gitDirPath.trim(), - 'hooks/pre-commit' - ) - ); - }); - }); + return resolve(rootPath, gitDirPath, 'hooks/pre-commit'); } function getKbnPrecommitGitHookScript(rootPath) { From 2b0adf7856a45732b9bcf5a6e6f1c49c7951be97 Mon Sep 17 00:00:00 2001 From: Tiago Costa Date: Tue, 20 Nov 2018 16:34:59 +0000 Subject: [PATCH 3/3] docs(NA): fix typo on comments --- src/dev/register_git_hook/register_git_hook.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dev/register_git_hook/register_git_hook.js b/src/dev/register_git_hook/register_git_hook.js index 16b1958e4383ed..670a3cb2b3aeaf 100644 --- a/src/dev/register_git_hook/register_git_hook.js +++ b/src/dev/register_git_hook/register_git_hook.js @@ -33,7 +33,7 @@ const unlinkAsync = promisify(unlink); const writeFileAsync = promisify(writeFile); async function getPrecommitGitHookScriptPath(rootPath) { - // gets the correct location for the .git dir for + // Retrieves the correct location for the .git dir for // every git setup (including git worktree) const gitDirPath = (await gitRevParseAsync(['--git-dir'])).trim();