license import exec , { ExecResult } from './util/exec' ; import { existsSync } from './util/fs' ; import path = require ( 'path' ) ; export class GitRepo { dir : string ; constructor ( dir ) { this . dir = dir ; } /**
     * Returns true if directory exists and its own git repo.
     */ isGit ( ) { return existsSync ( path . join ( this . dir , '.git' ) ) ; } /**
     * Returns the git commit hash at HEAD.
     */ async getHeadSha ( ) { return ( await exec ( this . dir , 'git' , [ 'rev-parse' , 'HEAD' ] ) ) . stdout ; } /**
     * Run `git clone [url] [this.dir]`.
     */ async clone ( url ) { return await exec ( process . cwd ( ) , 'git' , [ 'clone' , url , this . dir , '--depth' , '1' , '--no-single-branch' ] ) ; } /**
     * Run `git fetch [remoteName]`. If remoteName is not given, git will fetch
     * from default.
     */ async fetch ( remoteName ? ) { const commandArgs = remoteName ? [ 'fetch' , remoteName , '--depth' , '1' ] : [ 'fetch' , '--depth' , '1' ] ; return await exec ( this . dir , 'git' , commandArgs ) ; } /**
     * Run `git checkout [branch] --`.
     */ async checkout ( branch ) { return await exec ( this . dir , 'git' , [ 'checkout' , branch , '--' ] ) ; } /**
     * Resets the repo back to a clean state. Note that this deletes any
     * uncommitted changes and untracked files in the repo directory, created
     * through tooling or otherwise.
     */ async destroyAllUncommittedChangesAndFiles ( ) { const resetResult = await exec ( this . dir , 'git' , [ 'reset' , '--hard' ] ) ; const cleanResult = await exec ( this . dir , 'git' , [ 'clean' , '-fd' ] ) ; return { reset : resetResult , clean : cleanResult , } ; } /**
     * Run `git commit -am [message]`.
     */ async commit ( message ) { return await exec ( this . dir , 'git' , [ 'commit' , '-a' , '-m' , message ] ) ; } /**
     * Run `git checkout -b [branch]`.
     */ async createBranch ( branch ) { return await exec ( this . dir , 'git' , [ 'checkout' , '-b' , branch ] ) ; } /**
     * Run `git push` with options to push to a specific branch & --force.
     */ async pushCurrentBranchToOrigin ( originBranch ? , forcePush = false ) { return await exec ( this . dir , 'git' , [ 'push' , 'origin' , originBranch ? `HEAD:${ originBranch }` : `HEAD` , ... ( forcePush ? [ '--force' ] : [ ] ) ] ) ; } }