Analyzer CLI: Allow commit hashes for comparisons (#33356)
This commit is contained in:
parent
b7931409f2
commit
7fcba06a62
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue