- import {ChildProcess, spawn} from 'child_process';
- import * as fs from 'fs/promises';
-
- export interface GitService {
- create(cwd: string): Promise<ChildProcess>
- delete(cwd: string): Promise<void>
- advertiseReceivePackRefs(cwd: string): Promise<ChildProcess>
- advertiseUploadPackRefs(cwd: string): Promise<ChildProcess>
- receivePack(cwd: string): Promise<ChildProcess>
- uploadPack(cwd: string): Promise<ChildProcess>
- }
-
- export class GitServiceImpl implements GitService {
- private static isWindows() {
- return /^win/.test(process.platform);
- }
-
- async create(cwd: string): Promise<ChildProcess> {
- await fs.mkdir(cwd, { recursive: true });
- return spawn('git', ['init', '--bare'], { cwd });
- }
-
- async delete(cwd: string): Promise<void> {
- await fs.rm(cwd, { recursive: true, force: true, });
- }
-
- async advertiseReceivePackRefs(cwd: string): Promise<ChildProcess> {
- const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack';
- const commonArgs = ['--stateless-rpc', '--advertise-refs', cwd];
- const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs;
- return spawn(command, args);
- }
-
- async advertiseUploadPackRefs(cwd: string): Promise<ChildProcess> {
- const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack';
- const commonArgs = ['--stateless-rpc', '--advertise-refs', cwd];
- const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs;
- return spawn(command, args);
- }
-
- async receivePack(cwd: string): Promise<ChildProcess> {
- const command = GitServiceImpl.isWindows() ? 'git' : 'git-receive-pack';
- const commonArgs = ['--stateless-rpc', cwd];
- const args = GitServiceImpl.isWindows() ? ['receive-pack', ...commonArgs] : commonArgs;
- return spawn(command, args);
- }
-
- async uploadPack(cwd: string): Promise<ChildProcess> {
- const command = GitServiceImpl.isWindows() ? 'git' : 'git-upload-pack';
- const commonArgs = ['--stateless-rpc', cwd];
- const args = GitServiceImpl.isWindows() ? ['upload-pack', ...commonArgs] : commonArgs;
- return spawn(command, args);
- }
- }
|