import { Logger } from "../utils/logger" ; import { CpProxy } from "../moduleproxies/cp-proxy" ; import { inject , injectable } from "inversify" ; import { TYPES } from "../utils/constants" ; import { GitCommandArgumentsParser } from "../../dal/git/GitCommandArgumentsParser" ; @ injectable ( ) export class GitIsActiveValidator { constructor ( @ inject ( TYPES . CpProxy ) private readonly cpProxy ) { } public async validate ( workspaceRootPath , gitPath ) { await this . checkVersionCompatibility ( gitPath ) ; await this . checkIsGitRepository ( workspaceRootPath , gitPath ) ; } private async checkVersionCompatibility ( path ) { const version = await this . getVersion ( path ) ; GitIsActiveValidator . checkVersion ( version ) ; } private async getVersion ( path ) { const promiseResult = await this . cpProxy . execAsync ( `"${ path }" --version` ) ; const versionCommandResult = promiseResult . stdout . toString ( "utf8" ) . trim ( ) ; return GitCommandArgumentsParser . parseVersion ( versionCommandResult ) ; } private static checkVersion ( version ) { if ( GitIsActiveValidator . isFirstVersion ( version ) ) { Logger . logWarning ( `GitUtils#collectInfo: git ${ version } installed. TeamCity extension requires git >= 2` ) ; throw new Error ( `Git ${ version } installed. TeamCity extension requires git >= 2` ) ; } } private static isFirstVersion ( version ) { return /^[01]/ . test ( version ) ; } private async checkIsGitRepository ( workspaceRootPath , gitPath ) { const revParseCommand = `"${ gitPath }" -C "${ workspaceRootPath }" rev-parse --show-toplevel` ; Logger . logDebug ( `GitUtils#collectInfo: revParseCommand is ${ revParseCommand }` ) ; try { await this . cpProxy . execAsync ( revParseCommand ) ; } catch ( err ) { throw new Error ( "Git repository was not determined" ) ; } } }