From 13b09da2cf613a456303ddf5d4ff2af95dde4829 Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Tue, 21 Feb 2023 08:42:44 +1300 Subject: [PATCH] Add post checkout pnpm install to git hooks (#36731) * Add post checkout pnpm install to help devs, disable hooks on pre push. * Disable hooks in all simpleGit instances where checkout is run. --- .husky/post-checkout | 4 ++++ bin/pre-push.sh | 7 ++++--- tools/cli-core/src/git.ts | 15 ++++++++++++--- .../src/commands/analyzer/analyzer-major-minor.ts | 5 ++++- tools/code-analyzer/src/git.ts | 5 ++++- 5 files changed, 28 insertions(+), 8 deletions(-) create mode 100755 .husky/post-checkout diff --git a/.husky/post-checkout b/.husky/post-checkout new file mode 100755 index 00000000000..1fd4a5b5941 --- /dev/null +++ b/.husky/post-checkout @@ -0,0 +1,4 @@ +#!/bin/sh +. "$(dirname "$0")/_/husky.sh" + +pnpm install diff --git a/bin/pre-push.sh b/bin/pre-push.sh index 024e1f21b3a..9651af08fda 100755 --- a/bin/pre-push.sh +++ b/bin/pre-push.sh @@ -31,8 +31,9 @@ if [ $? -ne 0 ]; then exit 1 fi -# Ensure both branches are tracked or check-changelogger-use will fail. -git checkout $PROTECTED_BRANCH --quiet -git checkout $CURRENT_BRANCH --quiet +# Ensure both branches are tracked or check-changelogger-use will fail. Note we pass hooksPath +# to avoid running the pre-commit hook. +git -c core.hooksPath=/dev/null checkout $PROTECTED_BRANCH --quiet +git -c core.hooksPath=/dev/null checkout $CURRENT_BRANCH --quiet php tools/monorepo/check-changelogger-use.php $PROTECTED_BRANCH $CURRENT_BRANCH diff --git a/tools/cli-core/src/git.ts b/tools/cli-core/src/git.ts index cc9467eba2c..9c7f39ad888 100644 --- a/tools/cli-core/src/git.ts +++ b/tools/cli-core/src/git.ts @@ -132,7 +132,10 @@ export const sparseCheckoutRepo = async ( * @return {Response} - the simple-git response. */ export const checkoutRef = ( pathToRepo: string, ref: string ) => { - const git = simpleGit( { baseDir: pathToRepo } ); + const git = simpleGit( { + baseDir: pathToRepo, + config: [ 'core.hooksPath=/dev/null' ], + } ); return git.checkout( ref ); }; @@ -252,7 +255,10 @@ export const getPullRequestNumberFromHash = async ( hash: string ) => { try { - const git = await simpleGit( { baseDir } ); + const git = await simpleGit( { + baseDir, + config: [ 'core.hooksPath=/dev/null' ], + } ); const formerHead = await git.revparse( 'HEAD' ); await git.checkout( hash ); const cmdOutput = await git.raw( [ @@ -294,7 +300,10 @@ export const generateDiff = async ( excludePaths: string[] = [] ) => { try { - const git = simpleGit( { baseDir: tmpRepoPath } ); + const git = simpleGit( { + baseDir: tmpRepoPath, + config: [ 'core.hooksPath=/dev/null' ], + } ); const validBranches = [ hashA, hashB ].filter( ( hash ) => ! refIsHash( hash ) diff --git a/tools/code-analyzer/src/commands/analyzer/analyzer-major-minor.ts b/tools/code-analyzer/src/commands/analyzer/analyzer-major-minor.ts index 1a2c0e2a90f..3a957b40363 100644 --- a/tools/code-analyzer/src/commands/analyzer/analyzer-major-minor.ts +++ b/tools/code-analyzer/src/commands/analyzer/analyzer-major-minor.ts @@ -21,7 +21,10 @@ const getPluginData = async ( pathToMainFile: string, hashOrBranch: string ): Promise< string | void > => { - const git = simpleGit( { baseDir: tmpRepoPath } ); + const git = simpleGit( { + baseDir: tmpRepoPath, + config: [ 'core.hooksPath=/dev/null' ], + } ); await git.checkout( [ hashOrBranch ] ); const mainFile = join( tmpRepoPath, pathToMainFile ); diff --git a/tools/code-analyzer/src/git.ts b/tools/code-analyzer/src/git.ts index 58db3826059..36ba984558f 100644 --- a/tools/code-analyzer/src/git.ts +++ b/tools/code-analyzer/src/git.ts @@ -81,7 +81,10 @@ export const generateSchemaDiff = async ( build: () => Promise< void > | void, error: ( s: string ) => void ): Promise< SchemaDiff[] | null > => { - const git = simpleGit( { baseDir: tmpRepoPath } ); + const git = simpleGit( { + baseDir: tmpRepoPath, + config: [ 'core.hooksPath=/dev/null' ], + } ); // Be sure the wp-env engine is started. await startWPEnv( tmpRepoPath, error );