Allow use of pathspecs to limit the scope of generating a diff in Code Analyzer (#35925)

This commit is contained in:
Sam Seay 2022-12-13 10:22:13 +13:00 committed by GitHub
parent 613e58c061
commit 3cc47d245d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 12 deletions

View File

@ -128,10 +128,26 @@ export const checkoutRef = ( pathToRepo: string, ref: string ) => {
* @param {string} baseDir - baseDir that the repo is in * @param {string} baseDir - baseDir that the repo is in
* @param {string} hashA - either a git commit hash or a git branch * @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} hashB - either a git commit hash or a git branch
* @param {Array<string>} excludePaths - A list of paths to exclude from the diff
* @return {Promise<string>} - diff of the changes between the 2 hashes * @return {Promise<string>} - 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 } ); const git = simpleGit( { baseDir } );
if ( excludePaths.length ) {
return git.diff( [
`${ hashA }..${ hashB }`,
'--',
'.',
...excludePaths.map( ( ps ) => `:^${ ps }` ),
] );
}
return git.diff( [ `${ hashA }..${ hashB }` ] ); return git.diff( [ `${ hashA }..${ hashB }` ] );
}; };
@ -181,12 +197,14 @@ export const getCommitHash = async ( baseDir: string, ref: string ) => {
* @param {string} hashA - commit hash or branch name. * @param {string} hashA - commit hash or branch name.
* @param {string} hashB - 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 {Function} onError - the handler to call when an error occurs.
* @param {Array<string>} excludePaths - A list of directories to exclude from the diff.
*/ */
export const generateDiff = async ( export const generateDiff = async (
tmpRepoPath: string, tmpRepoPath: string,
hashA: string, hashA: string,
hashB: string, hashB: string,
onError: ( error: string ) => void onError: ( error: string ) => void,
excludePaths: string[] = []
) => { ) => {
try { try {
const git = simpleGit( { baseDir: tmpRepoPath } ); const git = simpleGit( { baseDir: tmpRepoPath } );
@ -213,7 +231,12 @@ export const generateDiff = async (
throw new Error( 'Not a git repository' ); throw new Error( 'Not a git repository' );
} }
const diff = await diffHashes( tmpRepoPath, commitHashA, commitHashB ); const diff = await diffHashes(
tmpRepoPath,
commitHashA,
commitHashB,
excludePaths
);
return diff; return diff;
} catch ( e ) { } catch ( e ) {

View File

@ -36,7 +36,8 @@ export const scanForChanges = async (
tmpRepoPath, tmpRepoPath,
base, base,
compareVersion, compareVersion,
Logger.error Logger.error,
[ 'tools' ]
); );
// Only checkout the compare version if we're in CLI mode. // Only checkout the compare version if we're in CLI mode.

View File

@ -23,14 +23,15 @@ export const scanForTemplateChanges = ( content: string, version: string ) => {
/\./g, /\./g,
'\\.' '\\.'
) }).*`; ) }).*`;
const versionRegex = new RegExp( matchVersion, 'g' ); const versionRegex = new RegExp( matchVersion, 'g' );
for ( const p in patches ) { for ( const p in patches ) {
const patch = patches[ p ]; const patch = patches[ p ];
const lines = patch.split( '\n' ); const lines = patch.split( '\n' );
const filePath = getFilename( lines[ 0 ] ); const filePath = getFilename( lines[ 0 ] );
let code = 'warning';
let code = 'warning';
let message = 'This template may require a version bump!'; let message = 'This template may require a version bump!';
for ( const l in lines ) { for ( const l in lines ) {