import * as exec from '@actions/exec' ; import * as io from '@actions/io' ; import { directoryExistsSync } from './utils' ; export async function createCommandManager ( workingDirectory ) { return await GitCommandManager . createCommandManager ( workingDirectory ) ; } class GitCommandManager { private gitPath = '' ; private workingDirectory = '' ; private constructor ( ) { } getWorkingDirectory ( ) { return this . workingDirectory ; } async latestTag ( ) { const revListOutput = await this . execGit ( [ 'rev-list' , '--tags' , '--skip=0' , '--max-count=1' ] ) ; const output = await this . execGit ( [ 'describe' , '--abbrev=0' , '--tags' , revListOutput . stdout . trim ( ) ] ) ; return output . stdout . trim ( ) ; } async initialCommit ( ) { const revListOutput = await this . execGit ( [ 'rev-list' , '--max-parents=0' , 'HEAD' ] ) ; return revListOutput . stdout . trim ( ) ; } static async createCommandManager ( workingDirectory ) { const result = new GitCommandManager ( ) ; await result . initializeCommandManager ( workingDirectory ) ; return result ; } private async execGit ( args , allowAllExitCodes = false , silent = false ) { directoryExistsSync ( this . workingDirectory , true ) ; const result = new GitOutput ( ) ; const stdout = [ ] ; const options = { cwd : this . workingDirectory , silent , ignoreReturnCode : allowAllExitCodes , listeners : { stdout : ( data ) => { stdout . push ( data . toString ( ) ) ; } } } ; result . exitCode = await exec . exec ( `"${ this . gitPath }"` , args , options ) ; result . stdout = stdout . join ( '' ) ; return result ; } private async initializeCommandManager ( workingDirectory ) { this . workingDirectory = workingDirectory ; this . gitPath = await io . which ( 'git' , true ) ; } } class GitOutput { stdout = '' ; exitCode = 0 ; }