Previously, the `pre-commit-validate` command (used in the `commit-msg` git hook) assumed that the commit message was stored in `.git/COMMIT_EDITMSG` file. This is usually true, but not when using [git worktrees](https://git-scm.com/docs/git-worktree), where `.git` is a file containing the path to the actual git directory. This commit fixes it by taking advantage of the fact that git passes the actual path of the file holding the commit message to the `commit-msg` hook and husky exposes the arguments passed by git as `$HUSKY_GIT_PARAMS`. NOTE: We cannot use the environment variable directly in the `commit-msg` hook command, because environment variables need to be referenced differently on Windows (`%VAR_NAME%`) vs macOS/Linux (`$VAR_NAME`). Instead, we pass the name of the environment variable and the validation script reads the variable's value off of `process.env`. PR Close #36507
25 lines
750 B
TypeScript
25 lines
750 B
TypeScript
/**
|
|
* @license
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
*
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
* found in the LICENSE file at https://angular.io/license
|
|
*/
|
|
import {readFileSync} from 'fs';
|
|
import {resolve} from 'path';
|
|
|
|
import {getRepoBaseDir} from '../utils/config';
|
|
|
|
import {validateCommitMessage} from './validate';
|
|
|
|
/** Validate commit message at the provided file path. */
|
|
export function validateFile(filePath: string) {
|
|
const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
|
|
if (validateCommitMessage(commitMessage)) {
|
|
console.info('√ Valid commit message');
|
|
return;
|
|
}
|
|
// If the validation did not return true, exit as a failure.
|
|
process.exit(1);
|
|
}
|