From 3cc47d245d77b63383e4d2e1d3817b7f688cd3bf Mon Sep 17 00:00:00 2001 From: Sam Seay Date: Tue, 13 Dec 2022 10:22:13 +1300 Subject: [PATCH] Allow use of pathspecs to limit the scope of generating a diff in Code Analyzer (#35925) --- tools/cli-core/src/git.ts | 43 ++++++++++++++----- tools/code-analyzer/src/lib/scan-changes.ts | 3 +- .../code-analyzer/src/lib/template-changes.ts | 3 +- 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/tools/cli-core/src/git.ts b/tools/cli-core/src/git.ts index 952e438868a..59edd5f96e5 100644 --- a/tools/cli-core/src/git.ts +++ b/tools/cli-core/src/git.ts @@ -125,13 +125,29 @@ export const checkoutRef = ( pathToRepo: string, ref: string ) => { /** * Do a git diff of 2 commit hashes (or branches) * - * @param {string} baseDir - baseDir that the repo is in - * @param {string} hashA - either a git commit hash or a git branch - * @param {string} hashB - either a git commit hash or a git branch + * @param {string} baseDir - baseDir that the repo is in + * @param {string} hashA - either a git commit hash or a git branch + * @param {string} hashB - either a git commit hash or a git branch + * @param {Array} excludePaths - A list of paths to exclude from the diff * @return {Promise} - diff of the changes between the 2 hashes */ -export const diffHashes = ( baseDir: string, hashA: string, hashB: string ) => { +export const diffHashes = ( + baseDir: string, + hashA: string, + hashB: string, + excludePaths: string[] = [] +) => { const git = simpleGit( { baseDir } ); + + if ( excludePaths.length ) { + return git.diff( [ + `${ hashA }..${ hashB }`, + '--', + '.', + ...excludePaths.map( ( ps ) => `:^${ ps }` ), + ] ); + } + return git.diff( [ `${ hashA }..${ hashB }` ] ); }; @@ -177,16 +193,18 @@ export const getCommitHash = async ( baseDir: string, ref: string ) => { /** * generateDiff generates a diff for a given repo and 2 hashes or branch names. * - * @param {string} tmpRepoPath - filepath to the repo to generate a diff from. - * @param {string} hashA - commit hash or branch name. - * @param {string} hashB - commit hash or branch name. - * @param {Function} onError - the handler to call when an error occurs. + * @param {string} tmpRepoPath - filepath to the repo to generate a diff from. + * @param {string} hashA - commit hash or branch name. + * @param {string} hashB - commit hash or branch name. + * @param {Function} onError - the handler to call when an error occurs. + * @param {Array} excludePaths - A list of directories to exclude from the diff. */ export const generateDiff = async ( tmpRepoPath: string, hashA: string, hashB: string, - onError: ( error: string ) => void + onError: ( error: string ) => void, + excludePaths: string[] = [] ) => { try { const git = simpleGit( { baseDir: tmpRepoPath } ); @@ -213,7 +231,12 @@ export const generateDiff = async ( throw new Error( 'Not a git repository' ); } - const diff = await diffHashes( tmpRepoPath, commitHashA, commitHashB ); + const diff = await diffHashes( + tmpRepoPath, + commitHashA, + commitHashB, + excludePaths + ); return diff; } catch ( e ) { diff --git a/tools/code-analyzer/src/lib/scan-changes.ts b/tools/code-analyzer/src/lib/scan-changes.ts index 4ed137f97d3..f89e95069e9 100644 --- a/tools/code-analyzer/src/lib/scan-changes.ts +++ b/tools/code-analyzer/src/lib/scan-changes.ts @@ -36,7 +36,8 @@ export const scanForChanges = async ( tmpRepoPath, base, compareVersion, - Logger.error + Logger.error, + [ 'tools' ] ); // Only checkout the compare version if we're in CLI mode. diff --git a/tools/code-analyzer/src/lib/template-changes.ts b/tools/code-analyzer/src/lib/template-changes.ts index be3885e104b..abecd850a5a 100644 --- a/tools/code-analyzer/src/lib/template-changes.ts +++ b/tools/code-analyzer/src/lib/template-changes.ts @@ -23,14 +23,15 @@ export const scanForTemplateChanges = ( content: string, version: string ) => { /\./g, '\\.' ) }).*`; + const versionRegex = new RegExp( matchVersion, 'g' ); for ( const p in patches ) { const patch = patches[ p ]; const lines = patch.split( '\n' ); const filePath = getFilename( lines[ 0 ] ); - let code = 'warning'; + let code = 'warning'; let message = 'This template may require a version bump!'; for ( const l in lines ) {