import {ChildProcess} from 'child_process'; import {spawn} from '../../utils/process'; import {mkdirp, unlink} from '../../utils/fs'; export interface GitService { create(repoBasePath: string): Promise delete(repoBasePath: string): Promise advertiseReceivePackRefs(repoBasePath: string): Promise advertiseUploadPackRefs(repoBasePath: string): Promise receivePack(repoBasePath: string): Promise uploadPack(repoBasePath: string): Promise } export class GitServiceImpl implements GitService { private static isWindows() { return /^win/.test(process.platform); } async create(repoBasePath: string): Promise { await mkdirp(repoBasePath); return spawn( repoBasePath, 'git', ['init', '--bare'], ); } async delete(repoBasePath: string): Promise { await unlink(repoBasePath); } async advertiseReceivePackRefs(repoBasePath: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack'; const commonArgs = ['--stateless-rpc', '--advertise-refs', '.']; const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs; return spawn(repoBasePath, command, args); } async advertiseUploadPackRefs(repoBasePath: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack'; const commonArgs = ['--stateless-rpc', '--advertise-refs', '.']; const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs; return spawn(repoBasePath, command, args); } async receivePack(repoBasePath: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack'; const commonArgs = ['--stateless-rpc', '.']; const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs; return spawn(repoBasePath, command, args); } async uploadPack(repoBasePath: string): Promise { const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack'; const commonArgs = ['--stateless-rpc', '.']; const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs; return spawn(repoBasePath, command, args); } }