fix(dev-infra): fix commit message validation in git worktrees (#36507)

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
This commit is contained in:
George Kalpakas 2020-04-08 23:56:05 +03:00 committed by atscott
parent e526f74dfd
commit 34aa5570ed
3 changed files with 26 additions and 6 deletions

View File

@ -14,9 +14,29 @@ export function buildCommitMessageParser(localYargs: yargs.Argv) {
return localYargs.help() return localYargs.help()
.strict() .strict()
.command( .command(
'pre-commit-validate', 'Validate the most recent commit message', {}, 'pre-commit-validate', 'Validate the most recent commit message', {
() => { 'file': {
validateFile('.git/COMMIT_EDITMSG'); type: 'string',
conflicts: ['file-env-variable'],
description: 'The path of the commit message file.',
},
'file-env-variable': {
type: 'string',
conflicts: ['file'],
description:
'The key of the environment variable for the path of the commit message file.',
coerce: arg => {
const file = process.env[arg];
if (!file) {
throw new Error(`Provided environment variable "${arg}" was not found.`);
}
return file;
},
}
},
args => {
const file = args.file || args.fileEnvVariable || '.git/COMMIT_EDITMSG';
validateFile(file);
}) })
.command( .command(
'validate-range', 'Validate a range of commit messages', { 'validate-range', 'Validate a range of commit messages', {

View File

@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license * found in the LICENSE file at https://angular.io/license
*/ */
import {readFileSync} from 'fs'; import {readFileSync} from 'fs';
import {join} from 'path'; import {resolve} from 'path';
import {getRepoBaseDir} from '../utils/config'; import {getRepoBaseDir} from '../utils/config';
@ -14,7 +14,7 @@ import {validateCommitMessage} from './validate';
/** Validate commit message at the provided file path. */ /** Validate commit message at the provided file path. */
export function validateFile(filePath: string) { export function validateFile(filePath: string) {
const commitMessage = readFileSync(join(getRepoBaseDir(), filePath), 'utf8'); const commitMessage = readFileSync(resolve(getRepoBaseDir(), filePath), 'utf8');
if (validateCommitMessage(commitMessage)) { if (validateCommitMessage(commitMessage)) {
console.info('√ Valid commit message'); console.info('√ Valid commit message');
return; return;

View File

@ -199,7 +199,7 @@
"cldr-data-coverage": "full", "cldr-data-coverage": "full",
"husky": { "husky": {
"hooks": { "hooks": {
"commit-msg": "yarn -s ng-dev commit-message pre-commit-validate" "commit-msg": "yarn -s ng-dev commit-message pre-commit-validate --file-env-variable HUSKY_GIT_PARAMS"
} }
} }
} }