Skip to content

Commit

Permalink
chore(NA): check git version on pre-commit hook install (elastic#84811)
Browse files Browse the repository at this point in the history
* chore(NA): checks installed git version when installing pre-commit hook

* chore(NA): throw an error instead of log a warning

* chore(NA): use createFailError instead

* fix(NA): apply feedback from pr review for isCorrectGitVersionInstalled

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
mistic and kibanamachine committed Dec 4, 2020
1 parent bdbcb16 commit 87f8d7e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/kbn-dev-utils/src/precommit_hook/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,22 @@ import { promisify } from 'util';
import { REPO_ROOT } from '@kbn/utils';

import { run } from '../run';
import { createFailError } from '../run';
import { SCRIPT_SOURCE } from './script_source';
import { getGitDir } from './get_git_dir';
import { getGitDir, isCorrectGitVersionInstalled } from './git_utils';

const chmodAsync = promisify(chmod);
const writeFileAsync = promisify(writeFile);

run(
async ({ log }) => {
try {
if (!(await isCorrectGitVersionInstalled())) {
throw createFailError(
`We could not detect a git version in the required range. Please install a git version >= 2.5. Skipping Kibana pre-commit git hook installation.`
);
}

const gitDir = await getGitDir();
const installPath = Path.resolve(REPO_ROOT, gitDir, 'hooks/pre-commit');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,20 @@ export async function getGitDir() {
})
).stdout.trim();
}

// Checks if a correct git version is installed
export async function isCorrectGitVersionInstalled() {
const rawGitVersionStr = (
await execa('git', ['--version'], {
cwd: REPO_ROOT,
})
).stdout.trim();

const match = rawGitVersionStr.match(/[0-9]+(\.[0-9]+)+/);
if (!match) {
return false;
}

const [major, minor] = match[0].split('.').map((n) => parseInt(n, 10));
return major > 2 || (major === 2 && minor >= 5);
}

0 comments on commit 87f8d7e

Please sign in to comment.