Skip to content

Commit

Permalink
[Build] fail silently on register git hook failure
Browse files Browse the repository at this point in the history
`node scripts/register_git_hook` fails depending on the installed
git version because it ends up calling `git rev-parse --git-common-dir`.
Depending on the git version, `--git-common-dir` returns the literal
string `--git-common-dir` when it is suppose to return $GIT_COMMON_DIR
if set and $GIT_DIR if the previous is not set. Then it proceeds to try
to install from a path that doesn't exist because it will use
`--git-common-dir` in the path.

The goal for this fix is to bypass hard failures in 1.x since I do
not believe it is appropriate to fail building something due to git.
But I do not want to just remove adding the pre-commit git hook
if people are already used to it being installed while bootstrapping.

Here, I propose the removal of git hooks on bootstrap in a 2+ version
of Dashboards:
opensearch-project#851

Issue resolved:
opensearch-project#850

Signed-off-by: Kawika Avilla <[email protected]>
  • Loading branch information
kavilla committed Oct 11, 2021
1 parent e4c40db commit 38ace12
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/osd-dev-utils/src/precommit_hook/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,36 @@ import { promisify } from 'util';
import { REPO_ROOT } from '@osd/utils';

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

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

function validateGitDir(gitDir: string) {
if (gitDir === '--git-common-dir') {
throw createFailError(
`--git-common-dir not accessible on current git version. Skipping installation of pre-commit git hook.`
);
}
}

run(
async ({ log }) => {
try {
const gitDir = await getGitDir();
validateGitDir(gitDir);
const installPath = Path.resolve(REPO_ROOT, gitDir, 'hooks/pre-commit');

log.info(`Registering OpenSearch Dashboards pre-commit git hook...`);
await writeFileAsync(installPath, SCRIPT_SOURCE);
await chmodAsync(installPath, 0o755);
log.success(`OpenSearch Dashboards pre-commit git hook was installed successfully.`);
} catch (e) {
if (isFailError(e)) {
return;
}
log.error(`OpenSearch Dashboards pre-commit git hook was not installed as an error occur.`);
throw e;
}
Expand Down

0 comments on commit 38ace12

Please sign in to comment.