Analyzer CLI: Allow commit hashes for comparisons (#33356)

This commit is contained in:
Paul Sealock 2022-06-10 10:31:39 +12:00 committed by GitHub
parent b7931409f2
commit 7fcba06a62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -10,7 +10,7 @@ import { readFileSync } from 'fs';
/**
* Internal dependencies
*/
import { startWPEnv, stopWPEnv } from './utils';
import { startWPEnv, stopWPEnv, isValidCommitHash } from './utils';
/**
* Fetch branches from origin.
@ -35,13 +35,20 @@ export const fetchBranch = (
return true;
}
if ( isValidCommitHash( branch ) ) {
// The hash is valid and available in history. No need to fetch anything.
return true;
}
try {
// Fetch branch.
execSync( `git fetch origin ${ branch }` );
// Create branch.
execSync( `git branch ${ branch } origin/${ branch }` );
} catch ( e ) {
error( `Unable to fetch ${ branch }` );
error(
`Unable to fetch ${ branch }. Supply a valid branch name or commit hash.`
);
return false;
}

View File

@ -174,3 +174,22 @@ export const stopWPEnv = ( error: ( s: string ) => void ): boolean => {
return false;
}
};
/**
* Check if branch string is actually a commit hash and exists in git history.
*
* @param {string } branch branch name or commit hash.
* @return {boolean} If string is valid commit hash.
*/
export const isValidCommitHash = ( branch: string ): boolean => {
try {
// See if hash is valid and exists in the history.
execSync( `git show -s ${ branch }`, {
encoding: 'utf-8',
} );
return true;
} catch ( e ) {
// git show -s produces an error if the string is not a valid hash.
return false;
}
};