-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Spencer
authored
May 8, 2020
1 parent
88d9f17
commit 91b4c74
Showing
7 changed files
with
190 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import Path from 'path'; | ||
import { chmod, writeFile } from 'fs'; | ||
import { promisify } from 'util'; | ||
|
||
import { run } from '../run'; | ||
import { REPO_ROOT } from '../repo_root'; | ||
import { SCRIPT_SOURCE } from './script_source'; | ||
import { getGitDir } from './get_git_dir'; | ||
|
||
const chmodAsync = promisify(chmod); | ||
const writeFileAsync = promisify(writeFile); | ||
|
||
run( | ||
async ({ log }) => { | ||
try { | ||
const gitDir = await getGitDir(); | ||
const installPath = Path.resolve(REPO_ROOT, gitDir, 'hooks/pre-commit'); | ||
|
||
log.info(`Registering Kibana pre-commit git hook...`); | ||
await writeFileAsync(installPath, SCRIPT_SOURCE); | ||
await chmodAsync(installPath, 0o755); | ||
log.success(`Kibana pre-commit git hook was installed successfully.`); | ||
} catch (e) { | ||
log.error(`Kibana pre-commit git hook was not installed as an error occur.`); | ||
throw e; | ||
} | ||
}, | ||
{ | ||
description: 'Register git hooks in the local repo', | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
packages/kbn-dev-utils/src/precommit_hook/script_source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import os from 'os'; | ||
|
||
import normalizePath from 'normalize-path'; | ||
|
||
const HOME_DIR = normalizePath(os.homedir()); | ||
|
||
export const SCRIPT_SOURCE = `#!/usr/bin/env bash | ||
# | ||
# ** THIS IS AN AUTO-GENERATED FILE ** | ||
# ** PLEASE DO NOT CHANGE IT MANUALLY ** | ||
# | ||
# GENERATED BY \`node scripts/register_git_hook\` | ||
# IF YOU WANNA CHANGE SOMETHING IN THIS SCRIPT | ||
# PLEASE RE-RUN 'yarn kbn bootstrap' or 'node scripts/register_git_hook' | ||
# pre-commit script takes zero arguments: https://git-scm.com/docs/githooks#_pre_commit | ||
set -euo pipefail | ||
# Make it possible to terminate pre commit hook | ||
# using ctrl-c so nothing else would happen or be | ||
# sent to the output. | ||
# | ||
# The correct exit code on that situation | ||
# according the linux documentation project is 130 | ||
# https://www.tldp.org/LDP/abs/html/exitcodes.html | ||
trap "exit 130" INT | ||
has_node() { | ||
command -v node >/dev/null 2>&1 | ||
} | ||
has_nvm() { | ||
command -v nvm >/dev/null 2>&1 | ||
} | ||
try_load_node_from_nvm_paths () { | ||
# If nvm is not loaded, load it | ||
has_node || { | ||
NVM_SH="${HOME_DIR}/.nvm/nvm.sh" | ||
if [ "${process.platform}" == "darwin" ] && [ -s "$(brew --prefix nvm)/nvm.sh" ]; then | ||
NVM_SH="$(brew --prefix nvm)/nvm.sh" | ||
fi | ||
export NVM_DIR="${HOME_DIR}/.nvm" | ||
[ -s "$NVM_SH" ] && \. "$NVM_SH" | ||
# If nvm has been loaded correctly, use project .nvmrc | ||
has_nvm && nvm use | ||
} | ||
} | ||
extend_user_path() { | ||
if [ "${process.platform}" == "win32" ]; then | ||
export PATH="$PATH:/c/Program Files/nodejs" | ||
else | ||
export PATH="$PATH:/usr/local/bin:/usr/local" | ||
try_load_node_from_nvm_paths | ||
fi | ||
} | ||
# Extend path with common path locations for node | ||
# in order to make the hook working on git GUI apps | ||
extend_user_path | ||
# Check if we have node js bin in path | ||
has_node || { | ||
echo "Can't found node bin in the PATH. Please update the PATH to proceed." | ||
echo "If your PATH already has the node bin, maybe you are using some git GUI app." | ||
echo "Can't found node bin in the PATH. Please update the PATH to proceed." | ||
echo "If your PATH already has the node bin, maybe you are using some git GUI app not launched from the shell." | ||
echo "In order to proceed, you need to config the PATH used by the application that are launching your git GUI app." | ||
echo "If you are running macOS, you can do that using:" | ||
echo "'sudo launchctl config user path /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin'" | ||
exit 1 | ||
} | ||
execute_precommit_hook() { | ||
node scripts/precommit_hook || return 1 | ||
PRECOMMIT_FILE="./.git/hooks/pre-commit.local" | ||
if [ -x "\${PRECOMMIT_FILE}" ]; then | ||
echo "Executing local precommit hook found in \${PRECOMMIT_FILE}" | ||
"$PRECOMMIT_FILE" || return 1 | ||
fi | ||
} | ||
execute_precommit_hook || { | ||
echo "Pre-commit hook failed (add --no-verify to bypass)"; | ||
echo ' For eslint failures you can try running \`node scripts/precommit_hook --fix\`'; | ||
exit 1; | ||
} | ||
exit 0 | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters